-r prints the correct framerate now
[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             last = *dest;
422             dest++;
423             old++;
424             src++;
425         }
426     }
427     else if(mode==4) {
428         for(x=0;x<width;x++) {
429             *dest = *src+PaethPredictor(last,*old,upperlast);
430             last = *dest;
431             upperlast = *old;
432             dest++;
433             old++;
434             src++;
435         }
436     }    
437
438 }
439
440 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
441 {
442     SHAPE *s;
443     SRECT r;
444     MATRIX m;
445     int fs;
446
447     char tagid[4];
448     int len;
449     U8*data;
450     U8*imagedata;
451     U8*zimagedata=0;
452     unsigned long int imagedatalen;
453     unsigned long int zimagedatalen=0;
454     U8*palette = 0;
455     int palettelen = 0;
456     U8*alphapalette = 0;
457     int alphapalettelen = 0;
458     struct png_header header;
459     int bypp;
460
461     FILE *fi;
462     U8 *scanline;
463
464     if ((fi = fopen(sname, "rb")) == NULL) {
465         if (VERBOSE(1))
466             fprintf(stderr, "Read access failed: %s\n", sname);
467         return t;
468     }
469
470     if(!png_read_header(fi, &header))
471         return 0;
472
473     if(header.mode == 3 || header.mode == 0) bypp = 1;
474     else
475     if(header.mode == 2) bypp = 3;
476     else
477     if(header.mode == 6) bypp = 4;
478     else
479         return 0;
480     imagedatalen = bypp * header.width * header.height + 65536;
481     imagedata = malloc(imagedatalen);
482
483     fseek(fi,8,SEEK_SET);
484     while(!feof(fi))
485     {
486         if(!png_read_chunk(&tagid, &len, &data, fi))
487             break;
488         if(!strncmp(tagid, "IEND", 4)) {
489             break;
490         }
491         if(!strncmp(tagid, "PLTE", 4)) {
492             palette = data;
493             palettelen = len/3;
494             data = 0; //don't free data
495             if(VERBOSE(2))
496                 printf("%d colors in palette\n", palettelen);
497         }
498         if(!strncmp(tagid, "tRNS", 4)) {
499             if(header.mode == 3) {
500                 alphapalette = data;
501                 alphapalettelen = len;
502                 data = 0; //don't free data
503                 if(VERBOSE(2))
504                     printf("found %d alpha colors\n", alphapalettelen);
505             }
506         }
507         if(!strncmp(tagid, "IDAT", 4)) {
508             if(!zimagedata) {
509                 zimagedatalen = len;
510                 zimagedata = malloc(len);
511                 memcpy(zimagedata,data,len);
512             } else {
513                 zimagedata = realloc(zimagedata, zimagedatalen+len);
514                 memcpy(&zimagedata[zimagedatalen], data, len);
515                 zimagedatalen += len;
516             }
517         }
518         if(data)
519             free(data);
520     }
521     
522     if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
523         fprintf(stderr, "Couldn't uncompress %s!\n", sname);
524         if(zimagedata)
525             free(zimagedata);
526         return 0;
527     }
528     free(zimagedata);
529
530     if(alphapalette)
531         t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
532     else
533         t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
534
535     swf_SetU16(t, id);          // id
536     if(header.mode == 2 || header.mode == 6) {
537         U8*data2 = malloc(header.width*header.height*4);
538         int i,s=0;
539         int x,y;
540         int pos=0;
541         int opaque=0;
542         int transparent=0;
543         int semitransparent=0;
544         /* in case for mode 2, the following also performs 24->32 bit conversion */
545         for(y=0;y<header.height;y++) {
546             int mode = imagedata[pos++]; //filter mode
547             U8*src;
548             U8*dest;
549             U8*old;
550             dest = &data2[(y*header.width)*4];
551
552             if(header.bpp == 8)
553             {
554                 /* one byte per pixel */
555                 src = &imagedata[pos];
556                 pos+=header.width*(header.mode==6?4:3);
557             } else {
558                 /* not implemented yet */
559                 exit(1);
560             }
561
562             if(!y) {
563                 memset(data2,0,header.width*4);
564                 old = &data2[y*header.width*4];
565             } else {
566                 old = &data2[(y-1)*header.width*4];
567             }
568             if(header.mode==6)
569                 applyfilter4(mode, src, old, dest, header.width);
570             else if(header.mode==2)
571                 applyfilter3(mode, src, old, dest, header.width);
572         }
573
574         /* the image is now compressed and stored in data. Now let's take
575            a look at the alpha values to determine which bitmap type we
576            should write */
577         if(header.mode == 6)
578         for(y=0;y<header.height;y++) {
579             U8*l = &data2[(y*header.width)*4];
580             for(x=0;x<header.width;x++) {
581                 if(l[x*4+0]==255) transparent++;
582                 else if(l[x*4+0]==0) opaque++;
583                 else semitransparent++;
584             }
585         }
586         /* mode 6 images which are not fully opaque or fully transparent
587            will be stored as definejpeg3 */
588         if(header.mode == 6 && transparent != header.width*header.height
589                             && opaque != header.width*header.height)
590 #ifndef HAVE_JPEGLIB
591             fprintf(stderr, "Warning: No jpeg lib compiled in- not able to store transparency information\n");
592 #else
593         {   
594             fprintf(stderr, "Image has transparency information. Storing as DefineBitsJpeg3 Tag (jpeg+alpha)\n");
595             if(VERBOSE(2))
596                 printf("Image is semi-transparent\n");
597
598             // we always use quality 100, since png2swf is expected to
599             // use more or less lossless compression
600             
601             swf_SetJPEGBits3(t, header.width, header.height, (RGBA*)data2, 100);
602             t->id = ST_DEFINEBITSJPEG3;
603         } 
604         else
605 #endif
606         {
607             swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
608         }
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                     if(alphapalette[i] == 0) {
631                         /* if the color is fully transparent, it doesn't matter
632                            what it's rgb values are. furthermore, all Flash 
633                            players up to Flash 5 can't deal with anything beyond
634                            one transparent color with value (00,00,00,00). */
635                         rgba[i].r = rgba[i].g = rgba[i].b = 0;
636                     }
637                 } else {
638                     rgba[i].a = 255;
639                 }
640             }
641         } else {
642             palettelen = 256;
643             rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
644             for(i=0;i<256;i++) {
645                 rgba[i].r = i;
646                 rgba[i].g = i;
647                 rgba[i].b = i;
648             }
649         }
650
651         for(y=0;y<header.height;y++) {
652             int mode = imagedata[pos++]; //filter mode
653             U8*old;
654             U8*dest = &data2[y*swf_width];
655             U8*src;
656             src = &imagedata[pos];
657             if(header.bpp == 8) {
658                 pos+=header.width;
659             } else {
660                 int x,s=0;
661                 int bitpos = 0;
662                 U32 v = (1<<header.bpp)-1;
663                 for(x=0;x<header.width;x++) {
664                     U32 r = src[s/8]<<8 | 
665                             src[s/8+1];
666                     int t;
667                     tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
668                     s+=header.bpp;
669                 }
670                 src = tmpline;
671                 pos+=(header.width*header.bpp+7)/8;
672             }
673
674             if(!y) {
675                 memset(data2,0,swf_width);
676                 old = &data2[y*swf_width];
677             } else {
678                 old = &data2[(y-1)*swf_width];
679             }
680             applyfilter1(mode, src, old, dest, header.width);
681         }
682         swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
683         free(tmpline);
684         free(rgba);
685         free(data2);
686     }
687
688     while(t->len < 64) {
689         swf_SetU8(t, 0); //fix for flash player bug- see ../lib/modules/swfbits.c
690     }
691
692     t = swf_InsertTag(t, ST_DEFINESHAPE3);
693
694     swf_ShapeNew(&s);
695     swf_GetMatrix(NULL, &m);
696     m.sx = 20 * 0x10000;
697     m.sy = 20 * 0x10000;
698     fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
699
700     swf_SetU16(t, id + 1);      // id
701
702     r.xmin = r.ymin = 0;
703     r.xmax = header.width * 20;
704     r.ymax = header.height * 20;
705     swf_SetRect(t, &r);
706
707     swf_SetShapeHeader(t, s);
708
709     swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
710     swf_ShapeSetLine(t, s, r.xmax, 0);
711     swf_ShapeSetLine(t, s, 0, r.ymax);
712     swf_ShapeSetLine(t, s, -r.xmax, 0);
713     swf_ShapeSetLine(t, s, 0, -r.ymax);
714
715     swf_ShapeSetEnd(t);
716
717     t = swf_InsertTag(t, ST_REMOVEOBJECT2);
718     swf_SetU16(t, 50);          // depth
719
720     t = swf_InsertTag(t, ST_PLACEOBJECT2);
721
722     swf_GetMatrix(NULL, &m);
723     m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
724     m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
725     swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
726
727     t = swf_InsertTag(t, ST_SHOWFRAME);
728
729     fclose(fi);
730
731     return t;
732 }
733
734
735 int CheckInputFile(char *fname, char **realname)
736 {
737     FILE *fi;
738     char *s = malloc(strlen(fname) + 5);
739     struct png_header head;
740
741     if (!s)
742         exit(2);
743     (*realname) = s;
744     strcpy(s, fname);
745
746     // Check whether file exists (with typical extensions)
747
748     if ((fi = fopen(s, "rb")) == NULL) {
749         sprintf(s, "%s.png", fname);
750         if ((fi = fopen(s, "rb")) == NULL) {
751             sprintf(s, "%s.PNG", fname);
752             if ((fi = fopen(s, "rb")) == NULL) {
753                 sprintf(s, "%s.Png", fname);
754                 if ((fi = fopen(s, "rb")) == NULL)
755                     fprintf(stderr, "Couldn't open %s!\n", fname);
756                     return -1;
757             }
758         }
759     }
760
761     if(!png_read_header(fi, &head)) {
762         fprintf(stderr, "%s is not a PNG file!\n", fname);
763         return -1;
764     }
765
766     if (global.max_image_width < head.width)
767         global.max_image_width = head.width;
768     if (global.max_image_height < head.height)
769         global.max_image_height = head.height;
770
771     fclose(fi);
772
773     return 0;
774 }
775
776 int args_callback_option(char *arg, char *val)
777 {
778     int res = 0;
779     if (arg[1])
780         res = -1;
781     else
782         switch (arg[0]) {
783         case 'r':
784             if (val)
785                 global.framerate = atof(val);
786             if ((global.framerate < 1.0/256) ||(global.framerate >= 256.0)) {
787                 if (VERBOSE(1))
788                     fprintf(stderr,
789                             "Error: You must specify a valid framerate between 1/256 and 255.\n");
790                 exit(1);
791             }
792             res = 1;
793             break;
794
795         case 'o':
796             if (val)
797                 global.outfile = val;
798             res = 1;
799             break;
800
801         case 'C':
802             global.do_cgi = 1;
803             break;
804
805         case 'v':
806             if (val)
807                 global.verbose = atoi(val);
808             res = 1;
809             break;
810
811         case 'X':
812             if (val)
813                 global.force_width = atoi(val);
814             res = 1;
815             break;
816
817         case 'Y':
818             if (val)
819                 global.force_height = atoi(val);
820             res = 1;
821             break;
822
823         case 'V':
824             printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
825             exit(0);
826
827         default:
828             res = -1;
829             break;
830         }
831
832     if (res < 0) {
833         if (VERBOSE(1))
834             fprintf(stderr, "Unknown option: -%s\n", arg);
835         exit(1);
836         return 0;
837     }
838     return res;
839 }
840
841 static struct options_t options[] = {
842 {"r", "rate"},
843 {"o", "output"},
844 {"X", "pixel"},
845 {"Y", "pixel"},
846 {"v", "verbose"},
847 {"C", "cgi"},
848 {"V", "version"},
849 {0,0}
850 };
851
852 int args_callback_longoption(char *name, char *val)
853 {
854     return args_long2shortoption(options, name, val);
855 }
856
857 int args_callback_command(char *arg, char *next)        // actually used as filename
858 {
859     char *s;
860     if (CheckInputFile(arg, &s) < 0) {
861         if (VERBOSE(1))
862             fprintf(stderr, "Error opening input file: %s\n", arg);
863         free(s);
864     } else {
865         image[global.nfiles].filename = s;
866         global.nfiles++;
867         if (global.nfiles >= MAX_INPUT_FILES) {
868             if (VERBOSE(1))
869                 fprintf(stderr, "Error: Too many input files.\n");
870             exit(1);
871         }
872     }
873     return 0;
874 }
875
876 void args_callback_usage(char *name)
877 {
878     printf("\n");
879     printf("Usage: %s [-X width] [-Y height] [-o file.swf] [-r rate] file1.png [file2.png...]\n", name);
880     printf("\n");
881     printf("-r , --rate <framerate>        Set movie framerate (frames per second)\n");
882     printf("-o , --output <filename>       Set name for SWF output file.\n");
883     printf("-X , --pixel <width>           Force movie width to <width> (default: autodetect)\n");
884     printf("-Y , --pixel <height>          Force movie height to <height> (default: autodetect)\n");
885     printf("-v , --verbose <level>         Set verbose level (0=quiet, 1=default, 2=debug)\n");
886     printf("-C , --cgi                     For use as CGI- prepend http header, write to stdout\n");
887     printf("-V , --version                 Print version information and exit\n");
888     printf("\n");
889 }
890
891 int main(int argc, char **argv)
892 {
893     SWF swf;
894     TAG *t;
895
896     memset(&global, 0x00, sizeof(global));
897
898     global.framerate = 1.0;
899     global.verbose = 1;
900
901     processargs(argc, argv);
902
903     if(global.nfiles<=0)
904         return 1;
905
906     if (VERBOSE(2))
907         fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
908
909     t = MovieStart(&swf, global.framerate,
910                    global.force_width ? global.force_width : global.
911                    max_image_width,
912                    global.force_height ? global.force_height : global.
913                    max_image_height);
914
915     {
916         int i;
917         for (i = 0; i < global.nfiles; i++) {
918             if (VERBOSE(3))
919                 fprintf(stderr, "[%03i] %s\n", i,
920                         image[i].filename);
921             t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
922             free(image[i].filename);
923         }
924     }
925
926     MovieFinish(&swf, t, global.outfile);
927
928     return 0;
929 }