]> git.imager.perl.org - imager.git/blobdiff - polygon.c
commit changes from draw branch
[imager.git] / polygon.c
index 257713ca54398bd339a801da942f35f528dde883..da5d7beb1c8aa2f39fe1049e6e9bc5c111769c19 100644 (file)
--- a/polygon.c
+++ b/polygon.c
@@ -1,7 +1,7 @@
-#include "image.h"
+#include "imager.h"
 #include "draw.h"
 #include "log.h"
-
+#include "imrender.h"
 
 #define IMTRUNC(x) ((int)((x)*16))
 
@@ -91,7 +91,7 @@ p_eval_atx(p_line *l, pcord x) {
 
 static
 p_line *
-line_set_new(double *x, double *y, int l) {
+line_set_new(const double *x, const double *y, int l) {
   int i;
   p_line *lset = mymalloc(sizeof(p_line) * l);
 
@@ -111,7 +111,7 @@ line_set_new(double *x, double *y, int l) {
 
 static
 p_point *
-point_set_new(double *x, double *y, int l) {
+point_set_new(const double *x, const double *y, int l) {
   int i;
   p_point *pset = mymalloc(sizeof(p_point) * l);
   
@@ -264,12 +264,12 @@ saturate(int in) {
   return 0;
 }
 
-typedef void (*scanline_flusher)(i_img *im, ss_scanline *ss, int y, void *ctx);
+typedef void (*scanline_flusher)(i_img *im, ss_scanline *ss, int y, const void *ctx);
 
 /* This function must be modified later to do proper blending */
 
 static void
-scanline_flush(i_img *im, ss_scanline *ss, int y, void *ctx) {
+scanline_flush(i_img *im, ss_scanline *ss, int y, const void *ctx) {
   int x, ch, tv;
   i_color t;
   i_color *val = (i_color *)ctx;
@@ -339,6 +339,8 @@ pixel_coverage(p_line *line, pcord minx, pcord maxx, pcord  miny, pcord maxy) {
       (maxx-p_eval_aty(line, miny))*(p_eval_atx(line, maxx)-miny)/2.0;
     return r;
   }
+
+  return 0; /* silence compiler warning */
 }
 
 
@@ -529,11 +531,12 @@ render_slice_scanline_old(ss_scanline *ss, int y, p_line *l, p_line *r) {
 
 
 static void
-i_poly_aa_low(i_img *im, int l, double *x, double *y, void *ctx, scanline_flusher flusher) {
+i_poly_aa_low(i_img *im, int l, const double *x, const double *y, void const *ctx, scanline_flusher flusher) {
   int i ,k;                    /* Index variables */
   int clc;                     /* Lines inside current interval */
-  pcord tempy;
-  int cscl;                    /* Current scanline */
+  /* initialize to avoid compiler warnings */
+  pcord tempy = 0;
+  int cscl = 0;                        /* Current scanline */
 
   ss_scanline templine;                /* scanline accumulator */
   p_point *pset;               /* List of points in polygon */
@@ -637,10 +640,11 @@ i_poly_aa_low(i_img *im, int l, double *x, double *y, void *ctx, scanline_flushe
 } /* Function */
 
 void
-i_poly_aa(i_img *im, int l, double *x, double *y, i_color *val) {
+i_poly_aa(i_img *im, int l, const double *x, const double *y, const i_color *val) {
   i_poly_aa_low(im, l, x, y, val, scanline_flush);
 }
 
+#if 0
 struct poly_cfill_state {
   i_color *fillbuf;
   i_color *linebuf;
@@ -649,11 +653,11 @@ struct poly_cfill_state {
 };
 
 static void
-scanline_flush_cfill(i_img *im, ss_scanline *ss, int y, void *ctx) {
+scanline_flush_cfill(i_img *im, ss_scanline *ss, int y, const void *ctx) {
   int x, ch, tv;
   int pos;
   int left, right;
-  struct poly_cfill_state *state = (struct poly_cfill_state *)ctx;
+  struct poly_cfill_state const *state = (struct poly_cfill_state const *)ctx;
   i_color *fillbuf = state->fillbuf;
   i_color *line = state->linebuf;
 
@@ -711,11 +715,11 @@ struct poly_cfill_state_f {
 };
 
 static void
-scanline_flush_cfill_f(i_img *im, ss_scanline *ss, int y, void *ctx) {
+scanline_flush_cfill_f(i_img *im, ss_scanline *ss, int y, const void *ctx) {
   int x, ch, tv;
   int pos;
   int left, right;
-  struct poly_cfill_state_f *state = (struct poly_cfill_state_f *)ctx;
+  struct poly_cfill_state_f const *state = (struct poly_cfill_state_f const *)ctx;
   i_fcolor *fillbuf = state->fillbuf;
   i_fcolor *line = state->linebuf;
 
@@ -764,9 +768,52 @@ scanline_flush_cfill_f(i_img *im, ss_scanline *ss, int y, void *ctx) {
     i_plinf(im, left, right, y, line);
   }
 }
+#endif
+
+struct poly_render_state {
+  i_render render;
+  i_fill_t *fill;
+  unsigned char *cover;
+};
+
+static void
+scanline_flush_render(i_img *im, ss_scanline *ss, int y, const void *ctx) {
+  int x, ch, tv;
+  int pos;
+  int left, right;
+  struct poly_render_state const *state = (struct poly_render_state const *)ctx;
+
+  left = 0;
+  while (left < im->xsize && ss->line[left] <= 0)
+    ++left;
+  if (left < im->xsize) {
+    right = im->xsize;
+    /* since going from the left found something, moving from the 
+       right should */
+    while (/* right > left && */ ss->line[right-1] <= 0) 
+      --right;
+    
+    /* convert to the format the render interface wants */
+    for (x = left; x < right; ++x) {
+      state->cover[x-left] = saturate(ss->line[x]);
+    }
+    i_render_fill(&state->render, left, y, right-left, state->cover, 
+                 state->fill);
+  }
+}
 
 void
-i_poly_aa_cfill(i_img *im, int l, double *x, double *y, i_fill_t *fill) {
+i_poly_aa_cfill(i_img *im, int l, const double *x, const double *y, 
+               i_fill_t *fill) {
+  struct poly_render_state ctx;
+
+  i_render_init(&ctx.render, im, im->xsize);
+  ctx.fill = fill;
+  ctx.cover = mymalloc(im->xsize);
+  i_poly_aa_low(im, l, x, y, &ctx, scanline_flush_render);
+  myfree(ctx.cover);
+  i_render_done(&ctx.render);
+#if 0
   if (im->bits == i_8_bits && fill->fill_with_color) {
     struct poly_cfill_state ctx;
     ctx.fillbuf = mymalloc(sizeof(i_color) * im->xsize * 2);
@@ -787,4 +834,5 @@ i_poly_aa_cfill(i_img *im, int l, double *x, double *y, i_fill_t *fill) {
     myfree(ctx.fillbuf);
     myfree(ctx.cover);
   }
+#endif
 }