]> git.imager.perl.org - imager.git/blobdiff - fills.c
correct an old bug link
[imager.git] / fills.c
diff --git a/fills.c b/fills.c
index b92eaaf37774c9c87eb73955d7c860beebc3fa71..b2305802b545dee2522236267bd22bd103942429 100644 (file)
--- a/fills.c
+++ b/fills.c
@@ -1,5 +1,7 @@
+#define IMAGER_NO_CONTEXT
 #include "imager.h"
 #include "imageri.h"
+#include <assert.h>
 
 /*
 =head1 NAME
@@ -398,7 +400,7 @@ typedef struct
   i_color fg, bg;
   i_fcolor ffg, fbg;
   unsigned char hatch[8];
-  int dx, dy;
+  i_img_dim dx, dy;
 } i_fill_hatch_t;
 
 static void fill_hatch(i_fill_t *fill, i_img_dim x, i_img_dim y,
@@ -409,7 +411,7 @@ static
 i_fill_t *
 i_new_hatch_low(const i_color *fg, const i_color *bg, const i_fcolor *ffg, const i_fcolor *fbg, 
                 int combine, int hatch, const unsigned char *cust_hatch,
-                int dx, int dy);
+                i_img_dim dx, i_img_dim dy);
 
 /*
 =item i_new_fill_hatch(C<fg>, C<bg>, C<combine>, C<hatch>, C<cust_hatch>, C<dx>, C<dy>)
@@ -428,13 +430,15 @@ If C<cust_hatch> is NULL then one of the standard hatches is used.
 
 (C<dx>, C<dy>) are an offset into the hatch which can be used to hatch
 adjoining areas out of alignment, or to align the origin of a hatch
-with the the side of a filled area.
+with the side of a filled area.
 
 =cut
 */
 i_fill_t *
 i_new_fill_hatch(const i_color *fg, const i_color *bg, int combine, int hatch, 
-            const unsigned char *cust_hatch, int dx, int dy) {
+            const unsigned char *cust_hatch, i_img_dim dx, i_img_dim dy) {
+  assert(fg);
+  assert(bg);
   return i_new_hatch_low(fg, bg, NULL, NULL, combine, hatch, cust_hatch, 
                          dx, dy);
 }
@@ -456,13 +460,15 @@ If C<cust_hatch> is NULL then one of the standard hatches is used.
 
 (C<dx>, C<dy>) are an offset into the hatch which can be used to hatch
 adjoining areas out of alignment, or to align the origin of a hatch
-with the the side of a filled area.
+with the side of a filled area.
 
 =cut
 */
 i_fill_t *
 i_new_fill_hatchf(const i_fcolor *fg, const i_fcolor *bg, int combine, int hatch, 
-                 const unsigned char *cust_hatch, int dx, int dy) {
+                 const unsigned char *cust_hatch, i_img_dim dx, i_img_dim dy) {
+  assert(fg);
+  assert(bg);
   return i_new_hatch_low(NULL, NULL, fg, bg, combine, hatch, cust_hatch, 
                          dx, dy);
 }
