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"
29 #include "../lib/png.h"
31 #define MAX_INPUT_FILES 1024
32 #define VERBOSE(x) (global.verbose>=x)
51 } image[MAX_INPUT_FILES];
53 static int custom_move=0;
56 static int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
57 static int custom_clip = 0;
59 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
64 memset(swf, 0x00, sizeof(SWF));
66 swf->fileVersion = global.version;
67 swf->frameRate = (int)(256.0 * framerate);
69 swf->movieSize.xmin = clip_x1 * 20;
70 swf->movieSize.ymin = clip_y1 * 20;
71 swf->movieSize.xmax = clip_x2 * 20;
72 swf->movieSize.ymax = clip_y2 * 20;
74 swf->movieSize.xmax = dx * 20;
75 swf->movieSize.ymax = dy * 20;
78 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
80 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
81 //rgb.g = 0xff; //<--- handy for testing alpha conversion
87 int MovieFinish(SWF * swf, TAG * t, char *sname)
89 int f, so = fileno(stdout);
90 t = swf_InsertTag(t, ST_END);
92 if ((!isatty(so)) && (!sname))
97 f = open(sname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
101 if FAILED(swf_WriteCGI(swf)) fprintf(stderr,"WriteCGI() failed.\n");
103 if (swf_WriteSWF(f, swf)<0)
104 fprintf(stderr, "Unable to write output file: %s\n", sname);
113 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
116 if(destlen) *destlen=0;
117 if(destdata) *destdata=0;
118 if(!fread(&len, 4, 1, fi))
120 if(!fread(head, 4, 1, fi))
122 len = BE_32_TO_NATIVE(len);
123 if(destlen) *destlen = len;
126 *destdata = malloc(len);
129 if(!fread(*destdata, len, 1, fi)) {
131 if(destlen) *destlen=0;
134 fseek(fi, 4, SEEK_CUR);
137 fseek(fi, len+4, SEEK_CUR);
142 unsigned int png_get_dword(FILE*fi)
146 return BE_32_TO_NATIVE(a);
157 int png_read_header(FILE*fi, struct png_header*header)
162 U8 head[8] = {137,80,78,71,13,10,26,10};
166 if(strncmp((char*)head,(char*)head2,4))
169 while(png_read_chunk(&id, &len, &data, fi))
172 printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
173 if(!strncasecmp(id, "IHDR", 4)) {
176 header->width = BE_32_TO_NATIVE(*(U32*)&data[0]);
177 header->height = BE_32_TO_NATIVE(*(U32*)&data[4]);
178 a = data[8]; // should be 8
179 b = data[9]; // should be 3(indexed), 2(rgb), 0(grayscale) or 6(truecolor+alpha)
181 c = data[10]; // compression mode (0)
182 f = data[11]; // filter mode (0)
183 i = data[12]; // interlace mode (0)
185 if(VERBOSE(2)) printf("image mode:%d\n", b);
186 if(VERBOSE(2)) printf("bpp: %d\n", a);
187 if(VERBOSE(2)) printf("compression: %d\n", c);
188 if(VERBOSE(2)) printf("filter: %d\n", f);
189 if(VERBOSE(2)) printf("interlace: %d\n", i);
191 if(b!=0 && b!=2 && b!=3 && b!=6) {
192 fprintf(stderr, "Image mode %d not supported!\n", b);
194 fprintf(stderr, "(This is a grayscale image with alpha channel-\n");
195 fprintf(stderr, " try converting it into an RGB image with alpha channel)\n");
199 if(a!=8 && (b==2 || b==6)) {
200 fprintf(stderr, "Bpp %d in mode %d not supported!\n", b, a);
204 fprintf(stderr, "Compression mode %d not supported!\n", c);
208 fprintf(stderr, "Filter mode %d not supported!\n", f);
212 fprintf(stderr, "Interlace mode %d not supported!\n", i);
216 printf("%dx%d %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
227 typedef unsigned char byte;
228 #define ABS(a) ((a)>0?(a):(-(a)))
229 byte inline PaethPredictor (byte a,byte b,byte c)
231 // a = left, b = above, c = upper left
232 int p = a + b - c; // initial estimate
233 int pa = ABS(p - a); // distances to a, b, c
236 // return nearest of a,b,c,
237 // breaking ties in order a,b,c.
238 if (pa <= pb && pa <= pc)
245 void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
248 unsigned char lastr=0;
249 unsigned char lastg=0;
250 unsigned char lastb=0;
251 unsigned char upperlastr=0;
252 unsigned char upperlastg=0;
253 unsigned char upperlastb=0;
256 for(x=0;x<width;x++) {
266 for(x=0;x<width;x++) {
268 dest[1] = src[0]+lastr;
269 dest[2] = src[1]+lastg;
270 dest[3] = src[2]+lastb;
279 for(x=0;x<width;x++) {
281 dest[1] = src[0]+old[1];
282 dest[2] = src[1]+old[2];
283 dest[3] = src[2]+old[3];
290 for(x=0;x<width;x++) {
292 dest[1] = src[0]+(old[1]+lastr)/2;
293 dest[2] = src[1]+(old[2]+lastg)/2;
294 dest[3] = src[2]+(old[3]+lastb)/2;
304 for(x=0;x<width;x++) {
306 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
307 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
308 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
322 void applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
325 unsigned char lastr=0;
326 unsigned char lastg=0;
327 unsigned char lastb=0;
328 unsigned char lasta=0; //TODO: 255?
329 unsigned char upperlastr=0;
330 unsigned char upperlastg=0;
331 unsigned char upperlastb=0;
332 unsigned char upperlasta=0; //TODO: 255?
335 for(x=0;x<width;x++) {
345 for(x=0;x<width;x++) {
346 dest[0] = src[3]+lasta;
347 dest[1] = src[0]+lastr;
348 dest[2] = src[1]+lastg;
349 dest[3] = src[2]+lastb;
359 for(x=0;x<width;x++) {
360 dest[0] = src[3]+old[0];
361 dest[1] = src[0]+old[1];
362 dest[2] = src[1]+old[2];
363 dest[3] = src[2]+old[3];
370 for(x=0;x<width;x++) {
371 dest[0] = src[3]+(old[0]+lasta)/2;
372 dest[1] = src[0]+(old[1]+lastr)/2;
373 dest[2] = src[1]+(old[2]+lastg)/2;
374 dest[3] = src[2]+(old[3]+lastb)/2;
385 for(x=0;x<width;x++) {
386 dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
387 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
388 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
389 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
406 void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
409 unsigned char last=0;
410 unsigned char upperlast=0;
413 for(x=0;x<width;x++) {
420 for(x=0;x<width;x++) {
428 for(x=0;x<width;x++) {
436 for(x=0;x<width;x++) {
437 *dest = *src+(*old+last)/2;
445 for(x=0;x<width;x++) {
446 *dest = *src+PaethPredictor(last,*old,upperlast);
457 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
464 unsigned width=0, height=0;
469 msg("<warning> No jpeg support compiled in");
475 png_load(sname, &width, &height, (unsigned char**)&data);
478 if(swf_ImageHasAlpha(data, width, height)) {
479 t = swf_InsertTag(t, ST_DEFINEBITSJPEG3);
481 swf_SetJPEGBits3(t, width,height,data,global.mkjpeg);
483 t = swf_InsertTag(t, ST_DEFINEBITSJPEG2);
485 swf_SetJPEGBits2(t, width,height,data,global.mkjpeg);
490 png_load(sname, &width, &height, (unsigned char**)&data);
493 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
495 swf_SetLosslessImage(t, data,width,height);
498 t = swf_InsertTag(t, ST_DEFINESHAPE3);
501 swf_GetMatrix(NULL, &m);
502 m.sx = (int)(20 * 0x10000);
503 m.sy = (int)(20 * 0x10000);
506 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 1);
508 swf_SetU16(t, id + 1); // id
512 r.ymax = height * 20;
515 swf_SetShapeHeader(t, s);
517 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
518 swf_ShapeSetLine(t, s, r.xmax, 0);
519 swf_ShapeSetLine(t, s, 0, r.ymax);
520 swf_ShapeSetLine(t, s, -r.xmax, 0);
521 swf_ShapeSetLine(t, s, 0, -r.ymax);
525 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
526 swf_SetU16(t, 50); // depth
528 t = swf_InsertTag(t, ST_PLACEOBJECT2);
530 swf_GetMatrix(NULL, &m);
531 m.sx = (int)(0x10000 * global.scale);
532 m.sy = (int)(0x10000 * global.scale);
538 m.tx = (swf->movieSize.xmax - (int) (width * global.scale * 20)) / 2;
539 m.ty = (swf->movieSize.ymax - (int) (height * global.scale * 20)) / 2;
541 swf_ObjectPlace(t, id + 1, 50, &m, NULL, NULL);
543 t = swf_InsertTag(t, ST_SHOWFRAME);
549 int CheckInputFile(char *fname, char **realname)
552 char *s = malloc(strlen(fname) + 5);
553 struct png_header head;
560 // Check whether file exists (with typical extensions)
562 if ((fi = fopen(s, "rb")) == NULL) {
563 sprintf(s, "%s.png", fname);
564 if ((fi = fopen(s, "rb")) == NULL) {
565 sprintf(s, "%s.PNG", fname);
566 if ((fi = fopen(s, "rb")) == NULL) {
567 sprintf(s, "%s.Png", fname);
568 if ((fi = fopen(s, "rb")) == NULL) {
569 fprintf(stderr, "Couldn't open %s!\n", fname);
576 if(!png_read_header(fi, &head)) {
577 fprintf(stderr, "%s is not a PNG file!\n", fname);
581 if (global.max_image_width < head.width)
582 global.max_image_width = head.width;
583 if (global.max_image_height < head.height)
584 global.max_image_height = head.height;
591 int args_callback_option(char *arg, char *val)
600 global.framerate = atof(val);
601 /* removed framerate>0 restriction in order to make
602 Flash Communication Server compatible SWFs */
603 if ((global.framerate < 0) ||(global.framerate >= 256.0)) {
606 "Error: You must specify a valid framerate between 1/256 and 255.\n");
614 global.outfile = val;
619 global.scale = atof(val)/100;
630 global.mkjpeg = atoi(val);
635 global.version = atoi(val);
657 global.force_width = atoi(val);
663 global.force_height = atoi(val);
668 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
672 char*s = strdup(val);
673 char*x1 = strtok(s, ":");
674 char*y1 = strtok(0, ":");
675 char*x2 = strtok(0, ":");
676 char*y2 = strtok(0, ":");
677 if(!(x1 && y1 && x2 && y2)) {
678 fprintf(stderr, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
693 char*s = strdup(val);
694 char*c = strchr(s, ':');
696 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
716 fprintf(stderr, "Unknown option: -%s\n", arg);
723 static struct options_t options[] = {
728 {"T", "flashversion"},
739 int args_callback_longoption(char *name, char *val)
741 return args_long2shortoption(options, name, val);
744 int args_callback_command(char *arg, char *next) // actually used as filename
747 if (CheckInputFile(arg, &s) < 0) {
749 fprintf(stderr, "Error opening input file: %s\n", arg);
752 image[global.nfiles].filename = s;
754 if (global.nfiles >= MAX_INPUT_FILES) {
756 fprintf(stderr, "Error: Too many input files.\n");
763 void args_callback_usage(char *name)
766 printf("Usage: %s [-X width] [-Y height] [-o file.swf] [-r rate] file1.png [file2.png...]\n", name);
768 printf("-r , --rate <framerate> Set movie framerate (frames per second)\n");
769 printf("-o , --output <filename> Set name for SWF output file.\n");
770 printf("-j , --jpeg <quality> Generate a lossy jpeg bitmap inside the SWF, with a given quality (1-100)\n");
771 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
772 printf("-T , --flashversion Set the flash version to generate\n");
773 printf("-X , --pixel <width> Force movie width to <width> (default: autodetect)\n");
774 printf("-Y , --pixel <height> Force movie height to <height> (default: autodetect)\n");
775 printf("-v , --verbose <level> Set verbose level (0=quiet, 1=default, 2=debug)\n");
776 printf("-q , --quiet Omit normal log messages, only log errors\n");
777 printf("-C , --cgi For use as CGI- prepend http header, write to stdout\n");
778 printf("-V , --version Print version information and exit\n");
779 printf("-s , --scale <percent> Scale image to <percent>%% size.\n");
783 int main(int argc, char **argv)
788 memset(&global, 0x00, sizeof(global));
790 global.framerate = 1.0;
795 processargs(argc, argv);
797 if(global.nfiles<=0) {
798 fprintf(stderr, "No png files found in arguments\n");
803 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
805 t = MovieStart(&swf, global.framerate,
806 global.force_width ? global.force_width : (int)(global.max_image_width*global.scale),
807 global.force_height ? global.force_height : (int)(global.max_image_height*global.scale));
811 for (i = 0; i < global.nfiles; i++) {
813 fprintf(stderr, "[%03i] %s\n", i,
815 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
816 free(image[i].filename);
820 MovieFinish(&swf, t, global.outfile);