4 use Test::More tests => 23;
6 init_log("testout/t103raw.log",1);
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);
12 my $img=Imager::ImgRaw::new(150,150,3);
13 my $cmpimg=Imager::ImgRaw::new(150,150,3);
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]);
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);
25 open(FH,">testout/t103.raw") || die "Cannot open testout/t103.raw for writing\n";
27 my $IO = Imager::io_new_fd( fileno(FH) );
28 ok(i_writeraw_wiol($img, $IO), "write raw low") or
29 print "# Cannot write testout/t103.raw\n";
32 open(FH,"testout/t103.raw") || die "Cannot open testout/t103.raw\n";
34 $IO = Imager::io_new_fd( fileno(FH) );
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";
40 print "# raw average mean square pixel difference: ",sqrt(i_img_diff($img,$cmpimg))/150*150,"\n";
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');
52 open FH, "testout/t103_base.raw"
53 or die "Cannot open testout/t103_base.raw: $!";
55 $IO = Imager::io_new_fd( fileno(FH) );
57 my $baseimg = i_readraw_wiol( $IO, 4, 4, 3, 3, 0);
58 ok($baseimg, "read base raw image")
59 or die "Cannot read base raw image";
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
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);
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);
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);
80 Imager::i_ppal($palim, 0, $y, ($redindex) x 20);
83 Imager::i_ppal($palim, 0, $y, ($blueindex) x 20);
85 open FH, "> testout/t103_pal.raw"
86 or die "Cannot create testout/t103_pal.raw: $!";
88 $IO = Imager::io_new_fd(fileno(FH));
89 ok(i_writeraw_wiol($palim, $IO), "write low paletted");
92 open FH, "testout/t103_pal.raw"
93 or die "Cannot open testout/t103_pal.raw: $!";
95 my $data = do { local $/; <FH> };
96 is($data, "\x0" x 200 . "\x1" x 200, "compare paletted data written");
101 # we don't have 16-bit reads yet
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]);
112 open FH, "> testout/t103_16.raw"
113 or die "Cannot create testout/t103_16.raw: $!";
115 $IO = Imager::io_new_fd(fileno(FH));
116 ok(i_writeraw_wiol($img16, $IO), "write low 16 bit image");
120 # try a simple virtual image
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);
127 open FH, "> testout/t103_virt.raw"
128 or die "Cannot create testout/t103_virt.raw: $!";
130 $IO = Imager::io_new_fd(fileno(FH));
131 ok(i_writeraw_wiol($maskimg, $IO), "write virtual raw");
134 open FH, "testout/t103_virt.raw"
135 or die "Cannot open testout/t103_virt.raw: $!";
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");
144 # check that i_format is set correctly
145 my $index = Imager::i_tags_find($cmpimgmask, 'i_format', 0);
147 my $value = Imager::i_tags_get($cmpimgmask, $index);
148 is($value, 'raw', "check i_format value");
151 fail("couldn't find i_format tag");
155 { # error handling checks
156 # should get an error writing to a open for read file
158 open RAW, "> testout/t103_empty.raw"
159 or die "Cannot create testout/t103_empty.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");
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");
183 my ($in, $xsize, $ysize, $data, $store, $intrl, $base) = @_;
184 open FH, $in or die "Cannot open $in: $!";
186 my $IO = Imager::io_new_fd( fileno(FH) );
188 my $img = i_readraw_wiol($IO, $xsize, $ysize, $data, $store, $intrl);
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");
199 my $data = load_data();
200 open FH, "> $outname" or die "Cannot create $outname: $!";
215 my $result = pack("H*", $hex);
216 #print unpack("H*", $result),"\n";
220 # FIXME: may need tests for 1,2,4 channel images
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)
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
234 # test image for reading a 4 channel image into a 3 channel image
236 00112233 01122334 02132435 03142536
237 10213243 11223344 12233445 13243546
238 20314253 21324354 22334455 23344556
239 30415263 31425364 32435465 33445566
242 # test image for line based interlacing
266 # test image for image based interlacing