dbf-halloween2015
diff libs/libjpeg/jctrans.c @ 1:c3f5c32cb210
barfed all the libraries in the source tree to make porting easier
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 00:36:56 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/libjpeg/jctrans.c Sun Nov 01 00:36:56 2015 +0200 1.3 @@ -0,0 +1,388 @@ 1.4 +/* 1.5 + * jctrans.c 1.6 + * 1.7 + * Copyright (C) 1995-1998, Thomas G. Lane. 1.8 + * This file is part of the Independent JPEG Group's software. 1.9 + * For conditions of distribution and use, see the accompanying README file. 1.10 + * 1.11 + * This file contains library routines for transcoding compression, 1.12 + * that is, writing raw DCT coefficient arrays to an output JPEG file. 1.13 + * The routines in jcapimin.c will also be needed by a transcoder. 1.14 + */ 1.15 + 1.16 +#define JPEG_INTERNALS 1.17 +#include "jinclude.h" 1.18 +#include "jpeglib.h" 1.19 + 1.20 + 1.21 +/* Forward declarations */ 1.22 +LOCAL(void) transencode_master_selection 1.23 + JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)); 1.24 +LOCAL(void) transencode_coef_controller 1.25 + JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)); 1.26 + 1.27 + 1.28 +/* 1.29 + * Compression initialization for writing raw-coefficient data. 1.30 + * Before calling this, all parameters and a data destination must be set up. 1.31 + * Call jpeg_finish_compress() to actually write the data. 1.32 + * 1.33 + * The number of passed virtual arrays must match cinfo->num_components. 1.34 + * Note that the virtual arrays need not be filled or even realized at 1.35 + * the time write_coefficients is called; indeed, if the virtual arrays 1.36 + * were requested from this compression object's memory manager, they 1.37 + * typically will be realized during this routine and filled afterwards. 1.38 + */ 1.39 + 1.40 +GLOBAL(void) 1.41 +jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays) 1.42 +{ 1.43 + if (cinfo->global_state != CSTATE_START) 1.44 + ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); 1.45 + /* Mark all tables to be written */ 1.46 + jpeg_suppress_tables(cinfo, FALSE); 1.47 + /* (Re)initialize error mgr and destination modules */ 1.48 + (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); 1.49 + (*cinfo->dest->init_destination) (cinfo); 1.50 + /* Perform master selection of active modules */ 1.51 + transencode_master_selection(cinfo, coef_arrays); 1.52 + /* Wait for jpeg_finish_compress() call */ 1.53 + cinfo->next_scanline = 0; /* so jpeg_write_marker works */ 1.54 + cinfo->global_state = CSTATE_WRCOEFS; 1.55 +} 1.56 + 1.57 + 1.58 +/* 1.59 + * Initialize the compression object with default parameters, 1.60 + * then copy from the source object all parameters needed for lossless 1.61 + * transcoding. Parameters that can be varied without loss (such as 1.62 + * scan script and Huffman optimization) are left in their default states. 1.63 + */ 1.64 + 1.65 +GLOBAL(void) 1.66 +jpeg_copy_critical_parameters (j_decompress_ptr srcinfo, 1.67 + j_compress_ptr dstinfo) 1.68 +{ 1.69 + JQUANT_TBL ** qtblptr; 1.70 + jpeg_component_info *incomp, *outcomp; 1.71 + JQUANT_TBL *c_quant, *slot_quant; 1.72 + int tblno, ci, coefi; 1.73 + 1.74 + /* Safety check to ensure start_compress not called yet. */ 1.75 + if (dstinfo->global_state != CSTATE_START) 1.76 + ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state); 1.77 + /* Copy fundamental image dimensions */ 1.78 + dstinfo->image_width = srcinfo->image_width; 1.79 + dstinfo->image_height = srcinfo->image_height; 1.80 + dstinfo->input_components = srcinfo->num_components; 1.81 + dstinfo->in_color_space = srcinfo->jpeg_color_space; 1.82 + /* Initialize all parameters to default values */ 1.83 + jpeg_set_defaults(dstinfo); 1.84 + /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB. 1.85 + * Fix it to get the right header markers for the image colorspace. 1.86 + */ 1.87 + jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space); 1.88 + dstinfo->data_precision = srcinfo->data_precision; 1.89 + dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling; 1.90 + /* Copy the source's quantization tables. */ 1.91 + for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { 1.92 + if (srcinfo->quant_tbl_ptrs[tblno] != NULL) { 1.93 + qtblptr = & dstinfo->quant_tbl_ptrs[tblno]; 1.94 + if (*qtblptr == NULL) 1.95 + *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo); 1.96 + MEMCOPY((*qtblptr)->quantval, 1.97 + srcinfo->quant_tbl_ptrs[tblno]->quantval, 1.98 + SIZEOF((*qtblptr)->quantval)); 1.99 + (*qtblptr)->sent_table = FALSE; 1.100 + } 1.101 + } 1.102 + /* Copy the source's per-component info. 1.103 + * Note we assume jpeg_set_defaults has allocated the dest comp_info array. 1.104 + */ 1.105 + dstinfo->num_components = srcinfo->num_components; 1.106 + if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS) 1.107 + ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components, 1.108 + MAX_COMPONENTS); 1.109 + for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info; 1.110 + ci < dstinfo->num_components; ci++, incomp++, outcomp++) { 1.111 + outcomp->component_id = incomp->component_id; 1.112 + outcomp->h_samp_factor = incomp->h_samp_factor; 1.113 + outcomp->v_samp_factor = incomp->v_samp_factor; 1.114 + outcomp->quant_tbl_no = incomp->quant_tbl_no; 1.115 + /* Make sure saved quantization table for component matches the qtable 1.116 + * slot. If not, the input file re-used this qtable slot. 1.117 + * IJG encoder currently cannot duplicate this. 1.118 + */ 1.119 + tblno = outcomp->quant_tbl_no; 1.120 + if (tblno < 0 || tblno >= NUM_QUANT_TBLS || 1.121 + srcinfo->quant_tbl_ptrs[tblno] == NULL) 1.122 + ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno); 1.123 + slot_quant = srcinfo->quant_tbl_ptrs[tblno]; 1.124 + c_quant = incomp->quant_table; 1.125 + if (c_quant != NULL) { 1.126 + for (coefi = 0; coefi < DCTSIZE2; coefi++) { 1.127 + if (c_quant->quantval[coefi] != slot_quant->quantval[coefi]) 1.128 + ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno); 1.129 + } 1.130 + } 1.131 + /* Note: we do not copy the source's Huffman table assignments; 1.132 + * instead we rely on jpeg_set_colorspace to have made a suitable choice. 1.133 + */ 1.134 + } 1.135 + /* Also copy JFIF version and resolution information, if available. 1.136 + * Strictly speaking this isn't "critical" info, but it's nearly 1.137 + * always appropriate to copy it if available. In particular, 1.138 + * if the application chooses to copy JFIF 1.02 extension markers from 1.139 + * the source file, we need to copy the version to make sure we don't 1.140 + * emit a file that has 1.02 extensions but a claimed version of 1.01. 1.141 + * We will *not*, however, copy version info from mislabeled "2.01" files. 1.142 + */ 1.143 + if (srcinfo->saw_JFIF_marker) { 1.144 + if (srcinfo->JFIF_major_version == 1) { 1.145 + dstinfo->JFIF_major_version = srcinfo->JFIF_major_version; 1.146 + dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version; 1.147 + } 1.148 + dstinfo->density_unit = srcinfo->density_unit; 1.149 + dstinfo->X_density = srcinfo->X_density; 1.150 + dstinfo->Y_density = srcinfo->Y_density; 1.151 + } 1.152 +} 1.153 + 1.154 + 1.155 +/* 1.156 + * Master selection of compression modules for transcoding. 1.157 + * This substitutes for jcinit.c's initialization of the full compressor. 1.158 + */ 1.159 + 1.160 +LOCAL(void) 1.161 +transencode_master_selection (j_compress_ptr cinfo, 1.162 + jvirt_barray_ptr * coef_arrays) 1.163 +{ 1.164 + /* Although we don't actually use input_components for transcoding, 1.165 + * jcmaster.c's initial_setup will complain if input_components is 0. 1.166 + */ 1.167 + cinfo->input_components = 1; 1.168 + /* Initialize master control (includes parameter checking/processing) */ 1.169 + jinit_c_master_control(cinfo, TRUE /* transcode only */); 1.170 + 1.171 + /* Entropy encoding: either Huffman or arithmetic coding. */ 1.172 + if (cinfo->arith_code) { 1.173 + ERREXIT(cinfo, JERR_ARITH_NOTIMPL); 1.174 + } else { 1.175 + if (cinfo->progressive_mode) { 1.176 +#ifdef C_PROGRESSIVE_SUPPORTED 1.177 + jinit_phuff_encoder(cinfo); 1.178 +#else 1.179 + ERREXIT(cinfo, JERR_NOT_COMPILED); 1.180 +#endif 1.181 + } else 1.182 + jinit_huff_encoder(cinfo); 1.183 + } 1.184 + 1.185 + /* We need a special coefficient buffer controller. */ 1.186 + transencode_coef_controller(cinfo, coef_arrays); 1.187 + 1.188 + jinit_marker_writer(cinfo); 1.189 + 1.190 + /* We can now tell the memory manager to allocate virtual arrays. */ 1.191 + (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); 1.192 + 1.193 + /* Write the datastream header (SOI, JFIF) immediately. 1.194 + * Frame and scan headers are postponed till later. 1.195 + * This lets application insert special markers after the SOI. 1.196 + */ 1.197 + (*cinfo->marker->write_file_header) (cinfo); 1.198 +} 1.199 + 1.200 + 1.201 +/* 1.202 + * The rest of this file is a special implementation of the coefficient 1.203 + * buffer controller. This is similar to jccoefct.c, but it handles only 1.204 + * output from presupplied virtual arrays. Furthermore, we generate any 1.205 + * dummy padding blocks on-the-fly rather than expecting them to be present 1.206 + * in the arrays. 1.207 + */ 1.208 + 1.209 +/* Private buffer controller object */ 1.210 + 1.211 +typedef struct { 1.212 + struct jpeg_c_coef_controller pub; /* public fields */ 1.213 + 1.214 + JDIMENSION iMCU_row_num; /* iMCU row # within image */ 1.215 + JDIMENSION mcu_ctr; /* counts MCUs processed in current row */ 1.216 + int MCU_vert_offset; /* counts MCU rows within iMCU row */ 1.217 + int MCU_rows_per_iMCU_row; /* number of such rows needed */ 1.218 + 1.219 + /* Virtual block array for each component. */ 1.220 + jvirt_barray_ptr * whole_image; 1.221 + 1.222 + /* Workspace for constructing dummy blocks at right/bottom edges. */ 1.223 + JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU]; 1.224 +} my_coef_controller; 1.225 + 1.226 +typedef my_coef_controller * my_coef_ptr; 1.227 + 1.228 + 1.229 +LOCAL(void) 1.230 +start_iMCU_row (j_compress_ptr cinfo) 1.231 +/* Reset within-iMCU-row counters for a new row */ 1.232 +{ 1.233 + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 1.234 + 1.235 + /* In an interleaved scan, an MCU row is the same as an iMCU row. 1.236 + * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. 1.237 + * But at the bottom of the image, process only what's left. 1.238 + */ 1.239 + if (cinfo->comps_in_scan > 1) { 1.240 + coef->MCU_rows_per_iMCU_row = 1; 1.241 + } else { 1.242 + if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1)) 1.243 + coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; 1.244 + else 1.245 + coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; 1.246 + } 1.247 + 1.248 + coef->mcu_ctr = 0; 1.249 + coef->MCU_vert_offset = 0; 1.250 +} 1.251 + 1.252 + 1.253 +/* 1.254 + * Initialize for a processing pass. 1.255 + */ 1.256 + 1.257 +METHODDEF(void) 1.258 +start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode) 1.259 +{ 1.260 + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 1.261 + 1.262 + if (pass_mode != JBUF_CRANK_DEST) 1.263 + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); 1.264 + 1.265 + coef->iMCU_row_num = 0; 1.266 + start_iMCU_row(cinfo); 1.267 +} 1.268 + 1.269 + 1.270 +/* 1.271 + * Process some data. 1.272 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row) 1.273 + * per call, ie, v_samp_factor block rows for each component in the scan. 1.274 + * The data is obtained from the virtual arrays and fed to the entropy coder. 1.275 + * Returns TRUE if the iMCU row is completed, FALSE if suspended. 1.276 + * 1.277 + * NB: input_buf is ignored; it is likely to be a NULL pointer. 1.278 + */ 1.279 + 1.280 +METHODDEF(boolean) 1.281 +compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf) 1.282 +{ 1.283 + my_coef_ptr coef = (my_coef_ptr) cinfo->coef; 1.284 + JDIMENSION MCU_col_num; /* index of current MCU within row */ 1.285 + JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; 1.286 + JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; 1.287 + int blkn, ci, xindex, yindex, yoffset, blockcnt; 1.288 + JDIMENSION start_col; 1.289 + JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; 1.290 + JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU]; 1.291 + JBLOCKROW buffer_ptr; 1.292 + jpeg_component_info *compptr; 1.293 + 1.294 + /* Align the virtual buffers for the components used in this scan. */ 1.295 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.296 + compptr = cinfo->cur_comp_info[ci]; 1.297 + buffer[ci] = (*cinfo->mem->access_virt_barray) 1.298 + ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], 1.299 + coef->iMCU_row_num * compptr->v_samp_factor, 1.300 + (JDIMENSION) compptr->v_samp_factor, FALSE); 1.301 + } 1.302 + 1.303 + /* Loop to process one whole iMCU row */ 1.304 + for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; 1.305 + yoffset++) { 1.306 + for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row; 1.307 + MCU_col_num++) { 1.308 + /* Construct list of pointers to DCT blocks belonging to this MCU */ 1.309 + blkn = 0; /* index of current DCT block within MCU */ 1.310 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.311 + compptr = cinfo->cur_comp_info[ci]; 1.312 + start_col = MCU_col_num * compptr->MCU_width; 1.313 + blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width 1.314 + : compptr->last_col_width; 1.315 + for (yindex = 0; yindex < compptr->MCU_height; yindex++) { 1.316 + if (coef->iMCU_row_num < last_iMCU_row || 1.317 + yindex+yoffset < compptr->last_row_height) { 1.318 + /* Fill in pointers to real blocks in this row */ 1.319 + buffer_ptr = buffer[ci][yindex+yoffset] + start_col; 1.320 + for (xindex = 0; xindex < blockcnt; xindex++) 1.321 + MCU_buffer[blkn++] = buffer_ptr++; 1.322 + } else { 1.323 + /* At bottom of image, need a whole row of dummy blocks */ 1.324 + xindex = 0; 1.325 + } 1.326 + /* Fill in any dummy blocks needed in this row. 1.327 + * Dummy blocks are filled in the same way as in jccoefct.c: 1.328 + * all zeroes in the AC entries, DC entries equal to previous 1.329 + * block's DC value. The init routine has already zeroed the 1.330 + * AC entries, so we need only set the DC entries correctly. 1.331 + */ 1.332 + for (; xindex < compptr->MCU_width; xindex++) { 1.333 + MCU_buffer[blkn] = coef->dummy_buffer[blkn]; 1.334 + MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0]; 1.335 + blkn++; 1.336 + } 1.337 + } 1.338 + } 1.339 + /* Try to write the MCU. */ 1.340 + if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) { 1.341 + /* Suspension forced; update state counters and exit */ 1.342 + coef->MCU_vert_offset = yoffset; 1.343 + coef->mcu_ctr = MCU_col_num; 1.344 + return FALSE; 1.345 + } 1.346 + } 1.347 + /* Completed an MCU row, but perhaps not an iMCU row */ 1.348 + coef->mcu_ctr = 0; 1.349 + } 1.350 + /* Completed the iMCU row, advance counters for next one */ 1.351 + coef->iMCU_row_num++; 1.352 + start_iMCU_row(cinfo); 1.353 + return TRUE; 1.354 +} 1.355 + 1.356 + 1.357 +/* 1.358 + * Initialize coefficient buffer controller. 1.359 + * 1.360 + * Each passed coefficient array must be the right size for that 1.361 + * coefficient: width_in_blocks wide and height_in_blocks high, 1.362 + * with unitheight at least v_samp_factor. 1.363 + */ 1.364 + 1.365 +LOCAL(void) 1.366 +transencode_coef_controller (j_compress_ptr cinfo, 1.367 + jvirt_barray_ptr * coef_arrays) 1.368 +{ 1.369 + my_coef_ptr coef; 1.370 + JBLOCKROW buffer; 1.371 + int i; 1.372 + 1.373 + coef = (my_coef_ptr) 1.374 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.375 + SIZEOF(my_coef_controller)); 1.376 + cinfo->coef = (struct jpeg_c_coef_controller *) coef; 1.377 + coef->pub.start_pass = start_pass_coef; 1.378 + coef->pub.compress_data = compress_output; 1.379 + 1.380 + /* Save pointer to virtual arrays */ 1.381 + coef->whole_image = coef_arrays; 1.382 + 1.383 + /* Allocate and pre-zero space for dummy DCT blocks. */ 1.384 + buffer = (JBLOCKROW) 1.385 + (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.386 + C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); 1.387 + jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); 1.388 + for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) { 1.389 + coef->dummy_buffer[i] = buffer + i; 1.390 + } 1.391 +}