jcmaster.c 20 KB

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