3 { Original: jdsample.c; Copyright (C) 1991-1996, Thomas G. Lane. }
5 { This file contains upsampling routines.
7 Upsampling input data is counted in "row groups". A row group
8 is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
9 sample rows of each component. Upsampling will normally produce
10 max_v_samp_factor pixel rows from each row group (but this could vary
11 if the upsampler is applying a scale factor of its own).
13 An excellent reference for image resampling is
14 Digital Image Warping, George Wolberg, 1990.
15 Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.}
17 interface
19 {$I imjconfig.inc}
21 uses
22 imjmorecfg,
23 imjinclude,
24 imjutils,
25 imjpeglib,
26 imjdeferr,
27 imjerror;
30 { Pointer to routine to upsample a single component }
31 type
37 { Module initialization routine for upsampling. }
39 {GLOBAL}
42 implementation
44 { Private subobject }
46 type
51 { Color conversion buffer. When using separate upsampling and color
52 conversion steps, this buffer holds one upsampled row group until it
53 has been color converted and output.
54 Note: we do not allocate any storage for component(s) which are full-size,
55 ie do not need rescaling. The corresponding entry of color_buf[] is
56 simply set to point to the input data array, thereby avoiding copying.}
60 { Per-component upsampling method pointers }
66 { Height of an input row group for each component. }
69 { These arrays save pixel expansion factors so that int_expand need not
70 recompute them each time. They are unused for other upsampling methods.}
76 { Initialize for an upsampling pass. }
78 {METHODDEF}
80 var
82 begin
85 { Mark the conversion buffer empty }
87 { Initialize total-height counter for detecting bottom of image }
92 { Control routine to do upsampling (and color conversion).
94 In this version we upsample each component independently.
95 We upsample one row group into the conversion buffer, then apply
96 color conversion a row at a time. }
98 {METHODDEF}
106 var
111 begin
114 { Fill the conversion buffer, if it's empty }
116 begin
119 begin
120 { Invoke per-component upsample method. Notice we pass a POINTER
121 to color_buf[ci], so that fullsize_upsample can change it. }
133 { Color-convert and emit rows }
135 { How many we have in the buffer: }
137 { Not more than the distance to the end of the image. Need this test
138 in case the image height is not a multiple of max_v_samp_factor: }
142 { And not more than what the client can accept: }
153 { Adjust counts }
157 { When the buffer is emptied, declare this input row group consumed }
163 { These are the routines invoked by sep_upsample to upsample pixel values
164 of a single component. One row group is processed per call. }
167 { For full-size components, we just make color_buf[ci] point at the
168 input buffer, and thus avoid copying any data. Note that this is
169 safe only because sep_upsample doesn't declare the input row group
170 "consumed" until we are done color converting and emitting it. }
172 {METHODDEF}
177 begin
182 { This is a no-op version used for "uninteresting" components.
183 These components will not be referenced by color conversion. }
185 {METHODDEF}
190 begin
195 { This version handles any integral sampling ratios.
196 This is not used for typical JPEG files, so it need not be fast.
197 Nor, for that matter, is it particularly accurate: the algorithm is
198 simple replication of the input pixel onto the corresponding output
199 pixels. The hi-falutin sampling literature refers to this as a
200 "box filter". A box filter tends to introduce visible artifacts,
201 so if you are actually going to use 3:1 or 4:1 sampling ratios
202 you would be well advised to improve this code. }
204 {METHODDEF}
209 var
215 {outend}
218 var
220 begin
230 begin
231 { Generate one output row with proper horizontal expansion }
236 begin
240 begin
247 { Generate any additional output rows by duplicating the first one }
249 begin
259 { Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
260 It's still a box filter. }
262 {METHODDEF}
267 var
271 {outend : JSAMPROW;}
274 begin
278 begin
281 {outend := outptr + cinfo^.output_width;}
284 begin
297 { Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
298 It's still a box filter. }
300 {METHODDEF}
305 var
309 {outend : JSAMPROW;}
312 begin
318 begin
321 {outend := outptr + cinfo^.output_width;}
324 begin
341 { Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
343 The upsampling algorithm is linear interpolation between pixel centers,
344 also known as a "triangle filter". This is a good compromise between
345 speed and visual quality. The centers of the output pixels are 1/4 and 3/4
346 of the way between input pixel centers.
348 A note about the "bias" calculations: when rounding fractional values to
349 integer, we do not want to always round 0.5 up to the next integer.
350 If we did that, we'd introduce a noticeable bias towards larger values.
351 Instead, this code is arranged so that 0.5 will be rounded up or down at
352 alternate pixel locations (a simple ordered dither pattern). }
354 {METHODDEF}
359 var
365 begin
369 begin
372 { Special case for first column }
382 begin
383 { General case: 3/4 * nearer pixel + 1/4 * further pixel }
393 { Special case for last column }
398 {Inc(outptr); - value never used }
403 { Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
404 Again a triangle filter; see comments for h2v1 case, above.
406 It is OK for us to reference the adjacent input rows because we demanded
407 context from the main buffer controller (see initialization code). }
409 {METHODDEF}
414 var
417 {$ifdef BITS_IN_JSAMPLE_IS_8}
419 {$else}
421 {$endif}
424 var
426 begin
432 begin
434 begin
435 { inptr0 points to nearest input row, inptr1 points to next nearest }
438 begin
439 {inptr1 := JSAMPLE_PTR(input_data^[inrow-1]);}
443 end
449 { Special case for first column }
464 begin
465 { General case: 3/4 * nearer pixel + 1/4 * further pixel in each }
466 { dimension, thus 9/16, 3/16, 3/16, 1/16 overall }
478 { Special case for last column }
482 {Inc(outptr); - value never used }
489 { Module initialization routine for upsampling. }
491 {GLOBAL}
493 var
499 begin
511 { jdmainct.c doesn't support context rows when min_DCT_scaled_size := 1,
512 so don't ask for it. }
516 { Verify we can handle the sampling factors, select per-component methods,
517 and create storage as needed. }
521 begin
522 { Compute size of an "input group" after IDCT scaling. This many samples
523 are to be converted to max_h_samp_factor * max_v_samp_factor pixels. }
534 begin
535 { Don't bother to upsample an uninteresting component. }
538 end
539 else
541 begin
542 { Fullsize components can be processed without any work. }
545 end
546 else
549 begin
550 { Special cases for 2h1v upsampling }
553 else
555 end
556 else
559 begin
560 { Special cases for 2h2v upsampling }
562 begin
565 end
566 else
568 end
569 else
572 begin
573 { Generic integral-factors upsampling method }
577 end
578 else
581 begin