I wrote the following program to test the copyResampled function in GD, and I have confirmed that the 'corruption' is really a bug in the GD library, most likely due to rounding errors (because of the resampling algorithm used) and I am getting an 'extra' line in both X and Y directions.
use strict;
use warnings;
use IO::File;
use GD;
my $src = GD::Image->newFromJpeg('077.jpg');
my ($srcw, $srch) = $src->getBounds(); # (550, 413)
my $destw = int ($srcw/3);
my $desth = int ($srch/3);
my $dest = GD::Image->new($destw-1, $desth-1);
$dest->copyResampled($src,0,0,0,0,$destw,$desth,$srcw,$srch);
my $f = new IO::File "078.jpg", "w"
or die "Can not create image: $!";
binmode $f;
print $f $dest->jpeg(80);
To get around this problem, I just created the target canvas 1 pixel smaller in both X and Y directions. I was actually expecting the library to core dump on the resampling because the destination width and height are 1 pixel greater than the canvas, but the GD library has clipped the target bitmap properly. So excellent that's the fix.
Another fix is to leave the target canvas unchanged, but add 1 to both destination width and height in the resample function, which also gives a good result.
my $dest = GD::Image->new($destw, $desth);
$dest->copyResampled($src,0,0,0,0,$destw+1,$desth+1,$srcw,$srch);
|