source: golgotha/src/i4/loaders/jpg/jccoefct.cc

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