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)
38 } image[MAX_INPUT_FILES];
40 TAG *MovieStart(SWF * swf, int framerate, int dx, int dy)
45 memset(swf, 0x00, sizeof(SWF));
48 swf->frameRate = (25600 / framerate);
49 swf->movieSize.xmax = dx * 20;
50 swf->movieSize.ymax = dy * 20;
52 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
54 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
60 int MovieFinish(SWF * swf, TAG * t, char *sname)
62 int handle, so = fileno(stdout);
63 t = swf_InsertTag(t, ST_END);
65 if ((!isatty(so)) && (!sname))
70 handle = open(sname, O_RDWR | O_CREAT | O_TRUNC, 0666);
73 (swf_WriteSWF(handle, swf)) if (VERBOSE(1))
74 fprintf(stderr, "Unable to write output file: %s\n", sname);
82 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
85 if(destlen) *destlen=0;
86 if(destdata) *destdata=0;
87 if(!fread(&len, 4, 1, fi))
89 if(!fread(head, 4, 1, fi))
91 len = REVERSESWAP32(len);
92 printf("id: %.4s len: %d\n", head, len);
93 if(destlen) *destlen = len;
96 *destdata = malloc(len);
99 if(!fread(*destdata, len, 1, fi)) {
101 if(destlen) *destlen=0;
104 fseek(fi, 4, SEEK_CUR);
107 fseek(fi, len+4, SEEK_CUR);
112 unsigned int png_get_dword(FILE*fi)
116 return REVERSESWAP32(a);
127 int png_read_header(FILE*fi, struct png_header*header)
131 U8 head[8] = {137,80,78,71,13,10,26,10};
135 if(strncmp(head,head2,4))
138 while(png_read_chunk(&id, &len, &data, fi))
140 printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
141 if(!strncasecmp(id, "IHDR", 4)) {
144 header->width = REVERSESWAP32(*(U32*)&data[0]);
145 header->height = REVERSESWAP32(*(U32*)&data[4]);
146 a = data[8]; // should be 8
147 b = data[9]; // should be 3(indexed) or 2(rgb)
149 c = data[10]; // compression mode (0)
150 f = data[11]; // filter mode (0)
151 i = data[12]; // interlace mode (0)
152 printf("%08xx%08x %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
157 fseek(fi, len, SEEK_CUR);
164 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int scale,
176 unsigned long int imagedatalen;
179 struct png_header header;
185 if ((fi = fopen(sname, "rb")) == NULL) {
187 fprintf(stderr, "Read access failed: %s\n", sname);
191 if(!png_read_header(fi, &header))
194 if(header.mode == 3) bypp = 1;
196 if(header.mode == 2) bypp = 3;
199 imagedatalen = bypp * header.width * header.height + 65536;
200 imagedata = malloc(imagedatalen);
202 fseek(fi,8,SEEK_SET);
205 if(!png_read_chunk(&tagid, &len, &data, fi))
207 if(!strncmp(tagid, "IEND", 4)) {
210 if(!strncmp(tagid, "PLTE", 4)) {
214 printf("%d palette\n", len);
216 if(!strncmp(tagid, "IDAT", 4)) {
217 if(uncompress(imagedata, &imagedatalen, data, len) != Z_OK) {
218 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
221 printf("IDAT %d -> %d\n", len, imagedatalen);
227 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
228 swf_SetU16(t, id); // id
229 if(header.mode == 2) {
230 U8*data2 = malloc(header.width*header.height*4);
234 /* 24->32 bit conversion */
235 for(y=0;y<header.height;y++) {
236 pos++; // filter mode
237 for(x=0;x<header.width;x++) {
238 U8*src = &imagedata[pos];
239 U8*dest = &data2[(y*header.width+x)*4];
247 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
251 RGBA*rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
252 U8*data2 = malloc((header.width+4)*header.height);
255 /* 24->32 bit conversion */
256 for(i=0;i<palettelen;i++) {
257 rgba[i].r = palette[i*3+0];
258 rgba[i].g = palette[i*3+1];
259 rgba[i].b = palette[i*3+2];
262 for(y=0;y<header.height;y++) {
264 for(x=0;x<header.width;x++) {
265 data2[y*header.width+x] =
269 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
274 t = swf_InsertTag(t, ST_DEFINESHAPE);
277 swf_GetMatrix(NULL, &m);
280 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
282 swf_SetU16(t, id + 1); // id
285 r.xmax = header.width * 20;
286 r.ymax = header.height * 20;
289 swf_SetShapeHeader(t, s);
291 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
292 swf_ShapeSetLine(t, s, r.xmax, 0);
293 swf_ShapeSetLine(t, s, 0, r.ymax);
294 swf_ShapeSetLine(t, s, -r.xmax, 0);
295 swf_ShapeSetLine(t, s, 0, -r.ymax);
299 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
300 swf_SetU16(t, 1); // depth
302 t = swf_InsertTag(t, ST_PLACEOBJECT2);
304 swf_GetMatrix(NULL, &m);
305 m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
306 m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
307 swf_ObjectPlace(t, id + 1, 1, &m, NULL, NULL);
309 t = swf_InsertTag(t, ST_SHOWFRAME);
317 int CheckInputFile(char *fname, char **realname)
320 char *s = malloc(strlen(fname) + 5);
321 struct png_header head;
328 // Check whether file exists (with typical extensions)
330 if ((fi = fopen(s, "rb")) == NULL) {
331 sprintf(s, "%s.png", fname);
332 if ((fi = fopen(s, "rb")) == NULL) {
333 sprintf(s, "%s.PNG", fname);
334 if ((fi = fopen(s, "rb")) == NULL) {
335 sprintf(s, "%s.Png", fname);
336 if ((fi = fopen(s, "rb")) == NULL)
337 fprintf(stderr, "Couldn't open %s!\n", fname);
343 if(!png_read_header(fi, &head)) {
344 fprintf(stderr, "%s is not a PNG file!\n", fname);
347 if(head.mode!=2 && head.mode!=3) {
348 fprintf(stderr, "%s has unsupported mode %d\n", head.mode);
352 fprintf(stderr, "%s has unsupported bpp %d\n", head.bpp);
356 if (global.max_image_width < head.width)
357 global.max_image_width = head.width;
358 if (global.max_image_height < head.height)
359 global.max_image_height = head.height;
366 int args_callback_option(char *arg, char *val)
375 global.framerate = atoi(val);
376 if ((global.framerate < 1) ||(global.framerate > 5000)) {
379 "Error: You must specify a valid framerate between 1 and 10000.\n");
387 global.outfile = val;
393 global.verbose = atoi(val);
399 global.force_width = atoi(val);
405 global.force_height = atoi(val);
410 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
420 fprintf(stderr, "Unknown option: -%s\n", arg);
427 struct options_t options[] =
438 int args_callback_longoption(char *name, char *val)
440 return args_long2shortoption(options, name, val);
443 int args_callback_command(char *arg, char *next) // actually used as filename
447 if (CheckInputFile(arg, &s) < 0) {
449 fprintf(stderr, "Error opening input file: %s\n", arg);
452 image[global.nfiles].filename = s;
453 image[global.nfiles].scale = global.prescale;
455 if (global.nfiles >= MAX_INPUT_FILES) {
457 fprintf(stderr, "Error: Too many input files.\n");
464 void args_callback_usage(char *name)
466 printf("Usage: %s [-options [value]] imagefiles[.png] [...]\n", name);
467 printf("-r framerate (rate) Set movie framerate (100/sec)\n");
468 printf("-o outputfile (output) Set name for SWF output file\n");
469 printf("-X pixel (width) Force movie width to scale (default: autodetect)\n");
470 printf("-Y pixel (height) Force movie height to scale (default: autodetect)\n");
471 printf("-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
472 printf("-V (version) Print version information and exit\n");
473 printf("The following options can be set independently for each image: -q -s\n");
477 int main(int argc, char **argv)
482 memset(&global, 0x00, sizeof(global));
484 global.framerate = 100;
488 processargs(argc, argv);
491 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
493 t = MovieStart(&swf, global.framerate,
494 global.force_width ? global.force_width : global.
496 global.force_height ? global.force_height : global.
501 for (i = 0; i < global.nfiles; i++) {
503 fprintf(stderr, "[%03i] %s (1/%i)\n", i,
506 t = MovieAddFrame(&swf, t, image[i].filename,
507 image[i].scale, (i * 2) + 1);
508 free(image[i].filename);
512 MovieFinish(&swf, t, global.outfile);