jcmaster.c 27 KB

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