along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+#include <stdlib.h>
+#include <stdio.h>
+#include <memory.h>
+#include "../../config.h"
+#ifdef HAVE_ZLIB
+#include <zconf.h>
+#include <zlib.h>
+#endif
+#include <fcntl.h>
+#include <ctype.h>
+
+#ifdef HAVE_JPEGLIB
+#define HAVE_BOOLEAN
#ifdef __cplusplus
extern "C" {
#endif
-#include "jpeglib.h"
+#include <jpeglib.h>
#ifdef __cplusplus
}
#endif
+#endif // HAVE_JPEGLIB
+
+#include "../rfxswf.h"
#define OUTBUFFER_SIZE 0x8000
return 0;
}
-void swf_SetJPEGBits2(TAG * tag, U16 width, U16 height, RGBA * bitmap,
- int quality)
+#if defined(HAVE_JPEGLIB)
+void swf_SetJPEGBits2(TAG * tag, U16 width, U16 height, RGBA * bitmap, int quality)
{
JPEGBITS *jpeg;
int y;
rfx_free(scanline);
swf_SetJPEGBitsFinish(jpeg);
}
+#else
+void swf_SetJPEGBits2(TAG * tag, U16 width, U16 height, RGBA * bitmap, int quality)
+{
+ fprintf(stderr, "Error: swftools compiled without jpeglib\n");
+ return -1;
+}
+#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;
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;
return 0;
}
+#else
+int swf_SetJPEGBits3(TAG * tag, U16 width, U16 height, RGBA * bitmap, int quality)
+{
+ fprintf(stderr, "Error: swftools compiled without jpeglib\n");
+ return -1;
+}
+#endif
+
+
/* expects mem to be non-premultiplied */
TAG* swf_AddImage(TAG*tag, int bitid, RGBA*mem, int width, int height, int quality)
{
swf_SetU16(tag1, bitid);
swf_SetLosslessImage(tag1, mem, width, height);
+#if defined(HAVE_JPEGLIB)
/* try jpeg image */
if(has_alpha) {
tag2 = swf_InsertTag(0, ST_DEFINEBITSJPEG3);
swf_SetU16(tag2, bitid);
swf_SetJPEGBits2(tag2, width, height, mem, quality);
}
+#endif
- if(quality>100 || (tag1 && tag1->len < tag2->len)) {
+ if(quality>100 || !tag2 || (tag1 && tag1->len < tag2->len)) {
/* use the zlib version- it's smaller */
tag1->prev = tag;
if(tag) tag->next = tag1;
tag = tag1;
- swf_DeleteTag(tag2);
+ swf_DeleteTag(0, tag2);
} else {
/* use the jpeg version- it's smaller */
tag2->prev = tag;
if(tag) tag->next = tag2;
tag = tag2;
- swf_DeleteTag(tag1);
+ swf_DeleteTag(0, tag1);
}
return tag;
}
-#endif
-
RGBA *swf_ExtractImage(TAG * tag, int *dwidth, int *dheight)
{
RGBA *img;
}
if (swf->firstTag == tables_tag)
swf->firstTag = tables_tag->next;
- swf_DeleteTag(tables_tag);
+ swf_DeleteTag(swf, tables_tag);
}
typedef struct scale_lookup {
}
}
+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;
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));