jcmaster.c 21 KB

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