source: golgotha/src/i4/loaders/jpg/jcprepct.cc @ 80

Last change on this file since 80 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: 12.6 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 * jcprepct.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 compression preprocessing controller.
17 * This controller manages the color conversion, downsampling,
18 * and edge expansion steps.
19 *
20 * Most of the complexity here is associated with buffering input rows
21 * as required by the downsampler.  See the comments at the head of
22 * jcsample.c for the downsampler's needs.
23 */
24
25#define JPEG_INTERNALS
26#include "loaders/jpg/jinclude.h"
27#include "loaders/jpg/jpeglib.h"
28
29
30/* At present, jcsample.c can request context rows only for smoothing.
31 * In the future, we might also need context rows for CCIR601 sampling
32 * or other more-complex downsampling procedures.  The code to support
33 * context rows should be compiled only if needed.
34 */
35#ifdef INPUT_SMOOTHING_SUPPORTED
36#define CONTEXT_ROWS_SUPPORTED
37#endif
38
39
40/*
41 * For the simple (no-context-row) case, we just need to buffer one
42 * row group's worth of pixels for the downsampling step.  At the bottom of
43 * the image, we pad to a full row group by replicating the last pixel row.
44 * The downsampler's last output row is then replicated if needed to pad
45 * out to a full iMCU row.
46 *
47 * When providing context rows, we must buffer three row groups' worth of
48 * pixels.  Three row groups are physically allocated, but the row pointer
49 * arrays are made five row groups high, with the extra pointers above and
50 * below "wrapping around" to point to the last and first real row groups.
51 * This allows the downsampler to access the proper context rows.
52 * At the top and bottom of the image, we create dummy context rows by
53 * copying the first or last real pixel row.  This copying could be avoided
54 * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
55 * trouble on the compression side.
56 */
57
58
59/* Private buffer controller object */
60
61typedef struct {
62  struct jpeg_c_prep_controller pub; /* public fields */
63
64  /* Downsampling input buffer.  This buffer holds color-converted data
65   * until we have enough to do a downsample step.
66   */
67  JSAMPARRAY color_buf[MAX_COMPONENTS];
68
69  JDIMENSION rows_to_go;        /* counts rows remaining in source image */
70  int next_buf_row;             /* index of next row to store in color_buf */
71
72#ifdef CONTEXT_ROWS_SUPPORTED   /* only needed for context case */
73  int this_row_group;           /* starting row index of group to process */
74  int next_buf_stop;            /* downsample when we reach this index */
75#endif
76} my_prep_controller;
77
78typedef my_prep_controller * my_prep_ptr;
79
80
81/*
82 * Initialize for a processing pass.
83 */
84
85METHODDEF(void)
86start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
87{
88  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
89
90  if (pass_mode != JBUF_PASS_THRU)
91    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
92
93  /* Initialize total-height counter for detecting bottom of image */
94  prep->rows_to_go = cinfo->image_height;
95  /* Mark the conversion buffer empty */
96  prep->next_buf_row = 0;
97#ifdef CONTEXT_ROWS_SUPPORTED
98  /* Preset additional state variables for context mode.
99   * These aren't used in non-context mode, so we needn't test which mode.
100   */
101  prep->this_row_group = 0;
102  /* Set next_buf_stop to stop after two row groups have been read in. */
103  prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
104#endif
105}
106
107
108/*
109 * Expand an image vertically from height input_rows to height output_rows,
110 * by duplicating the bottom row.
111 */
112
113LOCAL(void)
114expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
115                    int input_rows, int output_rows)
116{
117  register int row;
118
119  for (row = input_rows; row < output_rows; row++) {
120    jcopy_sample_rows(image_data, input_rows-1, image_data, row,
121                      1, num_cols);
122  }
123}
124
125
126/*
127 * Process some data in the simple no-context case.
128 *
129 * Preprocessor output data is counted in "row groups".  A row group
130 * is defined to be v_samp_factor sample rows of each component.
131 * Downsampling will produce this much data from each max_v_samp_factor
132 * input rows.
133 */
134
135METHODDEF(void)
136pre_process_data (j_compress_ptr cinfo,
137                  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
138                  JDIMENSION in_rows_avail,
139                  JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
140                  JDIMENSION out_row_groups_avail)
141{
142  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
143  int numrows, ci;
144  JDIMENSION inrows;
145  jpeg_component_info * compptr;
146
147  while (*in_row_ctr < in_rows_avail &&
148         *out_row_group_ctr < out_row_groups_avail) {
149    /* Do color conversion to fill the conversion buffer. */
150    inrows = in_rows_avail - *in_row_ctr;
151    numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
152    numrows = (int) MIN((JDIMENSION) numrows, inrows);
153    (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
154                                       prep->color_buf,
155                                       (JDIMENSION) prep->next_buf_row,
156                                       numrows);
157    *in_row_ctr += numrows;
158    prep->next_buf_row += numrows;
159    prep->rows_to_go -= numrows;
160    /* If at bottom of image, pad to fill the conversion buffer. */
161    if (prep->rows_to_go == 0 &&
162        prep->next_buf_row < cinfo->max_v_samp_factor) {
163      for (ci = 0; ci < cinfo->num_components; ci++) {
164        expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
165                           prep->next_buf_row, cinfo->max_v_samp_factor);
166      }
167      prep->next_buf_row = cinfo->max_v_samp_factor;
168    }
169    /* If we've filled the conversion buffer, empty it. */
170    if (prep->next_buf_row == cinfo->max_v_samp_factor) {
171      (*cinfo->downsample->downsample) (cinfo,
172                                        prep->color_buf, (JDIMENSION) 0,
173                                        output_buf, *out_row_group_ctr);
174      prep->next_buf_row = 0;
175      (*out_row_group_ctr)++;
176    }
177    /* If at bottom of image, pad the output to a full iMCU height.
178     * Note we assume the caller is providing a one-iMCU-height output buffer!
179     */
180    if (prep->rows_to_go == 0 &&
181        *out_row_group_ctr < out_row_groups_avail) {
182      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
183           ci++, compptr++) {
184        expand_bottom_edge(output_buf[ci],
185                           compptr->width_in_blocks * DCTSIZE,
186                           (int) (*out_row_group_ctr * compptr->v_samp_factor),
187                           (int) (out_row_groups_avail * compptr->v_samp_factor));
188      }
189      *out_row_group_ctr = out_row_groups_avail;
190      break;                    /* can exit outer loop without test */
191    }
192  }
193}
194
195
196#ifdef CONTEXT_ROWS_SUPPORTED
197
198/*
199 * Process some data in the context case.
200 */
201
202METHODDEF(void)
203pre_process_context (j_compress_ptr cinfo,
204                     JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
205                     JDIMENSION in_rows_avail,
206                     JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
207                     JDIMENSION out_row_groups_avail)
208{
209  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
210  int numrows, ci;
211  int buf_height = cinfo->max_v_samp_factor * 3;
212  JDIMENSION inrows;
213
214  while (*out_row_group_ctr < out_row_groups_avail) {
215    if (*in_row_ctr < in_rows_avail) {
216      /* Do color conversion to fill the conversion buffer. */
217      inrows = in_rows_avail - *in_row_ctr;
218      numrows = prep->next_buf_stop - prep->next_buf_row;
219      numrows = (int) MIN((JDIMENSION) numrows, inrows);
220      (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
221                                         prep->color_buf,
222                                         (JDIMENSION) prep->next_buf_row,
223                                         numrows);
224      /* Pad at top of image, if first time through */
225      if (prep->rows_to_go == cinfo->image_height) {
226        for (ci = 0; ci < cinfo->num_components; ci++) {
227          int row;
228          for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
229            jcopy_sample_rows(prep->color_buf[ci], 0,
230                              prep->color_buf[ci], -row,
231                              1, cinfo->image_width);
232          }
233        }
234      }
235      *in_row_ctr += numrows;
236      prep->next_buf_row += numrows;
237      prep->rows_to_go -= numrows;
238    } else {
239      /* Return for more data, unless we are at the bottom of the image. */
240      if (prep->rows_to_go != 0)
241        break;
242      /* When at bottom of image, pad to fill the conversion buffer. */
243      if (prep->next_buf_row < prep->next_buf_stop) {
244        for (ci = 0; ci < cinfo->num_components; ci++) {
245          expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
246                             prep->next_buf_row, prep->next_buf_stop);
247        }
248        prep->next_buf_row = prep->next_buf_stop;
249      }
250    }
251    /* If we've gotten enough data, downsample a row group. */
252    if (prep->next_buf_row == prep->next_buf_stop) {
253      (*cinfo->downsample->downsample) (cinfo,
254                                        prep->color_buf,
255                                        (JDIMENSION) prep->this_row_group,
256                                        output_buf, *out_row_group_ctr);
257      (*out_row_group_ctr)++;
258      /* Advance pointers with wraparound as necessary. */
259      prep->this_row_group += cinfo->max_v_samp_factor;
260      if (prep->this_row_group >= buf_height)
261        prep->this_row_group = 0;
262      if (prep->next_buf_row >= buf_height)
263        prep->next_buf_row = 0;
264      prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
265    }
266  }
267}
268
269
270/*
271 * Create the wrapped-around downsampling input buffer needed for context mode.
272 */
273
274LOCAL(void)
275create_context_buffer (j_compress_ptr cinfo)
276{
277  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
278  int rgroup_height = cinfo->max_v_samp_factor;
279  int ci, i;
280  jpeg_component_info * compptr;
281  JSAMPARRAY true_buffer, fake_buffer;
282
283  /* Grab enough space for fake row pointers for all the components;
284   * we need five row groups' worth of pointers for each component.
285   */
286  fake_buffer = (JSAMPARRAY)
287    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
288                                (cinfo->num_components * 5 * rgroup_height) *
289                                SIZEOF(JSAMPROW));
290
291  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
292       ci++, compptr++) {
293    /* Allocate the actual buffer space (3 row groups) for this component.
294     * We make the buffer wide enough to allow the downsampler to edge-expand
295     * horizontally within the buffer, if it so chooses.
296     */
297    true_buffer = (*cinfo->mem->alloc_sarray)
298      ((j_common_ptr) cinfo, JPOOL_IMAGE,
299       (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
300                      cinfo->max_h_samp_factor) / compptr->h_samp_factor),
301       (JDIMENSION) (3 * rgroup_height));
302    /* Copy true buffer row pointers into the middle of the fake row array */
303    MEMCOPY(fake_buffer + rgroup_height, true_buffer,
304            3 * rgroup_height * SIZEOF(JSAMPROW));
305    /* Fill in the above and below wraparound pointers */
306    for (i = 0; i < rgroup_height; i++) {
307      fake_buffer[i] = true_buffer[2 * rgroup_height + i];
308      fake_buffer[4 * rgroup_height + i] = true_buffer[i];
309    }
310    prep->color_buf[ci] = fake_buffer + rgroup_height;
311    fake_buffer += 5 * rgroup_height; /* point to space for next component */
312  }
313}
314
315#endif /* CONTEXT_ROWS_SUPPORTED */
316
317
318/*
319 * Initialize preprocessing controller.
320 */
321
322GLOBAL(void)
323jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
324{
325  my_prep_ptr prep;
326  int ci;
327  jpeg_component_info * compptr;
328
329  if (need_full_buffer)         /* safety check */
330    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
331
332  prep = (my_prep_ptr)
333    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
334                                SIZEOF(my_prep_controller));
335  cinfo->prep = (struct jpeg_c_prep_controller *) prep;
336  prep->pub.start_pass = start_pass_prep;
337
338  /* Allocate the color conversion buffer.
339   * We make the buffer wide enough to allow the downsampler to edge-expand
340   * horizontally within the buffer, if it so chooses.
341   */
342  if (cinfo->downsample->need_context_rows) {
343    /* Set up to provide context rows */
344#ifdef CONTEXT_ROWS_SUPPORTED
345    prep->pub.pre_process_data = pre_process_context;
346    create_context_buffer(cinfo);
347#else
348    ERREXIT(cinfo, JERR_NOT_COMPILED);
349#endif
350  } else {
351    /* No context, just make it tall enough for one row group */
352    prep->pub.pre_process_data = pre_process_data;
353    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
354         ci++, compptr++) {
355      prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
356        ((j_common_ptr) cinfo, JPOOL_IMAGE,
357         (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
358                        cinfo->max_h_samp_factor) / compptr->h_samp_factor),
359         (JDIMENSION) cinfo->max_v_samp_factor);
360    }
361  }
362}
Note: See TracBrowser for help on using the repository browser.