Debian Codegraph @

examples/images_videos/image_compare/image_compare.dpr

raw
1 {
2 Copyright 2016-2021 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Command-line tool to compare 2 images.
17 See README.md for details. }
18
19 uses SysUtils, CastleUtils, CastleVectors, CastleImages, CastleParameters;
20 const
21 Tolerance = 5;
22 var
23 Image1, Image2: TRGBImage;
24 X, Y: Integer;
25 Ptr1: PVector3Byte;
26 Ptr2: PVector3Byte;
27 begin
28 Parameters.CheckHigh(2);
29
30 Image1 := nil;
31 Image2 := nil;
32 try
33 Image1 := LoadImage(Parameters[1], [TRGBImage]) as TRGBImage;
34 Image2 := LoadImage(Parameters[2], [TRGBImage]) as TRGBImage;
35
36 if (Image1.Width <> Image2.Width) or
37 (Image1.Height <> Image2.Height) then
38 begin
39 Writeln(ErrOutput, Format('Image sizes differ: %dx%d vs %dx%d',
40 [Image1.Width, Image1.Height,
41 Image2.Width, Image2.Height]));
42 Halt(1);
43 end else
44 begin
45 Ptr1 := Image1.Pixels;
46 Ptr2 := Image2.Pixels;
47 { Count Y downward, to show respective to top in error message,
48 which is more common for image editing programs. }
49 for Y := Integer(Image1.Height) - 1 downto 0 do
50 for X := 0 to Integer(Image1.Width) - 1 do
51 begin
52 if (Abs(Ptr1^[0] - Ptr2^[0]) > Tolerance) or
53 (Abs(Ptr1^[1] - Ptr2^[1]) > Tolerance) or
54 (Abs(Ptr1^[2] - Ptr2^[2]) > Tolerance) then
55 begin
56 Writeln(ErrOutput, Format('Image colors differ on pixel (%d,%d) (counted from the top-left): first image has (%d,%d,%d) vs second image (%d,%d,%d)',
57 [X, Y, Ptr1^[0], Ptr1^[1], Ptr1^[2], Ptr2^[0], Ptr2^[1], Ptr2^[2]]));
58 Halt(1);
59 end;
60 Inc(Ptr1);
61 Inc(Ptr2);
62 end;
63 end;
64 finally
65 FreeAndNil(Image1);
66 FreeAndNil(Image2);
67 end;
68 end.