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, "s"))
70 config.scalex = config.scaley = atoi(val)/100.0;
73 else if (!strcmp(name, "t"))
76 fprintf(stderr, "error with arguments. Try --help.\n");
80 master_filename = "__none__";
83 else if (!strcmp(name, "V"))
85 printf("swfcombine - part of %s %s\n", PACKAGE, VERSION);
90 fprintf(stderr, "Unknown option: -%s\n", name);
95 struct options_t options[] =
108 int args_callback_longoption(char*name,char*val) {
109 return args_long2shortoption(options, name, val);
112 int args_callback_command(char*name, char*val) {
113 char*myname = strdup(name);
115 filename = strchr(myname, '=');
120 // argument has no explicit name field. guess one from the file name
121 char*path = strrchr(myname, '/');
122 char*ext = strrchr(myname, '.');
123 if(!path) path = myname;
130 if(!master_filename) {
132 master_filename = filename;
133 master_name = myname;
135 logf("<verbose> slave entity %s (named \"%s\")\n", filename, myname);
137 slave_filename[numslaves] = filename;
138 slave_name[numslaves] = myname;
139 slave_movex[numslaves] = config.movex;
140 slave_movey[numslaves] = config.movey;
141 slave_scalex[numslaves] = config.scalex;
142 slave_scaley[numslaves] = config.scaley;
143 config.movex = config.movey = 0;
144 config.scalex = config.scaley = 1.0;
150 void args_callback_usage(char*name)
152 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);
154 printf("-o outputfile explicitly specify output file. (otherwise, output.swf will be used)\n");
155 printf("-t (stack) place each slave into a seperate frame (no master movie)\n");
156 printf("-l (overlay) Don't remove any master objects, only overlay new objects\n");
157 printf("-c (clip) Clip the slave objects by the corresponding master objects\n");
158 printf("-v (verbose) Use more than one -v for greater effect \n");
159 printf("-x xpos (move x) Adjust position of slave by xpos twips (1/20 pixel)\n");
160 printf("-y ypos (move y) Adjust position of slave by ypos twips (1/20 pixel)\n");
161 printf("-s scale (scale) Adjust size of slave by scale%\n");
164 /* read a whole file in memory */
165 char* fi_slurp(FILE*fi, unsigned int * setlength)
168 long long int length; //;)
169 long long int pos = 0;
170 fseek(fi,0,SEEK_END);
172 fseek(fi,0,SEEK_SET);
175 mem = malloc(length);
180 pos += fread(&mem[pos], 1, 65536, fi);
187 void fi_dump(FILE*fi, void*_mem, int length)
189 char*mem = (char*)_mem;
194 if (size > (length - pos))
195 size = (length - pos);
196 pos += fwrite(&mem[pos], 1, size, fi);
200 void makestackmaster(u8**masterdata, int*masterlength)
202 u8 head[] = {'F','W','S'};
210 /* scan all slaves for bounding box */
211 for(t=0;t<numslaves;t++)
213 FILE*fi=fopen(slave_filename[t],"rb");
216 struct flash_header head;
217 strlength += strlen(slave_name[t]) + 9;
219 logf("<fatal> Couldn't open %s.", slave_filename[t]);
222 ret = fread(data,1,256,fi);
224 logf("<fatal> File %s is to small (%d bytes)", slave_filename[t], ret);
228 head = swf_read_header();
229 logf("<verbose> File %s has bounding box %d:%d:%d:%d\n",
231 head.boundingBox.x1, head.boundingBox.y1,
232 head.boundingBox.x2, head.boundingBox.y2);
233 if(head.version > fileversion)
234 fileversion = head.version;
236 box = head.boundingBox;
238 if(head.boundingBox.x1 < box.x1)
239 box.x1 = head.boundingBox.x1;
240 if(head.boundingBox.y1 < box.y1)
241 box.y1 = head.boundingBox.y1;
242 if(head.boundingBox.x2 > box.x2)
243 box.x2 = head.boundingBox.x2;
244 if(head.boundingBox.y2 > box.y2)
245 box.y2 = head.boundingBox.y2;
247 logf("<verbose> New master bounding box is %d:%d:%d:%d\n",
253 /* we don't have a master, so we create one ourselves. */
254 /* (please notice the philosophical content) */
255 *masterlength = (numslaves + 1) * 32 + strlength;
256 *masterdata = (u8*)malloc(*masterlength);
258 memcpy(pos, head, sizeof(head));
260 *pos++ = fileversion;
262 *(u32*)pos = 0x12345678; // to be overwritten
264 writeRECT(&pos, &box);
265 *(u16*)pos = 0x2000; // framerate
267 *(u16*)pos = numslaves;
269 for(t=0;t<numslaves;t++)
275 sprintf(buf, "Frame%02d", t);
276 slave_name[t] = strdup(buf);
278 namelen = strlen(slave_name[t]);
280 *(u16*)&pos[0] = (u16)(TAGID_DEFINESPRITE<<6) + 6;
281 *(u16*)&pos[2] = t+1; //ID
282 *(u16*)&pos[4] = 0; // Frames
283 *(u16*)&pos[6] = 0; // TAG1
284 *(u16*)&pos[8] = (u16)(TAGID_PLACEOBJECT2<<6) + 6 + namelen;
285 *(u16*)&pos[10]= 34; //flags: id+name
286 *(u16*)&pos[11]= 1; // depth
287 *(u16*)&pos[13]= t+1; // id
288 sprintf(&pos[15],slave_name[t]);
289 pos += 15 + namelen + 1;
290 *(u16*)&pos[0]= (u16)(TAGID_SHOWFRAME<<6) + 0;
294 *(u16*)&pos[0]= (u16)(TAGID_REMOVEOBJECT2<<6) + 2;
295 *(u16*)&pos[2]= 1; // depth;
299 *(u16*)pos = TAGID_END<<6 + 0;
300 *masterlength = pos - *masterdata;
301 *fixpos = *masterlength;
304 struct config_t config;
305 int main(int argn, char *argv[])
309 unsigned int masterlength;
311 unsigned int slavelength;
313 unsigned int newlength;
317 config.antistream = 0;
318 config.alloctest = 0;
327 processargs(argn, argv);
328 initLog(0,-1,0,0,-1,config.loglevel);
333 logf("<error> Can't combine -l and -t");
337 logf("<error> Can't combine -c and -t");
340 logf("<verbose> (stacking) %d files found\n", numslaves);
342 makestackmaster(&masterdata,&masterlength);
344 logf("<verbose> Generated %d bytes of master data", masterlength);
347 logf("<verbose> master entity %s (named \"%s\")\n", master_filename, master_name);
348 fi = fopen(master_filename, "rb");
350 fprintf(stderr, "Failed to open %s\n", master_filename);
353 masterdata = fi_slurp(fi, &masterlength);
355 fprintf(stderr, "Failed to read from %s\n", master_filename);
358 logf("<debug> Read %d bytes from masterfile\n", masterlength);
362 for(t=0;t<numslaves;t++)
363 logf("<verbose> slave entity(%d) %s (named \"%s\")\n", t+1, slave_filename[t], slave_name[t]);
365 if (config.alloctest)
367 int*bitmap = malloc(sizeof(int)*65536);
368 memset(bitmap, -1, 65536*sizeof(int));
369 memset(bitmap, 1, 101*sizeof(int));
370 swf_relocate(masterdata, masterlength, bitmap);
371 newdata = masterdata;
372 newlength = masterlength;
379 logf("<error> You must have at least one slave entity.");
382 for(t = 0; t < numslaves; t++)
384 config.movex = slave_movex[t];
385 config.movey = slave_movey[t];
386 config.scalex = slave_scalex[t];
387 config.scaley = slave_scaley[t];
389 logf("<notice> Combine [%s]%s and [%s]%s", master_name, master_filename,
390 slave_name[t], slave_filename[t]);
391 fi = fopen(slave_filename[t], "rb");
393 fprintf(stderr, "Failed to open %s\n", slave_filename[t]);
396 slavedata = fi_slurp(fi, &slavelength);
398 fprintf(stderr, "Failed to read from %s\n", slave_filename[t]);
401 logf("<debug> Read %d bytes from slavefile\n", slavelength);
404 newdata = combine(masterdata, masterlength, slave_name[t], slavedata, slavelength, &newlength);
406 logf("<fatal> Aborting.");
411 masterdata = newdata;
412 masterlength = newlength;
416 logf("<debug> New File is %d bytes \n", newlength);
417 if(newdata && newlength) {
418 FILE*fi = fopen(outputname, "wb");
419 fi_dump(fi, newdata, newlength);