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 */
25 #include "../lib/rfxswf.h"
26 #include "../lib/args.h"
27 #include "../lib/log.h"
28 #include "../config.h"
43 char local_with_networking;
44 char local_with_filesystem;
61 struct config_t config;
63 char * master_filename = 0;
64 char * master_name = 0;
65 char * slave_filename[128];
66 char * slave_name[128];
69 float slave_scalex[128];
70 float slave_scaley[128];
71 char slave_isframe[128];
74 char * outputname = "output.swf";
76 int args_callback_option(char*name,char*val) {
82 else if(!strcmp(name,"l"))
87 else if (!strcmp(name, "o"))
92 else if (!strcmp(name, "v"))
97 else if (!strcmp(name, "a"))
102 else if (!strcmp(name, "A"))
104 config.alloctest = 1;
107 else if (!strcmp(name, "x"))
110 config.movex = (int)(x*20+0.5);
113 else if (!strcmp(name, "y"))
116 config.movey = (int)(y*20+0.5);
119 else if (!strcmp(name, "m"))
124 else if (!strcmp(name, "f"))
129 else if (!strcmp(name, "d"))
134 else if (!strcmp(name, "z"))
139 else if (!strcmp(name, "r"))
142 float rate = atof(val);
143 if ((rate < 0) ||(rate >= 256.0)) {
144 fprintf(stderr, "Error: You must specify a valid framerate between 1/256 and 255.\n");
147 config.framerate = (int)(rate*256);
150 else if (!strcmp(name, "X"))
152 config.sizex = atoi(val)*20;
156 else if (!strcmp(name, "Y"))
158 config.sizey = atoi(val)*20;
162 else if (!strcmp(name, "s"))
164 config.scalex = config.scaley = atoi(val)/100.0;
167 else if (!strcmp(name, "w"))
169 config.scalex = atoi(val)/100.0;
172 else if (!strcmp(name, "h"))
174 config.scaley = atoi(val)/100.0;
177 else if (!strcmp(name, "N"))
179 config.local_with_networking = 1;
182 else if (!strcmp(name, "L"))
184 config.local_with_filesystem = 1;
187 else if (!strcmp(name, "t") || !strcmp(name, "T"))
189 if(master_filename) {
190 fprintf(stderr, "error with arguments. Try --help.\n");
194 if(!strcmp(name,"T"))
196 master_filename = "__none__";
199 else if (!strcmp(name, "V"))
201 printf("swfcombine - part of %s %s\n", PACKAGE, VERSION);
206 fprintf(stderr, "Unknown option: -%s\n", name);
211 static struct options_t options[] = {
229 {"N", "local-with-network"},
230 {"L", "local-with-filesystem"},
235 int args_callback_longoption(char*name,char*val) {
236 return args_long2shortoption(options, name, val);
239 int args_callback_command(char*name, char*val) {
240 char*myname = strdup(name);
242 filename = strchr(myname, '=');
247 // argument has no explicit name field. guess one from the file name
248 char*path = strrchr(myname, '/');
249 char*ext = strrchr(myname, '.');
250 if(!path) path = myname;
257 if(!master_filename) {
258 master_filename = filename;
259 master_name = myname;
260 config.mastermovex = config.movex;
261 config.mastermovey = config.movey;
262 config.masterscalex = config.scalex;
263 config.masterscaley = config.scaley;
264 config.movex = config.movey = 0;
265 config.scalex = config.scaley = 1.0;
267 msg("<verbose> slave entity %s (named \"%s\")\n", filename, myname);
269 slave_filename[numslaves] = filename;
270 slave_name[numslaves] = myname;
271 slave_movex[numslaves] = config.movex;
272 slave_movey[numslaves] = config.movey;
273 slave_scalex[numslaves] = config.scalex;
274 slave_scaley[numslaves] = config.scaley;
275 slave_isframe[numslaves] = config.isframe;
277 config.movex = config.movey = 0;
278 config.scalex = config.scaley = 1.0;
284 void args_callback_usage(char *name)
287 printf("Usage: %s [-rXYomlcv] [-f] masterfile [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
288 printf("OR: %s [-rXYomv] --stack[1] [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
289 printf("OR: %s [-rXYov] --cat [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
290 printf("OR: %s [-rXYomlcv] --dummy [-xys] [file]\n", name);
292 printf("-o , --output <outputfile> explicitly specify output file. (otherwise, output.swf will be used)\n");
293 printf("-t , --stack place each slave in a seperate frame (no master movie)\n");
294 printf("-T , --stack1 place each slave in the first frame (no master movie)\n");
295 printf("-m , --merge Don't store the slaves in Sprites/MovieClips\n");
296 printf("-a , --cat concatenate all slave files (no master movie)\n");
297 printf("-l , --overlay Don't remove any master objects, only overlay new objects\n");
298 printf("-c , --clip Clip the slave objects by the corresponding master objects\n");
299 printf("-v , --verbose Be verbose. Use more than one -v for greater effect \n");
300 printf("-d , --dummy Don't require slave objects (for changing movie attributes)\n");
301 printf("-f , --frame The following identifier is a frame or framelabel, not an id or objectname\n");
302 printf("-x , --movex <xpos> x Adjust position of slave by <xpos> pixels\n");
303 printf("-y , --movey <ypos> y Adjust position of slave by <ypos> pixels\n");
304 printf("-s , --scale <scale> Adjust size of slave by <scale> percent (e.g. 100% = original size)\n");
305 printf("-r , --rate <fps> Set movie framerate to <fps> (frames/sec)\n");
306 printf("-X , --width <width> Force movie bbox width to <width> (default: use master width (not with -t))\n");
307 printf("-Y , --height <height> Force movie bbox height to <height> (default: use master height (not with -t))\n");
308 printf("-N , --local-with-networking Make output file \"local-with-networking\"\n");
309 printf("-L , --local-with-filesystem Make output file \"local-with-filesystem\"\n");
310 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
314 void removeCommonTags(SWF * swf)
316 TAG*tag = swf->firstTag;
318 if(tag->id == ST_SCENEDESCRIPTION ||
319 tag->id == ST_FILEATTRIBUTES ||
320 tag->id == ST_REFLEX) {
321 tag = swf_DeleteTag(swf, tag);
328 static void makestackmaster(SWF*swf)
333 int fileversion = config.zlib?6:3;
335 U32 fileAttributes = 0;
338 memset(&box, 0, sizeof(box));
340 /* scan all slaves for bounding box */
341 for(t=numslaves-1;t>=0;t--)
345 int fi=open(slave_filename[t],O_RDONLY|O_BINARY);
347 if(fi<0 || swf_ReadSWF(fi, &head)<0) {
348 msg("<fatal> Couldn't open/read %s.", slave_filename[t]);
352 swf_RemoveJPEGTables(&head);
353 fileAttributes |= head.fileAttributes;
354 removeCommonTags(&head);
356 msg("<verbose> File %s has bounding box %d:%d:%d:%d\n",
358 head.movieSize.xmin, head.movieSize.ymin,
359 head.movieSize.xmax, head.movieSize.ymax);
363 if(tag->id == ST_SETBACKGROUNDCOLOR && tag->len>=3) {
364 rgb.r = tag->data[0];
365 rgb.g = tag->data[1];
366 rgb.b = tag->data[2];
370 frameRate = head.frameRate;
371 if(head.fileVersion > fileversion)
372 fileversion = head.fileVersion;
374 box = head.movieSize;
376 if(head.movieSize.xmin < box.xmin)
377 box.xmin = head.movieSize.xmin;
378 if(head.movieSize.ymin < box.ymin)
379 box.ymin = head.movieSize.ymin;
380 if(head.movieSize.xmax > box.xmax)
381 box.xmax = head.movieSize.xmax;
382 if(head.movieSize.ymax > box.ymax)
383 box.ymax = head.movieSize.ymax;
385 msg("<verbose> New master bounding box is %d:%d:%d:%d\n",
391 memset(swf, 0, sizeof(SWF));
392 swf->fileVersion = fileversion;
393 swf->movieSize = box;
394 swf->frameRate = frameRate;
395 swf->fileAttributes = fileAttributes;
397 swf->firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
399 swf_SetRGB(tag, &rgb);
401 for(t=0;t<numslaves;t++)
404 sprintf(buf, "Frame%02d", t);
405 slave_name[t] = strdup(buf);
407 tag = swf_InsertTag(tag, ST_DEFINESPRITE);
408 swf_SetU16(tag, t+1);
410 tag = swf_InsertTag(tag, ST_END);
411 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
412 swf_ObjectPlace(tag, t+1, 1+t,0,0, slave_name[t]);
414 if(!config.stack1 || t == numslaves-1) {
415 tag = swf_InsertTag(tag, ST_SHOWFRAME);
420 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
421 swf_SetU16(tag, 1+t);
424 tag = swf_InsertTag(tag, ST_END);
425 msg("<verbose> temporary SWF created");
428 static char* slavename = 0;
429 static int slaveid = -1;
430 static int slaveframe = -1;
431 static char masterbitmap[65536];
432 static char depthbitmap[65536];
434 #define FLAGS_WRITEDEFINES 1
435 #define FLAGS_WRITENONDEFINES 2
436 #define FLAGS_WRITESPRITE 4
437 #define FLAGS_WRITESLAVE 8
439 int get_free_id(char*bitmap)
450 void jpeg_assert(SWF*master, SWF*slave)
452 /* TODO: if there's a jpegtable found, store it
453 and handle it together with the flash file
456 /* check that master and slave don't have both
457 jpegtables (which would be fatal) */
459 TAG *mpos=0, *spos=0;
462 mtag = master->firstTag;
463 stag = slave->firstTag;
466 if(mtag->id == ST_JPEGTABLES)
472 if(stag->id == ST_JPEGTABLES)
478 if(spos->len == mpos->len &&
479 !memcmp(spos->data, mpos->data, mpos->len))
481 // ok, both have jpegtables, but they're identical.
482 // delete one and don't throw an error
483 swf_DeleteTag(slave, spos);
488 msg("<error> Master and slave have incompatible JPEGTABLES.");
492 TAG* write_sprite_defines(TAG*tag, SWF*sprite)
494 TAG*rtag = sprite->firstTag;
495 while(rtag && rtag->id!=ST_END) {
496 if(!swf_isAllowedSpriteTag(rtag)) {
497 msg("<debug> processing sprite tag %02x", tag->id);
498 if(swf_isDefiningTag(rtag))
500 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
502 tag = swf_InsertTag(tag, rtag->id);
503 swf_SetBlock(tag, rtag->data, rtag->len);
505 else if(swf_isPseudoDefiningTag(rtag))
507 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
509 tag = swf_InsertTag(tag, rtag->id);
510 swf_SetBlock(tag, rtag->data, rtag->len);
516 /* if we get here, jpeg_assert has already run,
517 ensuring this is the only one of it's kind,
518 so we may safely write it out */
519 tag = swf_InsertTag(tag, rtag->id);
520 swf_SetBlock(tag, rtag->data, rtag->len);
522 case ST_EXPORTASSETS:
523 msg("<debug> deliberately ignoring EXPORTASSETS tag");
525 case ST_ENABLEDEBUGGER:
526 msg("<debug> deliberately ignoring ENABLEDEBUGGER tag");
528 case ST_SETBACKGROUNDCOLOR:
529 msg("<debug> deliberately ignoring BACKGROUNDCOLOR tag");
532 msg("<debug> deliberately ignoring SHOWFRAME tag");
535 msg("<debug> deliberately ignoring REFLEX tag");
540 msg("<notice> found tag %d. This is a Generator template, isn't it?", rtag->id);
543 msg("<notice> funny tag: %d is neither defining nor sprite", rtag->id);
552 void changedepth(TAG*tag, int add)
554 if(tag->id == ST_PLACEOBJECT)
555 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
556 if(tag->id == ST_PLACEOBJECT2)
557 PUT16(&tag->data[1],GET16(&tag->data[1])+add);
558 if(tag->id == ST_REMOVEOBJECT)
559 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
560 if(tag->id == ST_REMOVEOBJECT2)
561 PUT16(&tag->data[0],GET16(&tag->data[0])+add);
562 if(tag->id == ST_PLACEOBJECT2) {
565 swf_SetTagPos(tag, 0);
566 flags = swf_GetU8(tag);
567 if(flags&2) swf_GetU16(tag); //id
568 if(flags&4) swf_GetMatrix(tag, 0);
569 if(flags&8) swf_GetCXForm(tag, 0,1);
570 if(flags&16) swf_GetU16(tag); //ratio
572 swf_ResetReadBits(tag);
573 printf("%d->%d\n", GET16(&tag->data[tag->pos]),
574 GET16(&tag->data[tag->pos])+add);
575 PUT16(&tag->data[tag->pos],GET16(&tag->data[tag->pos])+add);
577 msg("<warning> Depth relocation not fully working yet with clipdepths", tag->id);
581 void matrix_adjust(MATRIX*m, int movex, int movey, float scalex, float scaley, int scalepos)
583 m->sx = (int)(m->sx*scalex);
584 m->sy = (int)(m->sy*scaley);
585 m->r1 = (int)(m->r1*scalex);
586 m->r0 = (int)(m->r0*scaley);
595 void write_changepos(TAG*output, TAG*tag, int movex, int movey, float scalex, float scaley, int scalepos)
597 if(movex || movey || scalex != 1.0 || scaley != 1.0)
601 case ST_PLACEOBJECT2: {
604 swf_GetMatrix(0, &m);
608 flags = swf_GetU8(tag);
609 swf_SetU8(output, flags|4);
610 swf_SetU16(output, swf_GetU16(tag)); //depth
613 swf_SetU16(output, swf_GetU16(tag)); //id
617 swf_GetMatrix(tag, &m);
619 swf_GetMatrix(0, &m);
621 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
622 swf_SetMatrix(output, &m);
624 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
626 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
629 case ST_PLACEOBJECT: {
631 swf_SetU16(output, swf_GetU16(tag)); //id
632 swf_SetU16(output, swf_GetU16(tag)); //depth
634 swf_GetMatrix(tag, &m);
635 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
636 swf_SetMatrix(output, &m);
638 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
640 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
644 swf_SetBlock(output, tag->data, tag->len);
649 swf_SetBlock(output, tag->data, tag->len);
653 TAG* write_sprite(TAG*tag, SWF*sprite, int spriteid, int replaceddefine)
655 TAG* definespritetag;
659 definespritetag = tag = swf_InsertTag(tag, ST_DEFINESPRITE);
660 swf_SetU16(tag, spriteid);
661 swf_SetU16(tag, sprite->frameCount);
662 msg ("<notice> sprite id is %d", spriteid);
664 tmp = sprite->frameCount;
665 msg("<debug> %d frames to go",tmp);
668 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
669 swf_SetU8(tag, 2+64); //flags: character+clipdepth
670 swf_SetU16(tag, 0); //depth
671 swf_SetU16(tag, replaceddefine); //id
672 swf_SetU16(tag, 65535); //clipdepth
675 if(config.overlay && !config.isframe) {
676 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
677 swf_SetU8(tag, 2); //flags: character
678 swf_SetU16(tag, 1); //depth
679 swf_SetU16(tag, replaceddefine); //id
682 rtag = sprite->firstTag;
683 while(rtag && rtag->id!=ST_END)
685 if (swf_isAllowedSpriteTag(rtag)) {
687 msg("<debug> [sprite main] write tag %02x (%d bytes in body)",
688 rtag->id, rtag->len);
689 tag = swf_InsertTag(tag, rtag->id);
690 write_changepos(tag, rtag, config.movex, config.movey, config.scalex, config.scaley, 0);
692 if(config.clip || (config.overlay && !config.isframe))
693 changedepth(tag, +2);
695 if(tag->id == ST_SHOWFRAME)
698 msg("<debug> %d frames to go",tmp);
703 tag = swf_InsertTag(tag, ST_END);
707 static char tag_ok_for_slave(int id)
709 if(id == ST_SETBACKGROUNDCOLOR)
714 TAG* write_master(TAG*tag, SWF*master, SWF*slave, int spriteid, int replaceddefine, int flags)
719 int slavewritten = 0;
720 int deletedepth = -1;
722 TAG* rtag = master->firstTag;
723 TAG* stag = slave->firstTag;
725 while(rtag && rtag->id!=ST_END)
727 if(rtag->id == ST_SHOWFRAME && outputslave)
729 while(stag && stag->id!=ST_END) {
730 if(stag->id == ST_SHOWFRAME) {
735 if(tag_ok_for_slave(stag->id)) {
736 tag = swf_InsertTag(tag, stag->id);
737 write_changepos(tag, stag, config.movex, config.movey, config.scalex, config.scaley, 0);
742 if(rtag->id == ST_SHOWFRAME)
745 tag = swf_InsertTag(tag, ST_SHOWFRAME);
747 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
748 swf_SetU16(tag, deletedepth);
755 if(swf_isDefiningTag(rtag) && (flags&FLAGS_WRITEDEFINES))
757 msg("<debug> [master] write tag %02x (%d bytes in body)",
758 rtag->id, rtag->len);
759 if(swf_GetDefineID(rtag) == spriteid && !config.isframe)
763 tag = swf_InsertTag(tag, rtag->id);
764 swf_SetBlock(tag, rtag->data, rtag->len);
765 swf_SetDefineID(tag, replaceddefine);
767 /* don't write this tag */
768 msg("<verbose> replacing tag %d ID %d with sprite", rtag->id ,spriteid);
771 if(flags&FLAGS_WRITESPRITE)
773 msg("<debug> writing sprite defines");
774 tag = write_sprite_defines(tag, slave);
775 msg("<debug> writing sprite");
776 tag = write_sprite(tag, slave, spriteid, replaceddefine);
778 if(flags&FLAGS_WRITESLAVE)
780 msg("<debug> writing slave");
784 tag = swf_InsertTag(tag, rtag->id);
785 swf_SetBlock(tag, rtag->data, rtag->len);
788 if(frame == slaveframe) /* only happens with config.isframe: put slave at specific frame */
790 if(flags&FLAGS_WRITESLAVE) {
794 if((flags&FLAGS_WRITESPRITE) && !slavewritten)
796 int id = get_free_id(masterbitmap);
800 msg("<fatal> Can't combine --clip and --frame");
803 tag = write_sprite_defines(tag, slave);
804 tag = write_sprite(tag, slave, id, -1);
806 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
807 swf_SetU8(tag, 2); //flags: id
808 swf_SetU16(tag, depth);
814 if(!swf_isDefiningTag(rtag) && (flags&FLAGS_WRITENONDEFINES))
819 case ST_PLACEOBJECT2:
820 if(frame == slaveframe && !config.overlay)
822 case ST_REMOVEOBJECT:
823 /* place/removetags for the object we replaced
824 should be discarded, too, as the object to insert
827 if(spriteid>=0 && swf_GetPlaceID(rtag) == spriteid &&
828 !config.isframe && config.merge)
831 case ST_REMOVEOBJECT2:
835 msg("<debug> [master] write tag %02x (%d bytes in body)",
836 rtag->id, rtag->len);
837 tag = swf_InsertTag(tag, rtag->id);
838 write_changepos(tag, rtag, config.mastermovex, config.mastermovey, config.masterscalex, config.masterscaley, 1);
846 while(stag && stag->id!=ST_END)
848 if(tag_ok_for_slave(stag->id)) {
849 msg("<debug> [slave] write tag %02x (%d bytes in body), %.2f %.2f", rtag->id, rtag->len, config.movex /20.0, config.movey /20.0);
850 tag = swf_InsertTag(tag, stag->id);
851 write_changepos(tag, stag, config.movex, config.movey, config.scalex, config.scaley, 0);
855 if(!slavewritten && config.isframe && (flags&(FLAGS_WRITESLAVE|FLAGS_WRITESPRITE)))
858 msg("<warning> Frame %d doesn't exist in file. No substitution will occur",
861 msg("<warning> Frame \"%s\" doesn't exist in file. No substitution will occur",
864 tag = swf_InsertTag(tag, ST_END);
868 void adjustheader(SWF*swf)
871 swf->frameRate = config.framerate;
872 if(config.hassizex) {
873 swf->movieSize.xmax =
874 swf->movieSize.xmin + config.sizex;
876 if(config.hassizey) {
877 swf->movieSize.ymax =
878 swf->movieSize.ymin + config.sizey;
880 if(config.flashversion)
881 swf->fileVersion = config.flashversion;
884 void catcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
891 msg("<fatal> Can't combine --cat and --frame");
894 if(config.flashversion)
895 master->fileVersion = config.flashversion;
897 tag = master->firstTag;
900 if(swf_isDefiningTag(tag)) {
901 int defineid = swf_GetDefineID(tag);
902 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
903 masterbitmap[defineid] = 1;
908 swf_Relocate(slave, masterbitmap);
909 jpeg_assert(master, slave);
911 memcpy(newswf, master, sizeof(SWF));
912 adjustheader(newswf);
914 tag = newswf->firstTag = swf_InsertTag(0, ST_REFLEX); // to be removed later
916 depths = malloc(65536);
918 msg("<fatal> Couldn't allocate %d bytes of memory", 65536);
921 memset(depths, 0, 65536);
922 mtag = master->firstTag;
923 while(mtag && mtag->id!=ST_END)
927 msg("<debug> [master] write tag %02x (%d bytes in body)",
928 mtag->id, mtag->len);
930 case ST_PLACEOBJECT2:
932 case ST_PLACEOBJECT: {
933 depth = swf_GetDepth(mtag);
937 case ST_REMOVEOBJECT: {
938 depth = swf_GetDepth(mtag);
942 case ST_REMOVEOBJECT2: {
943 depth = swf_GetDepth(mtag);
948 tag = swf_InsertTag(tag, mtag->id);
949 swf_SetBlock(tag, mtag->data, mtag->len);
959 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
964 stag = slave->firstTag;
965 while(stag && stag->id!=ST_END)
967 msg("<debug> [slave] write tag %02x (%d bytes in body)",
968 stag->id, stag->len);
969 tag = swf_InsertTag(tag, stag->id);
970 swf_SetBlock(tag, stag->data, stag->len);
973 tag = swf_InsertTag(tag, ST_END);
975 swf_DeleteTag(newswf, tag);
978 void normalcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
981 int replaceddefine = -1;
984 TAG * tag = master->firstTag;
986 memset(depthbitmap, 0, sizeof(depthbitmap));
991 int depth = swf_GetDepth(tag);
993 depthbitmap[depth] = 1;
995 if(swf_isDefiningTag(tag)) {
996 int defineid = swf_GetDefineID(tag);
997 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
998 masterbitmap[defineid] = 1;
1000 if (!slavename && defineid==slaveid) {
1002 spriteid = defineid;
1003 msg("<notice> Slave file attached to object %d.", defineid);
1006 } else if(tag->id == ST_EXPORTASSETS) {
1008 int num = swf_GetU16(tag);
1011 U16 id = swf_GetU16(tag);
1012 char*name = swf_GetString(tag);
1013 if(spriteid<0 && slavename && !strcmp(name,slavename)) {
1015 msg("<notice> Slave file attached to object %d exported as %s.", id, name);
1018 } else if(tag->id == ST_SYMBOLCLASS) {
1019 /* a symbolclass tag is like a define tag: it defines id 0000 */
1020 int num = swf_GetU16(tag);
1022 for(t=0;t<num;t++) {
1023 U16 id = swf_GetU16(tag);
1025 masterbitmap[id] = 1;
1029 } else if(tag->id == ST_PLACEOBJECT2) {
1030 char * name = swf_GetName(tag);
1031 int id = swf_GetPlaceID(tag);
1035 swf_GetPlaceObject(tag, &obj);
1036 swf_PlaceObjectFree(&obj);
1038 depthbitmap[obj.clipdepth] = 1;
1043 msg("<verbose> tagid %02x places object %d named \"%s\"", tag->id, id, name);
1045 msg("<verbose> tagid %02x places object %d (no name)", tag->id, id);
1047 if (name && slavename && !strcmp(name,slavename)) {
1050 msg("<notice> Slave file attached to named object %s (%d).", name, id);
1053 } else if(tag->id == ST_SHOWFRAME) {
1054 if(slaveframe>=0 && frame==slaveframe) {
1055 msg("<notice> Slave file attached to frame %d.", frame);
1058 } else if(tag->id == ST_FRAMELABEL) {
1059 char * name = tag->data;
1060 if(name && slavename && config.isframe && !strcmp(name, slavename)) {
1062 msg("<notice> Slave file attached to frame %d (%s).", frame, name);
1068 if (spriteid<0 && !config.isframe) {
1070 if(strcmp(slavename,"!!dummy!!")) {
1071 msg("<warning> Didn't find anything named %s in file. No substitutions will occur.", slavename);
1072 if(!strcmp(slavename, "swf")) {
1073 msg("<warning> (If you were trying to combine rfxview with a document, try replacing 'swf' with 'viewport'.");
1078 msg("<warning> Didn't find id %d in file. No substitutions will occur.", slaveid);
1079 spriteid = get_free_id(masterbitmap);
1082 swf_Relocate (slave, masterbitmap);
1085 swf_RelocateDepth (slave, depthbitmap);
1086 jpeg_assert(slave, master);
1089 replaceddefine = get_free_id(masterbitmap);
1093 memcpy(newswf, master, sizeof(SWF));
1094 adjustheader(newswf);
1096 newswf->firstTag = tag = swf_InsertTag(0, ST_REFLEX); // to be removed later
1098 if (config.antistream) {
1100 msg("<fatal> Can't combine --antistream and --merge");
1102 tag = write_sprite_defines(tag, slave);
1103 tag = write_sprite(tag, slave, spriteid, replaceddefine);
1104 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITEDEFINES);
1105 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITENONDEFINES);
1108 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1109 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESLAVE );
1111 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1112 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESPRITE );
1115 swf_DeleteTag(newswf, newswf->firstTag);
1118 void combine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
1120 slavename = slave_name;
1124 if(!master->fileVersion && slave)
1125 master->fileVersion = slave->fileVersion;
1127 master->fileAttributes |= slave->fileAttributes;
1129 swf_FoldAll(master);
1132 if(slavename[0] == '#')
1134 slaveid = atoi(&slavename[1]);
1140 if(slavename && slavename[0]!='#') {
1143 sscanf(slavename, "%d%n", &tmp, &len);
1144 if(len == strlen(slavename)) {
1145 /* if the name the slave should replace
1146 consists only of digits and the -f
1147 option is given, it probably is not
1148 a frame name but a frame number.
1156 slaveframe = slaveid;
1159 /* if id wasn't given as either #number or number,
1160 the name is a frame label. BTW: The user wouldn't necessarily have
1161 needed to supply the -f option in this case */
1165 msg("<debug> move x (%d)", config.movex);
1166 msg("<debug> move y (%d)", config.movey);
1167 msg("<debug> scale x (%f)", config.scalex);
1168 msg("<debug> scale y (%f)", config.scaley);
1169 msg("<debug> master move x (%d)", config.mastermovex);
1170 msg("<debug> master move y (%d)", config.mastermovey);
1171 msg("<debug> master scale x (%f)", config.masterscalex);
1172 msg("<debug> master scale y (%f)", config.masterscaley);
1173 msg("<debug> is frame (%d)", config.isframe);
1175 memset(masterbitmap, 0, sizeof(masterbitmap));
1178 return catcombine(master, slave_name, slave, newswf);
1180 return normalcombine(master, slave_name, slave, newswf);
1183 int main(int argn, char *argv[])
1192 config.antistream = 0;
1193 config.alloctest = 0;
1197 config.loglevel = 2;
1200 config.scalex = 1.0;
1201 config.scaley = 1.0;
1204 config.masterscalex = 1.0;
1205 config.masterscaley = 1.0;
1206 config.mastermovex = 0;
1207 config.mastermovey = 0;
1208 config.hassizex = 0;
1209 config.hassizey = 0;
1210 config.framerate = 0;
1216 processargs(argn, argv);
1217 initLog(0,-1,0,0,-1,config.loglevel);
1219 if(config.merge && config.cat) {
1220 msg("<error> Can't combine --cat and --merge");
1224 if(config.stack && config.cat) {
1225 msg("<error> Can't combine --cat and --stack");
1230 if(config.overlay) {
1231 msg("<error> Can't combine -l and -t");
1235 msg("<error> Can't combine -c and -t");
1238 msg("<verbose> (stacking) %d files found\n", numslaves);
1240 makestackmaster(&master);
1244 msg("<verbose> master entity %s (named \"%s\")\n", master_filename, master_name);
1245 fi = open(master_filename, O_RDONLY|O_BINARY);
1247 msg("<fatal> Failed to open %s\n", master_filename);
1250 ret = swf_ReadSWF(fi, &master);
1252 msg("<fatal> Failed to read from %s\n", master_filename);
1255 swf_RemoveJPEGTables(&master);
1256 removeCommonTags(&master);
1257 msg("<debug> Read %d bytes from masterfile\n", ret);
1261 for(t=0;t<numslaves;t++) {
1262 msg("<verbose> slave entity(%d) %s (%s \"%s\")\n", t+1, slave_filename[t],
1263 slave_isframe[t]?"frame":"object", slave_name[t]);
1270 msg("<error> --dummy (-d) implies there are zero slave objects. You supplied %d.", numslaves);
1274 slave_filename[0] = "!!dummy!!";
1275 slave_name[0] = "!!dummy!!";
1276 slave_isframe[0] = 0;
1279 if (config.alloctest)
1281 char*bitmap = malloc(sizeof(char)*65536);
1282 memset(bitmap, 0, 65536*sizeof(char));
1283 memset(bitmap, 1, 101*sizeof(char));
1284 swf_Relocate(&master, bitmap);
1287 // makestackmaster(&newswf);
1294 msg("<error> You must have at least two objects.");
1296 msg("<error> You must have at least one slave entity.");
1299 for(t = 0; t < numslaves; t++)
1301 config.movex = slave_movex[t];
1302 config.movey = slave_movey[t];
1303 config.scalex = slave_scalex[t];
1304 config.scaley = slave_scaley[t];
1305 config.isframe = slave_isframe[t];
1307 msg("<notice> Combine [%s]%s and [%s]%s", master_name, master_filename,
1308 slave_name[t], slave_filename[t]);
1312 fi = open(slave_filename[t], O_RDONLY|O_BINARY);
1314 msg("<fatal> Failed to open %s\n", slave_filename[t]);
1317 ret = swf_ReadSWF(fi, &slave);
1319 msg("<fatal> Failed to read from %s\n", slave_filename[t]);
1322 msg("<debug> Read %d bytes from slavefile\n", ret);
1324 swf_RemoveJPEGTables(&slave);
1325 removeCommonTags(&slave);
1329 memset(&slave, 0, sizeof(slave));
1330 slave.firstTag = swf_InsertTag(0, ST_END);
1331 slave.frameRate = 0;
1332 slave.fileVersion = 0;
1333 slave.frameCount = 0;
1336 combine(&master, slave_name[t], &slave, &newswf);
1339 if(config.dummy && !config.hassizex && !config.hassizey && !config.mastermovex && !config.mastermovey) {
1340 newswf.movieSize.xmin = newswf.movieSize.xmin*config.masterscalex;
1341 newswf.movieSize.ymin = newswf.movieSize.ymin*config.masterscaley;
1342 newswf.movieSize.xmax = newswf.movieSize.xmax*config.masterscalex;
1343 newswf.movieSize.ymax = newswf.movieSize.ymax*config.masterscaley;
1347 if(!newswf.fileVersion)
1348 newswf.fileVersion = 4;
1350 if(config.local_with_filesystem)
1351 newswf.fileAttributes &= ~FILEATTRIBUTE_USENETWORK;
1352 if(config.local_with_networking)
1353 newswf.fileAttributes |= FILEATTRIBUTE_USENETWORK;
1355 fi = open(outputname, O_BINARY|O_RDWR|O_TRUNC|O_CREAT, 0777);
1358 if(newswf.fileVersion < 6)
1359 newswf.fileVersion = 6;
1360 newswf.compressed = 1;
1361 swf_WriteSWF(fi, &newswf);
1363 newswf.compressed = -1; // don't compress
1364 swf_WriteSWF(fi, &newswf);