/
Decimate.pas
65 lines (49 loc) · 1.86 KB
/
Decimate.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
UNIT Decimate;
{-------------------------------------------------------------------------------------------------------------
Downsamples (decimates) an image.
2019-08-17
Params:
img Image to modify
ImgWidth Image width
ImgHeigth Image height
factor Decimation factor
k The kernel to apply (e.g. low-pass filter). Can be 0.
Rez Buffer to hold the resulting image (w/factor*ImgHeigth/factor). If 0, the result will be written to the original image buffer.
rw rh Optional. The width/height of the resulting image will be stored here.
-------------------------------------------------------------------------------------------------------------}
INTERFACE
USES System.SysUtils, SsimDef;
procedure DecimateImage(img: RealImage; ImgWidth, ImgHeigth: integer; factor: integer; k: TKernelAttrib; Rez: RealImage; OUT rw, rh: Integer);
IMPLEMENTATION
USES Convolve;
procedure DecimateImage(img: RealImage; ImgWidth, ImgHeigth: integer; factor: integer; k: TKernelAttrib; Rez: RealImage; OUT rw, rh: Integer);
var
x, y: integer;
sw, sh: Integer;
dst_offset: integer;
dst: RealImage;
begin
// test oddity
sw:= ImgWidth DIV factor;
if Odd(sw) then Inc(sw);
sh:= ImgHeigth DIV factor;
if Odd(sh) then Inc(sh);
dst:= img;
if rez <> NIL
then dst:= Rez;
// Downsample
for y:= 0 to sh-1 do
begin
dst_offset:= y * sw;
Assert(dst_offset < Length(dst), 'Invalid dst!');
for x:= 0 to sw-1 do
begin
Assert(x*factor < {=} ImgWidth, 'x:'+ IntToStr(x)+ '. factor:'+ IntToStr(factor)+ '. w:'+ IntToStr(ImgWidth)); // here was: x*factor < w
dst[dst_offset]:= FilterPixel(img, ImgWidth, ImgHeigth, x*factor, y*factor, k, 1);
Inc(dst_offset);
end;
end;
rw:= sw;
rh:= sh;
end;
end.