}
return hasalpha;
}
-
-int swf_ImageGetNumberOfPaletteEntries(RGBA*img, int width, int height, RGBA*palette)
+
+/*int swf_ImageGetNumberOfPaletteEntries(RGBA*img, int width, int height, RGBA*palette)
{
int len = width*height;
int t;
int palsize = 0;
- RGBA pal[256];
+ RGBA pal[512];
+ U32*pal32=(U32*)pal;
int palette_overflow = 0;
- for(t=0;t<len;t++) {
+ U32 lastcol32 = 0;
+
+ if(sizeof(RGBA)!=sizeof(U32))
+ fprintf(stderr, "rfxswf: sizeof(RGBA)!=sizeof(U32))");
+
+ lastcol32 = pal32[palsize++] = *(U32*)&img[0];
+
+ for(t=1;t<len;t++) {
RGBA col = img[t];
U32 col32 = *(U32*)&img[t];
- int i=0;
+ int i;
+ if(col32==lastcol32)
+ continue;
for(i=0;i<palsize;i++) {
- if(col32 == *(U32*)&pal[i])
+ if(col32 == pal32[i])
break;
}
if(i==palsize) {
- if(palsize==256) {
+ if(palsize==512) {
palette_overflow = 1;
break;
}
- pal[palsize++] = col;
+ pal32[palsize++] = col32;
}
+ lastcol32 = col32;
}
if(palette_overflow)
return width*height;
if(palette)
memcpy(palette, pal, palsize*sizeof(RGBA));
return palsize;
+}*/
+
+int swf_ImageGetNumberOfPaletteEntries(RGBA*img, int width, int height, RGBA*palette)
+{
+ int len = width*height;
+ int t;
+ int palsize = 0;
+ U32* pal;
+ int size[256];
+ int palette_overflow = 0;
+ U32 lastcol32 = 0;
+
+ pal = malloc(65536*sizeof(U32));
+
+ memset(size, 0, sizeof(size));
+
+ if(sizeof(RGBA)!=sizeof(U32))
+ fprintf(stderr, "rfxswf: sizeof(RGBA)!=sizeof(U32))");
+
+ lastcol32 = (*(U32*)&img[0])^0xffffffff; // don't match
+
+ for(t=0;t<len;t++) {
+ RGBA col = img[t];
+ U32 col32 = *(U32*)&img[t];
+ int i;
+ U32 hash;
+ if(col32 == lastcol32)
+ continue;
+ hash = (col32 >> 17) ^ col32;
+ hash ^= ((hash>>8) + 1) ^ hash;
+ hash &= 255;
+
+ int csize = size[hash];
+ U32* cpal = &pal[hash*256];
+ for(i=0;i<csize;i++) {
+ if(col32 == cpal[i])
+ break;
+ }
+ if(i==csize) {
+ if(palsize==256) {
+ palette_overflow = 1;
+ break;
+ }
+ cpal[size[hash]++] = col32;
+ palsize++;
+ }
+ lastcol32 = col32;
+ }
+ if(palette_overflow)
+ return width*height;
+ if(palette) {
+ int i = 0;
+ for(t=0;t<256;t++) {
+ int s;
+ int csize = size[t];
+ U32* cpal = &pal[t*256];
+ for(s=0;s<csize;s++)
+ palette[i++] = *(RGBA*)(&cpal[s]);
+ }
+ }
+ free(pal);
+ return palsize;
}
+
+
#ifdef HAVE_JPEGLIB
typedef struct _JPEGDESTMGR {
rfx_free(data);
return 0;
}
+
+TAG* swf_AddImage(TAG*tag, int bitid, RGBA*mem, int width, int height, int quality)
+{
+ TAG *tag1 = 0, *tag2 = 0;
+ int has_alpha = swf_ImageHasAlpha(mem,width,height);
+
+ /* try lossless image */
+ tag1 = swf_InsertTag(0, /*ST_DEFINEBITSLOSSLESS1/2*/0);
+ swf_SetU16(tag1, bitid);
+ swf_SetLosslessImage(tag1, mem, width, height);
+
+ /* try jpeg image */
+ if(has_alpha) {
+ tag2 = swf_InsertTag(0, ST_DEFINEBITSJPEG3);
+ swf_SetU16(tag2, bitid);
+ swf_SetJPEGBits3(tag2, width, height, mem, quality);
+ } else {
+ tag2 = swf_InsertTag(0, ST_DEFINEBITSJPEG2);
+ swf_SetU16(tag2, bitid);
+ swf_SetJPEGBits2(tag2, width, height, mem, quality);
+ }
+
+ if(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);
+ } else {
+ /* use the jpeg version- it's smaller */
+ tag2->prev = tag;
+ if(tag) tag->next = tag2;
+ tag = tag2;
+ swf_DeleteTag(tag1);
+ }
+ return tag;
+}
+
#endif
RGBA *swf_ExtractImage(TAG * tag, int *dwidth, int *dheight)
void *data = rfx_alloc(tag->len);
swf_GetBlock(tag, data, tag->len);
swf_ResetTag(tag, ST_DEFINEBITSJPEG2);
+ swf_SetBlock(tag, &((U8*)data)[0], 2); //id
swf_SetBlock(tag, tables_tag->data, tables_tag->len);
- swf_SetBlock(tag, data, tag->len);
+ swf_SetBlock(tag, &((U8*)data)[2], tag->len-2);
free(data);
}
tag = tag->next;