7 inline_replace_color.pl - replace one color with another in an image, using Inline
11 perl inline_replace_color.pl fromcolor tocolor inimage outimage
13 perl inline_replace_color.pl white 808080 foo.jpg bar.png
17 This is a simple demonstration of using Imager with Inline::C to
18 replace one color in an image with another.
20 Most of the work is done in the inline_replace_color() function.
34 or die "Usage: $0 fromcolor tocolor inimage outimage\n";
36 # convert the colors into objects
37 my $from_color = Imager::Color->new($from)
38 or die "Cannot convert fromcolor $from into a color: ", Imager->errstr, "\n";
40 my $to_color = Imager::Color->new($to)
41 or die "Cannot convert tocolor $to into a color: ", Imager->errstr, "\n";
44 my $img = Imager->new;
46 or die "Cannot read image $in: ", $img->errstr, "\n";
48 # unlike the transform2() version this works in place
49 inline_replace_color($img, $from_color, $to_color);
51 $img->write(file=>$out)
52 or die "Cannot write image $out: ", $img->errstr, "\n";
54 =item inline_replace_color
58 inline_replace_color($in_image, $from_color, $to_color);
60 Returns a new image object with colors replaced.
64 use Inline C => <<'EOS' => WITH => 'Imager';
66 inline_replace_color(Imager::ImgRaw img, Imager::Color from, Imager::Color to) {
70 for (x = 0; x < img->xsize; ++x) {
71 for (y = 0; y < img->ysize; ++y) {
73 i_gpix(img, x, y, &c);
74 for (ch = 0; ch < img->channels; ++ch) {
75 if (c.channel[ch] != from->channel[ch]) {
81 i_ppix(img, x, y, to);
97 Tony Cook <tony@develop-help.com>
101 Imager, Imager::Inline, Imager::API, Imager::Color, Imager::Files