jcparam.c 19 KB

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