jcmaster.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * jcmaster.c
  3. *
  4. * Copyright (C) 1991-1995, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains master control logic for the JPEG compressor.
  9. * These routines are concerned with parameter validation, initial setup,
  10. * and inter-pass control (determining the number of passes and the work
  11. * to be done in each pass).
  12. */
  13. #define JPEG_INTERNALS
  14. #include "jinclude.h"
  15. #include "jpeglib.h"
  16. /* Private state */
  17. typedef enum {
  18. main_pass, /* input data, also do first output step */
  19. huff_opt_pass, /* Huffman code optimization pass */
  20. output_pass /* data output pass */
  21. } c_pass_type;
  22. typedef struct {
  23. struct jpeg_comp_master pub;/* public fields */
  24. c_pass_type pass_type; /* the type of the current pass */
  25. int pass_number; /* # of passes completed */
  26. int total_passes; /* total # of passes needed */
  27. int scan_number; /* current index in scan_info[] */
  28. } my_comp_master;
  29. typedef my_comp_master * my_master_ptr;
  30. /*
  31. * Support routines that do various essential calculations.
  32. */
  33. LOCAL void
  34. initial_setup( j_compress_ptr cinfo ) {
  35. /* Do computations that are needed before master selection phase */
  36. int ci;
  37. jpeg_component_info * compptr;
  38. long samplesperrow;
  39. JDIMENSION jd_samplesperrow;
  40. /* Sanity check on image dimensions */
  41. if ( ( cinfo->image_height <= 0 ) || ( cinfo->image_width <= 0 )
  42. || ( cinfo->num_components <= 0 ) || ( cinfo->input_components <= 0 ) ) {
  43. ERREXIT( cinfo, JERR_EMPTY_IMAGE );
  44. }
  45. /* Make sure image isn't bigger than I can handle */
  46. if ( ( (long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ) ||
  47. ( (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION ) ) {
  48. ERREXIT1( cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION );
  49. }
  50. /* Width of an input scanline must be representable as JDIMENSION. */
  51. samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
  52. jd_samplesperrow = (JDIMENSION) samplesperrow;
  53. if ( (long) jd_samplesperrow != samplesperrow ) {
  54. ERREXIT( cinfo, JERR_WIDTH_OVERFLOW );
  55. }
  56. /* For now, precision must match compiled-in value... */
  57. if ( cinfo->data_precision != BITS_IN_JSAMPLE ) {
  58. ERREXIT1( cinfo, JERR_BAD_PRECISION, cinfo->data_precision );
  59. }
  60. /* Check that number of components won't exceed internal array sizes */
  61. if ( cinfo->num_components > MAX_COMPONENTS ) {
  62. ERREXIT2( cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  63. MAX_COMPONENTS );
  64. }
  65. /* Compute maximum sampling factors; check factor validity */
  66. cinfo->max_h_samp_factor = 1;
  67. cinfo->max_v_samp_factor = 1;
  68. for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  69. ci++, compptr++ ) {
  70. if ( ( compptr->h_samp_factor <= 0 ) || ( compptr->h_samp_factor > MAX_SAMP_FACTOR ) ||
  71. ( compptr->v_samp_factor <= 0 ) || ( compptr->v_samp_factor > MAX_SAMP_FACTOR ) ) {
  72. ERREXIT( cinfo, JERR_BAD_SAMPLING );
  73. }
  74. cinfo->max_h_samp_factor = MAX( cinfo->max_h_samp_factor,
  75. compptr->h_samp_factor );
  76. cinfo->max_v_samp_factor = MAX( cinfo->max_v_samp_factor,
  77. compptr->v_samp_factor );
  78. }
  79. /* Compute dimensions of components */
  80. for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  81. ci++, compptr++ ) {
  82. /* Fill in the correct component_index value; don't rely on application */
  83. compptr->component_index = ci;
  84. /* For compression, we never do DCT scaling. */
  85. compptr->DCT_scaled_size = DCTSIZE;
  86. /* Size in DCT blocks */
  87. compptr->width_in_blocks = (JDIMENSION)
  88. jdiv_round_up( (long) cinfo->image_width * (long) compptr->h_samp_factor,
  89. (long) ( cinfo->max_h_samp_factor * DCTSIZE ) );
  90. compptr->height_in_blocks = (JDIMENSION)
  91. jdiv_round_up( (long) cinfo->image_height * (long) compptr->v_samp_factor,
  92. (long) ( cinfo->max_v_samp_factor * DCTSIZE ) );
  93. /* Size in samples */
  94. compptr->downsampled_width = (JDIMENSION)
  95. jdiv_round_up( (long) cinfo->image_width * (long) compptr->h_samp_factor,
  96. (long) cinfo->max_h_samp_factor );
  97. compptr->downsampled_height = (JDIMENSION)
  98. jdiv_round_up( (long) cinfo->image_height * (long) compptr->v_samp_factor,
  99. (long) cinfo->max_v_samp_factor );
  100. /* Mark component needed (this flag isn't actually used for compression) */
  101. compptr->component_needed = TRUE;
  102. }
  103. /* Compute number of fully interleaved MCU rows (number of times that
  104. * main controller will call coefficient controller).
  105. */
  106. cinfo->total_iMCU_rows = (JDIMENSION)
  107. jdiv_round_up( (long) cinfo->image_height,
  108. (long) ( cinfo->max_v_samp_factor * DCTSIZE ) );
  109. }
  110. #ifdef C_MULTISCAN_FILES_SUPPORTED
  111. LOCAL void
  112. validate_script( j_compress_ptr cinfo ) {
  113. /* Verify that the scan script in cinfo->scan_info[] is valid; also
  114. * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
  115. */
  116. const jpeg_scan_info * scanptr;
  117. int scanno, ncomps, ci, coefi, thisi;
  118. int Ss, Se, Ah, Al;
  119. boolean component_sent[MAX_COMPONENTS];
  120. #ifdef C_PROGRESSIVE_SUPPORTED
  121. int * last_bitpos_ptr;
  122. int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
  123. /* -1 until that coefficient has been seen; then last Al for it */
  124. #endif
  125. if ( cinfo->num_scans <= 0 ) {
  126. ERREXIT1( cinfo, JERR_BAD_SCAN_SCRIPT, 0 );
  127. }
  128. /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
  129. * for progressive JPEG, no scan can have this.
  130. */
  131. scanptr = cinfo->scan_info;
  132. if ( ( scanptr->Ss != 0 ) || ( scanptr->Se != DCTSIZE2 - 1 ) ) {
  133. #ifdef C_PROGRESSIVE_SUPPORTED
  134. cinfo->progressive_mode = TRUE;
  135. last_bitpos_ptr = &last_bitpos[0][0];
  136. for ( ci = 0; ci < cinfo->num_components; ci++ ) {
  137. for ( coefi = 0; coefi < DCTSIZE2; coefi++ ) {
  138. *last_bitpos_ptr++ = -1;
  139. }
  140. }
  141. #else
  142. ERREXIT( cinfo, JERR_NOT_COMPILED );
  143. #endif
  144. } else {
  145. cinfo->progressive_mode = FALSE;
  146. for ( ci = 0; ci < cinfo->num_components; ci++ ) {
  147. component_sent[ci] = FALSE;
  148. }
  149. }
  150. for ( scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++ ) {
  151. /* Validate component indexes */
  152. ncomps = scanptr->comps_in_scan;
  153. if ( ( ncomps <= 0 ) || ( ncomps > MAX_COMPS_IN_SCAN ) ) {
  154. ERREXIT2( cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN );
  155. }
  156. for ( ci = 0; ci < ncomps; ci++ ) {
  157. thisi = scanptr->component_index[ci];
  158. if ( ( thisi < 0 ) || ( thisi >= cinfo->num_components ) ) {
  159. ERREXIT1( cinfo, JERR_BAD_SCAN_SCRIPT, scanno );
  160. }
  161. /* Components must appear in SOF order within each scan */
  162. if ( ( ci > 0 ) && ( thisi <= scanptr->component_index[ci - 1] ) ) {
  163. ERREXIT1( cinfo, JERR_BAD_SCAN_SCRIPT, scanno );
  164. }
  165. }
  166. /* Validate progression parameters */
  167. Ss = scanptr->Ss;
  168. Se = scanptr->Se;
  169. Ah = scanptr->Ah;
  170. Al = scanptr->Al;
  171. if ( cinfo->progressive_mode ) {
  172. #ifdef C_PROGRESSIVE_SUPPORTED
  173. if ( ( Ss < 0 ) || ( Ss >= DCTSIZE2 ) || ( Se < Ss ) || ( Se >= DCTSIZE2 ) ||
  174. ( Ah < 0 ) || ( Ah > 13 ) || ( Al < 0 ) || ( Al > 13 ) ) {
  175. ERREXIT1( cinfo, JERR_BAD_PROG_SCRIPT, scanno );
  176. }
  177. if ( Ss == 0 ) {
  178. if ( Se != 0 ) {/* DC and AC together not OK */
  179. ERREXIT1( cinfo, JERR_BAD_PROG_SCRIPT, scanno );
  180. }
  181. } else {
  182. if ( ncomps != 1 ) {/* AC scans must be for only one component */
  183. ERREXIT1( cinfo, JERR_BAD_PROG_SCRIPT, scanno );
  184. }
  185. }
  186. for ( ci = 0; ci < ncomps; ci++ ) {
  187. last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0];
  188. if ( ( Ss != 0 ) && ( last_bitpos_ptr[0] < 0 ) ) {/* AC without prior DC scan */
  189. ERREXIT1( cinfo, JERR_BAD_PROG_SCRIPT, scanno );
  190. }
  191. for ( coefi = Ss; coefi <= Se; coefi++ ) {
  192. if ( last_bitpos_ptr[coefi] < 0 ) {
  193. /* first scan of this coefficient */
  194. if ( Ah != 0 ) {
  195. ERREXIT1( cinfo, JERR_BAD_PROG_SCRIPT, scanno );
  196. }
  197. } else {
  198. /* not first scan */
  199. if ( ( Ah != last_bitpos_ptr[coefi] ) || ( Al != Ah - 1 ) ) {
  200. ERREXIT1( cinfo, JERR_BAD_PROG_SCRIPT, scanno );
  201. }
  202. }
  203. last_bitpos_ptr[coefi] = Al;
  204. }
  205. }
  206. #endif
  207. } else {
  208. /* For sequential JPEG, all progression parameters must be these: */
  209. if ( ( Ss != 0 ) || ( Se != DCTSIZE2 - 1 ) || ( Ah != 0 ) || ( Al != 0 ) ) {
  210. ERREXIT1( cinfo, JERR_BAD_PROG_SCRIPT, scanno );
  211. }
  212. /* Make sure components are not sent twice */
  213. for ( ci = 0; ci < ncomps; ci++ ) {
  214. thisi = scanptr->component_index[ci];
  215. if ( component_sent[thisi] ) {
  216. ERREXIT1( cinfo, JERR_BAD_SCAN_SCRIPT, scanno );
  217. }
  218. component_sent[thisi] = TRUE;
  219. }
  220. }
  221. }
  222. /* Now verify that everything got sent. */
  223. if ( cinfo->progressive_mode ) {
  224. #ifdef C_PROGRESSIVE_SUPPORTED
  225. /* For progressive mode, we only check that at least some DC data
  226. * got sent for each component; the spec does not require that all bits
  227. * of all coefficients be transmitted. Would it be wiser to enforce
  228. * transmission of all coefficient bits??
  229. */
  230. for ( ci = 0; ci < cinfo->num_components; ci++ ) {
  231. if ( last_bitpos[ci][0] < 0 ) {
  232. ERREXIT( cinfo, JERR_MISSING_DATA );
  233. }
  234. }
  235. #endif
  236. } else {
  237. for ( ci = 0; ci < cinfo->num_components; ci++ ) {
  238. if ( !component_sent[ci] ) {
  239. ERREXIT( cinfo, JERR_MISSING_DATA );
  240. }
  241. }
  242. }
  243. }
  244. #endif /* C_MULTISCAN_FILES_SUPPORTED */
  245. LOCAL void
  246. select_scan_parameters( j_compress_ptr cinfo ) {
  247. /* Set up the scan parameters for the current scan */
  248. int ci;
  249. #ifdef C_MULTISCAN_FILES_SUPPORTED
  250. if ( cinfo->scan_info != NULL ) {
  251. /* Prepare for current scan --- the script is already validated */
  252. my_master_ptr master = (my_master_ptr) cinfo->master;
  253. const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
  254. cinfo->comps_in_scan = scanptr->comps_in_scan;
  255. for ( ci = 0; ci < scanptr->comps_in_scan; ci++ ) {
  256. cinfo->cur_comp_info[ci] =
  257. &cinfo->comp_info[scanptr->component_index[ci]];
  258. }
  259. cinfo->Ss = scanptr->Ss;
  260. cinfo->Se = scanptr->Se;
  261. cinfo->Ah = scanptr->Ah;
  262. cinfo->Al = scanptr->Al;
  263. } else
  264. #endif
  265. {
  266. /* Prepare for single sequential-JPEG scan containing all components */
  267. if ( cinfo->num_components > MAX_COMPS_IN_SCAN ) {
  268. ERREXIT2( cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  269. MAX_COMPS_IN_SCAN );
  270. }
  271. cinfo->comps_in_scan = cinfo->num_components;
  272. for ( ci = 0; ci < cinfo->num_components; ci++ ) {
  273. cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  274. }
  275. cinfo->Ss = 0;
  276. cinfo->Se = DCTSIZE2 - 1;
  277. cinfo->Ah = 0;
  278. cinfo->Al = 0;
  279. }
  280. }
  281. LOCAL void
  282. per_scan_setup( j_compress_ptr cinfo ) {
  283. /* Do computations that are needed before processing a JPEG scan */
  284. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  285. int ci, mcublks, tmp;
  286. jpeg_component_info * compptr;
  287. if ( cinfo->comps_in_scan == 1 ) {
  288. /* Noninterleaved (single-component) scan */
  289. compptr = cinfo->cur_comp_info[0];
  290. /* Overall image size in MCUs */
  291. cinfo->MCUs_per_row = compptr->width_in_blocks;
  292. cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  293. /* For noninterleaved scan, always one block per MCU */
  294. compptr->MCU_width = 1;
  295. compptr->MCU_height = 1;
  296. compptr->MCU_blocks = 1;
  297. compptr->MCU_sample_width = DCTSIZE;
  298. compptr->last_col_width = 1;
  299. /* For noninterleaved scans, it is convenient to define last_row_height
  300. * as the number of block rows present in the last iMCU row.
  301. */
  302. tmp = (int) ( compptr->height_in_blocks % compptr->v_samp_factor );
  303. if ( tmp == 0 ) {
  304. tmp = compptr->v_samp_factor;
  305. }
  306. compptr->last_row_height = tmp;
  307. /* Prepare array describing MCU composition */
  308. cinfo->blocks_in_MCU = 1;
  309. cinfo->MCU_membership[0] = 0;
  310. } else {
  311. /* Interleaved (multi-component) scan */
  312. if ( ( cinfo->comps_in_scan <= 0 ) || ( cinfo->comps_in_scan > MAX_COMPS_IN_SCAN ) ) {
  313. ERREXIT2( cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  314. MAX_COMPS_IN_SCAN );
  315. }
  316. /* Overall image size in MCUs */
  317. cinfo->MCUs_per_row = (JDIMENSION)
  318. jdiv_round_up( (long) cinfo->image_width,
  319. (long) ( cinfo->max_h_samp_factor * DCTSIZE ) );
  320. cinfo->MCU_rows_in_scan = (JDIMENSION)
  321. jdiv_round_up( (long) cinfo->image_height,
  322. (long) ( cinfo->max_v_samp_factor * DCTSIZE ) );
  323. cinfo->blocks_in_MCU = 0;
  324. for ( ci = 0; ci < cinfo->comps_in_scan; ci++ ) {
  325. compptr = cinfo->cur_comp_info[ci];
  326. /* Sampling factors give # of blocks of component in each MCU */
  327. compptr->MCU_width = compptr->h_samp_factor;
  328. compptr->MCU_height = compptr->v_samp_factor;
  329. compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  330. compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
  331. /* Figure number of non-dummy blocks in last MCU column & row */
  332. tmp = (int) ( compptr->width_in_blocks % compptr->MCU_width );
  333. if ( tmp == 0 ) {
  334. tmp = compptr->MCU_width;
  335. }
  336. compptr->last_col_width = tmp;
  337. tmp = (int) ( compptr->height_in_blocks % compptr->MCU_height );
  338. if ( tmp == 0 ) {
  339. tmp = compptr->MCU_height;
  340. }
  341. compptr->last_row_height = tmp;
  342. /* Prepare array describing MCU composition */
  343. mcublks = compptr->MCU_blocks;
  344. if ( cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU ) {
  345. ERREXIT( cinfo, JERR_BAD_MCU_SIZE );
  346. }
  347. while ( mcublks-- > 0 ) {
  348. cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  349. }
  350. }
  351. }
  352. /* Convert restart specified in rows to actual MCU count. */
  353. /* Note that count must fit in 16 bits, so we provide limiting. */
  354. if ( cinfo->restart_in_rows > 0 ) {
  355. long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  356. cinfo->restart_interval = (unsigned int) MIN( nominal, 65535L );
  357. }
  358. }
  359. /*
  360. * Per-pass setup.
  361. * This is called at the beginning of each pass. We determine which modules
  362. * will be active during this pass and give them appropriate start_pass calls.
  363. * We also set is_last_pass to indicate whether any more passes will be
  364. * required.
  365. */
  366. METHODDEF void
  367. prepare_for_pass( j_compress_ptr cinfo ) {
  368. my_master_ptr master = (my_master_ptr) cinfo->master;
  369. switch ( master->pass_type ) {
  370. case main_pass:
  371. /* Initial pass: will collect input data, and do either Huffman
  372. * optimization or data output for the first scan.
  373. */
  374. select_scan_parameters( cinfo );
  375. per_scan_setup( cinfo );
  376. if ( !cinfo->raw_data_in ) {
  377. ( *cinfo->cconvert->start_pass )( cinfo );
  378. ( *cinfo->downsample->start_pass )( cinfo );
  379. ( *cinfo->prep->start_pass )( cinfo, JBUF_PASS_THRU );
  380. }
  381. ( *cinfo->fdct->start_pass )( cinfo );
  382. ( *cinfo->entropy->start_pass )( cinfo, cinfo->optimize_coding );
  383. ( *cinfo->coef->start_pass )( cinfo,
  384. ( master->total_passes > 1 ?
  385. JBUF_SAVE_AND_PASS : JBUF_PASS_THRU ) );
  386. ( *cinfo->main->start_pass )( cinfo, JBUF_PASS_THRU );
  387. if ( cinfo->optimize_coding ) {
  388. /* No immediate data output; postpone writing frame/scan headers */
  389. master->pub.call_pass_startup = FALSE;
  390. } else {
  391. /* Will write frame/scan headers at first jpeg_write_scanlines call */
  392. master->pub.call_pass_startup = TRUE;
  393. }
  394. break;
  395. #ifdef ENTROPY_OPT_SUPPORTED
  396. case huff_opt_pass:
  397. /* Do Huffman optimization for a scan after the first one. */
  398. select_scan_parameters( cinfo );
  399. per_scan_setup( cinfo );
  400. if ( ( cinfo->Ss != 0 ) || ( cinfo->Ah == 0 ) || ( cinfo->arith_code ) ) {
  401. ( *cinfo->entropy->start_pass )( cinfo, TRUE );
  402. ( *cinfo->coef->start_pass )( cinfo, JBUF_CRANK_DEST );
  403. master->pub.call_pass_startup = FALSE;
  404. break;
  405. }
  406. /* Special case: Huffman DC refinement scans need no Huffman table
  407. * and therefore we can skip the optimization pass for them.
  408. */
  409. master->pass_type = output_pass;
  410. master->pass_number++;
  411. /*FALLTHROUGH*/
  412. #endif
  413. case output_pass:
  414. /* Do a data-output pass. */
  415. /* We need not repeat per-scan setup if prior optimization pass did it. */
  416. if ( !cinfo->optimize_coding ) {
  417. select_scan_parameters( cinfo );
  418. per_scan_setup( cinfo );
  419. }
  420. ( *cinfo->entropy->start_pass )( cinfo, FALSE );
  421. ( *cinfo->coef->start_pass )( cinfo, JBUF_CRANK_DEST );
  422. /* We emit frame/scan headers now */
  423. if ( master->scan_number == 0 ) {
  424. ( *cinfo->marker->write_frame_header )( cinfo );
  425. }
  426. ( *cinfo->marker->write_scan_header )( cinfo );
  427. master->pub.call_pass_startup = FALSE;
  428. break;
  429. default:
  430. ERREXIT( cinfo, JERR_NOT_COMPILED );
  431. }
  432. master->pub.is_last_pass = ( master->pass_number == master->total_passes - 1 );
  433. /* Set up progress monitor's pass info if present */
  434. if ( cinfo->progress != NULL ) {
  435. cinfo->progress->completed_passes = master->pass_number;
  436. cinfo->progress->total_passes = master->total_passes;
  437. }
  438. }
  439. /*
  440. * Special start-of-pass hook.
  441. * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  442. * In single-pass processing, we need this hook because we don't want to
  443. * write frame/scan headers during jpeg_start_compress; we want to let the
  444. * application write COM markers etc. between jpeg_start_compress and the
  445. * jpeg_write_scanlines loop.
  446. * In multi-pass processing, this routine is not used.
  447. */
  448. METHODDEF void
  449. pass_startup( j_compress_ptr cinfo ) {
  450. cinfo->master->call_pass_startup = FALSE;/* reset flag so call only once */
  451. ( *cinfo->marker->write_frame_header )( cinfo );
  452. ( *cinfo->marker->write_scan_header )( cinfo );
  453. }
  454. /*
  455. * Finish up at end of pass.
  456. */
  457. METHODDEF void
  458. finish_pass_master( j_compress_ptr cinfo ) {
  459. my_master_ptr master = (my_master_ptr) cinfo->master;
  460. /* The entropy coder always needs an end-of-pass call,
  461. * either to analyze statistics or to flush its output buffer.
  462. */
  463. ( *cinfo->entropy->finish_pass )( cinfo );
  464. /* Update state for next pass */
  465. switch ( master->pass_type ) {
  466. case main_pass:
  467. /* next pass is either output of scan 0 (after optimization)
  468. * or output of scan 1 (if no optimization).
  469. */
  470. master->pass_type = output_pass;
  471. if ( !cinfo->optimize_coding ) {
  472. master->scan_number++;
  473. }
  474. break;
  475. case huff_opt_pass:
  476. /* next pass is always output of current scan */
  477. master->pass_type = output_pass;
  478. break;
  479. case output_pass:
  480. /* next pass is either optimization or output of next scan */
  481. if ( cinfo->optimize_coding ) {
  482. master->pass_type = huff_opt_pass;
  483. }
  484. master->scan_number++;
  485. break;
  486. }
  487. master->pass_number++;
  488. }
  489. /*
  490. * Initialize master compression control.
  491. */
  492. GLOBAL void
  493. jinit_c_master_control( j_compress_ptr cinfo, boolean transcode_only ) {
  494. my_master_ptr master;
  495. master = (my_master_ptr)
  496. ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
  497. SIZEOF( my_comp_master ) );
  498. cinfo->master = (struct jpeg_comp_master *) master;
  499. master->pub.prepare_for_pass = prepare_for_pass;
  500. master->pub.pass_startup = pass_startup;
  501. master->pub.finish_pass = finish_pass_master;
  502. master->pub.is_last_pass = FALSE;
  503. /* Validate parameters, determine derived values */
  504. initial_setup( cinfo );
  505. if ( cinfo->scan_info != NULL ) {
  506. #ifdef C_MULTISCAN_FILES_SUPPORTED
  507. validate_script( cinfo );
  508. #else
  509. ERREXIT( cinfo, JERR_NOT_COMPILED );
  510. #endif
  511. } else {
  512. cinfo->progressive_mode = FALSE;
  513. cinfo->num_scans = 1;
  514. }
  515. if ( cinfo->progressive_mode ) {/* TEMPORARY HACK ??? */
  516. cinfo->optimize_coding = TRUE;
  517. } /* assume default tables no good for progressive mode */
  518. /* Initialize my private state */
  519. if ( transcode_only ) {
  520. /* no main pass in transcoding */
  521. if ( cinfo->optimize_coding ) {
  522. master->pass_type = huff_opt_pass;
  523. } else {
  524. master->pass_type = output_pass;
  525. }
  526. } else {
  527. /* for normal compression, first pass is always this type: */
  528. master->pass_type = main_pass;
  529. }
  530. master->scan_number = 0;
  531. master->pass_number = 0;
  532. if ( cinfo->optimize_coding ) {
  533. master->total_passes = cinfo->num_scans * 2;
  534. } else {
  535. master->total_passes = cinfo->num_scans;
  536. }
  537. }