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.

859 lines
31KB

  1. /*
  2. * jcmaster.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 2003-2011 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains master control logic for the JPEG compressor.
  10. * These routines are concerned with parameter validation, initial setup,
  11. * and inter-pass control (determining the number of passes and the work
  12. * to be done in each pass).
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. /* Private state */
  18. typedef enum {
  19. main_pass, /* input data, also do first output step */
  20. huff_opt_pass, /* Huffman code optimization pass */
  21. output_pass /* data output pass */
  22. } c_pass_type;
  23. typedef struct {
  24. struct jpeg_comp_master pub; /* public fields */
  25. c_pass_type pass_type; /* the type of the current pass */
  26. int pass_number; /* # of passes completed */
  27. int total_passes; /* total # of passes needed */
  28. int scan_number; /* current index in scan_info[] */
  29. } my_comp_master;
  30. typedef my_comp_master * my_master_ptr;
  31. /*
  32. * Support routines that do various essential calculations.
  33. */
  34. /*
  35. * Compute JPEG image dimensions and related values.
  36. * NOTE: this is exported for possible use by application.
  37. * Hence it mustn't do anything that can't be done twice.
  38. */
  39. GLOBAL(void)
  40. jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
  41. /* Do computations that are needed before master selection phase */
  42. {
  43. #ifdef DCT_SCALING_SUPPORTED
  44. /* Sanity check on input image dimensions to prevent overflow in
  45. * following calculation.
  46. * We do check jpeg_width and jpeg_height in initial_setup below,
  47. * but image_width and image_height can come from arbitrary data,
  48. * and we need some space for multiplication by block_size.
  49. */
  50. if (((long) cinfo->image_width >> 24) || ((long) cinfo->image_height >> 24))
  51. ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  52. /* Compute actual JPEG image dimensions and DCT scaling choices. */
  53. if (cinfo->scale_num >= cinfo->scale_denom * cinfo->block_size) {
  54. /* Provide block_size/1 scaling */
  55. cinfo->jpeg_width = cinfo->image_width * cinfo->block_size;
  56. cinfo->jpeg_height = cinfo->image_height * cinfo->block_size;
  57. cinfo->min_DCT_h_scaled_size = 1;
  58. cinfo->min_DCT_v_scaled_size = 1;
  59. } else if (cinfo->scale_num * 2 >= cinfo->scale_denom * cinfo->block_size) {
  60. /* Provide block_size/2 scaling */
  61. cinfo->jpeg_width = (JDIMENSION)
  62. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 2L);
  63. cinfo->jpeg_height = (JDIMENSION)
  64. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 2L);
  65. cinfo->min_DCT_h_scaled_size = 2;
  66. cinfo->min_DCT_v_scaled_size = 2;
  67. } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * cinfo->block_size) {
  68. /* Provide block_size/3 scaling */
  69. cinfo->jpeg_width = (JDIMENSION)
  70. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 3L);
  71. cinfo->jpeg_height = (JDIMENSION)
  72. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 3L);
  73. cinfo->min_DCT_h_scaled_size = 3;
  74. cinfo->min_DCT_v_scaled_size = 3;
  75. } else if (cinfo->scale_num * 4 >= cinfo->scale_denom * cinfo->block_size) {
  76. /* Provide block_size/4 scaling */
  77. cinfo->jpeg_width = (JDIMENSION)
  78. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 4L);
  79. cinfo->jpeg_height = (JDIMENSION)
  80. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 4L);
  81. cinfo->min_DCT_h_scaled_size = 4;
  82. cinfo->min_DCT_v_scaled_size = 4;
  83. } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * cinfo->block_size) {
  84. /* Provide block_size/5 scaling */
  85. cinfo->jpeg_width = (JDIMENSION)
  86. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 5L);
  87. cinfo->jpeg_height = (JDIMENSION)
  88. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 5L);
  89. cinfo->min_DCT_h_scaled_size = 5;
  90. cinfo->min_DCT_v_scaled_size = 5;
  91. } else if (cinfo->scale_num * 6 >= cinfo->scale_denom * cinfo->block_size) {
  92. /* Provide block_size/6 scaling */
  93. cinfo->jpeg_width = (JDIMENSION)
  94. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 6L);
  95. cinfo->jpeg_height = (JDIMENSION)
  96. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 6L);
  97. cinfo->min_DCT_h_scaled_size = 6;
  98. cinfo->min_DCT_v_scaled_size = 6;
  99. } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * cinfo->block_size) {
  100. /* Provide block_size/7 scaling */
  101. cinfo->jpeg_width = (JDIMENSION)
  102. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 7L);
  103. cinfo->jpeg_height = (JDIMENSION)
  104. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 7L);
  105. cinfo->min_DCT_h_scaled_size = 7;
  106. cinfo->min_DCT_v_scaled_size = 7;
  107. } else if (cinfo->scale_num * 8 >= cinfo->scale_denom * cinfo->block_size) {
  108. /* Provide block_size/8 scaling */
  109. cinfo->jpeg_width = (JDIMENSION)
  110. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 8L);
  111. cinfo->jpeg_height = (JDIMENSION)
  112. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 8L);
  113. cinfo->min_DCT_h_scaled_size = 8;
  114. cinfo->min_DCT_v_scaled_size = 8;
  115. } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * cinfo->block_size) {
  116. /* Provide block_size/9 scaling */
  117. cinfo->jpeg_width = (JDIMENSION)
  118. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 9L);
  119. cinfo->jpeg_height = (JDIMENSION)
  120. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 9L);
  121. cinfo->min_DCT_h_scaled_size = 9;
  122. cinfo->min_DCT_v_scaled_size = 9;
  123. } else if (cinfo->scale_num * 10 >= cinfo->scale_denom * cinfo->block_size) {
  124. /* Provide block_size/10 scaling */
  125. cinfo->jpeg_width = (JDIMENSION)
  126. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 10L);
  127. cinfo->jpeg_height = (JDIMENSION)
  128. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 10L);
  129. cinfo->min_DCT_h_scaled_size = 10;
  130. cinfo->min_DCT_v_scaled_size = 10;
  131. } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * cinfo->block_size) {
  132. /* Provide block_size/11 scaling */
  133. cinfo->jpeg_width = (JDIMENSION)
  134. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 11L);
  135. cinfo->jpeg_height = (JDIMENSION)
  136. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 11L);
  137. cinfo->min_DCT_h_scaled_size = 11;
  138. cinfo->min_DCT_v_scaled_size = 11;
  139. } else if (cinfo->scale_num * 12 >= cinfo->scale_denom * cinfo->block_size) {
  140. /* Provide block_size/12 scaling */
  141. cinfo->jpeg_width = (JDIMENSION)
  142. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 12L);
  143. cinfo->jpeg_height = (JDIMENSION)
  144. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 12L);
  145. cinfo->min_DCT_h_scaled_size = 12;
  146. cinfo->min_DCT_v_scaled_size = 12;
  147. } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * cinfo->block_size) {
  148. /* Provide block_size/13 scaling */
  149. cinfo->jpeg_width = (JDIMENSION)
  150. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 13L);
  151. cinfo->jpeg_height = (JDIMENSION)
  152. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 13L);
  153. cinfo->min_DCT_h_scaled_size = 13;
  154. cinfo->min_DCT_v_scaled_size = 13;
  155. } else if (cinfo->scale_num * 14 >= cinfo->scale_denom * cinfo->block_size) {
  156. /* Provide block_size/14 scaling */
  157. cinfo->jpeg_width = (JDIMENSION)
  158. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 14L);
  159. cinfo->jpeg_height = (JDIMENSION)
  160. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 14L);
  161. cinfo->min_DCT_h_scaled_size = 14;
  162. cinfo->min_DCT_v_scaled_size = 14;
  163. } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * cinfo->block_size) {
  164. /* Provide block_size/15 scaling */
  165. cinfo->jpeg_width = (JDIMENSION)
  166. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 15L);
  167. cinfo->jpeg_height = (JDIMENSION)
  168. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 15L);
  169. cinfo->min_DCT_h_scaled_size = 15;
  170. cinfo->min_DCT_v_scaled_size = 15;
  171. } else {
  172. /* Provide block_size/16 scaling */
  173. cinfo->jpeg_width = (JDIMENSION)
  174. jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 16L);
  175. cinfo->jpeg_height = (JDIMENSION)
  176. jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 16L);
  177. cinfo->min_DCT_h_scaled_size = 16;
  178. cinfo->min_DCT_v_scaled_size = 16;
  179. }
  180. #else /* !DCT_SCALING_SUPPORTED */
  181. /* Hardwire it to "no scaling" */
  182. cinfo->jpeg_width = cinfo->image_width;
  183. cinfo->jpeg_height = cinfo->image_height;
  184. cinfo->min_DCT_h_scaled_size = DCTSIZE;
  185. cinfo->min_DCT_v_scaled_size = DCTSIZE;
  186. #endif /* DCT_SCALING_SUPPORTED */
  187. }
  188. LOCAL(void)
  189. jpeg_calc_trans_dimensions (j_compress_ptr cinfo)
  190. {
  191. if (cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size)
  192. ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
  193. cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size);
  194. cinfo->block_size = cinfo->min_DCT_h_scaled_size;
  195. }
  196. LOCAL(void)
  197. initial_setup (j_compress_ptr cinfo, boolean transcode_only)
  198. /* Do computations that are needed before master selection phase */
  199. {
  200. int ci, ssize;
  201. jpeg_component_info *compptr;
  202. long samplesperrow;
  203. JDIMENSION jd_samplesperrow;
  204. if (transcode_only)
  205. jpeg_calc_trans_dimensions(cinfo);
  206. else
  207. jpeg_calc_jpeg_dimensions(cinfo);
  208. /* Sanity check on block_size */
  209. if (cinfo->block_size < 1 || cinfo->block_size > 16)
  210. ERREXIT2(cinfo, JERR_BAD_DCTSIZE, cinfo->block_size, cinfo->block_size);
  211. /* Derive natural_order from block_size */
  212. switch (cinfo->block_size) {
  213. case 2: cinfo->natural_order = jpeg_natural_order2; break;
  214. case 3: cinfo->natural_order = jpeg_natural_order3; break;
  215. case 4: cinfo->natural_order = jpeg_natural_order4; break;
  216. case 5: cinfo->natural_order = jpeg_natural_order5; break;
  217. case 6: cinfo->natural_order = jpeg_natural_order6; break;
  218. case 7: cinfo->natural_order = jpeg_natural_order7; break;
  219. default: cinfo->natural_order = jpeg_natural_order; break;
  220. }
  221. /* Derive lim_Se from block_size */
  222. cinfo->lim_Se = cinfo->block_size < DCTSIZE ?
  223. cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1;
  224. /* Sanity check on image dimensions */
  225. if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 ||
  226. cinfo->num_components <= 0 || cinfo->input_components <= 0)
  227. ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  228. /* Make sure image isn't bigger than I can handle */
  229. if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION ||
  230. (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION)
  231. ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  232. /* Width of an input scanline must be representable as JDIMENSION. */
  233. samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
  234. jd_samplesperrow = (JDIMENSION) samplesperrow;
  235. if ((long) jd_samplesperrow != samplesperrow)
  236. ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  237. /* For now, precision must match compiled-in value... */
  238. if (cinfo->data_precision != BITS_IN_JSAMPLE)
  239. ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  240. /* Check that number of components won't exceed internal array sizes */
  241. if (cinfo->num_components > MAX_COMPONENTS)
  242. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  243. MAX_COMPONENTS);
  244. /* Compute maximum sampling factors; check factor validity */
  245. cinfo->max_h_samp_factor = 1;
  246. cinfo->max_v_samp_factor = 1;
  247. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  248. ci++, compptr++) {
  249. if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  250. compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  251. ERREXIT(cinfo, JERR_BAD_SAMPLING);
  252. cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  253. compptr->h_samp_factor);
  254. cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  255. compptr->v_samp_factor);
  256. }
  257. /* Compute dimensions of components */
  258. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  259. ci++, compptr++) {
  260. /* Fill in the correct component_index value; don't rely on application */
  261. compptr->component_index = ci;
  262. /* In selecting the actual DCT scaling for each component, we try to
  263. * scale down the chroma components via DCT scaling rather than downsampling.
  264. * This saves time if the downsampler gets to use 1:1 scaling.
  265. * Note this code adapts subsampling ratios which are powers of 2.
  266. */
  267. ssize = 1;
  268. #ifdef DCT_SCALING_SUPPORTED
  269. while (cinfo->min_DCT_h_scaled_size * ssize <=
  270. (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
  271. (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
  272. ssize = ssize * 2;
  273. }
  274. #endif
  275. compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
  276. ssize = 1;
  277. #ifdef DCT_SCALING_SUPPORTED
  278. while (cinfo->min_DCT_v_scaled_size * ssize <=
  279. (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
  280. (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
  281. ssize = ssize * 2;
  282. }
  283. #endif
  284. compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
  285. /* We don't support DCT ratios larger than 2. */
  286. if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
  287. compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
  288. else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
  289. compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
  290. /* Size in DCT blocks */
  291. compptr->width_in_blocks = (JDIMENSION)
  292. jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
  293. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  294. compptr->height_in_blocks = (JDIMENSION)
  295. jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
  296. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  297. /* Size in samples */
  298. compptr->downsampled_width = (JDIMENSION)
  299. jdiv_round_up((long) cinfo->jpeg_width *
  300. (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
  301. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  302. compptr->downsampled_height = (JDIMENSION)
  303. jdiv_round_up((long) cinfo->jpeg_height *
  304. (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
  305. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  306. /* Mark component needed (this flag isn't actually used for compression) */
  307. compptr->component_needed = TRUE;
  308. }
  309. /* Compute number of fully interleaved MCU rows (number of times that
  310. * main controller will call coefficient controller).
  311. */
  312. cinfo->total_iMCU_rows = (JDIMENSION)
  313. jdiv_round_up((long) cinfo->jpeg_height,
  314. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  315. }
  316. #ifdef C_MULTISCAN_FILES_SUPPORTED
  317. LOCAL(void)
  318. validate_script (j_compress_ptr cinfo)
  319. /* Verify that the scan script in cinfo->scan_info[] is valid; also
  320. * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
  321. */
  322. {
  323. const jpeg_scan_info * scanptr;
  324. int scanno, ncomps, ci, coefi, thisi;
  325. int Ss, Se, Ah, Al;
  326. boolean component_sent[MAX_COMPONENTS];
  327. #ifdef C_PROGRESSIVE_SUPPORTED
  328. int * last_bitpos_ptr;
  329. int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
  330. /* -1 until that coefficient has been seen; then last Al for it */
  331. #endif
  332. if (cinfo->num_scans <= 0)
  333. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
  334. /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
  335. * for progressive JPEG, no scan can have this.
  336. */
  337. scanptr = cinfo->scan_info;
  338. if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
  339. #ifdef C_PROGRESSIVE_SUPPORTED
  340. cinfo->progressive_mode = TRUE;
  341. last_bitpos_ptr = & last_bitpos[0][0];
  342. for (ci = 0; ci < cinfo->num_components; ci++)
  343. for (coefi = 0; coefi < DCTSIZE2; coefi++)
  344. *last_bitpos_ptr++ = -1;
  345. #else
  346. ERREXIT(cinfo, JERR_NOT_COMPILED);
  347. #endif
  348. } else {
  349. cinfo->progressive_mode = FALSE;
  350. for (ci = 0; ci < cinfo->num_components; ci++)
  351. component_sent[ci] = FALSE;
  352. }
  353. for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
  354. /* Validate component indexes */
  355. ncomps = scanptr->comps_in_scan;
  356. if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
  357. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
  358. for (ci = 0; ci < ncomps; ci++) {
  359. thisi = scanptr->component_index[ci];
  360. if (thisi < 0 || thisi >= cinfo->num_components)
  361. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  362. /* Components must appear in SOF order within each scan */
  363. if (ci > 0 && thisi <= scanptr->component_index[ci-1])
  364. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  365. }
  366. /* Validate progression parameters */
  367. Ss = scanptr->Ss;
  368. Se = scanptr->Se;
  369. Ah = scanptr->Ah;
  370. Al = scanptr->Al;
  371. if (cinfo->progressive_mode) {
  372. #ifdef C_PROGRESSIVE_SUPPORTED
  373. /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
  374. * seems wrong: the upper bound ought to depend on data precision.
  375. * Perhaps they really meant 0..N+1 for N-bit precision.
  376. * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
  377. * out-of-range reconstructed DC values during the first DC scan,
  378. * which might cause problems for some decoders.
  379. */
  380. #if BITS_IN_JSAMPLE == 8
  381. #define MAX_AH_AL 10
  382. #else
  383. #define MAX_AH_AL 13
  384. #endif
  385. if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
  386. Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
  387. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  388. if (Ss == 0) {
  389. if (Se != 0) /* DC and AC together not OK */
  390. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  391. } else {
  392. if (ncomps != 1) /* AC scans must be for only one component */
  393. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  394. }
  395. for (ci = 0; ci < ncomps; ci++) {
  396. last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
  397. if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
  398. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  399. for (coefi = Ss; coefi <= Se; coefi++) {
  400. if (last_bitpos_ptr[coefi] < 0) {
  401. /* first scan of this coefficient */
  402. if (Ah != 0)
  403. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  404. } else {
  405. /* not first scan */
  406. if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
  407. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  408. }
  409. last_bitpos_ptr[coefi] = Al;
  410. }
  411. }
  412. #endif
  413. } else {
  414. /* For sequential JPEG, all progression parameters must be these: */
  415. if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
  416. ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  417. /* Make sure components are not sent twice */
  418. for (ci = 0; ci < ncomps; ci++) {
  419. thisi = scanptr->component_index[ci];
  420. if (component_sent[thisi])
  421. ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  422. component_sent[thisi] = TRUE;
  423. }
  424. }
  425. }
  426. /* Now verify that everything got sent. */
  427. if (cinfo->progressive_mode) {
  428. #ifdef C_PROGRESSIVE_SUPPORTED
  429. /* For progressive mode, we only check that at least some DC data
  430. * got sent for each component; the spec does not require that all bits
  431. * of all coefficients be transmitted. Would it be wiser to enforce
  432. * transmission of all coefficient bits??
  433. */
  434. for (ci = 0; ci < cinfo->num_components; ci++) {
  435. if (last_bitpos[ci][0] < 0)
  436. ERREXIT(cinfo, JERR_MISSING_DATA);
  437. }
  438. #endif
  439. } else {
  440. for (ci = 0; ci < cinfo->num_components; ci++) {
  441. if (! component_sent[ci])
  442. ERREXIT(cinfo, JERR_MISSING_DATA);
  443. }
  444. }
  445. }
  446. LOCAL(void)
  447. reduce_script (j_compress_ptr cinfo)
  448. /* Adapt scan script for use with reduced block size;
  449. * assume that script has been validated before.
  450. */
  451. {
  452. jpeg_scan_info * scanptr;
  453. int idxout, idxin;
  454. /* Circumvent const declaration for this function */
  455. scanptr = (jpeg_scan_info *) cinfo->scan_info;
  456. idxout = 0;
  457. for (idxin = 0; idxin < cinfo->num_scans; idxin++) {
  458. /* After skipping, idxout becomes smaller than idxin */
  459. if (idxin != idxout)
  460. /* Copy rest of data;
  461. * note we stay in given chunk of allocated memory.
  462. */
  463. scanptr[idxout] = scanptr[idxin];
  464. if (scanptr[idxout].Ss > cinfo->lim_Se)
  465. /* Entire scan out of range - skip this entry */
  466. continue;
  467. if (scanptr[idxout].Se > cinfo->lim_Se)
  468. /* Limit scan to end of block */
  469. scanptr[idxout].Se = cinfo->lim_Se;
  470. idxout++;
  471. }
  472. cinfo->num_scans = idxout;
  473. }
  474. #endif /* C_MULTISCAN_FILES_SUPPORTED */
  475. LOCAL(void)
  476. select_scan_parameters (j_compress_ptr cinfo)
  477. /* Set up the scan parameters for the current scan */
  478. {
  479. int ci;
  480. #ifdef C_MULTISCAN_FILES_SUPPORTED
  481. if (cinfo->scan_info != NULL) {
  482. /* Prepare for current scan --- the script is already validated */
  483. my_master_ptr master = (my_master_ptr) cinfo->master;
  484. const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
  485. cinfo->comps_in_scan = scanptr->comps_in_scan;
  486. for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
  487. cinfo->cur_comp_info[ci] =
  488. &cinfo->comp_info[scanptr->component_index[ci]];
  489. }
  490. if (cinfo->progressive_mode) {
  491. cinfo->Ss = scanptr->Ss;
  492. cinfo->Se = scanptr->Se;
  493. cinfo->Ah = scanptr->Ah;
  494. cinfo->Al = scanptr->Al;
  495. return;
  496. }
  497. }
  498. else
  499. #endif
  500. {
  501. /* Prepare for single sequential-JPEG scan containing all components */
  502. if (cinfo->num_components > MAX_COMPS_IN_SCAN)
  503. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  504. MAX_COMPS_IN_SCAN);
  505. cinfo->comps_in_scan = cinfo->num_components;
  506. for (ci = 0; ci < cinfo->num_components; ci++) {
  507. cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  508. }
  509. }
  510. cinfo->Ss = 0;
  511. cinfo->Se = cinfo->block_size * cinfo->block_size - 1;
  512. cinfo->Ah = 0;
  513. cinfo->Al = 0;
  514. }
  515. LOCAL(void)
  516. per_scan_setup (j_compress_ptr cinfo)
  517. /* Do computations that are needed before processing a JPEG scan */
  518. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  519. {
  520. int ci, mcublks, tmp;
  521. jpeg_component_info *compptr;
  522. if (cinfo->comps_in_scan == 1) {
  523. /* Noninterleaved (single-component) scan */
  524. compptr = cinfo->cur_comp_info[0];
  525. /* Overall image size in MCUs */
  526. cinfo->MCUs_per_row = compptr->width_in_blocks;
  527. cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  528. /* For noninterleaved scan, always one block per MCU */
  529. compptr->MCU_width = 1;
  530. compptr->MCU_height = 1;
  531. compptr->MCU_blocks = 1;
  532. compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
  533. compptr->last_col_width = 1;
  534. /* For noninterleaved scans, it is convenient to define last_row_height
  535. * as the number of block rows present in the last iMCU row.
  536. */
  537. tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  538. if (tmp == 0) tmp = compptr->v_samp_factor;
  539. compptr->last_row_height = tmp;
  540. /* Prepare array describing MCU composition */
  541. cinfo->blocks_in_MCU = 1;
  542. cinfo->MCU_membership[0] = 0;
  543. } else {
  544. /* Interleaved (multi-component) scan */
  545. if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  546. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  547. MAX_COMPS_IN_SCAN);
  548. /* Overall image size in MCUs */
  549. cinfo->MCUs_per_row = (JDIMENSION)
  550. jdiv_round_up((long) cinfo->jpeg_width,
  551. (long) (cinfo->max_h_samp_factor * cinfo->block_size));
  552. cinfo->MCU_rows_in_scan = (JDIMENSION)
  553. jdiv_round_up((long) cinfo->jpeg_height,
  554. (long) (cinfo->max_v_samp_factor * cinfo->block_size));
  555. cinfo->blocks_in_MCU = 0;
  556. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  557. compptr = cinfo->cur_comp_info[ci];
  558. /* Sampling factors give # of blocks of component in each MCU */
  559. compptr->MCU_width = compptr->h_samp_factor;
  560. compptr->MCU_height = compptr->v_samp_factor;
  561. compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  562. compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
  563. /* Figure number of non-dummy blocks in last MCU column & row */
  564. tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  565. if (tmp == 0) tmp = compptr->MCU_width;
  566. compptr->last_col_width = tmp;
  567. tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  568. if (tmp == 0) tmp = compptr->MCU_height;
  569. compptr->last_row_height = tmp;
  570. /* Prepare array describing MCU composition */
  571. mcublks = compptr->MCU_blocks;
  572. if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
  573. ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  574. while (mcublks-- > 0) {
  575. cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  576. }
  577. }
  578. }
  579. /* Convert restart specified in rows to actual MCU count. */
  580. /* Note that count must fit in 16 bits, so we provide limiting. */
  581. if (cinfo->restart_in_rows > 0) {
  582. long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  583. cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  584. }
  585. }
  586. /*
  587. * Per-pass setup.
  588. * This is called at the beginning of each pass. We determine which modules
  589. * will be active during this pass and give them appropriate start_pass calls.
  590. * We also set is_last_pass to indicate whether any more passes will be
  591. * required.
  592. */
  593. METHODDEF(void)
  594. prepare_for_pass (j_compress_ptr cinfo)
  595. {
  596. my_master_ptr master = (my_master_ptr) cinfo->master;
  597. switch (master->pass_type) {
  598. case main_pass:
  599. /* Initial pass: will collect input data, and do either Huffman
  600. * optimization or data output for the first scan.
  601. */
  602. select_scan_parameters(cinfo);
  603. per_scan_setup(cinfo);
  604. if (! cinfo->raw_data_in) {
  605. (*cinfo->cconvert->start_pass) (cinfo);
  606. (*cinfo->downsample->start_pass) (cinfo);
  607. (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  608. }
  609. (*cinfo->fdct->start_pass) (cinfo);
  610. (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
  611. (*cinfo->coef->start_pass) (cinfo,
  612. (master->total_passes > 1 ?
  613. JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  614. (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  615. if (cinfo->optimize_coding) {
  616. /* No immediate data output; postpone writing frame/scan headers */
  617. master->pub.call_pass_startup = FALSE;
  618. } else {
  619. /* Will write frame/scan headers at first jpeg_write_scanlines call */
  620. master->pub.call_pass_startup = TRUE;
  621. }
  622. break;
  623. #ifdef ENTROPY_OPT_SUPPORTED
  624. case huff_opt_pass:
  625. /* Do Huffman optimization for a scan after the first one. */
  626. select_scan_parameters(cinfo);
  627. per_scan_setup(cinfo);
  628. if (cinfo->Ss != 0 || cinfo->Ah == 0) {
  629. (*cinfo->entropy->start_pass) (cinfo, TRUE);
  630. (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  631. master->pub.call_pass_startup = FALSE;
  632. break;
  633. }
  634. /* Special case: Huffman DC refinement scans need no Huffman table
  635. * and therefore we can skip the optimization pass for them.
  636. */
  637. master->pass_type = output_pass;
  638. master->pass_number++;
  639. /*FALLTHROUGH*/
  640. #endif
  641. case output_pass:
  642. /* Do a data-output pass. */
  643. /* We need not repeat per-scan setup if prior optimization pass did it. */
  644. if (! cinfo->optimize_coding) {
  645. select_scan_parameters(cinfo);
  646. per_scan_setup(cinfo);
  647. }
  648. (*cinfo->entropy->start_pass) (cinfo, FALSE);
  649. (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  650. /* We emit frame/scan headers now */
  651. if (master->scan_number == 0)
  652. (*cinfo->marker->write_frame_header) (cinfo);
  653. (*cinfo->marker->write_scan_header) (cinfo);
  654. master->pub.call_pass_startup = FALSE;
  655. break;
  656. default:
  657. ERREXIT(cinfo, JERR_NOT_COMPILED);
  658. }
  659. master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
  660. /* Set up progress monitor's pass info if present */
  661. if (cinfo->progress != NULL) {
  662. cinfo->progress->completed_passes = master->pass_number;
  663. cinfo->progress->total_passes = master->total_passes;
  664. }
  665. }
  666. /*
  667. * Special start-of-pass hook.
  668. * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  669. * In single-pass processing, we need this hook because we don't want to
  670. * write frame/scan headers during jpeg_start_compress; we want to let the
  671. * application write COM markers etc. between jpeg_start_compress and the
  672. * jpeg_write_scanlines loop.
  673. * In multi-pass processing, this routine is not used.
  674. */
  675. METHODDEF(void)
  676. pass_startup (j_compress_ptr cinfo)
  677. {
  678. cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
  679. (*cinfo->marker->write_frame_header) (cinfo);
  680. (*cinfo->marker->write_scan_header) (cinfo);
  681. }
  682. /*
  683. * Finish up at end of pass.
  684. */
  685. METHODDEF(void)
  686. finish_pass_master (j_compress_ptr cinfo)
  687. {
  688. my_master_ptr master = (my_master_ptr) cinfo->master;
  689. /* The entropy coder always needs an end-of-pass call,
  690. * either to analyze statistics or to flush its output buffer.
  691. */
  692. (*cinfo->entropy->finish_pass) (cinfo);
  693. /* Update state for next pass */
  694. switch (master->pass_type) {
  695. case main_pass:
  696. /* next pass is either output of scan 0 (after optimization)
  697. * or output of scan 1 (if no optimization).
  698. */
  699. master->pass_type = output_pass;
  700. if (! cinfo->optimize_coding)
  701. master->scan_number++;
  702. break;
  703. case huff_opt_pass:
  704. /* next pass is always output of current scan */
  705. master->pass_type = output_pass;
  706. break;
  707. case output_pass:
  708. /* next pass is either optimization or output of next scan */
  709. if (cinfo->optimize_coding)
  710. master->pass_type = huff_opt_pass;
  711. master->scan_number++;
  712. break;
  713. }
  714. master->pass_number++;
  715. }
  716. /*
  717. * Initialize master compression control.
  718. */
  719. GLOBAL(void)
  720. jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
  721. {
  722. my_master_ptr master;
  723. master = (my_master_ptr)
  724. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  725. SIZEOF(my_comp_master));
  726. cinfo->master = (struct jpeg_comp_master *) master;
  727. master->pub.prepare_for_pass = prepare_for_pass;
  728. master->pub.pass_startup = pass_startup;
  729. master->pub.finish_pass = finish_pass_master;
  730. master->pub.is_last_pass = FALSE;
  731. /* Validate parameters, determine derived values */
  732. initial_setup(cinfo, transcode_only);
  733. if (cinfo->scan_info != NULL) {
  734. #ifdef C_MULTISCAN_FILES_SUPPORTED
  735. validate_script(cinfo);
  736. if (cinfo->block_size < DCTSIZE)
  737. reduce_script(cinfo);
  738. #else
  739. ERREXIT(cinfo, JERR_NOT_COMPILED);
  740. #endif
  741. } else {
  742. cinfo->progressive_mode = FALSE;
  743. cinfo->num_scans = 1;
  744. }
  745. if ((cinfo->progressive_mode || cinfo->block_size < DCTSIZE) &&
  746. !cinfo->arith_code) /* TEMPORARY HACK ??? */
  747. /* assume default tables no good for progressive or downscale mode */
  748. cinfo->optimize_coding = TRUE;
  749. /* Initialize my private state */
  750. if (transcode_only) {
  751. /* no main pass in transcoding */
  752. if (cinfo->optimize_coding)
  753. master->pass_type = huff_opt_pass;
  754. else
  755. master->pass_type = output_pass;
  756. } else {
  757. /* for normal compression, first pass is always this type: */
  758. master->pass_type = main_pass;
  759. }
  760. master->scan_number = 0;
  761. master->pass_number = 0;
  762. if (cinfo->optimize_coding)
  763. master->total_passes = cinfo->num_scans * 2;
  764. else
  765. master->total_passes = cinfo->num_scans;
  766. }