moved wav.h to ../lib
[swftools.git] / src / swfc.c
index 49b3998..131c794 100644 (file)
 #include "../lib/log.h"
 #include "../lib/args.h"
 #include "../lib/q.h"
+#include "../lib/mp3.h"
+#include "../lib/wav.h"
 #include "parser.h"
-#include "wav.h"
+#include "../lib/png.h"
 
 //#define DEBUG
 
@@ -727,11 +729,13 @@ void s_frame(int nr, int cut, char*name)
        if(t==nr-1 && name && *name) {
            tag = swf_InsertTag(tag, ST_FRAMELABEL);
            swf_SetString(tag, name);
+           swf_SetU8(tag, 1); //make this an anchor
        }
     }
     if(nr == 0 && currentframe == 0 && name) {
         tag = swf_InsertTag(tag, ST_FRAMELABEL);
         swf_SetString(tag, name);
+       swf_SetU8(tag, 1); //make this an anchor
     }
 
     if(cut) {
@@ -1020,12 +1024,7 @@ void s_image(char*name, char*type, char*filename, int quality)
     SRECT r;
     int imageID = id;
     int width, height;
-    if(type=="png") {
-       warning("image type \"png\" not supported yet!");
-       s_box(name, 0, 0, black, 20, 0);
-       return;
-    }
-    if(type=="jpeg") {
+    if(!strcmp(type,"jpeg")) {
 #ifndef HAVE_LIBJPEG
        warning("no jpeg support compiled in");
        s_box(name, 0, 0, black, 20, 0);
@@ -1048,6 +1047,31 @@ void s_image(char*name, char*type, char*filename, int quality)
        s_addimage(name, id, tag, r);
        incrementid();
 #endif
+    } else if(!strcmp(type,"png")) {
+       RGBA*data = 0;
+       swf_SetU16(tag, imageID);
+
+       getPNG(filename, &width, &height, (unsigned char**)&data);
+
+       if(!data) {
+           syntaxerror("Image \"%s\" not found, or contains errors", filename);
+       }
+
+       /*tag = swf_AddImage(tag, imageID, data, width, height, quality)*/
+       tag = swf_InsertTag(tag, ST_DEFINEBITSLOSSLESS);
+       swf_SetU16(tag, imageID);
+       swf_SetLosslessImage(tag, data, width, height);
+
+       r.xmin = 0;
+       r.ymin = 0;
+       r.xmax = width*20;
+       r.ymax = height*20;
+       s_addimage(name, id, tag, r);
+       incrementid();
+    } else {
+       warning("image type \"%s\" not supported yet!", type);
+       s_box(name, 0, 0, black, 20, 0);
+       return;
     }
 
     /* step 2: the character */
@@ -1156,6 +1180,10 @@ void s_font(char*name, char*filename)
     font->id = id;
     tag = swf_InsertTag(tag, ST_DEFINEFONT2);
     swf_FontSetDefine2(tag, font);
+    tag = swf_InsertTag(tag, ST_EXPORTASSETS);
+    swf_SetU16(tag, 1);
+    swf_SetU16(tag, id);
+    swf_SetString(tag, name);
     incrementid();
 
     if(dictionary_lookup(&fonts, name))
@@ -1174,18 +1202,16 @@ typedef struct _sound_t
 void s_sound(char*name, char*filename)
 {
     struct WAV wav, wav2;
+    struct MP3 mp3;
     sound_t* sound;
-    U16*samples;
-    int numsamples;
-    int t;
-    int blocksize = 576;
-
-    if(!readWAV(filename, &wav)) {
-       warning("Couldn't read wav file \"%s\"", filename);
-       samples = 0;
-       numsamples = 0;
-    } else {
-       convertWAV2mono(&wav, &wav2, 44100);
+    U16*samples = NULL;
+    unsigned numsamples;
+    unsigned blocksize = 1152;
+    int is_mp3 = 0;
+
+    if(wav_read(filename, &wav)) {
+        int t;
+       wav_convert2mono(&wav, &wav2, 44100);
        samples = (U16*)wav2.data;
        numsamples = wav2.size/2;
        free(wav.data);
@@ -1195,6 +1221,16 @@ void s_sound(char*name, char*filename)
            samples[t] = (samples[t]>>8)&0xff | (samples[t]<<8)&0xff00;
        }
 #endif
+    } else if(mp3_read(&mp3, filename)) {
+        fprintf(stderr, "\"%s\" seems to work as a MP3 file...\n", filename);
+        blocksize = 1;
+        is_mp3 = 1;
+    }
+    else
+    {
+       warning("Couldn't read WAV/MP3 file \"%s\"", filename);
+       samples = 0;
+       numsamples = 0;
     }
     
     if(numsamples%blocksize != 0)
@@ -1213,7 +1249,19 @@ void s_sound(char*name, char*filename)
 
     tag = swf_InsertTag(tag, ST_DEFINESOUND);
     swf_SetU16(tag, id); //id
-    swf_SetSoundDefine(tag, samples, numsamples);
+    if(is_mp3)
+    {
+        swf_SetSoundDefineMP3(
+                tag, mp3.data, mp3.size,
+                mp3.SampRate,
+                mp3.Channels,
+                mp3.NumFrames);
+       mp3_clear(&mp3);
+    }
+    else
+    {
+        swf_SetSoundDefine(tag, samples, numsamples);
+    }
    
     sound = (sound_t*)malloc(sizeof(sound_t)); /* mem leak */
     sound->tag = tag;