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/log.h"
30 #include "../lib/png.h"
32 #define MAX_INPUT_FILES 1024
33 #define VERBOSE(x) (global.verbose>=x)
52 } image
[MAX_INPUT_FILES
];
54 static int custom_move
=0;
57 static int clip_x1
=0,clip_y1
=0,clip_x2
=0,clip_y2
=0;
58 static int custom_clip
= 0;
60 TAG
*MovieStart(SWF
* swf
, float framerate
, int dx
, int dy
)
65 memset(swf
, 0x00, sizeof(SWF
));
67 swf
->fileVersion
= global
.version
;
68 swf
->frameRate
= (int)(256.0 * framerate
);
70 swf
->movieSize
.xmin
= clip_x1
* 20;
71 swf
->movieSize
.ymin
= clip_y1
* 20;
72 swf
->movieSize
.xmax
= clip_x2
* 20;
73 swf
->movieSize
.ymax
= clip_y2
* 20;
75 swf
->movieSize
.xmax
= dx
* 20;
76 swf
->movieSize
.ymax
= dy
* 20;
79 t
= swf
->firstTag
= swf_InsertTag(NULL
, ST_SETBACKGROUNDCOLOR
);
81 rgb
.r
= rgb
.g
= rgb
.b
= rgb
.a
= 0x00;
82 //rgb.g = 0xff; //<--- handy for testing alpha conversion
88 int MovieFinish(SWF
* swf
, TAG
* t
, char *sname
)
90 int f
, so
= fileno(stdout
);
91 t
= swf_InsertTag(t
, ST_END
);
93 if ((!isatty(so
)) && (!sname
))
98 f
= open(sname
,O_WRONLY
|O_CREAT
|O_TRUNC
|O_BINARY
, 0644);
102 if FAILED(swf_WriteCGI(swf
)) fprintf(stderr
,"WriteCGI() failed.\n");
104 if (swf_WriteSWF(f
, swf
)<0)
105 fprintf(stderr
, "Unable to write output file: %s\n", sname
);
114 int png_read_chunk(char (*head
)[4], int*destlen
, U8
**destdata
, FILE*fi
)
117 if(destlen
) *destlen
=0;
118 if(destdata
) *destdata
=0;
119 if(!fread(&len
, 4, 1, fi
))
121 if(!fread(head
, 4, 1, fi
))
123 len
= BE_32_TO_NATIVE(len
);
124 if(destlen
) *destlen
= len
;
127 *destdata
= malloc(len
);
130 if(!fread(*destdata
, len
, 1, fi
)) {
132 if(destlen
) *destlen
=0;
135 fseek(fi
, 4, SEEK_CUR
);
138 fseek(fi
, len
+4, SEEK_CUR
);
143 unsigned int png_get_dword(FILE*fi
)
147 return BE_32_TO_NATIVE(a
);
158 int png_read_header(FILE*fi
, struct png_header
*header
)
163 U8 head
[8] = {137,80,78,71,13,10,26,10};
167 if(strncmp((char*)head
,(char*)head2
,4))
170 while(png_read_chunk(&id
, &len
, &data
, fi
))
173 printf("%c%c%c%c %d\n", id
[0],id
[1],id
[2],id
[3],len
);
174 if(!strncasecmp(id
, "IHDR", 4)) {
177 header
->width
= BE_32_TO_NATIVE(*(U32
*)&data
[0]);
178 header
->height
= BE_32_TO_NATIVE(*(U32
*)&data
[4]);
179 a
= data
[8]; // should be 8
180 b
= data
[9]; // should be 3(indexed), 2(rgb), 0(grayscale) or 6(truecolor+alpha)
182 c
= data
[10]; // compression mode (0)
183 f
= data
[11]; // filter mode (0)
184 i
= data
[12]; // interlace mode (0)
186 if(VERBOSE(2)) printf("image mode:%d\n", b
);
187 if(VERBOSE(2)) printf("bpp: %d\n", a
);
188 if(VERBOSE(2)) printf("compression: %d\n", c
);
189 if(VERBOSE(2)) printf("filter: %d\n", f
);
190 if(VERBOSE(2)) printf("interlace: %d\n", i
);
192 if(b
!=0 && b
!=2 && b
!=3 && b
!=6) {
193 fprintf(stderr
, "Image mode %d not supported!\n", b
);
195 fprintf(stderr
, "(This is a grayscale image with alpha channel-\n");
196 fprintf(stderr
, " try converting it into an RGB image with alpha channel)\n");
200 if(a
!=8 && (b
==2 || b
==6)) {
201 fprintf(stderr
, "Bpp %d in mode %d not supported!\n", b
, a
);
205 fprintf(stderr
, "Compression mode %d not supported!\n", c
);
209 fprintf(stderr
, "Filter mode %d not supported!\n", f
);
213 fprintf(stderr
, "Interlace mode %d not supported!\n", i
);
217 printf("%dx%d %d %d %d %d %d\n",header
->width
, header
->height
, a
,b
,c
,f
,i
);
228 typedef unsigned char byte
;
229 #define ABS(a) ((a)>0?(a):(-(a)))
230 byte
inline PaethPredictor (byte a
,byte b
,byte c
)
232 // a = left, b = above, c = upper left
233 int p
= a
+ b
- c
; // initial estimate
234 int pa
= ABS(p
- a
); // distances to a, b, c
237 // return nearest of a,b,c,
238 // breaking ties in order a,b,c.
239 if (pa
<= pb
&& pa
<= pc
)
246 void applyfilter3(int mode
, U8
*src
, U8
*old
, U8
*dest
, int width
)
249 unsigned char lastr
=0;
250 unsigned char lastg
=0;
251 unsigned char lastb
=0;
252 unsigned char upperlastr
=0;
253 unsigned char upperlastg
=0;
254 unsigned char upperlastb
=0;
257 for(x
=0;x
<width
;x
++) {
267 for(x
=0;x
<width
;x
++) {
269 dest
[1] = src
[0]+lastr
;
270 dest
[2] = src
[1]+lastg
;
271 dest
[3] = src
[2]+lastb
;
280 for(x
=0;x
<width
;x
++) {
282 dest
[1] = src
[0]+old
[1];
283 dest
[2] = src
[1]+old
[2];
284 dest
[3] = src
[2]+old
[3];
291 for(x
=0;x
<width
;x
++) {
293 dest
[1] = src
[0]+(old
[1]+lastr
)/2;
294 dest
[2] = src
[1]+(old
[2]+lastg
)/2;
295 dest
[3] = src
[2]+(old
[3]+lastb
)/2;
305 for(x
=0;x
<width
;x
++) {
307 dest
[1] = src
[0]+PaethPredictor(lastr
,old
[1],upperlastr
);
308 dest
[2] = src
[1]+PaethPredictor(lastg
,old
[2],upperlastg
);
309 dest
[3] = src
[2]+PaethPredictor(lastb
,old
[3],upperlastb
);
323 void applyfilter4(int mode
, U8
*src
, U8
*old
, U8
*dest
, int width
)
326 unsigned char lastr
=0;
327 unsigned char lastg
=0;
328 unsigned char lastb
=0;
329 unsigned char lasta
=0; //TODO: 255?
330 unsigned char upperlastr
=0;
331 unsigned char upperlastg
=0;
332 unsigned char upperlastb
=0;
333 unsigned char upperlasta
=0; //TODO: 255?
336 for(x
=0;x
<width
;x
++) {
346 for(x
=0;x
<width
;x
++) {
347 dest
[0] = src
[3]+lasta
;
348 dest
[1] = src
[0]+lastr
;
349 dest
[2] = src
[1]+lastg
;
350 dest
[3] = src
[2]+lastb
;
360 for(x
=0;x
<width
;x
++) {
361 dest
[0] = src
[3]+old
[0];
362 dest
[1] = src
[0]+old
[1];
363 dest
[2] = src
[1]+old
[2];
364 dest
[3] = src
[2]+old
[3];
371 for(x
=0;x
<width
;x
++) {
372 dest
[0] = src
[3]+(old
[0]+lasta
)/2;
373 dest
[1] = src
[0]+(old
[1]+lastr
)/2;
374 dest
[2] = src
[1]+(old
[2]+lastg
)/2;
375 dest
[3] = src
[2]+(old
[3]+lastb
)/2;
386 for(x
=0;x
<width
;x
++) {
387 dest
[0] = src
[3]+PaethPredictor(lasta
,old
[0],upperlasta
);
388 dest
[1] = src
[0]+PaethPredictor(lastr
,old
[1],upperlastr
);
389 dest
[2] = src
[1]+PaethPredictor(lastg
,old
[2],upperlastg
);
390 dest
[3] = src
[2]+PaethPredictor(lastb
,old
[3],upperlastb
);
407 void applyfilter1(int mode
, U8
*src
, U8
*old
, U8
*dest
, int width
)
410 unsigned char last
=0;
411 unsigned char upperlast
=0;
414 for(x
=0;x
<width
;x
++) {
421 for(x
=0;x
<width
;x
++) {
429 for(x
=0;x
<width
;x
++) {
437 for(x
=0;x
<width
;x
++) {
438 *dest
= *src
+(*old
+last
)/2;
446 for(x
=0;x
<width
;x
++) {
447 *dest
= *src
+PaethPredictor(last
,*old
,upperlast
);
458 TAG
*MovieAddFrame(SWF
* swf
, TAG
* t
, char *sname
, int id
)
465 unsigned width
=0, height
=0;
470 msg("<warning> No jpeg support compiled in");
476 png_load(sname
, &width
, &height
, (unsigned char**)&data
);
479 if(swf_ImageHasAlpha(data
, width
, height
)) {
480 t
= swf_InsertTag(t
, ST_DEFINEBITSJPEG3
);
482 swf_SetJPEGBits3(t
, width
,height
,data
,global
.mkjpeg
);
484 t
= swf_InsertTag(t
, ST_DEFINEBITSJPEG2
);
486 swf_SetJPEGBits2(t
, width
,height
,data
,global
.mkjpeg
);
491 png_load(sname
, &width
, &height
, (unsigned char**)&data
);
494 t
= swf_InsertTag(t
, ST_DEFINEBITSLOSSLESS
);
496 swf_SetLosslessImage(t
, data
,width
,height
);
499 t
= swf_InsertTag(t
, ST_DEFINESHAPE3
);
502 swf_GetMatrix(NULL
, &m
);
503 m
.sx
= (int)(20 * 0x10000);
504 m
.sy
= (int)(20 * 0x10000);
507 fs
= swf_ShapeAddBitmapFillStyle(s
, &m
, id
, 1);
509 swf_SetU16(t
, id
+ 1); // id
513 r
.ymax
= height
* 20;
516 swf_SetShapeHeader(t
, s
);
518 swf_ShapeSetAll(t
, s
, 0, 0, 0, fs
, 0);
519 swf_ShapeSetLine(t
, s
, r
.xmax
, 0);
520 swf_ShapeSetLine(t
, s
, 0, r
.ymax
);
521 swf_ShapeSetLine(t
, s
, -r
.xmax
, 0);
522 swf_ShapeSetLine(t
, s
, 0, -r
.ymax
);
526 t
= swf_InsertTag(t
, ST_REMOVEOBJECT2
);
527 swf_SetU16(t
, 50); // depth
529 t
= swf_InsertTag(t
, ST_PLACEOBJECT2
);
531 swf_GetMatrix(NULL
, &m
);
532 m
.sx
= (int)(0x10000 * global
.scale
);
533 m
.sy
= (int)(0x10000 * global
.scale
);
539 m
.tx
= (swf
->movieSize
.xmax
- (int) (width
* global
.scale
* 20)) / 2;
540 m
.ty
= (swf
->movieSize
.ymax
- (int) (height
* global
.scale
* 20)) / 2;
542 swf_ObjectPlace(t
, id
+ 1, 50, &m
, NULL
, NULL
);
544 t
= swf_InsertTag(t
, ST_SHOWFRAME
);
550 int CheckInputFile(char *fname
, char **realname
)
553 char *s
= malloc(strlen(fname
) + 5);
554 struct png_header head
;
561 // Check whether file exists (with typical extensions)
563 if ((fi
= fopen(s
, "rb")) == NULL
) {
564 sprintf(s
, "%s.png", fname
);
565 if ((fi
= fopen(s
, "rb")) == NULL
) {
566 sprintf(s
, "%s.PNG", fname
);
567 if ((fi
= fopen(s
, "rb")) == NULL
) {
568 sprintf(s
, "%s.Png", fname
);
569 if ((fi
= fopen(s
, "rb")) == NULL
) {
570 fprintf(stderr
, "Couldn't open %s!\n", fname
);
577 if(!png_read_header(fi
, &head
)) {
578 fprintf(stderr
, "%s is not a PNG file!\n", fname
);
582 if (global
.max_image_width
< head
.width
)
583 global
.max_image_width
= head
.width
;
584 if (global
.max_image_height
< head
.height
)
585 global
.max_image_height
= head
.height
;
592 int args_callback_option(char *arg
, char *val
)
601 global
.framerate
= atof(val
);
602 /* removed framerate>0 restriction in order to make
603 Flash Communication Server compatible SWFs */
604 if ((global
.framerate
< 0) ||(global
.framerate
>= 256.0)) {
607 "Error: You must specify a valid framerate between 1/256 and 255.\n");
615 global
.outfile
= val
;
620 global
.scale
= atof(val
)/100;
631 global
.mkjpeg
= atoi(val
);
636 global
.version
= atoi(val
);
658 global
.force_width
= atoi(val
);
664 global
.force_height
= atoi(val
);
669 printf("png2swf - part of %s %s\n", PACKAGE
, VERSION
);
673 char*s
= strdup(val
);
674 char*x1
= strtok(s
, ":");
675 char*y1
= strtok(0, ":");
676 char*x2
= strtok(0, ":");
677 char*y2
= strtok(0, ":");
678 if(!(x1
&& y1
&& x2
&& y2
)) {
679 fprintf(stderr
, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
694 char*s
= strdup(val
);
695 char*c
= strchr(s
, ':');
697 fprintf(stderr
, "-m option requires two arguments, <x>:<y>\n");
717 fprintf(stderr
, "Unknown option: -%s\n", arg
);
724 static struct options_t options
[] = {
729 {"T", "flashversion"},
740 int args_callback_longoption(char *name
, char *val
)
742 return args_long2shortoption(options
, name
, val
);
745 int args_callback_command(char *arg
, char *next
) // actually used as filename
748 if (CheckInputFile(arg
, &s
) < 0) {
750 fprintf(stderr
, "Error opening input file: %s\n", arg
);
753 image
[global
.nfiles
].filename
= s
;
755 if (global
.nfiles
>= MAX_INPUT_FILES
) {
757 fprintf(stderr
, "Error: Too many input files.\n");
764 void args_callback_usage(char *name
)
767 printf("Usage: %s [-X width] [-Y height] [-o file.swf] [-r rate] file1.png [file2.png...]\n", name
);
769 printf("-r , --rate <framerate> Set movie framerate (frames per second)\n");
770 printf("-o , --output <filename> Set name for SWF output file.\n");
771 printf("-j , --jpeg <quality> Generate a lossy jpeg bitmap inside the SWF, with a given quality (1-100)\n");
772 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
773 printf("-T , --flashversion Set the flash version to generate\n");
774 printf("-X , --pixel <width> Force movie width to <width> (default: autodetect)\n");
775 printf("-Y , --pixel <height> Force movie height to <height> (default: autodetect)\n");
776 printf("-v , --verbose <level> Set verbose level (0=quiet, 1=default, 2=debug)\n");
777 printf("-q , --quiet Omit normal log messages, only log errors\n");
778 printf("-C , --cgi For use as CGI- prepend http header, write to stdout\n");
779 printf("-V , --version Print version information and exit\n");
780 printf("-s , --scale <percent> Scale image to <percent>%% size.\n");
784 int main(int argc
, char **argv
)
789 memset(&global
, 0x00, sizeof(global
));
791 global
.framerate
= 1.0;
796 processargs(argc
, argv
);
798 if(global
.nfiles
<=0) {
799 fprintf(stderr
, "No png files found in arguments\n");
804 fprintf(stderr
, "Processing %i file(s)...\n", global
.nfiles
);
806 t
= MovieStart(&swf
, global
.framerate
,
807 global
.force_width
? global
.force_width
: (int)(global
.max_image_width
*global
.scale
),
808 global
.force_height
? global
.force_height
: (int)(global
.max_image_height
*global
.scale
));
812 for (i
= 0; i
< global
.nfiles
; i
++) {
814 fprintf(stderr
, "[%03i] %s\n", i
,
816 t
= MovieAddFrame(&swf
, t
, image
[i
].filename
, (i
* 2) + 1);
817 free(image
[i
].filename
);
821 MovieFinish(&swf
, t
, global
.outfile
);