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 */
27 #include "../lib/rfxswf.h"
28 #include "../lib/args.h"
30 #define MAX_INPUT_FILES 1024
31 #define VERBOSE(x) (global.verbose>=x)
49 } image[MAX_INPUT_FILES];
51 static int custom_move=0;
54 static int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
55 static int custom_clip = 0;
57 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
62 memset(swf, 0x00, sizeof(SWF));
64 swf->fileVersion = global.version;
65 swf->frameRate = (int)(256.0 * framerate);
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;
72 swf->movieSize.xmax = dx * 20;
73 swf->movieSize.ymax = dy * 20;
76 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
78 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
79 //rgb.g = 0xff; //<--- handy for testing alpha conversion
85 int MovieFinish(SWF * swf, TAG * t, char *sname)
87 int f, so = fileno(stdout);
88 t = swf_InsertTag(t, ST_END);
90 if ((!isatty(so)) && (!sname))
95 f = open(sname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
99 if FAILED(swf_WriteCGI(swf)) fprintf(stderr,"WriteCGI() failed.\n");
101 if(global.version >= 6) {
102 if (swf_WriteSWC(f, swf)<0)
103 fprintf(stderr, "Unable to write output file: %s\n", sname);
105 if (swf_WriteSWF(f, swf)<0)
106 fprintf(stderr, "Unable to write output file: %s\n", sname);
116 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
119 if(destlen) *destlen=0;
120 if(destdata) *destdata=0;
121 if(!fread(&len, 4, 1, fi))
123 if(!fread(head, 4, 1, fi))
125 len = REVERSESWAP32(len);
126 if(destlen) *destlen = len;
129 *destdata = malloc(len);
132 if(!fread(*destdata, len, 1, fi)) {
134 if(destlen) *destlen=0;
137 fseek(fi, 4, SEEK_CUR);
140 fseek(fi, len+4, SEEK_CUR);
145 unsigned int png_get_dword(FILE*fi)
149 return REVERSESWAP32(a);
160 int png_read_header(FILE*fi, struct png_header*header)
165 U8 head[8] = {137,80,78,71,13,10,26,10};
169 if(strncmp(head,head2,4))
172 while(png_read_chunk(&id, &len, &data, fi))
175 printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
176 if(!strncasecmp(id, "IHDR", 4)) {
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)
184 c = data[10]; // compression mode (0)
185 f = data[11]; // filter mode (0)
186 i = data[12]; // interlace mode (0)
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);
194 if(b!=0 && b!=2 && b!=3 && b!=6) {
195 fprintf(stderr, "Image mode %d not supported!\n", b);
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");
202 if(a!=8 && (b==2 || b==6)) {
203 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
207 fprintf(stderr, "Compression mode %d not supported!\n", c);
211 fprintf(stderr, "Filter mode %d not supported!\n", f);
215 fprintf(stderr, "Interlace mode %d not supported!\n", i);
219 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
230 typedef unsigned char byte;
231 #define ABS(a) ((a)>0?(a):(-(a)))
232 byte inline PaethPredictor (byte a,byte b,byte c)
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
239 // return nearest of a,b,c,
240 // breaking ties in order a,b,c.
241 if (pa <= pb && pa <= pc)
248 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
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;
259 for(x=0;x<width;x++) {
269 for(x=0;x<width;x++) {
271 dest[1] = src[0]+lastr;
272 dest[2] = src[1]+lastg;
273 dest[3] = src[2]+lastb;
282 for(x=0;x<width;x++) {
284 dest[1] = src[0]+old[1];
285 dest[2] = src[1]+old[2];
286 dest[3] = src[2]+old[3];
293 for(x=0;x<width;x++) {
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;
307 for(x=0;x<width;x++) {
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);
326 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
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?
339 for(x=0;x<width;x++) {
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;
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];
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;
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);
410 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
413 unsigned char last=0;
414 unsigned char upperlast=0;
417 for(x=0;x<width;x++) {
424 for(x=0;x<width;x++) {
432 for(x=0;x<width;x++) {
440 for(x=0;x<width;x++) {
441 *dest = *src+(*old+last)/2;
449 for(x=0;x<width;x++) {
450 *dest = *src+PaethPredictor(last,*old,upperlast);
461 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
473 unsigned long int imagedatalen;
474 unsigned long int zimagedatalen=0;
478 int alphapalettelen = 0;
479 struct png_header header;
485 if ((fi = fopen(sname, "rb")) == NULL) {
487 fprintf(stderr, "Read access failed: %s\n", sname);
491 if(!png_read_header(fi, &header))
494 if(header.mode == 3 || header.mode == 0) bypp = 1;
496 if(header.mode == 2) bypp = 3;
498 if(header.mode == 6) bypp = 4;
501 imagedatalen = bypp * header.width * header.height + 65536;
502 imagedata = malloc(imagedatalen);
504 fseek(fi,8,SEEK_SET);
507 if(!png_read_chunk(&tagid, &len, &data, fi))
509 if(!strncmp(tagid, "IEND", 4)) {
512 if(!strncmp(tagid, "PLTE", 4)) {
515 data = 0; //don't free data
517 printf("%d colors in palette\n", palettelen);
519 if(!strncmp(tagid, "tRNS", 4)) {
520 if(header.mode == 3) {
522 alphapalettelen = len;
523 data = 0; //don't free data
525 printf("found %d alpha colors\n", alphapalettelen);
528 if(!strncmp(tagid, "IDAT", 4)) {
531 zimagedata = malloc(len);
532 memcpy(zimagedata,data,len);
534 zimagedata = realloc(zimagedata, zimagedatalen+len);
535 memcpy(&zimagedata[zimagedatalen], data, len);
536 zimagedatalen += len;
543 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
544 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
552 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
554 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
556 swf_SetU16(t, id); // id
557 if(header.mode == 2 || header.mode == 6) {
558 U8*data2 = malloc(header.width*header.height*4);
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);
568 for(y=0;y<header.height;y++) {
569 int mode = imagedata[pos++]; //filter mode
573 dest = &data2[(y*header.width)*4];
577 /* one byte per pixel */
578 src = &imagedata[pos];
579 pos+=header.width*(header.mode==6?4:3);
581 /* not implemented yet */
587 memset(old, 0, header.width*4); //TODO: fill alpha with 255?
589 old = &data2[(y-1)*header.width*4];
592 applyfilter4(mode, src, old, dest, header.width);
593 else if(header.mode==2)
594 applyfilter3(mode, src, old, dest, header.width);
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++) {
608 if(a==255) transparent++;
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;
618 if(semitransparent || opaque) {
619 t->id = ST_DEFINEBITSLOSSLESS2;
622 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
624 } else if(header.mode == 0 || header.mode == 3) {
626 int swf_width = BYTES_PER_SCANLINE(header.width);
627 U8*data2 = malloc(swf_width*header.height);
628 U8*tmpline = malloc(header.width);
631 if(header.mode == 3) { // palette or grayscale?
632 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
634 fprintf(stderr, "Error: No palette found!\n");
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;
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++) {
662 for(y=0;y<header.height;y++) {
663 int mode = imagedata[pos++]; //filter mode
665 U8*dest = &data2[y*swf_width];
667 src = &imagedata[pos];
668 if(header.bpp == 8) {
673 U32 v = (1<<header.bpp)-1;
674 for(x=0;x<header.width;x++) {
675 U32 r = src[s/8]<<8 |
678 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
682 pos+=(header.width*header.bpp+7)/8;
686 memset(data2,0,swf_width);
687 old = &data2[y*swf_width];
689 old = &data2[(y-1)*swf_width];
691 applyfilter1(mode, src, old, dest, header.width);
693 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
699 t = swf_InsertTag(t, ST_DEFINESHAPE3);
702 swf_GetMatrix(NULL, &m);
703 m.sx = (int)(20 * 0x10000);
704 m.sy = (int)(20 * 0x10000);
707 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 1);
709 swf_SetU16(t, id + 1); // id
712 r.xmax = header.width * 20;
713 r.ymax = header.height * 20;
716 swf_SetShapeHeader(t, s);
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);
726 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
727 swf_SetU16(t, 50); // depth
729 t = swf_InsertTag(t, ST_PLACEOBJECT2);
731 swf_GetMatrix(NULL, &m);
732 m.sx = (int)(0x10000 * global.scale);
733 m.sy = (int)(0x10000 * global.scale);
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;
742 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
744 t = swf_InsertTag(t, ST_SHOWFRAME);
752 int CheckInputFile(char *fname, char **realname)
755 char *s = malloc(strlen(fname) + 5);
756 struct png_header head;
763 // Check whether file exists (with typical extensions)
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);
779 if(!png_read_header(fi, &head)) {
780 fprintf(stderr, "%s is not a PNG file!\n", fname);
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;
794 int args_callback_option(char *arg, char *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)) {
809 "Error: You must specify a valid framerate between 1/256 and 255.\n");
817 global.outfile = val;
822 global.scale = atof(val)/100;
832 global.version = atoi(val);
854 global.force_width = atoi(val);
860 global.force_height = atoi(val);
865 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
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");
890 char*s = strdup(val);
891 char*c = strchr(s, ':');
893 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
913 fprintf(stderr, "Unknown option: -%s\n", arg);
920 static struct options_t options[] = {
933 int args_callback_longoption(char *name, char *val)
935 return args_long2shortoption(options, name, val);
938 int args_callback_command(char *arg, char *next) // actually used as filename
941 if (CheckInputFile(arg, &s) < 0) {
943 fprintf(stderr, "Error opening input file: %s\n", arg);
946 image[global.nfiles].filename = s;
948 if (global.nfiles >= MAX_INPUT_FILES) {
950 fprintf(stderr, "Error: Too many input files.\n");
957 void args_callback_usage(char *name)
960 printf("Usage: %s [-X width] [-Y height] [-o file.swf] [-r rate] file1.png [file2.png...]\n", name);
962 printf("-r , --rate <framerate> Set movie framerate (frames per second)\n");
963 printf("-o , --output <filename> Set name for SWF output file.\n");
964 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
965 printf("-X , --pixel <width> Force movie width to <width> (default: autodetect)\n");
966 printf("-Y , --pixel <height> Force movie height to <height> (default: autodetect)\n");
967 printf("-v , --verbose Be verbose. Use more than one -v for greater effect \n");
968 printf("-q , --quiet Omit normal log messages, only log errors\n");
969 printf("-C , --cgi For use as CGI- prepend http header, write to stdout\n");
970 printf("-V , --version Print version information and exit\n");
974 int main(int argc, char **argv)
979 memset(&global, 0x00, sizeof(global));
981 global.framerate = 1.0;
986 processargs(argc, argv);
988 if(global.nfiles<=0) {
989 fprintf(stderr, "No png files found in arguments\n");
994 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
996 t = MovieStart(&swf, global.framerate,
997 global.force_width ? global.force_width : (int)(global.max_image_width*global.scale),
998 global.force_height ? global.force_height : (int)(global.max_image_height*global.scale));
1002 for (i = 0; i < global.nfiles; i++) {
1004 fprintf(stderr, "[%03i] %s\n", i,
1006 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
1007 free(image[i].filename);
1011 MovieFinish(&swf, t, global.outfile);