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);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.