small file extension bug fix.
[swftools.git] / src / swfextract.c
index d7cbf10..9393ff6 100644 (file)
@@ -4,8 +4,20 @@
    Part of the swftools package.
    
    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
 
-   This file is distributed under the GPL, see file COPYING for details */
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
 #include <unistd.h>
 #include <stdio.h>
@@ -273,7 +285,9 @@ void extractTag(SWF*swf, char*filename)
        changed = 0;
        for(t=0;t<65536;t++) {
           if(used[t] && !(used[t]&2)) {
-            if(tags[t]->id==ST_DEFINESPRITE) {
+            if(tags[t]==0) {
+                msg("<warning> ID %d is referenced, but never defined.", t);
+            } else if(tags[t]->id==ST_DEFINESPRITE) {
                 TAG*tag = tags[t];
                 while(tag->id != ST_END)
                 {
@@ -370,80 +384,104 @@ void extractTag(SWF*swf, char*filename)
     swf_FreeTags(&newswf);                       // cleanup
 }
 
+int isOfType(int t, TAG*tag)
+{
+    int show = 0;
+    if(t == 0 && (tag->id == ST_DEFINESHAPE ||
+       tag->id == ST_DEFINESHAPE2 ||
+       tag->id == ST_DEFINESHAPE3)) {
+       show = 1;
+    }
+    if(t==1 && tag->id == ST_DEFINESPRITE) {
+       show = 1;
+    }
+    if(t == 2 && (tag->id == ST_DEFINEBITS ||
+       tag->id == ST_DEFINEBITSJPEG2 ||
+       tag->id == ST_DEFINEBITSJPEG3)) {
+       show = 1;
+    }
+    if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
+       tag->id == ST_DEFINEBITSLOSSLESS2)) {
+       show = 1;
+    }
+    if(t == 4 && (tag->id == ST_DEFINESOUND)) {
+       show = 1;
+    }
+    if(t == 5 && (tag->id == ST_DEFINEFONT || tag->id == ST_DEFINEFONT2)) {
+       show = 1;
+    }
+    return show;
+}
+
 void listObjects(SWF*swf)
 {
     TAG*tag;
     char first;
     int t;
     int frame = 0;
-    char*names[] = {"Shapes","MovieClips","JPEGs","PNGs","Sounds","Fonts"};
+    char*names[] = {"Shape", "MovieClip", "JPEG", "PNG", "Sound", "Font"};
     printf("Objects in file %s:\n",filename);
+    swf_FoldAll(swf);
     for(t=0;t<sizeof(names)/sizeof(names[0]);t++) {
+       int nr=0;
+       int lastid = -2, lastprint=-1;
+       int follow=0;
        tag = swf->firstTag;
        first = 1;
        while(tag) {
-           char show = 0;
+           if(isOfType(t,tag))
+               nr++;
+           tag = tag->next;
+       }
+       if(!nr)
+           continue;
+       
+       printf(" %d %s%s: ID(s) ", nr, names[t], nr>1?"s":"");
+
+       tag = swf->firstTag;
+       while(tag) {
            char text[80];
-           if(t == 0 &&
-              (tag->id == ST_DEFINESHAPE ||
-               tag->id == ST_DEFINESHAPE2 ||
-               tag->id == ST_DEFINESHAPE3)) {
-               show = 1;
-               sprintf(text,"%d", swf_GetDefineID(tag));
+           char show = isOfType(t,tag);
+           int id;
+           if(!show) {
+               tag = tag->next;
+               continue;
            }
+           id = swf_GetDefineID(tag);
 
-           if(tag->id == ST_DEFINESPRITE) {
-               if (t == 1)  {
-                   show = 1;
-                   sprintf(text,"%d", swf_GetDefineID(tag));
+           if(id == lastid+1) {
+               follow=1;
+           } else {
+               if(first || !follow) {
+                   if(!first)
+                       printf(", ");
+                   printf("%d", id);
+               } else {
+                   if(lastprint + 1 == lastid) 
+                       printf(", %d, %d", lastid, id);
+                   else
+                       printf("-%d, %d", lastid, id);
                }
-
-               while(tag->id != ST_END)
-                   tag = tag->next;
-           }
-
-           if(t == 2 && (tag->id == ST_DEFINEBITS ||
-               tag->id == ST_DEFINEBITSJPEG2 ||
-               tag->id == ST_DEFINEBITSJPEG3)) {
-               show = 1;
-               sprintf(text,"%d", swf_GetDefineID(tag));
-           }
-
-           if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
-               tag->id == ST_DEFINEBITSLOSSLESS2)) {
-               show = 1;
-               sprintf(text,"%d", swf_GetDefineID(tag));
-           }
-
-
-           if(t == 4 && (tag->id == ST_DEFINESOUND)) {
-               show = 1;
-               sprintf(text,"%d", swf_GetDefineID(tag));
-           }
-           
-           if(t == 5 && (tag->id == ST_DEFINEFONT || tag->id == ST_DEFINEFONT2)) {
-               show = 1;
-               sprintf(text,"%d", swf_GetDefineID(tag));
-           }
-           
-           if(show) {
-               if(!first)
-                   printf(", ");
-               else
-                   printf("%s: ", names[t]);
-               printf("%s", text);
+               lastprint = id;
                first = 0;
+               follow = 0;
            }
+           lastid = id;
            tag=tag->next;
        }
-       if(!first)
-           printf("\n");
+       if(follow) {
+           if(lastprint + 1 == lastid)
+               printf(", %d", lastid);
+           else
+               printf("-%d", lastid);
+       }
+       printf("\n");
     }
 
     if(frame)
-       printf("Frames: 0-%d\n", frame);
+       printf(" %d Frames: ID(s) 0-%d\n", frame, frame);
     else
-       printf("Frames: 0\n");
+       printf(" 1 Frame: ID(s) 0\n");
 }
 
 void handlefont(SWF*swf, TAG*tag)
@@ -579,7 +617,7 @@ static void make_crc32_table(void)
     crc32_table[t] = c;
   }
 }
-static void png_write_byte(FILE*fi, U8 byte)
+static inline void png_write_byte(FILE*fi, U8 byte)
 {
     fwrite(&byte,1,1,fi);
     mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
@@ -676,11 +714,11 @@ void handlelossless(TAG*tag)
 //    if(format == 5) cols = swf_GetU32(tag) + 1;
     else cols = 0;
 
-    logf("<verbose> Width %d", width);
-    logf("<verbose> Height %d", height);
-    logf("<verbose> Format %d", format);
-    logf("<verbose> Cols %d", cols);
-    logf("<verbose> Bpp %d", bpp);
+    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 {
@@ -694,7 +732,7 @@ void handlelossless(TAG*tag)
        fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
        return;
     }
-    logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
+    msg("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
     pos = 0;
     datalen2 = datalen;
     data2 = malloc(datalen2);
@@ -724,8 +762,10 @@ void handlelossless(TAG*tag)
      png_write_byte(fi,8);
      if(format == 3)
      png_write_byte(fi,3); //indexed
-     else if(format == 5)
+     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
@@ -735,6 +775,7 @@ void handlelossless(TAG*tag)
    
     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);
@@ -752,12 +793,22 @@ void handlelossless(TAG*tag)
        {
           data3[pos2++]=0; //filter type
           if(bpp==32) {
-           // 32 bit to 24 bit "conversion"
-           for(x=0;x<width;x++) {
-               data3[pos2++]=data[pos+1];
-               data3[pos2++]=data[pos+2];
-               data3[pos2++]=data[pos+3];
-               pos+=4; //ignore padding byte
+           if(!alpha) {
+               // 32 bit to 24 bit "conversion"
+               for(x=0;x<width;x++) {
+                   data3[pos2++]=data[pos+1];
+                   data3[pos2++]=data[pos+2];
+                   data3[pos2++]=data[pos+3];
+                   pos+=4; //ignore padding byte
+               }
+           } else {
+               for(x=0;x<width;x++) {
+                   data3[pos2++]=data[pos+1];
+                   data3[pos2++]=data[pos+2];
+                   data3[pos2++]=data[pos+3];
+                   data3[pos2++]=data[pos+0]; //alpha
+                   pos+=4;
+               }
            }
           }
           else {
@@ -774,7 +825,7 @@ void handlelossless(TAG*tag)
        fprintf(stderr, "zlib error in pic %d\n", id);
        return;
     }
-    logf("<verbose> Compressed data is %d bytes", datalen2);
+    msg("<verbose> Compressed data is %d bytes", datalen2);
     png_start_chunk(fi, "IDAT", datalen2);
     png_write_bytes(fi,data2,datalen2);
     png_end_chunk(fi);
@@ -800,18 +851,18 @@ void handlesoundstream(TAG*tag)
        case ST_SOUNDSTREAMHEAD:
            if((tag->data[1]&0x30) == 0x20) { //mp3 compression
                mp3file = fopen(filename, "wb");
-               logf("<notice> Writing mp3 data to %s",filename);
+               msg("<notice> Writing mp3 data to %s",filename);
            }
            else
-               logf("<error> Soundstream is not mp3");
+               msg("<error> Soundstream is not mp3");
        break;
        case ST_SOUNDSTREAMHEAD2:
            if((tag->data[1]&0x30) == 0x20) {//mp3 compression
                mp3file = fopen(filename, "wb");
-               logf("<notice> Writing mp3 data to %s",filename);
+               msg("<notice> Writing mp3 data to %s",filename);
            }
            else
-               logf("<error> Soundstream is not mp3 (2)");
+               msg("<error> Soundstream is not mp3 (2)");
        break;
        case ST_SOUNDSTREAMBLOCK:
            if(mp3file)
@@ -827,25 +878,42 @@ void handledefinesound(TAG*tag)
     char buf[128];
     char*filename = buf;
     FILE*fi;
+    char*extension = 0;
+    int format;
     U16 id;
+    int rate,bits,stereo;
+    char*rates[] = {"5500","11025","22050","44100"};
     id = swf_GetU16(tag); //id
-    sprintf(buf, "sound%d.mp3", id);
+    
+    flags = swf_GetU8(tag);
+    format = flags>>4;
+    rate = (flags>>2)&3;
+    bits = flags&2?16:8;
+    stereo = flags&1;
+    
+    samples = swf_GetU32(tag);
 
+    extension = "raw";
+
+    if(format == 2) { // mp3
+       swf_GetU16(tag); //numsamples_seek
+       extension = "mp3";
+    } else if(format == 0) { // raw
+       printf("Sound is RAW, format: %s samples/sec, %d bit, %s\n", rates[rate], bits, stereo?"stereo":"mono");
+       // TODO: convert to WAV
+       extension = "raw";
+    } else if(format == 1) { // adpcm
+       printf("Sound is ADPCM, format: %s samples/sec, %d bit, %s\n", rates[rate], bits, stereo?"stereo":"mono");
+       extension = "adpcm";
+    }
+    sprintf(buf, "sound%d.%s", id, extension);
     if(numextracts==1) {
        filename = destfilename;
-       if(!strcmp(filename,"output.swf"))
-           filename = "output.mp3";
-    }
-    flags = swf_GetU8(tag);
-    if((flags>>4)!=2) {
-       printf("Sorry, can only extract MP3 sounds. Sound %d is ADPCM or RAW.\n", id);
-       /* not mp3 */
-       return;
+       if(!strcmp(filename,"output.swf")) {
+           sprintf(buf, "output.%s", extension);
+           filename = buf;
+       }
     }
-    samples = swf_GetU32(tag);
-    
-    swf_GetU16(tag); //(only for mp3) numsamples_seek
-
     fi = save_fopen(filename, "wb");
     fwrite(&tag->data[tag->pos], tag->len - tag->pos, 1, fi);
     fclose(fi);
@@ -874,7 +942,7 @@ int main (int argc,char ** argv)
     }
     initLog(0,-1,0,0,-1, verbose);
 
-    f = open(filename,O_RDONLY);
+    f = open(filename,O_RDONLY|O_BINARY);
 
     if (f<0)
     {