}
/*
-=item i_diff_image(im1, im2, mindiff)
+=item i_diff_image(im1, im2, mindist)
Creates a new image that is transparent, except where the pixel in im2
is different from im1, where it is the pixel from im2.
*/
i_img *
-i_diff_image(i_img *im1, i_img *im2, int mindiff) {
+i_diff_image(i_img *im1, i_img *im2, double mindist) {
i_img *out;
int outchans, diffchans;
int xsize, ysize;
i_color *line2 = mymalloc(xsize * sizeof(*line1)); /* checked 17feb2005 tonyc */
i_color empty;
int x, y, ch;
+ int imindist = (int)mindist;
for (ch = 0; ch < MAXCHANNELS; ++ch)
empty.channel[ch] = 0;
int diff = 0;
for (ch = 0; ch < diffchans; ++ch) {
if (line1[x].channel[ch] != line2[x].channel[ch]
- && abs(line1[x].channel[ch] - line2[x].channel[ch]) > mindiff) {
+ && abs(line1[x].channel[ch] - line2[x].channel[ch]) > imindist) {
diff = 1;
break;
}
i_fcolor *line2 = mymalloc(xsize * sizeof(*line2)); /* checked 17feb2005 tonyc */
i_fcolor empty;
int x, y, ch;
- double dist = mindiff / 255;
+ double dist = mindist / 255.0;
for (ch = 0; ch < MAXCHANNELS; ++ch)
empty.channel[ch] = 0;
int diff = 0;
for (ch = 0; ch < diffchans; ++ch) {
if (line1[x].channel[ch] != line2[x].channel[ch]
- && abs(line1[x].channel[ch] - line2[x].channel[ch]) > dist) {
+ && fabs(line1[x].channel[ch] - line2[x].channel[ch]) > dist) {
diff = 1;
break;
}
void i_turbnoise(i_img *im,float xo,float yo,float scale);
void i_gradgen(i_img *im, int num, int *xo, int *yo, i_color *ival, int dmeasure);
int i_nearest_color(i_img *im, int num, int *xo, int *yo, i_color *ival, int dmeasure);
-i_img *i_diff_image(i_img *im, i_img *im2, int mindist);
+i_img *i_diff_image(i_img *im, i_img *im2, double mindist);
int
i_fountain(i_img *im, double xa, double ya, double xb, double yb,
i_fountain_type type, i_fountain_repeat repeat,
#!perl -w
use strict;
use Imager qw(:handy);
-use Test::More tests => 69;
+use Test::More tests => 73;
Imager::init_log("testout/t61filters.log", 1);
-use Imager::Test qw(is_image_similar);
+use Imager::Test qw(is_image_similar test_image is_image);
# meant for testing the filters themselves
-my $imbase = Imager->new;
-$imbase->open(file=>'testout/t104.ppm') or die;
+
+my $imbase = test_image();
+
my $im_other = Imager->new(xsize=>150, ysize=>150);
$im_other->box(xmin=>30, ymin=>60, xmax=>120, ymax=>90, filled=>1);
test($imbase, { type => 'perl_test' }, 'testout/t61perl.ppm');
}
+{ # check the difference method out
+ my $im1 = Imager->new(xsize => 3, ysize => 2);
+ $im1->box(filled => 1, color => '#FF0000');
+ my $im2 = $im1->copy;
+ $im1->setpixel(x => 1, 'y' => 0, color => '#FF00FF');
+ $im2->setpixel(x => 1, 'y' => 0, color => '#FF01FF');
+ $im1->setpixel(x => 2, 'y' => 0, color => '#FF00FF');
+ $im2->setpixel(x => 2, 'y' => 0, color => '#FF02FF');
+
+ my $diff1 = $im1->difference(other => $im2);
+ my $cmp1 = Imager->new(xsize => 3, ysize => 2, channels => 4);
+ $cmp1->setpixel(x => 1, 'y' => 0, color => '#FF01FF');
+ $cmp1->setpixel(x => 2, 'y' => 0, color => '#FF02FF');
+ is_image($diff1, $cmp1, "difference() - check image with mindist 0");
+
+ my $diff2 = $im1->difference(other => $im2, mindist => 1);
+ $diff2->write(file=>'foo.png');
+ my $cmp2 = Imager->new(xsize => 3, ysize => 2, channels => 4);
+ $cmp2->setpixel(x => 2, 'y' => 0, color => '#FF02FF');
+ is_image($diff2, $cmp2, "difference() - check image with mindist 1");
+}
+
+{
+ # and again with large samples
+ my $im1 = Imager->new(xsize => 3, ysize => 2, bits => 'double');
+ $im1->box(filled => 1, color => '#FF0000');
+ my $im2 = $im1->copy;
+ $im1->setpixel(x => 1, 'y' => 0, color => '#FF00FF');
+ $im2->setpixel(x => 1, 'y' => 0, color => '#FF01FF');
+ $im1->setpixel(x => 2, 'y' => 0, color => '#FF00FF');
+ $im2->setpixel(x => 2, 'y' => 0, color => '#FF02FF');
+
+ my $diff1 = $im1->difference(other => $im2);
+ my $cmp1 = Imager->new(xsize => 3, ysize => 2, channels => 4);
+ $cmp1->setpixel(x => 1, 'y' => 0, color => '#FF01FF');
+ $cmp1->setpixel(x => 2, 'y' => 0, color => '#FF02FF');
+ is_image($diff1, $cmp1, "difference() - check image with mindist 0 - large samples");
+
+ my $diff2 = $im1->difference(other => $im2, mindist => 1.1);
+ $diff2->write(file=>'foo.png');
+ my $cmp2 = Imager->new(xsize => 3, ysize => 2, channels => 4);
+ $cmp2->setpixel(x => 2, 'y' => 0, color => '#FF02FF');
+ is_image($diff2, $cmp2, "difference() - check image with mindist 1.1 - large samples");
+}
+
sub test {
my ($in, $params, $out) = @_;