Team Fortress 2 Source Code as on 22/4/2020
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.

838 lines
29 KiB

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