jctrans.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * jctrans.c
  3. *
  4. * Copyright (C) 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 library routines for transcoding compression,
  9. * that is, writing raw DCT coefficient arrays to an output JPEG file.
  10. * The routines in jcapimin.c will also be needed by a transcoder.
  11. */
  12. #define JPEG_INTERNALS
  13. #include "jinclude.h"
  14. #include "jpeglib.h"
  15. /* Forward declarations */
  16. LOCAL void transencode_master_selection
  17. JPP( ( j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays ) );
  18. LOCAL void transencode_coef_controller
  19. JPP( ( j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays ) );
  20. /*
  21. * Compression initialization for writing raw-coefficient data.
  22. * Before calling this, all parameters and a data destination must be set up.
  23. * Call jpeg_finish_compress() to actually write the data.
  24. *
  25. * The number of passed virtual arrays must match cinfo->num_components.
  26. * Note that the virtual arrays need not be filled or even realized at
  27. * the time write_coefficients is called; indeed, if the virtual arrays
  28. * were requested from this compression object's memory manager, they
  29. * typically will be realized during this routine and filled afterwards.
  30. */
  31. GLOBAL void
  32. jpeg_write_coefficients( j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays ) {
  33. if ( cinfo->global_state != CSTATE_START ) {
  34. ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state );
  35. }
  36. /* Mark all tables to be written */
  37. jpeg_suppress_tables( cinfo, FALSE );
  38. /* (Re)initialize error mgr and destination modules */
  39. ( *cinfo->err->reset_error_mgr )( (j_common_ptr) cinfo );
  40. ( *cinfo->dest->init_destination )( cinfo );
  41. /* Perform master selection of active modules */
  42. transencode_master_selection( cinfo, coef_arrays );
  43. /* Wait for jpeg_finish_compress() call */
  44. cinfo->next_scanline = 0;/* so jpeg_write_marker works */
  45. cinfo->global_state = CSTATE_WRCOEFS;
  46. }
  47. /*
  48. * Initialize the compression object with default parameters,
  49. * then copy from the source object all parameters needed for lossless
  50. * transcoding. Parameters that can be varied without loss (such as
  51. * scan script and Huffman optimization) are left in their default states.
  52. */
  53. GLOBAL void
  54. jpeg_copy_critical_parameters( j_decompress_ptr srcinfo,
  55. j_compress_ptr dstinfo ) {
  56. JQUANT_TBL ** qtblptr;
  57. jpeg_component_info * incomp, * outcomp;
  58. JQUANT_TBL * c_quant, * slot_quant;
  59. int tblno, ci, coefi;
  60. /* Safety check to ensure start_compress not called yet. */
  61. if ( dstinfo->global_state != CSTATE_START ) {
  62. ERREXIT1( dstinfo, JERR_BAD_STATE, dstinfo->global_state );
  63. }
  64. /* Copy fundamental image dimensions */
  65. dstinfo->image_width = srcinfo->image_width;
  66. dstinfo->image_height = srcinfo->image_height;
  67. dstinfo->input_components = srcinfo->num_components;
  68. dstinfo->in_color_space = srcinfo->jpeg_color_space;
  69. /* Initialize all parameters to default values */
  70. jpeg_set_defaults( dstinfo );
  71. /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
  72. * Fix it to get the right header markers for the image colorspace.
  73. */
  74. jpeg_set_colorspace( dstinfo, srcinfo->jpeg_color_space );
  75. dstinfo->data_precision = srcinfo->data_precision;
  76. dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
  77. /* Copy the source's quantization tables. */
  78. for ( tblno = 0; tblno < NUM_QUANT_TBLS; tblno++ ) {
  79. if ( srcinfo->quant_tbl_ptrs[tblno] != NULL ) {
  80. qtblptr = &dstinfo->quant_tbl_ptrs[tblno];
  81. if ( *qtblptr == NULL ) {
  82. *qtblptr = jpeg_alloc_quant_table( (j_common_ptr) dstinfo );
  83. }
  84. MEMCOPY( ( *qtblptr )->quantval,
  85. srcinfo->quant_tbl_ptrs[tblno]->quantval,
  86. SIZEOF( ( *qtblptr )->quantval ) );
  87. ( *qtblptr )->sent_table = FALSE;
  88. }
  89. }
  90. /* Copy the source's per-component info.
  91. * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
  92. */
  93. dstinfo->num_components = srcinfo->num_components;
  94. if ( ( dstinfo->num_components < 1 ) || ( dstinfo->num_components > MAX_COMPONENTS ) ) {
  95. ERREXIT2( dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
  96. MAX_COMPONENTS );
  97. }
  98. for ( ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
  99. ci < dstinfo->num_components; ci++, incomp++, outcomp++ ) {
  100. outcomp->component_id = incomp->component_id;
  101. outcomp->h_samp_factor = incomp->h_samp_factor;
  102. outcomp->v_samp_factor = incomp->v_samp_factor;
  103. outcomp->quant_tbl_no = incomp->quant_tbl_no;
  104. /* Make sure saved quantization table for component matches the qtable
  105. * slot. If not, the input file re-used this qtable slot.
  106. * IJG encoder currently cannot duplicate this.
  107. */
  108. tblno = outcomp->quant_tbl_no;
  109. if ( ( tblno < 0 ) || ( tblno >= NUM_QUANT_TBLS ) ||
  110. ( srcinfo->quant_tbl_ptrs[tblno] == NULL ) ) {
  111. ERREXIT1( dstinfo, JERR_NO_QUANT_TABLE, tblno );
  112. }
  113. slot_quant = srcinfo->quant_tbl_ptrs[tblno];
  114. c_quant = incomp->quant_table;
  115. if ( c_quant != NULL ) {
  116. for ( coefi = 0; coefi < DCTSIZE2; coefi++ ) {
  117. if ( c_quant->quantval[coefi] != slot_quant->quantval[coefi] ) {
  118. ERREXIT1( dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno );
  119. }
  120. }
  121. }
  122. /* Note: we do not copy the source's Huffman table assignments;
  123. * instead we rely on jpeg_set_colorspace to have made a suitable choice.
  124. */
  125. }
  126. }
  127. /*
  128. * Master selection of compression modules for transcoding.
  129. * This substitutes for jcinit.c's initialization of the full compressor.
  130. */
  131. LOCAL void
  132. transencode_master_selection( j_compress_ptr cinfo,
  133. jvirt_barray_ptr * coef_arrays ) {
  134. /* Although we don't actually use input_components for transcoding,
  135. * jcmaster.c's initial_setup will complain if input_components is 0.
  136. */
  137. cinfo->input_components = 1;
  138. /* Initialize master control (includes parameter checking/processing) */
  139. jinit_c_master_control( cinfo, TRUE /* transcode only */ );
  140. /* Entropy encoding: either Huffman or arithmetic coding. */
  141. if ( cinfo->arith_code ) {
  142. ERREXIT( cinfo, JERR_ARITH_NOTIMPL );
  143. } else {
  144. if ( cinfo->progressive_mode ) {
  145. #ifdef C_PROGRESSIVE_SUPPORTED
  146. jinit_phuff_encoder( cinfo );
  147. #else
  148. ERREXIT( cinfo, JERR_NOT_COMPILED );
  149. #endif
  150. } else {
  151. jinit_huff_encoder( cinfo );
  152. }
  153. }
  154. /* We need a special coefficient buffer controller. */
  155. transencode_coef_controller( cinfo, coef_arrays );
  156. jinit_marker_writer( cinfo );
  157. /* We can now tell the memory manager to allocate virtual arrays. */
  158. ( *cinfo->mem->realize_virt_arrays )( (j_common_ptr) cinfo );
  159. /* Write the datastream header (SOI) immediately.
  160. * Frame and scan headers are postponed till later.
  161. * This lets application insert special markers after the SOI.
  162. */
  163. ( *cinfo->marker->write_file_header )( cinfo );
  164. }
  165. /*
  166. * The rest of this file is a special implementation of the coefficient
  167. * buffer controller. This is similar to jccoefct.c, but it handles only
  168. * output from presupplied virtual arrays. Furthermore, we generate any
  169. * dummy padding blocks on-the-fly rather than expecting them to be present
  170. * in the arrays.
  171. */
  172. /* Private buffer controller object */
  173. typedef struct {
  174. struct jpeg_c_coef_controller pub;/* public fields */
  175. JDIMENSION iMCU_row_num;/* iMCU row # within image */
  176. JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
  177. int MCU_vert_offset; /* counts MCU rows within iMCU row */
  178. int MCU_rows_per_iMCU_row; /* number of such rows needed */
  179. /* Virtual block array for each component. */
  180. jvirt_barray_ptr * whole_image;
  181. /* Workspace for constructing dummy blocks at right/bottom edges. */
  182. JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
  183. } my_coef_controller;
  184. typedef my_coef_controller * my_coef_ptr;
  185. LOCAL void
  186. start_iMCU_row( j_compress_ptr cinfo ) {
  187. /* Reset within-iMCU-row counters for a new row */
  188. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  189. /* In an interleaved scan, an MCU row is the same as an iMCU row.
  190. * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  191. * But at the bottom of the image, process only what's left.
  192. */
  193. if ( cinfo->comps_in_scan > 1 ) {
  194. coef->MCU_rows_per_iMCU_row = 1;
  195. } else {
  196. if ( coef->iMCU_row_num < ( cinfo->total_iMCU_rows - 1 ) ) {
  197. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  198. } else {
  199. coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  200. }
  201. }
  202. coef->mcu_ctr = 0;
  203. coef->MCU_vert_offset = 0;
  204. }
  205. /*
  206. * Initialize for a processing pass.
  207. */
  208. METHODDEF void
  209. start_pass_coef( j_compress_ptr cinfo, J_BUF_MODE pass_mode ) {
  210. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  211. if ( pass_mode != JBUF_CRANK_DEST ) {
  212. ERREXIT( cinfo, JERR_BAD_BUFFER_MODE );
  213. }
  214. coef->iMCU_row_num = 0;
  215. start_iMCU_row( cinfo );
  216. }
  217. /*
  218. * Process some data.
  219. * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  220. * per call, ie, v_samp_factor block rows for each component in the scan.
  221. * The data is obtained from the virtual arrays and fed to the entropy coder.
  222. * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  223. *
  224. * NB: input_buf is ignored; it is likely to be a NULL pointer.
  225. */
  226. METHODDEF boolean
  227. compress_output( j_compress_ptr cinfo, JSAMPIMAGE input_buf ) {
  228. my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  229. JDIMENSION MCU_col_num; /* index of current MCU within row */
  230. JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  231. JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  232. int blkn, ci, xindex, yindex, yoffset, blockcnt;
  233. JDIMENSION start_col;
  234. JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  235. JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
  236. JBLOCKROW buffer_ptr;
  237. jpeg_component_info * compptr;
  238. /* Align the virtual buffers for the components used in this scan. */
  239. for ( ci = 0; ci < cinfo->comps_in_scan; ci++ ) {
  240. compptr = cinfo->cur_comp_info[ci];
  241. buffer[ci] = ( *cinfo->mem->access_virt_barray )
  242. ( (j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  243. coef->iMCU_row_num * compptr->v_samp_factor,
  244. (JDIMENSION) compptr->v_samp_factor, FALSE );
  245. }
  246. /* Loop to process one whole iMCU row */
  247. for ( yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  248. yoffset++ ) {
  249. for ( MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  250. MCU_col_num++ ) {
  251. /* Construct list of pointers to DCT blocks belonging to this MCU */
  252. blkn = 0; /* index of current DCT block within MCU */
  253. for ( ci = 0; ci < cinfo->comps_in_scan; ci++ ) {
  254. compptr = cinfo->cur_comp_info[ci];
  255. start_col = MCU_col_num * compptr->MCU_width;
  256. blockcnt = ( MCU_col_num < last_MCU_col ) ? compptr->MCU_width
  257. : compptr->last_col_width;
  258. for ( yindex = 0; yindex < compptr->MCU_height; yindex++ ) {
  259. if ( ( coef->iMCU_row_num < last_iMCU_row ) ||
  260. ( yindex + yoffset < compptr->last_row_height ) ) {
  261. /* Fill in pointers to real blocks in this row */
  262. buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
  263. for ( xindex = 0; xindex < blockcnt; xindex++ ) {
  264. MCU_buffer[blkn++] = buffer_ptr++;
  265. }
  266. } else {
  267. /* At bottom of image, need a whole row of dummy blocks */
  268. xindex = 0;
  269. }
  270. /* Fill in any dummy blocks needed in this row.
  271. * Dummy blocks are filled in the same way as in jccoefct.c:
  272. * all zeroes in the AC entries, DC entries equal to previous
  273. * block's DC value. The init routine has already zeroed the
  274. * AC entries, so we need only set the DC entries correctly.
  275. */
  276. for (; xindex < compptr->MCU_width; xindex++ ) {
  277. MCU_buffer[blkn] = coef->dummy_buffer[blkn];
  278. MCU_buffer[blkn][0][0] = MCU_buffer[blkn - 1][0][0];
  279. blkn++;
  280. }
  281. }
  282. }
  283. /* Try to write the MCU. */
  284. if ( !( *cinfo->entropy->encode_mcu )( cinfo, MCU_buffer ) ) {
  285. /* Suspension forced; update state counters and exit */
  286. coef->MCU_vert_offset = yoffset;
  287. coef->mcu_ctr = MCU_col_num;
  288. return FALSE;
  289. }
  290. }
  291. /* Completed an MCU row, but perhaps not an iMCU row */
  292. coef->mcu_ctr = 0;
  293. }
  294. /* Completed the iMCU row, advance counters for next one */
  295. coef->iMCU_row_num++;
  296. start_iMCU_row( cinfo );
  297. return TRUE;
  298. }
  299. /*
  300. * Initialize coefficient buffer controller.
  301. *
  302. * Each passed coefficient array must be the right size for that
  303. * coefficient: width_in_blocks wide and height_in_blocks high,
  304. * with unitheight at least v_samp_factor.
  305. */
  306. LOCAL void
  307. transencode_coef_controller( j_compress_ptr cinfo,
  308. jvirt_barray_ptr * coef_arrays ) {
  309. my_coef_ptr coef;
  310. JBLOCKROW buffer;
  311. int i;
  312. coef = (my_coef_ptr)
  313. ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
  314. SIZEOF( my_coef_controller ) );
  315. cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  316. coef->pub.start_pass = start_pass_coef;
  317. coef->pub.compress_data = compress_output;
  318. /* Save pointer to virtual arrays */
  319. coef->whole_image = coef_arrays;
  320. /* Allocate and pre-zero space for dummy DCT blocks. */
  321. buffer = (JBLOCKROW)
  322. ( *cinfo->mem->alloc_large )( (j_common_ptr) cinfo, JPOOL_IMAGE,
  323. C_MAX_BLOCKS_IN_MCU * SIZEOF( JBLOCK ) );
  324. jzero_far( (void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF( JBLOCK ) );
  325. for ( i = 0; i < C_MAX_BLOCKS_IN_MCU; i++ ) {
  326. coef->dummy_buffer[i] = buffer + i;
  327. }
  328. }