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