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