moved extern declaration up to prevent compile-time errors.
[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     char *outfile;
43 } global;
44
45 struct {
46     char *filename;
47 } image[MAX_INPUT_FILES];
48
49 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
50 {
51     TAG *t;
52     RGBA rgb;
53
54     memset(swf, 0x00, sizeof(SWF));
55
56     swf->fileVersion = 5;
57     swf->frameRate = (int)(256.0 * framerate);
58     swf->movieSize.xmax = dx * 20;
59     swf->movieSize.ymax = dy * 20;
60
61     t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
62
63     rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
64     //rgb.g = 0xff; <--- handy for testing alpha conversion
65     swf_SetRGB(t, &rgb);
66
67     return t;
68 }
69
70 int MovieFinish(SWF * swf, TAG * t, char *sname)
71 {
72     int f, so = fileno(stdout);
73     t = swf_InsertTag(t, ST_END);
74
75     if ((!isatty(so)) && (!sname))
76         f = so;
77     else {
78         if (!sname)
79             sname = "output.swf";
80         f = open(sname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
81     }
82
83     if(global.do_cgi) {
84         if FAILED(swf_WriteCGI(swf)) fprintf(stderr,"WriteCGI() failed.\n");
85     } else {
86         if FAILED(swf_WriteSWF(f,swf)) fprintf(stderr,"WriteSWF() failed.\n");
87         if (f != so)
88             close(f);
89     }
90
91     swf_FreeTags(swf);
92     return 0;
93 }
94
95 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
96 {
97     unsigned int len;
98     if(destlen) *destlen=0;
99     if(destdata) *destdata=0;
100     if(!fread(&len, 4, 1, fi))
101         return 0;
102     if(!fread(head, 4, 1, fi))
103         return 0;
104     len = REVERSESWAP32(len);
105     if(destlen) *destlen = len;
106     if(destdata) {
107         if(len)
108             *destdata = malloc(len);
109         else 
110             *destdata = 0;
111         if(!fread(*destdata, len, 1, fi)) {
112             *destdata = 0;
113             if(destlen) *destlen=0;
114             return 0;
115         }
116         fseek(fi, 4, SEEK_CUR);
117
118     } else {
119         fseek(fi, len+4, SEEK_CUR);
120     }
121     return 1;
122 }
123
124 unsigned int png_get_dword(FILE*fi)
125 {
126     unsigned int a;
127     fread(&a,4,1,fi);
128     return REVERSESWAP32(a);
129 }
130
131 struct png_header
132 {
133     int width;
134     int height;
135     int bpp;
136     int mode;
137 };
138
139 int png_read_header(FILE*fi, struct png_header*header)
140 {
141     char id[4];
142     int len;
143     int ok=0;
144     U8 head[8] = {137,80,78,71,13,10,26,10};
145     U8 head2[8];
146     U8*data;
147     fread(head2,8,1,fi);
148     if(strncmp(head,head2,4))
149         return 0;
150    
151     while(png_read_chunk(&id, &len, &data, fi))
152     {
153         if(VERBOSE(2))
154             printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
155         if(!strncasecmp(id, "IHDR", 4)) {
156             char a,b,c,f,i;
157             if(len < 8) exit(1);
158             header->width = REVERSESWAP32(*(U32*)&data[0]);
159             header->height = REVERSESWAP32(*(U32*)&data[4]);
160             a = data[8];      // should be 8
161             b = data[9];      // should be 3(indexed) or 2(rgb)
162
163             c = data[10];     // compression mode (0)
164             f = data[11];     // filter mode (0)
165             i = data[12];     // interlace mode (0)
166         
167             if(VERBOSE(2)) printf("image mode:%d\n", b);
168             if(VERBOSE(2)) printf("bpp: %d\n", a);
169             if(VERBOSE(2)) printf("compression: %d\n", c);
170             if(VERBOSE(2)) printf("filter: %d\n", f);
171             if(VERBOSE(2)) printf("interlace: %d\n", i);
172
173             if(b!=0 && b!=2 && b!=3 && b!=6) {
174                 fprintf(stderr, "Image mode %d not supported!\n", b);
175                 if(b == 4) {
176                     fprintf(stderr, "(This is a grayscale image with alpha channel-\n");
177                     fprintf(stderr, " try converting it into an RGB image with alpha channel)\n");
178                 }
179                 exit(1);
180             }
181             if(a!=8 && (b==2 || b==6)) {
182                 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
183                 exit(1);
184             }
185             if(c!=0) {
186                 fprintf(stderr, "Compression mode %d not supported!\n", c);
187                 exit(1);
188             }
189             if(f!=0) {
190                 fprintf(stderr, "Filter mode %d not supported!\n", f);
191                 exit(1);
192             }
193             if(i!=0) {
194                 fprintf(stderr, "Interlace mode %d not supported!\n", i);
195                 exit(1);
196             }
197             if(VERBOSE(2))
198                 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
199             header->bpp = a;
200             header->mode = b;
201             ok = 1;
202         } 
203         
204         free(data);
205     }
206     return ok;
207 }
208
209 typedef unsigned char byte;
210 #define ABS(a) ((a)>0?(a):(-(a)))
211 byte inline PaethPredictor (byte a,byte b,byte c)
212 {
213         // a = left, b = above, c = upper left
214         int p = a + b - c;        // initial estimate
215         int pa = ABS(p - a);      // distances to a, b, c
216         int pb = ABS(p - b);
217         int pc = ABS(p - c);
218         // return nearest of a,b,c,
219         // breaking ties in order a,b,c.
220         if (pa <= pb && pa <= pc) 
221                 return a;
222         else if (pb <= pc) 
223                 return b;
224         else return c;
225 }
226
227 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
228 {
229     int x;
230     unsigned char lastr=0;
231     unsigned char lastg=0;
232     unsigned char lastb=0;
233     unsigned char upperlastr=0;
234     unsigned char upperlastg=0;
235     unsigned char upperlastb=0;
236
237     if(mode==0) {
238         for(x=0;x<width;x++) {
239             dest[0] = 255;
240             dest[1] = src[0];
241             dest[2] = src[1];
242             dest[3] = src[2];
243             dest+=4;
244             src+=3;
245         }
246     }
247     else if(mode==1) {
248         for(x=0;x<width;x++) {
249             dest[0] = 255;
250             dest[1] = src[0]+lastr;
251             dest[2] = src[1]+lastg;
252             dest[3] = src[2]+lastb;
253             lastr = dest[1];
254             lastg = dest[2];
255             lastb = dest[3];
256             dest+=4;
257             src+=3;
258         }
259     }
260     else if(mode==2) {
261         for(x=0;x<width;x++) {
262             dest[0] = 255;
263             dest[1] = src[0]+old[1];
264             dest[2] = src[1]+old[2];
265             dest[3] = src[2]+old[3];
266             dest+=4;
267             old+=4;
268             src+=3;
269         }
270     }
271     else if(mode==3) {
272         for(x=0;x<width;x++) {
273             dest[0] = 255;
274             dest[1] = src[0]+(old[1]+lastr)/2;
275             dest[2] = src[1]+(old[2]+lastg)/2;
276             dest[3] = src[2]+(old[3]+lastb)/2;
277             lastr = dest[1];
278             lastg = dest[2];
279             lastb = dest[3];
280             dest+=4;
281             old+=4;
282             src+=3;
283         }
284     }
285     else if(mode==4) {
286         for(x=0;x<width;x++) {
287             dest[0] = 255;
288             dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
289             dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
290             dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
291             lastr = dest[1];
292             lastg = dest[2];
293             lastb = dest[3];
294             upperlastr = old[1];
295             upperlastg = old[2];
296             upperlastb = old[3];
297             dest+=4;
298             old+=4;
299             src+=3;
300         }
301     }    
302
303 }
304
305 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
306 {
307     int x;
308     unsigned char lastr=0;
309     unsigned char lastg=0;
310     unsigned char lastb=0;
311     unsigned char lasta=0;
312     unsigned char upperlastr=0;
313     unsigned char upperlastg=0;
314     unsigned char upperlastb=0;
315     unsigned char upperlasta=0;
316
317     if(mode==0) {
318         for(x=0;x<width;x++) {
319             dest[0] = src[3];
320             dest[1] = src[0];
321             dest[2] = src[1];
322             dest[3] = src[2];
323             dest+=4;
324             src+=4;
325         }
326     }
327     else if(mode==1) {
328         for(x=0;x<width;x++) {
329             dest[0] = src[3]+lasta;
330             dest[1] = src[0]+lastr;
331             dest[2] = src[1]+lastg;
332             dest[3] = src[2]+lastb;
333             lasta = dest[0];
334             lastr = dest[1];
335             lastg = dest[2];
336             lastb = dest[3];
337             dest+=4;
338             src+=4;
339         }
340     }
341     else if(mode==2) {
342         for(x=0;x<width;x++) {
343             dest[0] = src[3]+old[0];
344             dest[1] = src[0]+old[1];
345             dest[2] = src[1]+old[2];
346             dest[3] = src[2]+old[3];
347             dest+=4;
348             old+=4;
349             src+=4;
350         }
351     }
352     else if(mode==3) {
353         for(x=0;x<width;x++) {
354             dest[0] = src[3]+(old[0]+lasta)/2;
355             dest[1] = src[0]+(old[1]+lastr)/2;
356             dest[2] = src[1]+(old[2]+lastg)/2;
357             dest[3] = src[2]+(old[3]+lastb)/2;
358             lasta = dest[0];
359             lastr = dest[1];
360             lastg = dest[2];
361             lastb = dest[3];
362             dest+=4;
363             old+=4;
364             src+=4;
365         }
366     }
367     else if(mode==4) {
368         for(x=0;x<width;x++) {
369             dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
370             dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
371             dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
372             dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
373             lasta = dest[0];
374             lastr = dest[1];
375             lastg = dest[2];
376             lastb = dest[3];
377             upperlasta = old[0];
378             upperlastr = old[1];
379             upperlastg = old[2];
380             upperlastb = old[3];
381             dest+=4;
382             old+=4;
383             src+=4;
384         }
385     }    
386
387 }
388
389 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
390 {
391     int x;
392     unsigned char last=0;
393     unsigned char upperlast=0;
394
395     if(mode==0) {
396         for(x=0;x<width;x++) {
397             *dest = *src;
398             dest++;
399             src++;
400         }
401     }
402     else if(mode==1) {
403         for(x=0;x<width;x++) {
404             *dest = *src+last;
405             last = *dest;
406             dest++;
407             src++;
408         }
409     }
410     else if(mode==2) {
411         for(x=0;x<width;x++) {
412             *dest = *src+*old;
413             dest++;
414             old++;
415             src++;
416         }
417     }
418     else if(mode==3) {
419         for(x=0;x<width;x++) {
420             *dest = *src+(*old+last)/2;
421             dest++;
422             old++;
423             src++;
424         }
425     }
426     else if(mode==4) {
427         for(x=0;x<width;x++) {
428             *dest = *src+PaethPredictor(last,*old,upperlast);
429             last = *dest;
430             upperlast = *old;
431             dest++;
432             old++;
433             src++;
434         }
435     }    
436
437 }
438
439 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
440 {
441     SHAPE *s;
442     SRECT r;
443     MATRIX m;
444     int fs;
445
446     char tagid[4];
447     int len;
448     U8*data;
449     U8*imagedata;
450     U8*zimagedata=0;
451     unsigned long int imagedatalen;
452     unsigned long int zimagedatalen=0;
453     U8*palette = 0;
454     int palettelen = 0;
455     U8*alphapalette = 0;
456     int alphapalettelen = 0;
457     struct png_header header;
458     int bypp;
459
460     FILE *fi;
461     U8 *scanline;
462
463     if ((fi = fopen(sname, "rb")) == NULL) {
464         if (VERBOSE(1))
465             fprintf(stderr, "Read access failed: %s\n", sname);
466         return t;
467     }
468
469     if(!png_read_header(fi, &header))
470         return 0;
471
472     if(header.mode == 3 || header.mode == 0) bypp = 1;
473     else
474     if(header.mode == 2) bypp = 3;
475     else
476     if(header.mode == 6) bypp = 4;
477     else
478         return 0;
479     imagedatalen = bypp * header.width * header.height + 65536;
480     imagedata = malloc(imagedatalen);
481
482     fseek(fi,8,SEEK_SET);
483     while(!feof(fi))
484     {
485         if(!png_read_chunk(&tagid, &len, &data, fi))
486             break;
487         if(!strncmp(tagid, "IEND", 4)) {
488             break;
489         }
490         if(!strncmp(tagid, "PLTE", 4)) {
491             palette = data;
492             palettelen = len/3;
493             data = 0; //don't free data
494             if(VERBOSE(2))
495                 printf("%d colors in palette\n", palettelen);
496         }
497         if(!strncmp(tagid, "tRNS", 4)) {
498             if(header.mode == 3) {
499                 alphapalette = data;
500                 alphapalettelen = len;
501                 data = 0; //don't free data
502                 if(VERBOSE(2))
503                     printf("found %d alpha colors\n", alphapalettelen);
504             }
505         }
506         if(!strncmp(tagid, "IDAT", 4)) {
507             if(!zimagedata) {
508                 zimagedatalen = len;
509                 zimagedata = malloc(len);
510                 memcpy(zimagedata,data,len);
511             } else {
512                 zimagedata = realloc(zimagedata, zimagedatalen+len);
513                 memcpy(&zimagedata[zimagedatalen], data, len);
514                 zimagedatalen += len;
515             }
516         }
517         if(data)
518             free(data);
519     }
520     
521     if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
522         fprintf(stderr, "Couldn't uncompress %s!\n", sname);
523         if(zimagedata)
524             free(zimagedata);
525         return 0;
526     }
527     free(zimagedata);
528
529     if(alphapalette)
530         t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
531     else
532         t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
533
534     swf_SetU16(t, id);          // id
535     if(header.mode == 2 || header.mode == 6) {
536         U8*data2 = malloc(header.width*header.height*4);
537         int i,s=0;
538         int x,y;
539         int pos=0;
540         int opaque=0;
541         int transparent=0;
542         int semitransparent=0;
543         /* in case for mode 2, the following also performs 24->32 bit conversion */
544         for(y=0;y<header.height;y++) {
545             int mode = imagedata[pos++]; //filter mode
546             U8*src;
547             U8*dest;
548             U8*old;
549             dest = &data2[(y*header.width)*4];
550
551             if(header.bpp == 8)
552             {
553                 /* one byte per pixel */
554                 src = &imagedata[pos];
555                 pos+=header.width*(header.mode==6?4:3);
556             } else {
557                 /* not implemented yet */
558                 exit(1);
559             }
560
561             if(!y) {
562                 memset(data2,0,header.width*4);
563                 old = &data2[y*header.width*4];
564             } else {
565                 old = &data2[(y-1)*header.width*4];
566             }
567             if(header.mode==6)
568                 applyfilter4(mode, src, old, dest, header.width);
569             else if(header.mode==2)
570                 applyfilter3(mode, src, old, dest, header.width);
571         }
572
573         /* the image is now compressed and stored in data. Now let's take
574            a look at the alpha values to determine which bitmap type we
575            should write */
576         if(header.mode == 6)
577         for(y=0;y<header.height;y++) {
578             U8*l = &data2[(y*header.width)*4];
579             for(x=0;x<header.width;x++) {
580                 if(l[x*4+0]==255) transparent++;
581                 else if(l[x*4+0]==0) opaque++;
582                 else semitransparent++;
583             }
584         }
585         /* mode 6 images which are not fully opaque or fully transparent
586            will be stored as definejpeg3 */
587         if(header.mode == 6 && transparent != header.width*header.height
588                             && opaque != header.width*header.height)
589 #ifndef HAVE_JPEGLIB
590             fprintf(stderr, "Warning: No jpeg lib compiled in- not able to store transparency information\n");
591 #else
592         {   
593             fprintf(stderr, "Image has transparency information. Storing as DefineBitsJpeg3 Tag (jpeg+alpha)\n");
594             if(VERBOSE(2))
595                 printf("Image is semi-transparent\n");
596
597             // we always use quality 100, since png2swf is expected to
598             // use more or less lossless compression
599             
600             swf_SetJPEGBits3(t, header.width, header.height, (RGBA*)data2, 100);
601             t->id = ST_DEFINEBITSJPEG3;
602         } 
603         else
604 #endif
605         {
606             swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
607         }
608         free(data2);
609     } else if(header.mode == 0 || header.mode == 3) {
610         RGBA*rgba;
611         int swf_width = BYTES_PER_SCANLINE(header.width);
612         U8*data2 = malloc(swf_width*header.height);
613         U8*tmpline = malloc(header.width);
614         int i,x,y;
615         int pos=0;
616         if(header.mode == 3) { // palette or grayscale?
617             rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
618             if(!palette) {
619                 fprintf(stderr, "Error: No palette found!\n");
620                 exit(1);
621             }
622             /* 24->32 bit conversion */
623             for(i=0;i<palettelen;i++) {
624                 rgba[i].r = palette[i*3+0];
625                 rgba[i].g = palette[i*3+1];
626                 rgba[i].b = palette[i*3+2];
627                 if(alphapalette && i<alphapalettelen) {
628                     rgba[i].a = alphapalette[i];
629                     if(alphapalette[i] == 0) {
630                         /* if the color is fully transparent, it doesn't matter
631                            what it's rgb values are. furthermore, all Flash 
632                            players up to Flash 5 can't deal with anything beyond
633                            one transparent color with value (00,00,00,00). */
634                         rgba[i].r = rgba[i].g = rgba[i].b = 0;
635                     }
636                 } else {
637                     rgba[i].a = 255;
638                 }
639             }
640         } else {
641             palettelen = 256;
642             rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
643             for(i=0;i<256;i++) {
644                 rgba[i].r = i;
645                 rgba[i].g = i;
646                 rgba[i].b = i;
647             }
648         }
649
650         for(y=0;y<header.height;y++) {
651             int mode = imagedata[pos++]; //filter mode
652             U8*old;
653             U8*dest = &data2[y*swf_width];
654             U8*src;
655             src = &imagedata[pos];
656             if(header.bpp == 8) {
657                 pos+=header.width;
658             } else {
659                 int x,s=0;
660                 int bitpos = 0;
661                 U32 v = (1<<header.bpp)-1;
662                 for(x=0;x<header.width;x++) {
663                     U32 r = src[s/8]<<8 | 
664                             src[s/8+1];
665                     int t;
666                     tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
667                     s+=header.bpp;
668                 }
669                 src = tmpline;
670                 pos+=(header.width*header.bpp+7)/8;
671             }
672
673             if(!y) {
674                 memset(data2,0,swf_width);
675                 old = &data2[y*swf_width];
676             } else {
677                 old = &data2[(y-1)*swf_width];
678             }
679             applyfilter1(mode, src, old, dest, header.width);
680         }
681         swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
682         free(tmpline);
683         free(rgba);
684         free(data2);
685     }
686
687     t = swf_InsertTag(t, ST_DEFINESHAPE3);
688
689     swf_ShapeNew(&s);
690     swf_GetMatrix(NULL, &m);
691     m.sx = 20 * 0x10000;
692     m.sy = 20 * 0x10000;
693     fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
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     if(!png_read_header(fi, &head)) {
757         fprintf(stderr, "%s is not a PNG file!\n", fname);
758         return -1;
759     }
760
761     if (global.max_image_width < head.width)
762         global.max_image_width = head.width;
763     if (global.max_image_height < head.height)
764         global.max_image_height = head.height;
765
766     fclose(fi);
767
768     return 0;
769 }
770
771 int args_callback_option(char *arg, char *val)
772 {
773     int res = 0;
774     if (arg[1])
775         res = -1;
776     else
777         switch (arg[0]) {
778         case 'r':
779             if (val)
780                 global.framerate = atof(val);
781             if ((global.framerate < 1.0/256) ||(global.framerate >= 256.0)) {
782                 if (VERBOSE(1))
783                     fprintf(stderr,
784                             "Error: You must specify a valid framerate between 1/256 and 255.\n");
785                 exit(1);
786             }
787             res = 1;
788             break;
789
790         case 'o':
791             if (val)
792                 global.outfile = val;
793             res = 1;
794             break;
795
796         case 'C':
797             global.do_cgi = 1;
798             break;
799
800         case 'v':
801             if (val)
802                 global.verbose = atoi(val);
803             res = 1;
804             break;
805
806         case 'X':
807             if (val)
808                 global.force_width = atoi(val);
809             res = 1;
810             break;
811
812         case 'Y':
813             if (val)
814                 global.force_height = atoi(val);
815             res = 1;
816             break;
817
818         case 'V':
819             printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
820             exit(0);
821
822         default:
823             res = -1;
824             break;
825         }
826
827     if (res < 0) {
828         if (VERBOSE(1))
829             fprintf(stderr, "Unknown option: -%s\n", arg);
830         exit(1);
831         return 0;
832     }
833     return res;
834 }
835
836 struct options_t options[] = 
837
838 {"h", "help"},
839 {"o", "output"},
840 {"r", "rate"},
841 {"C", "cgi"},
842 {"v", "verbose"},
843 {"X", "width"},
844 {"Y", "height"},
845 {"V", "version"},
846 {0,0}
847 };
848
849 int args_callback_longoption(char *name, char *val)
850 {
851     return args_long2shortoption(options, name, val);
852 }
853
854 int args_callback_command(char *arg, char *next)        // actually used as filename
855 {
856     char *s;
857     if (CheckInputFile(arg, &s) < 0) {
858         if (VERBOSE(1))
859             fprintf(stderr, "Error opening input file: %s\n", arg);
860         free(s);
861     } else {
862         image[global.nfiles].filename = s;
863         global.nfiles++;
864         if (global.nfiles >= MAX_INPUT_FILES) {
865             if (VERBOSE(1))
866                 fprintf(stderr, "Error: Too many input files.\n");
867             exit(1);
868         }
869     }
870     return 0;
871 }
872
873 void args_callback_usage(char *name)
874 {
875     printf("Usage: %s  [-options [value]] imagefiles[.png] [...]\n", name);
876     printf("\t-r , --rate <framerate>\t\t\tSet movie framerate (frames per second)\n");
877     printf("\t-o , --output <filename>\t\t\tSet name for SWF output file\n");
878     printf("\t-X , --pixel <width>\t\t\tForce movie width to pixel (default: autodetect)\n");
879     printf("\t-Y , --pixel <height>\t\t\tForce movie height to pixel (default: autodetect)\n");
880     printf("\t-v , --verbose <level>\t\t\t Set verbose level (0=quiet, 1=default, 2=debug)\n");
881     printf("\t-C , --cgi\t\t\t For use as CGI- prepend http header, write to stdout\n");
882     printf("\t-V , --version\t\t\tPrint version information and exit\n");
883 }
884
885 int main(int argc, char **argv)
886 {
887     SWF swf;
888     TAG *t;
889
890     memset(&global, 0x00, sizeof(global));
891
892     global.framerate = 1.0;
893     global.verbose = 1;
894
895     processargs(argc, argv);
896
897     if(global.nfiles<=0)
898         return 1;
899
900     if (VERBOSE(2))
901         fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
902
903     t = MovieStart(&swf, global.framerate,
904                    global.force_width ? global.force_width : global.
905                    max_image_width,
906                    global.force_height ? global.force_height : global.
907                    max_image_height);
908
909     {
910         int i;
911         for (i = 0; i < global.nfiles; i++) {
912             if (VERBOSE(3))
913                 fprintf(stderr, "[%03i] %s\n", i,
914                         image[i].filename);
915             t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
916             free(image[i].filename);
917         }
918     }
919
920     MovieFinish(&swf, t, global.outfile);
921
922     return 0;
923 }