Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

591 lines
20KB

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