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;
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;
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) {
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)
{
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 */
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 {
- int pos;
- unsigned int weight;
-} scale_lookup_t;
-
-typedef struct rgba_int {
- unsigned int r,g,b,a;
-} rgba_int_t;
-
-static int bicubic = 0;
-
-static scale_lookup_t**make_scale_lookup(int width, int newwidth)
-{
- scale_lookup_t*lookupx = (scale_lookup_t*)malloc((width>newwidth?width:newwidth)*2*sizeof(scale_lookup_t));
- scale_lookup_t**lblockx = (scale_lookup_t**)malloc((newwidth+1)*sizeof(scale_lookup_t**));
- double fx = ((double)width)/((double)newwidth);
- double px = 0;
- int x;
- scale_lookup_t*p_x = lookupx;
-
- if(newwidth<=width) {
- for(x=0;x<newwidth;x++) {
- double ex = px + fx;
- int fromx = (int)px;
- int tox = (int)ex;
- double rem = fromx+1-px;
- int i = (int)(256/fx);
- int xweight = (int)(rem*256/fx);
- int xx;
- int w = 0;
- lblockx[x] = p_x;
- if(tox>=width) tox = width-1;
- for(xx=fromx;xx<=tox;xx++) {
- if(xx==fromx && xx==tox) p_x->weight = 256;
- else if(xx==fromx) p_x->weight = xweight;
- else if(xx==tox) p_x->weight = 256-w;
- else p_x->weight = i;
- w+=p_x->weight;
- p_x->pos = xx;
- p_x++;
- }
- px = ex;
- }
- } else {
- for(x=0;x<newwidth;x++) {
- int ix1 = (int)px;
- int ix2 = ((int)px)+1;
- double r = px-ix1;
- if(ix2>=width) ix2=width-1;
- lblockx[x] = p_x;
- if(bicubic)
- r = -2*r*r*r+3*r*r;
- p_x[0].weight = (int)(256*(1-r));
- p_x[0].pos = ix1;
- p_x[1].weight = 256-p_x[0].weight;
- p_x[1].pos = ix2;
- p_x+=2;
- px += fx;
- }
- }
- lblockx[newwidth] = p_x;
- return lblockx;
-}
-
-static void encodeMonochromeImage(RGBA*data, int width, int height, RGBA*colors)
-{
- int t;
- int len = width*height;
-
- U32* img = (U32*)data;
- U32 color1 = img[0];
- U32 color2 = 0;
- for(t=1;t<len;t++) {
- if(img[t] != color1) {
- color2 = img[t];
- break;
- }
- }
- *(U32*)&colors[0] = color1;
- *(U32*)&colors[1] = color2;
- for(t=0;t<len;t++) {
- if(img[t] == color1) {
- img[t] = 0;
- } else {
- img[t] = 0xffffffff;
- }
- }
-}
-
-static void decodeMonochromeImage(RGBA*data, int width, int height, RGBA*colors)
-{
- int t;
- int len = width*height;
-
- for(t=0;t<len;t++) {
- U32 m = data[t].r;
- data[t].r = (colors[0].r * (255-m) + colors[1].r * m) >> 8;
- data[t].g = (colors[0].g * (255-m) + colors[1].g * m) >> 8;
- data[t].b = (colors[0].b * (255-m) + colors[1].b * m) >> 8;
- data[t].a = (colors[0].a * (255-m) + colors[1].a * m) >> 8;
- }
-}
-
-RGBA* swf_ImageScale(RGBA*data, int width, int height, int newwidth, int newheight)
-{
- int x,y;
- RGBA* newdata;
- scale_lookup_t *p, **lblockx,**lblocky;
- rgba_int_t*tmpline;
- int monochrome = 0;
- RGBA monochrome_colors[2];
-
- if(newwidth<1 || newheight<1)
- return 0;
-
- if(swf_ImageGetNumberOfPaletteEntries2(data, width, height) == 2) {
- monochrome=1;
- encodeMonochromeImage(data, width, height, monochrome_colors);
- }
-
- tmpline = (rgba_int_t*)malloc(width*sizeof(rgba_int_t));
- newdata = (RGBA*)malloc(newwidth*newheight*sizeof(RGBA));
-
- lblockx = make_scale_lookup(width, newwidth);
- lblocky = make_scale_lookup(height, newheight);
-
- for(p=lblocky[0];p<lblocky[newheight];p++)
- p->pos*=width;
-
- for(y=0;y<newheight;y++) {
- RGBA*destline = &newdata[y*newwidth];
-
- /* create lookup table for y */
- rgba_int_t*l = tmpline;
- scale_lookup_t*p_y,*p_x;
- memset(tmpline, 0, width*sizeof(rgba_int_t));
- for(p_y=lblocky[y];p_y<lblocky[y+1];p_y++) {
- RGBA*line = &data[p_y->pos];
- scale_lookup_t*p_x;
- int weight = p_y->weight;
- for(x=0;x<width;x++) {
- tmpline[x].r += line[x].r*weight;
- tmpline[x].g += line[x].g*weight;
- tmpline[x].b += line[x].b*weight;
- tmpline[x].a += line[x].a*weight;
- }
- }
-
- /* process x direction */
- p_x = lblockx[0];
- for(x=0;x<newwidth;x++) {
- unsigned int r=0,g=0,b=0,a=0;
- scale_lookup_t*p_x_to = lblockx[x+1];
- do {
- rgba_int_t* col = &tmpline[p_x->pos];
- unsigned int weight = p_x->weight;
- r += col->r*weight;
- g += col->g*weight;
- b += col->b*weight;
- a += col->a*weight;
- p_x++;
- } while (p_x<p_x_to);
-
- destline->r = r >> 16;
- destline->g = g >> 16;
- destline->b = b >> 16;
- destline->a = a >> 16;
-
- destline++;
- }
- }
-
- if(monochrome)
- decodeMonochromeImage(newdata, newwidth, newheight, monochrome_colors);
-
- free(tmpline);
- free(*lblockx);
- free(lblockx);
- free(*lblocky);
- free(lblocky);
- return newdata;
-}
-
-