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