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)
48 } image[MAX_INPUT_FILES];
50 static int custom_move=0;
53 static int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
54 static int custom_clip = 0;
56 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
61 memset(swf, 0x00, sizeof(SWF));
63 swf->fileVersion = global.version;
64 swf->frameRate = (int)(256.0 * framerate);
66 swf->movieSize.xmin = clip_x1 * 20;
67 swf->movieSize.ymin = clip_y1 * 20;
68 swf->movieSize.xmax = clip_x2 * 20;
69 swf->movieSize.ymax = clip_y2 * 20;
71 swf->movieSize.xmax = dx * 20;
72 swf->movieSize.ymax = dy * 20;
75 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
77 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
78 //rgb.g = 0xff; //<--- handy for testing alpha conversion
84 int MovieFinish(SWF * swf, TAG * t, char *sname)
86 int f, so = fileno(stdout);
87 t = swf_InsertTag(t, ST_END);
89 if ((!isatty(so)) && (!sname))
94 f = open(sname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
98 if FAILED(swf_WriteCGI(swf)) fprintf(stderr,"WriteCGI() failed.\n");
100 if(global.version >= 6) {
101 if (swf_WriteSWC(f, swf)<0)
102 fprintf(stderr, "Unable to write output file: %s\n", sname);
104 if (swf_WriteSWF(f, swf)<0)
105 fprintf(stderr, "Unable to write output file: %s\n", sname);
115 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
118 if(destlen) *destlen=0;
119 if(destdata) *destdata=0;
120 if(!fread(&len, 4, 1, fi))
122 if(!fread(head, 4, 1, fi))
124 len = REVERSESWAP32(len);
125 if(destlen) *destlen = len;
128 *destdata = malloc(len);
131 if(!fread(*destdata, len, 1, fi)) {
133 if(destlen) *destlen=0;
136 fseek(fi, 4, SEEK_CUR);
139 fseek(fi, len+4, SEEK_CUR);
144 unsigned int png_get_dword(FILE*fi)
148 return REVERSESWAP32(a);
159 int png_read_header(FILE*fi, struct png_header*header)
164 U8 head[8] = {137,80,78,71,13,10,26,10};
168 if(strncmp(head,head2,4))
171 while(png_read_chunk(&id, &len, &data, fi))
174 printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
175 if(!strncasecmp(id, "IHDR", 4)) {
178 header->width = REVERSESWAP32(*(U32*)&data[0]);
179 header->height = REVERSESWAP32(*(U32*)&data[4]);
180 a = data[8]; // should be 8
181 b = data[9]; // should be 3(indexed) or 2(rgb)
183 c = data[10]; // compression mode (0)
184 f = data[11]; // filter mode (0)
185 i = data[12]; // interlace mode (0)
187 if(VERBOSE(2)) printf("image mode:%d\n", b);
188 if(VERBOSE(2)) printf("bpp: %d\n", a);
189 if(VERBOSE(2)) printf("compression: %d\n", c);
190 if(VERBOSE(2)) printf("filter: %d\n", f);
191 if(VERBOSE(2)) printf("interlace: %d\n", i);
193 if(b!=0 && b!=2 && b!=3 && b!=6) {
194 fprintf(stderr, "Image mode %d not supported!\n", b);
196 fprintf(stderr, "(This is a grayscale image with alpha channel-\n");
197 fprintf(stderr, " try converting it into an RGB image with alpha channel)\n");
201 if(a!=8 && (b==2 || b==6)) {
202 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
206 fprintf(stderr, "Compression mode %d not supported!\n", c);
210 fprintf(stderr, "Filter mode %d not supported!\n", f);
214 fprintf(stderr, "Interlace mode %d not supported!\n", i);
218 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
229 typedef unsigned char byte;
230 #define ABS(a) ((a)>0?(a):(-(a)))
231 byte inline PaethPredictor (byte a,byte b,byte c)
233 // a = left, b = above, c = upper left
234 int p = a + b - c; // initial estimate
235 int pa = ABS(p - a); // distances to a, b, c
238 // return nearest of a,b,c,
239 // breaking ties in order a,b,c.
240 if (pa <= pb && pa <= pc)
247 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
250 unsigned char lastr=0;
251 unsigned char lastg=0;
252 unsigned char lastb=0;
253 unsigned char upperlastr=0;
254 unsigned char upperlastg=0;
255 unsigned char upperlastb=0;
258 for(x=0;x<width;x++) {
268 for(x=0;x<width;x++) {
270 dest[1] = src[0]+lastr;
271 dest[2] = src[1]+lastg;
272 dest[3] = src[2]+lastb;
281 for(x=0;x<width;x++) {
283 dest[1] = src[0]+old[1];
284 dest[2] = src[1]+old[2];
285 dest[3] = src[2]+old[3];
292 for(x=0;x<width;x++) {
294 dest[1] = src[0]+(old[1]+lastr)/2;
295 dest[2] = src[1]+(old[2]+lastg)/2;
296 dest[3] = src[2]+(old[3]+lastb)/2;
306 for(x=0;x<width;x++) {
308 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
309 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
310 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
325 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
328 unsigned char lastr=0;
329 unsigned char lastg=0;
330 unsigned char lastb=0;
331 unsigned char lasta=0; //TODO: 255?
332 unsigned char upperlastr=0;
333 unsigned char upperlastg=0;
334 unsigned char upperlastb=0;
335 unsigned char upperlasta=0; //TODO: 255?
338 for(x=0;x<width;x++) {
348 for(x=0;x<width;x++) {
349 dest[0] = src[3]+lasta;
350 dest[1] = src[0]+lastr;
351 dest[2] = src[1]+lastg;
352 dest[3] = src[2]+lastb;
362 for(x=0;x<width;x++) {
363 dest[0] = src[3]+old[0];
364 dest[1] = src[0]+old[1];
365 dest[2] = src[1]+old[2];
366 dest[3] = src[2]+old[3];
373 for(x=0;x<width;x++) {
374 dest[0] = src[3]+(old[0]+lasta)/2;
375 dest[1] = src[0]+(old[1]+lastr)/2;
376 dest[2] = src[1]+(old[2]+lastg)/2;
377 dest[3] = src[2]+(old[3]+lastb)/2;
388 for(x=0;x<width;x++) {
389 dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
390 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
391 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
392 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
409 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
412 unsigned char last=0;
413 unsigned char upperlast=0;
416 for(x=0;x<width;x++) {
423 for(x=0;x<width;x++) {
431 for(x=0;x<width;x++) {
439 for(x=0;x<width;x++) {
440 *dest = *src+(*old+last)/2;
448 for(x=0;x<width;x++) {
449 *dest = *src+PaethPredictor(last,*old,upperlast);
460 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
472 unsigned long int imagedatalen;
473 unsigned long int zimagedatalen=0;
477 int alphapalettelen = 0;
478 struct png_header header;
484 if ((fi = fopen(sname, "rb")) == NULL) {
486 fprintf(stderr, "Read access failed: %s\n", sname);
490 if(!png_read_header(fi, &header))
493 if(header.mode == 3 || header.mode == 0) bypp = 1;
495 if(header.mode == 2) bypp = 3;
497 if(header.mode == 6) bypp = 4;
500 imagedatalen = bypp * header.width * header.height + 65536;
501 imagedata = malloc(imagedatalen);
503 fseek(fi,8,SEEK_SET);
506 if(!png_read_chunk(&tagid, &len, &data, fi))
508 if(!strncmp(tagid, "IEND", 4)) {
511 if(!strncmp(tagid, "PLTE", 4)) {
514 data = 0; //don't free data
516 printf("%d colors in palette\n", palettelen);
518 if(!strncmp(tagid, "tRNS", 4)) {
519 if(header.mode == 3) {
521 alphapalettelen = len;
522 data = 0; //don't free data
524 printf("found %d alpha colors\n", alphapalettelen);
527 if(!strncmp(tagid, "IDAT", 4)) {
530 zimagedata = malloc(len);
531 memcpy(zimagedata,data,len);
533 zimagedata = realloc(zimagedata, zimagedatalen+len);
534 memcpy(&zimagedata[zimagedatalen], data, len);
535 zimagedatalen += len;
542 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
543 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
551 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
553 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
555 swf_SetU16(t, id); // id
556 if(header.mode == 2 || header.mode == 6) {
557 U8*data2 = malloc(header.width*header.height*4);
563 int semitransparent=0;
564 /* in case for mode 2, the following also performs 24->32 bit conversion */
565 unsigned char* firstline = malloc(header.width*4);
567 for(y=0;y<header.height;y++) {
568 int mode = imagedata[pos++]; //filter mode
572 dest = &data2[(y*header.width)*4];
576 /* one byte per pixel */
577 src = &imagedata[pos];
578 pos+=header.width*(header.mode==6?4:3);
580 /* not implemented yet */
586 memset(old, 0, header.width*4); //TODO: fill alpha with 255?
588 old = &data2[(y-1)*header.width*4];
591 applyfilter4(mode, src, old, dest, header.width);
592 else if(header.mode==2)
593 applyfilter3(mode, src, old, dest, header.width);
597 /* the image is now compressed and stored in data. Now let's take
598 a look at the alpha values */
599 if(header.mode == 6) {
600 for(y=0;y<header.height;y++) {
601 U8*l = &data2[(y*header.width)*4];
602 for(x=0;x<header.width;x++) {
607 if(a==255) transparent++;
610 else semitransparent++;
611 l[x*4+3]=(int)r*a/255;
612 l[x*4+2]=(int)g*a/255;
613 l[x*4+1]=(int)b*a/255;
617 if(semitransparent || opaque) {
618 t->id = ST_DEFINEBITSLOSSLESS2;
621 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
623 } else if(header.mode == 0 || header.mode == 3) {
625 int swf_width = BYTES_PER_SCANLINE(header.width);
626 U8*data2 = malloc(swf_width*header.height);
627 U8*tmpline = malloc(header.width);
630 if(header.mode == 3) { // palette or grayscale?
631 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
633 fprintf(stderr, "Error: No palette found!\n");
636 /* 24->32 bit conversion */
637 for(i=0;i<palettelen;i++) {
638 rgba[i].r = palette[i*3+0];
639 rgba[i].g = palette[i*3+1];
640 rgba[i].b = palette[i*3+2];
641 if(alphapalette && i<alphapalettelen) {
642 rgba[i].a = alphapalette[i];
643 rgba[i].r = ((int)rgba[i].r*rgba[i].a)/255;
644 rgba[i].g = ((int)rgba[i].g*rgba[i].a)/255;
645 rgba[i].b = ((int)rgba[i].b*rgba[i].a)/255;
651 int mult = (0x1ff>>header.bpp);
652 palettelen = 1<<header.bpp;
653 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
654 for(i=0;i<palettelen;i++) {
661 for(y=0;y<header.height;y++) {
662 int mode = imagedata[pos++]; //filter mode
664 U8*dest = &data2[y*swf_width];
666 src = &imagedata[pos];
667 if(header.bpp == 8) {
672 U32 v = (1<<header.bpp)-1;
673 for(x=0;x<header.width;x++) {
674 U32 r = src[s/8]<<8 |
677 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
681 pos+=(header.width*header.bpp+7)/8;
685 memset(data2,0,swf_width);
686 old = &data2[y*swf_width];
688 old = &data2[(y-1)*swf_width];
690 applyfilter1(mode, src, old, dest, header.width);
692 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
698 t = swf_InsertTag(t, ST_DEFINESHAPE3);
701 swf_GetMatrix(NULL, &m);
706 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 1);
708 swf_SetU16(t, id + 1); // id
711 r.xmax = header.width * 20;
712 r.ymax = header.height * 20;
715 swf_SetShapeHeader(t, s);
717 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
718 swf_ShapeSetLine(t, s, r.xmax, 0);
719 swf_ShapeSetLine(t, s, 0, r.ymax);
720 swf_ShapeSetLine(t, s, -r.xmax, 0);
721 swf_ShapeSetLine(t, s, 0, -r.ymax);
725 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
726 swf_SetU16(t, 50); // depth
728 t = swf_InsertTag(t, ST_PLACEOBJECT2);
730 swf_GetMatrix(NULL, &m);
735 m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
736 m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
738 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
740 t = swf_InsertTag(t, ST_SHOWFRAME);
748 int CheckInputFile(char *fname, char **realname)
751 char *s = malloc(strlen(fname) + 5);
752 struct png_header head;
759 // Check whether file exists (with typical extensions)
761 if ((fi = fopen(s, "rb")) == NULL) {
762 sprintf(s, "%s.png", fname);
763 if ((fi = fopen(s, "rb")) == NULL) {
764 sprintf(s, "%s.PNG", fname);
765 if ((fi = fopen(s, "rb")) == NULL) {
766 sprintf(s, "%s.Png", fname);
767 if ((fi = fopen(s, "rb")) == NULL) {
768 fprintf(stderr, "Couldn't open %s!\n", fname);
775 if(!png_read_header(fi, &head)) {
776 fprintf(stderr, "%s is not a PNG file!\n", fname);
780 if (global.max_image_width < head.width)
781 global.max_image_width = head.width;
782 if (global.max_image_height < head.height)
783 global.max_image_height = head.height;
790 int args_callback_option(char *arg, char *val)
799 global.framerate = atof(val);
800 /* removed framerate>0 restriction in order to make
801 Flash Communication Server compatible SWFs */
802 if ((global.framerate < 0) ||(global.framerate >= 256.0)) {
805 "Error: You must specify a valid framerate between 1/256 and 255.\n");
813 global.outfile = val;
823 global.version = atoi(val);
845 global.force_width = atoi(val);
851 global.force_height = atoi(val);
856 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
860 char*s = strdup(val);
861 char*x1 = strtok(s, ":");
862 char*y1 = strtok(0, ":");
863 char*x2 = strtok(0, ":");
864 char*y2 = strtok(0, ":");
865 if(!(x1 && y1 && x2 && y2)) {
866 fprintf(stderr, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
881 char*s = strdup(val);
882 char*c = strchr(s, ':');
884 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
904 fprintf(stderr, "Unknown option: -%s\n", arg);
911 static struct options_t options[] = {
924 int args_callback_longoption(char *name, char *val)
926 return args_long2shortoption(options, name, val);
929 int args_callback_command(char *arg, char *next) // actually used as filename
932 if (CheckInputFile(arg, &s) < 0) {
934 fprintf(stderr, "Error opening input file: %s\n", arg);
937 image[global.nfiles].filename = s;
939 if (global.nfiles >= MAX_INPUT_FILES) {
941 fprintf(stderr, "Error: Too many input files.\n");
948 void args_callback_usage(char *name)
951 printf("Usage: %s [-X width] [-Y height] [-o file.swf] [-r rate] file1.png [file2.png...]\n", name);
953 printf("-r , --rate <framerate> Set movie framerate (frames per second)\n");
954 printf("-o , --output <filename> Set name for SWF output file.\n");
955 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
956 printf("-X , --pixel <width> Force movie width to <width> (default: autodetect)\n");
957 printf("-Y , --pixel <height> Force movie height to <height> (default: autodetect)\n");
958 printf("-v , --verbose Be verbose. Use more than one -v for greater effect \n");
959 printf("-q , --quiet Omit normal log messages, only log errors\n");
960 printf("-C , --cgi For use as CGI- prepend http header, write to stdout\n");
961 printf("-V , --version Print version information and exit\n");
965 int main(int argc, char **argv)
970 memset(&global, 0x00, sizeof(global));
972 global.framerate = 1.0;
976 processargs(argc, argv);
978 if(global.nfiles<=0) {
979 fprintf(stderr, "No png files found in arguments\n");
984 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
986 t = MovieStart(&swf, global.framerate,
987 global.force_width ? global.force_width : global.
989 global.force_height ? global.force_height : global.
994 for (i = 0; i < global.nfiles; i++) {
996 fprintf(stderr, "[%03i] %s\n", i,
998 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
999 free(image[i].filename);
1003 MovieFinish(&swf, t, global.outfile);