added swf_ImageGetNumberOfPaletteEntries2() function
[swftools.git] / lib / modules / swfbits.c
index f238503..5b98b17 100644 (file)
@@ -37,6 +37,30 @@ int swf_ImageHasAlpha(RGBA*img, int width, int height)
     return hasalpha;
 }
 
+int swf_ImageGetNumberOfPaletteEntries2(RGBA*_img, int width, int height)
+{
+    int len = width*height;
+    int t;
+    U32* img = (U32*)_img;
+    U32 color1 = img[0];
+    U32 color2 = 0;
+    for(t=1;t<len;t++) {
+       if(img[t] != color1) {
+           color2 = img[t];
+           break;
+       }
+    }
+    if(t==len)
+       return 1;
+
+    for(;t<len;t++) {
+       if(img[t] != color1 && img[t] != color2) {
+           return width*height;
+       }
+    }
+    return 2;
+}
+
 /*int swf_ImageGetNumberOfPaletteEntries(RGBA*img, int width, int height, RGBA*palette)
 {
     int len = width*height;
@@ -801,6 +825,8 @@ RGBA *swf_DefineLosslessBitsTagToImage(TAG * tag, int *dwidth, int *dheight)
            palette[t].b = data[pos++];
            if (alpha) {
                palette[t].a = data[pos++];
+           } else {
+               palette[t].a = 255;
            }
        }
     }
@@ -820,7 +846,11 @@ RGBA *swf_DefineLosslessBitsTagToImage(TAG * tag, int *dwidth, int *dheight)
                }
            } else {
                for (x = 0; x < width; x++) {
-                   /* TODO: un-premultiply alpha? */
+                   /* TODO: premultiply alpha? 
+                   dest[pos2].r = (data[pos + 1]*255)/data[pos+0];
+                   dest[pos2].g = (data[pos + 2]*255)/data[pos+0];
+                   dest[pos2].b = (data[pos + 3]*255)/data[pos+0];
+                   */
                    dest[pos2].r = data[pos + 1];
                    dest[pos2].g = data[pos + 2];
                    dest[pos2].b = data[pos + 3];
@@ -1091,7 +1121,6 @@ static scale_lookup_t**make_scale_lookup(int width, int newwidth)
     lblockx[newwidth] = p_x;
     return lblockx;
 }
-
 RGBA* swf_ImageScale(RGBA*data, int width, int height, int newwidth, int newheight)
 {
     int x,y;
@@ -1102,6 +1131,12 @@ RGBA* swf_ImageScale(RGBA*data, int width, int height, int newwidth, int newheig
     if(newwidth<1 || newheight<1)
        return 0;
 
+    /* this is bad because this scaler doesn't yet handle monochrome
+       images with 2 colors in a way that the final image hasn't more
+       than 256 colors */
+    if(swf_ImageGetNumberOfPaletteEntries2(data, width, height) == 2)
+       fprintf(stderr, "Warning: scaling monochrome image\n");
+
     tmpline = (rgba_int_t*)malloc(width*sizeof(rgba_int_t));
     newdata = (RGBA*)malloc(newwidth*newheight*sizeof(RGBA));