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