removed lame.
[swftools.git] / src / png2swf.c
blob6d4fd77c1f13e3a51c69d17957912be0117fa5e2
1 /* png2swf.c
3 PNG to SWF converter tool
5 Part of the swftools package.
7 Copyright (c) 2002,2003 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
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"
30 #define MAX_INPUT_FILES 1024
31 #define VERBOSE(x) (global.verbose>=x)
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;
45 struct {
46 char *filename;
47 } image[MAX_INPUT_FILES];
49 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
51 TAG *t;
52 RGBA rgb;
54 memset(swf, 0x00, sizeof(SWF));
56 swf->fileVersion = 5;
57 swf->frameRate = (int)(256.0 * framerate);
58 swf->movieSize.xmax = dx * 20;
59 swf->movieSize.ymax = dy * 20;
61 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
63 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
64 //rgb.g = 0xff; //<--- handy for testing alpha conversion
65 swf_SetRGB(t, &rgb);
67 return t;
70 int MovieFinish(SWF * swf, TAG * t, char *sname)
72 int f, so = fileno(stdout);
73 t = swf_InsertTag(t, ST_END);
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);
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);
91 swf_FreeTags(swf);
92 return 0;
95 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
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;
116 fseek(fi, 4, SEEK_CUR);
118 } else {
119 fseek(fi, len+4, SEEK_CUR);
121 return 1;
124 unsigned int png_get_dword(FILE*fi)
126 unsigned int a;
127 fread(&a,4,1,fi);
128 return REVERSESWAP32(a);
131 struct png_header
133 int width;
134 int height;
135 int bpp;
136 int mode;
139 int png_read_header(FILE*fi, struct png_header*header)
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;
151 while(png_read_chunk(&id, &len, &data, fi))
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)
163 c = data[10]; // compression mode (0)
164 f = data[11]; // filter mode (0)
165 i = data[12]; // interlace mode (0)
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);
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");
179 exit(1);
181 if(a!=8 && (b==2 || b==6)) {
182 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
183 exit(1);
185 if(c!=0) {
186 fprintf(stderr, "Compression mode %d not supported!\n", c);
187 exit(1);
189 if(f!=0) {
190 fprintf(stderr, "Filter mode %d not supported!\n", f);
191 exit(1);
193 if(i!=0) {
194 fprintf(stderr, "Interlace mode %d not supported!\n", i);
195 exit(1);
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;
204 free(data);
206 return ok;
209 typedef unsigned char byte;
210 #define ABS(a) ((a)>0?(a):(-(a)))
211 byte inline PaethPredictor (byte a,byte b,byte c)
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;
227 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
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;
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;
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;
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;
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;
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;
305 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
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;
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;
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;
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;
352 else if(mode==3) {
353 for(x=0;x<width;x++) {
354 dest[0] = src[3]+(old[0]+lasta)/2;
355 dest[1] = src[0]+(old[1]+lastr)/2;
356 dest[2] = src[1]+(old[2]+lastg)/2;
357 dest[3] = src[2]+(old[3]+lastb)/2;
358 lasta = dest[0];
359 lastr = dest[1];
360 lastg = dest[2];
361 lastb = dest[3];
362 dest+=4;
363 old+=4;
364 src+=4;
367 else if(mode==4) {
368 for(x=0;x<width;x++) {
369 dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
370 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
371 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
372 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
373 lasta = dest[0];
374 lastr = dest[1];
375 lastg = dest[2];
376 lastb = dest[3];
377 upperlasta = old[0];
378 upperlastr = old[1];
379 upperlastg = old[2];
380 upperlastb = old[3];
381 dest+=4;
382 old+=4;
383 src+=4;
389 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
391 int x;
392 unsigned char last=0;
393 unsigned char upperlast=0;
395 if(mode==0) {
396 for(x=0;x<width;x++) {
397 *dest = *src;
398 dest++;
399 src++;
402 else if(mode==1) {
403 for(x=0;x<width;x++) {
404 *dest = *src+last;
405 last = *dest;
406 dest++;
407 src++;
410 else if(mode==2) {
411 for(x=0;x<width;x++) {
412 *dest = *src+*old;
413 dest++;
414 old++;
415 src++;
418 else if(mode==3) {
419 for(x=0;x<width;x++) {
420 *dest = *src+(*old+last)/2;
421 last = *dest;
422 dest++;
423 old++;
424 src++;
427 else if(mode==4) {
428 for(x=0;x<width;x++) {
429 *dest = *src+PaethPredictor(last,*old,upperlast);
430 last = *dest;
431 upperlast = *old;
432 dest++;
433 old++;
434 src++;
440 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
442 SHAPE *s;
443 SRECT r;
444 MATRIX m;
445 int fs;
447 char tagid[4];
448 int len;
449 U8*data;
450 U8*imagedata;
451 U8*zimagedata=0;
452 unsigned long int imagedatalen;
453 unsigned long int zimagedatalen=0;
454 U8*palette = 0;
455 int palettelen = 0;
456 U8*alphapalette = 0;
457 int alphapalettelen = 0;
458 struct png_header header;
459 int bypp;
461 FILE *fi;
462 U8 *scanline;
464 if ((fi = fopen(sname, "rb")) == NULL) {
465 if (VERBOSE(1))
466 fprintf(stderr, "Read access failed: %s\n", sname);
467 return t;
470 if(!png_read_header(fi, &header))
471 return 0;
473 if(header.mode == 3 || header.mode == 0) bypp = 1;
474 else
475 if(header.mode == 2) bypp = 3;
476 else
477 if(header.mode == 6) bypp = 4;
478 else
479 return 0;
480 imagedatalen = bypp * header.width * header.height + 65536;
481 imagedata = malloc(imagedatalen);
483 fseek(fi,8,SEEK_SET);
484 while(!feof(fi))
486 if(!png_read_chunk(&tagid, &len, &data, fi))
487 break;
488 if(!strncmp(tagid, "IEND", 4)) {
489 break;
491 if(!strncmp(tagid, "PLTE", 4)) {
492 palette = data;
493 palettelen = len/3;
494 data = 0; //don't free data
495 if(VERBOSE(2))
496 printf("%d colors in palette\n", palettelen);
498 if(!strncmp(tagid, "tRNS", 4)) {
499 if(header.mode == 3) {
500 alphapalette = data;
501 alphapalettelen = len;
502 data = 0; //don't free data
503 if(VERBOSE(2))
504 printf("found %d alpha colors\n", alphapalettelen);
507 if(!strncmp(tagid, "IDAT", 4)) {
508 if(!zimagedata) {
509 zimagedatalen = len;
510 zimagedata = malloc(len);
511 memcpy(zimagedata,data,len);
512 } else {
513 zimagedata = realloc(zimagedata, zimagedatalen+len);
514 memcpy(&zimagedata[zimagedatalen], data, len);
515 zimagedatalen += len;
518 if(data)
519 free(data);
522 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
523 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
524 if(zimagedata)
525 free(zimagedata);
526 return 0;
528 free(zimagedata);
530 if(alphapalette)
531 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
532 else
533 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
535 swf_SetU16(t, id); // id
536 if(header.mode == 2 || header.mode == 6) {
537 U8*data2 = malloc(header.width*header.height*4);
538 int i,s=0;
539 int x,y;
540 int pos=0;
541 int opaque=0;
542 int transparent=0;
543 int semitransparent=0;
544 /* in case for mode 2, the following also performs 24->32 bit conversion */
545 unsigned char* firstline = malloc(header.width*4);
547 for(y=0;y<header.height;y++) {
548 int mode = imagedata[pos++]; //filter mode
549 U8*src;
550 U8*dest;
551 U8*old;
552 dest = &data2[(y*header.width)*4];
554 if(header.bpp == 8)
556 /* one byte per pixel */
557 src = &imagedata[pos];
558 pos+=header.width*(header.mode==6?4:3);
559 } else {
560 /* not implemented yet */
561 exit(1);
564 if(!y) {
565 old = firstline;
566 memset(old, 0, header.width*4);
567 } else {
568 old = &data2[(y-1)*header.width*4];
570 if(header.mode==6)
571 applyfilter4(mode, src, old, dest, header.width);
572 else if(header.mode==2)
573 applyfilter3(mode, src, old, dest, header.width);
575 free(firstline);
577 /* the image is now compressed and stored in data. Now let's take
578 a look at the alpha values */
579 if(header.mode == 6) {
580 for(y=0;y<header.height;y++) {
581 U8*l = &data2[(y*header.width)*4];
582 for(x=0;x<header.width;x++) {
583 U8 a = l[x*4];
584 U8 b = l[x*4+1];
585 U8 g = l[x*4+2];
586 U8 r = l[x*4+3];
587 if(a==255) transparent++;
588 else {
589 if(a==0) opaque++;
590 else semitransparent++;
591 l[x*4+3]=(int)r*a/255;
592 l[x*4+2]=(int)g*a/255;
593 l[x*4+1]=(int)b*a/255;
597 if(semitransparent || opaque) {
598 t->id = ST_DEFINEBITSLOSSLESS2;
601 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
602 free(data2);
603 } else if(header.mode == 0 || header.mode == 3) {
604 RGBA*rgba;
605 int swf_width = BYTES_PER_SCANLINE(header.width);
606 U8*data2 = malloc(swf_width*header.height);
607 U8*tmpline = malloc(header.width);
608 int i,x,y;
609 int pos=0;
610 if(header.mode == 3) { // palette or grayscale?
611 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
612 if(!palette) {
613 fprintf(stderr, "Error: No palette found!\n");
614 exit(1);
616 /* 24->32 bit conversion */
617 for(i=0;i<palettelen;i++) {
618 rgba[i].r = palette[i*3+0];
619 rgba[i].g = palette[i*3+1];
620 rgba[i].b = palette[i*3+2];
621 if(alphapalette && i<alphapalettelen) {
622 rgba[i].a = alphapalette[i];
623 rgba[i].r = ((int)rgba[i].r*rgba[i].a)/255;
624 rgba[i].g = ((int)rgba[i].g*rgba[i].a)/255;
625 rgba[i].b = ((int)rgba[i].b*rgba[i].a)/255;
626 } else {
627 rgba[i].a = 255;
630 } else {
631 palettelen = 256;
632 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
633 for(i=0;i<256;i++) {
634 rgba[i].r = i;
635 rgba[i].g = i;
636 rgba[i].b = i;
640 for(y=0;y<header.height;y++) {
641 int mode = imagedata[pos++]; //filter mode
642 U8*old;
643 U8*dest = &data2[y*swf_width];
644 U8*src;
645 src = &imagedata[pos];
646 if(header.bpp == 8) {
647 pos+=header.width;
648 } else {
649 int x,s=0;
650 int bitpos = 0;
651 U32 v = (1<<header.bpp)-1;
652 for(x=0;x<header.width;x++) {
653 U32 r = src[s/8]<<8 |
654 src[s/8+1];
655 int t;
656 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
657 s+=header.bpp;
659 src = tmpline;
660 pos+=(header.width*header.bpp+7)/8;
663 if(!y) {
664 memset(data2,0,swf_width);
665 old = &data2[y*swf_width];
666 } else {
667 old = &data2[(y-1)*swf_width];
669 applyfilter1(mode, src, old, dest, header.width);
671 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
672 free(tmpline);
673 free(rgba);
674 free(data2);
677 t = swf_InsertTag(t, ST_DEFINESHAPE3);
679 swf_ShapeNew(&s);
680 swf_GetMatrix(NULL, &m);
681 m.sx = 20 * 0x10000;
682 m.sy = 20 * 0x10000;
683 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
685 swf_SetU16(t, id + 1); // id
687 r.xmin = r.ymin = 0;
688 r.xmax = header.width * 20;
689 r.ymax = header.height * 20;
690 swf_SetRect(t, &r);
692 swf_SetShapeHeader(t, s);
694 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
695 swf_ShapeSetLine(t, s, r.xmax, 0);
696 swf_ShapeSetLine(t, s, 0, r.ymax);
697 swf_ShapeSetLine(t, s, -r.xmax, 0);
698 swf_ShapeSetLine(t, s, 0, -r.ymax);
700 swf_ShapeSetEnd(t);
702 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
703 swf_SetU16(t, 50); // depth
705 t = swf_InsertTag(t, ST_PLACEOBJECT2);
707 swf_GetMatrix(NULL, &m);
708 m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
709 m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
710 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
712 t = swf_InsertTag(t, ST_SHOWFRAME);
714 fclose(fi);
716 return t;
720 int CheckInputFile(char *fname, char **realname)
722 FILE *fi;
723 char *s = malloc(strlen(fname) + 5);
724 struct png_header head;
726 if (!s)
727 exit(2);
728 (*realname) = s;
729 strcpy(s, fname);
731 // Check whether file exists (with typical extensions)
733 if ((fi = fopen(s, "rb")) == NULL) {
734 sprintf(s, "%s.png", fname);
735 if ((fi = fopen(s, "rb")) == NULL) {
736 sprintf(s, "%s.PNG", fname);
737 if ((fi = fopen(s, "rb")) == NULL) {
738 sprintf(s, "%s.Png", fname);
739 if ((fi = fopen(s, "rb")) == NULL)
740 fprintf(stderr, "Couldn't open %s!\n", fname);
741 return -1;
746 if(!png_read_header(fi, &head)) {
747 fprintf(stderr, "%s is not a PNG file!\n", fname);
748 return -1;
751 if (global.max_image_width < head.width)
752 global.max_image_width = head.width;
753 if (global.max_image_height < head.height)
754 global.max_image_height = head.height;
756 fclose(fi);
758 return 0;
761 int args_callback_option(char *arg, char *val)
763 int res = 0;
764 if (arg[1])
765 res = -1;
766 else
767 switch (arg[0]) {
768 case 'r':
769 if (val)
770 global.framerate = atof(val);
771 if ((global.framerate < 1.0/256) ||(global.framerate >= 256.0)) {
772 if (VERBOSE(1))
773 fprintf(stderr,
774 "Error: You must specify a valid framerate between 1/256 and 255.\n");
775 exit(1);
777 res = 1;
778 break;
780 case 'o':
781 if (val)
782 global.outfile = val;
783 res = 1;
784 break;
786 case 'C':
787 global.do_cgi = 1;
788 break;
790 case 'v':
791 if (val)
792 global.verbose = atoi(val);
793 res = 1;
794 break;
796 case 'X':
797 if (val)
798 global.force_width = atoi(val);
799 res = 1;
800 break;
802 case 'Y':
803 if (val)
804 global.force_height = atoi(val);
805 res = 1;
806 break;
808 case 'V':
809 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
810 exit(0);
812 default:
813 res = -1;
814 break;
817 if (res < 0) {
818 if (VERBOSE(1))
819 fprintf(stderr, "Unknown option: -%s\n", arg);
820 exit(1);
821 return 0;
823 return res;
826 static struct options_t options[] = {
827 {"r", "rate"},
828 {"o", "output"},
829 {"X", "pixel"},
830 {"Y", "pixel"},
831 {"v", "verbose"},
832 {"C", "cgi"},
833 {"V", "version"},
834 {0,0}
837 int args_callback_longoption(char *name, char *val)
839 return args_long2shortoption(options, name, val);
842 int args_callback_command(char *arg, char *next) // actually used as filename
844 char *s;
845 if (CheckInputFile(arg, &s) < 0) {
846 if (VERBOSE(1))
847 fprintf(stderr, "Error opening input file: %s\n", arg);
848 free(s);
849 } else {
850 image[global.nfiles].filename = s;
851 global.nfiles++;
852 if (global.nfiles >= MAX_INPUT_FILES) {
853 if (VERBOSE(1))
854 fprintf(stderr, "Error: Too many input files.\n");
855 exit(1);
858 return 0;
861 void args_callback_usage(char *name)
863 printf("\n");
864 printf("Usage: %s [-X width] [-Y height] [-o file.swf] [-r rate] file1.png [file2.png...]\n", name);
865 printf("\n");
866 printf("-r , --rate <framerate> Set movie framerate (frames per second)\n");
867 printf("-o , --output <filename> Set name for SWF output file.\n");
868 printf("-X , --pixel <width> Force movie width to <width> (default: autodetect)\n");
869 printf("-Y , --pixel <height> Force movie height to <height> (default: autodetect)\n");
870 printf("-v , --verbose <level> Set verbose level (0=quiet, 1=default, 2=debug)\n");
871 printf("-C , --cgi For use as CGI- prepend http header, write to stdout\n");
872 printf("-V , --version Print version information and exit\n");
873 printf("\n");
876 int main(int argc, char **argv)
878 SWF swf;
879 TAG *t;
881 memset(&global, 0x00, sizeof(global));
883 global.framerate = 1.0;
884 global.verbose = 1;
886 processargs(argc, argv);
888 if(global.nfiles<=0)
889 return 1;
891 if (VERBOSE(2))
892 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
894 t = MovieStart(&swf, global.framerate,
895 global.force_width ? global.force_width : global.
896 max_image_width,
897 global.force_height ? global.force_height : global.
898 max_image_height);
901 int i;
902 for (i = 0; i < global.nfiles; i++) {
903 if (VERBOSE(3))
904 fprintf(stderr, "[%03i] %s\n", i,
905 image[i].filename);
906 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
907 free(image[i].filename);
911 MovieFinish(&swf, t, global.outfile);
913 return 0;