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