merge the thickline branch polygon fix
authorTony Cook <tony@develop=help.com>
Sun, 20 Jun 2010 10:42:03 +0000 (10:42 +0000)
committerTony Cook <tony@develop=help.com>
Sun, 20 Jun 2010 10:42:03 +0000 (10:42 +0000)
Changes
Imager.xs
imager.h
polygon.c
t/t75polyaa.t

diff --git a/Changes b/Changes
index b1a3d4cb058d0373f0d565215e265020b0c9ce69..da01f680794750a90079950ff16bf5a4367d8766 100644 (file)
--- a/Changes
+++ b/Changes
@@ -11,6 +11,9 @@ Bugs:
  - an opacity fill based on a fountain fill would segfault when
    filling an 8-bit/sample image.
 
+ - merge thickline branch polygon fix
+   https://rt.cpan.org/Ticket/Display.html?id=43518
+
 Imager 0.74 - 7 May 2010
 ===========
 
index e6dce765e7ba57ea93720b989987cc7e6acfe3ed..837d02dfcb3ccecf34faaa6d87455af755882fbf 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -1558,7 +1558,7 @@ i_bezier_multi(im,xc,yc,val)
              myfree(y);
 
 
-void
+int
 i_poly_aa(im,xc,yc,val)
     Imager::ImgRaw     im
              Imager::Color  val
@@ -1570,7 +1570,7 @@ i_poly_aa(im,xc,yc,val)
             SV       *sv1;
             SV       *sv2;
             int i;
-            PPCODE:
+            CODE:
             ICL_info(val);
             if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
             if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
@@ -1588,11 +1588,13 @@ i_poly_aa(im,xc,yc,val)
               x[i]=(double)SvNV(sv1);
               y[i]=(double)SvNV(sv2);
             }
-             i_poly_aa(im,len,x,y,val);
+             RETVAL = i_poly_aa(im,len,x,y,val);
              myfree(x);
              myfree(y);
+            OUTPUT:
+              RETVAL
 
-void
+int
 i_poly_aa_cfill(im,xc,yc,fill)
     Imager::ImgRaw     im
      Imager::FillHandle     fill
@@ -1604,7 +1606,7 @@ i_poly_aa_cfill(im,xc,yc,fill)
             SV       *sv1;
             SV       *sv2;
             int i;
-            PPCODE:
+            CODE:
             if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
             if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
             if (!SvROK(ST(2))) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
@@ -1621,9 +1623,11 @@ i_poly_aa_cfill(im,xc,yc,fill)
               x[i]=(double)SvNV(sv1);
               y[i]=(double)SvNV(sv2);
             }
-             i_poly_aa_cfill(im,len,x,y,fill);
+             RETVAL = i_poly_aa_cfill(im,len,x,y,fill);
              myfree(x);
              myfree(y);
+            OUTPUT:
+              RETVAL
 
 
 
index e8246cef293113162644bcb3d5e037208799e99e..f9986a1869229fd29e97677ebbd27b507e19e8b1 100644 (file)
--- a/imager.h
+++ b/imager.h
@@ -198,8 +198,8 @@ extern i_img *i_matrix_transform(i_img *im, int xsize, int ysize, const double *
 extern i_img *i_matrix_transform_bg(i_img *im, int xsize, int ysize, const double *matrix,  const i_color *backp, const i_fcolor *fbackp);
 
 void i_bezier_multi(i_img *im,int l,const double *x,const double *y,const i_color *val);
-void i_poly_aa     (i_img *im,int l,const double *x,const double *y,const i_color *val);
-void i_poly_aa_cfill(i_img *im,int l,const double *x,const double *y,i_fill_t *fill);
+int i_poly_aa     (i_img *im,int l,const double *x,const double *y,const i_color *val);
+int i_poly_aa_cfill(i_img *im,int l,const double *x,const double *y,i_fill_t *fill);
 
 undef_int i_flood_fill  (i_img *im,int seedx,int seedy, const i_color *dcol);
 undef_int i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill);
