synchronized with downstream git
[swftools.git] / lib / modules / swfbits.c
index 72e65af..4643551 100644 (file)
@@ -326,7 +326,7 @@ void swf_SetJPEGBits2(TAG * tag, U16 width, U16 height, RGBA * bitmap, int quali
 }
 #endif
 
-void swf_GetJPEGSize(char *fname, int *width, int *height)
+void swf_GetJPEGSize(const char *fname, int *width, int *height)
 {
     struct jpeg_decompress_struct cinfo;
     struct jpeg_error_mgr jerr;
@@ -347,7 +347,7 @@ void swf_GetJPEGSize(char *fname, int *width, int *height)
     fclose(fi);
 }
 
-int swf_SetJPEGBits(TAG * t, char *fname, int quality)
+int swf_SetJPEGBits(TAG * t, const char *fname, int quality)
 {
     struct jpeg_decompress_struct cinfo;
     struct jpeg_error_mgr jerr;
@@ -561,7 +561,8 @@ static void tag_init_source(struct jpeg_decompress_struct *cinfo)
 static boolean tag_fill_input_buffer(struct jpeg_decompress_struct *cinfo)
 {
     TAG *tag = (TAG *) cinfo->client_data;
-    if (tag->data[tag->pos + 0] == 0xff &&
+    if (tag->pos + 4 <= tag->len &&
+       tag->data[tag->pos + 0] == 0xff &&
        tag->data[tag->pos + 1] == 0xd9 &&
        tag->data[tag->pos + 2] == 0xff &&
        tag->data[tag->pos + 3] == 0xd8) {
@@ -1181,9 +1182,15 @@ TAG* swf_AddImage(TAG*tag, int bitid, RGBA*mem, int width, int height, int quali
     int has_alpha = swf_ImageHasAlpha(mem,width,height);
 
     /* try lossless image */
+
+#ifdef NO_LOSSLESS
+    tag1 = swf_InsertTag(0, /*ST_DEFINEBITSLOSSLESS1/2*/0);
+    tag1->len = 0x7fffffff;
+#else
     tag1 = swf_InsertTag(0, /*ST_DEFINEBITSLOSSLESS1/2*/0);
     swf_SetU16(tag1, bitid);
     swf_SetLosslessImage(tag1, mem, width, height);
+#endif
 
 #if defined(HAVE_JPEGLIB)
     /* try jpeg image */
@@ -1382,6 +1389,97 @@ static void decodeMonochromeImage(RGBA*data, int width, int height, RGBA*colors)
     }
 }
 
+static void blurImage(RGBA*src, int width, int height, int r)
+{
+    int e = 2; // r times e is the sampling interval
+    double*gauss = (double*)malloc(r*e*sizeof(double));
+    double sum=0;
+    int x;
+    for(x=0;x<r*e;x++) {
+        double t = (x - r*e/2.0)/r;
+        gauss[x] = exp(-0.5*t*t);
+        sum += gauss[x];
+    }
+    int*weights = (int*)malloc(r*e*sizeof(int));
+    for(x=0;x<r*e;x++) {
+        weights[x] = (int)(gauss[x]*65536.0001/sum);
+    }
+    int range = r*e/2;
+
+    RGBA*tmp = malloc(sizeof(RGBA)*width*height);
+
+    int y;
+    for(y=0;y<height;y++) {
+        RGBA*s = &src[y*width];
+        RGBA*d = &tmp[y*width];
+        for(x=0;x<range;x++) {
+            d[x] = s[x];
+        }
+        for(x=range;x<width-range;x++) {
+            int r=0;
+            int g=0;
+            int b=0;
+            int a=0;
+            int*f = weights;
+            int xx;
+            for(xx=x-range;xx<x+range;xx++) {
+                r += s[xx].r * f[0];
+                g += s[xx].g * f[0];
+                b += s[xx].b * f[0];
+                a += s[xx].a * f[0];
+                f++;
+            }
+            d[x].r = r >> 16;
+            d[x].g = g >> 16;
+            d[x].b = b >> 16;
+            d[x].a = a >> 16;
+        }
+        for(x=width-range;x<width;x++) {
+            d[x] = s[x];
+        }
+    }
+
+    for(x=0;x<width;x++) {
+        RGBA*s = &tmp[x];
+        RGBA*d = &src[x];
+        int yy=0;
+        for(y=0;y<range;y++) {
+            d[yy] = s[yy];
+            yy+=width;
+        }
+        for(y=range;y<height-range;y++) {
+            int r=0;
+            int g=0;
+            int b=0;
+            int a=0;
+            int*f = weights;
+            int cy,cyy=yy-range*width;
+            for(cy=y-range;cy<y+range;cy++) {
+                r += s[cyy].r * f[0];
+                g += s[cyy].g * f[0];
+                b += s[cyy].b * f[0];
+                a += s[cyy].a * f[0];
+                cyy += width;
+                f++;
+            }
+            d[yy].r = r >> 16;
+            d[yy].g = g >> 16;
+            d[yy].b = b >> 16;
+            d[yy].a = a >> 16;
+            yy += width;
+        }
+        for(y=0;y<range;y++) {
+            d[yy] = s[yy];
+            yy += width;
+        }
+    }
+
+    free(tmp);
+    free(weights);
+    free(gauss);
+}
+
+
 RGBA* swf_ImageScale(RGBA*data, int width, int height, int newwidth, int newheight)
 {
     int x,y;
@@ -1397,6 +1495,14 @@ RGBA* swf_ImageScale(RGBA*data, int width, int height, int newwidth, int newheig
     if(swf_ImageGetNumberOfPaletteEntries2(data, width, height) == 2) {
        monochrome=1;
        encodeMonochromeImage(data, width, height, monochrome_colors);
+        int r1 = width / newwidth;
+        int r2 = height / newheight;
+        int r = r1<r2?r1:r2;
+        if(r>4) {
+            /* high-resolution monochrome images are usually dithered, so 
+               low-pass filter them first to get rid of any moire patterns */
+            blurImage(data, width, height, r+1);
+        }
     }
 
     tmpline = (rgba_int_t*)malloc(width*sizeof(rgba_int_t));