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)
46 } image[MAX_INPUT_FILES];
48 TAG *MovieStart(SWF * swf, int framerate, int dx, int dy)
53 memset(swf, 0x00, sizeof(SWF));
56 swf->frameRate = (25600 / framerate);
57 swf->movieSize.xmax = dx * 20;
58 swf->movieSize.ymax = dy * 20;
60 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
62 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
63 //rgb.g = 0xff; <--- handy for testing alpha conversion
69 int MovieFinish(SWF * swf, TAG * t, char *sname)
71 int handle, so = fileno(stdout);
72 t = swf_InsertTag(t, ST_END);
74 if ((!isatty(so)) && (!sname))
79 handle = open(sname, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
82 (swf_WriteSWF(handle, swf)) if (VERBOSE(1))
83 fprintf(stderr, "Unable to write output file: %s\n", sname);
91 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
94 if(destlen) *destlen=0;
95 if(destdata) *destdata=0;
96 if(!fread(&len, 4, 1, fi))
98 if(!fread(head, 4, 1, fi))
100 len = REVERSESWAP32(len);
101 if(destlen) *destlen = len;
104 *destdata = malloc(len);
107 if(!fread(*destdata, len, 1, fi)) {
109 if(destlen) *destlen=0;
112 fseek(fi, 4, SEEK_CUR);
115 fseek(fi, len+4, SEEK_CUR);
120 unsigned int png_get_dword(FILE*fi)
124 return REVERSESWAP32(a);
135 int png_read_header(FILE*fi, struct png_header*header)
140 U8 head[8] = {137,80,78,71,13,10,26,10};
144 if(strncmp(head,head2,4))
147 while(png_read_chunk(&id, &len, &data, fi))
150 printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
151 if(!strncasecmp(id, "IHDR", 4)) {
154 header->width = REVERSESWAP32(*(U32*)&data[0]);
155 header->height = REVERSESWAP32(*(U32*)&data[4]);
156 a = data[8]; // should be 8
157 b = data[9]; // should be 3(indexed) or 2(rgb)
159 c = data[10]; // compression mode (0)
160 f = data[11]; // filter mode (0)
161 i = data[12]; // interlace mode (0)
163 if(b!=2 && b!=3 && b!=6) {
164 fprintf(stderr, "Image mode %d not supported!\n", b);
167 if(a!=8 && (b==2 || b==6)) {
168 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
172 fprintf(stderr, "Compression mode %d not supported!\n", c);
176 fprintf(stderr, "Filter mode %d not supported!\n", f);
180 fprintf(stderr, "Interlace mode %d not supported!\n", i);
184 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
195 typedef unsigned char byte;
196 #define ABS(a) ((a)>0?(a):(-(a)))
197 byte inline PaethPredictor (byte a,byte b,byte c)
199 // a = left, b = above, c = upper left
200 int p = a + b - c; // initial estimate
201 int pa = ABS(p - a); // distances to a, b, c
204 // return nearest of a,b,c,
205 // breaking ties in order a,b,c.
206 if (pa <= pb && pa <= pc)
213 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
216 unsigned char lastr=0;
217 unsigned char lastg=0;
218 unsigned char lastb=0;
219 unsigned char upperlastr=0;
220 unsigned char upperlastg=0;
221 unsigned char upperlastb=0;
224 for(x=0;x<width;x++) {
234 for(x=0;x<width;x++) {
236 dest[1] = src[0]+lastr;
237 dest[2] = src[1]+lastg;
238 dest[3] = src[2]+lastb;
247 for(x=0;x<width;x++) {
249 dest[1] = src[0]+old[1];
250 dest[2] = src[1]+old[2];
251 dest[3] = src[2]+old[3];
258 for(x=0;x<width;x++) {
260 dest[1] = src[0]+(old[1]+lastr)/2;
261 dest[2] = src[1]+(old[2]+lastg)/2;
262 dest[3] = src[2]+(old[3]+lastb)/2;
272 for(x=0;x<width;x++) {
274 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
275 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
276 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
291 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
294 unsigned char lastr=0;
295 unsigned char lastg=0;
296 unsigned char lastb=0;
297 unsigned char lasta=0;
298 unsigned char upperlastr=0;
299 unsigned char upperlastg=0;
300 unsigned char upperlastb=0;
301 unsigned char upperlasta=0;
304 for(x=0;x<width;x++) {
314 for(x=0;x<width;x++) {
315 dest[0] = src[3]+lasta;
316 dest[1] = src[0]+lastr;
317 dest[2] = src[1]+lastg;
318 dest[3] = src[2]+lastb;
328 for(x=0;x<width;x++) {
329 dest[0] = src[3]+old[0];
330 dest[1] = src[0]+old[1];
331 dest[2] = src[1]+old[2];
332 dest[3] = src[2]+old[3];
339 for(x=0;x<width;x++) {
340 dest[0] = src[3]+(old[0]+lasta)/2;
341 dest[1] = src[0]+(old[1]+lastr)/2;
342 dest[2] = src[1]+(old[2]+lastg)/2;
343 dest[3] = src[2]+(old[3]+lastb)/2;
353 for(x=0;x<width;x++) {
354 dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
355 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
356 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
357 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
374 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
377 unsigned char last=0;
378 unsigned char upperlast=0;
381 for(x=0;x<width;x++) {
388 for(x=0;x<width;x++) {
396 for(x=0;x<width;x++) {
404 for(x=0;x<width;x++) {
405 *dest = *src+(*old+last)/2;
412 for(x=0;x<width;x++) {
413 *dest = *src+PaethPredictor(last,*old,upperlast);
424 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
436 unsigned long int imagedatalen;
437 unsigned long int zimagedatalen=0;
441 int alphapalettelen = 0;
442 struct png_header header;
448 if ((fi = fopen(sname, "rb")) == NULL) {
450 fprintf(stderr, "Read access failed: %s\n", sname);
454 if(!png_read_header(fi, &header))
457 if(header.mode == 3) bypp = 1;
459 if(header.mode == 2) bypp = 3;
461 if(header.mode == 6) bypp = 4;
464 imagedatalen = bypp * header.width * header.height + 65536;
465 imagedata = malloc(imagedatalen);
467 fseek(fi,8,SEEK_SET);
470 if(!png_read_chunk(&tagid, &len, &data, fi))
472 if(!strncmp(tagid, "IEND", 4)) {
475 if(!strncmp(tagid, "PLTE", 4)) {
478 data = 0; //don't free data
480 printf("%d colors in palette\n", palettelen);
482 if(!strncmp(tagid, "tRNS", 4)) {
483 if(header.mode == 3) {
485 alphapalettelen = len;
486 data = 0; //don't free data
488 printf("found %d alpha colors\n", alphapalettelen);
491 if(!strncmp(tagid, "IDAT", 4)) {
494 zimagedata = malloc(len);
495 memcpy(zimagedata,data,len);
497 zimagedata = realloc(zimagedata, zimagedatalen+len);
498 memcpy(&zimagedata[zimagedatalen], data, len);
499 zimagedatalen += len;
506 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
507 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
515 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
517 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
519 swf_SetU16(t, id); // id
520 if(header.mode == 2 || header.mode == 6) {
521 U8*data2 = malloc(header.width*header.height*4);
527 /* in case for mode 2, the following also performs 24->32 bit conversion */
528 for(y=0;y<header.height;y++) {
529 int mode = imagedata[pos++]; //filter mode
533 dest = &data2[(y*header.width)*4];
537 /* one byte per pixel */
538 src = &imagedata[pos];
539 pos+=header.width*(header.mode==6?4:3);
541 /* not implemented yet */
546 memset(data2,0,header.width*4);
547 old = &data2[y*header.width*4];
549 old = &data2[(y-1)*header.width*4];
552 applyfilter4(mode, src, old, dest, header.width);
554 applyfilter3(mode, src, old, dest, header.width);
558 /* the image is now compressed and stored in data. Now let's take
559 a look at the alpha values to determine which bitmap type we
562 for(y=0;y<header.height;y++) {
563 U8*l = &data2[(y*header.width)*4];
564 for(x=0;x<header.width;x++) {
565 if(l[x*4+0]==255) transparent++;
566 if(l[x*4+0]==0) opaque++;
569 /* mode 6 images which are not fully opaque or fully transparent
570 will be stored as definejpeg3 */
571 if(header.mode == 6 && transparent != header.width*header.height
572 && opaque != header.width*header.height) {
574 printf("Image has transparency information. Storing as DefineBitsJpeg3 Tag (jpeg+alpha)\n");
576 // we always use quality 100, since png2swf is expected to
577 // use more or less lossless compression
579 swf_SetJPEGBits3(t, header.width, header.height, (RGBA*)data2, 100);
580 t->id = ST_DEFINEBITSJPEG3;
585 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
590 RGBA*rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
591 int swf_width = BYTES_PER_SCANLINE(header.width);
592 U8*data2 = malloc(swf_width*header.height);
593 U8*tmpline = malloc(header.width);
597 fprintf(stderr, "Error: No palette found!\n");
600 /* 24->32 bit conversion */
601 for(i=0;i<palettelen;i++) {
602 rgba[i].r = palette[i*3+0];
603 rgba[i].g = palette[i*3+1];
604 rgba[i].b = palette[i*3+2];
605 if(alphapalette && i<alphapalettelen) {
606 rgba[i].a = alphapalette[i];
607 if(alphapalette[i] == 0) {
608 /* if the color is fully transparent, it doesn't matter
609 what it's rgb values are. furthermore, all Flash
610 players up to Flash 5 can't deal with anything beyond
611 one transparent color with value (00,00,00,00). */
612 rgba[i].r = rgba[i].g = rgba[i].b = 0;
619 for(y=0;y<header.height;y++) {
620 int mode = imagedata[pos++]; //filter mode
622 U8*dest = &data2[y*swf_width];
624 src = &imagedata[pos];
625 if(header.bpp == 8) {
630 U32 v = (1<<header.bpp)-1;
631 for(x=0;x<header.width;x++) {
632 U32 r = src[s/8]<<8 |
635 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
639 pos+=(header.width*header.bpp+7)/8;
643 memset(data2,0,swf_width);
644 old = &data2[y*swf_width];
646 old = &data2[(y-1)*swf_width];
648 applyfilter1(mode, src, old, dest, header.width);
650 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
656 t = swf_InsertTag(t, ST_DEFINESHAPE3);
659 swf_GetMatrix(NULL, &m);
662 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
664 swf_SetU16(t, id + 1); // id
667 r.xmax = header.width * 20;
668 r.ymax = header.height * 20;
671 swf_SetShapeHeader(t, s);
673 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
674 swf_ShapeSetLine(t, s, r.xmax, 0);
675 swf_ShapeSetLine(t, s, 0, r.ymax);
676 swf_ShapeSetLine(t, s, -r.xmax, 0);
677 swf_ShapeSetLine(t, s, 0, -r.ymax);
681 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
682 swf_SetU16(t, 50); // depth
684 t = swf_InsertTag(t, ST_PLACEOBJECT2);
686 swf_GetMatrix(NULL, &m);
687 m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
688 m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
689 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
691 t = swf_InsertTag(t, ST_SHOWFRAME);
699 int CheckInputFile(char *fname, char **realname)
702 char *s = malloc(strlen(fname) + 5);
703 struct png_header head;
710 // Check whether file exists (with typical extensions)
712 if ((fi = fopen(s, "rb")) == NULL) {
713 sprintf(s, "%s.png", fname);
714 if ((fi = fopen(s, "rb")) == NULL) {
715 sprintf(s, "%s.PNG", fname);
716 if ((fi = fopen(s, "rb")) == NULL) {
717 sprintf(s, "%s.Png", fname);
718 if ((fi = fopen(s, "rb")) == NULL)
719 fprintf(stderr, "Couldn't open %s!\n", fname);
725 if(!png_read_header(fi, &head)) {
726 fprintf(stderr, "%s is not a PNG file!\n", fname);
730 if (global.max_image_width < head.width)
731 global.max_image_width = head.width;
732 if (global.max_image_height < head.height)
733 global.max_image_height = head.height;
740 int args_callback_option(char *arg, char *val)
749 global.framerate = atoi(val);
750 if ((global.framerate < 1) ||(global.framerate > 5000)) {
753 "Error: You must specify a valid framerate between 1 and 10000.\n");
761 global.outfile = val;
767 global.verbose = atoi(val);
773 global.force_width = atoi(val);
779 global.force_height = atoi(val);
784 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
794 fprintf(stderr, "Unknown option: -%s\n", arg);
801 struct options_t options[] =
811 int args_callback_longoption(char *name, char *val)
813 return args_long2shortoption(options, name, val);
816 int args_callback_command(char *arg, char *next) // actually used as filename
819 if (CheckInputFile(arg, &s) < 0) {
821 fprintf(stderr, "Error opening input file: %s\n", arg);
824 image[global.nfiles].filename = s;
826 if (global.nfiles >= MAX_INPUT_FILES) {
828 fprintf(stderr, "Error: Too many input files.\n");
835 void args_callback_usage(char *name)
837 printf("Usage: %s [-options [value]] imagefiles[.png] [...]\n", name);
838 printf("-r framerate (rate) Set movie framerate (100/sec)\n");
839 printf("-o outputfile (output) Set name for SWF output file\n");
840 printf("-X pixel (width) Force movie width to pixel (default: autodetect)\n");
841 printf("-Y pixel (height) Force movie height to pixel (default: autodetect)\n");
842 printf("-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
843 printf("-V (version) Print version information and exit\n");
847 int main(int argc, char **argv)
852 memset(&global, 0x00, sizeof(global));
854 global.framerate = 100;
857 processargs(argc, argv);
863 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
865 t = MovieStart(&swf, global.framerate,
866 global.force_width ? global.force_width : global.
868 global.force_height ? global.force_height : global.
873 for (i = 0; i < global.nfiles; i++) {
875 fprintf(stderr, "[%03i] %s\n", i,
877 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
878 free(image[i].filename);
882 MovieFinish(&swf, t, global.outfile);