]> git.imager.perl.org - imager.git/blobdiff - fills.c
- clean up Imager::Font documentation:
[imager.git] / fills.c
diff --git a/fills.c b/fills.c
index 181fca6e48ca78ce94f6309af63d3e615601cf08..e075c5381fb8c52197b97a92122fc7b8b9024b74 100644 (file)
--- a/fills.c
+++ b/fills.c
@@ -163,13 +163,13 @@ typedef struct
 } i_fill_solid_t;
 
 static void fill_solid(i_fill_t *, int x, int y, int width, int channels, 
-                       i_color *, i_color *);
+                       i_color *);
 static void fill_solidf(i_fill_t *, int x, int y, int width, int channels, 
-                        i_fcolor *, i_fcolor *);
+                        i_fcolor *);
 static void fill_solid_comb(i_fill_t *, int x, int y, int width, int channels, 
-                            i_color *, i_color *);
+                            i_color *);
 static void fill_solidf_comb(i_fill_t *, int x, int y, int width, 
-                             int channels, i_fcolor *, i_fcolor *);
+                             int channels, i_fcolor *);
 
 static i_fill_solid_t base_solid_fill =
 {
@@ -220,7 +220,7 @@ If combine is non-zero then alpha values will be combined.
 i_fill_t *
 i_new_fill_solidf(i_fcolor *c, int combine) {
   int ch;
-  i_fill_solid_t *fill = mymalloc(sizeof(i_fill_solid_t));
+  i_fill_solid_t *fill = mymalloc(sizeof(i_fill_solid_t)); /* checked 14jul05 tonyc */
   
   if (combine) {
     *fill = base_solid_fill_comb;
@@ -249,7 +249,7 @@ If combine is non-zero then alpha values will be combined.
 i_fill_t *
 i_new_fill_solid(i_color *c, int combine) {
   int ch;
-  i_fill_solid_t *fill = mymalloc(sizeof(i_fill_solid_t));
+  i_fill_solid_t *fill = mymalloc(sizeof(i_fill_solid_t)); /* checked 14jul05 tonyc */
 
   if (combine) {
     *fill = base_solid_fill_comb;
@@ -408,9 +408,9 @@ typedef struct
 } i_fill_hatch_t;
 
 static void fill_hatch(i_fill_t *fill, int x, int y, int width, int channels, 
-                       i_color *data, i_color *work);
+                       i_color *data);
 static void fill_hatchf(i_fill_t *fill, int x, int y, int width, int channels, 
-                        i_fcolor *data, i_fcolor *work);
+                        i_fcolor *data);
 static
 i_fill_t *
 i_new_hatch_low(i_color *fg, i_color *bg, i_fcolor *ffg, i_fcolor *fbg, 
@@ -464,9 +464,9 @@ i_new_fill_hatchf(i_fcolor *fg, i_fcolor *bg, int combine, int hatch,
 }
 
 static void fill_image(i_fill_t *fill, int x, int y, int width, int channels,
-                       i_color *data, i_color *work);
+                       i_color *data);
 static void fill_imagef(i_fill_t *fill, int x, int y, int width, int channels,
-                       i_fcolor *data, i_fcolor *work);
+                       i_fcolor *data);
 struct i_fill_image_t {
   i_fill_t base;
   i_img *src;
@@ -484,7 +484,7 @@ Create an image based fill.
 */
 i_fill_t *
 i_new_fill_image(i_img *im, double *matrix, int xoff, int yoff, int combine) {
-  struct i_fill_image_t *fill = mymalloc(sizeof(*fill));
+  struct i_fill_image_t *fill = mymalloc(sizeof(*fill)); /* checked 14jul05 tonyc */
 
   fill->base.fill_with_color = fill_image;
   fill->base.fill_with_fcolor = fill_imagef;
@@ -532,7 +532,7 @@ The 8-bit sample fill function for non-combining solid fills.
 */
 static void
 fill_solid(i_fill_t *fill, int x, int y, int width, int channels, 
-           i_color *data, i_color *work) {
+           i_color *data) {
   while (width-- > 0) {
     *data++ = T_SOLID_FILL(fill)->c;
   }
@@ -547,7 +547,7 @@ The floating sample fill function for non-combining solid fills.
 */
 static void
 fill_solidf(i_fill_t *fill, int x, int y, int width, int channels, 
-           i_fcolor *data, i_fcolor *work) {
+           i_fcolor *data) {
   while (width-- > 0) {
     *data++ = T_SOLID_FILL(fill)->fc;
   }
@@ -562,15 +562,12 @@ The 8-bit sample fill function for combining solid fills.
 */
 static void
 fill_solid_comb(i_fill_t *fill, int x, int y, int width, int channels, 
-                i_color *data, i_color *work) {
+                i_color *data) {
   i_color c = T_SOLID_FILL(fill)->c;
-  int count = width;
-  i_color *wstart = work;
 
   while (width-- > 0) {
-    *work++ = c;
+    *data++ = c;
   }
-  (fill->combine)(data, wstart, channels, count);
 }
 
 /*
@@ -582,15 +579,12 @@ The floating sample fill function for combining solid fills.
 */
 static void
 fill_solidf_comb(i_fill_t *fill, int x, int y, int width, int channels, 
-           i_fcolor *data, i_fcolor *work) {
+           i_fcolor *data) {
   i_fcolor c = T_SOLID_FILL(fill)->fc;
-  int count = width;
-  i_fcolor *wstart = work;
 
   while (width-- > 0) {
-    *work++ = c;
+    *data++ = c;
   }
-  (fill->combinef)(data, wstart, channels, count);
 }
 
 /*
@@ -605,7 +599,7 @@ i_fill_t *
 i_new_hatch_low(i_color *fg, i_color *bg, i_fcolor *ffg, i_fcolor *fbg, 
                 int combine, int hatch, unsigned char *cust_hatch,
                 int dx, int dy) {
-  i_fill_hatch_t *fill = mymalloc(sizeof(i_fill_hatch_t));
+  i_fill_hatch_t *fill = mymalloc(sizeof(i_fill_hatch_t)); /* checked 14jul05 tonyc */
 
   fill->base.fill_with_color = fill_hatch;
   fill->base.fill_with_fcolor = fill_hatchf;
@@ -643,31 +637,17 @@ The 8-bit sample fill function for hatched fills.
 =cut
 */
 static void fill_hatch(i_fill_t *fill, int x, int y, int width, int channels, 
-                       i_color *data, i_color *work) {
+                       i_color *data) {
   i_fill_hatch_t *f = (i_fill_hatch_t *)fill;
   int byte = f->hatch[(y + f->dy) & 7];
   int xpos = (x + f->dx) & 7;
   int mask = 128 >> xpos;
 
-  if (fill->combine) {
-    int count = width;
-    i_color *wstart = work;
-
-    while (count-- > 0) {
-      *work++ = (byte & mask) ? f->fg : f->bg;
-      
-      if ((mask >>= 1) == 0)
-        mask = 128;
-    }
-    (fill->combine)(data, wstart, channels, width);
-  }
-  else {
-    while (width-- > 0) {
-      *data++ = (byte & mask) ? f->fg : f->bg;
-
-      if ((mask >>= 1) == 0)
-        mask = 128;
-    }
+  while (width-- > 0) {
+    *data++ = (byte & mask) ? f->fg : f->bg;
+    
+    if ((mask >>= 1) == 0)
+      mask = 128;
   }
 }
 
@@ -679,31 +659,17 @@ The floating sample fill function for hatched fills.
 =back
 */
 static void fill_hatchf(i_fill_t *fill, int x, int y, int width, int channels, 
-                        i_fcolor *data, i_fcolor *work) {
+                        i_fcolor *data) {
   i_fill_hatch_t *f = (i_fill_hatch_t *)fill;
   int byte = f->hatch[(y + f->dy) & 7];
   int xpos = (x + f->dx) & 7;
   int mask = 128 >> xpos;
   
-  if (fill->combinef) {
-    int count = width;
-    i_fcolor *wstart = work;
-
-    while (count-- > 0) {
-      *work++ = (byte & mask) ? f->ffg : f->fbg;
-      
-      if ((mask >>= 1) == 0)
-        mask = 128;
-    }
-    (fill->combinef)(data, wstart, channels, width);
-  }
-  else {
-    while (width-- > 0) {
-      *data++ = (byte & mask) ? f->ffg : f->fbg;
-
-      if ((mask >>= 1) == 0)
-        mask = 128;
-    }
+  while (width-- > 0) {
+    *data++ = (byte & mask) ? f->ffg : f->fbg;
+    
+    if ((mask >>= 1) == 0)
+      mask = 128;
   }
 }
 
@@ -757,11 +723,10 @@ static i_fcolor interp_i_fcolor(i_fcolor before, i_fcolor after, double pos,
 =cut
 */
 static void fill_image(i_fill_t *fill, int x, int y, int width, int channels,
-                       i_color *data, i_color *work) {
+                       i_color *data) {
   struct i_fill_image_t *f = (struct i_fill_image_t *)fill;
-  i_color *out = fill->combine ? work : data;
   int i = 0;
-  i_color c;
+  i_color *out = data;
   
   if (f->has_matrix) {
     /* the hard way */
@@ -824,9 +789,28 @@ static void fill_image(i_fill_t *fill, int x, int y, int width, int channels,
       ++i;
     }
   }
-
-  if (fill->combine) {
-    (fill->combine)(data, work, channels, width);
+  if (f->src->channels == 3) {
+    /* just set the alpha */
+    for (i = 0; i <  width; ++i) {
+      data->channel[3] = 255;
+      data++;
+    }
+  }
+  else if (f->src->channels == 2) {
+    /* copy the alpha to channel 3, duplicate the grey value */
+    for (i = 0; i <  width; ++i) {
+      data->channel[3] = data->channel[1];
+      data->channel[1] = data->channel[2] = data->channel[0];
+      data++;
+    }
+  }
+  else if (f->src->channels == 1) {
+    /* set the alpha, duplicate grey */
+    for (i = 0; i <  width; ++i) {
+      data->channel[3] = 255;
+      data->channel[1] = data->channel[2] = data->channel[0];
+      data++;
+    }
   }
 }
 
@@ -836,11 +820,9 @@ static void fill_image(i_fill_t *fill, int x, int y, int width, int channels,
 =cut
 */
 static void fill_imagef(i_fill_t *fill, int x, int y, int width, int channels,
-                       i_fcolor *data, i_fcolor *work) {
+                       i_fcolor *data) {
   struct i_fill_image_t *f = (struct i_fill_image_t *)fill;
-  i_fcolor *out = fill->combine ? work : data;
   int i = 0;
-  i_fcolor c;
   
   if (f->has_matrix) {
     /* the hard way */
@@ -875,7 +857,7 @@ static void fill_imagef(i_fill_t *fill, int x, int y, int width, int channels,
         }
         c2[dy] = interp_i_fcolor(c[dy][0], c[dy][1], rx, f->src->channels);
       }
-      *out++ = interp_i_fcolor(c2[0], c2[1], ry, f->src->channels);
+      *data++ = interp_i_fcolor(c2[0], c2[1], ry, f->src->channels);
       ++i;
     }
   }
@@ -898,14 +880,33 @@ static void fill_imagef(i_fill_t *fill, int x, int y, int width, int channels,
       }
       rx -= ix * f->src->xsize;
       ry -= iy * f->src->ysize;
-      i_gpixf(f->src, rx, ry, out);
-      ++out;
+      i_gpixf(f->src, rx, ry, data);
+      ++data;
       ++i;
     }
   }
-
-  if (fill->combinef) {
-    (fill->combinef)(data, work, channels, width);
+  if (f->src->channels == 3) {
+    /* just set the alpha */
+    for (i = 0; i <  width; ++i) {
+      data->channel[3] = 1.0;
+      data++;
+    }
+  }
+  else if (f->src->channels == 2) {
+    /* copy the alpha to channel 3, duplicate the grey value */
+    for (i = 0; i <  width; ++i) {
+      data->channel[3] = data->channel[1];
+      data->channel[1] = data->channel[2] = data->channel[0];
+      data++;
+    }
+  }
+  else if (f->src->channels == 1) {
+    /* set the alpha, duplicate grey */
+    for (i = 0; i <  width; ++i) {
+      data->channel[3] = 1.0;
+      data->channel[1] = data->channel[2] = data->channel[0];
+      data++;
+    }
   }
 }
 
@@ -936,7 +937,7 @@ static void combine_valuef(i_fcolor *, i_fcolor *, int, int);
 static void combine_color(i_color *, i_color *, int, int);
 static void combine_colorf(i_fcolor *, i_fcolor *, int, int);
 
-struct i_combines {
+static struct i_combines {
   i_fill_combine_f combine;
   i_fill_combinef_f combinef;
 } combines[] =
@@ -1047,7 +1048,6 @@ static void combine_mult(i_color *out, i_color *in, int channels, int count) {
   int ch;
 
   while (count--) {
-    i_color c = *in;
     double mult[MAXCHANNELS];
     mult[3] = in->channel[3];
     for (ch = 0; ch < (channels); ++ch) { 
@@ -1076,8 +1076,6 @@ static void combine_multf(i_fcolor *out, i_fcolor *in, int channels, int count)
 }
 
 static void combine_dissolve(i_color *out, i_color *in, int channels, int count) {
-  int ch;
-
   while (count--) {
     if (in->channel[3] > rand() * (255.0 / RAND_MAX))
       COMBINE(*out, *in, channels);
@@ -1087,8 +1085,6 @@ static void combine_dissolve(i_color *out, i_color *in, int channels, int count)
 }
 
 static void combine_dissolvef(i_fcolor *out, i_fcolor *in, int channels, int count) {
-  int ch;
-
   while (count--) {
     if (in->channel[3] > rand() * (1.0 / RAND_MAX))
       COMBINEF(*out, *in, channels);