rev |
line source |
nuclear@26
|
1 /*
|
nuclear@26
|
2 * jcsample.c
|
nuclear@26
|
3 *
|
nuclear@26
|
4 * Copyright (C) 1991-1996, Thomas G. Lane.
|
nuclear@26
|
5 * This file is part of the Independent JPEG Group's software.
|
nuclear@26
|
6 * For conditions of distribution and use, see the accompanying README file.
|
nuclear@26
|
7 *
|
nuclear@26
|
8 * This file contains downsampling routines.
|
nuclear@26
|
9 *
|
nuclear@26
|
10 * Downsampling input data is counted in "row groups". A row group
|
nuclear@26
|
11 * is defined to be max_v_samp_factor pixel rows of each component,
|
nuclear@26
|
12 * from which the downsampler produces v_samp_factor sample rows.
|
nuclear@26
|
13 * A single row group is processed in each call to the downsampler module.
|
nuclear@26
|
14 *
|
nuclear@26
|
15 * The downsampler is responsible for edge-expansion of its output data
|
nuclear@26
|
16 * to fill an integral number of DCT blocks horizontally. The source buffer
|
nuclear@26
|
17 * may be modified if it is helpful for this purpose (the source buffer is
|
nuclear@26
|
18 * allocated wide enough to correspond to the desired output width).
|
nuclear@26
|
19 * The caller (the prep controller) is responsible for vertical padding.
|
nuclear@26
|
20 *
|
nuclear@26
|
21 * The downsampler may request "context rows" by setting need_context_rows
|
nuclear@26
|
22 * during startup. In this case, the input arrays will contain at least
|
nuclear@26
|
23 * one row group's worth of pixels above and below the passed-in data;
|
nuclear@26
|
24 * the caller will create dummy rows at image top and bottom by replicating
|
nuclear@26
|
25 * the first or last real pixel row.
|
nuclear@26
|
26 *
|
nuclear@26
|
27 * An excellent reference for image resampling is
|
nuclear@26
|
28 * Digital Image Warping, George Wolberg, 1990.
|
nuclear@26
|
29 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
|
nuclear@26
|
30 *
|
nuclear@26
|
31 * The downsampling algorithm used here is a simple average of the source
|
nuclear@26
|
32 * pixels covered by the output pixel. The hi-falutin sampling literature
|
nuclear@26
|
33 * refers to this as a "box filter". In general the characteristics of a box
|
nuclear@26
|
34 * filter are not very good, but for the specific cases we normally use (1:1
|
nuclear@26
|
35 * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
|
nuclear@26
|
36 * nearly so bad. If you intend to use other sampling ratios, you'd be well
|
nuclear@26
|
37 * advised to improve this code.
|
nuclear@26
|
38 *
|
nuclear@26
|
39 * A simple input-smoothing capability is provided. This is mainly intended
|
nuclear@26
|
40 * for cleaning up color-dithered GIF input files (if you find it inadequate,
|
nuclear@26
|
41 * we suggest using an external filtering program such as pnmconvol). When
|
nuclear@26
|
42 * enabled, each input pixel P is replaced by a weighted sum of itself and its
|
nuclear@26
|
43 * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF,
|
nuclear@26
|
44 * where SF = (smoothing_factor / 1024).
|
nuclear@26
|
45 * Currently, smoothing is only supported for 2h2v sampling factors.
|
nuclear@26
|
46 */
|
nuclear@26
|
47
|
nuclear@26
|
48 #define JPEG_INTERNALS
|
nuclear@26
|
49 #include "jinclude.h"
|
nuclear@26
|
50 #include "jpeglib.h"
|
nuclear@26
|
51
|
nuclear@26
|
52
|
nuclear@26
|
53 /* Pointer to routine to downsample a single component */
|
nuclear@26
|
54 typedef JMETHOD(void, downsample1_ptr,
|
nuclear@26
|
55 (j_compress_ptr cinfo, jpeg_component_info * compptr,
|
nuclear@26
|
56 JSAMPARRAY input_data, JSAMPARRAY output_data));
|
nuclear@26
|
57
|
nuclear@26
|
58 /* Private subobject */
|
nuclear@26
|
59
|
nuclear@26
|
60 typedef struct {
|
nuclear@26
|
61 struct jpeg_downsampler pub; /* public fields */
|
nuclear@26
|
62
|
nuclear@26
|
63 /* Downsampling method pointers, one per component */
|
nuclear@26
|
64 downsample1_ptr methods[MAX_COMPONENTS];
|
nuclear@26
|
65 } my_downsampler;
|
nuclear@26
|
66
|
nuclear@26
|
67 typedef my_downsampler * my_downsample_ptr;
|
nuclear@26
|
68
|
nuclear@26
|
69
|
nuclear@26
|
70 /*
|
nuclear@26
|
71 * Initialize for a downsampling pass.
|
nuclear@26
|
72 */
|
nuclear@26
|
73
|
nuclear@26
|
74 METHODDEF(void)
|
nuclear@26
|
75 start_pass_downsample (j_compress_ptr cinfo)
|
nuclear@26
|
76 {
|
nuclear@26
|
77 /* no work for now */
|
nuclear@26
|
78 }
|
nuclear@26
|
79
|
nuclear@26
|
80
|
nuclear@26
|
81 /*
|
nuclear@26
|
82 * Expand a component horizontally from width input_cols to width output_cols,
|
nuclear@26
|
83 * by duplicating the rightmost samples.
|
nuclear@26
|
84 */
|
nuclear@26
|
85
|
nuclear@26
|
86 LOCAL(void)
|
nuclear@26
|
87 expand_right_edge (JSAMPARRAY image_data, int num_rows,
|
nuclear@26
|
88 JDIMENSION input_cols, JDIMENSION output_cols)
|
nuclear@26
|
89 {
|
nuclear@26
|
90 register JSAMPROW ptr;
|
nuclear@26
|
91 register JSAMPLE pixval;
|
nuclear@26
|
92 register int count;
|
nuclear@26
|
93 int row;
|
nuclear@26
|
94 int numcols = (int) (output_cols - input_cols);
|
nuclear@26
|
95
|
nuclear@26
|
96 if (numcols > 0) {
|
nuclear@26
|
97 for (row = 0; row < num_rows; row++) {
|
nuclear@26
|
98 ptr = image_data[row] + input_cols;
|
nuclear@26
|
99 pixval = ptr[-1]; /* don't need GETJSAMPLE() here */
|
nuclear@26
|
100 for (count = numcols; count > 0; count--)
|
nuclear@26
|
101 *ptr++ = pixval;
|
nuclear@26
|
102 }
|
nuclear@26
|
103 }
|
nuclear@26
|
104 }
|
nuclear@26
|
105
|
nuclear@26
|
106
|
nuclear@26
|
107 /*
|
nuclear@26
|
108 * Do downsampling for a whole row group (all components).
|
nuclear@26
|
109 *
|
nuclear@26
|
110 * In this version we simply downsample each component independently.
|
nuclear@26
|
111 */
|
nuclear@26
|
112
|
nuclear@26
|
113 METHODDEF(void)
|
nuclear@26
|
114 sep_downsample (j_compress_ptr cinfo,
|
nuclear@26
|
115 JSAMPIMAGE input_buf, JDIMENSION in_row_index,
|
nuclear@26
|
116 JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
|
nuclear@26
|
117 {
|
nuclear@26
|
118 my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
|
nuclear@26
|
119 int ci;
|
nuclear@26
|
120 jpeg_component_info * compptr;
|
nuclear@26
|
121 JSAMPARRAY in_ptr, out_ptr;
|
nuclear@26
|
122
|
nuclear@26
|
123 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
nuclear@26
|
124 ci++, compptr++) {
|
nuclear@26
|
125 in_ptr = input_buf[ci] + in_row_index;
|
nuclear@26
|
126 out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
|
nuclear@26
|
127 (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
|
nuclear@26
|
128 }
|
nuclear@26
|
129 }
|
nuclear@26
|
130
|
nuclear@26
|
131
|
nuclear@26
|
132 /*
|
nuclear@26
|
133 * Downsample pixel values of a single component.
|
nuclear@26
|
134 * One row group is processed per call.
|
nuclear@26
|
135 * This version handles arbitrary integral sampling ratios, without smoothing.
|
nuclear@26
|
136 * Note that this version is not actually used for customary sampling ratios.
|
nuclear@26
|
137 */
|
nuclear@26
|
138
|
nuclear@26
|
139 METHODDEF(void)
|
nuclear@26
|
140 int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
|
nuclear@26
|
141 JSAMPARRAY input_data, JSAMPARRAY output_data)
|
nuclear@26
|
142 {
|
nuclear@26
|
143 int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
|
nuclear@26
|
144 JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */
|
nuclear@26
|
145 JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
|
nuclear@26
|
146 JSAMPROW inptr, outptr;
|
nuclear@26
|
147 INT32 outvalue;
|
nuclear@26
|
148
|
nuclear@26
|
149 h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
|
nuclear@26
|
150 v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
|
nuclear@26
|
151 numpix = h_expand * v_expand;
|
nuclear@26
|
152 numpix2 = numpix/2;
|
nuclear@26
|
153
|
nuclear@26
|
154 /* Expand input data enough to let all the output samples be generated
|
nuclear@26
|
155 * by the standard loop. Special-casing padded output would be more
|
nuclear@26
|
156 * efficient.
|
nuclear@26
|
157 */
|
nuclear@26
|
158 expand_right_edge(input_data, cinfo->max_v_samp_factor,
|
nuclear@26
|
159 cinfo->image_width, output_cols * h_expand);
|
nuclear@26
|
160
|
nuclear@26
|
161 inrow = 0;
|
nuclear@26
|
162 for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
|
nuclear@26
|
163 outptr = output_data[outrow];
|
nuclear@26
|
164 for (outcol = 0, outcol_h = 0; outcol < output_cols;
|
nuclear@26
|
165 outcol++, outcol_h += h_expand) {
|
nuclear@26
|
166 outvalue = 0;
|
nuclear@26
|
167 for (v = 0; v < v_expand; v++) {
|
nuclear@26
|
168 inptr = input_data[inrow+v] + outcol_h;
|
nuclear@26
|
169 for (h = 0; h < h_expand; h++) {
|
nuclear@26
|
170 outvalue += (INT32) GETJSAMPLE(*inptr++);
|
nuclear@26
|
171 }
|
nuclear@26
|
172 }
|
nuclear@26
|
173 *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
|
nuclear@26
|
174 }
|
nuclear@26
|
175 inrow += v_expand;
|
nuclear@26
|
176 }
|
nuclear@26
|
177 }
|
nuclear@26
|
178
|
nuclear@26
|
179
|
nuclear@26
|
180 /*
|
nuclear@26
|
181 * Downsample pixel values of a single component.
|
nuclear@26
|
182 * This version handles the special case of a full-size component,
|
nuclear@26
|
183 * without smoothing.
|
nuclear@26
|
184 */
|
nuclear@26
|
185
|
nuclear@26
|
186 METHODDEF(void)
|
nuclear@26
|
187 fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
|
nuclear@26
|
188 JSAMPARRAY input_data, JSAMPARRAY output_data)
|
nuclear@26
|
189 {
|
nuclear@26
|
190 /* Copy the data */
|
nuclear@26
|
191 jcopy_sample_rows(input_data, 0, output_data, 0,
|
nuclear@26
|
192 cinfo->max_v_samp_factor, cinfo->image_width);
|
nuclear@26
|
193 /* Edge-expand */
|
nuclear@26
|
194 expand_right_edge(output_data, cinfo->max_v_samp_factor,
|
nuclear@26
|
195 cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
|
nuclear@26
|
196 }
|
nuclear@26
|
197
|
nuclear@26
|
198
|
nuclear@26
|
199 /*
|
nuclear@26
|
200 * Downsample pixel values of a single component.
|
nuclear@26
|
201 * This version handles the common case of 2:1 horizontal and 1:1 vertical,
|
nuclear@26
|
202 * without smoothing.
|
nuclear@26
|
203 *
|
nuclear@26
|
204 * A note about the "bias" calculations: when rounding fractional values to
|
nuclear@26
|
205 * integer, we do not want to always round 0.5 up to the next integer.
|
nuclear@26
|
206 * If we did that, we'd introduce a noticeable bias towards larger values.
|
nuclear@26
|
207 * Instead, this code is arranged so that 0.5 will be rounded up or down at
|
nuclear@26
|
208 * alternate pixel locations (a simple ordered dither pattern).
|
nuclear@26
|
209 */
|
nuclear@26
|
210
|
nuclear@26
|
211 METHODDEF(void)
|
nuclear@26
|
212 h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
|
nuclear@26
|
213 JSAMPARRAY input_data, JSAMPARRAY output_data)
|
nuclear@26
|
214 {
|
nuclear@26
|
215 int outrow;
|
nuclear@26
|
216 JDIMENSION outcol;
|
nuclear@26
|
217 JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
|
nuclear@26
|
218 register JSAMPROW inptr, outptr;
|
nuclear@26
|
219 register int bias;
|
nuclear@26
|
220
|
nuclear@26
|
221 /* Expand input data enough to let all the output samples be generated
|
nuclear@26
|
222 * by the standard loop. Special-casing padded output would be more
|
nuclear@26
|
223 * efficient.
|
nuclear@26
|
224 */
|
nuclear@26
|
225 expand_right_edge(input_data, cinfo->max_v_samp_factor,
|
nuclear@26
|
226 cinfo->image_width, output_cols * 2);
|
nuclear@26
|
227
|
nuclear@26
|
228 for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
|
nuclear@26
|
229 outptr = output_data[outrow];
|
nuclear@26
|
230 inptr = input_data[outrow];
|
nuclear@26
|
231 bias = 0; /* bias = 0,1,0,1,... for successive samples */
|
nuclear@26
|
232 for (outcol = 0; outcol < output_cols; outcol++) {
|
nuclear@26
|
233 *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
|
nuclear@26
|
234 + bias) >> 1);
|
nuclear@26
|
235 bias ^= 1; /* 0=>1, 1=>0 */
|
nuclear@26
|
236 inptr += 2;
|
nuclear@26
|
237 }
|
nuclear@26
|
238 }
|
nuclear@26
|
239 }
|
nuclear@26
|
240
|
nuclear@26
|
241
|
nuclear@26
|
242 /*
|
nuclear@26
|
243 * Downsample pixel values of a single component.
|
nuclear@26
|
244 * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
|
nuclear@26
|
245 * without smoothing.
|
nuclear@26
|
246 */
|
nuclear@26
|
247
|
nuclear@26
|
248 METHODDEF(void)
|
nuclear@26
|
249 h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
|
nuclear@26
|
250 JSAMPARRAY input_data, JSAMPARRAY output_data)
|
nuclear@26
|
251 {
|
nuclear@26
|
252 int inrow, outrow;
|
nuclear@26
|
253 JDIMENSION outcol;
|
nuclear@26
|
254 JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
|
nuclear@26
|
255 register JSAMPROW inptr0, inptr1, outptr;
|
nuclear@26
|
256 register int bias;
|
nuclear@26
|
257
|
nuclear@26
|
258 /* Expand input data enough to let all the output samples be generated
|
nuclear@26
|
259 * by the standard loop. Special-casing padded output would be more
|
nuclear@26
|
260 * efficient.
|
nuclear@26
|
261 */
|
nuclear@26
|
262 expand_right_edge(input_data, cinfo->max_v_samp_factor,
|
nuclear@26
|
263 cinfo->image_width, output_cols * 2);
|
nuclear@26
|
264
|
nuclear@26
|
265 inrow = 0;
|
nuclear@26
|
266 for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
|
nuclear@26
|
267 outptr = output_data[outrow];
|
nuclear@26
|
268 inptr0 = input_data[inrow];
|
nuclear@26
|
269 inptr1 = input_data[inrow+1];
|
nuclear@26
|
270 bias = 1; /* bias = 1,2,1,2,... for successive samples */
|
nuclear@26
|
271 for (outcol = 0; outcol < output_cols; outcol++) {
|
nuclear@26
|
272 *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
|
nuclear@26
|
273 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
|
nuclear@26
|
274 + bias) >> 2);
|
nuclear@26
|
275 bias ^= 3; /* 1=>2, 2=>1 */
|
nuclear@26
|
276 inptr0 += 2; inptr1 += 2;
|
nuclear@26
|
277 }
|
nuclear@26
|
278 inrow += 2;
|
nuclear@26
|
279 }
|
nuclear@26
|
280 }
|
nuclear@26
|
281
|
nuclear@26
|
282
|
nuclear@26
|
283 #ifdef INPUT_SMOOTHING_SUPPORTED
|
nuclear@26
|
284
|
nuclear@26
|
285 /*
|
nuclear@26
|
286 * Downsample pixel values of a single component.
|
nuclear@26
|
287 * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
|
nuclear@26
|
288 * with smoothing. One row of context is required.
|
nuclear@26
|
289 */
|
nuclear@26
|
290
|
nuclear@26
|
291 METHODDEF(void)
|
nuclear@26
|
292 h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
|
nuclear@26
|
293 JSAMPARRAY input_data, JSAMPARRAY output_data)
|
nuclear@26
|
294 {
|
nuclear@26
|
295 int inrow, outrow;
|
nuclear@26
|
296 JDIMENSION colctr;
|
nuclear@26
|
297 JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
|
nuclear@26
|
298 register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
|
nuclear@26
|
299 INT32 membersum, neighsum, memberscale, neighscale;
|
nuclear@26
|
300
|
nuclear@26
|
301 /* Expand input data enough to let all the output samples be generated
|
nuclear@26
|
302 * by the standard loop. Special-casing padded output would be more
|
nuclear@26
|
303 * efficient.
|
nuclear@26
|
304 */
|
nuclear@26
|
305 expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
|
nuclear@26
|
306 cinfo->image_width, output_cols * 2);
|
nuclear@26
|
307
|
nuclear@26
|
308 /* We don't bother to form the individual "smoothed" input pixel values;
|
nuclear@26
|
309 * we can directly compute the output which is the average of the four
|
nuclear@26
|
310 * smoothed values. Each of the four member pixels contributes a fraction
|
nuclear@26
|
311 * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
|
nuclear@26
|
312 * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
|
nuclear@26
|
313 * output. The four corner-adjacent neighbor pixels contribute a fraction
|
nuclear@26
|
314 * SF to just one smoothed pixel, or SF/4 to the final output; while the
|
nuclear@26
|
315 * eight edge-adjacent neighbors contribute SF to each of two smoothed
|
nuclear@26
|
316 * pixels, or SF/2 overall. In order to use integer arithmetic, these
|
nuclear@26
|
317 * factors are scaled by 2^16 = 65536.
|
nuclear@26
|
318 * Also recall that SF = smoothing_factor / 1024.
|
nuclear@26
|
319 */
|
nuclear@26
|
320
|
nuclear@26
|
321 memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
|
nuclear@26
|
322 neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
|
nuclear@26
|
323
|
nuclear@26
|
324 inrow = 0;
|
nuclear@26
|
325 for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
|
nuclear@26
|
326 outptr = output_data[outrow];
|
nuclear@26
|
327 inptr0 = input_data[inrow];
|
nuclear@26
|
328 inptr1 = input_data[inrow+1];
|
nuclear@26
|
329 above_ptr = input_data[inrow-1];
|
nuclear@26
|
330 below_ptr = input_data[inrow+2];
|
nuclear@26
|
331
|
nuclear@26
|
332 /* Special case for first column: pretend column -1 is same as column 0 */
|
nuclear@26
|
333 membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
|
nuclear@26
|
334 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
|
nuclear@26
|
335 neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
|
nuclear@26
|
336 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
|
nuclear@26
|
337 GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
|
nuclear@26
|
338 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
|
nuclear@26
|
339 neighsum += neighsum;
|
nuclear@26
|
340 neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
|
nuclear@26
|
341 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
|
nuclear@26
|
342 membersum = membersum * memberscale + neighsum * neighscale;
|
nuclear@26
|
343 *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
|
nuclear@26
|
344 inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
|
nuclear@26
|
345
|
nuclear@26
|
346 for (colctr = output_cols - 2; colctr > 0; colctr--) {
|
nuclear@26
|
347 /* sum of pixels directly mapped to this output element */
|
nuclear@26
|
348 membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
|
nuclear@26
|
349 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
|
nuclear@26
|
350 /* sum of edge-neighbor pixels */
|
nuclear@26
|
351 neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
|
nuclear@26
|
352 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
|
nuclear@26
|
353 GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
|
nuclear@26
|
354 GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
|
nuclear@26
|
355 /* The edge-neighbors count twice as much as corner-neighbors */
|
nuclear@26
|
356 neighsum += neighsum;
|
nuclear@26
|
357 /* Add in the corner-neighbors */
|
nuclear@26
|
358 neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
|
nuclear@26
|
359 GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
|
nuclear@26
|
360 /* form final output scaled up by 2^16 */
|
nuclear@26
|
361 membersum = membersum * memberscale + neighsum * neighscale;
|
nuclear@26
|
362 /* round, descale and output it */
|
nuclear@26
|
363 *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
|
nuclear@26
|
364 inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
|
nuclear@26
|
365 }
|
nuclear@26
|
366
|
nuclear@26
|
367 /* Special case for last column */
|
nuclear@26
|
368 membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
|
nuclear@26
|
369 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
|
nuclear@26
|
370 neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
|
nuclear@26
|
371 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
|
nuclear@26
|
372 GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
|
nuclear@26
|
373 GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
|
nuclear@26
|
374 neighsum += neighsum;
|
nuclear@26
|
375 neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
|
nuclear@26
|
376 GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
|
nuclear@26
|
377 membersum = membersum * memberscale + neighsum * neighscale;
|
nuclear@26
|
378 *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
|
nuclear@26
|
379
|
nuclear@26
|
380 inrow += 2;
|
nuclear@26
|
381 }
|
nuclear@26
|
382 }
|
nuclear@26
|
383
|
nuclear@26
|
384
|
nuclear@26
|
385 /*
|
nuclear@26
|
386 * Downsample pixel values of a single component.
|
nuclear@26
|
387 * This version handles the special case of a full-size component,
|
nuclear@26
|
388 * with smoothing. One row of context is required.
|
nuclear@26
|
389 */
|
nuclear@26
|
390
|
nuclear@26
|
391 METHODDEF(void)
|
nuclear@26
|
392 fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
|
nuclear@26
|
393 JSAMPARRAY input_data, JSAMPARRAY output_data)
|
nuclear@26
|
394 {
|
nuclear@26
|
395 int outrow;
|
nuclear@26
|
396 JDIMENSION colctr;
|
nuclear@26
|
397 JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
|
nuclear@26
|
398 register JSAMPROW inptr, above_ptr, below_ptr, outptr;
|
nuclear@26
|
399 INT32 membersum, neighsum, memberscale, neighscale;
|
nuclear@26
|
400 int colsum, lastcolsum, nextcolsum;
|
nuclear@26
|
401
|
nuclear@26
|
402 /* Expand input data enough to let all the output samples be generated
|
nuclear@26
|
403 * by the standard loop. Special-casing padded output would be more
|
nuclear@26
|
404 * efficient.
|
nuclear@26
|
405 */
|
nuclear@26
|
406 expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
|
nuclear@26
|
407 cinfo->image_width, output_cols);
|
nuclear@26
|
408
|
nuclear@26
|
409 /* Each of the eight neighbor pixels contributes a fraction SF to the
|
nuclear@26
|
410 * smoothed pixel, while the main pixel contributes (1-8*SF). In order
|
nuclear@26
|
411 * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
|
nuclear@26
|
412 * Also recall that SF = smoothing_factor / 1024.
|
nuclear@26
|
413 */
|
nuclear@26
|
414
|
nuclear@26
|
415 memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
|
nuclear@26
|
416 neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
|
nuclear@26
|
417
|
nuclear@26
|
418 for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
|
nuclear@26
|
419 outptr = output_data[outrow];
|
nuclear@26
|
420 inptr = input_data[outrow];
|
nuclear@26
|
421 above_ptr = input_data[outrow-1];
|
nuclear@26
|
422 below_ptr = input_data[outrow+1];
|
nuclear@26
|
423
|
nuclear@26
|
424 /* Special case for first column */
|
nuclear@26
|
425 colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
|
nuclear@26
|
426 GETJSAMPLE(*inptr);
|
nuclear@26
|
427 membersum = GETJSAMPLE(*inptr++);
|
nuclear@26
|
428 nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
|
nuclear@26
|
429 GETJSAMPLE(*inptr);
|
nuclear@26
|
430 neighsum = colsum + (colsum - membersum) + nextcolsum;
|
nuclear@26
|
431 membersum = membersum * memberscale + neighsum * neighscale;
|
nuclear@26
|
432 *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
|
nuclear@26
|
433 lastcolsum = colsum; colsum = nextcolsum;
|
nuclear@26
|
434
|
nuclear@26
|
435 for (colctr = output_cols - 2; colctr > 0; colctr--) {
|
nuclear@26
|
436 membersum = GETJSAMPLE(*inptr++);
|
nuclear@26
|
437 above_ptr++; below_ptr++;
|
nuclear@26
|
438 nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
|
nuclear@26
|
439 GETJSAMPLE(*inptr);
|
nuclear@26
|
440 neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
|
nuclear@26
|
441 membersum = membersum * memberscale + neighsum * neighscale;
|
nuclear@26
|
442 *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
|
nuclear@26
|
443 lastcolsum = colsum; colsum = nextcolsum;
|
nuclear@26
|
444 }
|
nuclear@26
|
445
|
nuclear@26
|
446 /* Special case for last column */
|
nuclear@26
|
447 membersum = GETJSAMPLE(*inptr);
|
nuclear@26
|
448 neighsum = lastcolsum + (colsum - membersum) + colsum;
|
nuclear@26
|
449 membersum = membersum * memberscale + neighsum * neighscale;
|
nuclear@26
|
450 *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
|
nuclear@26
|
451
|
nuclear@26
|
452 }
|
nuclear@26
|
453 }
|
nuclear@26
|
454
|
nuclear@26
|
455 #endif /* INPUT_SMOOTHING_SUPPORTED */
|
nuclear@26
|
456
|
nuclear@26
|
457
|
nuclear@26
|
458 /*
|
nuclear@26
|
459 * Module initialization routine for downsampling.
|
nuclear@26
|
460 * Note that we must select a routine for each component.
|
nuclear@26
|
461 */
|
nuclear@26
|
462
|
nuclear@26
|
463 GLOBAL(void)
|
nuclear@26
|
464 jinit_downsampler (j_compress_ptr cinfo)
|
nuclear@26
|
465 {
|
nuclear@26
|
466 my_downsample_ptr downsample;
|
nuclear@26
|
467 int ci;
|
nuclear@26
|
468 jpeg_component_info * compptr;
|
nuclear@26
|
469 boolean smoothok = TRUE;
|
nuclear@26
|
470
|
nuclear@26
|
471 downsample = (my_downsample_ptr)
|
nuclear@26
|
472 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@26
|
473 SIZEOF(my_downsampler));
|
nuclear@26
|
474 cinfo->downsample = (struct jpeg_downsampler *) downsample;
|
nuclear@26
|
475 downsample->pub.start_pass = start_pass_downsample;
|
nuclear@26
|
476 downsample->pub.downsample = sep_downsample;
|
nuclear@26
|
477 downsample->pub.need_context_rows = FALSE;
|
nuclear@26
|
478
|
nuclear@26
|
479 if (cinfo->CCIR601_sampling)
|
nuclear@26
|
480 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
|
nuclear@26
|
481
|
nuclear@26
|
482 /* Verify we can handle the sampling factors, and set up method pointers */
|
nuclear@26
|
483 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
nuclear@26
|
484 ci++, compptr++) {
|
nuclear@26
|
485 if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
|
nuclear@26
|
486 compptr->v_samp_factor == cinfo->max_v_samp_factor) {
|
nuclear@26
|
487 #ifdef INPUT_SMOOTHING_SUPPORTED
|
nuclear@26
|
488 if (cinfo->smoothing_factor) {
|
nuclear@26
|
489 downsample->methods[ci] = fullsize_smooth_downsample;
|
nuclear@26
|
490 downsample->pub.need_context_rows = TRUE;
|
nuclear@26
|
491 } else
|
nuclear@26
|
492 #endif
|
nuclear@26
|
493 downsample->methods[ci] = fullsize_downsample;
|
nuclear@26
|
494 } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
|
nuclear@26
|
495 compptr->v_samp_factor == cinfo->max_v_samp_factor) {
|
nuclear@26
|
496 smoothok = FALSE;
|
nuclear@26
|
497 downsample->methods[ci] = h2v1_downsample;
|
nuclear@26
|
498 } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
|
nuclear@26
|
499 compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
|
nuclear@26
|
500 #ifdef INPUT_SMOOTHING_SUPPORTED
|
nuclear@26
|
501 if (cinfo->smoothing_factor) {
|
nuclear@26
|
502 downsample->methods[ci] = h2v2_smooth_downsample;
|
nuclear@26
|
503 downsample->pub.need_context_rows = TRUE;
|
nuclear@26
|
504 } else
|
nuclear@26
|
505 #endif
|
nuclear@26
|
506 downsample->methods[ci] = h2v2_downsample;
|
nuclear@26
|
507 } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
|
nuclear@26
|
508 (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
|
nuclear@26
|
509 smoothok = FALSE;
|
nuclear@26
|
510 downsample->methods[ci] = int_downsample;
|
nuclear@26
|
511 } else
|
nuclear@26
|
512 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
|
nuclear@26
|
513 }
|
nuclear@26
|
514
|
nuclear@26
|
515 #ifdef INPUT_SMOOTHING_SUPPORTED
|
nuclear@26
|
516 if (cinfo->smoothing_factor && !smoothok)
|
nuclear@26
|
517 TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
|
nuclear@26
|
518 #endif
|
nuclear@26
|
519 }
|