Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shoaib_ahmed 0:791a779d6220 1 /*
shoaib_ahmed 0:791a779d6220 2 * jccoefct.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Copyright (C) 1994-1997, Thomas G. Lane.
shoaib_ahmed 0:791a779d6220 5 * Modified 2003-2011 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 the coefficient buffer controller for compression.
shoaib_ahmed 0:791a779d6220 10 * This controller is the top level of the JPEG compressor proper.
shoaib_ahmed 0:791a779d6220 11 * The coefficient buffer lies between forward-DCT and entropy encoding steps.
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 /* We use a full-image coefficient buffer when doing Huffman optimization,
shoaib_ahmed 0:791a779d6220 20 * and also for writing multiple-scan JPEG files. In all cases, the DCT
shoaib_ahmed 0:791a779d6220 21 * step is run during the first pass, and subsequent passes need only read
shoaib_ahmed 0:791a779d6220 22 * the buffered coefficients.
shoaib_ahmed 0:791a779d6220 23 */
shoaib_ahmed 0:791a779d6220 24 #ifdef ENTROPY_OPT_SUPPORTED
shoaib_ahmed 0:791a779d6220 25 #define FULL_COEF_BUFFER_SUPPORTED
shoaib_ahmed 0:791a779d6220 26 #else
shoaib_ahmed 0:791a779d6220 27 #ifdef C_MULTISCAN_FILES_SUPPORTED
shoaib_ahmed 0:791a779d6220 28 #define FULL_COEF_BUFFER_SUPPORTED
shoaib_ahmed 0:791a779d6220 29 #endif
shoaib_ahmed 0:791a779d6220 30 #endif
shoaib_ahmed 0:791a779d6220 31
shoaib_ahmed 0:791a779d6220 32
shoaib_ahmed 0:791a779d6220 33 /* Private buffer controller object */
shoaib_ahmed 0:791a779d6220 34
shoaib_ahmed 0:791a779d6220 35 typedef struct {
shoaib_ahmed 0:791a779d6220 36 struct jpeg_c_coef_controller pub; /* public fields */
shoaib_ahmed 0:791a779d6220 37
shoaib_ahmed 0:791a779d6220 38 JDIMENSION iMCU_row_num; /* iMCU row # within image */
shoaib_ahmed 0:791a779d6220 39 JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
shoaib_ahmed 0:791a779d6220 40 int MCU_vert_offset; /* counts MCU rows within iMCU row */
shoaib_ahmed 0:791a779d6220 41 int MCU_rows_per_iMCU_row; /* number of such rows needed */
shoaib_ahmed 0:791a779d6220 42
shoaib_ahmed 0:791a779d6220 43 /* For single-pass compression, it's sufficient to buffer just one MCU
shoaib_ahmed 0:791a779d6220 44 * (although this may prove a bit slow in practice). We allocate a
shoaib_ahmed 0:791a779d6220 45 * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
shoaib_ahmed 0:791a779d6220 46 * MCU constructed and sent. (On 80x86, the workspace is FAR even though
shoaib_ahmed 0:791a779d6220 47 * it's not really very big; this is to keep the module interfaces unchanged
shoaib_ahmed 0:791a779d6220 48 * when a large coefficient buffer is necessary.)
shoaib_ahmed 0:791a779d6220 49 * In multi-pass modes, this array points to the current MCU's blocks
shoaib_ahmed 0:791a779d6220 50 * within the virtual arrays.
shoaib_ahmed 0:791a779d6220 51 */
shoaib_ahmed 0:791a779d6220 52 JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
shoaib_ahmed 0:791a779d6220 53
shoaib_ahmed 0:791a779d6220 54 /* In multi-pass modes, we need a virtual block array for each component. */
shoaib_ahmed 0:791a779d6220 55 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
shoaib_ahmed 0:791a779d6220 56 } my_coef_controller;
shoaib_ahmed 0:791a779d6220 57
shoaib_ahmed 0:791a779d6220 58 typedef my_coef_controller * my_coef_ptr;
shoaib_ahmed 0:791a779d6220 59
shoaib_ahmed 0:791a779d6220 60
shoaib_ahmed 0:791a779d6220 61 /* Forward declarations */
shoaib_ahmed 0:791a779d6220 62 METHODDEF(boolean) compress_data
shoaib_ahmed 0:791a779d6220 63 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
shoaib_ahmed 0:791a779d6220 64 #ifdef FULL_COEF_BUFFER_SUPPORTED
shoaib_ahmed 0:791a779d6220 65 METHODDEF(boolean) compress_first_pass
shoaib_ahmed 0:791a779d6220 66 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
shoaib_ahmed 0:791a779d6220 67 METHODDEF(boolean) compress_output
shoaib_ahmed 0:791a779d6220 68 JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
shoaib_ahmed 0:791a779d6220 69 #endif
shoaib_ahmed 0:791a779d6220 70
shoaib_ahmed 0:791a779d6220 71
shoaib_ahmed 0:791a779d6220 72 LOCAL(void)
shoaib_ahmed 0:791a779d6220 73 start_iMCU_row (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 74 /* Reset within-iMCU-row counters for a new row */
shoaib_ahmed 0:791a779d6220 75 {
shoaib_ahmed 0:791a779d6220 76 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
shoaib_ahmed 0:791a779d6220 77
shoaib_ahmed 0:791a779d6220 78 /* In an interleaved scan, an MCU row is the same as an iMCU row.
shoaib_ahmed 0:791a779d6220 79 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
shoaib_ahmed 0:791a779d6220 80 * But at the bottom of the image, process only what's left.
shoaib_ahmed 0:791a779d6220 81 */
shoaib_ahmed 0:791a779d6220 82 if (cinfo->comps_in_scan > 1) {
shoaib_ahmed 0:791a779d6220 83 coef->MCU_rows_per_iMCU_row = 1;
shoaib_ahmed 0:791a779d6220 84 } else {
shoaib_ahmed 0:791a779d6220 85 if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
shoaib_ahmed 0:791a779d6220 86 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
shoaib_ahmed 0:791a779d6220 87 else
shoaib_ahmed 0:791a779d6220 88 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
shoaib_ahmed 0:791a779d6220 89 }
shoaib_ahmed 0:791a779d6220 90
shoaib_ahmed 0:791a779d6220 91 coef->mcu_ctr = 0;
shoaib_ahmed 0:791a779d6220 92 coef->MCU_vert_offset = 0;
shoaib_ahmed 0:791a779d6220 93 }
shoaib_ahmed 0:791a779d6220 94
shoaib_ahmed 0:791a779d6220 95
shoaib_ahmed 0:791a779d6220 96 /*
shoaib_ahmed 0:791a779d6220 97 * Initialize for a processing pass.
shoaib_ahmed 0:791a779d6220 98 */
shoaib_ahmed 0:791a779d6220 99
shoaib_ahmed 0:791a779d6220 100 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 101 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
shoaib_ahmed 0:791a779d6220 102 {
shoaib_ahmed 0:791a779d6220 103 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
shoaib_ahmed 0:791a779d6220 104
shoaib_ahmed 0:791a779d6220 105 coef->iMCU_row_num = 0;
shoaib_ahmed 0:791a779d6220 106 start_iMCU_row(cinfo);
shoaib_ahmed 0:791a779d6220 107
shoaib_ahmed 0:791a779d6220 108 switch (pass_mode) {
shoaib_ahmed 0:791a779d6220 109 case JBUF_PASS_THRU:
shoaib_ahmed 0:791a779d6220 110 if (coef->whole_image[0] != NULL)
shoaib_ahmed 0:791a779d6220 111 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
shoaib_ahmed 0:791a779d6220 112 coef->pub.compress_data = compress_data;
shoaib_ahmed 0:791a779d6220 113 break;
shoaib_ahmed 0:791a779d6220 114 #ifdef FULL_COEF_BUFFER_SUPPORTED
shoaib_ahmed 0:791a779d6220 115 case JBUF_SAVE_AND_PASS:
shoaib_ahmed 0:791a779d6220 116 if (coef->whole_image[0] == NULL)
shoaib_ahmed 0:791a779d6220 117 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
shoaib_ahmed 0:791a779d6220 118 coef->pub.compress_data = compress_first_pass;
shoaib_ahmed 0:791a779d6220 119 break;
shoaib_ahmed 0:791a779d6220 120 case JBUF_CRANK_DEST:
shoaib_ahmed 0:791a779d6220 121 if (coef->whole_image[0] == NULL)
shoaib_ahmed 0:791a779d6220 122 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
shoaib_ahmed 0:791a779d6220 123 coef->pub.compress_data = compress_output;
shoaib_ahmed 0:791a779d6220 124 break;
shoaib_ahmed 0:791a779d6220 125 #endif
shoaib_ahmed 0:791a779d6220 126 default:
shoaib_ahmed 0:791a779d6220 127 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
shoaib_ahmed 0:791a779d6220 128 break;
shoaib_ahmed 0:791a779d6220 129 }
shoaib_ahmed 0:791a779d6220 130 }
shoaib_ahmed 0:791a779d6220 131
shoaib_ahmed 0:791a779d6220 132
shoaib_ahmed 0:791a779d6220 133 /*
shoaib_ahmed 0:791a779d6220 134 * Process some data in the single-pass case.
shoaib_ahmed 0:791a779d6220 135 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
shoaib_ahmed 0:791a779d6220 136 * per call, ie, v_samp_factor block rows for each component in the image.
shoaib_ahmed 0:791a779d6220 137 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
shoaib_ahmed 0:791a779d6220 138 *
shoaib_ahmed 0:791a779d6220 139 * NB: input_buf contains a plane for each component in image,
shoaib_ahmed 0:791a779d6220 140 * which we index according to the component's SOF position.
shoaib_ahmed 0:791a779d6220 141 */
shoaib_ahmed 0:791a779d6220 142
shoaib_ahmed 0:791a779d6220 143 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 144 compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
shoaib_ahmed 0:791a779d6220 145 {
shoaib_ahmed 0:791a779d6220 146 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
shoaib_ahmed 0:791a779d6220 147 JDIMENSION MCU_col_num; /* index of current MCU within row */
shoaib_ahmed 0:791a779d6220 148 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
shoaib_ahmed 0:791a779d6220 149 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
shoaib_ahmed 0:791a779d6220 150 int blkn, bi, ci, yindex, yoffset, blockcnt;
shoaib_ahmed 0:791a779d6220 151 JDIMENSION ypos, xpos;
shoaib_ahmed 0:791a779d6220 152 jpeg_component_info *compptr;
shoaib_ahmed 0:791a779d6220 153 forward_DCT_ptr forward_DCT;
shoaib_ahmed 0:791a779d6220 154
shoaib_ahmed 0:791a779d6220 155 /* Loop to write as much as one whole iMCU row */
shoaib_ahmed 0:791a779d6220 156 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
shoaib_ahmed 0:791a779d6220 157 yoffset++) {
shoaib_ahmed 0:791a779d6220 158 for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
shoaib_ahmed 0:791a779d6220 159 MCU_col_num++) {
shoaib_ahmed 0:791a779d6220 160 /* Determine where data comes from in input_buf and do the DCT thing.
shoaib_ahmed 0:791a779d6220 161 * Each call on forward_DCT processes a horizontal row of DCT blocks
shoaib_ahmed 0:791a779d6220 162 * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
shoaib_ahmed 0:791a779d6220 163 * sequentially. Dummy blocks at the right or bottom edge are filled in
shoaib_ahmed 0:791a779d6220 164 * specially. The data in them does not matter for image reconstruction,
shoaib_ahmed 0:791a779d6220 165 * so we fill them with values that will encode to the smallest amount of
shoaib_ahmed 0:791a779d6220 166 * data, viz: all zeroes in the AC entries, DC entries equal to previous
shoaib_ahmed 0:791a779d6220 167 * block's DC value. (Thanks to Thomas Kinsman for this idea.)
shoaib_ahmed 0:791a779d6220 168 */
shoaib_ahmed 0:791a779d6220 169 blkn = 0;
shoaib_ahmed 0:791a779d6220 170 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shoaib_ahmed 0:791a779d6220 171 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 172 forward_DCT = cinfo->fdct->forward_DCT[compptr->component_index];
shoaib_ahmed 0:791a779d6220 173 blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
shoaib_ahmed 0:791a779d6220 174 : compptr->last_col_width;
shoaib_ahmed 0:791a779d6220 175 xpos = MCU_col_num * compptr->MCU_sample_width;
shoaib_ahmed 0:791a779d6220 176 ypos = yoffset * compptr->DCT_v_scaled_size;
shoaib_ahmed 0:791a779d6220 177 /* ypos == (yoffset+yindex) * DCTSIZE */
shoaib_ahmed 0:791a779d6220 178 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
shoaib_ahmed 0:791a779d6220 179 if (coef->iMCU_row_num < last_iMCU_row ||
shoaib_ahmed 0:791a779d6220 180 yoffset+yindex < compptr->last_row_height) {
shoaib_ahmed 0:791a779d6220 181 (*forward_DCT) (cinfo, compptr,
shoaib_ahmed 0:791a779d6220 182 input_buf[compptr->component_index],
shoaib_ahmed 0:791a779d6220 183 coef->MCU_buffer[blkn],
shoaib_ahmed 0:791a779d6220 184 ypos, xpos, (JDIMENSION) blockcnt);
shoaib_ahmed 0:791a779d6220 185 if (blockcnt < compptr->MCU_width) {
shoaib_ahmed 0:791a779d6220 186 /* Create some dummy blocks at the right edge of the image. */
shoaib_ahmed 0:791a779d6220 187 FMEMZERO((void FAR *) coef->MCU_buffer[blkn + blockcnt],
shoaib_ahmed 0:791a779d6220 188 (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
shoaib_ahmed 0:791a779d6220 189 for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
shoaib_ahmed 0:791a779d6220 190 coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
shoaib_ahmed 0:791a779d6220 191 }
shoaib_ahmed 0:791a779d6220 192 }
shoaib_ahmed 0:791a779d6220 193 } else {
shoaib_ahmed 0:791a779d6220 194 /* Create a row of dummy blocks at the bottom of the image. */
shoaib_ahmed 0:791a779d6220 195 FMEMZERO((void FAR *) coef->MCU_buffer[blkn],
shoaib_ahmed 0:791a779d6220 196 compptr->MCU_width * SIZEOF(JBLOCK));
shoaib_ahmed 0:791a779d6220 197 for (bi = 0; bi < compptr->MCU_width; bi++) {
shoaib_ahmed 0:791a779d6220 198 coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
shoaib_ahmed 0:791a779d6220 199 }
shoaib_ahmed 0:791a779d6220 200 }
shoaib_ahmed 0:791a779d6220 201 blkn += compptr->MCU_width;
shoaib_ahmed 0:791a779d6220 202 ypos += compptr->DCT_v_scaled_size;
shoaib_ahmed 0:791a779d6220 203 }
shoaib_ahmed 0:791a779d6220 204 }
shoaib_ahmed 0:791a779d6220 205 /* Try to write the MCU. In event of a suspension failure, we will
shoaib_ahmed 0:791a779d6220 206 * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
shoaib_ahmed 0:791a779d6220 207 */
shoaib_ahmed 0:791a779d6220 208 if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
shoaib_ahmed 0:791a779d6220 209 /* Suspension forced; update state counters and exit */
shoaib_ahmed 0:791a779d6220 210 coef->MCU_vert_offset = yoffset;
shoaib_ahmed 0:791a779d6220 211 coef->mcu_ctr = MCU_col_num;
shoaib_ahmed 0:791a779d6220 212 return FALSE;
shoaib_ahmed 0:791a779d6220 213 }
shoaib_ahmed 0:791a779d6220 214 }
shoaib_ahmed 0:791a779d6220 215 /* Completed an MCU row, but perhaps not an iMCU row */
shoaib_ahmed 0:791a779d6220 216 coef->mcu_ctr = 0;
shoaib_ahmed 0:791a779d6220 217 }
shoaib_ahmed 0:791a779d6220 218 /* Completed the iMCU row, advance counters for next one */
shoaib_ahmed 0:791a779d6220 219 coef->iMCU_row_num++;
shoaib_ahmed 0:791a779d6220 220 start_iMCU_row(cinfo);
shoaib_ahmed 0:791a779d6220 221 return TRUE;
shoaib_ahmed 0:791a779d6220 222 }
shoaib_ahmed 0:791a779d6220 223
shoaib_ahmed 0:791a779d6220 224
shoaib_ahmed 0:791a779d6220 225 #ifdef FULL_COEF_BUFFER_SUPPORTED
shoaib_ahmed 0:791a779d6220 226
shoaib_ahmed 0:791a779d6220 227 /*
shoaib_ahmed 0:791a779d6220 228 * Process some data in the first pass of a multi-pass case.
shoaib_ahmed 0:791a779d6220 229 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
shoaib_ahmed 0:791a779d6220 230 * per call, ie, v_samp_factor block rows for each component in the image.
shoaib_ahmed 0:791a779d6220 231 * This amount of data is read from the source buffer, DCT'd and quantized,
shoaib_ahmed 0:791a779d6220 232 * and saved into the virtual arrays. We also generate suitable dummy blocks
shoaib_ahmed 0:791a779d6220 233 * as needed at the right and lower edges. (The dummy blocks are constructed
shoaib_ahmed 0:791a779d6220 234 * in the virtual arrays, which have been padded appropriately.) This makes
shoaib_ahmed 0:791a779d6220 235 * it possible for subsequent passes not to worry about real vs. dummy blocks.
shoaib_ahmed 0:791a779d6220 236 *
shoaib_ahmed 0:791a779d6220 237 * We must also emit the data to the entropy encoder. This is conveniently
shoaib_ahmed 0:791a779d6220 238 * done by calling compress_output() after we've loaded the current strip
shoaib_ahmed 0:791a779d6220 239 * of the virtual arrays.
shoaib_ahmed 0:791a779d6220 240 *
shoaib_ahmed 0:791a779d6220 241 * NB: input_buf contains a plane for each component in image. All
shoaib_ahmed 0:791a779d6220 242 * components are DCT'd and loaded into the virtual arrays in this pass.
shoaib_ahmed 0:791a779d6220 243 * However, it may be that only a subset of the components are emitted to
shoaib_ahmed 0:791a779d6220 244 * the entropy encoder during this first pass; be careful about looking
shoaib_ahmed 0:791a779d6220 245 * at the scan-dependent variables (MCU dimensions, etc).
shoaib_ahmed 0:791a779d6220 246 */
shoaib_ahmed 0:791a779d6220 247
shoaib_ahmed 0:791a779d6220 248 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 249 compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
shoaib_ahmed 0:791a779d6220 250 {
shoaib_ahmed 0:791a779d6220 251 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
shoaib_ahmed 0:791a779d6220 252 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
shoaib_ahmed 0:791a779d6220 253 JDIMENSION blocks_across, MCUs_across, MCUindex;
shoaib_ahmed 0:791a779d6220 254 int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
shoaib_ahmed 0:791a779d6220 255 JCOEF lastDC;
shoaib_ahmed 0:791a779d6220 256 jpeg_component_info *compptr;
shoaib_ahmed 0:791a779d6220 257 JBLOCKARRAY buffer;
shoaib_ahmed 0:791a779d6220 258 JBLOCKROW thisblockrow, lastblockrow;
shoaib_ahmed 0:791a779d6220 259 forward_DCT_ptr forward_DCT;
shoaib_ahmed 0:791a779d6220 260
shoaib_ahmed 0:791a779d6220 261 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
shoaib_ahmed 0:791a779d6220 262 ci++, compptr++) {
shoaib_ahmed 0:791a779d6220 263 /* Align the virtual buffer for this component. */
shoaib_ahmed 0:791a779d6220 264 buffer = (*cinfo->mem->access_virt_barray)
shoaib_ahmed 0:791a779d6220 265 ((j_common_ptr) cinfo, coef->whole_image[ci],
shoaib_ahmed 0:791a779d6220 266 coef->iMCU_row_num * compptr->v_samp_factor,
shoaib_ahmed 0:791a779d6220 267 (JDIMENSION) compptr->v_samp_factor, TRUE);
shoaib_ahmed 0:791a779d6220 268 /* Count non-dummy DCT block rows in this iMCU row. */
shoaib_ahmed 0:791a779d6220 269 if (coef->iMCU_row_num < last_iMCU_row)
shoaib_ahmed 0:791a779d6220 270 block_rows = compptr->v_samp_factor;
shoaib_ahmed 0:791a779d6220 271 else {
shoaib_ahmed 0:791a779d6220 272 /* NB: can't use last_row_height here, since may not be set! */
shoaib_ahmed 0:791a779d6220 273 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
shoaib_ahmed 0:791a779d6220 274 if (block_rows == 0) block_rows = compptr->v_samp_factor;
shoaib_ahmed 0:791a779d6220 275 }
shoaib_ahmed 0:791a779d6220 276 blocks_across = compptr->width_in_blocks;
shoaib_ahmed 0:791a779d6220 277 h_samp_factor = compptr->h_samp_factor;
shoaib_ahmed 0:791a779d6220 278 /* Count number of dummy blocks to be added at the right margin. */
shoaib_ahmed 0:791a779d6220 279 ndummy = (int) (blocks_across % h_samp_factor);
shoaib_ahmed 0:791a779d6220 280 if (ndummy > 0)
shoaib_ahmed 0:791a779d6220 281 ndummy = h_samp_factor - ndummy;
shoaib_ahmed 0:791a779d6220 282 forward_DCT = cinfo->fdct->forward_DCT[ci];
shoaib_ahmed 0:791a779d6220 283 /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
shoaib_ahmed 0:791a779d6220 284 * on forward_DCT processes a complete horizontal row of DCT blocks.
shoaib_ahmed 0:791a779d6220 285 */
shoaib_ahmed 0:791a779d6220 286 for (block_row = 0; block_row < block_rows; block_row++) {
shoaib_ahmed 0:791a779d6220 287 thisblockrow = buffer[block_row];
shoaib_ahmed 0:791a779d6220 288 (*forward_DCT) (cinfo, compptr, input_buf[ci], thisblockrow,
shoaib_ahmed 0:791a779d6220 289 (JDIMENSION) (block_row * compptr->DCT_v_scaled_size),
shoaib_ahmed 0:791a779d6220 290 (JDIMENSION) 0, blocks_across);
shoaib_ahmed 0:791a779d6220 291 if (ndummy > 0) {
shoaib_ahmed 0:791a779d6220 292 /* Create dummy blocks at the right edge of the image. */
shoaib_ahmed 0:791a779d6220 293 thisblockrow += blocks_across; /* => first dummy block */
shoaib_ahmed 0:791a779d6220 294 FMEMZERO((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
shoaib_ahmed 0:791a779d6220 295 lastDC = thisblockrow[-1][0];
shoaib_ahmed 0:791a779d6220 296 for (bi = 0; bi < ndummy; bi++) {
shoaib_ahmed 0:791a779d6220 297 thisblockrow[bi][0] = lastDC;
shoaib_ahmed 0:791a779d6220 298 }
shoaib_ahmed 0:791a779d6220 299 }
shoaib_ahmed 0:791a779d6220 300 }
shoaib_ahmed 0:791a779d6220 301 /* If at end of image, create dummy block rows as needed.
shoaib_ahmed 0:791a779d6220 302 * The tricky part here is that within each MCU, we want the DC values
shoaib_ahmed 0:791a779d6220 303 * of the dummy blocks to match the last real block's DC value.
shoaib_ahmed 0:791a779d6220 304 * This squeezes a few more bytes out of the resulting file...
shoaib_ahmed 0:791a779d6220 305 */
shoaib_ahmed 0:791a779d6220 306 if (coef->iMCU_row_num == last_iMCU_row) {
shoaib_ahmed 0:791a779d6220 307 blocks_across += ndummy; /* include lower right corner */
shoaib_ahmed 0:791a779d6220 308 MCUs_across = blocks_across / h_samp_factor;
shoaib_ahmed 0:791a779d6220 309 for (block_row = block_rows; block_row < compptr->v_samp_factor;
shoaib_ahmed 0:791a779d6220 310 block_row++) {
shoaib_ahmed 0:791a779d6220 311 thisblockrow = buffer[block_row];
shoaib_ahmed 0:791a779d6220 312 lastblockrow = buffer[block_row-1];
shoaib_ahmed 0:791a779d6220 313 FMEMZERO((void FAR *) thisblockrow,
shoaib_ahmed 0:791a779d6220 314 (size_t) (blocks_across * SIZEOF(JBLOCK)));
shoaib_ahmed 0:791a779d6220 315 for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
shoaib_ahmed 0:791a779d6220 316 lastDC = lastblockrow[h_samp_factor-1][0];
shoaib_ahmed 0:791a779d6220 317 for (bi = 0; bi < h_samp_factor; bi++) {
shoaib_ahmed 0:791a779d6220 318 thisblockrow[bi][0] = lastDC;
shoaib_ahmed 0:791a779d6220 319 }
shoaib_ahmed 0:791a779d6220 320 thisblockrow += h_samp_factor; /* advance to next MCU in row */
shoaib_ahmed 0:791a779d6220 321 lastblockrow += h_samp_factor;
shoaib_ahmed 0:791a779d6220 322 }
shoaib_ahmed 0:791a779d6220 323 }
shoaib_ahmed 0:791a779d6220 324 }
shoaib_ahmed 0:791a779d6220 325 }
shoaib_ahmed 0:791a779d6220 326 /* NB: compress_output will increment iMCU_row_num if successful.
shoaib_ahmed 0:791a779d6220 327 * A suspension return will result in redoing all the work above next time.
shoaib_ahmed 0:791a779d6220 328 */
shoaib_ahmed 0:791a779d6220 329
shoaib_ahmed 0:791a779d6220 330 /* Emit data to the entropy encoder, sharing code with subsequent passes */
shoaib_ahmed 0:791a779d6220 331 return compress_output(cinfo, input_buf);
shoaib_ahmed 0:791a779d6220 332 }
shoaib_ahmed 0:791a779d6220 333
shoaib_ahmed 0:791a779d6220 334
shoaib_ahmed 0:791a779d6220 335 /*
shoaib_ahmed 0:791a779d6220 336 * Process some data in subsequent passes of a multi-pass case.
shoaib_ahmed 0:791a779d6220 337 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
shoaib_ahmed 0:791a779d6220 338 * per call, ie, v_samp_factor block rows for each component in the scan.
shoaib_ahmed 0:791a779d6220 339 * The data is obtained from the virtual arrays and fed to the entropy coder.
shoaib_ahmed 0:791a779d6220 340 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
shoaib_ahmed 0:791a779d6220 341 *
shoaib_ahmed 0:791a779d6220 342 * NB: input_buf is ignored; it is likely to be a NULL pointer.
shoaib_ahmed 0:791a779d6220 343 */
shoaib_ahmed 0:791a779d6220 344
shoaib_ahmed 0:791a779d6220 345 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 346 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
shoaib_ahmed 0:791a779d6220 347 {
shoaib_ahmed 0:791a779d6220 348 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
shoaib_ahmed 0:791a779d6220 349 JDIMENSION MCU_col_num; /* index of current MCU within row */
shoaib_ahmed 0:791a779d6220 350 int blkn, ci, xindex, yindex, yoffset;
shoaib_ahmed 0:791a779d6220 351 JDIMENSION start_col;
shoaib_ahmed 0:791a779d6220 352 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
shoaib_ahmed 0:791a779d6220 353 JBLOCKROW buffer_ptr;
shoaib_ahmed 0:791a779d6220 354 jpeg_component_info *compptr;
shoaib_ahmed 0:791a779d6220 355
shoaib_ahmed 0:791a779d6220 356 /* Align the virtual buffers for the components used in this scan.
shoaib_ahmed 0:791a779d6220 357 * NB: during first pass, this is safe only because the buffers will
shoaib_ahmed 0:791a779d6220 358 * already be aligned properly, so jmemmgr.c won't need to do any I/O.
shoaib_ahmed 0:791a779d6220 359 */
shoaib_ahmed 0:791a779d6220 360 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shoaib_ahmed 0:791a779d6220 361 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 362 buffer[ci] = (*cinfo->mem->access_virt_barray)
shoaib_ahmed 0:791a779d6220 363 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
shoaib_ahmed 0:791a779d6220 364 coef->iMCU_row_num * compptr->v_samp_factor,
shoaib_ahmed 0:791a779d6220 365 (JDIMENSION) compptr->v_samp_factor, FALSE);
shoaib_ahmed 0:791a779d6220 366 }
shoaib_ahmed 0:791a779d6220 367
shoaib_ahmed 0:791a779d6220 368 /* Loop to process one whole iMCU row */
shoaib_ahmed 0:791a779d6220 369 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
shoaib_ahmed 0:791a779d6220 370 yoffset++) {
shoaib_ahmed 0:791a779d6220 371 for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
shoaib_ahmed 0:791a779d6220 372 MCU_col_num++) {
shoaib_ahmed 0:791a779d6220 373 /* Construct list of pointers to DCT blocks belonging to this MCU */
shoaib_ahmed 0:791a779d6220 374 blkn = 0; /* index of current DCT block within MCU */
shoaib_ahmed 0:791a779d6220 375 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shoaib_ahmed 0:791a779d6220 376 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 377 start_col = MCU_col_num * compptr->MCU_width;
shoaib_ahmed 0:791a779d6220 378 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
shoaib_ahmed 0:791a779d6220 379 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
shoaib_ahmed 0:791a779d6220 380 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
shoaib_ahmed 0:791a779d6220 381 coef->MCU_buffer[blkn++] = buffer_ptr++;
shoaib_ahmed 0:791a779d6220 382 }
shoaib_ahmed 0:791a779d6220 383 }
shoaib_ahmed 0:791a779d6220 384 }
shoaib_ahmed 0:791a779d6220 385 /* Try to write the MCU. */
shoaib_ahmed 0:791a779d6220 386 if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
shoaib_ahmed 0:791a779d6220 387 /* Suspension forced; update state counters and exit */
shoaib_ahmed 0:791a779d6220 388 coef->MCU_vert_offset = yoffset;
shoaib_ahmed 0:791a779d6220 389 coef->mcu_ctr = MCU_col_num;
shoaib_ahmed 0:791a779d6220 390 return FALSE;
shoaib_ahmed 0:791a779d6220 391 }
shoaib_ahmed 0:791a779d6220 392 }
shoaib_ahmed 0:791a779d6220 393 /* Completed an MCU row, but perhaps not an iMCU row */
shoaib_ahmed 0:791a779d6220 394 coef->mcu_ctr = 0;
shoaib_ahmed 0:791a779d6220 395 }
shoaib_ahmed 0:791a779d6220 396 /* Completed the iMCU row, advance counters for next one */
shoaib_ahmed 0:791a779d6220 397 coef->iMCU_row_num++;
shoaib_ahmed 0:791a779d6220 398 start_iMCU_row(cinfo);
shoaib_ahmed 0:791a779d6220 399 return TRUE;
shoaib_ahmed 0:791a779d6220 400 }
shoaib_ahmed 0:791a779d6220 401
shoaib_ahmed 0:791a779d6220 402 #endif /* FULL_COEF_BUFFER_SUPPORTED */
shoaib_ahmed 0:791a779d6220 403
shoaib_ahmed 0:791a779d6220 404
shoaib_ahmed 0:791a779d6220 405 /*
shoaib_ahmed 0:791a779d6220 406 * Initialize coefficient buffer controller.
shoaib_ahmed 0:791a779d6220 407 */
shoaib_ahmed 0:791a779d6220 408
shoaib_ahmed 0:791a779d6220 409 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 410 jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
shoaib_ahmed 0:791a779d6220 411 {
shoaib_ahmed 0:791a779d6220 412 my_coef_ptr coef;
shoaib_ahmed 0:791a779d6220 413
shoaib_ahmed 0:791a779d6220 414 coef = (my_coef_ptr)
shoaib_ahmed 0:791a779d6220 415 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 416 SIZEOF(my_coef_controller));
shoaib_ahmed 0:791a779d6220 417 cinfo->coef = (struct jpeg_c_coef_controller *) coef;
shoaib_ahmed 0:791a779d6220 418 coef->pub.start_pass = start_pass_coef;
shoaib_ahmed 0:791a779d6220 419
shoaib_ahmed 0:791a779d6220 420 /* Create the coefficient buffer. */
shoaib_ahmed 0:791a779d6220 421 if (need_full_buffer) {
shoaib_ahmed 0:791a779d6220 422 #ifdef FULL_COEF_BUFFER_SUPPORTED
shoaib_ahmed 0:791a779d6220 423 /* Allocate a full-image virtual array for each component, */
shoaib_ahmed 0:791a779d6220 424 /* padded to a multiple of samp_factor DCT blocks in each direction. */
shoaib_ahmed 0:791a779d6220 425 int ci;
shoaib_ahmed 0:791a779d6220 426 jpeg_component_info *compptr;
shoaib_ahmed 0:791a779d6220 427
shoaib_ahmed 0:791a779d6220 428 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
shoaib_ahmed 0:791a779d6220 429 ci++, compptr++) {
shoaib_ahmed 0:791a779d6220 430 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
shoaib_ahmed 0:791a779d6220 431 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
shoaib_ahmed 0:791a779d6220 432 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
shoaib_ahmed 0:791a779d6220 433 (long) compptr->h_samp_factor),
shoaib_ahmed 0:791a779d6220 434 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
shoaib_ahmed 0:791a779d6220 435 (long) compptr->v_samp_factor),
shoaib_ahmed 0:791a779d6220 436 (JDIMENSION) compptr->v_samp_factor);
shoaib_ahmed 0:791a779d6220 437 }
shoaib_ahmed 0:791a779d6220 438 #else
shoaib_ahmed 0:791a779d6220 439 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
shoaib_ahmed 0:791a779d6220 440 #endif
shoaib_ahmed 0:791a779d6220 441 } else {
shoaib_ahmed 0:791a779d6220 442 /* We only need a single-MCU buffer. */
shoaib_ahmed 0:791a779d6220 443 JBLOCKROW buffer;
shoaib_ahmed 0:791a779d6220 444 int i;
shoaib_ahmed 0:791a779d6220 445
shoaib_ahmed 0:791a779d6220 446 buffer = (JBLOCKROW)
shoaib_ahmed 0:791a779d6220 447 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 448 C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
shoaib_ahmed 0:791a779d6220 449 for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
shoaib_ahmed 0:791a779d6220 450 coef->MCU_buffer[i] = buffer + i;
shoaib_ahmed 0:791a779d6220 451 }
shoaib_ahmed 0:791a779d6220 452 coef->whole_image[0] = NULL; /* flag for no virtual arrays */
shoaib_ahmed 0:791a779d6220 453 }
shoaib_ahmed 0:791a779d6220 454 }