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