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 < 0) ||(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, "w"))
166 config.scalex = atoi(val)/100.0;
169 else if (!strcmp(name, "h"))
171 config.scaley = atoi(val)/100.0;
174 else if (!strcmp(name, "t") || !strcmp(name, "T"))
176 if(master_filename) {
177 fprintf(stderr, "error with arguments. Try --help.\n");
181 if(!strcmp(name,"T"))
183 master_filename = "__none__";
186 else if (!strcmp(name, "V"))
188 printf("swfcombine - part of %s %s\n", PACKAGE, VERSION);
193 fprintf(stderr, "Unknown option: -%s\n", name);
198 static struct options_t options[] = {
219 int args_callback_longoption(char*name,char*val) {
220 return args_long2shortoption(options, name, val);
223 int args_callback_command(char*name, char*val) {
224 char*myname = strdup(name);
226 filename = strchr(myname, '=');
231 // argument has no explicit name field. guess one from the file name
232 char*path = strrchr(myname, '/');
233 char*ext = strrchr(myname, '.');
234 if(!path) path = myname;
241 if(!master_filename) {
242 master_filename = filename;
243 master_name = myname;
244 config.mastermovex = config.movex;
245 config.mastermovey = config.movey;
246 config.masterscalex = config.scalex;
247 config.masterscaley = config.scaley;
248 config.movex = config.movey = 0;
249 config.scalex = config.scaley = 1.0;
251 msg("<verbose> slave entity %s (named \"%s\")\n", filename, myname);
253 slave_filename[numslaves] = filename;
254 slave_name[numslaves] = myname;
255 slave_movex[numslaves] = config.movex;
256 slave_movey[numslaves] = config.movey;
257 slave_scalex[numslaves] = config.scalex;
258 slave_scaley[numslaves] = config.scaley;
259 slave_isframe[numslaves] = config.isframe;
261 config.movex = config.movey = 0;
262 config.scalex = config.scaley = 1.0;
268 void args_callback_usage(char *name)
271 printf("Usage: %s [-rXYomlcv] [-f] masterfile [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
272 printf("OR: %s [-rXYomv] --stack[1] [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
273 printf("OR: %s [-rXYov] --cat [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
274 printf("OR: %s [-rXYomlcv] --dummy [-xys] [file]\n", name);
276 printf("-o , --output <outputfile> explicitly specify output file. (otherwise, output.swf will be used)\n");
277 printf("-t , --stack place each slave in a seperate frame (no master movie)\n");
278 printf("-T , --stack1 place each slave in the first frame (no master movie)\n");
279 printf("-m , --merge Don't store the slaves in Sprites/MovieClips\n");
280 printf("-a , --cat concatenate all slave files (no master movie)\n");
281 printf("-l , --overlay Don't remove any master objects, only overlay new objects\n");
282 printf("-c , --clip Clip the slave objects by the corresponding master objects\n");
283 printf("-v , --verbose Be verbose. Use more than one -v for greater effect \n");
284 printf("-d , --dummy Don't require slave objects (for changing movie attributes)\n");
285 printf("-f , --frame The following identifier is a frame or framelabel, not an id or objectname\n");
286 printf("-x , --movex <xpos> x Adjust position of slave by <xpos> pixels\n");
287 printf("-y , --movey <ypos> y Adjust position of slave by <ypos> pixels\n");
288 printf("-s , --scale <scale> Adjust size of slave by <scale> percent (e.g. 100%% = original size)\n");
289 printf("-r , --rate <fps> Set movie framerate to <fps> (frames/sec)\n");
290 printf("-X , --width <width> Force movie bbox width to <width> (default: use master width (not with -t))\n");
291 printf("-Y , --height <height> Force movie bbox height to <height> (default: use master height (not with -t))\n");
292 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
296 static void makestackmaster(SWF*swf)
301 int fileversion = config.zlib?6:3;
305 memset(&box, 0, sizeof(box));
307 /* scan all slaves for bounding box */
308 for(t=numslaves-1;t>=0;t--)
312 int fi=open(slave_filename[t],O_RDONLY|O_BINARY);
314 if(fi<0 || swf_ReadSWF(fi, &head)<0) {
315 msg("<fatal> Couldn't open/read %s.", slave_filename[t]);
319 swf_RemoveJPEGTables(&head);
320 msg("<verbose> File %s has bounding box %d:%d:%d:%d\n",
322 head.movieSize.xmin, head.movieSize.ymin,
323 head.movieSize.xmax, head.movieSize.ymax);
327 if(tag->id == ST_SETBACKGROUNDCOLOR && tag->len>=3) {
328 rgb.r = tag->data[0];
329 rgb.g = tag->data[1];
330 rgb.b = tag->data[2];
334 frameRate = head.frameRate;
335 if(head.fileVersion > fileversion)
336 fileversion = head.fileVersion;
338 box = head.movieSize;
340 if(head.movieSize.xmin < box.xmin)
341 box.xmin = head.movieSize.xmin;
342 if(head.movieSize.ymin < box.ymin)
343 box.ymin = head.movieSize.ymin;
344 if(head.movieSize.xmax > box.xmax)
345 box.xmax = head.movieSize.xmax;
346 if(head.movieSize.ymax > box.ymax)
347 box.ymax = head.movieSize.ymax;
349 msg("<verbose> New master bounding box is %d:%d:%d:%d\n",
355 memset(swf, 0, sizeof(SWF));
356 swf->fileVersion = fileversion;
357 swf->movieSize = box;
358 swf->frameRate = frameRate;
360 swf->firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
362 swf_SetRGB(tag, &rgb);
364 for(t=0;t<numslaves;t++)
367 sprintf(buf, "Frame%02d", t);
368 slave_name[t] = strdup(buf);
370 tag = swf_InsertTag(tag, ST_DEFINESPRITE);
371 swf_SetU16(tag, t+1);
373 tag = swf_InsertTag(tag, ST_END);
374 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
375 swf_ObjectPlace(tag, t+1, 1+t,0,0, slave_name[t]);
377 if(!config.stack1 || t == numslaves-1) {
378 tag = swf_InsertTag(tag, ST_SHOWFRAME);
383 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
384 swf_SetU16(tag, 1+t);
387 tag = swf_InsertTag(tag, ST_END);
388 msg("<verbose> temporary SWF created");
391 static char* slavename = 0;
392 static int slaveid = -1;
393 static int slaveframe = -1;
394 static char masterbitmap[65536];
395 static char depthbitmap[65536];
397 #define FLAGS_WRITEDEFINES 1
398 #define FLAGS_WRITENONDEFINES 2
399 #define FLAGS_WRITESPRITE 4
400 #define FLAGS_WRITESLAVE 8
402 int get_free_id(char*bitmap)
413 void jpeg_assert(SWF*master, SWF*slave)
415 /* TODO: if there's a jpegtable found, store it
416 and handle it together with the flash file
419 /* check that master and slave don't have both
420 jpegtables (which would be fatal) */
422 TAG *mpos=0, *spos=0;
425 mtag = master->firstTag;
426 stag = slave->firstTag;
429 if(mtag->id == ST_JPEGTABLES)
435 if(stag->id == ST_JPEGTABLES)
441 if(spos->len == mpos->len &&
442 !memcmp(spos->data, mpos->data, mpos->len))
444 // ok, both have jpegtables, but they're identical.
445 // delete one and don't throw an error
451 msg("<error> Master and slave have incompatible JPEGTABLES.");
455 TAG* write_sprite_defines(TAG*tag, SWF*sprite)
457 TAG*rtag = sprite->firstTag;
458 while(rtag && rtag->id!=ST_END) {
459 if(!swf_isAllowedSpriteTag(rtag)) {
460 msg("<debug> processing sprite tag %02x", tag->id);
461 if(swf_isDefiningTag(rtag))
463 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
465 tag = swf_InsertTag(tag, rtag->id);
466 swf_SetBlock(tag, rtag->data, rtag->len);
468 else if(swf_isPseudoDefiningTag(rtag))
470 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
472 tag = swf_InsertTag(tag, rtag->id);
473 swf_SetBlock(tag, rtag->data, rtag->len);
479 /* if we get here, jpeg_assert has already run,
480 ensuring this is the only one of it's kind,
481 so we may safely write it out */
482 tag = swf_InsertTag(tag, rtag->id);
483 swf_SetBlock(tag, rtag->data, rtag->len);
485 case ST_EXPORTASSETS:
486 msg("<debug> deliberately ignoring EXPORTASSETS tag");
488 case ST_ENABLEDEBUGGER:
489 msg("<debug> deliberately ignoring ENABLEDEBUGGER tag");
491 case ST_SETBACKGROUNDCOLOR:
492 msg("<debug> deliberately ignoring BACKGROUNDCOLOR tag");
495 msg("<debug> deliberately ignoring SHOWFRAME tag");
498 msg("<debug> deliberately ignoring REFLEX tag");
503 msg("<notice> found tag %d. This is a Generator template, isn't it?", rtag->id);
506 msg("<notice> funny tag: %d is neither defining nor sprite", rtag->id);
515 void changedepth(TAG*tag, int add)
517 if(tag->id == ST_PLACEOBJECT)
518 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
519 if(tag->id == ST_PLACEOBJECT2)
520 PUT16(&tag->data[1],GET16(&tag->data[1])+add);
521 if(tag->id == ST_REMOVEOBJECT)
522 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
523 if(tag->id == ST_REMOVEOBJECT2)
524 PUT16(&tag->data[0],GET16(&tag->data[0])+add);
525 if(tag->id == ST_PLACEOBJECT2) {
528 swf_SetTagPos(tag, 0);
529 flags = swf_GetU8(tag);
530 if(flags&2) swf_GetU16(tag); //id
531 if(flags&4) swf_GetMatrix(tag, 0);
532 if(flags&8) swf_GetCXForm(tag, 0,1);
533 if(flags&16) swf_GetU16(tag); //ratio
535 swf_ResetReadBits(tag);
536 printf("%d->%d\n", GET16(&tag->data[tag->pos]),
537 GET16(&tag->data[tag->pos])+add);
538 PUT16(&tag->data[tag->pos],GET16(&tag->data[tag->pos])+add);
540 msg("<warning> Depth relocation not fully working yet with clipdepths", tag->id);
544 void matrix_adjust(MATRIX*m, int movex, int movey, float scalex, float scaley, int scalepos)
546 m->sx = (int)(m->sx*scalex);
547 m->sy = (int)(m->sy*scaley);
548 m->r1 = (int)(m->r1*scalex);
549 m->r0 = (int)(m->r0*scaley);
558 void write_changepos(TAG*output, TAG*tag, int movex, int movey, float scalex, float scaley, int scalepos)
560 if(movex || movey || scalex != 1.0 || scaley != 1.0)
564 case ST_PLACEOBJECT2: {
567 swf_GetMatrix(0, &m);
571 flags = swf_GetU8(tag);
572 swf_SetU8(output, flags|4);
573 swf_SetU16(output, swf_GetU16(tag)); //depth
576 swf_SetU16(output, swf_GetU16(tag)); //id
580 swf_GetMatrix(tag, &m);
582 swf_GetMatrix(0, &m);
584 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
585 swf_SetMatrix(output, &m);
587 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
589 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
592 case ST_PLACEOBJECT: {
594 swf_SetU16(output, swf_GetU16(tag)); //id
595 swf_SetU16(output, swf_GetU16(tag)); //depth
597 swf_GetMatrix(tag, &m);
598 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
599 swf_SetMatrix(output, &m);
601 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
603 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
607 swf_SetBlock(output, tag->data, tag->len);
612 swf_SetBlock(output, tag->data, tag->len);
616 TAG* write_sprite(TAG*tag, SWF*sprite, int spriteid, int replaceddefine)
618 TAG* definespritetag;
622 definespritetag = tag = swf_InsertTag(tag, ST_DEFINESPRITE);
623 swf_SetU16(tag, spriteid);
624 swf_SetU16(tag, sprite->frameCount);
625 msg ("<notice> sprite id is %d", spriteid);
627 tmp = sprite->frameCount;
628 msg("<debug> %d frames to go",tmp);
631 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
632 swf_SetU8(tag, 2+64); //flags: character+clipdepth
633 swf_SetU16(tag, 0); //depth
634 swf_SetU16(tag, replaceddefine); //id
635 swf_SetU16(tag, 65535); //clipdepth
638 if(config.overlay && !config.isframe) {
639 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
640 swf_SetU8(tag, 2); //flags: character
641 swf_SetU16(tag, 1); //depth
642 swf_SetU16(tag, replaceddefine); //id
645 rtag = sprite->firstTag;
646 while(rtag && rtag->id!=ST_END)
648 if (swf_isAllowedSpriteTag(rtag)) {
650 msg("<debug> [sprite main] write tag %02x (%d bytes in body)",
651 rtag->id, rtag->len);
652 tag = swf_InsertTag(tag, rtag->id);
653 write_changepos(tag, rtag, config.movex, config.movey, config.scalex, config.scaley, 0);
655 if(config.clip || (config.overlay && !config.isframe))
656 changedepth(tag, +2);
658 if(tag->id == ST_SHOWFRAME)
661 msg("<debug> %d frames to go",tmp);
666 tag = swf_InsertTag(tag, ST_END);
670 static char tag_ok_for_slave(int id)
672 if(id == ST_SETBACKGROUNDCOLOR)
677 TAG* write_master(TAG*tag, SWF*master, SWF*slave, int spriteid, int replaceddefine, int flags)
682 int slavewritten = 0;
683 int deletedepth = -1;
685 TAG* rtag = master->firstTag;
686 TAG* stag = slave->firstTag;
688 while(rtag && rtag->id!=ST_END)
690 if(rtag->id == ST_SHOWFRAME && outputslave)
692 while(stag && stag->id!=ST_END) {
693 if(stag->id == ST_SHOWFRAME) {
698 if(tag_ok_for_slave(stag->id)) {
699 tag = swf_InsertTag(tag, stag->id);
700 write_changepos(tag, stag, config.movex, config.movey, config.scalex, config.scaley, 0);
705 if(rtag->id == ST_SHOWFRAME)
708 tag = swf_InsertTag(tag, ST_SHOWFRAME);
710 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
711 swf_SetU16(tag, deletedepth);
718 if(swf_isDefiningTag(rtag) && (flags&FLAGS_WRITEDEFINES))
720 msg("<debug> [master] write tag %02x (%d bytes in body)",
721 rtag->id, rtag->len);
722 if(swf_GetDefineID(rtag) == spriteid && !config.isframe)
726 tag = swf_InsertTag(tag, rtag->id);
727 swf_SetBlock(tag, rtag->data, rtag->len);
728 swf_SetDefineID(tag, replaceddefine);
730 /* don't write this tag */
731 msg("<verbose> replacing tag %d ID %d with sprite", rtag->id ,spriteid);
734 if(flags&FLAGS_WRITESPRITE)
736 msg("<debug> writing sprite defines");
737 tag = write_sprite_defines(tag, slave);
738 msg("<debug> writing sprite");
739 tag = write_sprite(tag, slave, spriteid, replaceddefine);
741 if(flags&FLAGS_WRITESLAVE)
743 msg("<debug> writing slave");
747 tag = swf_InsertTag(tag, rtag->id);
748 swf_SetBlock(tag, rtag->data, rtag->len);
751 if(frame == slaveframe) /* only happens with config.isframe: put slave at specific frame */
753 if(flags&FLAGS_WRITESLAVE) {
757 if((flags&FLAGS_WRITESPRITE) && !slavewritten)
759 int id = get_free_id(masterbitmap);
763 msg("<fatal> Can't combine --clip and --frame");
766 tag = write_sprite_defines(tag, slave);
767 tag = write_sprite(tag, slave, id, -1);
769 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
770 swf_SetU8(tag, 2); //flags: id
771 swf_SetU16(tag, depth);
777 if(!swf_isDefiningTag(rtag) && (flags&FLAGS_WRITENONDEFINES))
782 case ST_PLACEOBJECT2:
783 if(frame == slaveframe && !config.overlay)
785 case ST_REMOVEOBJECT:
786 /* place/removetags for the object we replaced
787 should be discarded, too, as the object to insert
790 if(spriteid>=0 && swf_GetPlaceID(rtag) == spriteid &&
791 !config.isframe && config.merge)
794 case ST_REMOVEOBJECT2:
798 msg("<debug> [master] write tag %02x (%d bytes in body)",
799 rtag->id, rtag->len);
800 tag = swf_InsertTag(tag, rtag->id);
801 write_changepos(tag, rtag, config.mastermovex, config.mastermovey, config.masterscalex, config.masterscaley, 1);
809 while(stag && stag->id!=ST_END)
811 if(tag_ok_for_slave(stag->id)) {
812 msg("<debug> [slave] write tag %02x (%d bytes in body), %.2f %.2f", rtag->id, rtag->len, config.movex /20.0, config.movey /20.0);
813 tag = swf_InsertTag(tag, stag->id);
814 write_changepos(tag, stag, config.movex, config.movey, config.scalex, config.scaley, 0);
818 if(!slavewritten && config.isframe && (flags&(FLAGS_WRITESLAVE|FLAGS_WRITESPRITE)))
821 msg("<warning> Frame %d doesn't exist in file. No substitution will occur",
824 msg("<warning> Frame \"%s\" doesn't exist in file. No substitution will occur",
827 tag = swf_InsertTag(tag, ST_END);
831 void adjustheader(SWF*swf)
834 swf->frameRate = config.framerate;
835 if(config.hassizex) {
836 swf->movieSize.xmax =
837 swf->movieSize.xmin + config.sizex;
839 if(config.hassizey) {
840 swf->movieSize.ymax =
841 swf->movieSize.ymin + config.sizey;
845 void catcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
852 msg("<fatal> Can't combine --cat and --frame");
856 tag = master->firstTag;
859 if(swf_isDefiningTag(tag)) {
860 int defineid = swf_GetDefineID(tag);
861 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
862 masterbitmap[defineid] = 1;
867 swf_Relocate(slave, masterbitmap);
868 jpeg_assert(master, slave);
870 memcpy(newswf, master, sizeof(SWF));
871 adjustheader(newswf);
873 tag = newswf->firstTag = swf_InsertTag(0, ST_REFLEX); // to be removed later
875 depths = malloc(65536);
877 msg("<fatal> Couldn't allocate %d bytes of memory", 65536);
880 memset(depths, 0, 65536);
881 mtag = master->firstTag;
882 while(mtag && mtag->id!=ST_END)
886 msg("<debug> [master] write tag %02x (%d bytes in body)",
887 mtag->id, mtag->len);
889 case ST_PLACEOBJECT2:
891 case ST_PLACEOBJECT: {
892 depth = swf_GetDepth(mtag);
896 case ST_REMOVEOBJECT: {
897 depth = swf_GetDepth(mtag);
901 case ST_REMOVEOBJECT2: {
902 depth = swf_GetDepth(mtag);
907 tag = swf_InsertTag(tag, mtag->id);
908 swf_SetBlock(tag, mtag->data, mtag->len);
918 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
923 stag = slave->firstTag;
924 while(stag && stag->id!=ST_END)
926 msg("<debug> [slave] write tag %02x (%d bytes in body)",
927 stag->id, stag->len);
928 tag = swf_InsertTag(tag, stag->id);
929 swf_SetBlock(tag, stag->data, stag->len);
932 tag = swf_InsertTag(tag, ST_END);
934 tag = newswf->firstTag;
935 newswf->firstTag = newswf->firstTag->next; //remove temporary tag
939 void normalcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
942 int replaceddefine = -1;
945 TAG * tag = master->firstTag;
947 memset(depthbitmap, 0, sizeof(depthbitmap));
952 int depth = swf_GetDepth(tag);
954 depthbitmap[depth] = 1;
956 if(swf_isDefiningTag(tag)) {
957 int defineid = swf_GetDefineID(tag);
958 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
959 masterbitmap[defineid] = 1;
961 if (!slavename && defineid==slaveid) {
964 msg("<notice> Slave file attached to object %d.", defineid);
967 } else if(tag->id == ST_PLACEOBJECT2) {
968 char * name = swf_GetName(tag);
969 int id = swf_GetPlaceID(tag);
973 swf_GetPlaceObject(tag, &obj);
974 swf_PlaceObjectFree(&obj);
976 depthbitmap[obj.clipdepth] = 1;
981 msg("<verbose> tagid %02x places object %d named \"%s\"", tag->id, id, name);
983 msg("<verbose> tagid %02x places object %d (no name)", tag->id, id);
985 if (name && slavename && !strcmp(name,slavename)) {
988 msg("<notice> Slave file attached to named object %s (%d).", name, id);
991 } else if(tag->id == ST_SHOWFRAME) {
992 if(slaveframe>=0 && frame==slaveframe) {
993 msg("<notice> Slave file attached to frame %d.", frame);
996 } else if(tag->id == ST_FRAMELABEL) {
997 char * name = tag->data;
998 if(name && slavename && config.isframe && !strcmp(name, slavename)) {
1000 msg("<notice> Slave file attached to frame %d (%s).", frame, name);
1006 if (spriteid<0 && !config.isframe) {
1008 if(strcmp(slavename,"!!dummy!!"))
1009 msg("<warning> Didn't find anything named %s in file. No substitutions will occur.", slavename);
1012 msg("<warning> Didn't find id %d in file. No substitutions will occur.", slaveid);
1013 spriteid = get_free_id(masterbitmap);
1016 swf_Relocate (slave, masterbitmap);
1018 swf_RelocateDepth (slave, depthbitmap);
1019 jpeg_assert(slave, master);
1022 replaceddefine = get_free_id(masterbitmap);
1026 memcpy(newswf, master, sizeof(SWF));
1027 adjustheader(newswf);
1029 newswf->firstTag = tag = swf_InsertTag(0, ST_REFLEX); // to be removed later
1031 if (config.antistream) {
1033 msg("<fatal> Can't combine --antistream and --merge");
1035 tag = write_sprite_defines(tag, slave);
1036 tag = write_sprite(tag, slave, spriteid, replaceddefine);
1037 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITEDEFINES);
1038 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITENONDEFINES);
1041 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1042 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESLAVE );
1044 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1045 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESPRITE );
1048 tag = newswf->firstTag;
1049 newswf->firstTag = newswf->firstTag->next; //remove temporary tag
1053 void combine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
1055 slavename = slave_name;
1059 if(!master->fileVersion && slave)
1060 master->fileVersion = slave->fileVersion;
1062 swf_FoldAll(master);
1065 if(slavename[0] == '#')
1067 slaveid = atoi(&slavename[1]);
1073 if(slavename && slavename[0]!='#') {
1076 sscanf(slavename, "%d%n", &tmp, &len);
1077 if(len == strlen(slavename)) {
1078 /* if the name the slave should replace
1079 consists only of digits and the -f
1080 option is given, it probably is not
1081 a frame name but a frame number.
1089 slaveframe = slaveid;
1092 /* if id wasn't given as either #number or number,
1093 the name is a frame label. BTW: The user wouldn't necessarily have
1094 needed to supply the -f option in this case */
1098 msg("<debug> move x (%d)", config.movex);
1099 msg("<debug> move y (%d)", config.movey);
1100 msg("<debug> scale x (%f)", config.scalex);
1101 msg("<debug> scale y (%f)", config.scaley);
1102 msg("<debug> master move x (%d)", config.mastermovex);
1103 msg("<debug> master move y (%d)", config.mastermovey);
1104 msg("<debug> master scale x (%f)", config.masterscalex);
1105 msg("<debug> master scale y (%f)", config.masterscaley);
1106 msg("<debug> is frame (%d)", config.isframe);
1108 memset(masterbitmap, 0, sizeof(masterbitmap));
1111 return catcombine(master, slave_name, slave, newswf);
1113 return normalcombine(master, slave_name, slave, newswf);
1116 int main(int argn, char *argv[])
1125 config.antistream = 0;
1126 config.alloctest = 0;
1130 config.loglevel = 2;
1133 config.scalex = 1.0;
1134 config.scaley = 1.0;
1137 config.masterscalex = 1.0;
1138 config.masterscaley = 1.0;
1139 config.mastermovex = 0;
1140 config.mastermovey = 0;
1141 config.hassizex = 0;
1142 config.hassizey = 0;
1143 config.framerate = 0;
1149 processargs(argn, argv);
1150 initLog(0,-1,0,0,-1,config.loglevel);
1152 if(config.merge && config.cat) {
1153 msg("<error> Can't combine --cat and --merge");
1157 if(config.stack && config.cat) {
1158 msg("<error> Can't combine --cat and --stack");
1163 if(config.overlay) {
1164 msg("<error> Can't combine -l and -t");
1168 msg("<error> Can't combine -c and -t");
1171 msg("<verbose> (stacking) %d files found\n", numslaves);
1173 makestackmaster(&master);
1177 msg("<verbose> master entity %s (named \"%s\")\n", master_filename, master_name);
1178 fi = open(master_filename, O_RDONLY|O_BINARY);
1180 msg("<fatal> Failed to open %s\n", master_filename);
1183 ret = swf_ReadSWF(fi, &master);
1185 msg("<fatal> Failed to read from %s\n", master_filename);
1188 swf_RemoveJPEGTables(&master);
1189 msg("<debug> Read %d bytes from masterfile\n", ret);
1193 for(t=0;t<numslaves;t++) {
1194 msg("<verbose> slave entity(%d) %s (%s \"%s\")\n", t+1, slave_filename[t],
1195 slave_isframe[t]?"frame":"object", slave_name[t]);
1202 msg("<error> --dummy (-d) implies there are zero slave objects. You supplied %d.", numslaves);
1206 slave_filename[0] = "!!dummy!!";
1207 slave_name[0] = "!!dummy!!";
1208 slave_isframe[0] = 0;
1211 if (config.alloctest)
1213 char*bitmap = malloc(sizeof(char)*65536);
1214 memset(bitmap, 0, 65536*sizeof(char));
1215 memset(bitmap, 1, 101*sizeof(char));
1216 swf_Relocate(&master, bitmap);
1219 // makestackmaster(&newswf);
1226 msg("<error> You must have at least two objects.");
1228 msg("<error> You must have at least one slave entity.");
1231 for(t = 0; t < numslaves; t++)
1233 config.movex = slave_movex[t];
1234 config.movey = slave_movey[t];
1235 config.scalex = slave_scalex[t];
1236 config.scaley = slave_scaley[t];
1237 config.isframe = slave_isframe[t];
1239 msg("<notice> Combine [%s]%s and [%s]%s", master_name, master_filename,
1240 slave_name[t], slave_filename[t]);
1244 fi = open(slave_filename[t], O_RDONLY|O_BINARY);
1246 msg("<fatal> Failed to open %s\n", slave_filename[t]);
1249 ret = swf_ReadSWF(fi, &slave);
1251 msg("<fatal> Failed to read from %s\n", slave_filename[t]);
1254 msg("<debug> Read %d bytes from slavefile\n", ret);
1256 swf_RemoveJPEGTables(&slave);
1260 memset(&slave, 0, sizeof(slave));
1261 slave.firstTag = swf_InsertTag(0, ST_END);
1262 slave.frameRate = 0;
1263 slave.fileVersion = 0;
1264 slave.frameCount = 0;
1267 combine(&master, slave_name[t], &slave, &newswf);
1270 if(config.dummy && !config.hassizex && !config.hassizey && !config.mastermovex && !config.mastermovey) {
1271 newswf.movieSize.xmin = newswf.movieSize.xmin*config.masterscalex;
1272 newswf.movieSize.ymin = newswf.movieSize.ymin*config.masterscaley;
1273 newswf.movieSize.xmax = newswf.movieSize.xmax*config.masterscalex;
1274 newswf.movieSize.ymax = newswf.movieSize.ymax*config.masterscaley;
1278 if(!newswf.fileVersion)
1279 newswf.fileVersion = 4;
1281 fi = open(outputname, O_BINARY|O_RDWR|O_TRUNC|O_CREAT, 0777);
1284 if(newswf.fileVersion < 6)
1285 newswf.fileVersion = 6;
1286 newswf.compressed = 1;
1287 swf_WriteSWF(fi, &newswf);
1289 newswf.compressed = -1; // don't compress
1290 swf_WriteSWF(fi, &newswf);