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, float framerate, int dx, int dy)
53 memset(swf, 0x00, sizeof(SWF));
56 swf->frameRate = (int)(256.0 * 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);
172 fprintf(stderr, "(This is a grayscale image with alpha channel-\n");
173 fprintf(stderr, " try converting it into an RGB image with alpha channel)\n");
177 if(a!=8 && (b==2 || b==6)) {
178 fprintf(stderr, "Bpp %d in mode %d not supported!\n", a);
182 fprintf(stderr, "Compression mode %d not supported!\n", c);
186 fprintf(stderr, "Filter mode %d not supported!\n", f);
190 fprintf(stderr, "Interlace mode %d not supported!\n", i);
194 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
205 typedef unsigned char byte;
206 #define ABS(a) ((a)>0?(a):(-(a)))
207 byte inline PaethPredictor (byte a,byte b,byte c)
209 // a = left, b = above, c = upper left
210 int p = a + b - c; // initial estimate
211 int pa = ABS(p - a); // distances to a, b, c
214 // return nearest of a,b,c,
215 // breaking ties in order a,b,c.
216 if (pa <= pb && pa <= pc)
223 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
226 unsigned char lastr=0;
227 unsigned char lastg=0;
228 unsigned char lastb=0;
229 unsigned char upperlastr=0;
230 unsigned char upperlastg=0;
231 unsigned char upperlastb=0;
234 for(x=0;x<width;x++) {
244 for(x=0;x<width;x++) {
246 dest[1] = src[0]+lastr;
247 dest[2] = src[1]+lastg;
248 dest[3] = src[2]+lastb;
257 for(x=0;x<width;x++) {
259 dest[1] = src[0]+old[1];
260 dest[2] = src[1]+old[2];
261 dest[3] = src[2]+old[3];
268 for(x=0;x<width;x++) {
270 dest[1] = src[0]+(old[1]+lastr)/2;
271 dest[2] = src[1]+(old[2]+lastg)/2;
272 dest[3] = src[2]+(old[3]+lastb)/2;
282 for(x=0;x<width;x++) {
284 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
285 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
286 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
301 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
304 unsigned char lastr=0;
305 unsigned char lastg=0;
306 unsigned char lastb=0;
307 unsigned char lasta=0;
308 unsigned char upperlastr=0;
309 unsigned char upperlastg=0;
310 unsigned char upperlastb=0;
311 unsigned char upperlasta=0;
314 for(x=0;x<width;x++) {
324 for(x=0;x<width;x++) {
325 dest[0] = src[3]+lasta;
326 dest[1] = src[0]+lastr;
327 dest[2] = src[1]+lastg;
328 dest[3] = src[2]+lastb;
338 for(x=0;x<width;x++) {
339 dest[0] = src[3]+old[0];
340 dest[1] = src[0]+old[1];
341 dest[2] = src[1]+old[2];
342 dest[3] = src[2]+old[3];
349 for(x=0;x<width;x++) {
350 dest[0] = src[3]+(old[0]+lasta)/2;
351 dest[1] = src[0]+(old[1]+lastr)/2;
352 dest[2] = src[1]+(old[2]+lastg)/2;
353 dest[3] = src[2]+(old[3]+lastb)/2;
363 for(x=0;x<width;x++) {
364 dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
365 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
366 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
367 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
384 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
387 unsigned char last=0;
388 unsigned char upperlast=0;
391 for(x=0;x<width;x++) {
398 for(x=0;x<width;x++) {
406 for(x=0;x<width;x++) {
414 for(x=0;x<width;x++) {
415 *dest = *src+(*old+last)/2;
422 for(x=0;x<width;x++) {
423 *dest = *src+PaethPredictor(last,*old,upperlast);
434 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
446 unsigned long int imagedatalen;
447 unsigned long int zimagedatalen=0;
451 int alphapalettelen = 0;
452 struct png_header header;
458 if ((fi = fopen(sname, "rb")) == NULL) {
460 fprintf(stderr, "Read access failed: %s\n", sname);
464 if(!png_read_header(fi, &header))
467 if(header.mode == 3 || header.mode == 0) bypp = 1;
469 if(header.mode == 2) bypp = 3;
471 if(header.mode == 6) bypp = 4;
474 imagedatalen = bypp * header.width * header.height + 65536;
475 imagedata = malloc(imagedatalen);
477 fseek(fi,8,SEEK_SET);
480 if(!png_read_chunk(&tagid, &len, &data, fi))
482 if(!strncmp(tagid, "IEND", 4)) {
485 if(!strncmp(tagid, "PLTE", 4)) {
488 data = 0; //don't free data
490 printf("%d colors in palette\n", palettelen);
492 if(!strncmp(tagid, "tRNS", 4)) {
493 if(header.mode == 3) {
495 alphapalettelen = len;
496 data = 0; //don't free data
498 printf("found %d alpha colors\n", alphapalettelen);
501 if(!strncmp(tagid, "IDAT", 4)) {
504 zimagedata = malloc(len);
505 memcpy(zimagedata,data,len);
507 zimagedata = realloc(zimagedata, zimagedatalen+len);
508 memcpy(&zimagedata[zimagedatalen], data, len);
509 zimagedatalen += len;
516 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
517 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
525 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
527 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
529 swf_SetU16(t, id); // id
530 if(header.mode == 2 || header.mode == 6) {
531 U8*data2 = malloc(header.width*header.height*4);
537 /* in case for mode 2, the following also performs 24->32 bit conversion */
538 for(y=0;y<header.height;y++) {
539 int mode = imagedata[pos++]; //filter mode
543 dest = &data2[(y*header.width)*4];
547 /* one byte per pixel */
548 src = &imagedata[pos];
549 pos+=header.width*(header.mode==6?4:3);
551 /* not implemented yet */
556 memset(data2,0,header.width*4);
557 old = &data2[y*header.width*4];
559 old = &data2[(y-1)*header.width*4];
562 applyfilter4(mode, src, old, dest, header.width);
563 else if(header.mode==2)
564 applyfilter3(mode, src, old, dest, header.width);
568 /* the image is now compressed and stored in data. Now let's take
569 a look at the alpha values to determine which bitmap type we
572 for(y=0;y<header.height;y++) {
573 U8*l = &data2[(y*header.width)*4];
574 for(x=0;x<header.width;x++) {
575 if(l[x*4+0]==255) transparent++;
576 if(l[x*4+0]==0) opaque++;
579 /* mode 6 images which are not fully opaque or fully transparent
580 will be stored as definejpeg3 */
581 if(header.mode == 6 && transparent != header.width*header.height
582 && opaque != header.width*header.height) {
584 printf("Image has transparency information. Storing as DefineBitsJpeg3 Tag (jpeg+alpha)\n");
586 // we always use quality 100, since png2swf is expected to
587 // use more or less lossless compression
589 swf_SetJPEGBits3(t, header.width, header.height, (RGBA*)data2, 100);
590 t->id = ST_DEFINEBITSJPEG3;
595 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
598 } else if(header.mode == 0 || header.mode == 3) {
600 int swf_width = BYTES_PER_SCANLINE(header.width);
601 U8*data2 = malloc(swf_width*header.height);
602 U8*tmpline = malloc(header.width);
605 if(header.mode == 3) { // palette or grayscale?
606 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
608 fprintf(stderr, "Error: No palette found!\n");
611 /* 24->32 bit conversion */
612 for(i=0;i<palettelen;i++) {
613 rgba[i].r = palette[i*3+0];
614 rgba[i].g = palette[i*3+1];
615 rgba[i].b = palette[i*3+2];
616 if(alphapalette && i<alphapalettelen) {
617 rgba[i].a = alphapalette[i];
618 if(alphapalette[i] == 0) {
619 /* if the color is fully transparent, it doesn't matter
620 what it's rgb values are. furthermore, all Flash
621 players up to Flash 5 can't deal with anything beyond
622 one transparent color with value (00,00,00,00). */
623 rgba[i].r = rgba[i].g = rgba[i].b = 0;
631 rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
639 for(y=0;y<header.height;y++) {
640 int mode = imagedata[pos++]; //filter mode
642 U8*dest = &data2[y*swf_width];
644 src = &imagedata[pos];
645 if(header.bpp == 8) {
650 U32 v = (1<<header.bpp)-1;
651 for(x=0;x<header.width;x++) {
652 U32 r = src[s/8]<<8 |
655 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
659 pos+=(header.width*header.bpp+7)/8;
663 memset(data2,0,swf_width);
664 old = &data2[y*swf_width];
666 old = &data2[(y-1)*swf_width];
668 applyfilter1(mode, src, old, dest, header.width);
670 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
676 t = swf_InsertTag(t, ST_DEFINESHAPE3);
679 swf_GetMatrix(NULL, &m);
682 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
684 swf_SetU16(t, id + 1); // id
687 r.xmax = header.width * 20;
688 r.ymax = header.height * 20;
691 swf_SetShapeHeader(t, s);
693 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
694 swf_ShapeSetLine(t, s, r.xmax, 0);
695 swf_ShapeSetLine(t, s, 0, r.ymax);
696 swf_ShapeSetLine(t, s, -r.xmax, 0);
697 swf_ShapeSetLine(t, s, 0, -r.ymax);
701 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
702 swf_SetU16(t, 50); // depth
704 t = swf_InsertTag(t, ST_PLACEOBJECT2);
706 swf_GetMatrix(NULL, &m);
707 m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
708 m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
709 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
711 t = swf_InsertTag(t, ST_SHOWFRAME);
719 int CheckInputFile(char *fname, char **realname)
722 char *s = malloc(strlen(fname) + 5);
723 struct png_header head;
730 // Check whether file exists (with typical extensions)
732 if ((fi = fopen(s, "rb")) == NULL) {
733 sprintf(s, "%s.png", fname);
734 if ((fi = fopen(s, "rb")) == NULL) {
735 sprintf(s, "%s.PNG", fname);
736 if ((fi = fopen(s, "rb")) == NULL) {
737 sprintf(s, "%s.Png", fname);
738 if ((fi = fopen(s, "rb")) == NULL)
739 fprintf(stderr, "Couldn't open %s!\n", fname);
745 if(!png_read_header(fi, &head)) {
746 fprintf(stderr, "%s is not a PNG file!\n", fname);
750 if (global.max_image_width < head.width)
751 global.max_image_width = head.width;
752 if (global.max_image_height < head.height)
753 global.max_image_height = head.height;
760 int args_callback_option(char *arg, char *val)
769 global.framerate = atof(val);
770 if ((global.framerate < 1.0/256) ||(global.framerate >= 256.0)) {
773 "Error: You must specify a valid framerate between 1/256 and 255.\n");
781 global.outfile = val;
787 global.verbose = atoi(val);
793 global.force_width = atoi(val);
799 global.force_height = atoi(val);
804 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
814 fprintf(stderr, "Unknown option: -%s\n", arg);
821 struct options_t options[] =
833 int args_callback_longoption(char *name, char *val)
835 return args_long2shortoption(options, name, val);
838 int args_callback_command(char *arg, char *next) // actually used as filename
841 if (CheckInputFile(arg, &s) < 0) {
843 fprintf(stderr, "Error opening input file: %s\n", arg);
846 image[global.nfiles].filename = s;
848 if (global.nfiles >= MAX_INPUT_FILES) {
850 fprintf(stderr, "Error: Too many input files.\n");
857 void args_callback_usage(char *name)
859 printf("Usage: %s [-options [value]] imagefiles[.png] [...]\n", name);
860 printf("-r framerate (rate) Set movie framerate (frames per second)\n");
861 printf("-o outputfile (output) Set name for SWF output file\n");
862 printf("-X pixel (width) Force movie width to pixel (default: autodetect)\n");
863 printf("-Y pixel (height) Force movie height to pixel (default: autodetect)\n");
864 printf("-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
865 printf("-V (version) Print version information and exit\n");
869 int main(int argc, char **argv)
874 memset(&global, 0x00, sizeof(global));
876 global.framerate = 1.0;
879 processargs(argc, argv);
885 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
887 t = MovieStart(&swf, global.framerate,
888 global.force_width ? global.force_width : global.
890 global.force_height ? global.force_height : global.
895 for (i = 0; i < global.nfiles; i++) {
897 fprintf(stderr, "[%03i] %s\n", i,
899 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
900 free(image[i].filename);
904 MovieFinish(&swf, t, global.outfile);