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;
60 struct config_t config;
62 char * master_filename = 0;
63 char * master_name = 0;
64 char * slave_filename[128];
65 char * slave_name[128];
68 float slave_scalex[128];
69 float slave_scaley[128];
70 char slave_isframe[128];
73 char * outputname = "output.swf";
75 int args_callback_option(char*name,char*val) {
81 else if(!strcmp(name,"l"))
86 else if (!strcmp(name, "o"))
91 else if (!strcmp(name, "v"))
96 else if (!strcmp(name, "a"))
101 else if (!strcmp(name, "A"))
103 config.alloctest = 1;
106 else if (!strcmp(name, "x"))
109 config.movex = (int)(x*20+0.5);
112 else if (!strcmp(name, "y"))
115 config.movey = (int)(y*20+0.5);
118 else if (!strcmp(name, "m"))
123 else if (!strcmp(name, "f"))
128 else if (!strcmp(name, "d"))
133 else if (!strcmp(name, "z"))
138 else if (!strcmp(name, "r"))
141 float rate = atof(val);
142 if ((rate < 0) ||(rate >= 256.0)) {
143 fprintf(stderr, "Error: You must specify a valid framerate between 1/256 and 255.\n");
146 config.framerate = (int)(rate*256);
149 else if (!strcmp(name, "X"))
151 config.sizex = atoi(val)*20;
155 else if (!strcmp(name, "Y"))
157 config.sizey = atoi(val)*20;
161 else if (!strcmp(name, "s"))
163 config.scalex = config.scaley = atoi(val)/100.0;
166 else if (!strcmp(name, "w"))
168 config.scalex = atoi(val)/100.0;
171 else if (!strcmp(name, "h"))
173 config.scaley = atoi(val)/100.0;
176 else if (!strcmp(name, "N"))
178 config.local_with_networking = 1;
181 else if (!strcmp(name, "L"))
183 config.local_with_filesystem = 1;
186 else if (!strcmp(name, "t") || !strcmp(name, "T"))
188 if(master_filename) {
189 fprintf(stderr, "error with arguments. Try --help.\n");
193 if(!strcmp(name,"T"))
195 master_filename = "__none__";
198 else if (!strcmp(name, "V"))
200 printf("swfcombine - part of %s %s\n", PACKAGE, VERSION);
205 fprintf(stderr, "Unknown option: -%s\n", name);
210 static struct options_t options[] = {
227 {"N", "local-with-networking"},
228 {"L", "local-with-filesystem"},
233 int args_callback_longoption(char*name,char*val) {
234 return args_long2shortoption(options, name, val);
237 int args_callback_command(char*name, char*val) {
238 char*myname = strdup(name);
240 filename = strchr(myname, '=');
245 // argument has no explicit name field. guess one from the file name
246 char*path = strrchr(myname, '/');
247 char*ext = strrchr(myname, '.');
248 if(!path) path = myname;
255 if(!master_filename) {
256 master_filename = filename;
257 master_name = myname;
258 config.mastermovex = config.movex;
259 config.mastermovey = config.movey;
260 config.masterscalex = config.scalex;
261 config.masterscaley = config.scaley;
262 config.movex = config.movey = 0;
263 config.scalex = config.scaley = 1.0;
265 msg("<verbose> slave entity %s (named \"%s\")\n", filename, myname);
267 slave_filename[numslaves] = filename;
268 slave_name[numslaves] = myname;
269 slave_movex[numslaves] = config.movex;
270 slave_movey[numslaves] = config.movey;
271 slave_scalex[numslaves] = config.scalex;
272 slave_scaley[numslaves] = config.scaley;
273 slave_isframe[numslaves] = config.isframe;
275 config.movex = config.movey = 0;
276 config.scalex = config.scaley = 1.0;
282 void args_callback_usage(char *name)
285 printf("Usage: %s [-rXYomlcv] [-f] masterfile [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
286 printf("OR: %s [-rXYomv] --stack[1] [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
287 printf("OR: %s [-rXYov] --cat [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
288 printf("OR: %s [-rXYomlcv] --dummy [-xys] [file]\n", name);
290 printf("-o , --output <outputfile> explicitly specify output file. (otherwise, output.swf will be used)\n");
291 printf("-t , --stack place each slave in a seperate frame (no master movie)\n");
292 printf("-T , --stack1 place each slave in the first frame (no master movie)\n");
293 printf("-m , --merge Don't store the slaves in Sprites/MovieClips\n");
294 printf("-a , --cat concatenate all slave files (no master movie)\n");
295 printf("-l , --overlay Don't remove any master objects, only overlay new objects\n");
296 printf("-c , --clip Clip the slave objects by the corresponding master objects\n");
297 printf("-v , --verbose Be verbose. Use more than one -v for greater effect \n");
298 printf("-d , --dummy Don't require slave objects (for changing movie attributes)\n");
299 printf("-f , --frame The following identifier is a frame or framelabel, not an id or objectname\n");
300 printf("-x , --movex <xpos> x Adjust position of slave by <xpos> pixels\n");
301 printf("-y , --movey <ypos> y Adjust position of slave by <ypos> pixels\n");
302 printf("-s , --scale <scale> Adjust size of slave by <scale> percent (e.g. 100% = original size)\n");
303 printf("-r , --rate <fps> Set movie framerate to <fps> (frames/sec)\n");
304 printf("-X , --width <width> Force movie bbox width to <width> (default: use master width (not with -t))\n");
305 printf("-Y , --height <height> Force movie bbox height to <height> (default: use master height (not with -t))\n");
306 printf("-N , --local-with-networking Make output file \"local-with-networking\"\n");
307 printf("-L , --local-with-filesystem Make output file \"local-with-filesystem\"\n");
308 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
312 void removeCommonTags(SWF * swf)
314 TAG*tag = swf->firstTag;
316 if(tag->id == ST_SCENEDESCRIPTION ||
317 tag->id == ST_FILEATTRIBUTES ||
318 tag->id == ST_REFLEX) {
319 tag = swf_DeleteTag(swf, tag);
326 static void makestackmaster(SWF*swf)
331 int fileversion = config.zlib?6:3;
333 U32 fileAttributes = 0;
336 memset(&box, 0, sizeof(box));
338 /* scan all slaves for bounding box */
339 for(t=numslaves-1;t>=0;t--)
343 int fi=open(slave_filename[t],O_RDONLY|O_BINARY);
345 if(fi<0 || swf_ReadSWF(fi, &head)<0) {
346 msg("<fatal> Couldn't open/read %s.", slave_filename[t]);
350 swf_RemoveJPEGTables(&head);
351 fileAttributes |= head.fileAttributes;
352 removeCommonTags(&head);
354 msg("<verbose> File %s has bounding box %d:%d:%d:%d\n",
356 head.movieSize.xmin, head.movieSize.ymin,
357 head.movieSize.xmax, head.movieSize.ymax);
361 if(tag->id == ST_SETBACKGROUNDCOLOR && tag->len>=3) {
362 rgb.r = tag->data[0];
363 rgb.g = tag->data[1];
364 rgb.b = tag->data[2];
368 frameRate = head.frameRate;
369 if(head.fileVersion > fileversion)
370 fileversion = head.fileVersion;
372 box = head.movieSize;
374 if(head.movieSize.xmin < box.xmin)
375 box.xmin = head.movieSize.xmin;
376 if(head.movieSize.ymin < box.ymin)
377 box.ymin = head.movieSize.ymin;
378 if(head.movieSize.xmax > box.xmax)
379 box.xmax = head.movieSize.xmax;
380 if(head.movieSize.ymax > box.ymax)
381 box.ymax = head.movieSize.ymax;
383 msg("<verbose> New master bounding box is %d:%d:%d:%d\n",
389 memset(swf, 0, sizeof(SWF));
390 swf->fileVersion = fileversion;
391 swf->movieSize = box;
392 swf->frameRate = frameRate;
393 swf->fileAttributes = fileAttributes;
395 swf->firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
397 swf_SetRGB(tag, &rgb);
399 for(t=0;t<numslaves;t++)
402 sprintf(buf, "Frame%02d", t);
403 slave_name[t] = strdup(buf);
405 tag = swf_InsertTag(tag, ST_DEFINESPRITE);
406 swf_SetU16(tag, t+1);
408 tag = swf_InsertTag(tag, ST_END);
409 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
410 swf_ObjectPlace(tag, t+1, 1+t,0,0, slave_name[t]);
412 if(!config.stack1 || t == numslaves-1) {
413 tag = swf_InsertTag(tag, ST_SHOWFRAME);
418 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
419 swf_SetU16(tag, 1+t);
422 tag = swf_InsertTag(tag, ST_END);
423 msg("<verbose> temporary SWF created");
426 static char* slavename = 0;
427 static int slaveid = -1;
428 static int slaveframe = -1;
429 static char masterbitmap[65536];
430 static char depthbitmap[65536];
432 #define FLAGS_WRITEDEFINES 1
433 #define FLAGS_WRITENONDEFINES 2
434 #define FLAGS_WRITESPRITE 4
435 #define FLAGS_WRITESLAVE 8
437 int get_free_id(char*bitmap)
448 void jpeg_assert(SWF*master, SWF*slave)
450 /* TODO: if there's a jpegtable found, store it
451 and handle it together with the flash file
454 /* check that master and slave don't have both
455 jpegtables (which would be fatal) */
457 TAG *mpos=0, *spos=0;
460 mtag = master->firstTag;
461 stag = slave->firstTag;
464 if(mtag->id == ST_JPEGTABLES)
470 if(stag->id == ST_JPEGTABLES)
476 if(spos->len == mpos->len &&
477 !memcmp(spos->data, mpos->data, mpos->len))
479 // ok, both have jpegtables, but they're identical.
480 // delete one and don't throw an error
481 swf_DeleteTag(slave, spos);
486 msg("<error> Master and slave have incompatible JPEGTABLES.");
490 TAG* write_sprite_defines(TAG*tag, SWF*sprite)
492 TAG*rtag = sprite->firstTag;
493 while(rtag && rtag->id!=ST_END) {
494 if(!swf_isAllowedSpriteTag(rtag)) {
495 msg("<debug> processing sprite tag %02x", tag->id);
496 if(swf_isDefiningTag(rtag))
498 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
500 tag = swf_InsertTag(tag, rtag->id);
501 swf_SetBlock(tag, rtag->data, rtag->len);
503 else if(swf_isPseudoDefiningTag(rtag))
505 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
507 tag = swf_InsertTag(tag, rtag->id);
508 swf_SetBlock(tag, rtag->data, rtag->len);
514 /* if we get here, jpeg_assert has already run,
515 ensuring this is the only one of it's kind,
516 so we may safely write it out */
517 tag = swf_InsertTag(tag, rtag->id);
518 swf_SetBlock(tag, rtag->data, rtag->len);
520 case ST_EXPORTASSETS:
521 msg("<debug> deliberately ignoring EXPORTASSETS tag");
523 case ST_ENABLEDEBUGGER:
524 msg("<debug> deliberately ignoring ENABLEDEBUGGER tag");
526 case ST_SETBACKGROUNDCOLOR:
527 msg("<debug> deliberately ignoring BACKGROUNDCOLOR tag");
530 msg("<debug> deliberately ignoring SHOWFRAME tag");
533 msg("<debug> deliberately ignoring REFLEX tag");
538 msg("<notice> found tag %d. This is a Generator template, isn't it?", rtag->id);
541 msg("<notice> funny tag: %d is neither defining nor sprite", rtag->id);
550 void changedepth(TAG*tag, int add)
552 if(tag->id == ST_PLACEOBJECT)
553 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
554 if(tag->id == ST_PLACEOBJECT2)
555 PUT16(&tag->data[1],GET16(&tag->data[1])+add);
556 if(tag->id == ST_REMOVEOBJECT)
557 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
558 if(tag->id == ST_REMOVEOBJECT2)
559 PUT16(&tag->data[0],GET16(&tag->data[0])+add);
560 if(tag->id == ST_PLACEOBJECT2) {
563 swf_SetTagPos(tag, 0);
564 flags = swf_GetU8(tag);
565 if(flags&2) swf_GetU16(tag); //id
566 if(flags&4) swf_GetMatrix(tag, 0);
567 if(flags&8) swf_GetCXForm(tag, 0,1);
568 if(flags&16) swf_GetU16(tag); //ratio
570 swf_ResetReadBits(tag);
571 printf("%d->%d\n", GET16(&tag->data[tag->pos]),
572 GET16(&tag->data[tag->pos])+add);
573 PUT16(&tag->data[tag->pos],GET16(&tag->data[tag->pos])+add);
575 msg("<warning> Depth relocation not fully working yet with clipdepths", tag->id);
579 void matrix_adjust(MATRIX*m, int movex, int movey, float scalex, float scaley, int scalepos)
581 m->sx = (int)(m->sx*scalex);
582 m->sy = (int)(m->sy*scaley);
583 m->r1 = (int)(m->r1*scalex);
584 m->r0 = (int)(m->r0*scaley);
593 void write_changepos(TAG*output, TAG*tag, int movex, int movey, float scalex, float scaley, int scalepos)
595 if(movex || movey || scalex != 1.0 || scaley != 1.0)
599 case ST_PLACEOBJECT2: {
602 swf_GetMatrix(0, &m);
606 flags = swf_GetU8(tag);
607 swf_SetU8(output, flags|4);
608 swf_SetU16(output, swf_GetU16(tag)); //depth
611 swf_SetU16(output, swf_GetU16(tag)); //id
615 swf_GetMatrix(tag, &m);
617 swf_GetMatrix(0, &m);
619 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
620 swf_SetMatrix(output, &m);
622 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
624 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
627 case ST_PLACEOBJECT: {
629 swf_SetU16(output, swf_GetU16(tag)); //id
630 swf_SetU16(output, swf_GetU16(tag)); //depth
632 swf_GetMatrix(tag, &m);
633 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
634 swf_SetMatrix(output, &m);
636 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
638 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
642 swf_SetBlock(output, tag->data, tag->len);
647 swf_SetBlock(output, tag->data, tag->len);
651 TAG* write_sprite(TAG*tag, SWF*sprite, int spriteid, int replaceddefine)
653 TAG* definespritetag;
657 definespritetag = tag = swf_InsertTag(tag, ST_DEFINESPRITE);
658 swf_SetU16(tag, spriteid);
659 swf_SetU16(tag, sprite->frameCount);
660 msg ("<notice> sprite id is %d", spriteid);
662 tmp = sprite->frameCount;
663 msg("<debug> %d frames to go",tmp);
666 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
667 swf_SetU8(tag, 2+64); //flags: character+clipdepth
668 swf_SetU16(tag, 0); //depth
669 swf_SetU16(tag, replaceddefine); //id
670 swf_SetU16(tag, 65535); //clipdepth
673 if(config.overlay && !config.isframe) {
674 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
675 swf_SetU8(tag, 2); //flags: character
676 swf_SetU16(tag, 1); //depth
677 swf_SetU16(tag, replaceddefine); //id
680 rtag = sprite->firstTag;
681 while(rtag && rtag->id!=ST_END)
683 if (swf_isAllowedSpriteTag(rtag)) {
685 msg("<debug> [sprite main] write tag %02x (%d bytes in body)",
686 rtag->id, rtag->len);
687 tag = swf_InsertTag(tag, rtag->id);
688 write_changepos(tag, rtag, config.movex, config.movey, config.scalex, config.scaley, 0);
690 if(config.clip || (config.overlay && !config.isframe))
691 changedepth(tag, +2);
693 if(tag->id == ST_SHOWFRAME)
696 msg("<debug> %d frames to go",tmp);
701 tag = swf_InsertTag(tag, ST_END);
705 static char tag_ok_for_slave(int id)
707 if(id == ST_SETBACKGROUNDCOLOR)
712 TAG* write_master(TAG*tag, SWF*master, SWF*slave, int spriteid, int replaceddefine, int flags)
717 int slavewritten = 0;
718 int deletedepth = -1;
720 TAG* rtag = master->firstTag;
721 TAG* stag = slave->firstTag;
723 while(rtag && rtag->id!=ST_END)
725 if(rtag->id == ST_SHOWFRAME && outputslave)
727 while(stag && stag->id!=ST_END) {
728 if(stag->id == ST_SHOWFRAME) {
733 if(tag_ok_for_slave(stag->id)) {
734 tag = swf_InsertTag(tag, stag->id);
735 write_changepos(tag, stag, config.movex, config.movey, config.scalex, config.scaley, 0);
740 if(rtag->id == ST_SHOWFRAME)
743 tag = swf_InsertTag(tag, ST_SHOWFRAME);
745 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
746 swf_SetU16(tag, deletedepth);
753 if(swf_isDefiningTag(rtag) && (flags&FLAGS_WRITEDEFINES))
755 msg("<debug> [master] write tag %02x (%d bytes in body)",
756 rtag->id, rtag->len);
757 if(swf_GetDefineID(rtag) == spriteid && !config.isframe)
761 tag = swf_InsertTag(tag, rtag->id);
762 swf_SetBlock(tag, rtag->data, rtag->len);
763 swf_SetDefineID(tag, replaceddefine);
765 /* don't write this tag */
766 msg("<verbose> replacing tag %d ID %d with sprite", rtag->id ,spriteid);
769 if(flags&FLAGS_WRITESPRITE)
771 msg("<debug> writing sprite defines");
772 tag = write_sprite_defines(tag, slave);
773 msg("<debug> writing sprite");
774 tag = write_sprite(tag, slave, spriteid, replaceddefine);
776 if(flags&FLAGS_WRITESLAVE)
778 msg("<debug> writing slave");
782 tag = swf_InsertTag(tag, rtag->id);
783 swf_SetBlock(tag, rtag->data, rtag->len);
786 if(frame == slaveframe) /* only happens with config.isframe: put slave at specific frame */
788 if(flags&FLAGS_WRITESLAVE) {
792 if((flags&FLAGS_WRITESPRITE) && !slavewritten)
794 int id = get_free_id(masterbitmap);
798 msg("<fatal> Can't combine --clip and --frame");
801 tag = write_sprite_defines(tag, slave);
802 tag = write_sprite(tag, slave, id, -1);
804 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
805 swf_SetU8(tag, 2); //flags: id
806 swf_SetU16(tag, depth);
812 if(!swf_isDefiningTag(rtag) && (flags&FLAGS_WRITENONDEFINES))
817 case ST_PLACEOBJECT2:
818 if(frame == slaveframe && !config.overlay)
820 case ST_REMOVEOBJECT:
821 /* place/removetags for the object we replaced
822 should be discarded, too, as the object to insert
825 if(spriteid>=0 && swf_GetPlaceID(rtag) == spriteid &&
826 !config.isframe && config.merge)
829 case ST_REMOVEOBJECT2:
833 msg("<debug> [master] write tag %02x (%d bytes in body)",
834 rtag->id, rtag->len);
835 tag = swf_InsertTag(tag, rtag->id);
836 write_changepos(tag, rtag, config.mastermovex, config.mastermovey, config.masterscalex, config.masterscaley, 1);
844 while(stag && stag->id!=ST_END)
846 if(tag_ok_for_slave(stag->id)) {
847 msg("<debug> [slave] write tag %02x (%d bytes in body), %.2f %.2f", rtag->id, rtag->len, config.movex /20.0, config.movey /20.0);
848 tag = swf_InsertTag(tag, stag->id);
849 write_changepos(tag, stag, config.movex, config.movey, config.scalex, config.scaley, 0);
853 if(!slavewritten && config.isframe && (flags&(FLAGS_WRITESLAVE|FLAGS_WRITESPRITE)))
856 msg("<warning> Frame %d doesn't exist in file. No substitution will occur",
859 msg("<warning> Frame \"%s\" doesn't exist in file. No substitution will occur",
862 tag = swf_InsertTag(tag, ST_END);
866 void adjustheader(SWF*swf)
869 swf->frameRate = config.framerate;
870 if(config.hassizex) {
871 swf->movieSize.xmax =
872 swf->movieSize.xmin + config.sizex;
874 if(config.hassizey) {
875 swf->movieSize.ymax =
876 swf->movieSize.ymin + config.sizey;
880 void catcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
887 msg("<fatal> Can't combine --cat and --frame");
891 tag = master->firstTag;
894 if(swf_isDefiningTag(tag)) {
895 int defineid = swf_GetDefineID(tag);
896 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
897 masterbitmap[defineid] = 1;
902 swf_Relocate(slave, masterbitmap);
903 jpeg_assert(master, slave);
905 memcpy(newswf, master, sizeof(SWF));
906 adjustheader(newswf);
908 tag = newswf->firstTag = swf_InsertTag(0, ST_REFLEX); // to be removed later
910 depths = malloc(65536);
912 msg("<fatal> Couldn't allocate %d bytes of memory", 65536);
915 memset(depths, 0, 65536);
916 mtag = master->firstTag;
917 while(mtag && mtag->id!=ST_END)
921 msg("<debug> [master] write tag %02x (%d bytes in body)",
922 mtag->id, mtag->len);
924 case ST_PLACEOBJECT2:
926 case ST_PLACEOBJECT: {
927 depth = swf_GetDepth(mtag);
931 case ST_REMOVEOBJECT: {
932 depth = swf_GetDepth(mtag);
936 case ST_REMOVEOBJECT2: {
937 depth = swf_GetDepth(mtag);
942 tag = swf_InsertTag(tag, mtag->id);
943 swf_SetBlock(tag, mtag->data, mtag->len);
953 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
958 stag = slave->firstTag;
959 while(stag && stag->id!=ST_END)
961 msg("<debug> [slave] write tag %02x (%d bytes in body)",
962 stag->id, stag->len);
963 tag = swf_InsertTag(tag, stag->id);
964 swf_SetBlock(tag, stag->data, stag->len);
967 tag = swf_InsertTag(tag, ST_END);
969 swf_DeleteTag(newswf, tag);
972 void normalcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
975 int replaceddefine = -1;
978 TAG * tag = master->firstTag;
980 memset(depthbitmap, 0, sizeof(depthbitmap));
985 int depth = swf_GetDepth(tag);
987 depthbitmap[depth] = 1;
989 if(swf_isDefiningTag(tag)) {
990 int defineid = swf_GetDefineID(tag);
991 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
992 masterbitmap[defineid] = 1;
994 if (!slavename && defineid==slaveid) {
997 msg("<notice> Slave file attached to object %d.", defineid);
1000 } else if(tag->id == ST_PLACEOBJECT2) {
1001 char * name = swf_GetName(tag);
1002 int id = swf_GetPlaceID(tag);
1006 swf_GetPlaceObject(tag, &obj);
1007 swf_PlaceObjectFree(&obj);
1009 depthbitmap[obj.clipdepth] = 1;
1014 msg("<verbose> tagid %02x places object %d named \"%s\"", tag->id, id, name);
1016 msg("<verbose> tagid %02x places object %d (no name)", tag->id, id);
1018 if (name && slavename && !strcmp(name,slavename)) {
1021 msg("<notice> Slave file attached to named object %s (%d).", name, id);
1024 } else if(tag->id == ST_SHOWFRAME) {
1025 if(slaveframe>=0 && frame==slaveframe) {
1026 msg("<notice> Slave file attached to frame %d.", frame);
1029 } else if(tag->id == ST_FRAMELABEL) {
1030 char * name = tag->data;
1031 if(name && slavename && config.isframe && !strcmp(name, slavename)) {
1033 msg("<notice> Slave file attached to frame %d (%s).", frame, name);
1039 if (spriteid<0 && !config.isframe) {
1041 if(strcmp(slavename,"!!dummy!!")) {
1042 msg("<warning> Didn't find anything named %s in file. No substitutions will occur.", slavename);
1043 if(!strcmp(slavename, "swf")) {
1044 msg("<warning> (If you were trying to combine rfxview with a document, try replacing 'swf' with 'viewport'.");
1049 msg("<warning> Didn't find id %d in file. No substitutions will occur.", slaveid);
1050 spriteid = get_free_id(masterbitmap);
1053 swf_Relocate (slave, masterbitmap);
1055 swf_RelocateDepth (slave, depthbitmap);
1056 jpeg_assert(slave, master);
1059 replaceddefine = get_free_id(masterbitmap);
1063 memcpy(newswf, master, sizeof(SWF));
1064 adjustheader(newswf);
1066 newswf->firstTag = tag = swf_InsertTag(0, ST_REFLEX); // to be removed later
1068 if (config.antistream) {
1070 msg("<fatal> Can't combine --antistream and --merge");
1072 tag = write_sprite_defines(tag, slave);
1073 tag = write_sprite(tag, slave, spriteid, replaceddefine);
1074 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITEDEFINES);
1075 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITENONDEFINES);
1078 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1079 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESLAVE );
1081 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1082 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESPRITE );
1085 swf_DeleteTag(newswf, newswf->firstTag);
1088 void combine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
1090 slavename = slave_name;
1094 if(!master->fileVersion && slave)
1095 master->fileVersion = slave->fileVersion;
1097 master->fileAttributes |= slave->fileAttributes;
1099 swf_FoldAll(master);
1102 if(slavename[0] == '#')
1104 slaveid = atoi(&slavename[1]);
1110 if(slavename && slavename[0]!='#') {
1113 sscanf(slavename, "%d%n", &tmp, &len);
1114 if(len == strlen(slavename)) {
1115 /* if the name the slave should replace
1116 consists only of digits and the -f
1117 option is given, it probably is not
1118 a frame name but a frame number.
1126 slaveframe = slaveid;
1129 /* if id wasn't given as either #number or number,
1130 the name is a frame label. BTW: The user wouldn't necessarily have
1131 needed to supply the -f option in this case */
1135 msg("<debug> move x (%d)", config.movex);
1136 msg("<debug> move y (%d)", config.movey);
1137 msg("<debug> scale x (%f)", config.scalex);
1138 msg("<debug> scale y (%f)", config.scaley);
1139 msg("<debug> master move x (%d)", config.mastermovex);
1140 msg("<debug> master move y (%d)", config.mastermovey);
1141 msg("<debug> master scale x (%f)", config.masterscalex);
1142 msg("<debug> master scale y (%f)", config.masterscaley);
1143 msg("<debug> is frame (%d)", config.isframe);
1145 memset(masterbitmap, 0, sizeof(masterbitmap));
1148 return catcombine(master, slave_name, slave, newswf);
1150 return normalcombine(master, slave_name, slave, newswf);
1153 int main(int argn, char *argv[])
1162 config.antistream = 0;
1163 config.alloctest = 0;
1167 config.loglevel = 2;
1170 config.scalex = 1.0;
1171 config.scaley = 1.0;
1174 config.masterscalex = 1.0;
1175 config.masterscaley = 1.0;
1176 config.mastermovex = 0;
1177 config.mastermovey = 0;
1178 config.hassizex = 0;
1179 config.hassizey = 0;
1180 config.framerate = 0;
1186 processargs(argn, argv);
1187 initLog(0,-1,0,0,-1,config.loglevel);
1189 if(config.merge && config.cat) {
1190 msg("<error> Can't combine --cat and --merge");
1194 if(config.stack && config.cat) {
1195 msg("<error> Can't combine --cat and --stack");
1200 if(config.overlay) {
1201 msg("<error> Can't combine -l and -t");
1205 msg("<error> Can't combine -c and -t");
1208 msg("<verbose> (stacking) %d files found\n", numslaves);
1210 makestackmaster(&master);
1214 msg("<verbose> master entity %s (named \"%s\")\n", master_filename, master_name);
1215 fi = open(master_filename, O_RDONLY|O_BINARY);
1217 msg("<fatal> Failed to open %s\n", master_filename);
1220 ret = swf_ReadSWF(fi, &master);
1222 msg("<fatal> Failed to read from %s\n", master_filename);
1225 swf_RemoveJPEGTables(&master);
1226 removeCommonTags(&master);
1227 msg("<debug> Read %d bytes from masterfile\n", ret);
1231 for(t=0;t<numslaves;t++) {
1232 msg("<verbose> slave entity(%d) %s (%s \"%s\")\n", t+1, slave_filename[t],
1233 slave_isframe[t]?"frame":"object", slave_name[t]);
1240 msg("<error> --dummy (-d) implies there are zero slave objects. You supplied %d.", numslaves);
1244 slave_filename[0] = "!!dummy!!";
1245 slave_name[0] = "!!dummy!!";
1246 slave_isframe[0] = 0;
1249 if (config.alloctest)
1251 char*bitmap = malloc(sizeof(char)*65536);
1252 memset(bitmap, 0, 65536*sizeof(char));
1253 memset(bitmap, 1, 101*sizeof(char));
1254 swf_Relocate(&master, bitmap);
1257 // makestackmaster(&newswf);
1264 msg("<error> You must have at least two objects.");
1266 msg("<error> You must have at least one slave entity.");
1269 for(t = 0; t < numslaves; t++)
1271 config.movex = slave_movex[t];
1272 config.movey = slave_movey[t];
1273 config.scalex = slave_scalex[t];
1274 config.scaley = slave_scaley[t];
1275 config.isframe = slave_isframe[t];
1277 msg("<notice> Combine [%s]%s and [%s]%s", master_name, master_filename,
1278 slave_name[t], slave_filename[t]);
1282 fi = open(slave_filename[t], O_RDONLY|O_BINARY);
1284 msg("<fatal> Failed to open %s\n", slave_filename[t]);
1287 ret = swf_ReadSWF(fi, &slave);
1289 msg("<fatal> Failed to read from %s\n", slave_filename[t]);
1292 msg("<debug> Read %d bytes from slavefile\n", ret);
1294 swf_RemoveJPEGTables(&slave);
1295 removeCommonTags(&slave);
1299 memset(&slave, 0, sizeof(slave));
1300 slave.firstTag = swf_InsertTag(0, ST_END);
1301 slave.frameRate = 0;
1302 slave.fileVersion = 0;
1303 slave.frameCount = 0;
1306 combine(&master, slave_name[t], &slave, &newswf);
1309 if(config.dummy && !config.hassizex && !config.hassizey && !config.mastermovex && !config.mastermovey) {
1310 newswf.movieSize.xmin = newswf.movieSize.xmin*config.masterscalex;
1311 newswf.movieSize.ymin = newswf.movieSize.ymin*config.masterscaley;
1312 newswf.movieSize.xmax = newswf.movieSize.xmax*config.masterscalex;
1313 newswf.movieSize.ymax = newswf.movieSize.ymax*config.masterscaley;
1317 if(!newswf.fileVersion)
1318 newswf.fileVersion = 4;
1320 if(config.local_with_filesystem)
1321 newswf.fileAttributes &= ~FILEATTRIBUTE_USENETWORK;
1322 if(config.local_with_networking)
1323 newswf.fileAttributes |= FILEATTRIBUTE_USENETWORK;
1325 fi = open(outputname, O_BINARY|O_RDWR|O_TRUNC|O_CREAT, 0777);
1328 if(newswf.fileVersion < 6)
1329 newswf.fileVersion = 6;
1330 newswf.compressed = 1;
1331 swf_WriteSWF(fi, &newswf);
1333 newswf.compressed = -1; // don't compress
1334 swf_WriteSWF(fi, &newswf);