/media/libjpeg/jcmaster.c

http://github.com/zpao/v8monkey · C · 622 lines · 412 code · 74 blank · 136 comment · 125 complexity · dcec4f683eac2ea2356e4cb8bd7b1c15 MD5 · raw file

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