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