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(VERBOSE(2)) printf("image mode:%d\n", b);
164 if(VERBOSE(2)) printf("bpp: %d\n", a);
165 if(VERBOSE(2)) printf("compression: %d\n", c);
166 if(VERBOSE(2)) printf("filter: %d\n", f);
167 if(VERBOSE(2)) printf("interlace: %d\n", i);
169 if(b!=0 && b!=2 && b!=3 && b!=6) {
170 fprintf(stderr, "Image mode %d not supported!\n", b);
173 if(a!=8 && (b==2 || b==6)) {
174 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
178 fprintf(stderr, "Compression mode %d not supported!\n", c);
182 fprintf(stderr, "Filter mode %d not supported!\n", f);
186 fprintf(stderr, "Interlace mode %d not supported!\n", i);
190 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
201 typedef unsigned char byte;
202 #define ABS(a) ((a)>0?(a):(-(a)))
203 byte inline PaethPredictor (byte a,byte b,byte c)
205 // a = left, b = above, c = upper left
206 int p = a + b - c; // initial estimate
207 int pa = ABS(p - a); // distances to a, b, c
210 // return nearest of a,b,c,
211 // breaking ties in order a,b,c.
212 if (pa <= pb && pa <= pc)
219 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
222 unsigned char lastr=0;
223 unsigned char lastg=0;
224 unsigned char lastb=0;
225 unsigned char upperlastr=0;
226 unsigned char upperlastg=0;
227 unsigned char upperlastb=0;
230 for(x=0;x<width;x++) {
240 for(x=0;x<width;x++) {
242 dest[1] = src[0]+lastr;
243 dest[2] = src[1]+lastg;
244 dest[3] = src[2]+lastb;
253 for(x=0;x<width;x++) {
255 dest[1] = src[0]+old[1];
256 dest[2] = src[1]+old[2];
257 dest[3] = src[2]+old[3];
264 for(x=0;x<width;x++) {
266 dest[1] = src[0]+(old[1]+lastr)/2;
267 dest[2] = src[1]+(old[2]+lastg)/2;
268 dest[3] = src[2]+(old[3]+lastb)/2;
278 for(x=0;x<width;x++) {
280 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
281 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
282 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
297 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
300 unsigned char lastr=0;
301 unsigned char lastg=0;
302 unsigned char lastb=0;
303 unsigned char lasta=0;
304 unsigned char upperlastr=0;
305 unsigned char upperlastg=0;
306 unsigned char upperlastb=0;
307 unsigned char upperlasta=0;
310 for(x=0;x<width;x++) {
320 for(x=0;x<width;x++) {
321 dest[0] = src[3]+lasta;
322 dest[1] = src[0]+lastr;
323 dest[2] = src[1]+lastg;
324 dest[3] = src[2]+lastb;
334 for(x=0;x<width;x++) {
335 dest[0] = src[3]+old[0];
336 dest[1] = src[0]+old[1];
337 dest[2] = src[1]+old[2];
338 dest[3] = src[2]+old[3];
345 for(x=0;x<width;x++) {
346 dest[0] = src[3]+(old[0]+lasta)/2;
347 dest[1] = src[0]+(old[1]+lastr)/2;
348 dest[2] = src[1]+(old[2]+lastg)/2;
349 dest[3] = src[2]+(old[3]+lastb)/2;
359 for(x=0;x<width;x++) {
360 dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
361 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
362 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
363 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
380 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
383 unsigned char last=0;
384 unsigned char upperlast=0;
387 for(x=0;x<width;x++) {
394 for(x=0;x<width;x++) {
402 for(x=0;x<width;x++) {
410 for(x=0;x<width;x++) {
411 *dest = *src+(*old+last)/2;
418 for(x=0;x<width;x++) {
419 *dest = *src+PaethPredictor(last,*old,upperlast);
430 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
442 unsigned long int imagedatalen;
443 unsigned long int zimagedatalen=0;
447 int alphapalettelen = 0;
448 struct png_header header;
454 if ((fi = fopen(sname, "rb")) == NULL) {
456 fprintf(stderr, "Read access failed: %s\n", sname);
460 if(!png_read_header(fi, &header))
463 if(header.mode == 3 || header.mode == 0) bypp = 1;
465 if(header.mode == 2) bypp = 3;
467 if(header.mode == 6) bypp = 4;
470 imagedatalen = bypp * header.width * header.height + 65536;
471 imagedata = malloc(imagedatalen);
473 fseek(fi,8,SEEK_SET);
476 if(!png_read_chunk(&tagid, &len, &data, fi))
478 if(!strncmp(tagid, "IEND", 4)) {
481 if(!strncmp(tagid, "PLTE", 4)) {
484 data = 0; //don't free data
486 printf("%d colors in palette\n", palettelen);
488 if(!strncmp(tagid, "tRNS", 4)) {
489 if(header.mode == 3) {
491 alphapalettelen = len;
492 data = 0; //don't free data
494 printf("found %d alpha colors\n", alphapalettelen);
497 if(!strncmp(tagid, "IDAT", 4)) {
500 zimagedata = malloc(len);
501 memcpy(zimagedata,data,len);
503 zimagedata = realloc(zimagedata, zimagedatalen+len);
504 memcpy(&zimagedata[zimagedatalen], data, len);
505 zimagedatalen += len;
512 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
513 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
521 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
523 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
525 swf_SetU16(t, id); // id
526 if(header.mode == 2 || header.mode == 6) {
527 U8*data2 = malloc(header.width*header.height*4);
533 /* in case for mode 2, the following also performs 24->32 bit conversion */
534 for(y=0;y<header.height;y++) {
535 int mode = imagedata[pos++]; //filter mode
539 dest = &data2[(y*header.width)*4];
543 /* one byte per pixel */
544 src = &imagedata[pos];
545 pos+=header.width*(header.mode==6?4:3);
547 /* not implemented yet */
552 memset(data2,0,header.width*4);
553 old = &data2[y*header.width*4];
555 old = &data2[(y-1)*header.width*4];
558 applyfilter4(mode, src, old, dest, header.width);
560 applyfilter3(mode, src, old, dest, header.width);
564 /* the image is now compressed and stored in data. Now let's take
565 a look at the alpha values to determine which bitmap type we
568 for(y=0;y<header.height;y++) {
569 U8*l = &data2[(y*header.width)*4];
570 for(x=0;x<header.width;x++) {
571 if(l[x*4+0]==255) transparent++;
572 if(l[x*4+0]==0) opaque++;
575 /* mode 6 images which are not fully opaque or fully transparent
576 will be stored as definejpeg3 */
577 if(header.mode == 6 && transparent != header.width*header.height
578 && opaque != header.width*header.height) {
580 printf("Image has transparency information. Storing as DefineBitsJpeg3 Tag (jpeg+alpha)\n");
582 // we always use quality 100, since png2swf is expected to
583 // use more or less lossless compression
585 swf_SetJPEGBits3(t, header.width, header.height, (RGBA*)data2, 100);
586 t->id = ST_DEFINEBITSJPEG3;
591 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
595 else if(header.mode == 0 || header.mode == 3) {
597 int swf_width = BYTES_PER_SCANLINE(header.width);
598 U8*data2 = malloc(swf_width*header.height);
599 U8*tmpline = malloc(header.width);
602 if(header.mode == 3) { // palette or grayscale?
603 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
605 fprintf(stderr, "Error: No palette found!\n");
608 /* 24->32 bit conversion */
609 for(i=0;i<palettelen;i++) {
610 rgba[i].r = palette[i*3+0];
611 rgba[i].g = palette[i*3+1];
612 rgba[i].b = palette[i*3+2];
613 if(alphapalette && i<alphapalettelen) {
614 rgba[i].a = alphapalette[i];
615 if(alphapalette[i] == 0) {
616 /* if the color is fully transparent, it doesn't matter
617 what it's rgb values are. furthermore, all Flash
618 players up to Flash 5 can't deal with anything beyond
619 one transparent color with value (00,00,00,00). */
620 rgba[i].r = rgba[i].g = rgba[i].b = 0;
628 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
636 for(y=0;y<header.height;y++) {
637 int mode = imagedata[pos++]; //filter mode
639 U8*dest = &data2[y*swf_width];
641 src = &imagedata[pos];
642 if(header.bpp == 8) {
647 U32 v = (1<<header.bpp)-1;
648 for(x=0;x<header.width;x++) {
649 U32 r = src[s/8]<<8 |
652 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
656 pos+=(header.width*header.bpp+7)/8;
660 memset(data2,0,swf_width);
661 old = &data2[y*swf_width];
663 old = &data2[(y-1)*swf_width];
665 applyfilter1(mode, src, old, dest, header.width);
667 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
673 t = swf_InsertTag(t, ST_DEFINESHAPE3);
676 swf_GetMatrix(NULL, &m);
679 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
681 swf_SetU16(t, id + 1); // id
684 r.xmax = header.width * 20;
685 r.ymax = header.height * 20;
688 swf_SetShapeHeader(t, s);
690 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
691 swf_ShapeSetLine(t, s, r.xmax, 0);
692 swf_ShapeSetLine(t, s, 0, r.ymax);
693 swf_ShapeSetLine(t, s, -r.xmax, 0);
694 swf_ShapeSetLine(t, s, 0, -r.ymax);
698 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
699 swf_SetU16(t, 50); // depth
701 t = swf_InsertTag(t, ST_PLACEOBJECT2);
703 swf_GetMatrix(NULL, &m);
704 m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
705 m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
706 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
708 t = swf_InsertTag(t, ST_SHOWFRAME);
716 int CheckInputFile(char *fname, char **realname)
719 char *s = malloc(strlen(fname) + 5);
720 struct png_header head;
727 // Check whether file exists (with typical extensions)
729 if ((fi = fopen(s, "rb")) == NULL) {
730 sprintf(s, "%s.png", fname);
731 if ((fi = fopen(s, "rb")) == NULL) {
732 sprintf(s, "%s.PNG", fname);
733 if ((fi = fopen(s, "rb")) == NULL) {
734 sprintf(s, "%s.Png", fname);
735 if ((fi = fopen(s, "rb")) == NULL)
736 fprintf(stderr, "Couldn't open %s!\n", fname);
742 if(!png_read_header(fi, &head)) {
743 fprintf(stderr, "%s is not a PNG file!\n", fname);
747 if (global.max_image_width < head.width)
748 global.max_image_width = head.width;
749 if (global.max_image_height < head.height)
750 global.max_image_height = head.height;
757 int args_callback_option(char *arg, char *val)
766 global.framerate = atoi(val);
767 if ((global.framerate < 1) ||(global.framerate > 5000)) {
770 "Error: You must specify a valid framerate between 1 and 10000.\n");
778 global.outfile = val;
784 global.verbose = atoi(val);
790 global.force_width = atoi(val);
796 global.force_height = atoi(val);
801 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
811 fprintf(stderr, "Unknown option: -%s\n", arg);
818 struct options_t options[] =
828 int args_callback_longoption(char *name, char *val)
830 return args_long2shortoption(options, name, val);
833 int args_callback_command(char *arg, char *next) // actually used as filename
836 if (CheckInputFile(arg, &s) < 0) {
838 fprintf(stderr, "Error opening input file: %s\n", arg);
841 image[global.nfiles].filename = s;
843 if (global.nfiles >= MAX_INPUT_FILES) {
845 fprintf(stderr, "Error: Too many input files.\n");
852 void args_callback_usage(char *name)
854 printf("Usage: %s [-options [value]] imagefiles[.png] [...]\n", name);
855 printf("-r framerate (rate) Set movie framerate (100/sec)\n");
856 printf("-o outputfile (output) Set name for SWF output file\n");
857 printf("-X pixel (width) Force movie width to pixel (default: autodetect)\n");
858 printf("-Y pixel (height) Force movie height to pixel (default: autodetect)\n");
859 printf("-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
860 printf("-V (version) Print version information and exit\n");
864 int main(int argc, char **argv)
869 memset(&global, 0x00, sizeof(global));
871 global.framerate = 100;
874 processargs(argc, argv);
880 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
882 t = MovieStart(&swf, global.framerate,
883 global.force_width ? global.force_width : global.
885 global.force_height ? global.force_height : global.
890 for (i = 0; i < global.nfiles; i++) {
892 fprintf(stderr, "[%03i] %s\n", i,
894 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
895 free(image[i].filename);
899 MovieFinish(&swf, t, global.outfile);