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"
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];
66 float slave_scalex[128];
67 float slave_scaley[128];
68 char slave_isframe[128];
71 char * outputname = "output.swf";
73 int args_callback_option(char*name,char*val) {
79 else if(!strcmp(name,"l"))
84 else if (!strcmp(name, "o"))
89 else if (!strcmp(name, "v"))
94 else if (!strcmp(name, "a"))
99 else if (!strcmp(name, "A"))
101 config.alloctest = 1;
104 else if (!strcmp(name, "x"))
107 config.movex = (int)(x*20+0.5);
110 else if (!strcmp(name, "y"))
113 config.movey = (int)(y*20+0.5);
116 else if (!strcmp(name, "m"))
121 else if (!strcmp(name, "f"))
126 else if (!strcmp(name, "d"))
131 else if (!strcmp(name, "z"))
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");
144 config.framerate = (int)(rate*256);
147 else if (!strcmp(name, "X"))
149 config.sizex = atoi(val)*20;
153 else if (!strcmp(name, "Y"))
155 config.sizey = atoi(val)*20;
159 else if (!strcmp(name, "s"))
161 config.scalex = config.scaley = atoi(val)/100.0;
164 else if (!strcmp(name, "t") || !strcmp(name, "T"))
166 if(master_filename) {
167 fprintf(stderr, "error with arguments. Try --help.\n");
171 if(!strcmp(name,"T"))
173 master_filename = "__none__";
176 else if (!strcmp(name, "V"))
178 printf("swfcombine - part of %s %s\n", PACKAGE, VERSION);
183 fprintf(stderr, "Unknown option: -%s\n", name);
188 static struct options_t options[] = {
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);
216 filename = strchr(myname, '=');
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;
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;
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;
251 config.movex = config.movey = 0;
252 config.scalex = config.scaley = 1.0;
258 void args_callback_usage(char *name)
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);
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");
286 static void makestackmaster(SWF*swf)
291 int fileversion = config.zlib?6:3;
295 memset(&box, 0, sizeof(box));
297 /* scan all slaves for bounding box */
298 for(t=numslaves-1;t>=0;t--)
302 int fi=open(slave_filename[t],O_RDONLY|O_BINARY);
304 if(fi<0 || swf_ReadSWF(fi, &head)<0) {
305 msg("<fatal> Couldn't open/read %s.", slave_filename[t]);
309 msg("<verbose> File %s has bounding box %d:%d:%d:%d\n",
311 head.movieSize.xmin, head.movieSize.ymin,
312 head.movieSize.xmax, head.movieSize.ymax);
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];
323 frameRate = head.frameRate;
324 if(head.fileVersion > fileversion)
325 fileversion = head.fileVersion;
327 box = head.movieSize;
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",
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);
351 swf_SetRGB(tag, &rgb);
353 for(t=0;t<numslaves;t++)
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);
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);
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)
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
408 /* check that master and slave don't have both
409 jpegtables (which would be fatal) */
411 TAG *mpos=0, *spos=0;
414 mtag = master->firstTag;
415 stag = slave->firstTag;
418 if(mtag->id == ST_JPEGTABLES)
424 if(stag->id == ST_JPEGTABLES)
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
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)",
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)",
461 tag = swf_InsertTag(tag, rtag->id);
462 swf_SetBlock(tag, rtag->data, rtag->len);
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);
474 case ST_EXPORTASSETS:
475 msg("<debug> deliberately ignoring EXPORTASSETS tag");
477 case ST_ENABLEDEBUGGER:
478 msg("<debug> deliberately ignoring ENABLEDEBUGGER tag");
480 case ST_SETBACKGROUNDCOLOR:
481 msg("<debug> deliberately ignoring BACKGROUNDCOLOR tag");
484 msg("<debug> deliberately ignoring SHOWFRAME tag");
487 msg("<debug> deliberately ignoring REFLEX tag");
492 msg("<notice> found tag %d. This is a Generator template, isn't it?", rtag->id);
495 msg("<notice> funny tag: %d is neither defining nor sprite", rtag->id);
504 void changedepth(TAG*tag, int add)
506 if(tag->id == ST_PLACEOBJECT)
507 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
508 if(tag->id == ST_PLACEOBJECT2)
509 PUT16(&tag->data[1],GET16(&tag->data[1])+add);
510 if(tag->id == ST_REMOVEOBJECT)
511 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
512 if(tag->id == ST_REMOVEOBJECT2)
513 PUT16(&tag->data[0],GET16(&tag->data[0])+add);
514 if(tag->id == ST_PLACEOBJECT2) {
517 swf_SetTagPos(tag, 0);
518 flags = swf_GetU8(tag);
519 if(flags&2) swf_GetU16(tag); //id
520 if(flags&4) swf_GetMatrix(tag, 0);
521 if(flags&8) swf_GetCXForm(tag, 0,1);
522 if(flags&16) swf_GetU16(tag); //ratio
524 swf_ResetReadBits(tag);
525 printf("%d->%d\n", GET16(&tag->data[tag->pos]),
526 GET16(&tag->data[tag->pos])+add);
527 PUT16(&tag->data[tag->pos],GET16(&tag->data[tag->pos])+add);
529 msg("<warning> Depth relocation not fully working yet with clipdepths", tag->id);
533 void matrix_adjust(MATRIX*m, int movex, int movey, float scalex, float scaley, int scalepos)
535 m->sx = (int)(m->sx*scalex);
536 m->sy = (int)(m->sy*scaley);
537 m->r1 = (int)(m->r1*scalex);
538 m->r0 = (int)(m->r0*scaley);
547 void write_changepos(TAG*output, TAG*tag, int movex, int movey, float scalex, float scaley, int scalepos)
549 if(movex || movey || scalex != 1.0 || scaley != 1.0)
553 case ST_PLACEOBJECT2: {
556 swf_GetMatrix(0, &m);
560 flags = swf_GetU8(tag);
561 swf_SetU8(output, flags|4);
562 swf_SetU16(output, swf_GetU16(tag)); //depth
565 swf_SetU16(output, swf_GetU16(tag)); //id
569 swf_GetMatrix(tag, &m);
571 swf_GetMatrix(0, &m);
573 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
574 swf_SetMatrix(output, &m);
576 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
578 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
581 case ST_PLACEOBJECT: {
583 swf_SetU16(output, swf_GetU16(tag)); //id
584 swf_SetU16(output, swf_GetU16(tag)); //depth
586 swf_GetMatrix(tag, &m);
587 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
588 swf_SetMatrix(output, &m);
590 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
592 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
596 swf_SetBlock(output, tag->data, tag->len);
601 swf_SetBlock(output, tag->data, tag->len);
605 TAG* write_sprite(TAG*tag, SWF*sprite, int spriteid, int replaceddefine)
607 TAG* definespritetag;
611 definespritetag = tag = swf_InsertTag(tag, ST_DEFINESPRITE);
612 swf_SetU16(tag, spriteid);
613 swf_SetU16(tag, sprite->frameCount);
614 msg ("<notice> sprite id is %d", spriteid);
616 tmp = sprite->frameCount;
617 msg("<debug> %d frames to go",tmp);
620 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
621 swf_SetU8(tag, 2+64); //flags: character+clipdepth
622 swf_SetU16(tag, 0); //depth
623 swf_SetU16(tag, replaceddefine); //id
624 swf_SetU16(tag, 65535); //clipdepth
627 if(config.overlay && !config.isframe) {
628 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
629 swf_SetU8(tag, 2); //flags: character
630 swf_SetU16(tag, 1); //depth
631 swf_SetU16(tag, replaceddefine); //id
634 rtag = sprite->firstTag;
635 while(rtag && rtag->id!=ST_END)
637 if (swf_isAllowedSpriteTag(rtag)) {
639 msg("<debug> [sprite main] write tag %02x (%d bytes in body)",
640 rtag->id, rtag->len);
641 tag = swf_InsertTag(tag, rtag->id);
642 write_changepos(tag, rtag, config.movex, config.movey, config.scalex, config.scaley, 0);
644 if(config.clip || (config.overlay && !config.isframe))
645 changedepth(tag, +2);
647 if(tag->id == ST_SHOWFRAME)
650 msg("<debug> %d frames to go",tmp);
655 tag = swf_InsertTag(tag, ST_END);
659 static char tag_ok_for_slave(int id)
661 if(id == ST_SETBACKGROUNDCOLOR)
666 TAG* write_master(TAG*tag, SWF*master, SWF*slave, int spriteid, int replaceddefine, int flags)
671 int slavewritten = 0;
673 TAG* rtag = master->firstTag;
674 TAG* stag = slave->firstTag;
676 while(rtag && rtag->id!=ST_END)
678 if(rtag->id == ST_SHOWFRAME && outputslave)
680 while(stag && stag->id!=ST_END) {
681 if(stag->id == ST_SHOWFRAME) {
686 if(tag_ok_for_slave(stag->id)) {
687 tag = swf_InsertTag(tag, stag->id);
688 swf_SetBlock(tag, stag->data, stag->len);
693 if(rtag->id == ST_SHOWFRAME)
698 if(swf_isDefiningTag(rtag) && (flags&FLAGS_WRITEDEFINES))
700 msg("<debug> [master] write tag %02x (%d bytes in body)",
701 rtag->id, rtag->len);
702 if(swf_GetDefineID(rtag) == spriteid && !config.isframe)
706 tag = swf_InsertTag(tag, rtag->id);
707 swf_SetBlock(tag, rtag->data, rtag->len);
708 swf_SetDefineID(tag, replaceddefine);
710 /* don't write this tag */
711 msg("<verbose> replacing tag %d id %d with sprite", rtag->id
715 if(flags&FLAGS_WRITESPRITE)
717 tag = write_sprite_defines(tag, slave);
718 tag = write_sprite(tag, slave, spriteid, replaceddefine);
720 if(flags&FLAGS_WRITESLAVE)
725 tag = swf_InsertTag(tag, rtag->id);
726 swf_SetBlock(tag, rtag->data, rtag->len);
729 if(frame == slaveframe)
731 if(flags&FLAGS_WRITESLAVE) {
735 if((flags&FLAGS_WRITESPRITE) && !slavewritten)
737 int id = get_free_id(masterbitmap);
740 msg("<fatal> Can't combine --clip and --frame");
743 tag = write_sprite_defines(tag, slave);
744 tag = write_sprite(tag, slave, id, -1);
746 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
747 swf_SetU8(tag, 2); //flags: id
748 swf_SetU16(tag, depth);
754 if(!swf_isDefiningTag(rtag) && (flags&FLAGS_WRITENONDEFINES))
759 case ST_PLACEOBJECT2:
760 if(frame == slaveframe && !config.overlay)
762 case ST_REMOVEOBJECT:
763 /* place/removetags for the object we replaced
764 should be discarded, too, as the object to insert
767 if(spriteid>=0 && swf_GetPlaceID(rtag) == spriteid &&
768 !config.isframe && config.merge)
771 case ST_REMOVEOBJECT2:
775 msg("<debug> [master] write tag %02x (%d bytes in body)",
776 rtag->id, rtag->len);
777 tag = swf_InsertTag(tag, rtag->id);
778 write_changepos(tag, rtag, config.mastermovex, config.mastermovey, config.masterscalex, config.masterscaley, 1);
785 while(stag && stag->id!=ST_END)
787 if(tag_ok_for_slave(stag->id)) {
788 tag = swf_InsertTag(tag, stag->id);
789 swf_SetBlock(tag, stag->data, stag->len);
793 if(!slavewritten && config.isframe && (flags&(FLAGS_WRITESLAVE|FLAGS_WRITESPRITE)))
796 msg("<warning> Frame %d doesn't exist in file. No substitution will occur",
799 msg("<warning> Frame \"%s\" doesn't exist in file. No substitution will occur",
802 tag = swf_InsertTag(tag, ST_END);
806 void adjustheader(SWF*swf)
809 swf->frameRate = config.framerate;
810 if(config.hassizex) {
811 swf->movieSize.xmax =
812 swf->movieSize.xmin + config.sizex;
814 if(config.hassizey) {
815 swf->movieSize.ymax =
816 swf->movieSize.ymin + config.sizey;
820 void catcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
827 msg("<fatal> Can't combine --cat and --frame");
831 tag = master->firstTag;
834 if(swf_isDefiningTag(tag)) {
835 int defineid = swf_GetDefineID(tag);
836 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
837 masterbitmap[defineid] = 1;
842 swf_Relocate(slave, masterbitmap);
843 jpeg_assert(master, slave);
845 memcpy(newswf, master, sizeof(SWF));
846 adjustheader(newswf);
848 tag = newswf->firstTag = swf_InsertTag(0, ST_REFLEX); // to be removed later
850 depths = malloc(65536);
852 msg("<fatal> Couldn't allocate %d bytes of memory", 65536);
855 memset(depths, 0, 65536);
856 mtag = master->firstTag;
857 while(mtag && mtag->id!=ST_END)
861 msg("<debug> [master] write tag %02x (%d bytes in body)",
862 mtag->id, mtag->len);
864 case ST_PLACEOBJECT2:
866 case ST_PLACEOBJECT: {
867 depth = swf_GetDepth(mtag);
871 case ST_REMOVEOBJECT: {
872 depth = swf_GetDepth(mtag);
876 case ST_REMOVEOBJECT2: {
877 depth = swf_GetDepth(mtag);
882 tag = swf_InsertTag(tag, mtag->id);
883 swf_SetBlock(tag, mtag->data, mtag->len);
893 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
898 stag = slave->firstTag;
899 while(stag && stag->id!=ST_END)
901 msg("<debug> [slave] write tag %02x (%d bytes in body)",
902 stag->id, stag->len);
903 tag = swf_InsertTag(tag, stag->id);
904 swf_SetBlock(tag, stag->data, stag->len);
907 tag = swf_InsertTag(tag, ST_END);
909 tag = newswf->firstTag;
910 newswf->firstTag = newswf->firstTag->next; //remove temporary tag
914 void normalcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
917 int replaceddefine = -1;
920 TAG * tag = master->firstTag;
922 memset(depthbitmap, 0, sizeof(depthbitmap));
927 int depth = swf_GetDepth(tag);
929 depthbitmap[depth] = 1;
931 if(swf_isDefiningTag(tag)) {
932 int defineid = swf_GetDefineID(tag);
933 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
934 masterbitmap[defineid] = 1;
936 if (!slavename && defineid==slaveid) {
939 msg("<notice> Slave file attached to object %d.", defineid);
942 } else if(tag->id == ST_PLACEOBJECT2) {
943 char * name = swf_GetName(tag);
944 int id = swf_GetPlaceID(tag);
947 msg("<verbose> tagid %02x places object %d named \"%s\"", tag->id, id, name);
949 msg("<verbose> tagid %02x places object %d (no name)", tag->id, id);
951 if (name && slavename && !strcmp(name,slavename)) {
954 msg("<notice> Slave file attached to named object %s (%d).", name, id);
957 } else if(tag->id == ST_SHOWFRAME) {
958 if(slaveframe>=0 && frame==slaveframe) {
959 msg("<notice> Slave file attached to frame %d.", frame);
962 } else if(tag->id == ST_FRAMELABEL) {
963 char * name = tag->data;
964 if(name && slavename && config.isframe && !strcmp(name, slavename)) {
966 msg("<notice> Slave file attached to frame %d (%s).", frame, name);
972 if (spriteid<0 && !config.isframe) {
974 if(strcmp(slavename,"!!dummy!!"))
975 msg("<warning> Didn't find anything named %s in file. No substitutions will occur.", slavename);
978 msg("<warning> Didn't find id %d in file. No substitutions will occur.", slaveid);
979 spriteid = get_free_id(masterbitmap);
982 swf_Relocate (slave, masterbitmap);
984 swf_RelocateDepth (slave, depthbitmap);
985 jpeg_assert(slave, master);
988 replaceddefine = get_free_id(masterbitmap);
992 memcpy(newswf, master, sizeof(SWF));
993 adjustheader(newswf);
995 newswf->firstTag = tag = swf_InsertTag(0, ST_REFLEX); // to be removed later
997 if (config.antistream) {
999 msg("<fatal> Can't combine --antistream and --merge");
1001 tag = write_sprite_defines(tag, slave);
1002 tag = write_sprite(tag, slave, spriteid, replaceddefine);
1003 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITEDEFINES);
1004 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITENONDEFINES);
1007 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1008 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESLAVE );
1010 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1011 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESPRITE );
1014 tag = newswf->firstTag;
1015 newswf->firstTag = newswf->firstTag->next; //remove temporary tag
1019 void combine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
1021 slavename = slave_name;
1025 swf_FoldAll(master);
1028 if(slavename[0] == '#')
1030 slaveid = atoi(&slavename[1]);
1037 if(slavename && slavename[0]!='#' && (sscanf(slavename, "%d", &tmp) ==
1038 strlen(slavename))) {
1039 /* if the name the slave should replace
1040 consists only of digits and the -f
1041 option is given, it probably is not
1042 a frame name but a frame number.
1049 slaveframe = slaveid;
1052 /* if id wasn't given as either #number or number,
1053 the name is a frame label. BTW: The user wouldn't necessarily have
1054 needed to supply the -f option in this case */
1058 msg("<debug> move x (%d)", config.movex);
1059 msg("<debug> move y (%d)", config.movey);
1060 msg("<debug> scale x (%f)", config.scalex);
1061 msg("<debug> scale y (%f)", config.scaley);
1062 msg("<debug> master move x (%d)", config.mastermovex);
1063 msg("<debug> master move y (%d)", config.mastermovey);
1064 msg("<debug> master scale x (%f)", config.masterscalex);
1065 msg("<debug> master scale y (%f)", config.masterscaley);
1066 msg("<debug> is frame (%d)", config.isframe);
1068 memset(masterbitmap, 0, sizeof(masterbitmap));
1071 return catcombine(master, slave_name, slave, newswf);
1073 return normalcombine(master, slave_name, slave, newswf);
1076 int main(int argn, char *argv[])
1085 config.antistream = 0;
1086 config.alloctest = 0;
1090 config.loglevel = 2;
1093 config.scalex = 1.0;
1094 config.scaley = 1.0;
1097 config.masterscalex = 1.0;
1098 config.masterscaley = 1.0;
1099 config.mastermovex = 0;
1100 config.mastermovey = 0;
1101 config.hassizex = 0;
1102 config.hassizey = 0;
1103 config.framerate = 0;
1109 processargs(argn, argv);
1110 initLog(0,-1,0,0,-1,config.loglevel);
1112 if(config.merge && config.cat) {
1113 msg("<error> Can't combine --cat and --merge");
1118 if(config.overlay) {
1119 msg("<error> Can't combine -l and -t");
1123 msg("<error> Can't combine -c and -t");
1126 msg("<verbose> (stacking) %d files found\n", numslaves);
1128 makestackmaster(&master);
1132 msg("<verbose> master entity %s (named \"%s\")\n", master_filename, master_name);
1133 fi = open(master_filename, O_RDONLY|O_BINARY);
1135 msg("<fatal> Failed to open %s\n", master_filename);
1138 ret = swf_ReadSWF(fi, &master);
1140 msg("<fatal> Failed to read from %s\n", master_filename);
1143 msg("<debug> Read %d bytes from masterfile\n", ret);
1147 for(t=0;t<numslaves;t++) {
1148 msg("<verbose> slave entity(%d) %s (%s \"%s\")\n", t+1, slave_filename[t],
1149 slave_isframe[t]?"frame":"object", slave_name[t]);
1156 msg("<error> --dummy (-d) implies there are zero slave objects. You supplied %d.", numslaves);
1160 slave_filename[0] = "!!dummy!!";
1161 slave_name[0] = "!!dummy!!";
1162 slave_isframe[0] = 0;
1165 if (config.alloctest)
1167 char*bitmap = malloc(sizeof(char)*65536);
1168 memset(bitmap, 0, 65536*sizeof(char));
1169 memset(bitmap, 1, 101*sizeof(char));
1170 swf_Relocate(&master, bitmap);
1173 // makestackmaster(&newswf);
1180 msg("<error> You must have at least two objects.");
1182 msg("<error> You must have at least one slave entity.");
1185 for(t = 0; t < numslaves; t++)
1187 config.movex = slave_movex[t];
1188 config.movey = slave_movey[t];
1189 config.scalex = slave_scalex[t];
1190 config.scaley = slave_scaley[t];
1191 config.isframe = slave_isframe[t];
1193 msg("<notice> Combine [%s]%s and [%s]%s", master_name, master_filename,
1194 slave_name[t], slave_filename[t]);
1198 fi = open(slave_filename[t], O_RDONLY|O_BINARY);
1200 msg("<fatal> Failed to open %s\n", slave_filename[t]);
1203 ret = swf_ReadSWF(fi, &slave);
1205 msg("<fatal> Failed to read from %s\n", slave_filename[t]);
1208 msg("<debug> Read %d bytes from slavefile\n", ret);
1213 memset(&slave, 0, sizeof(slave));
1214 slave.firstTag = swf_InsertTag(0, ST_END);
1215 slave.frameRate = 0;
1216 slave.fileVersion = 4;
1217 slave.frameCount = 0;
1220 combine(&master, slave_name[t], &slave, &newswf);
1225 fi = open(outputname, O_BINARY|O_RDWR|O_TRUNC|O_CREAT, 0777);
1228 if(newswf.fileVersion < 6)
1229 newswf.fileVersion = 6;
1230 swf_WriteSWC(fi, &newswf);
1232 newswf.compressed = 0;
1233 swf_WriteSWF(fi, &newswf);