- reading a raw image no longer exits on a short read or read error,
[imager.git] / t / t103raw.t
CommitLineData
faa9b3e7 1#!perl -w
faa9b3e7 2use strict;
0bfa6bd6 3use lib 't';
5f8f8e17 4use Test::More tests => 23;
0bfa6bd6 5use Imager qw(:all);
9267f8f3
TC
6init_log("testout/t103raw.log",1);
7
faa9b3e7
TC
8my $green=i_color_new(0,255,0,255);
9my $blue=i_color_new(0,0,255,255);
10my $red=i_color_new(255,0,0,255);
9267f8f3 11
faa9b3e7
TC
12my $img=Imager::ImgRaw::new(150,150,3);
13my $cmpimg=Imager::ImgRaw::new(150,150,3);
9267f8f3
TC
14
15i_box_filled($img,70,25,130,125,$green);
16i_box_filled($img,20,25,80,125,$blue);
17i_arc($img,75,75,30,0,361,$red);
18i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
19
20my $timg = Imager::ImgRaw::new(20, 20, 4);
21my $trans = i_color_new(255, 0, 0, 127);
22i_box_filled($timg, 0, 0, 20, 20, $green);
23i_box_filled($timg, 2, 2, 18, 18, $trans);
24
25open(FH,">testout/t103.raw") || die "Cannot open testout/t103.raw for writing\n";
26binmode(FH);
faa9b3e7 27my $IO = Imager::io_new_fd( fileno(FH) );
0bfa6bd6
TC
28ok(i_writeraw_wiol($img, $IO), "write raw low") or
29 print "# Cannot write testout/t103.raw\n";
9267f8f3
TC
30close(FH);
31
9267f8f3
TC
32open(FH,"testout/t103.raw") || die "Cannot open testout/t103.raw\n";
33binmode(FH);
895dbd34 34$IO = Imager::io_new_fd( fileno(FH) );
0bfa6bd6
TC
35$cmpimg = i_readraw_wiol($IO, 150, 150, 3, 3, 0);
36ok($cmpimg, "read raw low")
37 or print "# Cannot read testout/t103.raw\n";
9267f8f3
TC
38close(FH);
39
40print "# 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
46save_data('testout/t103_base.raw');
47save_data('testout/t103_3to4.raw');
48save_data('testout/t103_line_int.raw');
49save_data('testout/t103_img_int.raw');
50
51# load the base image
52open FH, "testout/t103_base.raw"
53 or die "Cannot open testout/t103_base.raw: $!";
54binmode FH;
895dbd34
AMH
55$IO = Imager::io_new_fd( fileno(FH) );
56
0bfa6bd6
TC
57my $baseimg = i_readraw_wiol( $IO, 4, 4, 3, 3, 0);
58ok($baseimg, "read base raw image")
9267f8f3
TC
59 or die "Cannot read base raw image";
60close 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
66read_test('testout/t103_3to4.raw', 4, 4, 4, 3, 0, $baseimg);
67read_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
72SKIP:
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
102SKIP:
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
121SKIP:
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 182sub 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
197sub 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
206sub 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
228001122 011223 021324 031425
229102132 112233 122334 132435
230203142 213243 223344 233445
231304152 314253 324354 334455
232EOF
233
234# test image for reading a 4 channel image into a 3 channel image
235# 4 x 4 pixels
23600112233 01122334 02132435 03142536
23710213243 11223344 12233445 13243546
23820314253 21324354 22334455 23344556
23930415263 31425364 32435465 33445566
240EOF
241
242# test image for line based interlacing
243# 4 x 4 pixels
244# first line
24500 01 02 03
24611 12 13 14
24722 23 24 25
248
249# second line
25010 11 12 13
25121 22 23 24
25232 33 34 35
253
254# third line
25520 21 22 23
25631 32 33 34
25742 43 44 45
258
259# fourth line
26030 31 32 33
26141 42 43 44
26252 53 54 55
263
264EOF
265
266# test image for image based interlacing
267# first channel
26800 01 02 03
26910 11 12 13
27020 21 22 23
27130 31 32 33
272
273# second channel
27411 12 13 14
27521 22 23 24
27631 32 33 34
27741 42 43 44
278
279# third channel
28022 23 24 25
28132 33 34 35
28242 43 44 45
28352 53 54 55
284
285EOF