removed lame.
[swftools.git] / src / swfcombine.c
blob403419df80d542e7546b498aec287f99066849ec
1 /* swfcombine.c
2 main routine for swfcombine(1), a tool for merging .swf-files.
4 Part of the swftools package.
6 Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "../lib/rfxswf.h"
26 #include "../lib/args.h"
27 #include "../lib/log.h"
28 #include "../config.h"
30 struct config_t
32 char overlay;
33 char alloctest;
34 char clip;
35 char stack;
36 char stack1;
37 char antistream;
38 char dummy;
39 char zlib;
40 char cat;
41 char merge;
42 char isframe;
43 int loglevel;
44 int sizex;
45 char hassizex;
46 int sizey;
47 char hassizey;
48 int framerate;
49 int movex;
50 int movey;
51 float scalex;
52 float scaley;
53 int mastermovex;
54 int mastermovey;
55 float masterscalex;
56 float masterscaley;
58 struct config_t config;
60 char * master_filename = 0;
61 char * master_name = 0;
62 char * slave_filename[128];
63 char * slave_name[128];
64 int slave_movex[128];
65 int slave_movey[128];
66 float slave_scalex[128];
67 float slave_scaley[128];
68 char slave_isframe[128];
69 int numslaves = 0;
71 char * outputname = "output.swf";
73 int args_callback_option(char*name,char*val) {
74 if(!strcmp(name,"c"))
76 config.clip = 1;
77 return 0;
79 else if(!strcmp(name,"l"))
81 config.overlay = 1;
82 return 0;
84 else if (!strcmp(name, "o"))
86 outputname = val;
87 return 1;
89 else if (!strcmp(name, "v"))
91 config.loglevel ++;
92 return 0;
94 else if (!strcmp(name, "a"))
96 config.cat = 1;
97 return 0;
99 else if (!strcmp(name, "A"))
101 config.alloctest = 1;
102 return 0;
104 else if (!strcmp(name, "x"))
106 float x = atof(val);
107 config.movex = (int)(x*20+0.5);
108 return 1;
110 else if (!strcmp(name, "y"))
112 float y = atof(val);
113 config.movey = (int)(y*20+0.5);
114 return 1;
116 else if (!strcmp(name, "m"))
118 config.merge = 1;
119 return 0;
121 else if (!strcmp(name, "f"))
123 config.isframe = 1;
124 return 0;
126 else if (!strcmp(name, "d"))
128 config.dummy = 1;
129 return 0;
131 else if (!strcmp(name, "z"))
133 config.zlib = 1;
134 return 0;
136 else if (!strcmp(name, "r"))
139 float rate = atof(val);
140 if ((rate < 1.0/256) ||(rate >= 256.0)) {
141 fprintf(stderr, "Error: You must specify a valid framerate between 1/256 and 255.\n");
142 exit(1);
144 config.framerate = (int)(rate*256);
145 return 1;
147 else if (!strcmp(name, "X"))
149 config.sizex = atoi(val)*20;
150 config.hassizex = 1;
151 return 1;
153 else if (!strcmp(name, "Y"))
155 config.sizey = atoi(val)*20;
156 config.hassizey = 1;
157 return 1;
159 else if (!strcmp(name, "s"))
161 config.scalex = config.scaley = atoi(val)/100.0;
162 return 1;
164 else if (!strcmp(name, "t") || !strcmp(name, "T"))
166 if(master_filename) {
167 fprintf(stderr, "error with arguments. Try --help.\n");
168 exit(1);
170 config.stack = 1;
171 if(!strcmp(name,"T"))
172 config.stack1 = 1;
173 master_filename = "__none__";
174 return 0;
176 else if (!strcmp(name, "V"))
178 printf("swfcombine - part of %s %s\n", PACKAGE, VERSION);
179 exit(0);
181 else
183 fprintf(stderr, "Unknown option: -%s\n", name);
184 exit(1);
188 static struct options_t options[] = {
189 {"o", "output"},
190 {"t", "stack"},
191 {"T", "stack1"},
192 {"m", "merge"},
193 {"a", "cat"},
194 {"l", "overlay"},
195 {"c", "clip"},
196 {"v", "verbose"},
197 {"d", "dummy"},
198 {"f", "frame"},
199 {"x", "movex"},
200 {"y", "movey"},
201 {"s", "scale"},
202 {"r", "rate"},
203 {"X", "width"},
204 {"Y", "height"},
205 {"z", "zlib"},
206 {0,0}
209 int args_callback_longoption(char*name,char*val) {
210 return args_long2shortoption(options, name, val);
213 int args_callback_command(char*name, char*val) {
214 char*myname = strdup(name);
215 char*filename;
216 filename = strchr(myname, '=');
217 if(filename) {
218 *filename = 0;
219 filename++;
220 } else {
221 // argument has no explicit name field. guess one from the file name
222 char*path = strrchr(myname, '/');
223 char*ext = strrchr(myname, '.');
224 if(!path) path = myname;
225 else path ++;
226 if(ext) *ext = 0;
227 myname = path;
228 filename = name;
231 if(!master_filename) {
232 master_filename = filename;
233 master_name = myname;
234 config.mastermovex = config.movex;
235 config.mastermovey = config.movey;
236 config.masterscalex = config.scalex;
237 config.masterscaley = config.scaley;
238 config.movex = config.movey = 0;
239 config.scalex = config.scaley = 1.0;
240 } else {
241 msg("<verbose> slave entity %s (named \"%s\")\n", filename, myname);
243 slave_filename[numslaves] = filename;
244 slave_name[numslaves] = myname;
245 slave_movex[numslaves] = config.movex;
246 slave_movey[numslaves] = config.movey;
247 slave_scalex[numslaves] = config.scalex;
248 slave_scaley[numslaves] = config.scaley;
249 slave_isframe[numslaves] = config.isframe;
250 config.isframe = 0;
251 config.movex = config.movey = 0;
252 config.scalex = config.scaley = 1.0;
253 numslaves ++;
255 return 0;
258 void args_callback_usage(char *name)
260 printf("\n");
261 printf("Usage: %s [-rXYomlcv] [-f] masterfile [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
262 printf("OR: %s [-rXYomv] --stack[1] [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
263 printf("OR: %s [-rXYov] --cat [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
264 printf("OR: %s [-rXYomlcv] --dummy [-xys] [file]\n", name);
265 printf("\n");
266 printf("-o , --output <outputfile> explicitly specify output file. (otherwise, output.swf will be used)\n");
267 printf("-t , --stack place each slave in a seperate frame (no master movie)\n");
268 printf("-T , --stack1 place each slave in the first frame (no master movie)\n");
269 printf("-m , --merge Don't store the slaves in Sprites/MovieClips\n");
270 printf("-a , --cat concatenate all slave files (no master movie)\n");
271 printf("-l , --overlay Don't remove any master objects, only overlay new objects\n");
272 printf("-c , --clip Clip the slave objects by the corresponding master objects\n");
273 printf("-v , --verbose Be verbose. Use more than one -v for greater effect \n");
274 printf("-d , --dummy Don't require slave objects (for changing movie attributes)\n");
275 printf("-f , --frame The following identifier is a frame or framelabel, not an id or objectname\n");
276 printf("-x , --movex <xpos> x Adjust position of slave by <xpos> pixels\n");
277 printf("-y , --movey <ypos> y Adjust position of slave by <ypos> pixels\n");
278 printf("-s , --scale <scale> Adjust size of slave by <scale> percent (e.g. 100% = original size)\n");
279 printf("-r , --rate <fps> Set movie framerate to <fps> (frames/sec)\n");
280 printf("-X , --width <width> Force movie bbox width to <width> (default: use master width (not with -t))\n");
281 printf("-Y , --height <height> Force movie bbox height to <height> (default: use master height (not with -t))\n");
282 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
283 printf("\n");
286 static void makestackmaster(SWF*swf)
288 TAG*tag;
289 int t;
290 SRECT box;
291 int fileversion = 1;
292 int frameRate = 256;
293 RGBA rgb;
294 rgb.r=rgb.b=rgb.g=0;
295 memset(&box, 0, sizeof(box));
297 /* scan all slaves for bounding box */
298 for(t=numslaves-1;t>=0;t--)
300 SWF head;
301 int ret;
302 int fi=open(slave_filename[t],O_RDONLY|O_BINARY);
303 TAG*tag;
304 if(fi<0 || swf_ReadSWF(fi, &head)<0) {
305 msg("<fatal> Couldn't open/read %s.", slave_filename[t]);
306 exit(1);
308 close(fi);
309 msg("<verbose> File %s has bounding box %d:%d:%d:%d\n",
310 slave_filename[t],
311 head.movieSize.xmin, head.movieSize.ymin,
312 head.movieSize.xmax, head.movieSize.ymax);
314 tag = head.firstTag;
315 while(tag) {
316 if(tag->id == ST_SETBACKGROUNDCOLOR && tag->len>=3) {
317 rgb.r = tag->data[0];
318 rgb.g = tag->data[1];
319 rgb.b = tag->data[2];
321 tag=tag->next;
323 frameRate = head.frameRate;
324 if(head.fileVersion > fileversion)
325 fileversion = head.fileVersion;
326 if(!t)
327 box = head.movieSize;
328 else {
329 if(head.movieSize.xmin < box.xmin)
330 box.xmin = head.movieSize.xmin;
331 if(head.movieSize.ymin < box.ymin)
332 box.ymin = head.movieSize.ymin;
333 if(head.movieSize.xmax > box.xmax)
334 box.xmax = head.movieSize.xmax;
335 if(head.movieSize.ymax > box.ymax)
336 box.ymax = head.movieSize.ymax;
338 msg("<verbose> New master bounding box is %d:%d:%d:%d\n",
339 box.xmin, box.ymin,
340 box.xmax, box.ymax);
341 swf_FreeTags(&head);
344 memset(swf, 0, sizeof(SWF));
345 swf->fileVersion = fileversion;
346 swf->movieSize = box;
347 swf->frameRate = frameRate;
349 swf->firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
350 tag = swf->firstTag;
351 swf_SetRGB(tag, &rgb);
353 for(t=0;t<numslaves;t++)
355 char buf[128];
356 sprintf(buf, "Frame%02d", t);
357 slave_name[t] = strdup(buf);
359 tag = swf_InsertTag(tag, ST_DEFINESPRITE);
360 swf_SetU16(tag, t+1);
361 swf_SetU16(tag, 0);
362 tag = swf_InsertTag(tag, ST_END);
363 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
364 swf_ObjectPlace(tag, t+1, 1+t,0,0, slave_name[t]);
366 if(!config.stack1 || t == numslaves-1) {
367 tag = swf_InsertTag(tag, ST_SHOWFRAME);
369 if(!config.stack)
370 if(t!=numslaves-1)
372 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
373 swf_SetU16(tag, 1+t);
376 tag = swf_InsertTag(tag, ST_END);
377 msg("<verbose> temporary SWF created");
380 static char* slavename = 0;
381 static int slaveid = -1;
382 static int slaveframe = -1;
383 static char masterbitmap[65536];
384 static char depthbitmap[65536];
386 #define FLAGS_WRITEDEFINES 1
387 #define FLAGS_WRITENONDEFINES 2
388 #define FLAGS_WRITESPRITE 4
389 #define FLAGS_WRITESLAVE 8
391 int get_free_id(char*bitmap)
393 int t;
394 for(t=1;t<65536;t++)
395 if(!bitmap[t]) {
396 bitmap[t] = 1;
397 return t;
399 return -1;
402 void jpeg_assert(SWF*master, SWF*slave)
404 /* TODO: if there's a jpegtable found, store it
405 and handle it together with the flash file
406 headers */
408 /* check that master and slave don't have both
409 jpegtables (which would be fatal) */
410 int pos;
411 TAG *mpos=0, *spos=0;
412 TAG *mtag,*stag;
413 pos = 0;
414 mtag = master->firstTag;
415 stag = slave->firstTag;
416 while(mtag)
418 if(mtag->id == ST_JPEGTABLES)
419 mpos = mtag;
420 mtag = mtag->next;
422 while(stag)
424 if(stag->id == ST_JPEGTABLES)
425 spos = stag;
426 stag = stag->next;
428 if(mpos && spos)
430 if(spos->len == mpos->len &&
431 !memcmp(spos->data, mpos->data, mpos->len))
433 // ok, both have jpegtables, but they're identical.
434 // delete one and don't throw an error
435 swf_DeleteTag(spos);
436 spos = 0;
439 if(spos && mpos) {
440 msg("<error> Master and slave have incompatible JPEGTABLES.");
444 TAG* write_sprite_defines(TAG*tag, SWF*sprite)
446 TAG*rtag = sprite->firstTag;
447 while(rtag && rtag->id!=ST_END) {
448 if(!swf_isAllowedSpriteTag(rtag)) {
449 msg("<debug> processing sprite tag %02x", tag->id);
450 if(swf_isDefiningTag(rtag))
452 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
453 tag->id, tag->len);
454 tag = swf_InsertTag(tag, rtag->id);
455 swf_SetBlock(tag, rtag->data, rtag->len);
457 else if(swf_isPseudoDefiningTag(rtag))
459 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
460 tag->id, tag->len);
461 tag = swf_InsertTag(tag, rtag->id);
462 swf_SetBlock(tag, rtag->data, rtag->len);
464 else {
465 switch(rtag->id)
467 case ST_JPEGTABLES:
468 /* if we get here, jpeg_assert has already run,
469 ensuring this is the only one of it's kind,
470 so we may safely write it out */
471 tag = swf_InsertTag(tag, rtag->id);
472 swf_SetBlock(tag, rtag->data, rtag->len);
473 break;
474 case ST_EXPORTASSETS:
475 msg("<debug> deliberately ignoring EXPORTASSETS tag");
476 break;
477 case ST_ENABLEDEBUGGER:
478 msg("<debug> deliberately ignoring ENABLEDEBUGGER tag");
479 break;
480 case ST_SETBACKGROUNDCOLOR:
481 msg("<debug> deliberately ignoring BACKGROUNDCOLOR tag");
482 break;
483 case 40:
484 case 49:
485 case 51:
486 msg("<notice> found tag %d. This is a Generator template, isn't it?", tag->id);
487 break;
488 default:
489 msg("<notice> funny tag: %d is neither defining nor sprite", tag->id);
493 rtag = rtag->next;
495 return tag;
498 void changedepth(TAG*tag, int add)
500 if(tag->id == ST_PLACEOBJECT)
501 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
502 if(tag->id == ST_PLACEOBJECT2)
503 PUT16(&tag->data[1],GET16(&tag->data[1])+add);
504 if(tag->id == ST_REMOVEOBJECT)
505 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
506 if(tag->id == ST_REMOVEOBJECT2)
507 PUT16(&tag->data[0],GET16(&tag->data[0])+add);
508 if(tag->id == ST_PLACEOBJECT2) {
509 SWFPLACEOBJECT obj;
510 U8 flags;
511 swf_SetTagPos(tag, 0);
512 flags = swf_GetU8(tag);
513 if(flags&2) swf_GetU16(tag); //id
514 if(flags&4) swf_GetMatrix(tag, 0);
515 if(flags&8) swf_GetCXForm(tag, 0,1);
516 if(flags&16) swf_GetU16(tag); //ratio
517 if(flags&64) {
518 swf_ResetReadBits(tag);
519 printf("%d->%d\n", GET16(&tag->data[tag->pos]),
520 GET16(&tag->data[tag->pos])+add);
521 PUT16(&tag->data[tag->pos],GET16(&tag->data[tag->pos])+add);
523 msg("<warning> Depth relocation not fully working yet with clipdepths", tag->id);
527 void matrix_adjust(MATRIX*m, int movex, int movey, float scalex, float scaley, int scalepos)
529 m->sx = (int)(m->sx*scalex);
530 m->sy = (int)(m->sy*scaley);
531 m->r1 = (int)(m->r1*scalex);
532 m->r0 = (int)(m->r0*scaley);
533 if(scalepos) {
534 m->tx *= scalex;
535 m->ty *= scaley;
537 m->tx += movex;
538 m->ty += movey;
541 void write_changepos(TAG*output, TAG*tag, int movex, int movey, float scalex, float scaley, int scalepos)
543 if(movex || movey || scalex != 1.0 || scaley != 1.0)
545 switch(tag->id)
547 case ST_PLACEOBJECT2: {
548 MATRIX m;
549 U8 flags;
550 swf_GetMatrix(0, &m);
551 tag->pos = 0;
552 tag->readBit = 0;
554 flags = swf_GetU8(tag);
555 swf_SetU8(output, flags|4);
556 swf_SetU16(output, swf_GetU16(tag)); //depth
557 //flags&1: move
558 if(flags&2) {
559 swf_SetU16(output, swf_GetU16(tag)); //id
561 // flags & 4
562 if(flags&4) {
563 swf_GetMatrix(tag, &m);
564 } else {
565 swf_GetMatrix(0, &m);
567 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
568 swf_SetMatrix(output, &m);
570 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
572 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
573 break;
575 case ST_PLACEOBJECT: {
576 MATRIX m;
577 swf_SetU16(output, swf_GetU16(tag)); //id
578 swf_SetU16(output, swf_GetU16(tag)); //depth
580 swf_GetMatrix(tag, &m);
581 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
582 swf_SetMatrix(output, &m);
584 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
586 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
587 break;
589 default:
590 swf_SetBlock(output, tag->data, tag->len);
593 else
595 swf_SetBlock(output, tag->data, tag->len);
599 TAG* write_sprite(TAG*tag, SWF*sprite, int spriteid, int replaceddefine)
601 TAG* definespritetag;
602 TAG* rtag;
603 int tmp;
605 definespritetag = tag = swf_InsertTag(tag, ST_DEFINESPRITE);
606 swf_SetU16(tag, spriteid);
607 swf_SetU16(tag, sprite->frameCount);
608 msg ("<notice> sprite id is %d", spriteid);
610 tmp = sprite->frameCount;
611 msg("<debug> %d frames to go",tmp);
613 if(config.clip) {
614 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
615 swf_SetU8(tag, 2+64); //flags: character+clipdepth
616 swf_SetU16(tag, 0); //depth
617 swf_SetU16(tag, replaceddefine); //id
618 swf_SetU16(tag, 65535); //clipdepth
621 if(config.overlay && !config.isframe) {
622 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
623 swf_SetU8(tag, 2); //flags: character
624 swf_SetU16(tag, 1); //depth
625 swf_SetU16(tag, replaceddefine); //id
628 rtag = sprite->firstTag;
629 while(rtag && rtag->id!=ST_END)
631 if (swf_isAllowedSpriteTag(rtag)) {
633 msg("<debug> [sprite main] write tag %02x (%d bytes in body)",
634 rtag->id, rtag->len);
635 tag = swf_InsertTag(tag, rtag->id);
636 write_changepos(tag, rtag, config.movex, config.movey, config.scalex, config.scaley, 0);
638 if(config.clip || (config.overlay && !config.isframe))
639 changedepth(tag, +2);
641 if(tag->id == ST_SHOWFRAME)
643 tmp--;
644 msg("<debug> %d frames to go",tmp);
647 rtag = rtag->next;
649 tag = swf_InsertTag(tag, ST_END);
650 return tag;
653 static char tag_ok_for_slave(int id)
655 if(id == ST_SETBACKGROUNDCOLOR)
656 return 0;
657 return 1;
660 TAG* write_master(TAG*tag, SWF*master, SWF*slave, int spriteid, int replaceddefine, int flags)
662 int outputslave = 0;
663 int frame = 0;
664 int sframe = 0;
665 int slavewritten = 0;
667 TAG* rtag = master->firstTag;
668 TAG* stag = slave->firstTag;
670 while(rtag && rtag->id!=ST_END)
672 if(rtag->id == ST_SHOWFRAME && outputslave)
674 while(stag && stag->id!=ST_END) {
675 if(stag->id == ST_SHOWFRAME) {
676 stag = stag->next;
677 sframe++;
678 break;
680 if(tag_ok_for_slave(stag->id)) {
681 tag = swf_InsertTag(tag, stag->id);
682 swf_SetBlock(tag, stag->data, stag->len);
684 stag = stag->next;
687 if(rtag->id == ST_SHOWFRAME)
689 frame ++;
692 if(swf_isDefiningTag(rtag) && (flags&FLAGS_WRITEDEFINES))
694 msg("<debug> [master] write tag %02x (%d bytes in body)",
695 rtag->id, rtag->len);
696 if(swf_GetDefineID(rtag) == spriteid && !config.isframe)
698 if(config.overlay)
700 tag = swf_InsertTag(tag, rtag->id);
701 swf_SetBlock(tag, rtag->data, rtag->len);
702 swf_SetDefineID(tag, replaceddefine);
703 } else {
704 /* don't write this tag */
705 msg("<verbose> replacing tag %d id %d with sprite", rtag->id
706 ,spriteid);
709 if(flags&FLAGS_WRITESPRITE)
711 tag = write_sprite_defines(tag, slave);
712 tag = write_sprite(tag, slave, spriteid, replaceddefine);
714 if(flags&FLAGS_WRITESLAVE)
716 outputslave = 1;
718 } else {
719 tag = swf_InsertTag(tag, rtag->id);
720 swf_SetBlock(tag, rtag->data, rtag->len);
723 if(frame == slaveframe)
725 if(flags&FLAGS_WRITESLAVE) {
726 outputslave = 1;
727 slavewritten = 1;
729 if((flags&FLAGS_WRITESPRITE) && !slavewritten)
731 int id = get_free_id(masterbitmap);
732 int depth = 0;
733 if(config.clip) {
734 msg("<fatal> Can't combine --clip and --frame");
737 tag = write_sprite_defines(tag, slave);
738 tag = write_sprite(tag, slave, id, -1);
740 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
741 swf_SetU8(tag, 2); //flags: id
742 swf_SetU16(tag, depth);
743 swf_SetU16(tag, id);
745 slavewritten = 1;
748 if(!swf_isDefiningTag(rtag) && (flags&FLAGS_WRITENONDEFINES))
750 int dontwrite = 0;
751 switch(rtag->id) {
752 case ST_PLACEOBJECT:
753 case ST_PLACEOBJECT2:
754 if(frame == slaveframe && !config.overlay)
755 dontwrite = 1;
756 case ST_REMOVEOBJECT:
757 /* place/removetags for the object we replaced
758 should be discarded, too, as the object to insert
759 isn't a sprite
761 if(spriteid>=0 && swf_GetPlaceID(rtag) == spriteid &&
762 !config.isframe && config.merge)
763 dontwrite = 1;
764 break;
765 case ST_REMOVEOBJECT2:
766 break;
768 if(!dontwrite) {
769 msg("<debug> [master] write tag %02x (%d bytes in body)",
770 rtag->id, rtag->len);
771 tag = swf_InsertTag(tag, rtag->id);
772 write_changepos(tag, rtag, config.mastermovex, config.mastermovey, config.masterscalex, config.masterscaley, 1);
775 rtag = rtag->next;
778 if(outputslave)
779 while(stag && stag->id!=ST_END)
781 if(tag_ok_for_slave(stag->id)) {
782 tag = swf_InsertTag(tag, stag->id);
783 swf_SetBlock(tag, stag->data, stag->len);
785 stag = stag->next;
787 if(!slavewritten && config.isframe && (flags&(FLAGS_WRITESLAVE|FLAGS_WRITESPRITE)))
789 if(slaveframe>=0)
790 msg("<warning> Frame %d doesn't exist in file. No substitution will occur",
791 slaveframe);
792 else
793 msg("<warning> Frame \"%s\" doesn't exist in file. No substitution will occur",
794 slavename);
796 tag = swf_InsertTag(tag, ST_END);
797 return tag;
800 void adjustheader(SWF*swf)
802 if(config.framerate)
803 swf->frameRate = config.framerate;
804 if(config.hassizex) {
805 swf->movieSize.xmax =
806 swf->movieSize.xmin + config.sizex;
808 if(config.hassizey) {
809 swf->movieSize.ymax =
810 swf->movieSize.ymin + config.sizey;
814 void catcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
816 char* depths;
817 int t;
818 TAG*tag;
819 TAG*mtag,*stag;
820 if(config.isframe) {
821 msg("<fatal> Can't combine --cat and --frame");
822 exit(1);
825 tag = master->firstTag;
826 while(tag)
828 if(swf_isDefiningTag(tag)) {
829 int defineid = swf_GetDefineID(tag);
830 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
831 masterbitmap[defineid] = 1;
833 tag = tag->next;
836 swf_Relocate(slave, masterbitmap);
837 jpeg_assert(master, slave);
839 memcpy(newswf, master, sizeof(SWF));
840 adjustheader(newswf);
842 tag = newswf->firstTag = swf_InsertTag(0, ST_REFLEX); // to be removed later
844 depths = malloc(65536);
845 if(!depths) {
846 msg("<fatal> Couldn't allocate %d bytes of memory", 65536);
847 return;
849 memset(depths, 0, 65536);
850 mtag = master->firstTag;
851 while(mtag && mtag->id!=ST_END)
853 int num=1;
854 U16 depth;
855 msg("<debug> [master] write tag %02x (%d bytes in body)",
856 mtag->id, mtag->len);
857 switch(mtag->id) {
858 case ST_PLACEOBJECT2:
859 num++;
860 case ST_PLACEOBJECT: {
861 depth = swf_GetDepth(mtag);
862 depths[depth] = 1;
864 break;
865 case ST_REMOVEOBJECT: {
866 depth = swf_GetDepth(mtag);
867 depths[depth] = 0;
869 break;
870 case ST_REMOVEOBJECT2: {
871 depth = swf_GetDepth(mtag);
872 depths[depth] = 0;
874 break;
876 tag = swf_InsertTag(tag, mtag->id);
877 swf_SetBlock(tag, mtag->data, mtag->len);
879 mtag = mtag->next;
882 for(t=0;t<65536;t++)
883 if(depths[t])
885 char data[16];
886 int len;
887 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
888 swf_SetU16(tag, t);
890 free(depths);
892 stag = slave->firstTag;
893 while(stag && stag->id!=ST_END)
895 msg("<debug> [slave] write tag %02x (%d bytes in body)",
896 stag->id, stag->len);
897 tag = swf_InsertTag(tag, stag->id);
898 swf_SetBlock(tag, stag->data, stag->len);
899 stag = stag->next;
901 tag = swf_InsertTag(tag, ST_END);
903 tag = newswf->firstTag;
904 newswf->firstTag = newswf->firstTag->next; //remove temporary tag
905 swf_DeleteTag(tag);
908 void normalcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
910 int spriteid = -1;
911 int replaceddefine = -1;
912 int frame = 0;
913 char*framelabel;
914 TAG * tag = master->firstTag;
916 memset(depthbitmap, 0, sizeof(depthbitmap));
918 // set the idtab
919 while(tag)
921 int depth = swf_GetDepth(tag);
922 if(depth>=0) {
923 depthbitmap[depth] = 1;
925 if(swf_isDefiningTag(tag)) {
926 int defineid = swf_GetDefineID(tag);
927 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
928 masterbitmap[defineid] = 1;
930 if (!slavename && defineid==slaveid) {
931 if(defineid>=0) {
932 spriteid = defineid;
933 msg("<notice> Slave file attached to object %d.", defineid);
936 } else if(tag->id == ST_PLACEOBJECT2) {
937 char * name = swf_GetName(tag);
938 int id = swf_GetPlaceID(tag);
940 if(name)
941 msg("<verbose> tagid %02x places object %d named \"%s\"", tag->id, id, name);
942 else
943 msg("<verbose> tagid %02x places object %d (no name)", tag->id, id);
945 if (name && slavename && !strcmp(name,slavename)) {
946 if(id>=0) {
947 spriteid = id;
948 msg("<notice> Slave file attached to named object %s (%d).", name, id);
951 } else if(tag->id == ST_SHOWFRAME) {
952 if(slaveframe>=0 && frame==slaveframe) {
953 msg("<notice> Slave file attached to frame %d.", frame);
955 frame++;
956 } else if(tag->id == ST_FRAMELABEL) {
957 char * name = tag->data;
958 if(name && slavename && config.isframe && !strcmp(name, slavename)) {
959 slaveframe = frame;
960 msg("<notice> Slave file attached to frame %d (%s).", frame, name);
963 tag = tag->next;
966 if (spriteid<0 && !config.isframe) {
967 if(slavename) {
968 if(strcmp(slavename,"!!dummy!!"))
969 msg("<warning> Didn't find anything named %s in file. No substitutions will occur.", slavename);
971 else
972 msg("<warning> Didn't find id %d in file. No substitutions will occur.", slaveid);
973 spriteid = get_free_id(masterbitmap);
976 swf_Relocate (slave, masterbitmap);
977 if(config.merge)
978 swf_RelocateDepth (slave, depthbitmap);
979 jpeg_assert(slave, master);
981 if (config.overlay)
982 replaceddefine = get_free_id(masterbitmap);
984 // write file
986 memcpy(newswf, master, sizeof(SWF));
987 adjustheader(newswf);
989 newswf->firstTag = tag = swf_InsertTag(0, ST_REFLEX); // to be removed later
991 if (config.antistream) {
992 if (config.merge) {
993 msg("<fatal> Can't combine --antistream and --merge");
995 tag = write_sprite_defines(tag, slave);
996 tag = write_sprite(tag, slave, spriteid, replaceddefine);
997 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITEDEFINES);
998 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITENONDEFINES);
999 } else {
1000 if (config.merge)
1001 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1002 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESLAVE );
1003 else
1004 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1005 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESPRITE );
1008 tag = newswf->firstTag;
1009 newswf->firstTag = newswf->firstTag->next; //remove temporary tag
1010 swf_DeleteTag(tag);
1013 void combine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
1015 slavename = slave_name;
1016 slaveid = -1;
1017 slaveframe = -1;
1019 swf_FoldAll(master);
1020 swf_FoldAll(slave);
1022 if(slavename[0] == '#')
1024 slaveid = atoi(&slavename[1]);
1025 slavename = 0;
1028 if(config.isframe)
1030 int tmp;
1031 if(slavename && slavename[0]!='#' && (sscanf(slavename, "%d", &tmp) ==
1032 strlen(slavename))) {
1033 /* if the name the slave should replace
1034 consists only of digits and the -f
1035 option is given, it probably is not
1036 a frame name but a frame number.
1038 slaveid = tmp;
1039 slavename = 0;
1042 if(slaveid>=0) {
1043 slaveframe = slaveid;
1044 slaveid = -1;
1045 } else {
1046 /* if id wasn't given as either #number or number,
1047 the name is a frame label. BTW: The user wouldn't necessarily have
1048 needed to supply the -f option in this case */
1052 msg("<debug> move x (%d)", config.movex);
1053 msg("<debug> move y (%d)", config.movey);
1054 msg("<debug> scale x (%f)", config.scalex);
1055 msg("<debug> scale y (%f)", config.scaley);
1056 msg("<debug> master move x (%d)", config.mastermovex);
1057 msg("<debug> master move y (%d)", config.mastermovey);
1058 msg("<debug> master scale x (%f)", config.masterscalex);
1059 msg("<debug> master scale y (%f)", config.masterscaley);
1060 msg("<debug> is frame (%d)", config.isframe);
1062 memset(masterbitmap, 0, sizeof(masterbitmap));
1064 if(config.cat)
1065 return catcombine(master, slave_name, slave, newswf);
1066 else
1067 return normalcombine(master, slave_name, slave, newswf);
1070 int main(int argn, char *argv[])
1072 int fi;
1073 SWF master;
1074 SWF slave;
1075 SWF newswf;
1076 int t;
1078 config.overlay = 0;
1079 config.antistream = 0;
1080 config.alloctest = 0;
1081 config.cat = 0;
1082 config.merge = 0;
1083 config.clip = 0;
1084 config.loglevel = 2;
1085 config.movex = 0;
1086 config.movey = 0;
1087 config.scalex = 1.0;
1088 config.scaley = 1.0;
1089 config.sizex = 0;
1090 config.sizey = 0;
1091 config.masterscalex = 1.0;
1092 config.masterscaley = 1.0;
1093 config.mastermovex = 0;
1094 config.mastermovey = 0;
1095 config.hassizex = 0;
1096 config.hassizey = 0;
1097 config.framerate = 0;
1098 config.stack = 0;
1099 config.stack1 = 0;
1100 config.dummy = 0;
1101 config.zlib = 0;
1103 processargs(argn, argv);
1104 initLog(0,-1,0,0,-1,config.loglevel);
1106 if(config.merge && config.cat) {
1107 msg("<error> Can't combine --cat and --merge");
1108 exit(1);
1111 if(config.stack) {
1112 if(config.overlay) {
1113 msg("<error> Can't combine -l and -t");
1114 exit(1);
1116 if(config.clip) {
1117 msg("<error> Can't combine -c and -t");
1118 exit(1);
1120 msg("<verbose> (stacking) %d files found\n", numslaves);
1122 makestackmaster(&master);
1124 else {
1125 int ret;
1126 msg("<verbose> master entity %s (named \"%s\")\n", master_filename, master_name);
1127 fi = open(master_filename, O_RDONLY|O_BINARY);
1128 if(fi<0) {
1129 msg("<fatal> Failed to open %s\n", master_filename);
1130 exit(1);
1132 ret = swf_ReadSWF(fi, &master);
1133 if(ret<0) {
1134 msg("<fatal> Failed to read from %s\n", master_filename);
1135 exit(1);
1137 msg("<debug> Read %d bytes from masterfile\n", ret);
1138 close(fi);
1141 for(t=0;t<numslaves;t++) {
1142 msg("<verbose> slave entity(%d) %s (%s \"%s\")\n", t+1, slave_filename[t],
1143 slave_isframe[t]?"frame":"object", slave_name[t]);
1146 if(config.dummy)
1148 if(numslaves)
1150 msg("<error> --dummy (-d) implies there are zero slave objects. You supplied %d.", numslaves);
1151 exit(1);
1153 numslaves = 1;
1154 slave_filename[0] = "!!dummy!!";
1155 slave_name[0] = "!!dummy!!";
1156 slave_isframe[0] = 0;
1159 if (config.alloctest)
1161 char*bitmap = malloc(sizeof(char)*65536);
1162 memset(bitmap, 0, 65536*sizeof(char));
1163 memset(bitmap, 1, 101*sizeof(char));
1164 swf_Relocate(&master, bitmap);
1165 newswf = master;
1166 free(bitmap);
1167 // makestackmaster(&newswf);
1169 else
1171 if (!numslaves)
1173 if(config.cat)
1174 msg("<error> You must have at least two objects.");
1175 else
1176 msg("<error> You must have at least one slave entity.");
1177 return 0;
1179 for(t = 0; t < numslaves; t++)
1181 config.movex = slave_movex[t];
1182 config.movey = slave_movey[t];
1183 config.scalex = slave_scalex[t];
1184 config.scaley = slave_scaley[t];
1185 config.isframe = slave_isframe[t];
1187 msg("<notice> Combine [%s]%s and [%s]%s", master_name, master_filename,
1188 slave_name[t], slave_filename[t]);
1189 if(!config.dummy)
1191 int ret;
1192 fi = open(slave_filename[t], O_RDONLY|O_BINARY);
1193 if(!fi) {
1194 msg("<fatal> Failed to open %s\n", slave_filename[t]);
1195 exit(1);
1197 ret = swf_ReadSWF(fi, &slave);
1198 if(ret<0) {
1199 msg("<fatal> Failed to read from %s\n", slave_filename[t]);
1200 exit(1);
1202 msg("<debug> Read %d bytes from slavefile\n", ret);
1203 close(fi);
1205 else
1207 memset(&slave, 0, sizeof(slave));
1208 slave.firstTag = swf_InsertTag(0, ST_END);
1209 slave.frameRate = 0;
1210 slave.fileVersion = 4;
1211 slave.frameCount = 0;
1214 combine(&master, slave_name[t], &slave, &newswf);
1215 master = newswf;
1219 fi = open(outputname, O_BINARY|O_RDWR|O_TRUNC|O_CREAT, 0777);
1221 if(config.zlib)
1222 swf_WriteSWC(fi, &newswf);
1223 else {
1224 newswf.compressed = 0;
1225 swf_WriteSWF(fi, &newswf);
1227 close(fi);
1228 return 0;