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;
58 int MovieFinish(SWF * swf, TAG * t, char *sname)
60 int handle, so = fileno(stdout);
61 t = swf_InsertTag(t, ST_END);
63 if ((!isatty(so)) && (!sname))
68 handle = open(sname, O_RDWR | O_CREAT | O_TRUNC, 0666);
71 (swf_WriteSWF(handle, swf)) if (VERBOSE(1))
72 fprintf(stderr, "Unable to write output file: %s\n", sname);
80 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
83 if(destlen) *destlen=0;
84 if(destdata) *destdata=0;
85 if(!fread(&len, 4, 1, fi))
87 if(!fread(head, 4, 1, fi))
89 len = REVERSESWAP32(len);
90 if(destlen) *destlen = len;
93 *destdata = malloc(len);
96 if(!fread(*destdata, len, 1, fi)) {
98 if(destlen) *destlen=0;
101 fseek(fi, 4, SEEK_CUR);
104 fseek(fi, len+4, SEEK_CUR);
109 unsigned int png_get_dword(FILE*fi)
113 return REVERSESWAP32(a);
124 int png_read_header(FILE*fi, struct png_header*header)
129 U8 head[8] = {137,80,78,71,13,10,26,10};
133 if(strncmp(head,head2,4))
136 while(png_read_chunk(&id, &len, &data, fi))
139 printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
140 if(!strncasecmp(id, "IHDR", 4)) {
143 header->width = REVERSESWAP32(*(U32*)&data[0]);
144 header->height = REVERSESWAP32(*(U32*)&data[4]);
145 a = data[8]; // should be 8
146 b = data[9]; // should be 3(indexed) or 2(rgb)
148 c = data[10]; // compression mode (0)
149 f = data[11]; // filter mode (0)
150 i = data[12]; // interlace mode (0)
152 if(b!=2 && b!=3 && b!=6) {
153 fprintf(stderr, "Image mode %d not supported!\n", b);
156 if(a!=8 && (b==2 || b==6)) {
157 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
161 fprintf(stderr, "Compression mode %d not supported!\n", c);
165 fprintf(stderr, "Filter mode %d not supported!\n", f);
169 fprintf(stderr, "Interlace mode %d not supported!\n", i);
173 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
184 typedef unsigned char byte;
185 #define ABS(a) ((a)>0?(a):(-(a)))
186 byte inline PaethPredictor (byte a,byte b,byte c)
188 // a = left, b = above, c = upper left
189 int p = a + b - c; // initial estimate
190 int pa = ABS(p - a); // distances to a, b, c
193 // return nearest of a,b,c,
194 // breaking ties in order a,b,c.
195 if (pa <= pb && pa <= pc)
202 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
205 unsigned char lastr=0;
206 unsigned char lastg=0;
207 unsigned char lastb=0;
208 unsigned char upperlastr=0;
209 unsigned char upperlastg=0;
210 unsigned char upperlastb=0;
213 for(x=0;x<width;x++) {
223 for(x=0;x<width;x++) {
225 dest[1] = src[0]+lastr;
226 dest[2] = src[1]+lastg;
227 dest[3] = src[2]+lastb;
236 for(x=0;x<width;x++) {
238 dest[1] = src[0]+old[1];
239 dest[2] = src[1]+old[2];
240 dest[3] = src[2]+old[3];
247 for(x=0;x<width;x++) {
249 dest[1] = src[0]+(old[1]+lastr)/2;
250 dest[2] = src[1]+(old[2]+lastg)/2;
251 dest[3] = src[2]+(old[3]+lastb)/2;
261 for(x=0;x<width;x++) {
263 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
264 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
265 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
280 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
283 unsigned char lastr=0;
284 unsigned char lastg=0;
285 unsigned char lastb=0;
286 unsigned char lasta=0;
287 unsigned char upperlastr=0;
288 unsigned char upperlastg=0;
289 unsigned char upperlastb=0;
290 unsigned char upperlasta=0;
293 for(x=0;x<width;x++) {
303 for(x=0;x<width;x++) {
304 dest[0] = src[3]+lasta;
305 dest[1] = src[0]+lastr;
306 dest[2] = src[1]+lastg;
307 dest[3] = src[2]+lastb;
317 for(x=0;x<width;x++) {
318 dest[0] = src[3]+old[0];
319 dest[1] = src[0]+old[1];
320 dest[2] = src[1]+old[2];
321 dest[3] = src[2]+old[3];
328 for(x=0;x<width;x++) {
329 dest[0] = src[3]+(old[0]+lasta)/2;
330 dest[1] = src[0]+(old[1]+lastr)/2;
331 dest[2] = src[1]+(old[2]+lastg)/2;
332 dest[3] = src[2]+(old[3]+lastb)/2;
342 for(x=0;x<width;x++) {
343 dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
344 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
345 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
346 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
363 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
366 unsigned char last=0;
367 unsigned char upperlast=0;
370 for(x=0;x<width;x++) {
377 for(x=0;x<width;x++) {
385 for(x=0;x<width;x++) {
393 for(x=0;x<width;x++) {
394 *dest = *src+(*old+last)/2;
401 for(x=0;x<width;x++) {
402 *dest = *src+PaethPredictor(last,*old,upperlast);
413 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
425 unsigned long int imagedatalen;
426 unsigned long int zimagedatalen=0;
430 int alphapalettelen = 0;
431 struct png_header header;
437 if ((fi = fopen(sname, "rb")) == NULL) {
439 fprintf(stderr, "Read access failed: %s\n", sname);
443 if(!png_read_header(fi, &header))
446 if(header.mode == 3) bypp = 1;
448 if(header.mode == 2) bypp = 3;
450 if(header.mode == 6) bypp = 4;
453 imagedatalen = bypp * header.width * header.height + 65536;
454 imagedata = malloc(imagedatalen);
456 fseek(fi,8,SEEK_SET);
459 if(!png_read_chunk(&tagid, &len, &data, fi))
461 if(!strncmp(tagid, "IEND", 4)) {
464 if(!strncmp(tagid, "PLTE", 4)) {
467 data = 0; //don't free data
469 printf("%d colors in palette\n", palettelen);
471 if(!strncmp(tagid, "tRNS", 4)) {
472 if(header.mode == 3) {
474 alphapalettelen = len;
475 data = 0; //don't free data
477 printf("found %d alpha colors\n", alphapalettelen);
480 if(!strncmp(tagid, "IDAT", 4)) {
483 zimagedata = malloc(len);
484 memcpy(zimagedata,data,len);
486 zimagedata = realloc(zimagedata, zimagedatalen+len);
487 memcpy(&zimagedata[zimagedatalen], data, len);
488 zimagedatalen += len;
495 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
496 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
504 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
506 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
508 swf_SetU16(t, id); // id
509 if(header.mode == 2 || header.mode == 6) {
510 U8*data2 = malloc(header.width*header.height*4);
514 /* in case for mode 2, the following also performs 24->32 bit conversion */
515 for(y=0;y<header.height;y++) {
516 int mode = imagedata[pos++]; //filter mode
520 dest = &data2[(y*header.width)*4];
524 /* one byte per pixel */
525 src = &imagedata[pos];
526 pos+=header.width*(header.mode==6?4:3);
528 /* not implemented yet */
533 memset(data2,0,header.width*4);
534 old = &data2[y*header.width*4];
536 old = &data2[(y-1)*header.width*4];
539 applyfilter4(mode, src, old, dest, header.width);
541 applyfilter3(mode, src, old, dest, header.width);
543 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
547 RGBA*rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
548 int swf_width = BYTES_PER_SCANLINE(header.width);
549 U8*data2 = malloc(swf_width*header.height);
550 U8*tmpline = malloc(header.width);
554 fprintf(stderr, "Error: No palette found!\n");
557 /* 24->32 bit conversion */
558 for(i=0;i<palettelen;i++) {
559 rgba[i].r = palette[i*3+0];
560 rgba[i].g = palette[i*3+1];
561 rgba[i].b = palette[i*3+2];
562 if(alphapalette && i<alphapalettelen) {
563 rgba[i].a = alphapalette[i];
564 if(alphapalette[i] == 0) {
565 /* if the color is fully transparent, it doesn't matter
566 what it's rgb values are. furthermore, all Flash
567 players up to Flash 5 can't deal with anything beyond
568 one transparent color with value (00,00,00,00). */
569 rgba[i].r = rgba[i].g = rgba[i].b = 0;
576 for(y=0;y<header.height;y++) {
577 int mode = imagedata[pos++]; //filter mode
579 U8*dest = &data2[y*swf_width];
581 src = &imagedata[pos];
582 if(header.bpp == 8) {
587 U32 v = (1<<header.bpp)-1;
588 for(x=0;x<header.width;x++) {
589 U32 r = src[s/8]<<8 |
592 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
596 pos+=(header.width*header.bpp+7)/8;
600 memset(data2,0,swf_width);
601 old = &data2[y*swf_width];
603 old = &data2[(y-1)*swf_width];
605 applyfilter1(mode, src, old, dest, header.width);
607 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
613 t = swf_InsertTag(t, ST_DEFINESHAPE);
616 swf_GetMatrix(NULL, &m);
619 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
621 swf_SetU16(t, id + 1); // id
624 r.xmax = header.width * 20;
625 r.ymax = header.height * 20;
628 swf_SetShapeHeader(t, s);
630 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
631 swf_ShapeSetLine(t, s, r.xmax, 0);
632 swf_ShapeSetLine(t, s, 0, r.ymax);
633 swf_ShapeSetLine(t, s, -r.xmax, 0);
634 swf_ShapeSetLine(t, s, 0, -r.ymax);
638 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
639 swf_SetU16(t, 1); // depth
641 t = swf_InsertTag(t, ST_PLACEOBJECT2);
643 swf_GetMatrix(NULL, &m);
644 m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
645 m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
646 swf_ObjectPlace(t, id + 1, 1, &m, NULL, NULL);
648 t = swf_InsertTag(t, ST_SHOWFRAME);
656 int CheckInputFile(char *fname, char **realname)
659 char *s = malloc(strlen(fname) + 5);
660 struct png_header head;
667 // Check whether file exists (with typical extensions)
669 if ((fi = fopen(s, "rb")) == NULL) {
670 sprintf(s, "%s.png", fname);
671 if ((fi = fopen(s, "rb")) == NULL) {
672 sprintf(s, "%s.PNG", fname);
673 if ((fi = fopen(s, "rb")) == NULL) {
674 sprintf(s, "%s.Png", fname);
675 if ((fi = fopen(s, "rb")) == NULL)
676 fprintf(stderr, "Couldn't open %s!\n", fname);
682 if(!png_read_header(fi, &head)) {
683 fprintf(stderr, "%s is not a PNG file!\n", fname);
687 if (global.max_image_width < head.width)
688 global.max_image_width = head.width;
689 if (global.max_image_height < head.height)
690 global.max_image_height = head.height;
697 int args_callback_option(char *arg, char *val)
706 global.framerate = atoi(val);
707 if ((global.framerate < 1) ||(global.framerate > 5000)) {
710 "Error: You must specify a valid framerate between 1 and 10000.\n");
718 global.outfile = val;
724 global.verbose = atoi(val);
730 global.force_width = atoi(val);
736 global.force_height = atoi(val);
741 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
751 fprintf(stderr, "Unknown option: -%s\n", arg);
758 struct options_t options[] =
768 int args_callback_longoption(char *name, char *val)
770 return args_long2shortoption(options, name, val);
773 int args_callback_command(char *arg, char *next) // actually used as filename
776 if (CheckInputFile(arg, &s) < 0) {
778 fprintf(stderr, "Error opening input file: %s\n", arg);
781 image[global.nfiles].filename = s;
783 if (global.nfiles >= MAX_INPUT_FILES) {
785 fprintf(stderr, "Error: Too many input files.\n");
792 void args_callback_usage(char *name)
794 printf("Usage: %s [-options [value]] imagefiles[.png] [...]\n", name);
795 printf("-r framerate (rate) Set movie framerate (100/sec)\n");
796 printf("-o outputfile (output) Set name for SWF output file\n");
797 printf("-X pixel (width) Force movie width to pixel (default: autodetect)\n");
798 printf("-Y pixel (height) Force movie height to pixel (default: autodetect)\n");
799 printf("-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
800 printf("-V (version) Print version information and exit\n");
804 int main(int argc, char **argv)
809 memset(&global, 0x00, sizeof(global));
811 global.framerate = 100;
814 processargs(argc, argv);
820 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
822 t = MovieStart(&swf, global.framerate,
823 global.force_width ? global.force_width : global.
825 global.force_height ? global.force_height : global.
830 for (i = 0; i < global.nfiles; i++) {
832 fprintf(stderr, "[%03i] %s\n", i,
834 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
835 free(image[i].filename);
839 MovieFinish(&swf, t, global.outfile);