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