updated documentation
[swftools.git] / src / png2swf.c
index 541675d..e8a2c7c 100644 (file)
@@ -41,6 +41,7 @@ struct {
     int do_cgi;
     int version;
     char *outfile;
+    float scale;
 } global;
 
 struct {
@@ -477,6 +478,8 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
     int alphapalettelen = 0;
     struct png_header header;
     int bypp;
+    U8 alphacolor[3];
+    int hasalphacolor=0;
 
     FILE *fi;
     U8 *scanline;
@@ -510,7 +513,7 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
        }
        if(!strncmp(tagid, "PLTE", 4)) {
            palette = data;
-           palettelen = len/3;
+           palettelen = len/bypp;
            data = 0; //don't free data
            if(VERBOSE(2))
                printf("%d colors in palette\n", palettelen);
@@ -522,6 +525,21 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
                data = 0; //don't free data
                if(VERBOSE(2))
                    printf("found %d alpha colors\n", alphapalettelen);
+           } else if(header.mode == 0) {
+               int t;
+               if(header.mode == 3) { // palette or grayscale?
+                   alphacolor[0] = data[1];
+                   alphacolor[1] = data[3];
+                   alphacolor[2] = data[5];
+               } else {
+                   alphacolor[0] = alphacolor[1] = alphacolor[2] = data[1];
+               }
+               if(VERBOSE(2))
+                   printf("found alpha color: %02x%02x%02x\n", alphacolor[0], alphacolor[1], alphacolor[2]);
+               hasalphacolor = 1;
+           } else {
+               if(VERBOSE(2))
+                   printf("ignoring tRNS %d entry (%d bytes)\n", header.mode, len);
            }
        }
        if(!strncmp(tagid, "IDAT", 4)) {
@@ -547,7 +565,7 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
     }
     free(zimagedata);
 
-    if(alphapalette)
+    if(alphapalette || hasalphacolor)
        t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
     else
        t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
@@ -646,6 +664,16 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
                } else {
                    rgba[i].a = 255;
                }
+               if(hasalphacolor) {
+                   if(rgba[i].r == alphacolor[0] &&
+                      rgba[i].g == alphacolor[1] &&
+                      rgba[i].b == alphacolor[2]) {
+                       rgba[i].r = 0;
+                       rgba[i].g = 0;
+                       rgba[i].b = 0;
+                       rgba[i].a = 0;
+                   }
+               }
            }
        } else {
             int mult = (0x1ff>>header.bpp);
@@ -655,6 +683,15 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
                rgba[i].r = i*mult;
                rgba[i].g = i*mult;
                rgba[i].b = i*mult;
+               rgba[i].a = 255;
+               if(hasalphacolor) {
+                   if(rgba[i].r == alphacolor[0]) {
+                       rgba[i].r = 0;
+                       rgba[i].g = 0;
+                       rgba[i].b = 0;
+                       rgba[i].a = 0;
+                   }
+               }
            }
        }
 
@@ -699,8 +736,8 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
 
     swf_ShapeNew(&s);
     swf_GetMatrix(NULL, &m);
-    m.sx = 20 * 0x10000;
-    m.sy = 20 * 0x10000;
+    m.sx = (int)(20 * 0x10000);
+    m.sy = (int)(20 * 0x10000);
     m.tx = -10;
     m.ty = -10;
     fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 1);
@@ -728,12 +765,15 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
     t = swf_InsertTag(t, ST_PLACEOBJECT2);
 
     swf_GetMatrix(NULL, &m);
+    m.sx = (int)(0x10000 * global.scale);
+    m.sy = (int)(0x10000 * global.scale);
+
     if(custom_move) {
        m.tx = move_x*20;
        m.ty = move_y*20;
     } else {
-       m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
-       m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
+       m.tx = (swf->movieSize.xmax - (int) (header.width * global.scale * 20)) / 2;
+       m.ty = (swf->movieSize.ymax - (int) (header.height * global.scale * 20)) / 2;
     }
     swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
 
@@ -814,8 +854,14 @@ int args_callback_option(char *arg, char *val)
            res = 1;
            break;
 
+       case 's':
+           global.scale = atof(val)/100;
+           res = 1;
+           break;
+
        case 'z':
-           global.version = 6;
+           if(global.version<6)
+               global.version = 6;
            res = 0;
            break;
 
@@ -851,7 +897,7 @@ int args_callback_option(char *arg, char *val)
                global.force_height = atoi(val);
            res = 1;
            break;
-
+       
        case 'V':
            printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
            exit(0);
@@ -912,12 +958,14 @@ static struct options_t options[] = {
 {"r", "rate"},
 {"o", "output"},
 {"z", "zlib"},
+{"T", "flashversion"},
 {"X", "pixel"},
 {"Y", "pixel"},
 {"v", "verbose"},
 {"q", "quiet"},
 {"C", "cgi"},
 {"V", "version"},
+{"s", "scale"},
 {0,0}
 };
 
@@ -953,12 +1001,14 @@ void args_callback_usage(char *name)
     printf("-r , --rate <framerate>        Set movie framerate (frames per second)\n");
     printf("-o , --output <filename>       Set name for SWF output file.\n");
     printf("-z , --zlib <zlib>             Enable Flash 6 (MX) Zlib Compression\n");
+    printf("-T , --flashversion            Set the flash version to generate\n");
     printf("-X , --pixel <width>           Force movie width to <width> (default: autodetect)\n");
     printf("-Y , --pixel <height>          Force movie height to <height> (default: autodetect)\n");
-    printf("-v , --verbose                 Be verbose. Use more than one -v for greater effect \n");
+    printf("-v , --verbose <level>         Set verbose level (0=quiet, 1=default, 2=debug)\n");
     printf("-q , --quiet                   Omit normal log messages, only log errors\n");
     printf("-C , --cgi                     For use as CGI- prepend http header, write to stdout\n");
     printf("-V , --version                 Print version information and exit\n");
+    printf("-s , --scale <percent>         Scale image to <percent>% size.\n");
     printf("\n");
 }
 
@@ -972,6 +1022,7 @@ int main(int argc, char **argv)
     global.framerate = 1.0;
     global.verbose = 1;
     global.version = 4;
+    global.scale = 1.0;
 
     processargs(argc, argv);
     
@@ -984,10 +1035,8 @@ int main(int argc, char **argv)
        fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
 
     t = MovieStart(&swf, global.framerate,
-                  global.force_width ? global.force_width : global.
-                  max_image_width,
-                  global.force_height ? global.force_height : global.
-                  max_image_height);
+                  global.force_width ? global.force_width : (int)(global.max_image_width*global.scale),
+                  global.force_height ? global.force_height : (int)(global.max_image_height*global.scale));
 
     {
        int i;