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];
30 char * outputname = "output.swf";
32 int args_callback_option(char*name,char*val) {
38 else if(!strcmp(name,"l"))
43 else if (!strcmp(name, "o"))
48 else if (!strcmp(name, "v"))
53 else if (!strcmp(name, "a"))
58 else if (!strcmp(name, "x"))
60 config.movex = atoi(val);
63 else if (!strcmp(name, "y"))
65 config.movey = atoi(val);
68 else if (!strcmp(name, "d"))
73 else if (!strcmp(name, "r"))
75 config.framerate = atoi(val)*256/100;
78 else if (!strcmp(name, "X"))
80 config.sizex = atoi(val)*20;
84 else if (!strcmp(name, "Y"))
86 config.sizey = atoi(val)*20;
90 else if (!strcmp(name, "s"))
92 config.scalex = config.scaley = atoi(val)/100.0;
95 else if (!strcmp(name, "t"))
98 fprintf(stderr, "error with arguments. Try --help.\n");
102 master_filename = "__none__";
105 else if (!strcmp(name, "V"))
107 printf("swfcombine - part of %s %s\n", PACKAGE, VERSION);
112 fprintf(stderr, "Unknown option: -%s\n", name);
117 struct options_t options[] =
134 int args_callback_longoption(char*name,char*val) {
135 return args_long2shortoption(options, name, val);
138 int args_callback_command(char*name, char*val) {
139 char*myname = strdup(name);
141 filename = strchr(myname, '=');
146 // argument has no explicit name field. guess one from the file name
147 char*path = strrchr(myname, '/');
148 char*ext = strrchr(myname, '.');
149 if(!path) path = myname;
156 if(!master_filename) {
158 master_filename = filename;
159 master_name = myname;
161 logf("<verbose> slave entity %s (named \"%s\")\n", filename, myname);
163 slave_filename[numslaves] = filename;
164 slave_name[numslaves] = myname;
165 slave_movex[numslaves] = config.movex;
166 slave_movey[numslaves] = config.movey;
167 slave_scalex[numslaves] = config.scalex;
168 slave_scaley[numslaves] = config.scaley;
169 config.movex = config.movey = 0;
170 config.scalex = config.scaley = 1.0;
176 void args_callback_usage(char*name)
178 printf("Usage: %s [-l][-t] [-o outputfile] [[name=]masterfile] [-x xpos] [-y ypos] [-s scale] [name1=]slavefile1 .. [-x xpos] [-y ypos] [-s scale] [nameN=]slavefileN\n", name);
180 printf("-o outputfile (output) explicitly specify output file. (otherwise, output.swf will be used)\n");
181 printf("-t (stack) place each slave into a seperate frame (no master movie)\n");
182 printf("-l (overlay) Don't remove any master objects, only overlay new objects\n");
183 printf("-c (clip) Clip the slave objects by the corresponding master objects\n");
184 printf("-v (verbose) Use more than one -v for greater effect \n");
185 printf("-d (dummy) Don't require slave objects \n");
186 printf("-x xpos (move x) Adjust position of slave by xpos twips (1/20 pixel)\n");
187 printf("-y ypos (move y) Adjust position of slave by ypos twips (1/20 pixel)\n");
188 printf("-s scale (scale) Adjust size of slave by scale%\n");
189 printf("-r framerate (rate) Set movie framerate (100 frames/sec)\n");
190 printf("-X width (width) Force movie width to scale (default: use master width) (not with -t)\n");
191 printf("-Y height (height) Force movie height to scale (default: use master height) (not with -t)\n");
194 /* read a whole file in memory */
195 char* fi_slurp(FILE*fi, unsigned int * setlength)
198 long long int length; //;)
199 long long int pos = 0;
200 fseek(fi,0,SEEK_END);
202 fseek(fi,0,SEEK_SET);
205 mem = malloc(length);
210 pos += fread(&mem[pos], 1, 65536, fi);
217 void fi_dump(FILE*fi, void*_mem, int length)
219 char*mem = (char*)_mem;
224 if (size > (length - pos))
225 size = (length - pos);
226 pos += fwrite(&mem[pos], 1, size, fi);
230 void makestackmaster(u8**masterdata, int*masterlength)
232 u8 head[] = {'F','W','S'};
240 /* scan all slaves for bounding box */
241 for(t=0;t<numslaves;t++)
243 FILE*fi=fopen(slave_filename[t],"rb");
246 struct flash_header head;
247 strlength += strlen(slave_name[t]) + 9;
249 logf("<fatal> Couldn't open %s.", slave_filename[t]);
252 ret = fread(data,1,256,fi);
254 logf("<fatal> File %s is to small (%d bytes)", slave_filename[t], ret);
258 head = swf_read_header();
259 logf("<verbose> File %s has bounding box %d:%d:%d:%d\n",
261 head.boundingBox.x1, head.boundingBox.y1,
262 head.boundingBox.x2, head.boundingBox.y2);
263 if(head.version > fileversion)
264 fileversion = head.version;
266 box = head.boundingBox;
268 if(head.boundingBox.x1 < box.x1)
269 box.x1 = head.boundingBox.x1;
270 if(head.boundingBox.y1 < box.y1)
271 box.y1 = head.boundingBox.y1;
272 if(head.boundingBox.x2 > box.x2)
273 box.x2 = head.boundingBox.x2;
274 if(head.boundingBox.y2 > box.y2)
275 box.y2 = head.boundingBox.y2;
277 logf("<verbose> New master bounding box is %d:%d:%d:%d\n",
283 /* we don't have a master, so we create one ourselves. */
284 /* (please notice the philosophical content) */
285 *masterlength = (numslaves + 1) * 32 + strlength;
286 *masterdata = (u8*)malloc(*masterlength);
288 memcpy(pos, head, sizeof(head));
290 *pos++ = fileversion;
292 *(u32*)pos = 0x12345678; // to be overwritten
294 writeRECT(&pos, &box);
295 *(u16*)pos = 0x2000; // framerate
297 *(u16*)pos = numslaves;
299 for(t=0;t<numslaves;t++)
305 sprintf(buf, "Frame%02d", t);
306 slave_name[t] = strdup(buf);
308 namelen = strlen(slave_name[t]);
310 *(u16*)&pos[0] = (u16)(TAGID_DEFINESPRITE<<6) + 6;
311 *(u16*)&pos[2] = t+1; //ID
312 *(u16*)&pos[4] = 0; // Frames
313 *(u16*)&pos[6] = 0; // TAG1
314 *(u16*)&pos[8] = (u16)(TAGID_PLACEOBJECT2<<6) + 6 + namelen;
315 *(u16*)&pos[10]= 34; //flags: id+name
316 *(u16*)&pos[11]= 1; // depth
317 *(u16*)&pos[13]= t+1; // id
318 sprintf(&pos[15],slave_name[t]);
319 pos += 15 + namelen + 1;
320 *(u16*)&pos[0]= (u16)(TAGID_SHOWFRAME<<6) + 0;
324 *(u16*)&pos[0]= (u16)(TAGID_REMOVEOBJECT2<<6) + 2;
325 *(u16*)&pos[2]= 1; // depth;
329 *(u16*)pos = TAGID_END<<6 + 0;
330 *masterlength = pos - *masterdata;
331 *fixpos = *masterlength;
334 struct config_t config;
335 int main(int argn, char *argv[])
339 unsigned int masterlength;
341 unsigned int slavelength;
343 unsigned int newlength;
347 config.antistream = 0;
348 config.alloctest = 0;
359 config.framerate = 0;
363 processargs(argn, argv);
364 initLog(0,-1,0,0,-1,config.loglevel);
369 logf("<error> Can't combine -l and -t");
373 logf("<error> Can't combine -c and -t");
376 logf("<verbose> (stacking) %d files found\n", numslaves);
378 makestackmaster(&masterdata,&masterlength);
380 logf("<verbose> Generated %d bytes of master data", masterlength);
383 logf("<verbose> master entity %s (named \"%s\")\n", master_filename, master_name);
384 fi = fopen(master_filename, "rb");
386 fprintf(stderr, "Failed to open %s\n", master_filename);
389 masterdata = fi_slurp(fi, &masterlength);
391 fprintf(stderr, "Failed to read from %s\n", master_filename);
394 logf("<debug> Read %d bytes from masterfile\n", masterlength);
398 for(t=0;t<numslaves;t++)
399 logf("<verbose> slave entity(%d) %s (named \"%s\")\n", t+1, slave_filename[t], slave_name[t]);
405 logf("<error> --dummy (-d) implies there are zero slave objects. You supplied %d.", numslaves);
409 slave_filename[t] = "!!dummy!!";
410 slave_name[t] = "!!dummy!!";
413 if (config.alloctest)
415 int*bitmap = malloc(sizeof(int)*65536);
416 memset(bitmap, -1, 65536*sizeof(int));
417 memset(bitmap, 1, 101*sizeof(int));
418 swf_relocate(masterdata, masterlength, bitmap);
419 newdata = masterdata;
420 newlength = masterlength;
427 logf("<error> You must have at least one slave entity.");
430 for(t = 0; t < numslaves; t++)
432 config.movex = slave_movex[t];
433 config.movey = slave_movey[t];
434 config.scalex = slave_scalex[t];
435 config.scaley = slave_scaley[t];
437 logf("<notice> Combine [%s]%s and [%s]%s", master_name, master_filename,
438 slave_name[t], slave_filename[t]);
441 fi = fopen(slave_filename[t], "rb");
443 fprintf(stderr, "Failed to open %s\n", slave_filename[t]);
446 slavedata = fi_slurp(fi, &slavelength);
448 fprintf(stderr, "Failed to read from %s\n", slave_filename[t]);
451 logf("<debug> Read %d bytes from slavefile\n", slavelength);
456 slavedata = (u8*)malloc(16);
460 slavedata[3] = 4; //version
461 *(u32*)&slavedata[4] = 14; // length
462 slavedata[8] = 0; // boundingbox
463 *(u16*)&slavedata[9] = 0; // rate
464 *(u16*)&slavedata[11] = 0; // count
465 *(u16*)&slavedata[13] = 0; // end tag
469 newdata = combine(masterdata, masterlength, slave_name[t], slavedata, slavelength, &newlength);
471 logf("<fatal> Aborting.");
476 masterdata = newdata;
477 masterlength = newlength;
481 logf("<debug> New File is %d bytes \n", newlength);
482 if(newdata && newlength) {
483 FILE*fi = fopen(outputname, "wb");
484 fi_dump(fi, newdata, newlength);