]> git.imager.perl.org - imager.git/blob - t/t103raw.t
Added i_bumpmap_complex and corresponding filterentry and documentation.
[imager.git] / t / t103raw.t
1 #!perl -w
2 print "1..15\n";
3 use Imager qw(:all);
4 use strict;
5 init_log("testout/t103raw.log",1);
6
7 my $green=i_color_new(0,255,0,255);
8 my $blue=i_color_new(0,0,255,255);
9 my $red=i_color_new(255,0,0,255);
10
11 my $img=Imager::ImgRaw::new(150,150,3);
12 my $cmpimg=Imager::ImgRaw::new(150,150,3);
13
14 i_box_filled($img,70,25,130,125,$green);
15 i_box_filled($img,20,25,80,125,$blue);
16 i_arc($img,75,75,30,0,361,$red);
17 i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
18
19 my $timg = Imager::ImgRaw::new(20, 20, 4);
20 my $trans = i_color_new(255, 0, 0, 127);
21 i_box_filled($timg, 0, 0, 20, 20, $green);
22 i_box_filled($timg, 2, 2, 18, 18, $trans);
23
24 open(FH,">testout/t103.raw") || die "Cannot open testout/t103.raw for writing\n";
25 binmode(FH);
26 my $IO = Imager::io_new_fd( fileno(FH) );
27 i_writeraw_wiol($img, $IO) || die "Cannot write testout/t103.raw\n";
28 close(FH);
29
30 print "ok 1\n";
31
32 open(FH,"testout/t103.raw") || die "Cannot open testout/t103.raw\n";
33 binmode(FH);
34 $IO = Imager::io_new_fd( fileno(FH) );
35 $cmpimg = i_readraw_wiol($IO, 150, 150, 3, 3, 0) || die "Cannot read testout/t103.raw\n";
36 close(FH);
37
38 print "# raw average mean square pixel difference: ",sqrt(i_img_diff($img,$cmpimg))/150*150,"\n";
39 print "ok 2\n";
40
41 # I could have kept the raw images for these tests in binary files in
42 # testimg/, but I think keeping them as hex encoded data in here makes
43 # it simpler to add more if necessary
44 # Later we may change this to read from a scalar instead
45 save_data('testout/t103_base.raw');
46 save_data('testout/t103_3to4.raw');
47 save_data('testout/t103_line_int.raw');
48 save_data('testout/t103_img_int.raw');
49
50 # load the base image
51 open FH, "testout/t103_base.raw" 
52   or die "Cannot open testout/t103_base.raw: $!";
53 binmode FH;
54 $IO = Imager::io_new_fd( fileno(FH) );
55
56 my $baseimg = i_readraw_wiol( $IO, 4, 4, 3, 3, 0)
57   or die "Cannot read base raw image";
58 close FH;
59
60 # the actual read tests
61 # each read_test() call does 2 tests:
62 #  - check if the read succeeds
63 #  - check if it matches $baseimg
64 read_test('testout/t103_3to4.raw', 4, 4, 4, 3, 0, $baseimg, 3);
65 read_test('testout/t103_line_int.raw', 4, 4, 3, 3, 1, $baseimg, 5);
66 # intrl==2 is documented in raw.c but doesn't seem to be implemented
67 #read_test('testout/t103_img_int.raw', 4, 4, 3, 3, 2, $baseimg, 7);
68
69 # paletted images
70 my $palim = Imager::i_img_pal_new(20, 20, 3, 256)
71   or print "not ";
72 print "ok 7\n";
73 my $redindex = Imager::i_addcolors($palim, $red);
74 my $blueindex = Imager::i_addcolors($palim, $blue);
75 for my $y (0..9) {
76   Imager::i_ppal($palim, 0, $y, ($redindex) x 20);
77 }
78 for my $y (10..19) {
79   Imager::i_ppal($palim, 0, $y, ($blueindex) x 20);
80 }
81 open FH, "> testout/t103_pal.raw"
82   or die "Cannot create testout/t103_pal.raw: $!";
83 binmode FH;
84 $IO = Imager::io_new_fd(fileno(FH));
85 i_writeraw_wiol($palim, $IO) or print "not ";
86 print "ok 8\n";
87 close FH;
88
89 open FH, "testout/t103_pal.raw"
90   or die "Cannot open testout/t103_pal.raw: $!";
91 binmode FH;
92 my $data = do { local $/; <FH> };
93 $data eq "\x0" x 200 . "\x1" x 200
94   or print "not ";
95 print "ok 9\n";
96
97 # 16-bit image
98 # we don't have 16-bit reads yet
99 my $img16 = Imager::i_img_16_new(150, 150, 3)
100   or print "not ";
101 print "ok 10\n";
102 i_box_filled($img16,70,25,130,125,$green);
103 i_box_filled($img16,20,25,80,125,$blue);
104 i_arc($img16,75,75,30,0,361,$red);
105 i_conv($img16,[0.1, 0.2, 0.4, 0.2, 0.1]);
106
107 open FH, "> testout/t103_16.raw" 
108   or die "Cannot create testout/t103_16.raw: $!";
109 binmode FH;
110 $IO = Imager::io_new_fd(fileno(FH));
111 i_writeraw_wiol($img16, $IO) or print "not ";
112 print "ok 11\n";
113 close FH;
114
115 # try a simple virtual image
116 my $maskimg = Imager::i_img_masked_new($img, undef, 0, 0, 150, 150)
117   or print "not ";
118 print "ok 12\n";
119
120 open FH, "> testout/t103_virt.raw" 
121   or die "Cannot create testout/t103_virt.raw: $!";
122 binmode FH;
123 $IO = Imager::io_new_fd(fileno(FH));
124 i_writeraw_wiol($maskimg, $IO) or print "not ";
125 print "ok 13\n";
126 close FH;
127
128 open FH, "testout/t103_virt.raw"
129   or die "Cannot open testout/t103_virt.raw: $!";
130 binmode FH;
131 $IO = Imager::io_new_fd(fileno(FH));
132 my $cmpimgmask = i_readraw_wiol($IO, 150, 150, 3, 3, 0)
133   or print "not ";
134 print "ok 14\n";
135 my $diff = i_img_diff($maskimg, $cmpimgmask);
136 print "# difference for virtual image $diff\n";
137 $diff and print "not ";
138 print "ok 15\n";
139
140 sub read_test {
141   my ($in, $xsize, $ysize, $data, $store, $intrl, $base, $test) = @_;
142   open FH, $in or die "Cannot open $in: $!";
143   binmode FH;
144   my $IO = Imager::io_new_fd( fileno(FH) );
145
146   my $img = i_readraw_wiol($IO, $xsize, $ysize, $data, $store, $intrl);
147   if ($img) {
148     print "ok $test\n";
149     if (i_img_diff($img, $baseimg)) {
150       print "ok ",$test+1," # skip images don't match, but maybe I don't understand\n";
151     }
152     else {
153       print "ok ",$test+1,"\n";
154     }
155   }
156   else {
157     print "not ok $test # could not read image\n";
158     print "ok ",$test+1," # skip\n";
159   }
160 }
161
162 sub save_data {
163   my $outname = shift;
164   my $data = load_data();
165   open FH, "> $outname" or die "Cannot create $outname: $!";
166   binmode FH;
167   print FH $data;
168   close FH;
169 }
170
171 sub load_data {
172   my $hex = '';
173   while (<DATA>) {
174     next if /^#/;
175     last if /^EOF/;
176     chomp;
177     $hex .= $_;
178   }
179   $hex =~ tr/ //d;
180   my $result = pack("H*", $hex);
181   #print unpack("H*", $result),"\n";
182   return $result;
183 }
184
185 # FIXME: may need tests for 1,2,4 channel images
186
187 __DATA__
188 # we keep some packed raw images here
189 # we decode this in the code, ignoring lines starting with #, a subfile
190 # ends with EOF, data is HEX encoded (spaces ignored)
191
192 # basic 3 channel version of the image
193 001122 011223 021324 031425
194 102132 112233 122334 132435
195 203142 213243 223344 233445
196 304152 314253 324354 334455
197 EOF
198
199 # test image for reading a 4 channel image into a 3 channel image
200 # 4 x 4 pixels
201 00112233 01122334 02132435 03142536
202 10213243 11223344 12233445 13243546
203 20314253 21324354 22334455 23344556
204 30415263 31425364 32435465 33445566
205 EOF
206
207 # test image for line based interlacing
208 # 4 x 4 pixels
209 # first line
210 00 01 02 03
211 11 12 13 14
212 22 23 24 25
213
214 # second line
215 10 11 12 13
216 21 22 23 24
217 32 33 34 35
218
219 # third line
220 20 21 22 23
221 31 32 33 34
222 42 43 44 45
223
224 # fourth line
225 30 31 32 33
226 41 42 43 44
227 52 53 54 55
228
229 EOF
230
231 # test image for image based interlacing
232 # first channel
233 00 01 02 03
234 10 11 12 13
235 20 21 22 23
236 30 31 32 33
237
238 # second channel
239 11 12 13 14
240 21 22 23 24
241 31 32 33 34
242 41 42 43 44
243
244 # third channel
245 22 23 24 25
246 32 33 34 35
247 42 43 44 45
248 52 53 54 55
249
250 EOF