jccoefct.cpp 19 KB

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