jcmaster.cpp 20 KB

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