in reply to Re^5: Testing image output
in thread Testing image output

it is to be expected that libgd and/or the GD::Image wrapper around that library together have sufficient tests on copyResampled to ensure that it works in any situation they claim it will work

I think that's the key...
I have been getting too focussed on testing everything but I don't need to test the things that are out of the control of my module!

Following on from your 3 points I have tried this approach...

use strict; use warnings; use Test::More; use GD::Simple; use Image::Square; plan tests => 3; my $horizontal = GD::Simple->new(160, 90, 1); $horizontal->bgcolor('red'); $horizontal->rectangle(10,10,150,80); my $left = GD::Simple->new(90, 90, 1); $left->bgcolor('red'); $left->rectangle(10,10,90,80); my $center = GD::Simple->new(90, 90, 1); $center->bgcolor('red'); $center->rectangle(0,10,90,80); my $right = GD::Simple->new(90, 90, 1); $right->bgcolor('red'); $right->rectangle(0,10,80,80); my $square = Image::Square->new($horizontal->gd); my $im_left = $square->square(0, 0); cmp_ok($im_left->png, 'eq', $left->png, 'left image'); my $im_center = $square->square(0, 0.5); cmp_ok($im_center->png(5), 'eq', $center->png(5), 'center image'); my $im_right = $square->square(0, 1); cmp_ok($im_right->png(5), 'eq', $right->png(5), 'right image');
Drawing a rectangle with a smaller, red rectangle inside it. Then drawing the three parts of this simple shape and comparing them to the output from Image::Square. This works for the left image but not centre or right. Examining the image files, the output from Image::Square is one byte less. Not doubt from copyResampled.

Given your excellent point about libgd and GD::Image being tested, I don't think it is worth me testing the images other than to ensure they are the expected dimensions.

Replies are listed 'Best First'.
Re^7: Testing image output
by Anonymous Monk on Sep 16, 2023 at 11:02 UTC
    Not doubt from copyResampled.

    No. It's from you :-) -- not providing a line of code for stroke not being drawn. The copyResampled deficiencies discussed previously are not applicable if there's no scaling. It works as simple copy then, all tests are passed.

    I think the intention to test for correct image content was commendable, it's sad if you give up, though it's your module of course. With no scaling, tests must pass, if just one fails then GD is paperweight, sorry.

    Somewhat tangential, in code above you overshoot by 1 px i.e. address coordinate outside canvas, drawing rectangle for e.g. $center. Similarly, for $horizontal, rectangle clearly intended to be centered being NOT centered hurts my eyes badly :-). "5" in png(5) is a bit sore in the eye also

    Further off topic and pure speculation: I doubt that vague description of copyResampled is there to perpetually tweak/improve. It simply stems from long time ago when no solid and well understood (and correct) interpolation methods were implemented in GD. Anything better that nearest neighbour to avoid jagged lines was welcome. Then something close to bilinear scaling was quickly added. Though even then this method was not state-of-the-art, implemented elsewhere, described in papers, etc.

    Now completely off topic: checking your test failures, I was unfortunate to investigate how rectangle is stroked in GD. Looks like for lines thicker than 1 px, vertical fragments are drawn 1 px wider than horizontal fragments. It doesn't happen for stand-alone lines (left side in picture below), only for rectangles. Am I missing something? Or is it a bug? All the time being present there, for 30 or so years, then? Thing as simple as stroked frame... So much about

    expecting that libgd and/or the GD::Image wrapper around that library together have sufficient tests ... to ensure that it works in any situation they claim it will work
    use strict; use warnings; use GD; sub dump_red { my $gd = shift; my ( $w, $h ) = $gd-> getBounds; for my $y ( 0 .. $h - 1 ) { for my $x ( 0 .. $w - 1 ) { my ( $r ) = $gd-> rgb( $gd-> getPixel( $x, $y )); printf $r ? ( '%02x ', $r ) : '.. '; } print "\n"; } } my $i = GD::Image-> new( 23, 15, 1 ); my $r = $i-> colorAllocate( 255, 0, 0 ); $i-> line( 1, 2, 5, 2, $r ); $i-> line( 1, 9, 1, 13, $r ); $i-> rectangle( 7, 2, 10, 11, $r ); $i-> setThickness( 3 ); $i-> line( 1, 5, 5, 5, $r ); $i-> line( 4, 9, 4, 13, $r ); $i-> rectangle( 13, 2, 20, 11, $r ); dump_red( $i ); __END__ .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ff ff ff ff ff ff ff ff ff ff .. .. ff ff ff ff ff .. ff ff ff ff .. ff ff ff ff ff ff ff ff ff ff .. .. .. .. .. .. .. .. ff .. .. ff .. ff ff ff ff ff ff ff ff ff ff .. .. ff ff ff ff ff .. ff .. .. ff .. ff ff ff ff .. .. ff ff ff ff .. .. ff ff ff ff ff .. ff .. .. ff .. ff ff ff ff .. .. ff ff ff ff .. .. ff ff ff ff ff .. ff .. .. ff .. ff ff ff ff .. .. ff ff ff ff .. .. .. .. .. .. .. .. ff .. .. ff .. ff ff ff ff .. .. ff ff ff ff .. .. .. .. .. .. .. .. ff .. .. ff .. ff ff ff ff .. .. ff ff ff ff .. .. ff .. ff ff ff .. ff .. .. ff .. ff ff ff ff .. .. ff ff ff ff .. .. ff .. ff ff ff .. ff .. .. ff .. ff ff ff ff ff ff ff ff ff ff .. .. ff .. ff ff ff .. ff ff ff ff .. ff ff ff ff ff ff ff ff ff ff .. .. ff .. ff ff ff .. .. .. .. .. .. ff ff ff ff ff ff ff ff ff ff .. .. ff .. ff ff ff .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..