+ if(mp3)
+ printf(" [-m] 1 MP3 Soundstream\n");
+}
+
+void handlefont(SWF*swf, TAG*tag)
+{
+ SWFFONT* f=0;
+ U16 id;
+ char name[80];
+ char*filename = name;
+ int t;
+
+ id = swf_GetDefineID(tag);
+ sprintf(name, "font%d.swf", id);
+ if(numextracts==1) {
+ filename = destfilename;
+ }
+
+ swf_FontExtract(swf, id, &f);
+ if(!f) {
+ printf("Couldn't extract font %d\n", id);
+ return;
+ }
+ if(!f->layout)
+ swf_FontCreateLayout(f);
+
+ swf_WriteFont(f, filename);
+ swf_FontFree(f);
+}
+
+U8*jpegtables = 0;
+int jpegtablessize;
+
+void handlejpegtables(TAG*tag)
+{
+ if(tag->id == ST_JPEGTABLES) {
+ jpegtables = tag->data;
+ jpegtablessize = tag->len;
+ }
+}
+
+FILE* save_fopen(char* name, char* mode)
+{
+ FILE*fi = fopen(name, mode);
+ if(!fi) {
+ fprintf(stderr, "Error: Couldn't open %s\n", name);
+ exit(1);
+ }
+ return fi;
+}
+
+int findjpegboundary(U8*data, int len)
+{
+ int t;
+ int pos=-1;
+ for(t=0;t<len;t++) {
+ if(data[t ]==0xff &&
+ data[t+1]==0xd9 &&
+ data[t+2]==0xff &&
+ data[t+3]==0xd8) {
+ pos = t;
+ }
+ }
+ return pos;
+}
+
+/* extract jpeg data out of a tag */
+void handlejpeg(TAG*tag)
+{
+ char name[80];
+ char*filename = name;
+ FILE*fi;
+
+ sprintf(name, "pic%d.jpg", GET16(tag->data));
+ if(numextracts==1) {
+ filename = destfilename;
+ if(!strcmp(filename,"output.swf"))
+ filename = "output.jpg";
+ }
+ /* swf jpeg images have two streams, which both start with ff d8 and
+ end with ff d9. The following code handles sorting the middle
+ <ff d9 ff d8> bytes out, so that one stream remains */
+ if(tag->id == ST_DEFINEBITSJPEG && tag->len>2 && jpegtables) {
+ fi = save_fopen(filename, "wb");
+ fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
+ fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
+ fclose(fi);
+ }
+ else if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
+ int end = tag->len;
+ int pos = findjpegboundary(&tag->data[2], tag->len-2);
+ if(pos>=0) {
+ pos+=2;
+ fi = save_fopen(filename, "wb");
+ fwrite(&tag->data[2], pos-2, 1, fi);
+ fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
+ fclose(fi);
+ } else {
+ fi = save_fopen(filename, "wb");
+ fwrite(&tag->data[2], end-2, 1, fi);
+ fclose(fi);
+ }
+ }
+ else if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
+ U32 end = GET32(&tag->data[2])+6;
+ int pos = findjpegboundary(&tag->data[6], tag->len-6);
+ if(pos<0)
+ return;
+ pos+=6;
+ fi = save_fopen(filename, "wb");
+ fwrite(&tag->data[6], pos-6, 1, fi);
+ fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
+ fclose(fi);
+ }
+ else {
+ int id = GET16(tag->data);
+ fprintf(stderr, "Object %d is not a JPEG picture!\n",id);
+ exit(1);
+ }
+}
+
+#ifdef _ZLIB_INCLUDED_
+static U32 mycrc32;
+
+static U32*crc32_table = 0;
+static void make_crc32_table(void)
+{
+ int t;
+ if(crc32_table)
+ return;
+ crc32_table = (U32*)malloc(1024);
+
+ for (t = 0; t < 256; t++) {
+ U32 c = t;
+ int s;
+ for (s = 0; s < 8; s++) {
+ c = (0xedb88320L*(c&1)) ^ (c >> 1);
+ }
+ crc32_table[t] = c;
+ }
+}
+static inline void png_write_byte(FILE*fi, U8 byte)
+{
+ fwrite(&byte,1,1,fi);
+ mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
+}
+static void png_start_chunk(FILE*fi, char*type, int len)
+{
+ U8 mytype[4]={0,0,0,0};
+ U32 mylen = REVERSESWAP32(len);
+ memcpy(mytype,type,strlen(type));
+ fwrite(&mylen, 4, 1, fi);
+ mycrc32=0xffffffff;
+ png_write_byte(fi,mytype[0]);
+ png_write_byte(fi,mytype[1]);
+ png_write_byte(fi,mytype[2]);
+ png_write_byte(fi,mytype[3]);
+}
+static void png_write_bytes(FILE*fi, U8*bytes, int len)
+{
+ int t;
+ for(t=0;t<len;t++)
+ png_write_byte(fi,bytes[t]);
+}
+static void png_write_dword(FILE*fi, U32 dword)
+{
+ png_write_byte(fi,dword>>24);
+ png_write_byte(fi,dword>>16);
+ png_write_byte(fi,dword>>8);
+ png_write_byte(fi,dword);
+}
+static void png_end_chunk(FILE*fi)
+{
+ U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
+ fwrite(&tmp,4,1,fi);
+}
+
+
+/* extract a lossless image (png) out of a tag
+ This routine was originally meant to be a one-pager. I just
+ didn't know png is _that_ much fun. :) -mk
+ */
+void handlelossless(TAG*tag)
+{
+ char name[80];
+ char*filename = name;
+ FILE*fi;
+ int width, height;
+ int crc;
+ int id;
+ int t;
+ U8 bpp = 1;
+ U8 format;
+ U8 tmp;
+ Bytef* data=0;
+ U8* data2=0;
+ U8* data3=0;
+ uLongf datalen;
+ uLongf datalen2;
+ U32 datalen3;
+ U8 head[] = {137,80,78,71,13,10,26,10};
+ int cols;
+ char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
+ RGBA* palette;
+ int pos;
+ int error;
+ U32 tmp32;
+
+ make_crc32_table();
+
+ if(tag->id != ST_DEFINEBITSLOSSLESS &&
+ tag->id != ST_DEFINEBITSLOSSLESS2) {
+ int id = GET16(tag->data);
+ fprintf(stderr, "Object %d is not a PNG picture!\n",id);
+ exit(1);
+ }
+
+ id =swf_GetU16(tag);
+ format = swf_GetU8(tag);
+ if(format == 3) bpp = 8;
+ if(format == 4) bpp = 16;
+ if(format == 5) bpp = 32;
+ if(format!=3 && format!=5) {
+ if(format==4)
+ fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
+ else
+ fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
+ return;
+ }
+ width = swf_GetU16(tag);
+ height = swf_GetU16(tag);
+ if(format == 3) cols = swf_GetU8(tag) + 1;
+// this is what format means according to the flash specification. (which is
+// clearly wrong)
+// if(format == 4) cols = swf_GetU16(tag) + 1;
+// if(format == 5) cols = swf_GetU32(tag) + 1;
+ else cols = 0;
+
+ msg("<verbose> Width %d", width);
+ msg("<verbose> Height %d", height);
+ msg("<verbose> Format %d", format);
+ msg("<verbose> Cols %d", cols);
+ msg("<verbose> Bpp %d", bpp);
+
+ datalen = (width*height*bpp/8+cols*8);
+ do {
+ if(data)
+ free(data);
+ datalen+=4096;
+ data = malloc(datalen);
+ error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
+ } while(error == Z_BUF_ERROR);
+ if(error != Z_OK) {
+ fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
+ return;
+ }
+ msg("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
+ pos = 0;
+ datalen2 = datalen;
+ data2 = malloc(datalen2);
+ palette = (RGBA*)malloc(cols*sizeof(RGBA));
+
+ for(t=0;t<cols;t++) {
+ palette[t].r = data[pos++];
+ palette[t].g = data[pos++];
+ palette[t].b = data[pos++];
+ if(alpha) {
+ palette[t].a = data[pos++];
+ }
+ }
+
+ sprintf(name, "pic%d.png", id);
+ if(numextracts==1) {
+ filename = destfilename;
+ if(!strcmp(filename,"output.swf"))
+ filename = "output.png";
+ }
+ fi = save_fopen(filename, "wb");
+ fwrite(head,sizeof(head),1,fi);
+
+ png_start_chunk(fi, "IHDR", 13);
+ png_write_dword(fi,width);
+ png_write_dword(fi,height);
+ png_write_byte(fi,8);
+ if(format == 3)
+ png_write_byte(fi,3); //indexed
+ else if(format == 5 && alpha==0)
+ png_write_byte(fi,2); //rgb
+ else if(format == 5 && alpha==1)
+ png_write_byte(fi,6); //rgba
+ else return;
+
+ png_write_byte(fi,0); //compression mode
+ png_write_byte(fi,0); //filter mode
+ png_write_byte(fi,0); //interlace mode
+ png_end_chunk(fi);
+
+ if(format == 3) {
+ png_start_chunk(fi, "PLTE", 768);
+
+ for(t=0;t<256;t++) {
+ png_write_byte(fi,palette[t].r);
+ png_write_byte(fi,palette[t].g);
+ png_write_byte(fi,palette[t].b);
+ }
+ png_end_chunk(fi);
+
+ if(alpha) {
+ /* write alpha palette */
+ png_start_chunk(fi, "tRNS", 256);
+ for(t=0;t<256;t++) {
+ png_write_byte(fi,palette[t].a);