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