jcmaster.c 30 KB

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