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"
29 #include "../lib/png.h"
31 #define MAX_INPUT_FILES 1024
32 #define VERBOSE(x) (global.verbose>=x)
51 } image[MAX_INPUT_FILES];
53 static int custom_move=0;
56 static int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
57 static int custom_clip = 0;
59 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
64 memset(swf, 0x00, sizeof(SWF));
66 swf->fileVersion = global.version;
67 swf->frameRate = (int)(256.0 * framerate);
69 swf->movieSize.xmin = clip_x1 * 20;
70 swf->movieSize.ymin = clip_y1 * 20;
71 swf->movieSize.xmax = clip_x2 * 20;
72 swf->movieSize.ymax = clip_y2 * 20;
74 swf->movieSize.xmax = dx * 20;
75 swf->movieSize.ymax = dy * 20;
78 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
80 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
81 //rgb.g = 0xff; //<--- handy for testing alpha conversion
87 int MovieFinish(SWF * swf, TAG * t, char *sname)
89 int f, so = fileno(stdout);
90 t = swf_InsertTag(t, ST_END);
92 if ((!isatty(so)) && (!sname))
97 f = open(sname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
101 if FAILED(swf_WriteCGI(swf)) fprintf(stderr,"WriteCGI() failed.\n");
103 if (swf_WriteSWF(f, swf)<0)
104 fprintf(stderr, "Unable to write output file: %s\n", sname);
113 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
116 if(destlen) *destlen=0;
117 if(destdata) *destdata=0;
118 if(!fread(&len, 4, 1, fi))
120 if(!fread(head, 4, 1, fi))
122 len = BE_32_TO_NATIVE(len);
123 if(destlen) *destlen = len;
126 *destdata = malloc(len);
129 if(!fread(*destdata, len, 1, fi)) {
131 if(destlen) *destlen=0;
134 fseek(fi, 4, SEEK_CUR);
137 fseek(fi, len+4, SEEK_CUR);
142 unsigned int png_get_dword(FILE*fi)
146 return BE_32_TO_NATIVE(a);
157 int png_read_header(FILE*fi, struct png_header*header)
162 U8 head[8] = {137,80,78,71,13,10,26,10};
166 if(strncmp((char*)head,(char*)head2,4))
169 while(png_read_chunk(&id, &len, &data, fi))
172 printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
173 if(!strncasecmp(id, "IHDR", 4)) {
176 header->width = BE_32_TO_NATIVE(*(U32*)&data[0]);
177 header->height = BE_32_TO_NATIVE(*(U32*)&data[4]);
178 a = data[8]; // should be 8
179 b = data[9]; // should be 3(indexed), 2(rgb), 0(grayscale) or 6(truecolor+alpha)
181 c = data[10]; // compression mode (0)
182 f = data[11]; // filter mode (0)
183 i = data[12]; // interlace mode (0)
185 if(VERBOSE(2)) printf("image mode:%d\n", b);
186 if(VERBOSE(2)) printf("bpp: %d\n", a);
187 if(VERBOSE(2)) printf("compression: %d\n", c);
188 if(VERBOSE(2)) printf("filter: %d\n", f);
189 if(VERBOSE(2)) printf("interlace: %d\n", i);
191 if(b!=0 && b!=2 && b!=3 && b!=6) {
192 fprintf(stderr, "Image mode %d not supported!\n", b);
194 fprintf(stderr, "(This is a grayscale image with alpha channel-\n");
195 fprintf(stderr, " try converting it into an RGB image with alpha channel)\n");
199 if(a!=8 && (b==2 || b==6)) {
200 fprintf(stderr, "Bpp %d in mode %d not supported!\n", b, a);
204 fprintf(stderr, "Compression mode %d not supported!\n", c);
208 fprintf(stderr, "Filter mode %d not supported!\n", f);
212 fprintf(stderr, "Interlace mode %d not supported!\n", i);
216 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
227 typedef unsigned char byte;
228 #define ABS(a) ((a)>0?(a):(-(a)))
229 byte inline PaethPredictor (byte a,byte b,byte c)
231 // a = left, b = above, c = upper left
232 int p = a + b - c; // initial estimate
233 int pa = ABS(p - a); // distances to a, b, c
236 // return nearest of a,b,c,
237 // breaking ties in order a,b,c.
238 if (pa <= pb && pa <= pc)
245 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
248 unsigned char lastr=0;
249 unsigned char lastg=0;
250 unsigned char lastb=0;
251 unsigned char upperlastr=0;
252 unsigned char upperlastg=0;
253 unsigned char upperlastb=0;
256 for(x=0;x<width;x++) {
266 for(x=0;x<width;x++) {
268 dest[1] = src[0]+lastr;
269 dest[2] = src[1]+lastg;
270 dest[3] = src[2]+lastb;
279 for(x=0;x<width;x++) {
281 dest[1] = src[0]+old[1];
282 dest[2] = src[1]+old[2];
283 dest[3] = src[2]+old[3];
290 for(x=0;x<width;x++) {
292 dest[1] = src[0]+(old[1]+lastr)/2;
293 dest[2] = src[1]+(old[2]+lastg)/2;
294 dest[3] = src[2]+(old[3]+lastb)/2;
304 for(x=0;x<width;x++) {
306 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
307 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
308 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
322 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
325 unsigned char lastr=0;
326 unsigned char lastg=0;
327 unsigned char lastb=0;
328 unsigned char lasta=0; //TODO: 255?
329 unsigned char upperlastr=0;
330 unsigned char upperlastg=0;
331 unsigned char upperlastb=0;
332 unsigned char upperlasta=0; //TODO: 255?
335 for(x=0;x<width;x++) {
345 for(x=0;x<width;x++) {
346 dest[0] = src[3]+lasta;
347 dest[1] = src[0]+lastr;
348 dest[2] = src[1]+lastg;
349 dest[3] = src[2]+lastb;
359 for(x=0;x<width;x++) {
360 dest[0] = src[3]+old[0];
361 dest[1] = src[0]+old[1];
362 dest[2] = src[1]+old[2];
363 dest[3] = src[2]+old[3];
370 for(x=0;x<width;x++) {
371 dest[0] = src[3]+(old[0]+lasta)/2;
372 dest[1] = src[0]+(old[1]+lastr)/2;
373 dest[2] = src[1]+(old[2]+lastg)/2;
374 dest[3] = src[2]+(old[3]+lastb)/2;
385 for(x=0;x<width;x++) {
386 dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
387 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
388 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
389 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
406 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
409 unsigned char last=0;
410 unsigned char upperlast=0;
413 for(x=0;x<width;x++) {
420 for(x=0;x<width;x++) {
428 for(x=0;x<width;x++) {
436 for(x=0;x<width;x++) {
437 *dest = *src+(*old+last)/2;
445 for(x=0;x<width;x++) {
446 *dest = *src+PaethPredictor(last,*old,upperlast);
457 TAG* PNG2Image(TAG*t, U16 id, char*filename, unsigned*width, unsigned*height)
463 unsigned long int imagedatalen;
464 unsigned long int zimagedatalen=0;
468 int alphapalettelen = 0;
469 struct png_header header;
479 if ((fi = fopen(filename, "rb")) == NULL) {
481 fprintf(stderr, "Read access failed: %s\n", filename);
485 if(!png_read_header(fi, &header))
488 *width = header.width;
489 *height = header.height;
491 if(header.mode == 3 || header.mode == 0) bypp = 1;
493 if(header.mode == 2) bypp = 3;
495 if(header.mode == 6) bypp = 4;
498 imagedatalen = bypp * header.width * header.height + 65536;
499 imagedata = malloc(imagedatalen);
501 fseek(fi,8,SEEK_SET);
504 if(!png_read_chunk(&tagid, &len, &data, fi))
506 if(!strncmp(tagid, "IEND", 4)) {
509 if(!strncmp(tagid, "PLTE", 4)) {
512 data = 0; //don't free data
514 printf("%d colors in palette\n", palettelen);
516 if(!strncmp(tagid, "tRNS", 4)) {
517 if(header.mode == 3) {
519 alphapalettelen = len;
520 data = 0; //don't free data
522 printf("found %d alpha colors\n", alphapalettelen);
523 } else if(header.mode == 0 || header.mode == 2) {
525 if(header.mode == 2) { // palette or grayscale?
526 alphacolor[0] = data[1];
527 alphacolor[1] = data[3];
528 alphacolor[2] = data[5];
530 alphacolor[0] = alphacolor[1] = alphacolor[2] = data[1];
533 printf("found alpha color: %02x%02x%02x\n", alphacolor[0], alphacolor[1], alphacolor[2]);
537 printf("ignoring tRNS %d entry (%d bytes)\n", header.mode, len);
540 if(!strncmp(tagid, "IDAT", 4)) {
543 zimagedata = malloc(len);
544 memcpy(zimagedata,data,len);
546 zimagedata = realloc(zimagedata, zimagedatalen+len);
547 memcpy(&zimagedata[zimagedatalen], data, len);
548 zimagedatalen += len;
555 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
556 fprintf(stderr, "Couldn't uncompress IDAT chunk (%lu bytes) in %s!\n", imagedatalen, filename);
563 if(alphapalette || hasalphacolor)
564 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
566 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
568 swf_SetU16(t, id); // id
569 if(header.mode == 2 || header.mode == 6) {
570 U8*data2 = malloc(header.width*header.height*4);
576 int semitransparent=0;
577 /* in case for mode 2, the following also performs 24->32 bit conversion */
578 unsigned char* firstline = malloc(header.width*4);
580 for(y=0;y<header.height;y++) {
581 int mode = imagedata[pos++]; //filter mode
585 dest = &data2[(y*header.width)*4];
589 /* one byte per pixel */
590 src = &imagedata[pos];
591 pos+=header.width*(header.mode==6?4:3);
593 /* not implemented yet */
599 memset(old, 0, header.width*4); //TODO: fill alpha with 255?
601 old = &data2[(y-1)*header.width*4];
604 applyfilter4(mode, src, old, dest, header.width);
605 } else if(header.mode==2) {
606 applyfilter3(mode, src, old, dest, header.width);
607 /* replace alpha color */
610 for(x=0;x<header.width;x++) {
611 if(dest[x*4+1] == alphacolor[0] &&
612 dest[x*4+2] == alphacolor[1] &&
613 dest[x*4+3] == alphacolor[2]) {
614 *(U32*)&dest[x*4] = 0;
622 /* the image is now compressed and stored in data. Now let's take
623 a look at the alpha values */
624 if(header.mode == 6) {
625 for(y=0;y<header.height;y++) {
626 U8*l = &data2[(y*header.width)*4];
627 for(x=0;x<header.width;x++) {
632 if(a==255) transparent++;
635 else semitransparent++;
636 l[x*4+3]=(int)r*a/255;
637 l[x*4+2]=(int)g*a/255;
638 l[x*4+1]=(int)b*a/255;
642 if(semitransparent || opaque) {
643 t->id = ST_DEFINEBITSLOSSLESS2;
646 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
648 } else if(header.mode == 0 || header.mode == 3) {
650 int swf_width = BYTES_PER_SCANLINE(header.width);
651 U8*data2 = malloc(swf_width*header.height);
652 U8*tmpline = malloc(header.width);
655 if(header.mode == 3) { // palette or grayscale?
656 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
658 fprintf(stderr, "Error: No palette found!\n");
661 /* 24->32 bit conversion */
662 for(i=0;i<palettelen;i++) {
663 rgba[i].r = palette[i*3+0];
664 rgba[i].g = palette[i*3+1];
665 rgba[i].b = palette[i*3+2];
666 if(alphapalette && i<alphapalettelen) {
667 rgba[i].a = alphapalette[i];
668 rgba[i].r = ((int)rgba[i].r*rgba[i].a)/255;
669 rgba[i].g = ((int)rgba[i].g*rgba[i].a)/255;
670 rgba[i].b = ((int)rgba[i].b*rgba[i].a)/255;
675 if(rgba[i].r == alphacolor[0] &&
676 rgba[i].g == alphacolor[1] &&
677 rgba[i].b == alphacolor[2]) {
686 int mult = (0x1ff>>header.bpp);
687 palettelen = 1<<header.bpp;
688 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
689 for(i=0;i<palettelen;i++) {
695 if(rgba[i].r == alphacolor[0]) {
705 for(y=0;y<header.height;y++) {
706 int mode = imagedata[pos++]; //filter mode
708 U8*dest = &data2[y*swf_width];
710 src = &imagedata[pos];
711 if(header.bpp == 8) {
716 U32 v = (1<<header.bpp)-1;
717 for(x=0;x<header.width;x++) {
718 U32 r = src[s/8]<<8 |
721 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
725 pos+=(header.width*header.bpp+7)/8;
729 memset(data2,0,swf_width);
730 old = &data2[y*swf_width];
732 old = &data2[(y-1)*swf_width];
734 applyfilter1(mode, src, old, dest, header.width);
736 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
745 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
752 unsigned width=0, height=0;
757 msg("<warning> No jpeg support compiled in");
763 png_load(sname, &width, &height, (unsigned char**)&data);
766 if(swf_ImageHasAlpha(data, width, height)) {
767 t = swf_InsertTag(t, ST_DEFINEBITSJPEG3);
769 swf_SetJPEGBits3(t, width,height,data,global.mkjpeg);
771 t = swf_InsertTag(t, ST_DEFINEBITSJPEG2);
773 swf_SetJPEGBits2(t, width,height,data,global.mkjpeg);
778 png_load(sname, &width, &height, (unsigned char**)&data);
781 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
783 swf_SetLosslessImage(t, data,width,height);
785 /* old code: transform PNG encoding 1:1 to SWF */
786 t = PNG2Image(t, id, sname, &width, &height);
789 t = swf_InsertTag(t, ST_DEFINESHAPE3);
792 swf_GetMatrix(NULL, &m);
793 m.sx = (int)(20 * 0x10000);
794 m.sy = (int)(20 * 0x10000);
797 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 1);
799 swf_SetU16(t, id + 1); // id
803 r.ymax = height * 20;
806 swf_SetShapeHeader(t, s);
808 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
809 swf_ShapeSetLine(t, s, r.xmax, 0);
810 swf_ShapeSetLine(t, s, 0, r.ymax);
811 swf_ShapeSetLine(t, s, -r.xmax, 0);
812 swf_ShapeSetLine(t, s, 0, -r.ymax);
816 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
817 swf_SetU16(t, 50); // depth
819 t = swf_InsertTag(t, ST_PLACEOBJECT2);
821 swf_GetMatrix(NULL, &m);
822 m.sx = (int)(0x10000 * global.scale);
823 m.sy = (int)(0x10000 * global.scale);
829 m.tx = (swf->movieSize.xmax - (int) (width * global.scale * 20)) / 2;
830 m.ty = (swf->movieSize.ymax - (int) (height * global.scale * 20)) / 2;
832 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
834 t = swf_InsertTag(t, ST_SHOWFRAME);
840 int CheckInputFile(char *fname, char **realname)
843 char *s = malloc(strlen(fname) + 5);
844 struct png_header head;
851 // Check whether file exists (with typical extensions)
853 if ((fi = fopen(s, "rb")) == NULL) {
854 sprintf(s, "%s.png", fname);
855 if ((fi = fopen(s, "rb")) == NULL) {
856 sprintf(s, "%s.PNG", fname);
857 if ((fi = fopen(s, "rb")) == NULL) {
858 sprintf(s, "%s.Png", fname);
859 if ((fi = fopen(s, "rb")) == NULL) {
860 fprintf(stderr, "Couldn't open %s!\n", fname);
867 if(!png_read_header(fi, &head)) {
868 fprintf(stderr, "%s is not a PNG file!\n", fname);
872 if (global.max_image_width < head.width)
873 global.max_image_width = head.width;
874 if (global.max_image_height < head.height)
875 global.max_image_height = head.height;
882 int args_callback_option(char *arg, char *val)
891 global.framerate = atof(val);
892 /* removed framerate>0 restriction in order to make
893 Flash Communication Server compatible SWFs */
894 if ((global.framerate < 0) ||(global.framerate >= 256.0)) {
897 "Error: You must specify a valid framerate between 1/256 and 255.\n");
905 global.outfile = val;
910 global.scale = atof(val)/100;
921 global.mkjpeg = atoi(val);
926 global.version = atoi(val);
948 global.force_width = atoi(val);
954 global.force_height = atoi(val);
959 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
963 char*s = strdup(val);
964 char*x1 = strtok(s, ":");
965 char*y1 = strtok(0, ":");
966 char*x2 = strtok(0, ":");
967 char*y2 = strtok(0, ":");
968 if(!(x1 && y1 && x2 && y2)) {
969 fprintf(stderr, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
984 char*s = strdup(val);
985 char*c = strchr(s, ':');
987 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
1007 fprintf(stderr, "Unknown option: -%s\n", arg);
1014 static struct options_t options[] = {
1019 {"T", "flashversion"},
1030 int args_callback_longoption(char *name, char *val)
1032 return args_long2shortoption(options, name, val);
1035 int args_callback_command(char *arg, char *next) // actually used as filename
1038 if (CheckInputFile(arg, &s) < 0) {
1040 fprintf(stderr, "Error opening input file: %s\n", arg);
1043 image[global.nfiles].filename = s;
1045 if (global.nfiles >= MAX_INPUT_FILES) {
1047 fprintf(stderr, "Error: Too many input files.\n");
1054 void args_callback_usage(char *name)
1057 printf("Usage: %s [-X width] [-Y height] [-o file.swf] [-r rate] file1.png [file2.png...]\n", name);
1059 printf("-r , --rate <framerate> Set movie framerate (frames per second)\n");
1060 printf("-o , --output <filename> Set name for SWF output file.\n");
1061 printf("-j , --jpeg <quality> Generate a lossy jpeg bitmap inside the SWF, with a given quality (1-100)\n");
1062 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
1063 printf("-T , --flashversion Set the flash version to generate\n");
1064 printf("-X , --pixel <width> Force movie width to <width> (default: autodetect)\n");
1065 printf("-Y , --pixel <height> Force movie height to <height> (default: autodetect)\n");
1066 printf("-v , --verbose <level> Set verbose level (0=quiet, 1=default, 2=debug)\n");
1067 printf("-q , --quiet Omit normal log messages, only log errors\n");
1068 printf("-C , --cgi For use as CGI- prepend http header, write to stdout\n");
1069 printf("-V , --version Print version information and exit\n");
1070 printf("-s , --scale <percent> Scale image to <percent>%% size.\n");
1074 int main(int argc, char **argv)
1079 memset(&global, 0x00, sizeof(global));
1081 global.framerate = 1.0;
1086 processargs(argc, argv);
1088 if(global.nfiles<=0) {
1089 fprintf(stderr, "No png files found in arguments\n");
1094 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
1096 t = MovieStart(&swf, global.framerate,
1097 global.force_width ? global.force_width : (int)(global.max_image_width*global.scale),
1098 global.force_height ? global.force_height : (int)(global.max_image_height*global.scale));
1102 for (i = 0; i < global.nfiles; i++) {
1104 fprintf(stderr, "[%03i] %s\n", i,
1106 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
1107 free(image[i].filename);
1111 MovieFinish(&swf, t, global.outfile);