changed -v syntax- no longer expect an argument
[swftools.git] / src / png2swf.c
1 /* png2swf.c
2
3    PNG to SWF converter tool
4
5    Part of the swftools package.
6
7    Copyright (c) 2002,2003 Matthias Kramm <kramm@quiss.org>
8  
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
22  
23 #include <stdio.h>
24 #include <math.h>
25 #include <fcntl.h>
26 #include <zlib.h>
27 #include "../lib/rfxswf.h"
28 #include "../lib/args.h"
29
30 #define MAX_INPUT_FILES 1024
31 #define VERBOSE(x) (global.verbose>=x)
32
33 struct {
34     float framerate;
35     int max_image_width;
36     int max_image_height;
37     int force_width;
38     int force_height;
39     int nfiles;
40     int verbose;
41     int do_cgi;
42     int version;
43     char *outfile;
44 } global;
45
46 struct {
47     char *filename;
48 } image[MAX_INPUT_FILES];
49
50 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
51 {
52     TAG *t;
53     RGBA rgb;
54
55     memset(swf, 0x00, sizeof(SWF));
56
57     swf->fileVersion = global.version;
58     swf->frameRate = (int)(256.0 * framerate);
59     swf->movieSize.xmax = dx * 20;
60     swf->movieSize.ymax = dy * 20;
61
62     t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
63
64     rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
65     //rgb.g = 0xff; //<--- handy for testing alpha conversion
66     swf_SetRGB(t, &rgb);
67
68     return t;
69 }
70
71 int MovieFinish(SWF * swf, TAG * t, char *sname)
72 {
73     int f, so = fileno(stdout);
74     t = swf_InsertTag(t, ST_END);
75
76     if ((!isatty(so)) && (!sname))
77         f = so;
78     else {
79         if (!sname)
80             sname = "output.swf";
81         f = open(sname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
82     }
83
84     if(global.do_cgi) {
85         if FAILED(swf_WriteCGI(swf)) fprintf(stderr,"WriteCGI() failed.\n");
86     } else {
87         if(global.version >= 6) {
88             if (swf_WriteSWC(f, swf)<0) 
89                     fprintf(stderr, "Unable to write output file: %s\n", sname);
90         } else {
91             if (swf_WriteSWF(f, swf)<0) 
92                     fprintf(stderr, "Unable to write output file: %s\n", sname);
93         }
94         if (f != so)
95             close(f);
96     }
97
98     swf_FreeTags(swf);
99     return 0;
100 }
101
102 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
103 {
104     unsigned int len;
105     if(destlen) *destlen=0;
106     if(destdata) *destdata=0;
107     if(!fread(&len, 4, 1, fi))
108         return 0;
109     if(!fread(head, 4, 1, fi))
110         return 0;
111     len = REVERSESWAP32(len);
112     if(destlen) *destlen = len;
113     if(destdata) {
114         if(len)
115             *destdata = malloc(len);
116         else 
117             *destdata = 0;
118         if(!fread(*destdata, len, 1, fi)) {
119             *destdata = 0;
120             if(destlen) *destlen=0;
121             return 0;
122         }
123         fseek(fi, 4, SEEK_CUR);
124
125     } else {
126         fseek(fi, len+4, SEEK_CUR);
127     }
128     return 1;
129 }
130
131 unsigned int png_get_dword(FILE*fi)
132 {
133     unsigned int a;
134     fread(&a,4,1,fi);
135     return REVERSESWAP32(a);
136 }
137
138 struct png_header
139 {
140     int width;
141     int height;
142     int bpp;
143     int mode;
144 };
145
146 int png_read_header(FILE*fi, struct png_header*header)
147 {
148     char id[4];
149     int len;
150     int ok=0;
151     U8 head[8] = {137,80,78,71,13,10,26,10};
152     U8 head2[8];
153     U8*data;
154     fread(head2,8,1,fi);
155     if(strncmp(head,head2,4))
156         return 0;
157    
158     while(png_read_chunk(&id, &len, &data, fi))
159     {
160         if(VERBOSE(2))
161             printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
162         if(!strncasecmp(id, "IHDR", 4)) {
163             char a,b,c,f,i;
164             if(len < 8) exit(1);
165             header->width = REVERSESWAP32(*(U32*)&data[0]);
166             header->height = REVERSESWAP32(*(U32*)&data[4]);
167             a = data[8];      // should be 8
168             b = data[9];      // should be 3(indexed) or 2(rgb)
169
170             c = data[10];     // compression mode (0)
171             f = data[11];     // filter mode (0)
172             i = data[12];     // interlace mode (0)
173         
174             if(VERBOSE(2)) printf("image mode:%d\n", b);
175             if(VERBOSE(2)) printf("bpp: %d\n", a);
176             if(VERBOSE(2)) printf("compression: %d\n", c);
177             if(VERBOSE(2)) printf("filter: %d\n", f);
178             if(VERBOSE(2)) printf("interlace: %d\n", i);
179
180             if(b!=0 && b!=2 && b!=3 && b!=6) {
181                 fprintf(stderr, "Image mode %d not supported!\n", b);
182                 if(b == 4) {
183                     fprintf(stderr, "(This is a grayscale image with alpha channel-\n");
184                     fprintf(stderr, " try converting it into an RGB image with alpha channel)\n");
185                 }
186                 exit(1);
187             }
188             if(a!=8 && (b==2 || b==6)) {
189                 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
190                 exit(1);
191             }
192             if(c!=0) {
193                 fprintf(stderr, "Compression mode %d not supported!\n", c);
194                 exit(1);
195             }
196             if(f!=0) {
197                 fprintf(stderr, "Filter mode %d not supported!\n", f);
198                 exit(1);
199             }
200             if(i!=0) {
201                 fprintf(stderr, "Interlace mode %d not supported!\n", i);
202                 exit(1);
203             }
204             if(VERBOSE(2))
205                 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
206             header->bpp = a;
207             header->mode = b;
208             ok = 1;
209         } 
210         
211         free(data);
212     }
213     return ok;
214 }
215
216 typedef unsigned char byte;
217 #define ABS(a) ((a)>0?(a):(-(a)))
218 byte inline PaethPredictor (byte a,byte b,byte c)
219 {
220         // a = left, b = above, c = upper left
221         int p = a + b - c;        // initial estimate
222         int pa = ABS(p - a);      // distances to a, b, c
223         int pb = ABS(p - b);
224         int pc = ABS(p - c);
225         // return nearest of a,b,c,
226         // breaking ties in order a,b,c.
227         if (pa <= pb && pa <= pc) 
228                 return a;
229         else if (pb <= pc) 
230                 return b;
231         else return c;
232 }
233
234 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
235 {
236     int x;
237     unsigned char lastr=0;
238     unsigned char lastg=0;
239     unsigned char lastb=0;
240     unsigned char upperlastr=0;
241     unsigned char upperlastg=0;
242     unsigned char upperlastb=0;
243
244     if(mode==0) {
245         for(x=0;x<width;x++) {
246             dest[0] = 255;
247             dest[1] = src[0];
248             dest[2] = src[1];
249             dest[3] = src[2];
250             dest+=4;
251             src+=3;
252         }
253     }
254     else if(mode==1) {
255         for(x=0;x<width;x++) {
256             dest[0] = 255;
257             dest[1] = src[0]+lastr;
258             dest[2] = src[1]+lastg;
259             dest[3] = src[2]+lastb;
260             lastr = dest[1];
261             lastg = dest[2];
262             lastb = dest[3];
263             dest+=4;
264             src+=3;
265         }
266     }
267     else if(mode==2) {
268         for(x=0;x<width;x++) {
269             dest[0] = 255;
270             dest[1] = src[0]+old[1];
271             dest[2] = src[1]+old[2];
272             dest[3] = src[2]+old[3];
273             dest+=4;
274             old+=4;
275             src+=3;
276         }
277     }
278     else if(mode==3) {
279         for(x=0;x<width;x++) {
280             dest[0] = 255;
281             dest[1] = src[0]+(old[1]+lastr)/2;
282             dest[2] = src[1]+(old[2]+lastg)/2;
283             dest[3] = src[2]+(old[3]+lastb)/2;
284             lastr = dest[1];
285             lastg = dest[2];
286             lastb = dest[3];
287             dest+=4;
288             old+=4;
289             src+=3;
290         }
291     }
292     else if(mode==4) {
293         for(x=0;x<width;x++) {
294             dest[0] = 255;
295             dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
296             dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
297             dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
298             lastr = dest[1];
299             lastg = dest[2];
300             lastb = dest[3];
301             upperlastr = old[1];
302             upperlastg = old[2];
303             upperlastb = old[3];
304             dest+=4;
305             old+=4;
306             src+=3;
307         }
308     }    
309
310 }
311
312 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
313 {
314     int x;
315     unsigned char lastr=0;
316     unsigned char lastg=0;
317     unsigned char lastb=0;
318     unsigned char lasta=0; //TODO: 255?
319     unsigned char upperlastr=0;
320     unsigned char upperlastg=0;
321     unsigned char upperlastb=0;
322     unsigned char upperlasta=0; //TODO: 255?
323
324     if(mode==0) {
325         for(x=0;x<width;x++) {
326             dest[0] = src[3];
327             dest[1] = src[0];
328             dest[2] = src[1];
329             dest[3] = src[2];
330             dest+=4;
331             src+=4;
332         }
333     }
334     else if(mode==1) {
335         for(x=0;x<width;x++) {
336             dest[0] = src[3]+lasta;
337             dest[1] = src[0]+lastr;
338             dest[2] = src[1]+lastg;
339             dest[3] = src[2]+lastb;
340             lasta = dest[0];
341             lastr = dest[1];
342             lastg = dest[2];
343             lastb = dest[3];
344             dest+=4;
345             src+=4;
346         }
347     }
348     else if(mode==2) {
349         for(x=0;x<width;x++) {
350             dest[0] = src[3]+old[0];
351             dest[1] = src[0]+old[1];
352             dest[2] = src[1]+old[2];
353             dest[3] = src[2]+old[3];
354             dest+=4;
355             old+=4;
356             src+=4;
357         }
358     }
359     else if(mode==3) {
360         for(x=0;x<width;x++) {
361             dest[0] = src[3]+(old[0]+lasta)/2;
362             dest[1] = src[0]+(old[1]+lastr)/2;
363             dest[2] = src[1]+(old[2]+lastg)/2;
364             dest[3] = src[2]+(old[3]+lastb)/2;
365             lasta = dest[0];
366             lastr = dest[1];
367             lastg = dest[2];
368             lastb = dest[3];
369             dest+=4;
370             old+=4;
371             src+=4;
372         }
373     }
374     else if(mode==4) {
375         for(x=0;x<width;x++) {
376             dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
377             dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
378             dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
379             dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
380             lasta = dest[0];
381             lastr = dest[1];
382             lastg = dest[2];
383             lastb = dest[3];
384             upperlasta = old[0];
385             upperlastr = old[1];
386             upperlastg = old[2];
387             upperlastb = old[3];
388             dest+=4;
389             old+=4;
390             src+=4;
391         }
392     }    
393
394 }
395
396 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
397 {
398     int x;
399     unsigned char last=0;
400     unsigned char upperlast=0;
401
402     if(mode==0) {
403         for(x=0;x<width;x++) {
404             *dest = *src;
405             dest++;
406             src++;
407         }
408     }
409     else if(mode==1) {
410         for(x=0;x<width;x++) {
411             *dest = *src+last;
412             last = *dest;
413             dest++;
414             src++;
415         }
416     }
417     else if(mode==2) {
418         for(x=0;x<width;x++) {
419             *dest = *src+*old;
420             dest++;
421             old++;
422             src++;
423         }
424     }
425     else if(mode==3) {
426         for(x=0;x<width;x++) {
427             *dest = *src+(*old+last)/2;
428             last = *dest;
429             dest++;
430             old++;
431             src++;
432         }
433     }
434     else if(mode==4) {
435         for(x=0;x<width;x++) {
436             *dest = *src+PaethPredictor(last,*old,upperlast);
437             last = *dest;
438             upperlast = *old;
439             dest++;
440             old++;
441             src++;
442         }
443     }    
444
445 }
446
447 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
448 {
449     SHAPE *s;
450     SRECT r;
451     MATRIX m;
452     int fs;
453
454     char tagid[4];
455     int len;
456     U8*data;
457     U8*imagedata;
458     U8*zimagedata=0;
459     unsigned long int imagedatalen;
460     unsigned long int zimagedatalen=0;
461     U8*palette = 0;
462     int palettelen = 0;
463     U8*alphapalette = 0;
464     int alphapalettelen = 0;
465     struct png_header header;
466     int bypp;
467
468     FILE *fi;
469     U8 *scanline;
470
471     if ((fi = fopen(sname, "rb")) == NULL) {
472         if (VERBOSE(1))
473             fprintf(stderr, "Read access failed: %s\n", sname);
474         return t;
475     }
476
477     if(!png_read_header(fi, &header))
478         return 0;
479
480     if(header.mode == 3 || header.mode == 0) bypp = 1;
481     else
482     if(header.mode == 2) bypp = 3;
483     else
484     if(header.mode == 6) bypp = 4;
485     else
486         return 0;
487     imagedatalen = bypp * header.width * header.height + 65536;
488     imagedata = malloc(imagedatalen);
489
490     fseek(fi,8,SEEK_SET);
491     while(!feof(fi))
492     {
493         if(!png_read_chunk(&tagid, &len, &data, fi))
494             break;
495         if(!strncmp(tagid, "IEND", 4)) {
496             break;
497         }
498         if(!strncmp(tagid, "PLTE", 4)) {
499             palette = data;
500             palettelen = len/3;
501             data = 0; //don't free data
502             if(VERBOSE(2))
503                 printf("%d colors in palette\n", palettelen);
504         }
505         if(!strncmp(tagid, "tRNS", 4)) {
506             if(header.mode == 3) {
507                 alphapalette = data;
508                 alphapalettelen = len;
509                 data = 0; //don't free data
510                 if(VERBOSE(2))
511                     printf("found %d alpha colors\n", alphapalettelen);
512             }
513         }
514         if(!strncmp(tagid, "IDAT", 4)) {
515             if(!zimagedata) {
516                 zimagedatalen = len;
517                 zimagedata = malloc(len);
518                 memcpy(zimagedata,data,len);
519             } else {
520                 zimagedata = realloc(zimagedata, zimagedatalen+len);
521                 memcpy(&zimagedata[zimagedatalen], data, len);
522                 zimagedatalen += len;
523             }
524         }
525         if(data)
526             free(data);
527     }
528     
529     if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
530         fprintf(stderr, "Couldn't uncompress %s!\n", sname);
531         if(zimagedata)
532             free(zimagedata);
533         return 0;
534     }
535     free(zimagedata);
536
537     if(alphapalette)
538         t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
539     else
540         t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
541
542     swf_SetU16(t, id);          // id
543     if(header.mode == 2 || header.mode == 6) {
544         U8*data2 = malloc(header.width*header.height*4);
545         int i,s=0;
546         int x,y;
547         int pos=0;
548         int opaque=0;
549         int transparent=0;
550         int semitransparent=0;
551         /* in case for mode 2, the following also performs 24->32 bit conversion */
552         unsigned char* firstline = malloc(header.width*4);
553
554         for(y=0;y<header.height;y++) {
555             int mode = imagedata[pos++]; //filter mode
556             U8*src;
557             U8*dest;
558             U8*old;
559             dest = &data2[(y*header.width)*4];
560
561             if(header.bpp == 8)
562             {
563                 /* one byte per pixel */
564                 src = &imagedata[pos];
565                 pos+=header.width*(header.mode==6?4:3);
566             } else {
567                 /* not implemented yet */
568                 exit(1);
569             }
570
571             if(!y) {
572                 old = firstline;
573                 memset(old, 0, header.width*4); //TODO: fill alpha with 255?
574             } else {
575                 old = &data2[(y-1)*header.width*4];
576             }
577             if(header.mode==6)
578                 applyfilter4(mode, src, old, dest, header.width);
579             else if(header.mode==2)
580                 applyfilter3(mode, src, old, dest, header.width);
581         }
582         free(firstline);
583
584         /* the image is now compressed and stored in data. Now let's take
585            a look at the alpha values */
586         if(header.mode == 6) {
587             for(y=0;y<header.height;y++) {
588                 U8*l = &data2[(y*header.width)*4];
589                 for(x=0;x<header.width;x++) {
590                     U8 a = l[x*4];
591                     U8 b = l[x*4+1];
592                     U8 g = l[x*4+2];
593                     U8 r = l[x*4+3];
594                     if(a==255) transparent++;
595                     else {
596                         if(a==0) opaque++;
597                         else semitransparent++;
598                         l[x*4+3]=(int)r*a/255;
599                         l[x*4+2]=(int)g*a/255;
600                         l[x*4+1]=(int)b*a/255;
601                     }
602                 }
603             }
604             if(semitransparent || opaque) {
605                 t->id = ST_DEFINEBITSLOSSLESS2;
606             }
607         }
608         swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
609         free(data2);
610     } else if(header.mode == 0 || header.mode == 3) {
611         RGBA*rgba;
612         int swf_width = BYTES_PER_SCANLINE(header.width);
613         U8*data2 = malloc(swf_width*header.height);
614         U8*tmpline = malloc(header.width);
615         int i,x,y;
616         int pos=0;
617         if(header.mode == 3) { // palette or grayscale?
618             rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
619             if(!palette) {
620                 fprintf(stderr, "Error: No palette found!\n");
621                 exit(1);
622             }
623             /* 24->32 bit conversion */
624             for(i=0;i<palettelen;i++) {
625                 rgba[i].r = palette[i*3+0];
626                 rgba[i].g = palette[i*3+1];
627                 rgba[i].b = palette[i*3+2];
628                 if(alphapalette && i<alphapalettelen) {
629                     rgba[i].a = alphapalette[i];
630                     rgba[i].r = ((int)rgba[i].r*rgba[i].a)/255;
631                     rgba[i].g = ((int)rgba[i].g*rgba[i].a)/255;
632                     rgba[i].b = ((int)rgba[i].b*rgba[i].a)/255;
633                 } else {
634                     rgba[i].a = 255;
635                 }
636             }
637         } else {
638             int mult = (0x1ff>>header.bpp);
639             palettelen = 1<<header.bpp;
640             rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
641             for(i=0;i<palettelen;i++) {
642                 rgba[i].r = i*mult;
643                 rgba[i].g = i*mult;
644                 rgba[i].b = i*mult;
645             }
646         }
647
648         for(y=0;y<header.height;y++) {
649             int mode = imagedata[pos++]; //filter mode
650             U8*old;
651             U8*dest = &data2[y*swf_width];
652             U8*src;
653             src = &imagedata[pos];
654             if(header.bpp == 8) {
655                 pos+=header.width;
656             } else {
657                 int x,s=0;
658                 int bitpos = 0;
659                 U32 v = (1<<header.bpp)-1;
660                 for(x=0;x<header.width;x++) {
661                     U32 r = src[s/8]<<8 | 
662                             src[s/8+1];
663                     int t;
664                     tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
665                     s+=header.bpp;
666                 }
667                 src = tmpline;
668                 pos+=(header.width*header.bpp+7)/8;
669             }
670
671             if(!y) {
672                 memset(data2,0,swf_width);
673                 old = &data2[y*swf_width];
674             } else {
675                 old = &data2[(y-1)*swf_width];
676             }
677             applyfilter1(mode, src, old, dest, header.width);
678         }
679         swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
680         free(tmpline);
681         free(rgba);
682         free(data2);
683     }
684
685     t = swf_InsertTag(t, ST_DEFINESHAPE3);
686
687     swf_ShapeNew(&s);
688     swf_GetMatrix(NULL, &m);
689     m.sx = 20 * 0x10000;
690     m.sy = 20 * 0x10000;
691     m.tx = -10;
692     m.ty = -10;
693     fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 1);
694
695     swf_SetU16(t, id + 1);      // id
696
697     r.xmin = r.ymin = 0;
698     r.xmax = header.width * 20;
699     r.ymax = header.height * 20;
700     swf_SetRect(t, &r);
701
702     swf_SetShapeHeader(t, s);
703
704     swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
705     swf_ShapeSetLine(t, s, r.xmax, 0);
706     swf_ShapeSetLine(t, s, 0, r.ymax);
707     swf_ShapeSetLine(t, s, -r.xmax, 0);
708     swf_ShapeSetLine(t, s, 0, -r.ymax);
709
710     swf_ShapeSetEnd(t);
711
712     t = swf_InsertTag(t, ST_REMOVEOBJECT2);
713     swf_SetU16(t, 50);          // depth
714
715     t = swf_InsertTag(t, ST_PLACEOBJECT2);
716
717     swf_GetMatrix(NULL, &m);
718     m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
719     m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
720     swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
721
722     t = swf_InsertTag(t, ST_SHOWFRAME);
723
724     fclose(fi);
725
726     return t;
727 }
728
729
730 int CheckInputFile(char *fname, char **realname)
731 {
732     FILE *fi;
733     char *s = malloc(strlen(fname) + 5);
734     struct png_header head;
735
736     if (!s)
737         exit(2);
738     (*realname) = s;
739     strcpy(s, fname);
740
741     // Check whether file exists (with typical extensions)
742
743     if ((fi = fopen(s, "rb")) == NULL) {
744         sprintf(s, "%s.png", fname);
745         if ((fi = fopen(s, "rb")) == NULL) {
746             sprintf(s, "%s.PNG", fname);
747             if ((fi = fopen(s, "rb")) == NULL) {
748                 sprintf(s, "%s.Png", fname);
749                 if ((fi = fopen(s, "rb")) == NULL) {
750                     fprintf(stderr, "Couldn't open %s!\n", fname);
751                     return -1;
752                 }
753             }
754         }
755     }
756
757     if(!png_read_header(fi, &head)) {
758         fprintf(stderr, "%s is not a PNG file!\n", fname);
759         return -1;
760     }
761
762     if (global.max_image_width < head.width)
763         global.max_image_width = head.width;
764     if (global.max_image_height < head.height)
765         global.max_image_height = head.height;
766
767     fclose(fi);
768
769     return 0;
770 }
771
772 int args_callback_option(char *arg, char *val)
773 {
774     int res = 0;
775     if (arg[1])
776         res = -1;
777     else
778         switch (arg[0]) {
779         case 'r':
780             if (val)
781                 global.framerate = atof(val);
782             /* removed framerate>0 restriction in order to make
783                Flash Communication Server compatible SWFs */
784             if ((global.framerate < 0) ||(global.framerate >= 256.0)) {
785                 if (VERBOSE(1))
786                     fprintf(stderr,
787                             "Error: You must specify a valid framerate between 1/256 and 255.\n");
788                 exit(1);
789             }
790             res = 1;
791             break;
792
793         case 'o':
794             if (val)
795                 global.outfile = val;
796             res = 1;
797             break;
798
799         case 'z':
800             global.version = 6;
801             res = 0;
802             break;
803
804         case 'C':
805             global.do_cgi = 1;
806             break;
807
808         case 'v':
809             global.verbose++;
810             res = 0;
811             break;
812
813         case 'q':
814             global.verbose--;
815             if(global.verbose<0)
816                 global.verbose = 0;
817             res = 0;
818             break;
819
820         case 'X':
821             if (val)
822                 global.force_width = atoi(val);
823             res = 1;
824             break;
825
826         case 'Y':
827             if (val)
828                 global.force_height = atoi(val);
829             res = 1;
830             break;
831
832         case 'V':
833             printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
834             exit(0);
835
836         default:
837             res = -1;
838             break;
839         }
840
841     if (res < 0) {
842         if (VERBOSE(1))
843             fprintf(stderr, "Unknown option: -%s\n", arg);
844         exit(1);
845         return 0;
846     }
847     return res;
848 }
849
850 static struct options_t options[] = {
851 {"r", "rate"},
852 {"o", "output"},
853 {"z", "zlib"},
854 {"X", "pixel"},
855 {"Y", "pixel"},
856 {"v", "verbose"},
857 {"q", "quiet"},
858 {"C", "cgi"},
859 {"V", "version"},
860 {0,0}
861 };
862
863 int args_callback_longoption(char *name, char *val)
864 {
865     return args_long2shortoption(options, name, val);
866 }
867
868 int args_callback_command(char *arg, char *next)        // actually used as filename
869 {
870     char *s;
871     if (CheckInputFile(arg, &s) < 0) {
872         if (VERBOSE(1))
873             fprintf(stderr, "Error opening input file: %s\n", arg);
874         free(s);
875     } else {
876         image[global.nfiles].filename = s;
877         global.nfiles++;
878         if (global.nfiles >= MAX_INPUT_FILES) {
879             if (VERBOSE(1))
880                 fprintf(stderr, "Error: Too many input files.\n");
881             exit(1);
882         }
883     }
884     return 0;
885 }
886
887 void args_callback_usage(char *name)
888 {
889     printf("\n");
890     printf("Usage: %s [-X width] [-Y height] [-o file.swf] [-r rate] file1.png [file2.png...]\n", name);
891     printf("\n");
892     printf("-r , --rate <framerate>        Set movie framerate (frames per second)\n");
893     printf("-o , --output <filename>       Set name for SWF output file.\n");
894     printf("-z , --zlib <zlib>             Enable Flash 6 (MX) Zlib Compression\n");
895     printf("-X , --pixel <width>           Force movie width to <width> (default: autodetect)\n");
896     printf("-Y , --pixel <height>          Force movie height to <height> (default: autodetect)\n");
897     printf("-v , --verbose                 Be verbose. Use more than one -v for greater effect \n");
898     printf("-q , --quiet                   Omit normal log messages, only log errors\n");
899     printf("-C , --cgi                     For use as CGI- prepend http header, write to stdout\n");
900     printf("-V , --version                 Print version information and exit\n");
901     printf("\n");
902 }
903
904 int main(int argc, char **argv)
905 {
906     SWF swf;
907     TAG *t;
908
909     memset(&global, 0x00, sizeof(global));
910
911     global.framerate = 1.0;
912     global.verbose = 1;
913     global.version = 4;
914
915     processargs(argc, argv);
916     
917     if(global.nfiles<=0) {
918         fprintf(stderr, "No png files found in arguments\n");
919         return 1;
920     }
921
922     if (VERBOSE(2))
923         fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
924
925     t = MovieStart(&swf, global.framerate,
926                    global.force_width ? global.force_width : global.
927                    max_image_width,
928                    global.force_height ? global.force_height : global.
929                    max_image_height);
930
931     {
932         int i;
933         for (i = 0; i < global.nfiles; i++) {
934             if (VERBOSE(3))
935                 fprintf(stderr, "[%03i] %s\n", i,
936                         image[i].filename);
937             t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
938             free(image[i].filename);
939         }
940     }
941
942     MovieFinish(&swf, t, global.outfile);
943
944     return 0;
945 }