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)
36 } image[MAX_INPUT_FILES];
38 TAG *MovieStart(SWF * swf, int framerate, int dx, int dy)
43 memset(swf, 0x00, sizeof(SWF));
46 swf->frameRate = (25600 / framerate);
47 swf->movieSize.xmax = dx * 20;
48 swf->movieSize.ymax = dy * 20;
50 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
52 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
58 int MovieFinish(SWF * swf, TAG * t, char *sname)
60 int handle, so = fileno(stdout);
61 t = swf_InsertTag(t, ST_END);
63 if ((!isatty(so)) && (!sname))
68 handle = open(sname, O_RDWR | O_CREAT | O_TRUNC, 0666);
71 (swf_WriteSWF(handle, swf)) if (VERBOSE(1))
72 fprintf(stderr, "Unable to write output file: %s\n", sname);
80 int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
83 if(destlen) *destlen=0;
84 if(destdata) *destdata=0;
85 if(!fread(&len, 4, 1, fi))
87 if(!fread(head, 4, 1, fi))
89 len = REVERSESWAP32(len);
90 printf("id: %.4s len: %d\n", head, len);
91 if(destlen) *destlen = len;
94 *destdata = malloc(len);
97 if(!fread(*destdata, len, 1, fi)) {
99 if(destlen) *destlen=0;
102 fseek(fi, 4, SEEK_CUR);
105 fseek(fi, len+4, SEEK_CUR);
110 unsigned int png_get_dword(FILE*fi)
114 return REVERSESWAP32(a);
125 int png_read_header(FILE*fi, struct png_header*header)
129 U8 head[8] = {137,80,78,71,13,10,26,10};
133 if(strncmp(head,head2,4))
136 while(png_read_chunk(&id, &len, &data, fi))
138 printf("%c%c%c%c %d\n", id[0],id[1],id[2],id[3],len);
139 if(!strncasecmp(id, "IHDR", 4)) {
142 header->width = REVERSESWAP32(*(U32*)&data[0]);
143 header->height = REVERSESWAP32(*(U32*)&data[4]);
144 a = data[8]; // should be 8
145 b = data[9]; // should be 3(indexed) or 2(rgb)
147 c = data[10]; // compression mode (0)
148 f = data[11]; // filter mode (0)
149 i = data[12]; // interlace mode (0)
150 printf("%08xx%08x %d %d %d %d %d\n",header->width, header->height, a,b,c,f,i);
155 fseek(fi, len, SEEK_CUR);
162 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int id)
173 unsigned long int imagedatalen;
176 struct png_header header;
182 if ((fi = fopen(sname, "rb")) == NULL) {
184 fprintf(stderr, "Read access failed: %s\n", sname);
188 if(!png_read_header(fi, &header))
191 if(header.mode == 3) bypp = 1;
193 if(header.mode == 2) bypp = 3;
196 imagedatalen = bypp * header.width * header.height + 65536;
197 imagedata = malloc(imagedatalen);
199 fseek(fi,8,SEEK_SET);
202 if(!png_read_chunk(&tagid, &len, &data, fi))
204 if(!strncmp(tagid, "IEND", 4)) {
207 if(!strncmp(tagid, "PLTE", 4)) {
211 printf("%d palette\n", len);
213 if(!strncmp(tagid, "IDAT", 4)) {
214 if(uncompress(imagedata, &imagedatalen, data, len) != Z_OK) {
215 fprintf(stderr, "Couldn't uncompress %s!\n", sname);
218 printf("IDAT %d -> %d\n", len, imagedatalen);
224 t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);
225 swf_SetU16(t, id); // id
226 if(header.mode == 2) {
227 U8*data2 = malloc(header.width*header.height*4);
231 /* 24->32 bit conversion */
232 for(y=0;y<header.height;y++) {
233 pos++; // filter mode
234 for(x=0;x<header.width;x++) {
235 U8*src = &imagedata[pos];
236 U8*dest = &data2[(y*header.width+x)*4];
244 swf_SetLosslessBits(t, header.width, header.height, data2, BMF_32BIT);
248 RGBA*rgba = (RGBA*)malloc(palettelen*sizeof(RGBA));
249 U8*data2 = malloc((header.width+4)*header.height);
252 /* 24->32 bit conversion */
253 for(i=0;i<palettelen;i++) {
254 rgba[i].r = palette[i*3+0];
255 rgba[i].g = palette[i*3+1];
256 rgba[i].b = palette[i*3+2];
259 for(y=0;y<header.height;y++) {
261 for(x=0;x<header.width;x++) {
262 data2[y*header.width+x] =
266 swf_SetLosslessBitsIndexed(t, header.width, header.height, data2, rgba, palettelen);
271 t = swf_InsertTag(t, ST_DEFINESHAPE);
274 swf_GetMatrix(NULL, &m);
277 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
279 swf_SetU16(t, id + 1); // id
282 r.xmax = header.width * 20;
283 r.ymax = header.height * 20;
286 swf_SetShapeHeader(t, s);
288 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
289 swf_ShapeSetLine(t, s, r.xmax, 0);
290 swf_ShapeSetLine(t, s, 0, r.ymax);
291 swf_ShapeSetLine(t, s, -r.xmax, 0);
292 swf_ShapeSetLine(t, s, 0, -r.ymax);
296 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
297 swf_SetU16(t, 1); // depth
299 t = swf_InsertTag(t, ST_PLACEOBJECT2);
301 swf_GetMatrix(NULL, &m);
302 m.tx = (swf->movieSize.xmax - (int) header.width * 20) / 2;
303 m.ty = (swf->movieSize.ymax - (int) header.height * 20) / 2;
304 swf_ObjectPlace(t, id + 1, 1, &m, NULL, NULL);
306 t = swf_InsertTag(t, ST_SHOWFRAME);
314 int CheckInputFile(char *fname, char **realname)
317 char *s = malloc(strlen(fname) + 5);
318 struct png_header head;
325 // Check whether file exists (with typical extensions)
327 if ((fi = fopen(s, "rb")) == NULL) {
328 sprintf(s, "%s.png", fname);
329 if ((fi = fopen(s, "rb")) == NULL) {
330 sprintf(s, "%s.PNG", fname);
331 if ((fi = fopen(s, "rb")) == NULL) {
332 sprintf(s, "%s.Png", fname);
333 if ((fi = fopen(s, "rb")) == NULL)
334 fprintf(stderr, "Couldn't open %s!\n", fname);
340 if(!png_read_header(fi, &head)) {
341 fprintf(stderr, "%s is not a PNG file!\n", fname);
344 if(head.mode!=2 && head.mode!=3) {
345 fprintf(stderr, "%s has unsupported mode %d\n", head.mode);
349 fprintf(stderr, "%s has unsupported bpp %d\n", head.bpp);
353 if (global.max_image_width < head.width)
354 global.max_image_width = head.width;
355 if (global.max_image_height < head.height)
356 global.max_image_height = head.height;
363 int args_callback_option(char *arg, char *val)
372 global.framerate = atoi(val);
373 if ((global.framerate < 1) ||(global.framerate > 5000)) {
376 "Error: You must specify a valid framerate between 1 and 10000.\n");
384 global.outfile = val;
390 global.verbose = atoi(val);
396 global.force_width = atoi(val);
402 global.force_height = atoi(val);
407 printf("png2swf - part of %s %s\n", PACKAGE, VERSION);
417 fprintf(stderr, "Unknown option: -%s\n", arg);
424 struct options_t options[] =
434 int args_callback_longoption(char *name, char *val)
436 return args_long2shortoption(options, name, val);
439 int args_callback_command(char *arg, char *next) // actually used as filename
442 if (CheckInputFile(arg, &s) < 0) {
444 fprintf(stderr, "Error opening input file: %s\n", arg);
447 image[global.nfiles].filename = s;
449 if (global.nfiles >= MAX_INPUT_FILES) {
451 fprintf(stderr, "Error: Too many input files.\n");
458 void args_callback_usage(char *name)
460 printf("Usage: %s [-options [value]] imagefiles[.png] [...]\n", name);
461 printf("-r framerate (rate) Set movie framerate (100/sec)\n");
462 printf("-o outputfile (output) Set name for SWF output file\n");
463 printf("-X pixel (width) Force movie width to pixel (default: autodetect)\n");
464 printf("-Y pixel (height) Force movie height to pixel (default: autodetect)\n");
465 printf("-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
466 printf("-V (version) Print version information and exit\n");
467 printf("The following options can be set independently for each image: -q -s\n");
471 int main(int argc, char **argv)
476 memset(&global, 0x00, sizeof(global));
478 global.framerate = 100;
481 processargs(argc, argv);
484 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
486 t = MovieStart(&swf, global.framerate,
487 global.force_width ? global.force_width : global.
489 global.force_height ? global.force_height : global.
494 for (i = 0; i < global.nfiles; i++) {
496 fprintf(stderr, "[%03i] %s\n", i,
498 t = MovieAddFrame(&swf, t, image[i].filename, (i * 2) + 1);
499 free(image[i].filename);
503 MovieFinish(&swf, t, global.outfile);