diff --git a/Changes b/Changes index eca7192d..540fba7b 100644 --- a/Changes +++ b/Changes @@ -677,6 +677,7 @@ Revision history for Perl extension Imager. for images missing the rowsperstrip tag. - Fixed default parameters for crop() - Added Preliminary specialized scaling code. + - Added image type detection. ================================================================= For latest versions check the Imager-devel pages: diff --git a/Imager.pm b/Imager.pm index d358cf27..744f221c 100644 --- a/Imager.pm +++ b/Imager.pm @@ -1024,7 +1024,7 @@ sub read { # Setup data source if ( $input{'type'} eq 'jpeg' ) { - ($self->{IMG},$self->{IPTCRAW})=i_readjpeg_wiol( $IO ); + ($self->{IMG},$self->{IPTCRAW}) = i_readjpeg_wiol( $IO ); if ( !defined($self->{IMG}) ) { $self->{ERRSTR}='unable to read jpeg image'; return undef; } diff --git a/image.c b/image.c index 9448faf0..0b568a0a 100644 --- a/image.c +++ b/image.c @@ -2189,15 +2189,17 @@ i_test_format_probe(io_glue *data, int length) { } } - if (match && !strcmp(match, "jpeg")) { - unsigned int x0, x1; - rc = data->readcb(data, head, 18); - if (rc == -1) return NULL; - x0 = (unsigned char)head[0]; - x1 = (unsigned char)head[1]; - data->seekcb(data, -rc, SEEK_CUR); - printf("Jpeg reread: %x %x\n", x0, x1); - } + /* + if (match && !strcmp(match, "jpeg")) { + unsigned int x0, x1; + rc = data->readcb(data, head, 18); + if (rc == -1) return NULL; + x0 = (unsigned char)head[0]; + x1 = (unsigned char)head[1]; + data->seekcb(data, -rc, SEEK_CUR); + printf("Jpeg reread: %x %x\n", x0, x1); + } + */ if (!match && (rc == 18) && diff --git a/iolayer.c b/iolayer.c index 4ef32811..caec2a26 100644 --- a/iolayer.c +++ b/iolayer.c @@ -824,10 +824,15 @@ Creates buffers and initializes structures to read with the chosen interface. void io_glue_commit_types(io_glue *ig) { io_type inn = ig->source.type; - + mm_log((1, "io_glue_commit_types(ig %p)\n", ig)); mm_log((1, "io_glue_commit_types: source type %d (%s)\n", inn, io_type_names[inn])); - + + if (ig->flags & 0x01) { + mm_log((1, "io_glue_commit_types: type already set up\n")); + return; + } + switch (inn) { case BUFCHAIN: { @@ -887,6 +892,7 @@ io_glue_commit_types(io_glue *ig) { break; } } + ig->flags |= 0x01; /* indicate source has been setup already */ } /* @@ -931,6 +937,7 @@ io_new_bufchain() { io_glue *ig; mm_log((1, "io_new_bufchain()\n")); ig = mymalloc(sizeof(io_glue)); + memset(ig, 0, sizeof(*ig)); io_obj_setp_bufchain(&ig->source); return ig; } @@ -958,6 +965,7 @@ io_new_buffer(char *data, size_t len, closebufp closecb, void *closedata) { ig = mymalloc(sizeof(io_glue)); memset(ig, 0, sizeof(*ig)); io_obj_setp_buffer(&ig->source, data, len, closecb, closedata); + ig->flags = 0; return ig; } @@ -982,6 +990,7 @@ io_new_fd(int fd) { memset(ig, 0, sizeof(*ig)); ig->source.type = FDSEEK; ig->source.fdseek.fd = fd; + ig->flags = 0; #if 0 #ifdef _MSC_VER io_obj_setp_cb(&ig->source, (void*)fd, _read, _write, _lseek); @@ -1000,7 +1009,7 @@ io_glue *io_new_cb(void *p, readl readcb, writel writecb, seekl seekcb, mm_log((1, "io_new_cb(p %p, readcb %p, writecb %p, seekcb %p, closecb %p, " "destroycb %p)\n", p, readcb, writecb, seekcb, closecb, destroycb)); ig = mymalloc(sizeof(io_glue)); - memset(ig, 0, sizeof(ig)); + memset(ig, 0, sizeof(*ig)); io_obj_setp_cb2(&ig->source, p, readcb, writecb, seekcb, closecb, destroycb); mm_log((1, "(%p) <- io_new_cb\n", ig)); diff --git a/iolayer.h b/iolayer.h index 8d00258b..ac49727d 100644 --- a/iolayer.h +++ b/iolayer.h @@ -32,15 +32,15 @@ typedef enum { FDSEEK, FDNOSEEK, BUFFER, CBSEEK, CBNOSEEK, BUFCHAIN } io_type; typedef int ssize_t; #endif -struct _io_glue; +struct io_glue; /* Callbacks we give out */ -typedef ssize_t(*readp) (struct _io_glue *ig, void *buf, size_t count); -typedef ssize_t(*writep)(struct _io_glue *ig, const void *buf, size_t count); -typedef off_t (*seekp) (struct _io_glue *ig, off_t offset, int whence); -typedef void (*closep)(struct _io_glue *ig); -typedef ssize_t(*sizep) (struct _io_glue *ig); +typedef ssize_t(*readp) (struct io_glue *ig, void *buf, size_t count); +typedef ssize_t(*writep)(struct io_glue *ig, const void *buf, size_t count); +typedef off_t (*seekp) (struct io_glue *ig, off_t offset, int whence); +typedef void (*closep)(struct io_glue *ig); +typedef ssize_t(*sizep) (struct io_glue *ig); typedef void (*closebufp)(void *p); @@ -57,12 +57,12 @@ typedef ssize_t(*sizel) (void *p); extern char *io_type_names[]; -typedef struct _io_blink { +typedef struct io_blink { char buf[BBSIZ]; /* size_t cnt; */ size_t len; /* How large is this buffer = BBZIS for now */ - struct _io_blink *next; - struct _io_blink *prev; + struct io_blink *next; + struct io_blink *prev; } io_blink; @@ -135,7 +135,7 @@ typedef union { io_cb cb; } io_obj; -typedef struct _io_glue { +typedef struct io_glue { io_obj source; int flags; /* Flags */ void *exdata; /* Pair specific data */ @@ -159,6 +159,6 @@ io_glue *io_new_bufchain(void); io_glue *io_new_buffer(char *data, size_t len, closebufp closecb, void *closedata); io_glue *io_new_cb(void *p, readl readcb, writel writecb, seekl seekcb, closel closecb, destroyl destroycb); size_t io_slurp(io_glue *ig, unsigned char **c); -void io_glue_DESTROY(io_glue *ig); +void io_glue_DESTROY(io_glue *ig); #endif /* _IOLAYER_H_ */ diff --git a/t/t50basicoo.t b/t/t50basicoo.t index 59cf844b..f5465eb6 100644 --- a/t/t50basicoo.t +++ b/t/t50basicoo.t @@ -42,9 +42,7 @@ my $img = Imager->new(); my %files; @files{@types} = ({ file => "testout/t101.jpg" }, { file => "testout/t102.png" }, - { file => "testout/t103.raw", xsize=>150, ysize=>150 - #, type=>"raw" # TODO: was this there for a reason? - }, + { file => "testout/t103.raw", xsize=>150, ysize=>150, type=>'raw'}, { file => "testout/t104.ppm" }, { file => "testout/t105.gif" }, { file => "testout/t106.tiff" }, @@ -67,20 +65,22 @@ for my $type (@types) { my %mopts = %opts; delete $mopts{file}; - + # read from a file handle my $fh = IO::File->new($opts{file}, "r"); if (ok($fh, "opening $opts{file}")) { binmode $fh; my $fhimg = Imager->new; - Imager::log_entry("Reading file: $opts{file}\n", -1); + Imager::log_entry("Reading file: $opts{file}\n", 0); + my $fhrc = $fhimg->read(fh=>$fh, %mopts); - if (ok(!$fhrc, "check that type is required")) { + if (!ok($fhrc, "check that type is required")) { ok ($fhimg->errstr =~ /type parameter missing/, "check for no type error"); } else { skip("previous test failed"); } + $fh->seek(0, SEEK_SET), "seek after read"; if (ok($fhimg->read(fh=>$fh, %mopts, type=>$type), "read from fh")) { ok(Imager::i_img_diff($img->{IMG}, $fhimg->{IMG}) == 0, "image comparison after fh read"); @@ -89,10 +89,10 @@ for my $type (@types) { skip("no image to compare"); } ok($fh->seek(0, SEEK_SET), "seek after read"); - + # read from a fd my $fdimg = Imager->new; - if (ok($fdimg->read(fd=>fileno($fh), %mopts, type=>$type), + if (ok($fdimg->read(fd=>fileno($fh), %mopts, type=>$type), "read from fd")) { ok(Imager::i_img_diff($img->{IMG}, $fdimg->{IMG}) == 0, "image comparistion after fd read"); @@ -363,6 +363,7 @@ sub ok { ++$test_num; if ($ok) { print "ok $test_num # $msg\n"; + Imager::log_entry("ok $test_num # $msg\n", 0); } else { my $err; @@ -371,6 +372,7 @@ sub ok { my $line = "not ok $test_num # line ".(caller)[2].": $msg"; $line .= ": $err" if $err; print $line, "\n"; + Imager::log_entry($line."\n", 0); } $ok;