3 PNG to SWF converter tool
5 Part of the swftools package.
7 Copyright (c) 2002 Matthias Kramm <kramm@quiss.org>
9 This file is distributed under the GPL, see file COPYING for details
17 #include "../lib/rfxswf.h"
18 #include "../lib/args.h"
20 #define MAX_INPUT_FILES 1024
21 #define VERBOSE(x) (global.verbose>=x)
36 } image[MAX_INPUT_FILES];
38 TAG *MovieStart(SWF * swf, int framerate, int dx, int dy)
43 memset(swf, 0x00, sizeof(SWF));
46 swf->frameRate = (25600 / framerate);
47 swf->movieSize.xmax = dx * 20;
48 swf->movieSize.ymax = dy * 20;
50 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
52 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
59 int MovieFinish(SWF * swf, TAG * t, char *sname)
61 int handle, so = fileno(stdout);
62 t = swf_InsertTag(t, ST_END);
64 if ((!isatty(so)) && (!sname))
69 handle = open(sname, O_RDWR | O_CREAT | O_TRUNC, 0666);
72 (swf_WriteSWF(handle, swf)) if (VERBOSE(1))
73 fprintf(stderr, "Unable to write output file: %s\n", sname);
81 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
84 if(destlen) *destlen=0;
85 if(destdata) *destdata=0;
86 if(!fread(&len, 4, 1, fi))
88 if(!fread(head, 4, 1, fi))
90 len = REVERSESWAP32(len);
91 if(destlen) *destlen = len;
94 *destdata = malloc(len);
97 if(!fread(*destdata, len, 1, fi)) {
99 if(destlen) *destlen=0;
102 fseek(fi, 4, SEEK_CUR);
105 fseek(fi, len+4, SEEK_CUR);
110 unsigned int png_get_dword(FILE*fi)
114 return REVERSESWAP32(a);
125 int png_read_header(FILE*fi, struct png_header*header)
130 U8 head[8] = {137,80,78,71,13,10,26,10};
134 if(strncmp(head,head2,4))
137 while(png_read_chunk(&id, &len, &data, fi))
140 printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
141 if(!strncasecmp(id, "IHDR", 4)) {
144 header->width = REVERSESWAP32(*(U32*)&data[0]);
145 header->height = REVERSESWAP32(*(U32*)&data[4]);
146 a = data[8]; // should be 8
147 b = data[9]; // should be 3(indexed) or 2(rgb)
149 c = data[10]; // compression mode (0)
150 f = data[11]; // filter mode (0)
151 i = data[12]; // interlace mode (0)
153 if(b!=2 && b!=3 && b!=6) {
154 fprintf(stderr, "Image mode %d not supported!\n", b);
157 if(a!=8 && (b==2 || b==6)) {
158 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
162 fprintf(stderr, "Compression mode %d not supported!\n", c);
166 fprintf(stderr, "Filter mode %d not supported!\n", f);
170 fprintf(stderr, "Interlace mode %d not supported!\n", i);
174 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
185 typedef unsigned char byte;
186 #define ABS(a) ((a)>0?(a):(-(a)))
187 byte inline PaethPredictor (byte a,byte b,byte c)
189 // a = left, b = above, c = upper left
190 int p = a + b - c; // initial estimate
191 int pa = ABS(p - a); // distances to a, b, c
194 // return nearest of a,b,c,
195 // breaking ties in order a,b,c.
196 if (pa <= pb && pa <= pc)
203 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
206 unsigned char lastr=0;
207 unsigned char lastg=0;
208 unsigned char lastb=0;
209 unsigned char upperlastr=0;
210 unsigned char upperlastg=0;
211 unsigned char upperlastb=0;
214 for(x=0;x<width;x++) {
224 for(x=0;x<width;x++) {
226 dest[1] = src[0]+lastr;
227 dest[2] = src[1]+lastg;
228 dest[3] = src[2]+lastb;
237 for(x=0;x<width;x++) {
239 dest[1] = src[0]+old[1];
240 dest[2] = src[1]+old[2];
241 dest[3] = src[2]+old[3];
248 for(x=0;x<width;x++) {
250 dest[1] = src[0]+(old[1]+lastr)/2;
251 dest[2] = src[1]+(old[2]+lastg)/2;
252 dest[3] = src[2]+(old[3]+lastb)/2;
262 for(x=0;x<width;x++) {
264 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
265 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
266 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
281 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
284 unsigned char lastr=0;
285 unsigned char lastg=0;
286 unsigned char lastb=0;
287 unsigned char lasta=0;
288 unsigned char upperlastr=0;
289 unsigned char upperlastg=0;
290 unsigned char upperlastb=0;
291 unsigned char upperlasta=0;
294 for(x=0;x<width;x++) {
304 for(x=0;x<width;x++) {
305 dest[0] = src[3]+lasta;
306 dest[1] = src[0]+lastr;
307 dest[2] = src[1]+lastg;
308 dest[3] = src[2]+lastb;
318 for(x=0;x<width;x++) {
319 dest[0] = src[3]+old[0];
320 dest[1] = src[0]+old[1];
321 dest[2] = src[1]+old[2];
322 dest[3] = src[2]+old[3];
329 for(x=0;x<width;x++) {
330 dest[0] = src[3]+(old[0]+lasta)/2;
331 dest[1] = src[0]+(old[1]+lastr)/2;
332 dest[2] = src[1]+(old[2]+lastg)/2;
333 dest[3] = src[2]+(old[3]+lastb)/2;
343 for(x=0;x<width;x++) {
344 dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
345 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
346 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
347 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
364 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
367 unsigned char last=0;
368 unsigned char upperlast=0;
371 for(x=0;x<width;x++) {
378 for(x=0;x<width;x++) {
386 for(x=0;x<width;x++) {
394 for(x=0;x<width;x++) {
395 *dest = *src+(*old+last)/2;
402 for(x=0;x<width;x++) {
403 *dest = *src+PaethPredictor(last,*old,upperlast);
414 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
426 unsigned long int imagedatalen;
427 unsigned long int zimagedatalen=0;
431 int alphapalettelen = 0;
432 struct png_header header;
438 if ((fi = fopen(sname, "rb")) == NULL) {
440 fprintf(stderr, "Read access failed: %s\n", sname);
444 if(!png_read_header(fi, &header))
447 if(header.mode == 3) bypp = 1;
449 if(header.mode == 2) bypp = 3;
451 if(header.mode == 6) bypp = 4;
454 imagedatalen = bypp * header.width * header.height + 65536;
455 imagedata = malloc(imagedatalen);
457 fseek(fi,8,SEEK_SET);
460 if(!png_read_chunk(&tagid, &len, &data, fi))
462 if(!strncmp(tagid, "IEND", 4)) {
465 if(!strncmp(tagid, "PLTE", 4)) {
468 data = 0; //don't free data
470 printf("%d colors in palette\n", palettelen);
472 if(!strncmp(tagid, "tRNS", 4)) {
473 if(header.mode == 3) {
475 alphapalettelen = len;
476 data = 0; //don't free data
478 printf("found %d alpha colors\n", alphapalettelen);
481 if(!strncmp(tagid, "IDAT", 4)) {
484 zimagedata = malloc(len);
485 memcpy(zimagedata,data,len);
487 zimagedata = realloc(zimagedata, zimagedatalen+len);
488 memcpy(&zimagedata[zimagedatalen], data, len);
489 zimagedatalen += len;
496 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
497 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
505 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
507 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
509 swf_SetU16(t, id); // id
510 if(header.mode == 2 || header.mode == 6) {
511 U8*data2 = malloc(header.width*header.height*4);
517 /* in case for mode 2, the following also performs 24->32 bit conversion */
518 for(y=0;y<header.height;y++) {
519 int mode = imagedata[pos++]; //filter mode
523 dest = &data2[(y*header.width)*4];
527 /* one byte per pixel */
528 src = &imagedata[pos];
529 pos+=header.width*(header.mode==6?4:3);
531 /* not implemented yet */
536 memset(data2,0,header.width*4);
537 old = &data2[y*header.width*4];
539 old = &data2[(y-1)*header.width*4];
542 applyfilter4(mode, src, old, dest, header.width);
544 applyfilter3(mode, src, old, dest, header.width);
548 /* the image is now compressed and stored in data. Now let's take
549 a look at the alpha values to determine which bitmap type we
552 for(y=0;y<header.height;y++) {
553 U8*l = &data2[(y*header.width)*4];
554 for(x=0;x<header.width;x++) {
555 if(l[x*4+0]==255) transparent++;
556 if(l[x*4+0]==0) opaque++;
559 /* mode 6 images which are not fully opaque or fully transparent
560 will be stored as definejpeg3 */
561 if(header.mode == 6 && transparent != header.width*header.height
562 && opaque != header.width*header.height) {
564 printf("Image has transparency information. Storing as DefineBitsJpeg3 Tag (jpeg+alpha)\n");
566 // we always use quality 100, since png2swf is expected to
567 // use more or less lossless compression
569 swf_SetJPEGBits3(t, header.width, header.height, (RGBA*)data2, 100);
570 t->id = ST_DEFINEBITSJPEG3;
575 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
580 RGBA*rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
581 int swf_width = BYTES_PER_SCANLINE(header.width);
582 U8*data2 = malloc(swf_width*header.height);
583 U8*tmpline = malloc(header.width);
587 fprintf(stderr, "Error: No palette found!\n");
590 /* 24->32 bit conversion */
591 for(i=0;i<palettelen;i++) {
592 rgba[i].r = palette[i*3+0];
593 rgba[i].g = palette[i*3+1];
594 rgba[i].b = palette[i*3+2];
595 if(alphapalette && i<alphapalettelen) {
596 rgba[i].a = alphapalette[i];
597 if(alphapalette[i] == 0) {
598 /* if the color is fully transparent, it doesn't matter
599 what it's rgb values are. furthermore, all Flash
600 players up to Flash 5 can't deal with anything beyond
601 one transparent color with value (00,00,00,00). */
602 rgba[i].r = rgba[i].g = rgba[i].b = 0;
609 for(y=0;y<header.height;y++) {
610 int mode = imagedata[pos++]; //filter mode
612 U8*dest = &data2[y*swf_width];
614 src = &imagedata[pos];
615 if(header.bpp == 8) {
620 U32 v = (1<<header.bpp)-1;
621 for(x=0;x<header.width;x++) {
622 U32 r = src[s/8]<<8 |
625 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
629 pos+=(header.width*header.bpp+7)/8;
633 memset(data2,0,swf_width);
634 old = &data2[y*swf_width];
636 old = &data2[(y-1)*swf_width];
638 applyfilter1(mode, src, old, dest, header.width);
640 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
646 t = swf_InsertTag(t, ST_DEFINESHAPE3);
649 swf_GetMatrix(NULL, &m);
652 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
654 swf_SetU16(t, id + 1); // id
657 r.xmax = header.width * 20;
658 r.ymax = header.height * 20;
661 swf_SetShapeHeader(t, s);
663 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
664 swf_ShapeSetLine(t, s, r.xmax, 0);
665 swf_ShapeSetLine(t, s, 0, r.ymax);
666 swf_ShapeSetLine(t, s, -r.xmax, 0);
667 swf_ShapeSetLine(t, s, 0, -r.ymax);
671 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
672 swf_SetU16(t, 50); // depth
674 t = swf_InsertTag(t, ST_PLACEOBJECT2);
676 swf_GetMatrix(NULL, &m);
677 m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
678 m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
679 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
681 t = swf_InsertTag(t, ST_SHOWFRAME);
689 int CheckInputFile(char *fname, char **realname)
692 char *s = malloc(strlen(fname) + 5);
693 struct png_header head;
700 // Check whether file exists (with typical extensions)
702 if ((fi = fopen(s, "rb")) == NULL) {
703 sprintf(s, "%s.png", fname);
704 if ((fi = fopen(s, "rb")) == NULL) {
705 sprintf(s, "%s.PNG", fname);
706 if ((fi = fopen(s, "rb")) == NULL) {
707 sprintf(s, "%s.Png", fname);
708 if ((fi = fopen(s, "rb")) == NULL)
709 fprintf(stderr, "Couldn't open %s!\n", fname);
715 if(!png_read_header(fi, &head)) {
716 fprintf(stderr, "%s is not a PNG file!\n", fname);
720 if (global.max_image_width < head.width)
721 global.max_image_width = head.width;
722 if (global.max_image_height < head.height)
723 global.max_image_height = head.height;
730 int args_callback_option(char *arg, char *val)
739 global.framerate = atoi(val);
740 if ((global.framerate < 1) ||(global.framerate > 5000)) {
743 "Error: You must specify a valid framerate between 1 and 10000.\n");
751 global.outfile = val;
757 global.verbose = atoi(val);
763 global.force_width = atoi(val);
769 global.force_height = atoi(val);
774 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
784 fprintf(stderr, "Unknown option: -%s\n", arg);
791 struct options_t options[] =
801 int args_callback_longoption(char *name, char *val)
803 return args_long2shortoption(options, name, val);
806 int args_callback_command(char *arg, char *next) // actually used as filename
809 if (CheckInputFile(arg, &s) < 0) {
811 fprintf(stderr, "Error opening input file: %s\n", arg);
814 image[global.nfiles].filename = s;
816 if (global.nfiles >= MAX_INPUT_FILES) {
818 fprintf(stderr, "Error: Too many input files.\n");
825 void args_callback_usage(char *name)
827 printf("Usage: %s [-options [value]] imagefiles[.png] [...]\n", name);
828 printf("-r framerate (rate) Set movie framerate (100/sec)\n");
829 printf("-o outputfile (output) Set name for SWF output file\n");
830 printf("-X pixel (width) Force movie width to pixel (default: autodetect)\n");
831 printf("-Y pixel (height) Force movie height to pixel (default: autodetect)\n");
832 printf("-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
833 printf("-V (version) Print version information and exit\n");
837 int main(int argc, char **argv)
842 memset(&global, 0x00, sizeof(global));
844 global.framerate = 100;
847 processargs(argc, argv);
853 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
855 t = MovieStart(&swf, global.framerate,
856 global.force_width ? global.force_width : global.
858 global.force_height ? global.force_height : global.
863 for (i = 0; i < global.nfiles; i++) {
865 fprintf(stderr, "[%03i] %s\n", i,
867 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
868 free(image[i].filename);
872 MovieFinish(&swf, t, global.outfile);