Skip to content

Commit

Permalink
More support for autodetection of formats, still some error comes up in
Browse files Browse the repository at this point in the history
t50basicoo.t.
  • Loading branch information
Arnar Mar Hrafnkelsson committed Aug 20, 2002
1 parent df54d9c commit 84e5129
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 28 deletions.
33 changes: 14 additions & 19 deletions Imager.pm
Expand Up @@ -890,14 +890,14 @@ sub settag {
}
}

my @needseekcb = qw/tiff/;
my %needseekcb = map { $_, $_ } @needseekcb;


sub _get_reader_io {
my ($self, $input, $type) = @_;
my ($self, $input) = @_;

if ($input->{fd}) {
if ($input->{io}) {
return $input->{io}, undef;
}
elsif ($input->{fd}) {
return io_new_fd($input->{fd});
}
elsif ($input->{fh}) {
Expand All @@ -921,8 +921,8 @@ sub _get_reader_io {
return io_new_buffer($input->{data});
}
elsif ($input->{callback} || $input->{readcb}) {
if ($needseekcb{$type} && !$input->{seekcb}) {
$self->_set_error("Format $type needs a seekcb parameter");
if (!$input->{seekcb}) {
$self->_set_error("Need a seekcb parameter");
}
if ($input->{maxbuffer}) {
return io_new_cb($input->{writecb},
Expand Down Expand Up @@ -1011,23 +1011,18 @@ sub read {
# has been there for half a year dude.
# Look, i just work here, ok?

if (!$input{'type'} and $input{file}) {
$input{'type'}=$FORMATGUESS->($input{file});
}
my ($IO, $fh) = $self->_get_reader_io(\%input) or return;

unless ($input{'type'}) {
$self->_set_error('type parameter missing and not possible to guess from extension');
$input{'type'} = i_test_format_probe($IO, -1);
}

unless ($input{'type'}) {
$self->_set_error('type parameter missing and not possible to guess from extension');
return undef;
}
if (!$formats{$input{'type'}}) {
$self->{ERRSTR}='format not supported'; return undef;
}

my %iolready=(jpeg=>1, png=>1, tiff=>1, pnm=>1, raw=>1, bmp=>1, tga=>1, rgb=>1, gif=>1);

# Setup data source
my ($IO, $fh) = $self->_get_reader_io(\%input, $input{'type'})
or return;

if ( $input{'type'} eq 'jpeg' ) {
($self->{IMG},$self->{IPTCRAW})=i_readjpeg_wiol( $IO );
if ( !defined($self->{IMG}) ) {
Expand Down
40 changes: 32 additions & 8 deletions image.c
Expand Up @@ -2131,6 +2131,20 @@ int i_free_gen_write_data(i_gen_write_data *info, int flush)



/*
=item i_test_format_probe(io_glue *data, int length)
Cleans up the write buffer.
Will flush any left-over data if I<flush> is non-zero.
Returns non-zero if flush is zero or if info->cb() returns non-zero.
Return zero only if flush is non-zero and info->cb() returns zero.
ie. if it fails.
=cut
*/


char *
Expand All @@ -2143,33 +2157,43 @@ i_test_format_probe(io_glue *data, int length) {
{"\xFF\xD8", "jpeg"},
{"GIF87a", "gif"},
{"GIF89a", "gif"},
{"MM\0*", "tiff"},
{"II*\0", "tiff"},
{"BM", "bmp"},
{"\x89PNG\x0d\x0a\x1a\x0a", "png"},
{"P1", "pnm"},
{"P2", "pnm"},
{"P3", "pnm"},
{"P4", "pnm"},
{"P5", "pnm"},
{"P6", "pnm"},
{"MM\0*", "tiff"},
{"II*\0", "tiff"},
{"BM", "bmp"},
{"\x89PNG\x0d\x0a\x1a\x0a", "png"}
};
unsigned int i;
char head[8];
char head[18];
char *match = NULL;
ssize_t rc;

io_glue_commit_types(data);
data->readcb(data, head, 8);
rc = data->readcb(data, head, 18);
if (rc == -1) return NULL;
data->seekcb(data, -rc, SEEK_CUR);

for(i=0; i<sizeof(formats)/sizeof(formats[0]); i++) {
int c = !strncmp(formats[i].magic, head, strlen(formats[i].magic));
int c;
ssize_t len = strlen(formats[i].magic);
if (rc<len) continue;
c = !strncmp(formats[i].magic, head, len);
if (c) {
match = formats[i].name;
break;
}
}

data->seekcb(data, -8, SEEK_CUR);


if (!match &&
(rc == 18) &&
tga_header_verify(head)) return "tga";
return match;
}

Expand Down
2 changes: 2 additions & 0 deletions image.h
Expand Up @@ -563,6 +563,8 @@ undef_int i_writeppm_wiol(i_img *im, io_glue *ig);
extern int i_writebmp_wiol(i_img *im, io_glue *ig);
extern i_img *i_readbmp_wiol(io_glue *ig);

int tga_header_verify(unsigned char headbuf[18]);

i_img * i_readtga_wiol(io_glue *ig, int length);
undef_int i_writetga_wiol(i_img *img, io_glue *ig, int wierdpack, int compress, char *idstring, size_t idlen);

Expand Down
2 changes: 2 additions & 0 deletions t/t101jpeg.t
Expand Up @@ -44,10 +44,12 @@ if (!i_has_format("jpeg")) {
$diff < 10000 or print "not ";
print "ok 3\n";

Imager::log_entry("Starting 4\n", 1);
my $imoo = Imager->new;
$imoo->read(file=>'testout/t101.jpg') or print "not ";
print "ok 4\n";
$imoo->write(file=>'testout/t101_oo.jpg') or print "not ";
Imager::log_entry("Starting 5\n", 1);
print "ok 5\n";
my $oocmp = Imager->new;
$oocmp->read(file=>'testout/t101_oo.jpg') or print "not ";
Expand Down
34 changes: 33 additions & 1 deletion tga.c
Expand Up @@ -321,6 +321,36 @@ tga_header_unpack(tga_header *header, unsigned char headbuf[18]) {
}



int
tga_header_verify(unsigned char headbuf[18]) {
tga_header header;
tga_header_unpack(&header, headbuf);
switch (header.datatypecode) {
default:
printf("bad typecode!\n");
return 0;
case 0:
case 1: /* Uncompressed, color-mapped images */
case 2: /* Uncompressed, rgb images */
case 3: /* Uncompressed, grayscale images */
case 9: /* Compressed, color-mapped images */
case 10: /* Compressed, rgb images */
case 11: /* Compressed, grayscale images */
}

switch (header.colourmaptype) {
default:
printf("bad colourmaptype!\n");
return 0;
case 0:
case 1:
}

return 1;
}


/*
=item tga_header_pack(header, headbuf)
Expand Down Expand Up @@ -388,9 +418,11 @@ tga_source_read(tga_source *s, unsigned char *buf, size_t pixels) {
s->len = (s->hdr &~(1<<7))+1;
s->state = (s->hdr & (1<<7)) ? Rle : Raw;
{
/*
static cnt = 0;
printf("%04d %s: %d\n", cnt++, s->state==Rle?"RLE":"RAW", s->len);
}
*/
}
if (s->state == Rle && s->ig->readcb(s->ig, s->cval, s->bytepp) != s->bytepp) return 0;

break;
Expand Down

0 comments on commit 84e5129

Please sign in to comment.