+ for(y=0;y<height;y++) {
+ int srcwidth = width * (bpp/8);
+ if(bpp==32) {
+ if(!alpha) {
+ // 32 bit to 24 bit "conversion"
+ for(x=0;x<width;x++) {
+ dest[pos2].r=data[pos+1];
+ dest[pos2].g=data[pos+2];
+ dest[pos2].b=data[pos+3];
+ dest[pos2].a=255;
+ pos2++;
+ pos+=4; //ignore padding byte
+ }
+ } else {
+ for(x=0;x<width;x++) {
+ dest[pos2].r=data[pos+1];
+ dest[pos2].g=data[pos+2];
+ dest[pos2].b=data[pos+3];
+ dest[pos2].a=data[pos+0]; //alpha
+ pos2++;
+ pos+=4;
+ }
+ }
+ } else {
+ for(x=0;x<srcwidth;x++) {
+ dest[pos2]=palette[data[pos++]];
+ pos2++;
+ }
+ }
+ pos+=((srcwidth+3)&~3)-srcwidth; //align
+ }
+ if(palette)
+ rfx_free(palette);
+ rfx_free(data);
+ return dest;
+}
+
+#endif // HAVE_ZLIB
+
+#if defined(HAVE_ZLIB) && defined(HAVE_JPEGLIB)
+int swf_SetJPEGBits3(TAG * tag,U16 width,U16 height,RGBA* bitmap, int quality)
+{
+ JPEGBITS* jpeg;
+ int y;
+ int pos;
+ int res = 0;
+ U8 * data;
+ z_stream zs;
+
+ pos = tag->len;
+ swf_SetU32(tag, 0); //placeholder
+ jpeg = swf_SetJPEGBitsStart(tag,width,height,quality);
+ for (y=0;y<height;y++)
+ { U8 scanline[3*width];
+ int x,p = 0;
+ for (x=0;x<width;x++)
+ { scanline[p++] = bitmap[width*y+x].r;
+ scanline[p++] = bitmap[width*y+x].g;
+ scanline[p++] = bitmap[width*y+x].b;
+ }
+ swf_SetJPEGBitsLine(jpeg,scanline);
+ }
+ swf_SetJPEGBitsFinish(jpeg);
+ PUT32(&tag->data[pos], tag->len - pos - 4);
+
+ data=rfx_alloc(OUTBUFFER_SIZE);
+ memset(&zs,0x00,sizeof(z_stream));
+
+ if (deflateInit(&zs,Z_DEFAULT_COMPRESSION)!=Z_OK) {
+ fprintf(stderr, "rfxswf: zlib compression failed");
+ return -3;
+ }
+
+ zs.next_out = data;
+ zs.avail_out = OUTBUFFER_SIZE;
+
+ for (y=0;y<height;y++)
+ { U8 scanline[width];
+ int x,p = 0;
+ for (x=0;x<width;x++) {
+ scanline[p++] = bitmap[width*y+x].a;
+ }
+ zs.avail_in = width;
+ zs.next_in = scanline;
+
+ while(1) {
+ if(deflate(&zs, Z_NO_FLUSH) != Z_OK) {
+ fprintf(stderr, "rfxswf: zlib compression failed");
+ return -4;
+ }
+ if(zs.next_out != data) {
+ swf_SetBlock(tag, data, zs.next_out - data);
+ zs.next_out = data;
+ zs.avail_out = OUTBUFFER_SIZE;
+ }
+ if(!zs.avail_in) {
+ break;
+ }
+ }
+ }
+
+ while(1) {
+ int ret = deflate(&zs, Z_FINISH);
+ if (ret != Z_OK &&
+ ret != Z_STREAM_END) {
+ fprintf(stderr, "rfxswf: zlib compression failed");
+ return -5;
+ }
+ if(zs.next_out != data) {
+ swf_SetBlock(tag, data, zs.next_out - data);
+ zs.next_out = data;
+ zs.avail_out = OUTBUFFER_SIZE;
+ }
+ if (ret == Z_STREAM_END) {
+ break;
+ }
+ }
+
+ deflateEnd(&zs);
+ rfx_free(data);
+ return 0;
+}
+#endif
+
+RGBA* swf_ExtractImage(TAG*tag, int*dwidth, int*dheight)
+{
+ RGBA*img;
+ if(tag->id == ST_DEFINEBITSJPEG ||
+ tag->id == ST_DEFINEBITSJPEG2 ||
+ tag->id == ST_DEFINEBITSJPEG3) {
+#ifdef HAVE_JPEGLIB
+ return swf_JPEG2TagToImage(tag, dwidth, dheight);
+#else
+ fprintf(stderr, "rfxswf: Error: No JPEG library compiled in");
+ return 0;
+#endif
+ }
+ if(tag->id == ST_DEFINEBITSLOSSLESS ||
+ tag->id == ST_DEFINEBITSLOSSLESS2) {
+#ifdef HAVE_ZLIB
+ return swf_DefineLosslessBitsTagToImage(tag, dwidth, dheight);
+#else
+ fprintf(stderr, "rfxswf: Error: No JPEG library compiled in");
+ return 0;
+#endif
+ }
+ fprintf(stderr, "rfxswf: Error: Invalid tag (%d, %s)", tag->id, swf_TagGetName(tag));
+ return 0;
+}