jcparam.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * jcparam.c
  3. *
  4. * Copyright (C) 1991-1998, Thomas G. Lane.
  5. * Modified 2003-2022 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 optional default-setting code for the JPEG compressor.
  10. * Applications do not have to use this file, but those that don't use it
  11. * must know a lot more about the innards of the JPEG code.
  12. */
  13. #define JPEG_INTERNALS
  14. #include "jinclude.h"
  15. #include "jpeglib.h"
  16. /*
  17. * Quantization table setup routines
  18. */
  19. GLOBAL(void)
  20. jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
  21. const unsigned int *basic_table,
  22. int scale_factor, boolean force_baseline)
  23. /* Define a quantization table equal to the basic_table times
  24. * a scale factor (given as a percentage).
  25. * If force_baseline is TRUE, the computed quantization table entries
  26. * are limited to 1..255 for JPEG baseline compatibility.
  27. */
  28. {
  29. JQUANT_TBL ** qtblptr;
  30. int i;
  31. long temp;
  32. /* Safety check to ensure start_compress not called yet. */
  33. if (cinfo->global_state != CSTATE_START)
  34. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  35. if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
  36. ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
  37. qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
  38. if (*qtblptr == NULL)
  39. *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
  40. for (i = 0; i < DCTSIZE2; i++) {
  41. temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
  42. /* limit the values to the valid range */
  43. if (temp <= 0L) temp = 1L;
  44. if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
  45. if (force_baseline && temp > 255L)
  46. temp = 255L; /* limit to baseline range if requested */
  47. (*qtblptr)->quantval[i] = (UINT16) temp;
  48. }
  49. /* Initialize sent_table FALSE so table will be written to JPEG file. */
  50. (*qtblptr)->sent_table = FALSE;
  51. }
  52. /* These are the sample quantization tables given in JPEG spec section K.1.
  53. * NOTE: chrominance DC value is changed from 17 to 16 for lossless support.
  54. * The spec says that the values given produce "good" quality,
  55. * and when divided by 2, "very good" quality.
  56. */
  57. static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
  58. 16, 11, 10, 16, 24, 40, 51, 61,
  59. 12, 12, 14, 19, 26, 58, 60, 55,
  60. 14, 13, 16, 24, 40, 57, 69, 56,
  61. 14, 17, 22, 29, 51, 87, 80, 62,
  62. 18, 22, 37, 56, 68, 109, 103, 77,
  63. 24, 35, 55, 64, 81, 104, 113, 92,
  64. 49, 64, 78, 87, 103, 121, 120, 101,
  65. 72, 92, 95, 98, 112, 100, 103, 99
  66. };
  67. static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
  68. 16, 18, 24, 47, 99, 99, 99, 99,
  69. 18, 21, 26, 66, 99, 99, 99, 99,
  70. 24, 26, 56, 99, 99, 99, 99, 99,
  71. 47, 66, 99, 99, 99, 99, 99, 99,
  72. 99, 99, 99, 99, 99, 99, 99, 99,
  73. 99, 99, 99, 99, 99, 99, 99, 99,
  74. 99, 99, 99, 99, 99, 99, 99, 99,
  75. 99, 99, 99, 99, 99, 99, 99, 99
  76. };
  77. GLOBAL(void)
  78. jpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline)
  79. /* Set or change the 'quality' (quantization) setting, using default tables
  80. * and straight percentage-scaling quality scales.
  81. * This entry point allows different scalings for luminance and chrominance.
  82. */
  83. {
  84. /* Set up two quantization tables using the specified scaling */
  85. jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
  86. cinfo->q_scale_factor[0], force_baseline);
  87. jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
  88. cinfo->q_scale_factor[1], force_baseline);
  89. }
  90. GLOBAL(void)
  91. jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
  92. boolean force_baseline)
  93. /* Set or change the 'quality' (quantization) setting, using default tables
  94. * and a straight percentage-scaling quality scale. In most cases it's better
  95. * to use jpeg_set_quality (below); this entry point is provided for
  96. * applications that insist on a linear percentage scaling.
  97. */
  98. {
  99. /* Set up two quantization tables using the specified scaling */
  100. jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
  101. scale_factor, force_baseline);
  102. jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
  103. scale_factor, force_baseline);
  104. }
  105. GLOBAL(int)
  106. jpeg_quality_scaling (int quality)
  107. /* Convert a user-specified quality rating to a percentage scaling factor
  108. * for an underlying quantization table, using our recommended scaling curve.
  109. * The input 'quality' factor should be 0 (terrible) to 100 (very good).
  110. */
  111. {
  112. /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */
  113. if (quality <= 0) quality = 1;
  114. if (quality > 100) quality = 100;
  115. /* The basic table is used as-is (scaling 100) for a quality of 50.
  116. * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
  117. * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
  118. * to make all the table entries 1 (hence, minimum quantization loss).
  119. * Qualities 1..50 are converted to scaling percentage 5000/Q.
  120. */
  121. if (quality < 50)
  122. quality = 5000 / quality;
  123. else
  124. quality = 200 - quality*2;
  125. return quality;
  126. }
  127. GLOBAL(void)
  128. jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
  129. /* Set or change the 'quality' (quantization) setting, using default tables.
  130. * This is the standard quality-adjusting entry point for typical user
  131. * interfaces; only those who want detailed control over quantization tables
  132. * would use the preceding routines directly.
  133. */
  134. {
  135. /* Convert user 0-100 rating to percentage scaling */
  136. quality = jpeg_quality_scaling(quality);
  137. /* Set up standard quality tables */
  138. jpeg_set_linear_quality(cinfo, quality, force_baseline);
  139. }
  140. /*
  141. * Reset standard Huffman tables
  142. */
  143. LOCAL(void)
  144. std_huff_tables (j_compress_ptr cinfo)
  145. {
  146. if (cinfo->dc_huff_tbl_ptrs[0] != NULL)
  147. (void) jpeg_std_huff_table((j_common_ptr) cinfo, TRUE, 0);
  148. if (cinfo->ac_huff_tbl_ptrs[0] != NULL)
  149. (void) jpeg_std_huff_table((j_common_ptr) cinfo, FALSE, 0);
  150. if (cinfo->dc_huff_tbl_ptrs[1] != NULL)
  151. (void) jpeg_std_huff_table((j_common_ptr) cinfo, TRUE, 1);
  152. if (cinfo->ac_huff_tbl_ptrs[1] != NULL)
  153. (void) jpeg_std_huff_table((j_common_ptr) cinfo, FALSE, 1);
  154. }
  155. /*
  156. * Default parameter setup for compression.
  157. *
  158. * Applications that don't choose to use this routine must do their
  159. * own setup of all these parameters. Alternately, you can call this
  160. * to establish defaults and then alter parameters selectively. This
  161. * is the recommended approach since, if we add any new parameters,
  162. * your code will still work (they'll be set to reasonable defaults).
  163. */
  164. GLOBAL(void)
  165. jpeg_set_defaults (j_compress_ptr cinfo)
  166. {
  167. int i;
  168. /* Safety check to ensure start_compress not called yet. */
  169. if (cinfo->global_state != CSTATE_START)
  170. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  171. /* Allocate comp_info array large enough for maximum component count.
  172. * Array is made permanent in case application wants to compress
  173. * multiple images at same param settings.
  174. */
  175. if (cinfo->comp_info == NULL)
  176. cinfo->comp_info = (jpeg_component_info *)
  177. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  178. MAX_COMPONENTS * SIZEOF(jpeg_component_info));
  179. /* Initialize everything not dependent on the color space */
  180. cinfo->scale_num = 1; /* 1:1 scaling */
  181. cinfo->scale_denom = 1;
  182. cinfo->data_precision = BITS_IN_JSAMPLE;
  183. /* Set up two quantization tables using default quality of 75 */
  184. jpeg_set_quality(cinfo, 75, TRUE);
  185. /* Reset standard Huffman tables */
  186. std_huff_tables(cinfo);
  187. /* Initialize default arithmetic coding conditioning */
  188. for (i = 0; i < NUM_ARITH_TBLS; i++) {
  189. cinfo->arith_dc_L[i] = 0;
  190. cinfo->arith_dc_U[i] = 1;
  191. cinfo->arith_ac_K[i] = 5;
  192. }
  193. /* Default is no multiple-scan output */
  194. cinfo->scan_info = NULL;
  195. cinfo->num_scans = 0;
  196. /* Expect normal source image, not raw downsampled data */
  197. cinfo->raw_data_in = FALSE;
  198. /* The standard Huffman tables are only valid for 8-bit data precision.
  199. * If the precision is higher, use arithmetic coding.
  200. * (Alternatively, using Huffman coding would be possible with forcing
  201. * optimization on so that usable tables will be computed, or by
  202. * supplying default tables that are valid for the desired precision.)
  203. * Otherwise, use Huffman coding by default.
  204. */
  205. cinfo->arith_code = cinfo->data_precision > 8 ? TRUE : FALSE;
  206. /* By default, don't do extra passes to optimize entropy coding */
  207. cinfo->optimize_coding = FALSE;
  208. /* By default, use the simpler non-cosited sampling alignment */
  209. cinfo->CCIR601_sampling = FALSE;
  210. /* By default, apply fancy downsampling */
  211. cinfo->do_fancy_downsampling = TRUE;
  212. /* No input smoothing */
  213. cinfo->smoothing_factor = 0;
  214. /* DCT algorithm preference */
  215. cinfo->dct_method = JDCT_DEFAULT;
  216. /* No restart markers */
  217. cinfo->restart_interval = 0;
  218. cinfo->restart_in_rows = 0;
  219. /* Fill in default JFIF marker parameters. Note that whether the marker
  220. * will actually be written is determined by jpeg_set_colorspace.
  221. *
  222. * By default, the library emits JFIF version code 1.01.
  223. * An application that wants to emit JFIF 1.02 extension markers should set
  224. * JFIF_minor_version to 2. We could probably get away with just defaulting
  225. * to 1.02, but there may still be some decoders in use that will complain
  226. * about that; saying 1.01 should minimize compatibility problems.
  227. *
  228. * For wide gamut colorspaces (BG_RGB and BG_YCC), the major version will be
  229. * overridden by jpeg_set_colorspace and set to 2.
  230. */
  231. cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
  232. cinfo->JFIF_minor_version = 1;
  233. cinfo->density_unit = 0; /* Pixel size is unknown by default */
  234. cinfo->X_density = 1; /* Pixel aspect ratio is square by default */
  235. cinfo->Y_density = 1;
  236. /* No color transform */
  237. cinfo->color_transform = JCT_NONE;
  238. /* Choose JPEG colorspace based on input space, set defaults accordingly */
  239. jpeg_default_colorspace(cinfo);
  240. }
  241. /*
  242. * Select an appropriate JPEG colorspace for in_color_space.
  243. */
  244. GLOBAL(void)
  245. jpeg_default_colorspace (j_compress_ptr cinfo)
  246. {
  247. switch (cinfo->in_color_space) {
  248. case JCS_UNKNOWN:
  249. jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
  250. break;
  251. case JCS_GRAYSCALE:
  252. jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  253. break;
  254. case JCS_RGB:
  255. jpeg_set_colorspace(cinfo, JCS_YCbCr);
  256. break;
  257. case JCS_YCbCr:
  258. jpeg_set_colorspace(cinfo, JCS_YCbCr);
  259. break;
  260. case JCS_CMYK:
  261. jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
  262. break;
  263. case JCS_YCCK:
  264. jpeg_set_colorspace(cinfo, JCS_YCCK);
  265. break;
  266. case JCS_BG_RGB:
  267. /* No translation for now -- conversion to BG_YCC not yet supportet */
  268. jpeg_set_colorspace(cinfo, JCS_BG_RGB);
  269. break;
  270. case JCS_BG_YCC:
  271. jpeg_set_colorspace(cinfo, JCS_BG_YCC);
  272. break;
  273. default:
  274. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  275. }
  276. }
  277. /*
  278. * Set the JPEG colorspace, and choose colorspace-dependent default values.
  279. */
  280. GLOBAL(void)
  281. jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
  282. {
  283. jpeg_component_info * compptr;
  284. int ci;
  285. #define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \
  286. (compptr = &cinfo->comp_info[index], \
  287. compptr->component_id = (id), \
  288. compptr->h_samp_factor = (hsamp), \
  289. compptr->v_samp_factor = (vsamp), \
  290. compptr->quant_tbl_no = (quant), \
  291. compptr->dc_tbl_no = (dctbl), \
  292. compptr->ac_tbl_no = (actbl) )
  293. /* Safety check to ensure start_compress not called yet. */
  294. if (cinfo->global_state != CSTATE_START)
  295. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  296. /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
  297. * tables 1 for chrominance components.
  298. */
  299. cinfo->jpeg_color_space = colorspace;
  300. cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
  301. cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
  302. switch (colorspace) {
  303. case JCS_UNKNOWN:
  304. cinfo->num_components = cinfo->input_components;
  305. if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
  306. ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  307. MAX_COMPONENTS);
  308. for (ci = 0; ci < cinfo->num_components; ci++) {
  309. SET_COMP(ci, ci, 1,1, 0, 0,0);
  310. }
  311. break;
  312. case JCS_GRAYSCALE:
  313. cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
  314. cinfo->num_components = 1;
  315. /* JFIF specifies component ID 1 */
  316. SET_COMP(0, 0x01, 1,1, 0, 0,0);
  317. break;
  318. case JCS_RGB:
  319. cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
  320. cinfo->num_components = 3;
  321. SET_COMP(0, 0x52 /* 'R' */, 1,1,
  322. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
  323. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
  324. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);
  325. SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
  326. SET_COMP(2, 0x42 /* 'B' */, 1,1,
  327. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
  328. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
  329. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);
  330. break;
  331. case JCS_YCbCr:
  332. cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
  333. cinfo->num_components = 3;
  334. /* JFIF specifies component IDs 1,2,3 */
  335. /* We default to 2x2 subsamples of chrominance */
  336. SET_COMP(0, 0x01, 2,2, 0, 0,0);
  337. SET_COMP(1, 0x02, 1,1, 1, 1,1);
  338. SET_COMP(2, 0x03, 1,1, 1, 1,1);
  339. break;
  340. case JCS_CMYK:
  341. cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
  342. cinfo->num_components = 4;
  343. SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
  344. SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
  345. SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
  346. SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
  347. break;
  348. case JCS_YCCK:
  349. cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
  350. cinfo->num_components = 4;
  351. SET_COMP(0, 0x01, 2,2, 0, 0,0);
  352. SET_COMP(1, 0x02, 1,1, 1, 1,1);
  353. SET_COMP(2, 0x03, 1,1, 1, 1,1);
  354. SET_COMP(3, 0x04, 2,2, 0, 0,0);
  355. break;
  356. case JCS_BG_RGB:
  357. cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
  358. cinfo->JFIF_major_version = 2; /* Set JFIF major version = 2 */
  359. cinfo->num_components = 3;
  360. /* Add offset 0x20 to the normal R/G/B component IDs */
  361. SET_COMP(0, 0x72 /* 'r' */, 1,1,
  362. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
  363. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
  364. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);
  365. SET_COMP(1, 0x67 /* 'g' */, 1,1, 0, 0,0);
  366. SET_COMP(2, 0x62 /* 'b' */, 1,1,
  367. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
  368. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0,
  369. cinfo->color_transform == JCT_SUBTRACT_GREEN ? 1 : 0);
  370. break;
  371. case JCS_BG_YCC:
  372. cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
  373. cinfo->JFIF_major_version = 2; /* Set JFIF major version = 2 */
  374. cinfo->num_components = 3;
  375. /* Add offset 0x20 to the normal Cb/Cr component IDs */
  376. /* We default to 2x2 subsamples of chrominance */
  377. SET_COMP(0, 0x01, 2,2, 0, 0,0);
  378. SET_COMP(1, 0x22, 1,1, 1, 1,1);
  379. SET_COMP(2, 0x23, 1,1, 1, 1,1);
  380. break;
  381. default:
  382. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  383. }
  384. }
  385. #ifdef C_PROGRESSIVE_SUPPORTED
  386. LOCAL(jpeg_scan_info *)
  387. fill_a_scan (jpeg_scan_info * scanptr, int ci,
  388. int Ss, int Se, int Ah, int Al)
  389. /* Support routine: generate one scan for specified component */
  390. {
  391. scanptr->comps_in_scan = 1;
  392. scanptr->component_index[0] = ci;
  393. scanptr->Ss = Ss;
  394. scanptr->Se = Se;
  395. scanptr->Ah = Ah;
  396. scanptr->Al = Al;
  397. scanptr++;
  398. return scanptr;
  399. }
  400. LOCAL(jpeg_scan_info *)
  401. fill_scans (jpeg_scan_info * scanptr, int ncomps,
  402. int Ss, int Se, int Ah, int Al)
  403. /* Support routine: generate one scan for each component */
  404. {
  405. int ci;
  406. for (ci = 0; ci < ncomps; ci++) {
  407. scanptr->comps_in_scan = 1;
  408. scanptr->component_index[0] = ci;
  409. scanptr->Ss = Ss;
  410. scanptr->Se = Se;
  411. scanptr->Ah = Ah;
  412. scanptr->Al = Al;
  413. scanptr++;
  414. }
  415. return scanptr;
  416. }
  417. LOCAL(jpeg_scan_info *)
  418. fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
  419. /* Support routine: generate interleaved DC scan if possible, else N scans */
  420. {
  421. int ci;
  422. if (ncomps <= MAX_COMPS_IN_SCAN) {
  423. /* Single interleaved DC scan */
  424. scanptr->comps_in_scan = ncomps;
  425. for (ci = 0; ci < ncomps; ci++)
  426. scanptr->component_index[ci] = ci;
  427. scanptr->Ss = scanptr->Se = 0;
  428. scanptr->Ah = Ah;
  429. scanptr->Al = Al;
  430. scanptr++;
  431. } else {
  432. /* Noninterleaved DC scan for each component */
  433. scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
  434. }
  435. return scanptr;
  436. }
  437. /*
  438. * Create a recommended progressive-JPEG script.
  439. * cinfo->num_components and cinfo->jpeg_color_space must be correct.
  440. */
  441. GLOBAL(void)
  442. jpeg_simple_progression (j_compress_ptr cinfo)
  443. {
  444. int ncomps = cinfo->num_components;
  445. int nscans;
  446. jpeg_scan_info * scanptr;
  447. /* Safety check to ensure start_compress not called yet. */
  448. if (cinfo->global_state != CSTATE_START)
  449. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  450. /* Figure space needed for script. Calculation must match code below! */
  451. if (ncomps == 3 &&
  452. (cinfo->jpeg_color_space == JCS_YCbCr ||
  453. cinfo->jpeg_color_space == JCS_BG_YCC)) {
  454. /* Custom script for YCC color images. */
  455. nscans = 10;
  456. } else {
  457. /* All-purpose script for other color spaces. */
  458. if (ncomps > MAX_COMPS_IN_SCAN)
  459. nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */
  460. else
  461. nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */
  462. }
  463. /* Allocate space for script.
  464. * We need to put it in the permanent pool in case the application performs
  465. * multiple compressions without changing the settings. To avoid a memory
  466. * leak if jpeg_simple_progression is called repeatedly for the same JPEG
  467. * object, we try to re-use previously allocated space, and we allocate
  468. * enough space to handle YCC even if initially asked for grayscale.
  469. */
  470. if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
  471. cinfo->script_space_size = MAX(nscans, 10);
  472. cinfo->script_space = (jpeg_scan_info *)
  473. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  474. cinfo->script_space_size * SIZEOF(jpeg_scan_info));
  475. }
  476. scanptr = cinfo->script_space;
  477. cinfo->scan_info = scanptr;
  478. cinfo->num_scans = nscans;
  479. if (ncomps == 3 &&
  480. (cinfo->jpeg_color_space == JCS_YCbCr ||
  481. cinfo->jpeg_color_space == JCS_BG_YCC)) {
  482. /* Custom script for YCC color images. */
  483. /* Initial DC scan */
  484. scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
  485. /* Initial AC scan: get some luma data out in a hurry */
  486. scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
  487. /* Chroma data is too small to be worth expending many scans on */
  488. scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
  489. scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
  490. /* Complete spectral selection for luma AC */
  491. scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
  492. /* Refine next bit of luma AC */
  493. scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
  494. /* Finish DC successive approximation */
  495. scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
  496. /* Finish AC successive approximation */
  497. scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
  498. scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
  499. /* Luma bottom bit comes last since it's usually largest scan */
  500. scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
  501. } else {
  502. /* All-purpose script for other color spaces. */
  503. /* Successive approximation first pass */
  504. scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
  505. scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
  506. scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
  507. /* Successive approximation second pass */
  508. scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
  509. /* Successive approximation final pass */
  510. scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
  511. scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
  512. }
  513. }
  514. #endif /* C_PROGRESSIVE_SUPPORTED */