Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

451 lines
16 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jccoefct.c
  5. *
  6. * Copyright (C) 1994-1996, Thomas G. Lane.
  7. * This file is part of the Independent JPEG Group's software.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains the coefficient buffer controller for compression.
  11. * This controller is the top level of the JPEG compressor proper.
  12. * The coefficient buffer lies between forward-DCT and entropy encoding steps.
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. /* We use a full-image coefficient buffer when doing Huffman optimization,
  18. * and also for writing multiple-scan JPEG files. In all cases, the DCT
  19. * step is run during the first pass, and subsequent passes need only read
  20. * the buffered coefficients.
  21. */
  22. #ifdef ENTROPY_OPT_SUPPORTED
  23. #define FULL_COEF_BUFFER_SUPPORTED
  24. #else
  25. #ifdef C_MULTISCAN_FILES_SUPPORTED
  26. #define FULL_COEF_BUFFER_SUPPORTED
  27. #endif
  28. #endif
  29. /* Private buffer controller object */
  30. typedef struct {
  31. struct jpeg_c_coef_controller pub; /* public fields */
  32. JDIMENSION iMCU_row_num; /* iMCU row # within image */
  33. JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
  34. int MCU_vert_offset; /* counts MCU rows within iMCU row */
  35. int MCU_rows_per_iMCU_row; /* number of such rows needed */
  36. /* For single-pass compression, it's sufficient to buffer just one MCU
  37. * (although this may prove a bit slow in practice). We allocate a
  38. * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
  39. * MCU constructed and sent. (On 80x86, the workspace is FAR even though
  40. * it's not really very big; this is to keep the module interfaces unchanged
  41. * when a large coefficient buffer is necessary.)
  42. * In multi-pass modes, this array points to the current MCU's blocks
  43. * 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. } my_coef_controller;
  49. typedef my_coef_controller * my_coef_ptr;
  50. /* Forward declarations */
  51. METHODDEF(boolean) compress_data
  52. JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
  53. #ifdef FULL_COEF_BUFFER_SUPPORTED
  54. METHODDEF(boolean) compress_first_pass
  55. JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
  56. METHODDEF(boolean) compress_output
  57. JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
  58. #endif
  59. LOCAL(void)
  60. start_iMCU_row (j_compress_ptr cinfo)
  61. /* Reset within-iMCU-row counters for a new row */
  62. {
  63. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  64. /* In an interleaved scan, an MCU row is the same as an iMCU row.
  65. * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  66. * But at the bottom of the image, process only what's left.
  67. */
  68. if (cinfo->comps_in_scan > 1) {
  69. coef->MCU_rows_per_iMCU_row = 1;
  70. } else {
  71. if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
  72. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  73. else
  74. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  75. }
  76. coef->mcu_ctr = 0;
  77. coef->MCU_vert_offset = 0;
  78. }
  79. /*
  80. * Initialize for a processing pass.
  81. */
  82. METHODDEF(void)
  83. start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  84. {
  85. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  86. coef->iMCU_row_num = 0;
  87. start_iMCU_row(cinfo);
  88. switch (pass_mode) {
  89. case JBUF_PASS_THRU:
  90. if (coef->whole_image[0] != NULL)
  91. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  92. coef->pub.compress_data = compress_data;
  93. break;
  94. #ifdef FULL_COEF_BUFFER_SUPPORTED
  95. case JBUF_SAVE_AND_PASS:
  96. if (coef->whole_image[0] == NULL)
  97. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  98. coef->pub.compress_data = compress_first_pass;
  99. break;
  100. case JBUF_CRANK_DEST:
  101. if (coef->whole_image[0] == NULL)
  102. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  103. coef->pub.compress_data = compress_output;
  104. break;
  105. #endif
  106. default:
  107. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  108. break;
  109. }
  110. }
  111. /*
  112. * Process some data in the single-pass case.
  113. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  114. * per call, ie, v_samp_factor block rows for each component in the image.
  115. * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  116. *
  117. * NB: input_buf contains a plane for each component in image.
  118. * For single pass, this is the same as the components in the scan.
  119. */
  120. METHODDEF(boolean)
  121. compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  122. {
  123. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  124. JDIMENSION MCU_col_num; /* index of current MCU within row */
  125. JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  126. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  127. int blkn, bi, ci, yindex, yoffset, blockcnt;
  128. JDIMENSION ypos, xpos;
  129. jpeg_component_info *compptr;
  130. /* Loop to write as much as one whole iMCU row */
  131. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  132. yoffset++) {
  133. for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
  134. MCU_col_num++) {
  135. /* Determine where data comes from in input_buf and do the DCT thing.
  136. * Each call on forward_DCT processes a horizontal row of DCT blocks
  137. * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
  138. * sequentially. Dummy blocks at the right or bottom edge are filled in
  139. * specially. The data in them does not matter for image reconstruction,
  140. * so we fill them with values that will encode to the smallest amount of
  141. * data, viz: all zeroes in the AC entries, DC entries equal to previous
  142. * block's DC value. (Thanks to Thomas Kinsman for this idea.)
  143. */
  144. blkn = 0;
  145. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  146. compptr = cinfo->cur_comp_info[ci];
  147. blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  148. : compptr->last_col_width;
  149. xpos = MCU_col_num * compptr->MCU_sample_width;
  150. ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
  151. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  152. if (coef->iMCU_row_num < last_iMCU_row ||
  153. yoffset+yindex < compptr->last_row_height) {
  154. (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  155. input_buf[ci], coef->MCU_buffer[blkn],
  156. ypos, xpos, (JDIMENSION) blockcnt);
  157. if (blockcnt < compptr->MCU_width) {
  158. /* Create some dummy blocks at the right edge of the image. */
  159. jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
  160. (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
  161. for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
  162. coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
  163. }
  164. }
  165. } else {
  166. /* Create a row of dummy blocks at the bottom of the image. */
  167. jzero_far((void FAR *) coef->MCU_buffer[blkn],
  168. compptr->MCU_width * SIZEOF(JBLOCK));
  169. for (bi = 0; bi < compptr->MCU_width; bi++) {
  170. coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
  171. }
  172. }
  173. blkn += compptr->MCU_width;
  174. ypos += DCTSIZE;
  175. }
  176. }
  177. /* Try to write the MCU. In event of a suspension failure, we will
  178. * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
  179. */
  180. if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  181. /* Suspension forced; update state counters and exit */
  182. coef->MCU_vert_offset = yoffset;
  183. coef->mcu_ctr = MCU_col_num;
  184. return FALSE;
  185. }
  186. }
  187. /* Completed an MCU row, but perhaps not an iMCU row */
  188. coef->mcu_ctr = 0;
  189. }
  190. /* Completed the iMCU row, advance counters for next one */
  191. coef->iMCU_row_num++;
  192. start_iMCU_row(cinfo);
  193. return TRUE;
  194. }
  195. #ifdef FULL_COEF_BUFFER_SUPPORTED
  196. /*
  197. * Process some data in the first pass of a multi-pass case.
  198. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  199. * per call, ie, v_samp_factor block rows for each component in the image.
  200. * This amount of data is read from the source buffer, DCT'd and quantized,
  201. * and saved into the virtual arrays. We also generate suitable dummy blocks
  202. * as needed at the right and lower edges. (The dummy blocks are constructed
  203. * in the virtual arrays, which have been padded appropriately.) This makes
  204. * it possible for subsequent passes not to worry about real vs. dummy blocks.
  205. *
  206. * We must also emit the data to the entropy encoder. This is conveniently
  207. * done by calling compress_output() after we've loaded the current strip
  208. * of the virtual arrays.
  209. *
  210. * NB: input_buf contains a plane for each component in image. All
  211. * components are DCT'd and loaded into the virtual arrays in this pass.
  212. * However, it may be that only a subset of the components are emitted to
  213. * the entropy encoder during this first pass; be careful about looking
  214. * at the scan-dependent variables (MCU dimensions, etc).
  215. */
  216. METHODDEF(boolean)
  217. compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  218. {
  219. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  220. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  221. JDIMENSION blocks_across, MCUs_across, MCUindex;
  222. int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  223. JCOEF lastDC;
  224. jpeg_component_info *compptr;
  225. JBLOCKARRAY buffer;
  226. JBLOCKROW thisblockrow, lastblockrow;
  227. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  228. ci++, compptr++) {
  229. /* Align the virtual buffer for this component. */
  230. buffer = (*cinfo->mem->access_virt_barray)
  231. ((j_common_ptr) cinfo, coef->whole_image[ci],
  232. coef->iMCU_row_num * compptr->v_samp_factor,
  233. (JDIMENSION) compptr->v_samp_factor, TRUE);
  234. /* Count non-dummy DCT block rows in this iMCU row. */
  235. if (coef->iMCU_row_num < last_iMCU_row)
  236. block_rows = compptr->v_samp_factor;
  237. else {
  238. /* NB: can't use last_row_height here, since may not be set! */
  239. block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  240. if (block_rows == 0) block_rows = compptr->v_samp_factor;
  241. }
  242. blocks_across = compptr->width_in_blocks;
  243. h_samp_factor = compptr->h_samp_factor;
  244. /* Count number of dummy blocks to be added at the right margin. */
  245. ndummy = (int) (blocks_across % h_samp_factor);
  246. if (ndummy > 0)
  247. ndummy = h_samp_factor - ndummy;
  248. /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
  249. * on forward_DCT processes a complete horizontal row of DCT blocks.
  250. */
  251. for (block_row = 0; block_row < block_rows; block_row++) {
  252. thisblockrow = buffer[block_row];
  253. (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  254. input_buf[ci], thisblockrow,
  255. (JDIMENSION) (block_row * DCTSIZE),
  256. (JDIMENSION) 0, blocks_across);
  257. if (ndummy > 0) {
  258. /* Create dummy blocks at the right edge of the image. */
  259. thisblockrow += blocks_across; /* => first dummy block */
  260. jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
  261. lastDC = thisblockrow[-1][0];
  262. for (bi = 0; bi < ndummy; bi++) {
  263. thisblockrow[bi][0] = lastDC;
  264. }
  265. }
  266. }
  267. /* If at end of image, create dummy block rows as needed.
  268. * The tricky part here is that within each MCU, we want the DC values
  269. * of the dummy blocks to match the last real block's DC value.
  270. * This squeezes a few more bytes out of the resulting file...
  271. */
  272. if (coef->iMCU_row_num == last_iMCU_row) {
  273. blocks_across += ndummy; /* include lower right corner */
  274. MCUs_across = blocks_across / h_samp_factor;
  275. for (block_row = block_rows; block_row < compptr->v_samp_factor;
  276. block_row++) {
  277. thisblockrow = buffer[block_row];
  278. lastblockrow = buffer[block_row-1];
  279. jzero_far((void FAR *) thisblockrow,
  280. (size_t) (blocks_across * SIZEOF(JBLOCK)));
  281. for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
  282. lastDC = lastblockrow[h_samp_factor-1][0];
  283. for (bi = 0; bi < h_samp_factor; bi++) {
  284. thisblockrow[bi][0] = lastDC;
  285. }
  286. thisblockrow += h_samp_factor; /* advance to next MCU in row */
  287. lastblockrow += h_samp_factor;
  288. }
  289. }
  290. }
  291. }
  292. /* NB: compress_output will increment iMCU_row_num if successful.
  293. * A suspension return will result in redoing all the work above next time.
  294. */
  295. /* Emit data to the entropy encoder, sharing code with subsequent passes */
  296. return compress_output(cinfo, input_buf);
  297. }
  298. /*
  299. * Process some data in subsequent passes of a multi-pass case.
  300. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  301. * per call, ie, v_samp_factor block rows for each component in the scan.
  302. * The data is obtained from the virtual arrays and fed to the entropy coder.
  303. * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  304. *
  305. * NB: input_buf is ignored; it is likely to be a NULL pointer.
  306. */
  307. METHODDEF(boolean)
  308. compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  309. {
  310. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  311. JDIMENSION MCU_col_num; /* index of current MCU within row */
  312. int blkn, ci, xindex, yindex, yoffset;
  313. JDIMENSION start_col;
  314. JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  315. JBLOCKROW buffer_ptr;
  316. jpeg_component_info *compptr;
  317. /* Align the virtual buffers for the components used in this scan.
  318. * NB: during first pass, this is safe only because the buffers will
  319. * already be aligned properly, so jmemmgr.c won't need to do any I/O.
  320. */
  321. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  322. compptr = cinfo->cur_comp_info[ci];
  323. buffer[ci] = (*cinfo->mem->access_virt_barray)
  324. ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  325. coef->iMCU_row_num * compptr->v_samp_factor,
  326. (JDIMENSION) compptr->v_samp_factor, FALSE);
  327. }
  328. /* Loop to process one whole iMCU row */
  329. for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  330. yoffset++) {
  331. for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  332. MCU_col_num++) {
  333. /* Construct list of pointers to DCT blocks belonging to this MCU */
  334. blkn = 0; /* index of current DCT block within MCU */
  335. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  336. compptr = cinfo->cur_comp_info[ci];
  337. start_col = MCU_col_num * compptr->MCU_width;
  338. for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  339. buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  340. for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  341. coef->MCU_buffer[blkn++] = buffer_ptr++;
  342. }
  343. }
  344. }
  345. /* Try to write the MCU. */
  346. if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  347. /* Suspension forced; update state counters and exit */
  348. coef->MCU_vert_offset = yoffset;
  349. coef->mcu_ctr = MCU_col_num;
  350. return FALSE;
  351. }
  352. }
  353. /* Completed an MCU row, but perhaps not an iMCU row */
  354. coef->mcu_ctr = 0;
  355. }
  356. /* Completed the iMCU row, advance counters for next one */
  357. coef->iMCU_row_num++;
  358. start_iMCU_row(cinfo);
  359. return TRUE;
  360. }
  361. #endif /* FULL_COEF_BUFFER_SUPPORTED */
  362. /*
  363. * Initialize coefficient buffer controller.
  364. */
  365. GLOBAL(void)
  366. jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  367. {
  368. my_coef_ptr coef;
  369. coef = (my_coef_ptr)
  370. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  371. SIZEOF(my_coef_controller));
  372. cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  373. coef->pub.start_pass = start_pass_coef;
  374. /* Create the coefficient buffer. */
  375. if (need_full_buffer) {
  376. #ifdef FULL_COEF_BUFFER_SUPPORTED
  377. /* Allocate a full-image virtual array for each component, */
  378. /* padded to a multiple of samp_factor DCT blocks in each direction. */
  379. int ci;
  380. jpeg_component_info *compptr;
  381. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  382. ci++, compptr++) {
  383. coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  384. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  385. (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  386. (long) compptr->h_samp_factor),
  387. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  388. (long) compptr->v_samp_factor),
  389. (JDIMENSION) compptr->v_samp_factor);
  390. }
  391. #else
  392. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  393. #endif
  394. } else {
  395. /* We only need a single-MCU buffer. */
  396. JBLOCKROW buffer;
  397. int i;
  398. buffer = (JBLOCKROW)
  399. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  400. C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  401. for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  402. coef->MCU_buffer[i] = buffer + i;
  403. }
  404. coef->whole_image[0] = NULL; /* flag for no virtual arrays */
  405. }
  406. }