added -c and -m options
[swftools.git] / src / png2swf.c
index e01e0b9..541675d 100644 (file)
@@ -47,6 +47,12 @@ struct {
     char *filename;
 } image[MAX_INPUT_FILES];
 
+static int custom_move=0;
+static int move_x=0;
+static int move_y=0;
+static int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
+static int custom_clip = 0;
+
 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
 {
     TAG *t;
@@ -56,8 +62,15 @@ TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
 
     swf->fileVersion = global.version;
     swf->frameRate = (int)(256.0 * framerate);
-    swf->movieSize.xmax = dx * 20;
-    swf->movieSize.ymax = dy * 20;
+    if(custom_clip) {
+       swf->movieSize.xmin = clip_x1 * 20;
+       swf->movieSize.ymin = clip_y1 * 20;
+       swf->movieSize.xmax = clip_x2 * 20;
+       swf->movieSize.ymax = clip_y2 * 20;
+    } else {
+       swf->movieSize.xmax = dx * 20;
+       swf->movieSize.ymax = dy * 20;
+    }
 
     t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
 
@@ -85,10 +98,10 @@ int MovieFinish(SWF * swf, TAG * t, char *sname)
        if FAILED(swf_WriteCGI(swf)) fprintf(stderr,"WriteCGI() failed.\n");
     } else {
        if(global.version >= 6) {
-           if (swf_WriteSWC(handle, swf)<0) 
+           if (swf_WriteSWC(f, swf)<0) 
                    fprintf(stderr, "Unable to write output file: %s\n", sname);
        } else {
-           if (swf_WriteSWF(handle, swf)<0) 
+           if (swf_WriteSWF(f, swf)<0) 
                    fprintf(stderr, "Unable to write output file: %s\n", sname);
        }
        if (f != so)
@@ -315,11 +328,11 @@ void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
     unsigned char lastr=0;
     unsigned char lastg=0;
     unsigned char lastb=0;
-    unsigned char lasta=0;
+    unsigned char lasta=0; //TODO: 255?
     unsigned char upperlastr=0;
     unsigned char upperlastg=0;
     unsigned char upperlastb=0;
-    unsigned char upperlasta=0;
+    unsigned char upperlasta=0; //TODO: 255?
 
     if(mode==0) {
        for(x=0;x<width;x++) {
@@ -570,7 +583,7 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
 
            if(!y) {
                old = firstline;
-               memset(old, 0, header.width*4);
+               memset(old, 0, header.width*4); //TODO: fill alpha with 255?
            } else {
                old = &data2[(y-1)*header.width*4];
            }
@@ -635,12 +648,13 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
                }
            }
        } else {
-           palettelen = 256;
+            int mult = (0x1ff>>header.bpp);
+           palettelen = 1<<header.bpp;
            rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
-           for(i=0;i<256;i++) {
-               rgba[i].r = i;
-               rgba[i].g = i;
-               rgba[i].b = i;
+           for(i=0;i<palettelen;i++) {
+               rgba[i].r = i*mult;
+               rgba[i].g = i*mult;
+               rgba[i].b = i*mult;
            }
        }
 
@@ -687,7 +701,9 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
     swf_GetMatrix(NULL, &m);
     m.sx = 20 * 0x10000;
     m.sy = 20 * 0x10000;
-    fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
+    m.tx = -10;
+    m.ty = -10;
+    fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 1);
 
     swf_SetU16(t, id + 1);     // id
 
@@ -712,8 +728,13 @@ TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
     t = swf_InsertTag(t, ST_PLACEOBJECT2);
 
     swf_GetMatrix(NULL, &m);
-    m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
-    m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
+    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;
+    }
     swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
 
     t = swf_InsertTag(t, ST_SHOWFRAME);
@@ -743,9 +764,10 @@ int CheckInputFile(char *fname, char **realname)
            sprintf(s, "%s.PNG", fname);
            if ((fi = fopen(s, "rb")) == NULL) {
                sprintf(s, "%s.Png", fname);
-               if ((fi = fopen(s, "rb")) == NULL)
+               if ((fi = fopen(s, "rb")) == NULL) {
                    fprintf(stderr, "Couldn't open %s!\n", fname);
                    return -1;
+               }
            }
        }
     }
@@ -775,7 +797,9 @@ int args_callback_option(char *arg, char *val)
        case 'r':
            if (val)
                global.framerate = atof(val);
-           if ((global.framerate < 1.0/256) ||(global.framerate >= 256.0)) {
+           /* removed framerate>0 restriction in order to make
+              Flash Communication Server compatible SWFs */
+           if ((global.framerate < 0) ||(global.framerate >= 256.0)) {
                if (VERBOSE(1))
                    fprintf(stderr,
                            "Error: You must specify a valid framerate between 1/256 and 255.\n");
@@ -791,7 +815,12 @@ int args_callback_option(char *arg, char *val)
            break;
 
        case 'z':
-           global.version = 1;
+           global.version = 6;
+           res = 0;
+           break;
+
+       case 'T':
+           global.version = atoi(val);
            res = 1;
            break;
 
@@ -800,9 +829,15 @@ int args_callback_option(char *arg, char *val)
            break;
 
        case 'v':
-           if (val)
-               global.verbose = atoi(val);
-           res = 1;
+           global.verbose++;
+           res = 0;
+           break;
+
+       case 'q':
+           global.verbose--;
+           if(global.verbose<0)
+               global.verbose = 0;
+           res = 0;
            break;
 
        case 'X':
@@ -820,6 +855,44 @@ int args_callback_option(char *arg, char *val)
        case 'V':
            printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
            exit(0);
+   
+       case 'c': {
+           char*s = strdup(val);
+           char*x1 = strtok(s, ":");
+           char*y1 = strtok(0, ":");
+           char*x2 = strtok(0, ":");
+           char*y2 = strtok(0, ":");
+           if(!(x1 && y1 && x2 && y2)) {
+               fprintf(stderr, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
+               exit(1);
+           }
+           custom_clip = 1;
+           clip_x1 = atoi(x1);
+           clip_y1 = atoi(y1);
+           clip_x2 = atoi(x2);
+           clip_y2 = atoi(y2);
+           free(s);
+
+           res = 1;
+           break;
+       }
+
+       case 'm': {
+           char*s = strdup(val);
+           char*c = strchr(s, ':');
+           if(!c) {
+               fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
+               exit(1);
+           }
+           *c = 0;
+           custom_move = 1;
+           move_x = atoi(val);
+           move_y = atoi(c+1);
+           free(s);
+
+           res = 1;
+           break;
+       }
 
        default:
            res = -1;
@@ -842,6 +915,7 @@ static struct options_t options[] = {
 {"X", "pixel"},
 {"Y", "pixel"},
 {"v", "verbose"},
+{"q", "quiet"},
 {"C", "cgi"},
 {"V", "version"},
 {0,0}
@@ -881,7 +955,8 @@ void args_callback_usage(char *name)
     printf("-z , --zlib <zlib>             Enable Flash 6 (MX) Zlib Compression\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 <level>         Set verbose level (0=quiet, 1=default, 2=debug)\n");
+    printf("-v , --verbose                 Be verbose. Use more than one -v for greater effect \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("\n");
@@ -899,9 +974,11 @@ int main(int argc, char **argv)
     global.version = 4;
 
     processargs(argc, argv);
-
-    if(global.nfiles<=0)
+    
+    if(global.nfiles<=0) {
+       fprintf(stderr, "No png files found in arguments\n");
        return 1;
+    }
 
     if (VERBOSE(2))
        fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);