Team Fortress 2 Source Code as on 22/4/2020
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.

382 lines
14 KiB

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