- error messages when writing PNM images were always
'unable to write pnm image', more useful messages are now reported.
- convert t/t103raw.t to Test::More
+- reading a raw image no longer exits on a short read or read error,
+ and returns an appropriate error message in $im->errstr
+- write failures when writing a raw image now return a useful
+ message in $im->errstr
=================================================================
$params{storechannels},
$params{interleave});
if ( !defined($self->{IMG}) ) {
- $self->{ERRSTR}='unable to read raw image';
+ $self->{ERRSTR}=$self->_error_as_msg();
return undef;
}
$self->{DEBUG} && print "loading a raw file\n";
$self->_set_opts(\%input, "raw_", $self)
or return undef;
if ( !i_writeraw_wiol($self->{IMG},$IO) ) {
- $self->{ERRSTR}='unable to write raw image';
+ $self->{ERRSTR} = $self->_error_as_msg();
return undef;
}
$self->{DEBUG} && print "writing a raw file\n";
=cut
*/
static ssize_t fd_read(io_glue *ig, void *buf, size_t count) {
+ ssize_t result;
#ifdef _MSC_VER
- return _read(ig->source.fdseek.fd, buf, count);
+ result = _read(ig->source.fdseek.fd, buf, count);
#else
- return read(ig->source.fdseek.fd, buf, count);
+ result = read(ig->source.fdseek.fd, buf, count);
#endif
+
+ /* 0 is valid - means EOF */
+ if (result < 0) {
+ i_push_errorf(0, "read() failure: %s (%d)", strerror(errno), errno);
+ }
+
+ return result;
}
static ssize_t fd_write(io_glue *ig, const void *buf, size_t count) {
i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, int intrl) {
i_img* im;
int rc,k;
+
+ i_clear_error();
unsigned char *inbuffer;
unsigned char *ilbuffer;
k=0;
while( k<im->ysize ) {
rc = ig->readcb(ig, inbuffer, inbuflen);
- if (rc != inbuflen) { fprintf(stderr,"Premature end of file.\n"); exit(2); }
+ if (rc != inbuflen) {
+ if (rc < 0)
+ i_push_error(0, "error reading file");
+ else
+ i_push_error(0, "premature end of file");
+ i_img_destroy(im);
+ myfree(inbuffer);
+ if (intrl != 0) myfree(ilbuffer);
+ if (datachannels != storechannels) myfree(exbuffer);
+ return NULL;
+ }
interleave(inbuffer,ilbuffer,im->xsize,datachannels);
expandchannels(ilbuffer,exbuffer,im->xsize,datachannels,storechannels);
/* FIXME: Do we ever want to save to a virtual image? */
#!perl -w
use strict;
use lib 't';
-use Test::More tests => 17;
+use Test::More tests => 23;
use Imager qw(:all);
init_log("testout/t103raw.log",1);
}
}
+{ # error handling checks
+ # should get an error writing to a open for read file
+ # make a empty file
+ open RAW, "> testout/t103_empty.raw"
+ or die "Cannot create testout/t103_empty.raw: $!";
+ close RAW;
+ open RAW, "< testout/t103_empty.raw"
+ or die "Cannot open testout/t103_empty.raw: $!";
+ my $im = Imager->new(xsize => 50, ysize=>50);
+ ok(!$im->write(fh => \*RAW, type => 'raw'),
+ "write to open for read handle");
+ cmp_ok($im->errstr, '=~', '^Could not write to file: write\(\) failure',
+ "check error message");
+ close RAW;
+
+ # should get an error reading an empty file
+ ok(!$im->read(file => 'testout/t103_empty.raw', xsize => 50, ysize=>50, type=>'raw'),
+ 'read an empty file');
+ is($im->errstr, 'premature end of file', "check message");
+ open RAW, "> testout/t103_empty.raw"
+ or die "Cannot create testout/t103_empty.raw: $!";
+ ok(!$im->read(fh => \*RAW, , xsize => 50, ysize=>50, type=>'raw'),
+ 'read a file open for write');
+ cmp_ok($im->errstr, '=~', '^error reading file: read\(\) failure', "check message");
+
+}
+
sub read_test {
my ($in, $xsize, $ysize, $data, $store, $intrl, $base) = @_;
open FH, $in or die "Cannot open $in: $!";