Imager - Re: Convering ico to gif makes background black

Posted on Mon Aug 27 03:06:37 2007 by tonyc in response to 5958 (See the whole thread of 4)
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;
Write a response