Leaked source code of windows server 2003
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.

371 lines
13 KiB

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