jccoefct.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * jccoefct.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1994-1997, Thomas G. Lane.
  6. * It was modified by The libjpeg-turbo Project to include only code and
  7. * information relevant to libjpeg-turbo.
  8. * mozjpeg Modifications:
  9. * Copyright (C) 2014, Mozilla Corporation.
  10. * For conditions of distribution and use, see the accompanying README file.
  11. *
  12. * This file contains the coefficient buffer controller for compression.
  13. * This controller is the top level of the JPEG compressor proper.
  14. * The coefficient buffer lies between forward-DCT and entropy encoding steps.
  15. */
  16. #define JPEG_INTERNALS
  17. #include "jinclude.h"
  18. #include "jpeglib.h"
  19. #include "jchuff.h"
  20. /* We use a full-image coefficient buffer when doing Huffman optimization,
  21. * and also for writing multiple-scan JPEG files. In all cases, the DCT
  22. * step is run during the first pass, and subsequent passes need only read
  23. * the buffered coefficients.
  24. */
  25. #ifdef ENTROPY_OPT_SUPPORTED
  26. #define FULL_COEF_BUFFER_SUPPORTED
  27. #else
  28. #ifdef C_MULTISCAN_FILES_SUPPORTED
  29. #define FULL_COEF_BUFFER_SUPPORTED
  30. #endif
  31. #endif
  32. /* Private buffer controller object */
  33. typedef struct {
  34. struct jpeg_c_coef_controller pub; /* public fields */
  35. JDIMENSION iMCU_row_num; /* iMCU row # within image */
  36. JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
  37. int MCU_vert_offset; /* counts MCU rows within iMCU row */
  38. int MCU_rows_per_iMCU_row; /* number of such rows needed */
  39. /* For single-pass compression, it's sufficient to buffer just one MCU
  40. * (although this may prove a bit slow in practice). We allocate a
  41. * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
  42. * MCU constructed and sent. In multi-pass modes, this array points to the
  43. * current MCU's blocks within the virtual arrays.
  44. */
  45. JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
  46. /* In multi-pass modes, we need a virtual block array for each component. */
  47. jvirt_barray_ptr whole_image[MAX_COMPONENTS];
  48. /* when using trellis quantization, need to keep a copy of all unquantized coefficients */
  49. jvirt_barray_ptr whole_image_uq[MAX_COMPONENTS];
  50. } my_coef_controller;
  51. typedef my_coef_controller *my_coef_ptr;
  52. /* Forward declarations */
  53. METHODDEF(boolean) compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf);
  54. #ifdef FULL_COEF_BUFFER_SUPPORTED
  55. METHODDEF(boolean) compress_first_pass(j_compress_ptr cinfo,
  56. JSAMPIMAGE input_buf);
  57. METHODDEF(boolean) compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf);
  58. #endif
  59. METHODDEF(boolean) compress_trellis_pass
  60. (j_compress_ptr cinfo, JSAMPIMAGE input_buf);
  61. LOCAL(void)
  62. start_iMCU_row(j_compress_ptr cinfo)
  63. /* Reset within-iMCU-row counters for a new row */
  64. {
  65. my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
  66. /* In an interleaved scan, an MCU row is the same as an iMCU row.
  67. * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  68. * But at the bottom of the image, process only what's left.
  69. */
  70. if (cinfo->comps_in_scan > 1) {
  71. coef->MCU_rows_per_iMCU_row = 1;
  72. } else {
  73. if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
  74. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  75. else
  76. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  77. }
  78. coef->mcu_ctr = 0;
  79. coef->MCU_vert_offset = 0;
  80. }
  81. /*
  82. * Initialize for a processing pass.
  83. */
  84. METHODDEF(void)
  85. start_pass_coef(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  86. {
  87. my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
  88. coef->iMCU_row_num = 0;
  89. start_iMCU_row(cinfo);
  90. switch (pass_mode) {
  91. case JBUF_PASS_THRU:
  92. if (coef->whole_image[0] != NULL)
  93. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  94. coef->pub.compress_data = compress_data;
  95. break;
  96. #ifdef FULL_COEF_BUFFER_SUPPORTED
  97. case JBUF_SAVE_AND_PASS:
  98. if (coef->whole_image[0] == NULL)
  99. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  100. coef->pub.compress_data = compress_first_pass;
  101. break;
  102. case JBUF_CRANK_DEST:
  103. if (coef->whole_image[0] == NULL)
  104. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  105. coef->pub.compress_data = compress_output;
  106. break;
  107. #endif
  108. case JBUF_REQUANT:
  109. if (coef->whole_image[0] == NULL)
  110. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  111. coef->pub.compress_data = compress_trellis_pass;
  112. break;
  113. default:
  114. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  115. break;
  116. }
  117. }
  118. /*
  119. * Process some data in the single-pass case.
  120. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  121. * per call, ie, v_samp_factor block rows for each component in the image.
  122. * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  123. *
  124. * NB: input_buf contains a plane for each component in image,
  125. * which we index according to the component's SOF position.
  126. */
  127. METHODDEF(boolean)
  128. compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  129. {
  130. my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
  131. JDIMENSION MCU_col_num; /* index of current MCU within row */
  132. JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  133. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  134. int blkn, bi, ci, yindex, yoffset, blockcnt;
  135. JDIMENSION ypos, xpos;
  136. jpeg_component_info *compptr;
  137. /* Loop to write as much as one whole iMCU row */
  138. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  139. yoffset++) {
  140. for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
  141. MCU_col_num++) {
  142. /* Determine where data comes from in input_buf and do the DCT thing.
  143. * Each call on forward_DCT processes a horizontal row of DCT blocks
  144. * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
  145. * sequentially. Dummy blocks at the right or bottom edge are filled in
  146. * specially. The data in them does not matter for image reconstruction,
  147. * so we fill them with values that will encode to the smallest amount of
  148. * data, viz: all zeroes in the AC entries, DC entries equal to previous
  149. * block's DC value. (Thanks to Thomas Kinsman for this idea.)
  150. */
  151. blkn = 0;
  152. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  153. compptr = cinfo->cur_comp_info[ci];
  154. blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
  155. compptr->last_col_width;
  156. xpos = MCU_col_num * compptr->MCU_sample_width;
  157. ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
  158. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  159. if (coef->iMCU_row_num < last_iMCU_row ||
  160. yoffset + yindex < compptr->last_row_height) {
  161. (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  162. input_buf[compptr->component_index],
  163. coef->MCU_buffer[blkn],
  164. ypos, xpos, (JDIMENSION) blockcnt,
  165. NULL);
  166. if (blockcnt < compptr->MCU_width) {
  167. /* Create some dummy blocks at the right edge of the image. */
  168. jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
  169. (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
  170. for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
  171. coef->MCU_buffer[blkn + bi][0][0] =
  172. coef->MCU_buffer[blkn + bi - 1][0][0];
  173. }
  174. }
  175. } else {
  176. /* Create a row of dummy blocks at the bottom of the image. */
  177. jzero_far((void *)coef->MCU_buffer[blkn],
  178. compptr->MCU_width * sizeof(JBLOCK));
  179. for (bi = 0; bi < compptr->MCU_width; bi++) {
  180. coef->MCU_buffer[blkn + bi][0][0] =
  181. coef->MCU_buffer[blkn - 1][0][0];
  182. }
  183. }
  184. blkn += compptr->MCU_width;
  185. ypos += DCTSIZE;
  186. }
  187. }
  188. /* Try to write the MCU. In event of a suspension failure, we will
  189. * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
  190. */
  191. if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  192. /* Suspension forced; update state counters and exit */
  193. coef->MCU_vert_offset = yoffset;
  194. coef->mcu_ctr = MCU_col_num;
  195. return FALSE;
  196. }
  197. }
  198. /* Completed an MCU row, but perhaps not an iMCU row */
  199. coef->mcu_ctr = 0;
  200. }
  201. /* Completed the iMCU row, advance counters for next one */
  202. coef->iMCU_row_num++;
  203. start_iMCU_row(cinfo);
  204. return TRUE;
  205. }
  206. #ifdef FULL_COEF_BUFFER_SUPPORTED
  207. /*
  208. * Process some data in the first pass of a multi-pass case.
  209. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  210. * per call, ie, v_samp_factor block rows for each component in the image.
  211. * This amount of data is read from the source buffer, DCT'd and quantized,
  212. * and saved into the virtual arrays. We also generate suitable dummy blocks
  213. * as needed at the right and lower edges. (The dummy blocks are constructed
  214. * in the virtual arrays, which have been padded appropriately.) This makes
  215. * it possible for subsequent passes not to worry about real vs. dummy blocks.
  216. *
  217. * We must also emit the data to the entropy encoder. This is conveniently
  218. * done by calling compress_output() after we've loaded the current strip
  219. * of the virtual arrays.
  220. *
  221. * NB: input_buf contains a plane for each component in image. All
  222. * components are DCT'd and loaded into the virtual arrays in this pass.
  223. * However, it may be that only a subset of the components are emitted to
  224. * the entropy encoder during this first pass; be careful about looking
  225. * at the scan-dependent variables (MCU dimensions, etc).
  226. */
  227. METHODDEF(boolean)
  228. compress_first_pass(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  229. {
  230. my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
  231. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  232. JDIMENSION blocks_across, MCUs_across, MCUindex;
  233. int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  234. JCOEF lastDC;
  235. jpeg_component_info *compptr;
  236. JBLOCKARRAY buffer;
  237. JBLOCKROW thisblockrow, lastblockrow;
  238. JBLOCKARRAY buffer_dst;
  239. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  240. ci++, compptr++) {
  241. /* Align the virtual buffer for this component. */
  242. buffer = (*cinfo->mem->access_virt_barray)
  243. ((j_common_ptr)cinfo, coef->whole_image[ci],
  244. coef->iMCU_row_num * compptr->v_samp_factor,
  245. (JDIMENSION) compptr->v_samp_factor, TRUE);
  246. buffer_dst = (*cinfo->mem->access_virt_barray)
  247. ((j_common_ptr) cinfo, coef->whole_image_uq[ci],
  248. coef->iMCU_row_num * compptr->v_samp_factor,
  249. (JDIMENSION) compptr->v_samp_factor, TRUE);
  250. /* Count non-dummy DCT block rows in this iMCU row. */
  251. if (coef->iMCU_row_num < last_iMCU_row)
  252. block_rows = compptr->v_samp_factor;
  253. else {
  254. /* NB: can't use last_row_height here, since may not be set! */
  255. block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
  256. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  257. }
  258. blocks_across = compptr->width_in_blocks;
  259. h_samp_factor = compptr->h_samp_factor;
  260. /* Count number of dummy blocks to be added at the right margin. */
  261. ndummy = (int)(blocks_across % h_samp_factor);
  262. if (ndummy > 0)
  263. ndummy = h_samp_factor - ndummy;
  264. /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
  265. * on forward_DCT processes a complete horizontal row of DCT blocks.
  266. */
  267. for (block_row = 0; block_row < block_rows; block_row++) {
  268. thisblockrow = buffer[block_row];
  269. (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  270. input_buf[ci], thisblockrow,
  271. (JDIMENSION) (block_row * DCTSIZE),
  272. (JDIMENSION) 0, blocks_across,
  273. buffer_dst[block_row]);
  274. if (ndummy > 0) {
  275. /* Create dummy blocks at the right edge of the image. */
  276. thisblockrow += blocks_across; /* => first dummy block */
  277. jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
  278. lastDC = thisblockrow[-1][0];
  279. for (bi = 0; bi < ndummy; bi++) {
  280. thisblockrow[bi][0] = lastDC;
  281. }
  282. }
  283. }
  284. /* If at end of image, create dummy block rows as needed.
  285. * The tricky part here is that within each MCU, we want the DC values
  286. * of the dummy blocks to match the last real block's DC value.
  287. * This squeezes a few more bytes out of the resulting file...
  288. */
  289. if (coef->iMCU_row_num == last_iMCU_row) {
  290. blocks_across += ndummy; /* include lower right corner */
  291. MCUs_across = blocks_across / h_samp_factor;
  292. for (block_row = block_rows; block_row < compptr->v_samp_factor;
  293. block_row++) {
  294. thisblockrow = buffer[block_row];
  295. lastblockrow = buffer[block_row - 1];
  296. jzero_far((void *)thisblockrow,
  297. (size_t)(blocks_across * sizeof(JBLOCK)));
  298. for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
  299. lastDC = lastblockrow[h_samp_factor - 1][0];
  300. for (bi = 0; bi < h_samp_factor; bi++) {
  301. thisblockrow[bi][0] = lastDC;
  302. }
  303. thisblockrow += h_samp_factor; /* advance to next MCU in row */
  304. lastblockrow += h_samp_factor;
  305. }
  306. }
  307. }
  308. }
  309. /* NB: compress_output will increment iMCU_row_num if successful.
  310. * A suspension return will result in redoing all the work above next time.
  311. */
  312. /* Emit data to the entropy encoder, sharing code with subsequent passes */
  313. return compress_output(cinfo, input_buf);
  314. }
  315. METHODDEF(boolean)
  316. compress_trellis_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  317. {
  318. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  319. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  320. JDIMENSION blocks_across, MCUs_across, MCUindex;
  321. int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  322. JCOEF lastDC;
  323. jpeg_component_info *compptr;
  324. JBLOCKARRAY buffer;
  325. JBLOCKROW thisblockrow, lastblockrow;
  326. JBLOCKARRAY buffer_dst;
  327. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  328. c_derived_tbl dctbl_data;
  329. c_derived_tbl *dctbl = &dctbl_data;
  330. c_derived_tbl actbl_data;
  331. c_derived_tbl *actbl = &actbl_data;
  332. #ifdef C_ARITH_CODING_SUPPORTED
  333. arith_rates arith_r_data;
  334. arith_rates *arith_r = &arith_r_data;
  335. #endif
  336. compptr = cinfo->cur_comp_info[ci];
  337. #ifdef C_ARITH_CODING_SUPPORTED
  338. if (cinfo->arith_code)
  339. jget_arith_rates(cinfo, compptr->dc_tbl_no, compptr->ac_tbl_no, arith_r);
  340. else
  341. #endif
  342. {
  343. jpeg_make_c_derived_tbl(cinfo, TRUE, compptr->dc_tbl_no, &dctbl);
  344. jpeg_make_c_derived_tbl(cinfo, FALSE, compptr->ac_tbl_no, &actbl);
  345. }
  346. /* Align the virtual buffer for this component. */
  347. buffer = (*cinfo->mem->access_virt_barray)
  348. ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  349. coef->iMCU_row_num * compptr->v_samp_factor,
  350. (JDIMENSION) compptr->v_samp_factor, TRUE);
  351. buffer_dst = (*cinfo->mem->access_virt_barray)
  352. ((j_common_ptr) cinfo, coef->whole_image_uq[compptr->component_index],
  353. coef->iMCU_row_num * compptr->v_samp_factor,
  354. (JDIMENSION) compptr->v_samp_factor, TRUE);
  355. /* Count non-dummy DCT block rows in this iMCU row. */
  356. if (coef->iMCU_row_num < last_iMCU_row)
  357. block_rows = compptr->v_samp_factor;
  358. else {
  359. /* NB: can't use last_row_height here, since may not be set! */
  360. block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  361. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  362. }
  363. blocks_across = compptr->width_in_blocks;
  364. h_samp_factor = compptr->h_samp_factor;
  365. /* Count number of dummy blocks to be added at the right margin. */
  366. ndummy = (int) (blocks_across % h_samp_factor);
  367. if (ndummy > 0)
  368. ndummy = h_samp_factor - ndummy;
  369. lastDC = 0;
  370. /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
  371. * on forward_DCT processes a complete horizontal row of DCT blocks.
  372. */
  373. for (block_row = 0; block_row < block_rows; block_row++) {
  374. thisblockrow = buffer[block_row];
  375. lastblockrow = (block_row > 0) ? buffer[block_row-1] : NULL;
  376. #ifdef C_ARITH_CODING_SUPPORTED
  377. if (cinfo->arith_code)
  378. quantize_trellis_arith(cinfo, arith_r, thisblockrow,
  379. buffer_dst[block_row], blocks_across,
  380. cinfo->quant_tbl_ptrs[compptr->quant_tbl_no],
  381. cinfo->master->norm_src[compptr->quant_tbl_no],
  382. cinfo->master->norm_coef[compptr->quant_tbl_no],
  383. &lastDC, lastblockrow, buffer_dst[block_row-1]);
  384. else
  385. #endif
  386. quantize_trellis(cinfo, dctbl, actbl, thisblockrow,
  387. buffer_dst[block_row], blocks_across,
  388. cinfo->quant_tbl_ptrs[compptr->quant_tbl_no],
  389. cinfo->master->norm_src[compptr->quant_tbl_no],
  390. cinfo->master->norm_coef[compptr->quant_tbl_no],
  391. &lastDC, lastblockrow, buffer_dst[block_row-1]);
  392. if (ndummy > 0) {
  393. /* Create dummy blocks at the right edge of the image. */
  394. thisblockrow += blocks_across; /* => first dummy block */
  395. jzero_far((void *) thisblockrow, ndummy * sizeof(JBLOCK));
  396. lastDC = thisblockrow[-1][0];
  397. for (bi = 0; bi < ndummy; bi++) {
  398. thisblockrow[bi][0] = lastDC;
  399. }
  400. }
  401. }
  402. /* If at end of image, create dummy block rows as needed.
  403. * The tricky part here is that within each MCU, we want the DC values
  404. * of the dummy blocks to match the last real block's DC value.
  405. * This squeezes a few more bytes out of the resulting file...
  406. */
  407. if (coef->iMCU_row_num == last_iMCU_row) {
  408. blocks_across += ndummy; /* include lower right corner */
  409. MCUs_across = blocks_across / h_samp_factor;
  410. for (block_row = block_rows; block_row < compptr->v_samp_factor;
  411. block_row++) {
  412. thisblockrow = buffer[block_row];
  413. lastblockrow = buffer[block_row-1];
  414. jzero_far((void *) thisblockrow,
  415. (size_t) (blocks_across * sizeof(JBLOCK)));
  416. for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
  417. lastDC = lastblockrow[h_samp_factor-1][0];
  418. for (bi = 0; bi < h_samp_factor; bi++) {
  419. thisblockrow[bi][0] = lastDC;
  420. }
  421. thisblockrow += h_samp_factor; /* advance to next MCU in row */
  422. lastblockrow += h_samp_factor;
  423. }
  424. }
  425. }
  426. }
  427. /* NB: compress_output will increment iMCU_row_num if successful.
  428. * A suspension return will result in redoing all the work above next time.
  429. */
  430. /* Emit data to the entropy encoder, sharing code with subsequent passes */
  431. return compress_output(cinfo, input_buf);
  432. }
  433. /*
  434. * Process some data in subsequent passes of a multi-pass case.
  435. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  436. * per call, ie, v_samp_factor block rows for each component in the scan.
  437. * The data is obtained from the virtual arrays and fed to the entropy coder.
  438. * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  439. *
  440. * NB: input_buf is ignored; it is likely to be a NULL pointer.
  441. */
  442. METHODDEF(boolean)
  443. compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  444. {
  445. my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
  446. JDIMENSION MCU_col_num; /* index of current MCU within row */
  447. int blkn, ci, xindex, yindex, yoffset;
  448. JDIMENSION start_col;
  449. JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  450. JBLOCKROW buffer_ptr;
  451. jpeg_component_info *compptr;
  452. /* Align the virtual buffers for the components used in this scan.
  453. * NB: during first pass, this is safe only because the buffers will
  454. * already be aligned properly, so jmemmgr.c won't need to do any I/O.
  455. */
  456. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  457. compptr = cinfo->cur_comp_info[ci];
  458. buffer[ci] = (*cinfo->mem->access_virt_barray)
  459. ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
  460. coef->iMCU_row_num * compptr->v_samp_factor,
  461. (JDIMENSION)compptr->v_samp_factor, FALSE);
  462. }
  463. /* Loop to process one whole iMCU row */
  464. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  465. yoffset++) {
  466. for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  467. MCU_col_num++) {
  468. /* Construct list of pointers to DCT blocks belonging to this MCU */
  469. blkn = 0; /* index of current DCT block within MCU */
  470. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  471. compptr = cinfo->cur_comp_info[ci];
  472. start_col = MCU_col_num * compptr->MCU_width;
  473. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  474. buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
  475. for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  476. coef->MCU_buffer[blkn++] = buffer_ptr++;
  477. }
  478. }
  479. }
  480. /* Try to write the MCU. */
  481. if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  482. /* Suspension forced; update state counters and exit */
  483. coef->MCU_vert_offset = yoffset;
  484. coef->mcu_ctr = MCU_col_num;
  485. return FALSE;
  486. }
  487. }
  488. /* Completed an MCU row, but perhaps not an iMCU row */
  489. coef->mcu_ctr = 0;
  490. }
  491. /* Completed the iMCU row, advance counters for next one */
  492. coef->iMCU_row_num++;
  493. start_iMCU_row(cinfo);
  494. return TRUE;
  495. }
  496. #endif /* FULL_COEF_BUFFER_SUPPORTED */
  497. /*
  498. * Initialize coefficient buffer controller.
  499. */
  500. GLOBAL(void)
  501. jinit_c_coef_controller(j_compress_ptr cinfo, boolean need_full_buffer)
  502. {
  503. my_coef_ptr coef;
  504. coef = (my_coef_ptr)
  505. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  506. sizeof(my_coef_controller));
  507. cinfo->coef = (struct jpeg_c_coef_controller *)coef;
  508. coef->pub.start_pass = start_pass_coef;
  509. /* Create the coefficient buffer. */
  510. if (need_full_buffer) {
  511. #ifdef FULL_COEF_BUFFER_SUPPORTED
  512. /* Allocate a full-image virtual array for each component, */
  513. /* padded to a multiple of samp_factor DCT blocks in each direction. */
  514. int ci;
  515. jpeg_component_info *compptr;
  516. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  517. ci++, compptr++) {
  518. coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  519. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  520. (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  521. (long) compptr->h_samp_factor),
  522. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  523. (long) compptr->v_samp_factor),
  524. (JDIMENSION) compptr->v_samp_factor);
  525. coef->whole_image_uq[ci] = (*cinfo->mem->request_virt_barray)
  526. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  527. (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  528. (long) compptr->h_samp_factor),
  529. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  530. (long) compptr->v_samp_factor),
  531. (JDIMENSION) compptr->v_samp_factor);
  532. }
  533. #else
  534. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  535. #endif
  536. } else {
  537. /* We only need a single-MCU buffer. */
  538. JBLOCKROW buffer;
  539. int i;
  540. buffer = (JBLOCKROW)
  541. (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  542. C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
  543. for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  544. coef->MCU_buffer[i] = buffer + i;
  545. }
  546. coef->whole_image[0] = NULL; /* flag for no virtual arrays */
  547. }
  548. }