index ce05870975b286394e16719245bcce73fa84b1de..511cf7a30f108e0ad14cd2e2ef97f64adca30105 100644 (file)
--- a/polygon.c
+++ b/polygon.c
@@ -8,8 +8,12 @@
 #define coarse(x) ((x)/16)
 #define fine(x)   ((x)%16)
 
+/*#define DEBUG_POLY*/
+#ifdef DEBUG_POLY
+#define POLY_DEB(x) x
+#else
 #define POLY_DEB(x)
-
+#endif
 
 
 typedef int pcord;
@@ -41,18 +45,8 @@ typedef struct {
 typedef struct {
   int *line;           /* temporary buffer for scanline */
   int linelen;         /* length of scanline */
-  ss_pair *ss_list;    /* list of start stop linepairs */
-  int ssnext;          /* index of the next pair to use */
-  int sslen;           /* maximum number of start stop pairs */
 } ss_scanline;
 
-
-
-
-
-
-
-
 static
 int
 p_compy(const p_point *p1, const p_point *p2) {
@@ -126,7 +120,6 @@ point_set_new(const double *x, const double *y, int l) {
 static
 void
 ss_scanline_reset(ss_scanline *ss) {
-  ss->ssnext = 0;
   memset(ss->line, 0, sizeof(int) * ss->linelen);
 }
 
@@ -135,8 +128,6 @@ void
 ss_scanline_init(ss_scanline *ss, int linelen, int linepairs) {
   ss->line    = mymalloc( sizeof(int) * linelen );
   ss->linelen = linelen;
-  ss->ss_list = mymalloc( sizeof(ss_pair) * linepairs );
-  ss->sslen   = linepairs;
   ss_scanline_reset(ss);
 }
 
@@ -144,7 +135,6 @@ static
 void
 ss_scanline_exorcise(ss_scanline *ss) {
   myfree(ss->line);
-  myfree(ss->ss_list);
 }
   
                     
@@ -242,6 +232,7 @@ scanline_flush(i_img *im, ss_scanline *ss, int y, void *ctx) {
   int x, ch, tv;
   i_color t;
   i_color *val = (i_color *)ctx;
+  POLY_DEB( printf("Flushing line %d\n", y) );
   for(x=0; x<im->xsize; x++) {
     tv = saturate(ss->line[x]);
     i_gpix(im, x, y, &t);
@@ -274,6 +265,12 @@ pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord  miny, pcord maxy) {
   double lycross, rycross;
   int l, r;
 
+  POLY_DEB
+    (
+     printf("    pixel_coverage(..., minx %g, maxx%g, miny %g, maxy %g)\n", 
+           minx/16.0, maxx/16.0, miny/16.0, maxy/16.0)
+     );
+
   if (!line->updown) {
     l = r = 0;
   } else {
@@ -283,9 +280,9 @@ pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord  miny, pcord maxy) {
     r = rycross <= maxy && rycross >= miny; /* true if it enters through left side */
   }
   POLY_DEB(
-          printf("%4s(%+d): ", line->updown ?  line->updown == 1 ? "up" : "down" : "vert", line->updown);
-          printf("(%2d,%2d) [%3d-%3d, %3d-%3d] lycross=%.2f rycross=%.2f", coarse(minx), coarse(miny), minx, maxx, miny, maxy, lycross, rycross);
-          printf("    l=%d r=%d\n", l, r)
+          printf("    %4s(%+d): ", line->updown ?  line->updown == 1 ? "up" : "down" : "vert", line->updown);
+          printf(" (%2d,%2d) [%3d-%3d, %3d-%3d] lycross=%.2f rycross=%.2f", coarse(minx), coarse(miny), minx, maxx, miny, maxy, lycross, rycross);
+          printf(" l=%d r=%d\n", l, r)
           );
   
   if (l && r) 
@@ -327,9 +324,8 @@ pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord  miny, pcord maxy) {
 
 static
 void
-render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r) {
+render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r, pcord miny, pcord maxy) {
   
-  pcord miny, maxy;    /* y bounds in fine coordinates */
   pcord lminx, lmaxx;  /* left line min/max within y bounds in fine coords */
   pcord rminx, rmaxx;  /* right line min/max within y bounds in fine coords */
   int cpix;            /* x-coordinate of current pixel */
@@ -339,12 +335,19 @@ render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r) {
 
   /* Find the y bounds of scanline_slice */
 
-  maxy = i_min( l->maxy, r->maxy );
-  miny = i_max( l->miny, r->miny );
-
-  maxy = i_min( maxy, (y+1)*16 );
-  miny = i_max( miny,  y*16 );
-
+  POLY_DEB
+    (
+     printf("render_slice_scanline(..., y=%d)\n");
+     printf("  left  n=%d p1(%.2g, %.2g) p2(%.2g,%.2g) min(%.2g, %.2g) max(%.2g,%.2g) updown(%d)\n",
+           l->n, l->x1/16.0, l->y1/16.0, l->x2/16.0, l->y2/16.0, 
+           l->minx/16.0, l->miny/16.0, l->maxx/16.0, l->maxy/16.0,
+           l->updown);
+     printf("  right n=%d p1(%.2g, %.2g) p2(%.2g,%.2g) min(%.2g, %.2g) max(%.2g,%.2g) updown(%d)\n",
+           r->n, r->x1/16.0, r->y1/16.0, r->x2/16.0, r->y2/16.0, 
+           r->minx/16.0, r->miny/16.0, r->maxx/16.0, r->maxy/16.0,
+           r->updown);
+     );
+  
   lminx = i_min( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
   lmaxx = i_max( p_eval_aty(l, maxy), p_eval_aty(l, miny) );
 
@@ -355,6 +358,8 @@ render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r) {
 
   startpix = i_max( coarse(lminx), 0 );
   stoppix  = i_min( coarse(rmaxx-1), ss->linelen-1 );
+
+  POLY_DEB(  printf("  miny=%g maxy=%g\n", miny/16.0, maxy/16.0)  );
   
   for(cpix=startpix; cpix<=stoppix; cpix++) {
     int lt = coarse(lmaxx-1) >= cpix;
@@ -362,18 +367,18 @@ render_slice_scanline(ss_scanline *ss, int y, p_line *l, p_line *r) {
     
     int A, B, C;
     
-    POLY_DEB( printf("(%d,%d) lt=%d rt=%d\n", cpix, y, lt, rt) );
+    POLY_DEB( printf("  (%d,%d) lt=%d rt=%d\n", cpix, y, lt, rt) );
 
     A = lt ? pixel_coverage(l, cpix*16, cpix*16+16, miny, maxy) : 0;
     B = lt ? 0 : 16*(maxy-miny);
     C = rt ? pixel_coverage(r, cpix*16, cpix*16+16, miny, maxy) : 0;
 
-    POLY_DEB( printf("A=%d B=%d C=%d\n", A, B, C) );
+    POLY_DEB( printf("  A=%d B=%d C=%d\n", A, B, C) );
 
     ss->line[cpix] += A+B-C;
 
   }
-  
+  POLY_DEB( printf("end render_slice_scanline()\n") );
 }
 
 /* Antialiasing polygon algorithm 
@@ -454,42 +459,62 @@ i_poly_aa_low(i_img *im, int l, const double *x, const double *y, void *ctx, sca
   for(i=0; i<l-1; i++) {
     int startscan = i_max( coarse(pset[i].y), 0);
     int stopscan = i_min( coarse(pset[i+1].y+15), im->ysize);
+    pcord miny, maxy;  /* y bounds in fine coordinates */
 
-    if (pset[i].y == pset[i+1].y) {
-      POLY_DEB( printf("current slice thickness = 0 => skipping\n") );
-      continue;
-    }
+    POLY_DEB( pcord cc = (pset[i].y + pset[i+1].y)/2 );
 
     POLY_DEB(
             printf("current slice is %d: %d to %d ( cpoint %d ) scanlines %d to %d\n", 
                    i, pset[i].y, pset[i+1].y, cc, startscan, stopscan)
             );
     
-    
+    if (pset[i].y == pset[i+1].y) {
+      POLY_DEB( printf("current slice thickness = 0 => skipping\n") );
+      continue;
+    }
+
     clc = lines_in_interval(lset, l, tllist, pset[i].y, pset[i+1].y);
     qsort(tllist, clc, sizeof(p_slice), (int(*)(const void *,const void *))p_compx);
 
     mark_updown_slices(lset, tllist, clc);
 
-    POLY_DEB( printf("Interval contains %d lines\n", clc) );
-
-    for(k=0; k<clc; k++) {
-      POLY_DEB(
-              printf("%d:  line #%2d: (%2d, %2d)->(%2d, %2d) (%2d/%2d, %2d/%2d) -> (%2d/%2d, %2d/%2d) alignment=%s\n",
-                     k, lno, ln->x1, ln->y1, ln->x2, ln->y2, 
-                     coarse(ln->x1), fine(ln->x1), 
-                     coarse(ln->y1), fine(ln->y1), 
-                     coarse(ln->x2), fine(ln->x2), 
-                     coarse(ln->y2), fine(ln->y2),
-                     ln->updown == 0 ? "vert" : ln->updown == 1 ? "up" : "down")
-              );
+    POLY_DEB
+      (
+       printf("Interval contains %d lines\n", clc);
+       for(k=0; k<clc; k++) {
+        int lno = tllist[k].n;
+        p_line *ln = lset+lno;
+        printf("%d:  line #%2d: (%2d, %2d)->(%2d, %2d) (%2d/%2d, %2d/%2d) -> (%2d/%2d, %2d/%2d) alignment=%s\n",
+               k, lno, ln->x1, ln->y1, ln->x2, ln->y2, 
+               coarse(ln->x1), fine(ln->x1), 
+               coarse(ln->y1), fine(ln->y1), 
+               coarse(ln->x2), fine(ln->x2), 
+               coarse(ln->y2), fine(ln->y2),
+               ln->updown == 0 ? "vert" : ln->updown == 1 ? "up" : "down");
+          
+       }
+       );
+    maxy = im->ysize * 16;
+    miny = 0;
+    for (k = 0; k < clc; ++k) {
+      p_line const * line = lset + tllist[k].n;
+      if (line->miny > miny)
+       miny = line->miny;
+      if (line->maxy < maxy)
+       maxy = line->maxy;
+      POLY_DEB( printf(" line miny %g maxy %g\n", line->miny/16.0, line->maxy/16.0) );
     }
+    POLY_DEB( printf("miny %g maxy %g\n", miny/16.0, maxy/16.0) );
+
     for(cscl=startscan; cscl<stopscan; cscl++) {
+      pcord scan_miny = i_max(miny, cscl * 16);
+      pcord scan_maxy = i_min(maxy, (cscl + 1 ) * 16);
+      
       tempy = i_min(cscl*16+16, pset[i+1].y);
       POLY_DEB( printf("evaluating scan line %d \n", cscl) );
       for(k=0; k<clc-1; k+=2) {
        POLY_DEB( printf("evaluating slice %d\n", k) );
-       render_slice_scanline(&templine, cscl, lset+tllist[k].n, lset+tllist[k+1].n);
+       render_slice_scanline(&templine, cscl, lset+tllist[k].n, lset+tllist[k+1].n, scan_miny, scan_maxy);
       }
       if (16*coarse(tempy) == tempy) {
        POLY_DEB( printf("flushing scan line %d\n", cscl) );
@@ -515,10 +540,11 @@ i_poly_aa_low(i_img *im, int l, const double *x, const double *y, void *ctx, sca
   
 } /* Function */
 
-void
+int
 i_poly_aa(i_img *im, int l, const double *x, const double *y, const i_color *val) {
   i_color c = *val;
   i_poly_aa_low(im, l, x, y, &c, scanline_flush);
+  return 1;
 }
 
 struct poly_render_state {
@@ -552,7 +578,7 @@ scanline_flush_render(i_img *im, ss_scanline *ss, int y, void *ctx) {
   }
 }
 
-void
+int
 i_poly_aa_cfill(i_img *im, int l, const double *x, const double *y, 
                i_fill_t *fill) {
   struct poly_render_state ctx;
@@ -563,4 +589,5 @@ i_poly_aa_cfill(i_img *im, int l, const double *x, const double *y,
   i_poly_aa_low(im, l, x, y, &ctx, scanline_flush_render);
   myfree(ctx.cover);
   i_render_done(&ctx.render);
+  return 1;
 }
index f8af081114b341e00d0b83a1e618573e98af0f44..73068e2442ff9199c5872063151f7aecd799521b 100644 (file)
-# Before `make install' is performed this script should be runnable with
-# `make test'. After `make install' it should work as `perl test.pl'
+#!perl -w
 
-######################### We start with some black magic to print on failure.
+use strict;
+use Test::More tests => 18;
 
-# Change 1..1 below to 1..last_test_to_print .
-# (It may become useful if the test is moved to ./t subdirectory.)
-
-BEGIN { $| = 1; print "1..9\n"; }
-END {print "not ok 1\n" unless $loaded;}
-use Imager qw(:all);
+use Imager qw/NC/;
+use Imager::Test qw(is_image is_color3);
 
 sub PI () { 3.14159265358979323846 }
 
-$loaded = 1;
-print "ok 1\n";
-
-init_log("testout/t75aapolyaa.log",1);
-
-$red   = Imager::Color->new(255,0,0);
-$green = Imager::Color->new(0,255,0);
-$blue  = Imager::Color->new(0,0,255);
-$white = Imager::Color->new(255,255,255);
-
-
-$img = Imager->new(xsize=>20, ysize=>10);
-@data = translate(5.5,5,
-                 rotate(0,
-                        scale(5, 5,
-                              get_polygon(n_gon => 5)
-                             )
-                       )
-                );
-
-
-my ($x, $y) = array_to_refpair(@data);
-i_poly_aa($img->{IMG}, $x, $y, $white);
-
-
-
-
-print "ok 2\n";
-
-$img->write(file=>"testout/t75.ppm") or die $img->errstr;
-print "ok 3\n";
-
-
-$zoom = make_zoom($img, 8, \@data, $red);
-$zoom->write(file=>"testout/t75zoom.ppm") or die $zoom->errstr;
-
-print "ok 4\n";
-
-$img = Imager->new(xsize=>300, ysize=>100);
+Imager::init_log("testout/t75aapolyaa.log",1);
+
+my $red   = Imager::Color->new(255,0,0);
+my $green = Imager::Color->new(0,255,0);
+my $blue  = Imager::Color->new(0,0,255);
+my $white = Imager::Color->new(255,255,255);
+
+{ # artifacts with multiple vertical lobes
+  # https://rt.cpan.org/Ticket/Display.html?id=43518
+  # previously this would have a full coverage pixel at (0,0) caused
+  # by the (20,0.5) point in the right lobe
+
+  my @pts = 
+    (
+     [ 0.5, -9 ],
+     [ 10, -9 ],
+     [ 10, 11 ],
+     [ 15, 11 ],
+     [ 15, -9 ],
+     [ 17, -9 ],
+     [ 20, 0.5 ],
+     [ 17, 11 ],
+     [ 0.5, 11 ],
+    );
+  my $im = Imager->new(xsize => 10, ysize => 2);
+  ok($im->polygon(points => \@pts,
+                 color => $white),
+     "draw with inside point");
+  ok($im->write(file => "testout/t75inside.ppm"), "save to file");
+  # both scanlines should be the same
+  my $line0 = $im->crop(top => 0, height => 1);
+  my $line1 = $im->crop(top => 1, height => 1);
+  is_image($line0, $line1, "both scanlines should be the same");
+}
 
-for $n (0..55) {
-  @data = translate(20+20*($n%14),18+20*int($n/14),
-                   rotate(15*$n/PI,
-                          scale(15, 15,
-                                get_polygon('box')
-                               )
-                         )
-                  );
-  my ($x, $y) = array_to_refpair(@data);
-  i_poly_aa($img->{IMG}, $x, $y, NC(rand(255), rand(255), rand(255)));
+{ # check vertical edges are consistent
+  my $im = Imager->new(xsize => 10, ysize => 10);
+  ok($im->polygon(points => [ [ 0.5, 0 ], [ 9.25, 0 ], 
+                             [ 9.25, 10 ], [ 0.5, 10 ] ],
+                 color => $white,
+                 aa => 1), 
+     "draw polygon with mid pixel vertical edges")
+    or diag $im->errstr;
+  my @line0 = $im->getscanline(y => 0);
+  my $im2 = Imager->new(xsize => 10, ysize => 10);
+  for my $y (0..9) {
+    $im2->setscanline(y => $y, pixels => \@line0);
+  }
+  is_image($im, $im2, "all scan lines should be the same");
+  is_color3($line0[0], 128, 128, 128, "(0,0) should be 50% coverage");
+  is_color3($line0[9], 64, 64, 64, "(9,0) should be 25% coverage");
 }
 
-$img->write(file=>"testout/t75big.ppm") or die $img->errstr;
+{ # check horizontal edges are consistent
+  my $im = Imager->new(xsize => 10, ysize => 10);
+  ok($im->polygon(points => [ [ 0, 0.5 ], [ 0, 9.25 ],
+                             [ 10, 9.25 ], [ 10, 0.5 ] ],
+                 color => $white,
+                 aa => 1),
+     "draw polygon with mid-pixel horizontal edges");
+  is_deeply([ $im->getsamples(y => 0, channels => [ 0 ]) ],
+           [ (128) x 10 ],
+           "all of line 0 should be 50% coverage");
+  is_deeply([ $im->getsamples(y => 9, channels => [ 0 ]) ],
+           [ (64) x 10 ],
+           "all of line 9 should be 25% coverage");
+}
 
-print "ok 5\n";
+{
+  my $img = Imager->new(xsize=>20, ysize=>10);
+  my @data = translate(5.5,5,
+                      rotate(0,
+                             scale(5, 5,
+                                   get_polygon(n_gon => 5)
+                                  )
+                            )
+                     );
+  
+  
+  my ($x, $y) = array_to_refpair(@data);
+  ok(Imager::i_poly_aa($img->{IMG}, $x, $y, $white), "primitive poly");
 
-$img = Imager->new(xsize => 300, ysize => 300);
-$img -> polygon(color=>$white,
-               points => [
-                          translate(150,150,
-                                    rotate(45*PI/180,
-                                           scale(70,70,
-                                                 get_polygon('wavycircle', 32*8, sub { 1.2+1*cos(4*$_) }))))
-                         ],
-              ) or die $img->errstr();
+  ok($img->write(file=>"testout/t75.ppm"), "write to file")
+    or diag $img->errstr;
 
-$img->write(file=>"testout/t75wave.ppm") or die $img->errstr;
+  my $zoom = make_zoom($img, 8, \@data, $red);
+  ok($zoom, "make zoom of primitive");
+  $zoom->write(file=>"testout/t75zoom.ppm") or die $zoom->errstr;
+}
 
-print "ok 6\n";
+{
+  my $img = Imager->new(xsize=>300, ysize=>100);
+
+  my $good = 1;
+  for my $n (0..55) {
+    my @data = translate(20+20*($n%14),18+20*int($n/14),
+                        rotate(15*$n/PI,
+                               scale(15, 15,
+                                     get_polygon('box')
+                                    )
+                              )
+                       );
+    my ($x, $y) = array_to_refpair(@data);
+    Imager::i_poly_aa($img->{IMG}, $x, $y, NC(rand(255), rand(255), rand(255)))
+       or $good = 0;
+  }
+  
+  $img->write(file=>"testout/t75big.ppm") or die $img->errstr;
 
+  ok($good, "primitive squares");
+}
 
-$img = Imager->new(xsize=>10,ysize=>6);
-@data = translate(165,5,
-                 scale(80,80,
-                       get_polygon('wavycircle', 32*8, sub { 1+1*cos(4*$_) })));
+{
+  my $img = Imager->new(xsize => 300, ysize => 300);
+  ok($img -> polygon(color=>$white,
+                 points => [
+                            translate(150,150,
+                                      rotate(45*PI/180,
+                                             scale(70,70,
+                                                   get_polygon('wavycircle', 32*8, sub { 1.2+1*cos(4*$_) }))))
+                           ],
+                ), "method call")
+    or diag $img->errstr();
+
+  $img->write(file=>"testout/t75wave.ppm") or die $img->errstr;
+}
 
-print "XXX\n";
-$img -> polygon(color=>$white,
+{
+  my $img = Imager->new(xsize=>10,ysize=>6);
+  my @data = translate(165,5,
+                      scale(80,80,
+                            get_polygon('wavycircle', 32*8, sub { 1+1*cos(4*$_) })));
+  
+  ok($img -> polygon(color=>$white,
                points => [
                           translate(165,5,
                                     scale(80,80,
                                           get_polygon('wavycircle', 32*8, sub { 1+1*cos(4*$_) })))
                          ],
-              ) or die $img->errstr();
+                ), "bug check")
+    or diag $img->errstr();
 
-make_zoom($img,20,\@data, $blue)->write(file=>"testout/t75wavebug.ppm") or die $img->errstr;
+  make_zoom($img,20,\@data, $blue)->write(file=>"testout/t75wavebug.ppm") or die $img->errstr;
 
+}
 
-print "ok 7\n";
-
-$img = Imager->new(xsize=>300, ysize=>300);
-$img->polygon(fill=>{ hatch=>'cross1', fg=>'00FF00', bg=>'0000FF', dx=>3 },
+{
+  my $img = Imager->new(xsize=>300, ysize=>300);
+  ok($img->polygon(fill=>{ hatch=>'cross1', fg=>'00FF00', bg=>'0000FF', dx=>3 },
               points => [
                          translate(150,150,
                                    scale(70,70,
                                          get_polygon('wavycircle', 32*8, sub { 1+1*cos(4*$_) })))
                         ],
-             ) or die $img->errstr();
-$img->write(file=>"testout/t75wave_fill.ppm") or die $img->errstr;
-
-print "ok 8\n";
+             ), "poly filled with hatch")
+    or diag $img->errstr();
+  $img->write(file=>"testout/t75wave_fill.ppm") or die $img->errstr;
+}
 
-$img = Imager->new(xsize=>300, ysize=>300, bits=>16);
-$img->polygon(fill=>{ hatch=>'cross1', fg=>'00FF00', bg=>'0000FF' },
+{
+  my $img = Imager->new(xsize=>300, ysize=>300, bits=>16);
+  ok($img->polygon(fill=>{ hatch=>'cross1', fg=>'00FF00', bg=>'0000FF' },
               points => [
                          translate(150,150,
                                    scale(70,70,
                                          get_polygon('wavycircle', 32*8, sub { 1+1*cos(5*$_) })))
                         ],
-             ) or die $img->errstr();
-$img->write(file=>"testout/t75wave_fill16.ppm") or die $img->errstr;
-
-print "ok 9\n";
+             ), "hatched to 16-bit image")
+    or diag $img->errstr();
+  $img->write(file=>"testout/t75wave_fill16.ppm") or die $img->errstr;
+}
 
-malloc_state();
+Imager::malloc_state();
 
 
+#initialized in a BEGIN, later
+my %primitives;
+my %polygens;
 
 sub get_polygon {
   my $name = shift;
@@ -151,11 +208,11 @@ sub make_zoom {
   my $timg = $img->scale(scalefactor=>$sc, qtype=>'preview');
 
   # draw the grid
-  for($lx=0; $lx<$timg->getwidth(); $lx+=$sc) {
+  for(my $lx=0; $lx<$timg->getwidth(); $lx+=$sc) {
     $timg->line(color=>$green, x1=>$lx, x2=>$lx, y1=>0, y2=>$timg->getheight(), antialias=>0);
   }
 
-  for($ly=0; $ly<$timg->getheight(); $ly+=$sc) {
+  for(my $ly=0; $ly<$timg->getheight(); $ly+=$sc) {
     $timg->line(color=>$green, y1=>$ly, y2=>$ly, x1=>0, x2=>$timg->getwidth(), antialias=>0);
   }
   my @data = scale($sc, $sc, @$polydata);