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)
47 } image[MAX_INPUT_FILES];
49 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
54 memset(swf, 0x00, sizeof(SWF));
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
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))
80 f = open(sname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
84 if FAILED(swf_WriteCGI(swf)) fprintf(stderr,"WriteCGI() failed.\n");
86 if FAILED(swf_WriteSWF(f,swf)) fprintf(stderr,"WriteSWF() failed.\n");
95 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
98 if(destlen) *destlen=0;
99 if(destdata) *destdata=0;
100 if(!fread(&len, 4, 1, fi))
102 if(!fread(head, 4, 1, fi))
104 len = REVERSESWAP32(len);
105 if(destlen) *destlen = len;
108 *destdata = malloc(len);
111 if(!fread(*destdata, len, 1, fi)) {
113 if(destlen) *destlen=0;
116 fseek(fi, 4, SEEK_CUR);
119 fseek(fi, len+4, SEEK_CUR);
124 unsigned int png_get_dword(FILE*fi)
128 return REVERSESWAP32(a);
139 int png_read_header(FILE*fi, struct png_header*header)
144 U8 head[8] = {137,80,78,71,13,10,26,10};
148 if(strncmp(head,head2,4))
151 while(png_read_chunk(&id, &len, &data, fi))
154 printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
155 if(!strncasecmp(id, "IHDR", 4)) {
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);
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");
181 if(a!=8 && (b==2 || b==6)) {
182 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
186 fprintf(stderr, "Compression mode %d not supported!\n", c);
190 fprintf(stderr, "Filter mode %d not supported!\n", f);
194 fprintf(stderr, "Interlace mode %d not supported!\n", i);
198 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
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
218 // return nearest of a,b,c,
219 // breaking ties in order a,b,c.
220 if (pa <= pb && pa <= pc)
227 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
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;
238 for(x=0;x<width;x++) {
248 for(x=0;x<width;x++) {
250 dest[1] = src[0]+lastr;
251 dest[2] = src[1]+lastg;
252 dest[3] = src[2]+lastb;
261 for(x=0;x<width;x++) {
263 dest[1] = src[0]+old[1];
264 dest[2] = src[1]+old[2];
265 dest[3] = src[2]+old[3];
272 for(x=0;x<width;x++) {
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;
286 for(x=0;x<width;x++) {
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);
305 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
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;
318 for(x=0;x<width;x++) {
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;
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];
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;
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);
389 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
392 unsigned char last=0;
393 unsigned char upperlast=0;
396 for(x=0;x<width;x++) {
403 for(x=0;x<width;x++) {
411 for(x=0;x<width;x++) {
419 for(x=0;x<width;x++) {
420 *dest = *src+(*old+last)/2;
427 for(x=0;x<width;x++) {
428 *dest = *src+PaethPredictor(last,*old,upperlast);
439 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
451 unsigned long int imagedatalen;
452 unsigned long int zimagedatalen=0;
456 int alphapalettelen = 0;
457 struct png_header header;
463 if ((fi = fopen(sname, "rb")) == NULL) {
465 fprintf(stderr, "Read access failed: %s\n", sname);
469 if(!png_read_header(fi, &header))
472 if(header.mode == 3 || header.mode == 0) bypp = 1;
474 if(header.mode == 2) bypp = 3;
476 if(header.mode == 6) bypp = 4;
479 imagedatalen = bypp * header.width * header.height + 65536;
480 imagedata = malloc(imagedatalen);
482 fseek(fi,8,SEEK_SET);
485 if(!png_read_chunk(&tagid, &len, &data, fi))
487 if(!strncmp(tagid, "IEND", 4)) {
490 if(!strncmp(tagid, "PLTE", 4)) {
493 data = 0; //don't free data
495 printf("%d colors in palette\n", palettelen);
497 if(!strncmp(tagid, "tRNS", 4)) {
498 if(header.mode == 3) {
500 alphapalettelen = len;
501 data = 0; //don't free data
503 printf("found %d alpha colors\n", alphapalettelen);
506 if(!strncmp(tagid, "IDAT", 4)) {
509 zimagedata = malloc(len);
510 memcpy(zimagedata,data,len);
512 zimagedata = realloc(zimagedata, zimagedatalen+len);
513 memcpy(&zimagedata[zimagedatalen], data, len);
514 zimagedatalen += len;
521 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
522 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
530 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
532 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
534 swf_SetU16(t, id); // id
535 if(header.mode == 2 || header.mode == 6) {
536 U8*data2 = malloc(header.width*header.height*4);
542 int semitransparent=0;
543 /* in case for mode 2, the following also performs 24->32 bit conversion */
544 for(y=0;y<header.height;y++) {
545 int mode = imagedata[pos++]; //filter mode
549 dest = &data2[(y*header.width)*4];
553 /* one byte per pixel */
554 src = &imagedata[pos];
555 pos+=header.width*(header.mode==6?4:3);
557 /* not implemented yet */
562 memset(data2,0,header.width*4);
563 old = &data2[y*header.width*4];
565 old = &data2[(y-1)*header.width*4];
568 applyfilter4(mode, src, old, dest, header.width);
569 else if(header.mode==2)
570 applyfilter3(mode, src, old, dest, header.width);
573 /* the image is now compressed and stored in data. Now let's take
574 a look at the alpha values to determine which bitmap type we
577 for(y=0;y<header.height;y++) {
578 U8*l = &data2[(y*header.width)*4];
579 for(x=0;x<header.width;x++) {
580 if(l[x*4+0]==255) transparent++;
581 else if(l[x*4+0]==0) opaque++;
582 else semitransparent++;
585 /* mode 6 images which are not fully opaque or fully transparent
586 will be stored as definejpeg3 */
587 if(header.mode == 6 && transparent != header.width*header.height
588 && opaque != header.width*header.height)
590 fprintf(stderr, "Warning: No jpeg lib compiled in- not able to store transparency information\n");
593 fprintf(stderr, "Image has transparency information. Storing as DefineBitsJpeg3 Tag (jpeg+alpha)\n");
595 printf("Image is semi-transparent\n");
597 // we always use quality 100, since png2swf is expected to
598 // use more or less lossless compression
600 swf_SetJPEGBits3(t, header.width, header.height, (RGBA*)data2, 100);
601 t->id = ST_DEFINEBITSJPEG3;
606 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
609 } else if(header.mode == 0 || header.mode == 3) {
611 int swf_width = BYTES_PER_SCANLINE(header.width);
612 U8*data2 = malloc(swf_width*header.height);
613 U8*tmpline = malloc(header.width);
616 if(header.mode == 3) { // palette or grayscale?
617 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
619 fprintf(stderr, "Error: No palette found!\n");
622 /* 24->32 bit conversion */
623 for(i=0;i<palettelen;i++) {
624 rgba[i].r = palette[i*3+0];
625 rgba[i].g = palette[i*3+1];
626 rgba[i].b = palette[i*3+2];
627 if(alphapalette && i<alphapalettelen) {
628 rgba[i].a = alphapalette[i];
629 if(alphapalette[i] == 0) {
630 /* if the color is fully transparent, it doesn't matter
631 what it's rgb values are. furthermore, all Flash
632 players up to Flash 5 can't deal with anything beyond
633 one transparent color with value (00,00,00,00). */
634 rgba[i].r = rgba[i].g = rgba[i].b = 0;
642 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
650 for(y=0;y<header.height;y++) {
651 int mode = imagedata[pos++]; //filter mode
653 U8*dest = &data2[y*swf_width];
655 src = &imagedata[pos];
656 if(header.bpp == 8) {
661 U32 v = (1<<header.bpp)-1;
662 for(x=0;x<header.width;x++) {
663 U32 r = src[s/8]<<8 |
666 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
670 pos+=(header.width*header.bpp+7)/8;
674 memset(data2,0,swf_width);
675 old = &data2[y*swf_width];
677 old = &data2[(y-1)*swf_width];
679 applyfilter1(mode, src, old, dest, header.width);
681 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
688 swf_SetU8(t, 0); //fix for flash player bug- see ../lib/modules/swfbits.c
691 t = swf_InsertTag(t, ST_DEFINESHAPE3);
694 swf_GetMatrix(NULL, &m);
697 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
699 swf_SetU16(t, id + 1); // id
702 r.xmax = header.width * 20;
703 r.ymax = header.height * 20;
706 swf_SetShapeHeader(t, s);
708 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
709 swf_ShapeSetLine(t, s, r.xmax, 0);
710 swf_ShapeSetLine(t, s, 0, r.ymax);
711 swf_ShapeSetLine(t, s, -r.xmax, 0);
712 swf_ShapeSetLine(t, s, 0, -r.ymax);
716 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
717 swf_SetU16(t, 50); // depth
719 t = swf_InsertTag(t, ST_PLACEOBJECT2);
721 swf_GetMatrix(NULL, &m);
722 m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
723 m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
724 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
726 t = swf_InsertTag(t, ST_SHOWFRAME);
734 int CheckInputFile(char *fname, char **realname)
737 char *s = malloc(strlen(fname) + 5);
738 struct png_header head;
745 // Check whether file exists (with typical extensions)
747 if ((fi = fopen(s, "rb")) == NULL) {
748 sprintf(s, "%s.png", fname);
749 if ((fi = fopen(s, "rb")) == NULL) {
750 sprintf(s, "%s.PNG", fname);
751 if ((fi = fopen(s, "rb")) == NULL) {
752 sprintf(s, "%s.Png", fname);
753 if ((fi = fopen(s, "rb")) == NULL)
754 fprintf(stderr, "Couldn't open %s!\n", fname);
760 if(!png_read_header(fi, &head)) {
761 fprintf(stderr, "%s is not a PNG file!\n", fname);
765 if (global.max_image_width < head.width)
766 global.max_image_width = head.width;
767 if (global.max_image_height < head.height)
768 global.max_image_height = head.height;
775 int args_callback_option(char *arg, char *val)
784 global.framerate = atof(val);
785 if ((global.framerate < 1.0/256) ||(global.framerate >= 256.0)) {
788 "Error: You must specify a valid framerate between 1/256 and 255.\n");
796 global.outfile = val;
806 global.verbose = atoi(val);
812 global.force_width = atoi(val);
818 global.force_height = atoi(val);
823 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
833 fprintf(stderr, "Unknown option: -%s\n", arg);
840 static struct options_t options[] = {
851 int args_callback_longoption(char *name, char *val)
853 return args_long2shortoption(options, name, val);
856 int args_callback_command(char *arg, char *next) // actually used as filename
859 if (CheckInputFile(arg, &s) < 0) {
861 fprintf(stderr, "Error opening input file: %s\n", arg);
864 image[global.nfiles].filename = s;
866 if (global.nfiles >= MAX_INPUT_FILES) {
868 fprintf(stderr, "Error: Too many input files.\n");
875 void args_callback_usage(char *name)
878 printf("Usage: %s [-X width] [-Y height] [-o file.swf] [-r rate] file1.png [file2.png...]\n", name);
880 printf("-r , --rate <framerate> Set movie framerate (frames per second)\n");
881 printf("-o , --output <filename> Set name for SWF output file.\n");
882 printf("-X , --pixel <width> Force movie width to <width> (default: autodetect)\n");
883 printf("-Y , --pixel <height> Force movie height to <height> (default: autodetect)\n");
884 printf("-v , --verbose <level> Set verbose level (0=quiet, 1=default, 2=debug)\n");
885 printf("-C , --cgi For use as CGI- prepend http header, write to stdout\n");
886 printf("-V , --version Print version information and exit\n");
890 int main(int argc, char **argv)
895 memset(&global, 0x00, sizeof(global));
897 global.framerate = 1.0;
900 processargs(argc, argv);
906 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
908 t = MovieStart(&swf, global.framerate,
909 global.force_width ? global.force_width : global.
911 global.force_height ? global.force_height : global.
916 for (i = 0; i < global.nfiles; i++) {
918 fprintf(stderr, "[%03i] %s\n", i,
920 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
921 free(image[i].filename);
925 MovieFinish(&swf, t, global.outfile);