2 main routine for swfcombine(1), which is a tool for merging .swf-files.
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This file is distributed under the GPL, see file COPYING for details */
13 #include "../lib/args.h"
18 #include "../config.h"
20 char * master_filename = 0;
21 char * master_name = 0;
22 char * slave_filename[128];
23 char * slave_name[128];
26 float slave_scalex[128];
27 float slave_scaley[128];
28 char slave_isframe[128];
31 char * outputname = "output.swf";
33 int args_callback_option(char*name,char*val) {
39 else if(!strcmp(name,"l"))
44 else if (!strcmp(name, "o"))
49 else if (!strcmp(name, "v"))
54 else if (!strcmp(name, "a"))
59 else if (!strcmp(name, "A"))
64 else if (!strcmp(name, "x"))
66 config.movex = atoi(val);
69 else if (!strcmp(name, "y"))
71 config.movey = atoi(val);
74 else if (!strcmp(name, "m"))
79 else if (!strcmp(name, "f"))
84 else if (!strcmp(name, "d"))
89 else if (!strcmp(name, "r"))
91 config.framerate = atoi(val)*256/100;
94 else if (!strcmp(name, "X"))
96 config.sizex = atoi(val)*20;
100 else if (!strcmp(name, "Y"))
102 config.sizey = atoi(val)*20;
106 else if (!strcmp(name, "s"))
108 config.scalex = config.scaley = atoi(val)/100.0;
111 else if (!strcmp(name, "t") || !strcmp(name, "T"))
113 if(master_filename) {
114 fprintf(stderr, "error with arguments. Try --help.\n");
118 if(!strcmp(name,"T"))
120 master_filename = "__none__";
123 else if (!strcmp(name, "V"))
125 printf("swfcombine - part of %s %s\n", PACKAGE, VERSION);
130 fprintf(stderr, "Unknown option: -%s\n", name);
135 struct options_t options[] =
156 int args_callback_longoption(char*name,char*val) {
157 return args_long2shortoption(options, name, val);
160 int args_callback_command(char*name, char*val) {
161 char*myname = strdup(name);
163 filename = strchr(myname, '=');
168 // argument has no explicit name field. guess one from the file name
169 char*path = strrchr(myname, '/');
170 char*ext = strrchr(myname, '.');
171 if(!path) path = myname;
178 if(!master_filename) {
179 master_filename = filename;
180 master_name = myname;
182 logf("<verbose> slave entity %s (named \"%s\")\n", filename, myname);
184 slave_filename[numslaves] = filename;
185 slave_name[numslaves] = myname;
186 slave_movex[numslaves] = config.movex;
187 slave_movey[numslaves] = config.movey;
188 slave_scalex[numslaves] = config.scalex;
189 slave_scaley[numslaves] = config.scaley;
190 slave_isframe[numslaves] = config.isframe;
192 config.movex = config.movey = 0;
193 config.scalex = config.scaley = 1.0;
199 void args_callback_usage(char*name)
201 printf("Usage: %s [-rXYomlcv] [-f] masterfile] [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
202 printf("OR: %s [-rXYomv] --stack[1] [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
203 printf("OR: %s [-rXYov] --cat [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
204 printf("OR: %s [-rXYomlcv] --dummy [-xys] [file]\n", name);
206 printf("-o outputfile --output explicitly specify output file. (otherwise, output.swf will be used\n");
207 printf("-t --stack place each slave in a seperate frame (no master movie\n");
208 printf("-T --stack1 place each slave in the first frame (no master movie\n");
209 printf("-m --merge Don't store the slaves in Sprites/MovieClips\n");
210 printf("-a --cat concatenate all slave files (no master movie\n");
211 printf("-l --overlay Don't remove any master objects, only overlay new objects\n");
212 printf("-c --clip Clip the slave objects by the corresponding master objects\n");
213 printf("-v --verbose Use more than one -v for greater effect \n");
214 printf("-d --dummy Don't require slave objects \n");
215 printf("-f --frame The following identifier is a frame or framelabel, not an id or objectname\n");
216 printf("-x xpos --movex x Adjust position of slave by xpos twips (1/20 pixel\n");
217 printf("-y ypos --movey y Adjust position of slave by ypos twips (1/20 pixel\n");
218 printf("-s scale --scale Adjust size of slave by scale%\n");
219 printf("-r framerate --rate Set movie framerate (100 frames/sec\n");
220 printf("-X width --width Force movie width to scale (default: use master width (not with -t\n");
221 printf("-Y height --height Force movie height to scale (default: use master height (not with -t\n");
224 /* read a whole file in memory */
225 char* fi_slurp(FILE*fi, unsigned int * setlength)
228 long long int length; //;)
229 long long int pos = 0;
230 fseek(fi,0,SEEK_END);
232 fseek(fi,0,SEEK_SET);
235 mem = malloc(length);
240 pos += fread(&mem[pos], 1, 65536, fi);
247 void fi_dump(FILE*fi, void*_mem, int length)
249 char*mem = (char*)_mem;
254 if (size > (length - pos))
255 size = (length - pos);
256 pos += fwrite(&mem[pos], 1, size, fi);
260 void makestackmaster(u8**masterdata, int*masterlength)
262 u8 head[] = {'F','W','S'};
270 /* scan all slaves for bounding box */
271 for(t=0;t<numslaves;t++)
273 FILE*fi=fopen(slave_filename[t],"rb");
276 struct flash_header head;
277 strlength += strlen(slave_name[t]) + 9;
279 logf("<fatal> Couldn't open %s.", slave_filename[t]);
282 ret = fread(data,1,256,fi);
284 logf("<fatal> File %s is to small (%d bytes)", slave_filename[t], ret);
288 head = swf_read_header();
289 logf("<verbose> File %s has bounding box %d:%d:%d:%d\n",
291 head.boundingBox.x1, head.boundingBox.y1,
292 head.boundingBox.x2, head.boundingBox.y2);
293 if(head.version > fileversion)
294 fileversion = head.version;
296 box = head.boundingBox;
298 if(head.boundingBox.x1 < box.x1)
299 box.x1 = head.boundingBox.x1;
300 if(head.boundingBox.y1 < box.y1)
301 box.y1 = head.boundingBox.y1;
302 if(head.boundingBox.x2 > box.x2)
303 box.x2 = head.boundingBox.x2;
304 if(head.boundingBox.y2 > box.y2)
305 box.y2 = head.boundingBox.y2;
307 logf("<verbose> New master bounding box is %d:%d:%d:%d\n",
313 /* we don't have a master, so we create one ourselves. */
314 /* (please notice the philosophical content) */
315 *masterlength = (numslaves + 1) * 32 + strlength;
316 *masterdata = (u8*)malloc(*masterlength);
318 memcpy(pos, head, sizeof(head));
320 *pos++ = fileversion;
322 *(u32*)pos = 0x12345678; // to be overwritten
324 writeRECT(&pos, &box);
325 *(u16*)pos = 0x2000; // framerate
327 *(u16*)pos = numslaves;
329 for(t=0;t<numslaves;t++)
335 sprintf(buf, "Frame%02d", t);
336 slave_name[t] = strdup(buf);
338 namelen = strlen(slave_name[t]);
340 *(u16*)&pos[0] = (u16)(TAGID_DEFINESPRITE<<6) + 6;
341 *(u16*)&pos[2] = t+1; //ID
342 *(u16*)&pos[4] = 0; // Frames
343 *(u16*)&pos[6] = 0; // TAG1
344 *(u16*)&pos[8] = (u16)(TAGID_PLACEOBJECT2<<6) + 6 + namelen;
345 *(u16*)&pos[10]= 34; //flags: id+name
346 *(u16*)&pos[11]= 1+t; // depth
347 *(u16*)&pos[13]= t+1; // id
348 sprintf(&pos[15],slave_name[t]);
349 pos += 15 + namelen + 1;
350 if(!config.stack1 || t == numslaves-1) {
351 *(u16*)&pos[0]= (u16)(TAGID_SHOWFRAME<<6) + 0;
357 *(u16*)&pos[0]= (u16)(TAGID_REMOVEOBJECT2<<6) + 2;
358 *(u16*)&pos[2]= 1+t; // depth;
362 *(u16*)pos = TAGID_END<<6 + 0;
363 *masterlength = pos - *masterdata;
364 *fixpos = *masterlength;
367 struct config_t config;
368 int main(int argn, char *argv[])
372 unsigned int masterlength;
374 unsigned int slavelength;
376 unsigned int newlength;
380 config.antistream = 0;
381 config.alloctest = 0;
394 config.framerate = 0;
399 processargs(argn, argv);
400 initLog(0,-1,0,0,-1,config.loglevel);
402 if(config.merge && config.cat) {
403 logf("<error> Can't combine --cat and --merge");
410 logf("<error> Can't combine -l and -t");
414 logf("<error> Can't combine -c and -t");
417 logf("<verbose> (stacking) %d files found\n", numslaves);
419 makestackmaster(&masterdata,&masterlength);
421 logf("<verbose> Generated %d bytes of master data", masterlength);
424 logf("<verbose> master entity %s (named \"%s\")\n", master_filename, master_name);
425 fi = fopen(master_filename, "rb");
427 fprintf(stderr, "Failed to open %s\n", master_filename);
430 masterdata = fi_slurp(fi, &masterlength);
432 fprintf(stderr, "Failed to read from %s\n", master_filename);
435 logf("<debug> Read %d bytes from masterfile\n", masterlength);
439 for(t=0;t<numslaves;t++) {
440 logf("<verbose> slave entity(%d) %s (%s \"%s\")\n", t+1, slave_filename[t],
441 slave_isframe[t]?"frame":"object", slave_name[t]);
448 logf("<error> --dummy (-d) implies there are zero slave objects. You supplied %d.", numslaves);
452 slave_filename[0] = "!!dummy!!";
453 slave_name[0] = "!!dummy!!";
454 slave_isframe[0] = 0;
457 if (config.alloctest)
459 int*bitmap = malloc(sizeof(int)*65536);
460 memset(bitmap, -1, 65536*sizeof(int));
461 memset(bitmap, 1, 101*sizeof(int));
462 swf_relocate(masterdata, masterlength, bitmap);
463 newdata = masterdata;
464 newlength = masterlength;
471 logf("<error> You must have at least one slave entity.");
474 for(t = 0; t < numslaves; t++)
476 config.movex = slave_movex[t];
477 config.movey = slave_movey[t];
478 config.scalex = slave_scalex[t];
479 config.scaley = slave_scaley[t];
480 config.isframe = slave_isframe[t];
482 logf("<notice> Combine [%s]%s and [%s]%s", master_name, master_filename,
483 slave_name[t], slave_filename[t]);
486 fi = fopen(slave_filename[t], "rb");
488 fprintf(stderr, "Failed to open %s\n", slave_filename[t]);
491 slavedata = fi_slurp(fi, &slavelength);
493 fprintf(stderr, "Failed to read from %s\n", slave_filename[t]);
496 logf("<debug> Read %d bytes from slavefile\n", slavelength);
501 slavedata = (u8*)malloc(16);
505 slavedata[3] = 4; //version
506 *(u32*)&slavedata[4] = 14; // length
507 slavedata[8] = 0; // boundingbox
508 *(u16*)&slavedata[9] = 0; // rate
509 *(u16*)&slavedata[11] = 0; // count
510 *(u16*)&slavedata[13] = 0; // end tag
514 newdata = combine(masterdata, masterlength, slave_name[t], slavedata, slavelength, &newlength);
516 logf("<fatal> Aborting.");
521 masterdata = newdata;
522 masterlength = newlength;
526 logf("<debug> New File is %d bytes \n", newlength);
527 if(newdata && newlength) {
528 FILE*fi = fopen(outputname, "wb");
529 fi_dump(fi, newdata, newlength);