7 inline_replace_color - replace one color with another in an image, using Inline
11 perl inline_replace_color fromcolor tocolor inimage outimage
15 This is a simple demonstration of using Imager with Inline::C to
16 replace one color in an image with another.
18 Most of the work is done in the inline_replace_color() function.
32 or die "Usage: $0 fromcolor tocolor inimage outimage\n";
34 # convert the colors into objects
35 my $from_color = Imager::Color->new($from)
36 or die "Cannot convert fromcolor $from into a color: ", Imager->errstr, "\n";
38 my $to_color = Imager::Color->new($to)
39 or die "Cannot convert tocolor $to into a color: ", Imager->errstr, "\n";
42 my $img = Imager->new;
44 or die "Cannot read image $in: ", $img->errstr, "\n";
46 # unlike the transform2() version this works in place
47 inline_replace_color($img, $from_color, $to_color);
49 $img->write(file=>$out)
50 or die "Cannot write image $out: ", $img->errstr, "\n";
52 =item inline_replace_color
56 inline_replace_color($in_image, $from_color, $to_color);
58 Returns a new image object with colors replaced.
62 use Inline C => <<'EOS' => WITH => 'Imager';
64 inline_replace_color(Imager::ImgRaw img, Imager::Color from, Imager::Color to) {
68 for (x = 0; x < img->xsize; ++x) {
69 for (y = 0; y < img->ysize; ++y) {
71 i_gpix(img, x, y, &c);
72 for (ch = 0; ch < img->channels; ++ch) {
73 if (c.channel[ch] != from->channel[ch]) {
79 i_ppix(img, x, y, to);
95 Tony Cook <tony@develop-help.com>
99 Imager, Imager::Inline, Imager::API, Imager::Color, Imager::Files