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;
487 if ((fi = fopen(sname, "rb")) == NULL) {
489 fprintf(stderr, "Read access failed: %s\n", sname);
493 if(!png_read_header(fi, &header))
496 if(header.mode == 3 || header.mode == 0) bypp = 1;
498 if(header.mode == 2) bypp = 3;
500 if(header.mode == 6) bypp = 4;
503 imagedatalen = bypp * header.width * header.height + 65536;
504 imagedata = malloc(imagedatalen);
506 fseek(fi,8,SEEK_SET);
509 if(!png_read_chunk(&tagid, &len, &data, fi))
511 if(!strncmp(tagid, "IEND", 4)) {
514 if(!strncmp(tagid, "PLTE", 4)) {
516 palettelen = len/bypp;
517 data = 0; //don't free data
519 printf("%d colors in palette\n", palettelen);
521 if(!strncmp(tagid, "tRNS", 4)) {
522 if(header.mode == 3) {
524 alphapalettelen = len;
525 data = 0; //don't free data
527 printf("found %d alpha colors\n", alphapalettelen);
528 } else if(header.mode == 0) {
530 if(header.mode == 3) { // palette or grayscale?
531 alphacolor[0] = data[1];
532 alphacolor[1] = data[3];
533 alphacolor[2] = data[5];
535 alphacolor[0] = alphacolor[1] = alphacolor[2] = data[1];
538 printf("found alpha color: %02x%02x%02x\n", alphacolor[0], alphacolor[1], alphacolor[2]);
542 printf("ignoring tRNS %d entry (%d bytes)\n", header.mode, len);
545 if(!strncmp(tagid, "IDAT", 4)) {
548 zimagedata = malloc(len);
549 memcpy(zimagedata,data,len);
551 zimagedata = realloc(zimagedata, zimagedatalen+len);
552 memcpy(&zimagedata[zimagedatalen], data, len);
553 zimagedatalen += len;
560 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
561 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
568 if(alphapalette || hasalphacolor)
569 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
571 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
573 swf_SetU16(t, id); // id
574 if(header.mode == 2 || header.mode == 6) {
575 U8*data2 = malloc(header.width*header.height*4);
581 int semitransparent=0;
582 /* in case for mode 2, the following also performs 24->32 bit conversion */
583 unsigned char* firstline = malloc(header.width*4);
585 for(y=0;y<header.height;y++) {
586 int mode = imagedata[pos++]; //filter mode
590 dest = &data2[(y*header.width)*4];
594 /* one byte per pixel */
595 src = &imagedata[pos];
596 pos+=header.width*(header.mode==6?4:3);
598 /* not implemented yet */
604 memset(old, 0, header.width*4); //TODO: fill alpha with 255?
606 old = &data2[(y-1)*header.width*4];
609 applyfilter4(mode, src, old, dest, header.width);
610 else if(header.mode==2)
611 applyfilter3(mode, src, old, dest, header.width);
615 /* the image is now compressed and stored in data. Now let's take
616 a look at the alpha values */
617 if(header.mode == 6) {
618 for(y=0;y<header.height;y++) {
619 U8*l = &data2[(y*header.width)*4];
620 for(x=0;x<header.width;x++) {
625 if(a==255) transparent++;
628 else semitransparent++;
629 l[x*4+3]=(int)r*a/255;
630 l[x*4+2]=(int)g*a/255;
631 l[x*4+1]=(int)b*a/255;
635 if(semitransparent || opaque) {
636 t->id = ST_DEFINEBITSLOSSLESS2;
639 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
641 } else if(header.mode == 0 || header.mode == 3) {
643 int swf_width = BYTES_PER_SCANLINE(header.width);
644 U8*data2 = malloc(swf_width*header.height);
645 U8*tmpline = malloc(header.width);
648 if(header.mode == 3) { // palette or grayscale?
649 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
651 fprintf(stderr, "Error: No palette found!\n");
654 /* 24->32 bit conversion */
655 for(i=0;i<palettelen;i++) {
656 rgba[i].r = palette[i*3+0];
657 rgba[i].g = palette[i*3+1];
658 rgba[i].b = palette[i*3+2];
659 if(alphapalette && i<alphapalettelen) {
660 rgba[i].a = alphapalette[i];
661 rgba[i].r = ((int)rgba[i].r*rgba[i].a)/255;
662 rgba[i].g = ((int)rgba[i].g*rgba[i].a)/255;
663 rgba[i].b = ((int)rgba[i].b*rgba[i].a)/255;
668 if(rgba[i].r == alphacolor[0] &&
669 rgba[i].g == alphacolor[1] &&
670 rgba[i].b == alphacolor[2]) {
679 int mult = (0x1ff>>header.bpp);
680 palettelen = 1<<header.bpp;
681 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
682 for(i=0;i<palettelen;i++) {
688 if(rgba[i].r == alphacolor[0]) {
698 for(y=0;y<header.height;y++) {
699 int mode = imagedata[pos++]; //filter mode
701 U8*dest = &data2[y*swf_width];
703 src = &imagedata[pos];
704 if(header.bpp == 8) {
709 U32 v = (1<<header.bpp)-1;
710 for(x=0;x<header.width;x++) {
711 U32 r = src[s/8]<<8 |
714 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
718 pos+=(header.width*header.bpp+7)/8;
722 memset(data2,0,swf_width);
723 old = &data2[y*swf_width];
725 old = &data2[(y-1)*swf_width];
727 applyfilter1(mode, src, old, dest, header.width);
729 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
735 t = swf_InsertTag(t, ST_DEFINESHAPE3);
738 swf_GetMatrix(NULL, &m);
739 m.sx = (int)(20 * 0x10000);
740 m.sy = (int)(20 * 0x10000);
743 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 1);
745 swf_SetU16(t, id + 1); // id
748 r.xmax = header.width * 20;
749 r.ymax = header.height * 20;
752 swf_SetShapeHeader(t, s);
754 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
755 swf_ShapeSetLine(t, s, r.xmax, 0);
756 swf_ShapeSetLine(t, s, 0, r.ymax);
757 swf_ShapeSetLine(t, s, -r.xmax, 0);
758 swf_ShapeSetLine(t, s, 0, -r.ymax);
762 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
763 swf_SetU16(t, 50); // depth
765 t = swf_InsertTag(t, ST_PLACEOBJECT2);
767 swf_GetMatrix(NULL, &m);
768 m.sx = (int)(0x10000 * global.scale);
769 m.sy = (int)(0x10000 * global.scale);
775 m.tx = (swf->movieSize.xmax - (int) (header.width * global.scale * 20)) / 2;
776 m.ty = (swf->movieSize.ymax - (int) (header.height * global.scale * 20)) / 2;
778 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
780 t = swf_InsertTag(t, ST_SHOWFRAME);
788 int CheckInputFile(char *fname, char **realname)
791 char *s = malloc(strlen(fname) + 5);
792 struct png_header head;
799 // Check whether file exists (with typical extensions)
801 if ((fi = fopen(s, "rb")) == NULL) {
802 sprintf(s, "%s.png", fname);
803 if ((fi = fopen(s, "rb")) == NULL) {
804 sprintf(s, "%s.PNG", fname);
805 if ((fi = fopen(s, "rb")) == NULL) {
806 sprintf(s, "%s.Png", fname);
807 if ((fi = fopen(s, "rb")) == NULL) {
808 fprintf(stderr, "Couldn't open %s!\n", fname);
815 if(!png_read_header(fi, &head)) {
816 fprintf(stderr, "%s is not a PNG file!\n", fname);
820 if (global.max_image_width < head.width)
821 global.max_image_width = head.width;
822 if (global.max_image_height < head.height)
823 global.max_image_height = head.height;
830 int args_callback_option(char *arg, char *val)
839 global.framerate = atof(val);
840 /* removed framerate>0 restriction in order to make
841 Flash Communication Server compatible SWFs */
842 if ((global.framerate < 0) ||(global.framerate >= 256.0)) {
845 "Error: You must specify a valid framerate between 1/256 and 255.\n");
853 global.outfile = val;
858 global.scale = atof(val)/100;
868 global.version = atoi(val);
890 global.force_width = atoi(val);
896 global.force_height = atoi(val);
901 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
905 char*s = strdup(val);
906 char*x1 = strtok(s, ":");
907 char*y1 = strtok(0, ":");
908 char*x2 = strtok(0, ":");
909 char*y2 = strtok(0, ":");
910 if(!(x1 && y1 && x2 && y2)) {
911 fprintf(stderr, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
926 char*s = strdup(val);
927 char*c = strchr(s, ':');
929 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
949 fprintf(stderr, "Unknown option: -%s\n", arg);
956 static struct options_t options[] = {
970 int args_callback_longoption(char *name, char *val)
972 return args_long2shortoption(options, name, val);
975 int args_callback_command(char *arg, char *next) // actually used as filename
978 if (CheckInputFile(arg, &s) < 0) {
980 fprintf(stderr, "Error opening input file: %s\n", arg);
983 image[global.nfiles].filename = s;
985 if (global.nfiles >= MAX_INPUT_FILES) {
987 fprintf(stderr, "Error: Too many input files.\n");
994 void args_callback_usage(char *name)
997 printf("Usage: %s [-X width] [-Y height] [-o file.swf] [-r rate] file1.png [file2.png...]\n", name);
999 printf("-r , --rate <framerate> Set movie framerate (frames per second)\n");
1000 printf("-o , --output <filename> Set name for SWF output file.\n");
1001 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
1002 printf("-X , --pixel <width> Force movie width to <width> (default: autodetect)\n");
1003 printf("-Y , --pixel <height> Force movie height to <height> (default: autodetect)\n");
1004 printf("-v , --verbose <level> Set verbose level (0=quiet, 1=default, 2=debug)\n");
1005 printf("-q , --quiet Omit normal log messages, only log errors\n");
1006 printf("-C , --cgi For use as CGI- prepend http header, write to stdout\n");
1007 printf("-V , --version Print version information and exit\n");
1008 printf("-s , --scale <percent> Scale image to <percent>% size.\n");
1012 int main(int argc, char **argv)
1017 memset(&global, 0x00, sizeof(global));
1019 global.framerate = 1.0;
1024 processargs(argc, argv);
1026 if(global.nfiles<=0) {
1027 fprintf(stderr, "No png files found in arguments\n");
1032 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
1034 t = MovieStart(&swf, global.framerate,
1035 global.force_width ? global.force_width : (int)(global.max_image_width*global.scale),
1036 global.force_height ? global.force_height : (int)(global.max_image_height*global.scale));
1040 for (i = 0; i < global.nfiles; i++) {
1042 fprintf(stderr, "[%03i] %s\n", i,
1044 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
1045 free(image[i].filename);
1049 MovieFinish(&swf, t, global.outfile);