]> git.imager.perl.org - imager.git/blobdiff - quant.c
- change the "double-include" protection macro that imio.h uses.
[imager.git] / quant.c
diff --git a/quant.c b/quant.c
index be67662e926bea40865b9cf0a0cf4af79f6342e3..2a23b1070624f8a716a525702774589d627da97e 100644 (file)
--- a/quant.c
+++ b/quant.c
@@ -50,7 +50,7 @@ quant_makemap(i_quantize *quant, i_img **imgs, int count) {
       for (r = 0; r < 256; r+=0x33)
        for (g = 0; g < 256; g+=0x33)
          for (b = 0; b < 256; b += 0x33)
-           setcol(quant->mc_colors+i++, r, g, b, 0);
+           setcol(quant->mc_colors+i++, r, g, b, 255);
       quant->mc_count = i;
     }
     break;
@@ -81,6 +81,13 @@ i_palidx *quant_translate(i_quantize *quant, i_img *img) {
   i_palidx *result;
   mm_log((1, "quant_translate(quant %p, img %p)\n", quant, img));
 
+  /* there must be at least one color in the paletted (though even that
+     isn't very useful */
+  if (quant->mc_count == 0) {
+    i_push_error(0, "no colors available for translation");
+    return NULL;
+  }
+
   result = mymalloc(img->xsize * img->ysize);
 
   switch (quant->translate) {
@@ -403,6 +410,9 @@ makemap_addi(i_quantize *quant, i_img **imgs, int count) {
   i_sample_t *line;
   const int *sample_indices;
 
+  mm_log((1, "makemap_addi(quant %p { mc_count=%d, mc_colors=%p }, imgs %p, count %d)\n", 
+          quant, quant->mc_count, quant->mc_colors, imgs, count));
+         
   i_mempool_init(&mp);
 
   clr = i_mempool_alloc(&mp, sizeof(cvec) * quant->mc_size);
@@ -416,6 +426,10 @@ makemap_addi(i_quantize *quant, i_img **imgs, int count) {
   }
   /* mymalloc doesn't clear memory, so I think we need this */
   for (; i < quant->mc_size; ++i) {
+    /*clr[i].r = clr[i].g = clr[i].b = 0;*/
+    clr[i].dr = 0;
+    clr[i].dg = 0;
+    clr[i].db = 0;
     clr[i].fixed = 0;
     clr[i].mcount = 0;
   }
@@ -538,6 +552,12 @@ makemap_addi(i_quantize *quant, i_img **imgs, int count) {
   quant->mc_count = cnum;
 #endif
 
+#if 0
+  mm_log((1, "makemap_addi returns - quant.mc_count = %d\n", quant->mc_count));
+  for (i = 0; i < quant->mc_count; ++i)
+    mm_log((5, "  map entry %d: (%d, %d, %d)\n", i, clr[i].r, clr[i].g, clr[i].b));
+#endif
+
   i_mempool_destroy(&mp);
 }
 
@@ -1525,9 +1545,9 @@ maxdist(int boxnum,cvec *cv) {
   
   bbox(boxnum,&r0,&r1,&g0,&g1,&b0,&b1);
 
-  mr=max(abs(b-b0),abs(b-b1));
-  mg=max(abs(g-g0),abs(g-g1));
-  mb=max(abs(r-r0),abs(r-r1));
+  mr=i_max(abs(b-b0),abs(b-b1));
+  mg=i_max(abs(g-g0),abs(g-g1));
+  mb=i_max(abs(r-r0),abs(r-r1));
   
   return PWR2(mr)+PWR2(mg)+PWR2(mb);
 }
@@ -1547,9 +1567,9 @@ mindist(int boxnum,cvec *cv) {
 
   if (r0<=r && r<=r1 && g0<=g && g<=g1 && b0<=b && b<=b1) return 0;
 
-  mr=min(abs(b-b0),abs(b-b1));
-  mg=min(abs(g-g0),abs(g-g1));
-  mb=min(abs(r-r0),abs(r-r1));
+  mr=i_min(abs(b-b0),abs(b-b1));
+  mg=i_min(abs(g-g0),abs(g-g1));
+  mb=i_min(abs(r-r0),abs(r-r1));
   
   mr=PWR2(mr);
   mg=PWR2(mg);
@@ -1670,7 +1690,8 @@ transparent_errdiff(i_quantize *quant, i_palidx *data, i_img *img,
 }
 
 /* builtin ordered dither maps */
-unsigned char orddith_maps[][64] =
+static unsigned char 
+orddith_maps[][64] =
 {
   { /* random 
        this is purely random - it's pretty awful