@@ -474,7 +480,7 @@ static void fill_imagef(i_fill_t *fill, i_img_dim x, i_img_dim y,
 struct i_fill_image_t {
   i_fill_t base;
   i_img *src;
-  int xoff, yoff;
+  i_img_dim xoff, yoff;
   int has_matrix;
   double matrix[9];
 };
@@ -504,7 +510,7 @@ C<xoff> and C<yoff> are the offset into the image to start filling from.
 =cut
 */
 i_fill_t *
-i_new_fill_image(i_img *im, const double *matrix, int xoff, int yoff, int combine) {
+i_new_fill_image(i_img *im, const double *matrix, i_img_dim xoff, i_img_dim yoff, int combine) {
   struct i_fill_image_t *fill = mymalloc(sizeof(*fill)); /* checked 14jul05 tonyc */
 
   *fill = image_fill_proto;
@@ -547,9 +553,11 @@ struct i_fill_opacity_t {
 static struct i_fill_opacity_t
 opacity_fill_proto =
   {
-    fill_opacity,
-    fill_opacityf,
-    NULL
+    {
+      fill_opacity,
+      fill_opacityf,
+      NULL
+    }
   };
 
 i_fill_t *
@@ -635,29 +643,27 @@ i_fill_t *
 i_new_hatch_low(const i_color *fg, const i_color *bg, 
                const i_fcolor *ffg, const i_fcolor *fbg, 
                 int combine, int hatch, const unsigned char *cust_hatch,
-                int dx, int dy) {
+                i_img_dim dx, i_img_dim dy) {
   i_fill_hatch_t *fill = mymalloc(sizeof(i_fill_hatch_t)); /* checked 14jul05 tonyc */
 
   *fill = hatch_fill_proto;
-  /* Some Sun C didn't like the condition expressions that were here.
-     See https://rt.cpan.org/Ticket/Display.html?id=21944
-   */
-  if (fg)
+  if (fg && bg) {
     fill->fg = *fg;
-  else
-    fill->fg = fcolor_to_color(ffg);
-  if (bg)
     fill->bg = *bg;
-  else
+    fill->ffg = color_to_fcolor(fg);
+    fill->fbg = color_to_fcolor(bg);
+  }
+  else if (ffg && fbg) {
+    fill->fg = fcolor_to_color(ffg);
     fill->bg = fcolor_to_color(fbg);
-  if (ffg) 
     fill->ffg = *ffg;
-  else
-    fill->ffg = color_to_fcolor(fg);
-  if (fbg)
     fill->fbg = *fbg;
-  else
-    fill->fbg = color_to_fcolor(bg);
+  }
+  else {
+    assert(0);
+    /* NOTREACHED */
+  }
+  
   if (combine) {
     i_get_combine(combine, &fill->base.combine, &fill->base.combinef);
   }
@@ -669,8 +675,10 @@ i_new_hatch_low(const i_color *fg, const i_color *bg,
     memcpy(fill->hatch, cust_hatch, 8);
   }
   else {
-    if (hatch > sizeof(builtin_hatches)/sizeof(*builtin_hatches)) 
+    if (hatch >= sizeof(builtin_hatches)/sizeof(*builtin_hatches)
+       || hatch < 0) {
       hatch = 0;
+    }
     memcpy(fill->hatch, builtin_hatches[hatch], 8);
   }
   fill->dx = dx & 7;
@@ -695,7 +703,6 @@ fill_hatch(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
   int mask = 128 >> xpos;
   i_color fg = f->fg;
   i_color bg = f->bg;
-  int want_channels = channels > 2 ? 4 : 2;
 
   if (channels < 3) {
     i_adapt_colors(2, 4, &fg, 1);
@@ -718,7 +725,7 @@ fill_hatch(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
 
 The floating sample fill function for hatched fills.
 
-=back
+=cut
 */
 static void
 fill_hatchf(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
@@ -756,14 +763,16 @@ static i_color interp_i_color(i_color before, i_color after, double pos,
   pos -= floor(pos);
   for (ch = 0; ch < channels; ++ch)
     out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
-  if (channels > 3 && out.channel[3])
-    for (ch = 0; ch < channels; ++ch)
+  if (channels > 3 && out.channel[3]) {
+    for (ch = 0; ch < channels; ++ch) {
       if (ch != 3) {
         int temp = out.channel[ch] * 255 / out.channel[3];
         if (temp > 255)
           temp = 255;
         out.channel[ch] = temp;
       }
+    }
+  }
 
   return out;
 }
@@ -778,14 +787,16 @@ static i_fcolor interp_i_fcolor(i_fcolor before, i_fcolor after, double pos,
   pos -= floor(pos);
   for (ch = 0; ch < channels; ++ch)
     out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
-  if (out.channel[3])
-    for (ch = 0; ch < channels; ++ch)
+  if (out.channel[3]) {
+    for (ch = 0; ch < channels; ++ch) {
       if (ch != 3) {
         int temp = out.channel[ch] / out.channel[3];
         if (temp > 1.0)
           temp = 1.0;
         out.channel[ch] = temp;
       }
+    }
+  }
 
   return out;
 }
@@ -826,12 +837,12 @@ fill_image(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
       ry -= iy * f->src->ysize;
 
       for (dy = 0; dy < 2; ++dy) {
-        if ((int)rx == f->src->xsize-1) {
-          i_gpix(f->src, f->src->xsize-1, ((int)ry+dy) % f->src->ysize, &c[dy][0]);
-          i_gpix(f->src, 0, ((int)ry+dy) % f->src->xsize, &c[dy][1]);
+        if ((i_img_dim)rx == f->src->xsize-1) {
+          i_gpix(f->src, f->src->xsize-1, ((i_img_dim)ry+dy) % f->src->ysize, &c[dy][0]);
+          i_gpix(f->src, 0, ((i_img_dim)ry+dy) % f->src->xsize, &c[dy][1]);
         }
         else {
-          i_glin(f->src, (int)rx, (int)rx+2, ((int)ry+dy) % f->src->ysize, 
+          i_glin(f->src, (i_img_dim)rx, (i_img_dim)rx+2, ((i_img_dim)ry+dy) % f->src->ysize, 
                  c[dy]);
         }
         c2[dy] = interp_i_color(c[dy][0], c[dy][1], rx, f->src->channels);
@@ -855,7 +866,7 @@ fill_image(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
       }
       else if (f->yoff) {
         ry += ix * f->yoff;
-        iy = ry / f->src->xsize;
+        iy = ry / f->src->ysize;
       }
       rx -= ix * f->src->xsize;
       ry -= iy * f->src->ysize;
@@ -904,12 +915,12 @@ fill_imagef(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
       ry -= iy * f->src->ysize;
 
       for (dy = 0; dy < 2; ++dy) {
-        if ((int)rx == f->src->xsize-1) {
-          i_gpixf(f->src, f->src->xsize-1, ((int)ry+dy) % f->src->ysize, &c[dy][0]);
-          i_gpixf(f->src, 0, ((int)ry+dy) % f->src->xsize, &c[dy][1]);
+        if ((i_img_dim)rx == f->src->xsize-1) {
+          i_gpixf(f->src, f->src->xsize-1, ((i_img_dim)ry+dy) % f->src->ysize, &c[dy][0]);
+          i_gpixf(f->src, 0, ((i_img_dim)ry+dy) % f->src->xsize, &c[dy][1]);
         }
         else {
-          i_glinf(f->src, (int)rx, (int)rx+2, ((int)ry+dy) % f->src->ysize, 
+          i_glinf(f->src, (i_img_dim)rx, (i_img_dim)rx+2, ((i_img_dim)ry+dy) % f->src->ysize, 
                  c[dy]);
         }
         c2[dy] = interp_i_fcolor(c[dy][0], c[dy][1], rx, f->src->channels);