Skip to content

Commit

Permalink
Autodetection of fileformat as loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnar Mar Hrafnkelsson committed Aug 22, 2002
1 parent 27c373c commit 527c0c3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 32 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Imager.pm
Expand Up @@ -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;
}
Expand Down
20 changes: 11 additions & 9 deletions image.c
Expand Up @@ -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) &&
Expand Down
15 changes: 12 additions & 3 deletions iolayer.c
Expand Up @@ -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:
{
Expand Down Expand Up @@ -887,6 +892,7 @@ io_glue_commit_types(io_glue *ig) {
break;
}
}
ig->flags |= 0x01; /* indicate source has been setup already */
}

/*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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));

Expand Down
22 changes: 11 additions & 11 deletions iolayer.h
Expand Up @@ -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);

Expand All @@ -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;


Expand Down Expand Up @@ -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 */
Expand All @@ -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_ */
18 changes: 10 additions & 8 deletions t/t50basicoo.t
Expand Up @@ -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" },
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 527c0c3

Please sign in to comment.