Convering ico to gif makes background black

Posted on 2007-08-26 19:12:11-07 by tjugo
Convering ico to gif makes background black
Hi,

I am using Imager 0.59 to convert ico files to gif. So far the module performs flawlessly with a little problem. After converting the ico image to gif the transparent background looks black.

I've been reading the Imager mail list archive and the documentation and after trying all the suggested tricks the converted image keeps the background black.

As an example take this image:
http://www.expedia.com/favicon.ico

basically what I am doing is:

use strict; use Imager; my $img = Imager->new(); $img->read(file=>'./favicon.ico'); $img->write(file=>'./favicon.gif');

And the resulting giff image will have a black background. I've tried stuff like:
$img->write(file=>'./favicon.gif', transp=>'threshold'); or stuff like: my $giff = $img->convert(matrix =>[ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 0 ], ]); $giff->write(file=>'./favicon.gif');

with the same result.
Any idea,
Thanks for your support!
Direct Responses: 5959 | 5961 | Write a response
Posted on 2007-08-27 00:06:37-07 by tonyc in response to 5958
Re: Convering ico to gif makes background black

Hi,

Unfortunately Imager doesn't apply the mask from the image to the color data, which results in the black background.

I'll look at changing that for 0.60, since the current behaviour is confusing.

In the meantime, I've included a simple program that will apply the mask to the image. You could adapt this for use in your code.

#!perl -w use strict; use Imager; my $in = shift; my $out = shift or die "Usage: $0 input output\n"; my $im = Imager->new; $im->read(file => $in) or die "Cannot read $in:", $im->errstr; my $format = $im->tags(name => 'i_format'); $format =~ /^(ico|cur)$/ or die "Input not a ico or cure image\n"; my $mask = $im->tags(name => "${format}_mask"); # Imager always uses * for the 1s # add an alpha channel $im = $im->convert(preset => 'addalpha'); my @mask = split /\n/, $mask; shift @mask; # lose the key for my $y (0 .. $im->getheight() - 1) { my $m = shift @mask; for my $x (0 .. $im->getwidth() - 1 ) { if (substr($m, $x, 1) eq '*') { my $c = $im->getpixel(x => $x, 'y' => $y); $im->setpixel(x => $x, 'y' => $y, color => Imager::Color->new(($c->rgba)[0,1,2], 0)); } } } $im->write(file => $out) or die "Cannot write $out: ", $im->errstr;
Direct Responses: Write a response
Posted on 2007-08-27 10:10:04-07 by tonyc in response to 5958
Re: Convering ico to gif makes background black

I've made this change for 0.60, which I expect to release this week some time.

Direct Responses: 5990 | Write a response
Posted on 2007-08-29 22:53:24-07 by tjugo in response to 5961
Re: Convering ico to gif makes background black
Thanks,
Looking forward to the new release!

tjugo
Direct Responses: Write a response