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 void removeCommonTags(SWF * swf)
298 TAG*tag = swf->firstTag;
300 if(tag->id == ST_SCENEDESCRIPTION ||
301 tag->id == ST_FILEATTRIBUTES ||
302 tag->id == ST_REFLEX) {
303 tag = swf_DeleteTag(swf, tag);
310 static void makestackmaster(SWF*swf)
315 int fileversion = config.zlib?6:3;
317 U32 fileAttributes = 0;
320 memset(&box, 0, sizeof(box));
322 /* scan all slaves for bounding box */
323 for(t=numslaves-1;t>=0;t--)
327 int fi=open(slave_filename[t],O_RDONLY|O_BINARY);
329 if(fi<0 || swf_ReadSWF(fi, &head)<0) {
330 msg("<fatal> Couldn't open/read %s.", slave_filename[t]);
334 swf_RemoveJPEGTables(&head);
335 fileAttributes |= head.fileAttributes;
336 removeCommonTags(&head);
338 msg("<verbose> File %s has bounding box %d:%d:%d:%d\n",
340 head.movieSize.xmin, head.movieSize.ymin,
341 head.movieSize.xmax, head.movieSize.ymax);
345 if(tag->id == ST_SETBACKGROUNDCOLOR && tag->len>=3) {
346 rgb.r = tag->data[0];
347 rgb.g = tag->data[1];
348 rgb.b = tag->data[2];
352 frameRate = head.frameRate;
353 if(head.fileVersion > fileversion)
354 fileversion = head.fileVersion;
356 box = head.movieSize;
358 if(head.movieSize.xmin < box.xmin)
359 box.xmin = head.movieSize.xmin;
360 if(head.movieSize.ymin < box.ymin)
361 box.ymin = head.movieSize.ymin;
362 if(head.movieSize.xmax > box.xmax)
363 box.xmax = head.movieSize.xmax;
364 if(head.movieSize.ymax > box.ymax)
365 box.ymax = head.movieSize.ymax;
367 msg("<verbose> New master bounding box is %d:%d:%d:%d\n",
373 memset(swf, 0, sizeof(SWF));
374 swf->fileVersion = fileversion;
375 swf->movieSize = box;
376 swf->frameRate = frameRate;
377 swf->fileAttributes = fileAttributes;
379 swf->firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
381 swf_SetRGB(tag, &rgb);
383 for(t=0;t<numslaves;t++)
386 sprintf(buf, "Frame%02d", t);
387 slave_name[t] = strdup(buf);
389 tag = swf_InsertTag(tag, ST_DEFINESPRITE);
390 swf_SetU16(tag, t+1);
392 tag = swf_InsertTag(tag, ST_END);
393 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
394 swf_ObjectPlace(tag, t+1, 1+t,0,0, slave_name[t]);
396 if(!config.stack1 || t == numslaves-1) {
397 tag = swf_InsertTag(tag, ST_SHOWFRAME);
402 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
403 swf_SetU16(tag, 1+t);
406 tag = swf_InsertTag(tag, ST_END);
407 msg("<verbose> temporary SWF created");
410 static char* slavename = 0;
411 static int slaveid = -1;
412 static int slaveframe = -1;
413 static char masterbitmap[65536];
414 static char depthbitmap[65536];
416 #define FLAGS_WRITEDEFINES 1
417 #define FLAGS_WRITENONDEFINES 2
418 #define FLAGS_WRITESPRITE 4
419 #define FLAGS_WRITESLAVE 8
421 int get_free_id(char*bitmap)
432 void jpeg_assert(SWF*master, SWF*slave)
434 /* TODO: if there's a jpegtable found, store it
435 and handle it together with the flash file
438 /* check that master and slave don't have both
439 jpegtables (which would be fatal) */
441 TAG *mpos=0, *spos=0;
444 mtag = master->firstTag;
445 stag = slave->firstTag;
448 if(mtag->id == ST_JPEGTABLES)
454 if(stag->id == ST_JPEGTABLES)
460 if(spos->len == mpos->len &&
461 !memcmp(spos->data, mpos->data, mpos->len))
463 // ok, both have jpegtables, but they're identical.
464 // delete one and don't throw an error
465 swf_DeleteTag(slave, spos);
470 msg("<error> Master and slave have incompatible JPEGTABLES.");
474 TAG* write_sprite_defines(TAG*tag, SWF*sprite)
476 TAG*rtag = sprite->firstTag;
477 while(rtag && rtag->id!=ST_END) {
478 if(!swf_isAllowedSpriteTag(rtag)) {
479 msg("<debug> processing sprite tag %02x", tag->id);
480 if(swf_isDefiningTag(rtag))
482 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
484 tag = swf_InsertTag(tag, rtag->id);
485 swf_SetBlock(tag, rtag->data, rtag->len);
487 else if(swf_isPseudoDefiningTag(rtag))
489 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
491 tag = swf_InsertTag(tag, rtag->id);
492 swf_SetBlock(tag, rtag->data, rtag->len);
498 /* if we get here, jpeg_assert has already run,
499 ensuring this is the only one of it's kind,
500 so we may safely write it out */
501 tag = swf_InsertTag(tag, rtag->id);
502 swf_SetBlock(tag, rtag->data, rtag->len);
504 case ST_EXPORTASSETS:
505 msg("<debug> deliberately ignoring EXPORTASSETS tag");
507 case ST_ENABLEDEBUGGER:
508 msg("<debug> deliberately ignoring ENABLEDEBUGGER tag");
510 case ST_SETBACKGROUNDCOLOR:
511 msg("<debug> deliberately ignoring BACKGROUNDCOLOR tag");
514 msg("<debug> deliberately ignoring SHOWFRAME tag");
517 msg("<debug> deliberately ignoring REFLEX tag");
522 msg("<notice> found tag %d. This is a Generator template, isn't it?", rtag->id);
525 msg("<notice> funny tag: %d is neither defining nor sprite", rtag->id);
534 void changedepth(TAG*tag, int add)
536 if(tag->id == ST_PLACEOBJECT)
537 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
538 if(tag->id == ST_PLACEOBJECT2)
539 PUT16(&tag->data[1],GET16(&tag->data[1])+add);
540 if(tag->id == ST_REMOVEOBJECT)
541 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
542 if(tag->id == ST_REMOVEOBJECT2)
543 PUT16(&tag->data[0],GET16(&tag->data[0])+add);
544 if(tag->id == ST_PLACEOBJECT2) {
547 swf_SetTagPos(tag, 0);
548 flags = swf_GetU8(tag);
549 if(flags&2) swf_GetU16(tag); //id
550 if(flags&4) swf_GetMatrix(tag, 0);
551 if(flags&8) swf_GetCXForm(tag, 0,1);
552 if(flags&16) swf_GetU16(tag); //ratio
554 swf_ResetReadBits(tag);
555 printf("%d->%d\n", GET16(&tag->data[tag->pos]),
556 GET16(&tag->data[tag->pos])+add);
557 PUT16(&tag->data[tag->pos],GET16(&tag->data[tag->pos])+add);
559 msg("<warning> Depth relocation not fully working yet with clipdepths", tag->id);
563 void matrix_adjust(MATRIX*m, int movex, int movey, float scalex, float scaley, int scalepos)
565 m->sx = (int)(m->sx*scalex);
566 m->sy = (int)(m->sy*scaley);
567 m->r1 = (int)(m->r1*scalex);
568 m->r0 = (int)(m->r0*scaley);
577 void write_changepos(TAG*output, TAG*tag, int movex, int movey, float scalex, float scaley, int scalepos)
579 if(movex || movey || scalex != 1.0 || scaley != 1.0)
583 case ST_PLACEOBJECT2: {
586 swf_GetMatrix(0, &m);
590 flags = swf_GetU8(tag);
591 swf_SetU8(output, flags|4);
592 swf_SetU16(output, swf_GetU16(tag)); //depth
595 swf_SetU16(output, swf_GetU16(tag)); //id
599 swf_GetMatrix(tag, &m);
601 swf_GetMatrix(0, &m);
603 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
604 swf_SetMatrix(output, &m);
606 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
608 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
611 case ST_PLACEOBJECT: {
613 swf_SetU16(output, swf_GetU16(tag)); //id
614 swf_SetU16(output, swf_GetU16(tag)); //depth
616 swf_GetMatrix(tag, &m);
617 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
618 swf_SetMatrix(output, &m);
620 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
622 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
626 swf_SetBlock(output, tag->data, tag->len);
631 swf_SetBlock(output, tag->data, tag->len);
635 TAG* write_sprite(TAG*tag, SWF*sprite, int spriteid, int replaceddefine)
637 TAG* definespritetag;
641 definespritetag = tag = swf_InsertTag(tag, ST_DEFINESPRITE);
642 swf_SetU16(tag, spriteid);
643 swf_SetU16(tag, sprite->frameCount);
644 msg ("<notice> sprite id is %d", spriteid);
646 tmp = sprite->frameCount;
647 msg("<debug> %d frames to go",tmp);
650 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
651 swf_SetU8(tag, 2+64); //flags: character+clipdepth
652 swf_SetU16(tag, 0); //depth
653 swf_SetU16(tag, replaceddefine); //id
654 swf_SetU16(tag, 65535); //clipdepth
657 if(config.overlay && !config.isframe) {
658 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
659 swf_SetU8(tag, 2); //flags: character
660 swf_SetU16(tag, 1); //depth
661 swf_SetU16(tag, replaceddefine); //id
664 rtag = sprite->firstTag;
665 while(rtag && rtag->id!=ST_END)
667 if (swf_isAllowedSpriteTag(rtag)) {
669 msg("<debug> [sprite main] write tag %02x (%d bytes in body)",
670 rtag->id, rtag->len);
671 tag = swf_InsertTag(tag, rtag->id);
672 write_changepos(tag, rtag, config.movex, config.movey, config.scalex, config.scaley, 0);
674 if(config.clip || (config.overlay && !config.isframe))
675 changedepth(tag, +2);
677 if(tag->id == ST_SHOWFRAME)
680 msg("<debug> %d frames to go",tmp);
685 tag = swf_InsertTag(tag, ST_END);
689 static char tag_ok_for_slave(int id)
691 if(id == ST_SETBACKGROUNDCOLOR)
696 TAG* write_master(TAG*tag, SWF*master, SWF*slave, int spriteid, int replaceddefine, int flags)
701 int slavewritten = 0;
702 int deletedepth = -1;
704 TAG* rtag = master->firstTag;
705 TAG* stag = slave->firstTag;
707 while(rtag && rtag->id!=ST_END)
709 if(rtag->id == ST_SHOWFRAME && outputslave)
711 while(stag && stag->id!=ST_END) {
712 if(stag->id == ST_SHOWFRAME) {
717 if(tag_ok_for_slave(stag->id)) {
718 tag = swf_InsertTag(tag, stag->id);
719 write_changepos(tag, stag, config.movex, config.movey, config.scalex, config.scaley, 0);
724 if(rtag->id == ST_SHOWFRAME)
727 tag = swf_InsertTag(tag, ST_SHOWFRAME);
729 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
730 swf_SetU16(tag, deletedepth);
737 if(swf_isDefiningTag(rtag) && (flags&FLAGS_WRITEDEFINES))
739 msg("<debug> [master] write tag %02x (%d bytes in body)",
740 rtag->id, rtag->len);
741 if(swf_GetDefineID(rtag) == spriteid && !config.isframe)
745 tag = swf_InsertTag(tag, rtag->id);
746 swf_SetBlock(tag, rtag->data, rtag->len);
747 swf_SetDefineID(tag, replaceddefine);
749 /* don't write this tag */
750 msg("<verbose> replacing tag %d ID %d with sprite", rtag->id ,spriteid);
753 if(flags&FLAGS_WRITESPRITE)
755 msg("<debug> writing sprite defines");
756 tag = write_sprite_defines(tag, slave);
757 msg("<debug> writing sprite");
758 tag = write_sprite(tag, slave, spriteid, replaceddefine);
760 if(flags&FLAGS_WRITESLAVE)
762 msg("<debug> writing slave");
766 tag = swf_InsertTag(tag, rtag->id);
767 swf_SetBlock(tag, rtag->data, rtag->len);
770 if(frame == slaveframe) /* only happens with config.isframe: put slave at specific frame */
772 if(flags&FLAGS_WRITESLAVE) {
776 if((flags&FLAGS_WRITESPRITE) && !slavewritten)
778 int id = get_free_id(masterbitmap);
782 msg("<fatal> Can't combine --clip and --frame");
785 tag = write_sprite_defines(tag, slave);
786 tag = write_sprite(tag, slave, id, -1);
788 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
789 swf_SetU8(tag, 2); //flags: id
790 swf_SetU16(tag, depth);
796 if(!swf_isDefiningTag(rtag) && (flags&FLAGS_WRITENONDEFINES))
801 case ST_PLACEOBJECT2:
802 if(frame == slaveframe && !config.overlay)
804 case ST_REMOVEOBJECT:
805 /* place/removetags for the object we replaced
806 should be discarded, too, as the object to insert
809 if(spriteid>=0 && swf_GetPlaceID(rtag) == spriteid &&
810 !config.isframe && config.merge)
813 case ST_REMOVEOBJECT2:
817 msg("<debug> [master] write tag %02x (%d bytes in body)",
818 rtag->id, rtag->len);
819 tag = swf_InsertTag(tag, rtag->id);
820 write_changepos(tag, rtag, config.mastermovex, config.mastermovey, config.masterscalex, config.masterscaley, 1);
828 while(stag && stag->id!=ST_END)
830 if(tag_ok_for_slave(stag->id)) {
831 msg("<debug> [slave] write tag %02x (%d bytes in body), %.2f %.2f", rtag->id, rtag->len, config.movex /20.0, config.movey /20.0);
832 tag = swf_InsertTag(tag, stag->id);
833 write_changepos(tag, stag, config.movex, config.movey, config.scalex, config.scaley, 0);
837 if(!slavewritten && config.isframe && (flags&(FLAGS_WRITESLAVE|FLAGS_WRITESPRITE)))
840 msg("<warning> Frame %d doesn't exist in file. No substitution will occur",
843 msg("<warning> Frame \"%s\" doesn't exist in file. No substitution will occur",
846 tag = swf_InsertTag(tag, ST_END);
850 void adjustheader(SWF*swf)
853 swf->frameRate = config.framerate;
854 if(config.hassizex) {
855 swf->movieSize.xmax =
856 swf->movieSize.xmin + config.sizex;
858 if(config.hassizey) {
859 swf->movieSize.ymax =
860 swf->movieSize.ymin + config.sizey;
864 void catcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
871 msg("<fatal> Can't combine --cat and --frame");
875 tag = master->firstTag;
878 if(swf_isDefiningTag(tag)) {
879 int defineid = swf_GetDefineID(tag);
880 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
881 masterbitmap[defineid] = 1;
886 swf_Relocate(slave, masterbitmap);
887 jpeg_assert(master, slave);
889 memcpy(newswf, master, sizeof(SWF));
890 adjustheader(newswf);
892 tag = newswf->firstTag = swf_InsertTag(0, ST_REFLEX); // to be removed later
894 depths = malloc(65536);
896 msg("<fatal> Couldn't allocate %d bytes of memory", 65536);
899 memset(depths, 0, 65536);
900 mtag = master->firstTag;
901 while(mtag && mtag->id!=ST_END)
905 msg("<debug> [master] write tag %02x (%d bytes in body)",
906 mtag->id, mtag->len);
908 case ST_PLACEOBJECT2:
910 case ST_PLACEOBJECT: {
911 depth = swf_GetDepth(mtag);
915 case ST_REMOVEOBJECT: {
916 depth = swf_GetDepth(mtag);
920 case ST_REMOVEOBJECT2: {
921 depth = swf_GetDepth(mtag);
926 tag = swf_InsertTag(tag, mtag->id);
927 swf_SetBlock(tag, mtag->data, mtag->len);
937 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
942 stag = slave->firstTag;
943 while(stag && stag->id!=ST_END)
945 msg("<debug> [slave] write tag %02x (%d bytes in body)",
946 stag->id, stag->len);
947 tag = swf_InsertTag(tag, stag->id);
948 swf_SetBlock(tag, stag->data, stag->len);
951 tag = swf_InsertTag(tag, ST_END);
953 swf_DeleteTag(newswf, tag);
956 void normalcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
959 int replaceddefine = -1;
962 TAG * tag = master->firstTag;
964 memset(depthbitmap, 0, sizeof(depthbitmap));
969 int depth = swf_GetDepth(tag);
971 depthbitmap[depth] = 1;
973 if(swf_isDefiningTag(tag)) {
974 int defineid = swf_GetDefineID(tag);
975 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
976 masterbitmap[defineid] = 1;
978 if (!slavename && defineid==slaveid) {
981 msg("<notice> Slave file attached to object %d.", defineid);
984 } else if(tag->id == ST_PLACEOBJECT2) {
985 char * name = swf_GetName(tag);
986 int id = swf_GetPlaceID(tag);
990 swf_GetPlaceObject(tag, &obj);
991 swf_PlaceObjectFree(&obj);
993 depthbitmap[obj.clipdepth] = 1;
998 msg("<verbose> tagid %02x places object %d named \"%s\"", tag->id, id, name);
1000 msg("<verbose> tagid %02x places object %d (no name)", tag->id, id);
1002 if (name && slavename && !strcmp(name,slavename)) {
1005 msg("<notice> Slave file attached to named object %s (%d).", name, id);
1008 } else if(tag->id == ST_SHOWFRAME) {
1009 if(slaveframe>=0 && frame==slaveframe) {
1010 msg("<notice> Slave file attached to frame %d.", frame);
1013 } else if(tag->id == ST_FRAMELABEL) {
1014 char * name = tag->data;
1015 if(name && slavename && config.isframe && !strcmp(name, slavename)) {
1017 msg("<notice> Slave file attached to frame %d (%s).", frame, name);
1023 if (spriteid<0 && !config.isframe) {
1025 if(strcmp(slavename,"!!dummy!!")) {
1026 msg("<warning> Didn't find anything named %s in file. No substitutions will occur.", slavename);
1027 if(!strcmp(slavename, "swf")) {
1028 msg("<warning> (If you were trying to combine rfxview with a document, try replacing 'swf' with 'viewport'.");
1033 msg("<warning> Didn't find id %d in file. No substitutions will occur.", slaveid);
1034 spriteid = get_free_id(masterbitmap);
1037 swf_Relocate (slave, masterbitmap);
1039 swf_RelocateDepth (slave, depthbitmap);
1040 jpeg_assert(slave, master);
1043 replaceddefine = get_free_id(masterbitmap);
1047 memcpy(newswf, master, sizeof(SWF));
1048 adjustheader(newswf);
1050 newswf->firstTag = tag = swf_InsertTag(0, ST_REFLEX); // to be removed later
1052 if (config.antistream) {
1054 msg("<fatal> Can't combine --antistream and --merge");
1056 tag = write_sprite_defines(tag, slave);
1057 tag = write_sprite(tag, slave, spriteid, replaceddefine);
1058 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITEDEFINES);
1059 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITENONDEFINES);
1062 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1063 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESLAVE );
1065 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1066 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESPRITE );
1069 swf_DeleteTag(newswf, newswf->firstTag);
1072 void combine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
1074 slavename = slave_name;
1078 if(!master->fileVersion && slave)
1079 master->fileVersion = slave->fileVersion;
1081 master->fileAttributes |= slave->fileAttributes;
1083 swf_FoldAll(master);
1086 if(slavename[0] == '#')
1088 slaveid = atoi(&slavename[1]);
1094 if(slavename && slavename[0]!='#') {
1097 sscanf(slavename, "%d%n", &tmp, &len);
1098 if(len == strlen(slavename)) {
1099 /* if the name the slave should replace
1100 consists only of digits and the -f
1101 option is given, it probably is not
1102 a frame name but a frame number.
1110 slaveframe = slaveid;
1113 /* if id wasn't given as either #number or number,
1114 the name is a frame label. BTW: The user wouldn't necessarily have
1115 needed to supply the -f option in this case */
1119 msg("<debug> move x (%d)", config.movex);
1120 msg("<debug> move y (%d)", config.movey);
1121 msg("<debug> scale x (%f)", config.scalex);
1122 msg("<debug> scale y (%f)", config.scaley);
1123 msg("<debug> master move x (%d)", config.mastermovex);
1124 msg("<debug> master move y (%d)", config.mastermovey);
1125 msg("<debug> master scale x (%f)", config.masterscalex);
1126 msg("<debug> master scale y (%f)", config.masterscaley);
1127 msg("<debug> is frame (%d)", config.isframe);
1129 memset(masterbitmap, 0, sizeof(masterbitmap));
1132 return catcombine(master, slave_name, slave, newswf);
1134 return normalcombine(master, slave_name, slave, newswf);
1137 int main(int argn, char *argv[])
1146 config.antistream = 0;
1147 config.alloctest = 0;
1151 config.loglevel = 2;
1154 config.scalex = 1.0;
1155 config.scaley = 1.0;
1158 config.masterscalex = 1.0;
1159 config.masterscaley = 1.0;
1160 config.mastermovex = 0;
1161 config.mastermovey = 0;
1162 config.hassizex = 0;
1163 config.hassizey = 0;
1164 config.framerate = 0;
1170 processargs(argn, argv);
1171 initLog(0,-1,0,0,-1,config.loglevel);
1173 if(config.merge && config.cat) {
1174 msg("<error> Can't combine --cat and --merge");
1178 if(config.stack && config.cat) {
1179 msg("<error> Can't combine --cat and --stack");
1184 if(config.overlay) {
1185 msg("<error> Can't combine -l and -t");
1189 msg("<error> Can't combine -c and -t");
1192 msg("<verbose> (stacking) %d files found\n", numslaves);
1194 makestackmaster(&master);
1198 msg("<verbose> master entity %s (named \"%s\")\n", master_filename, master_name);
1199 fi = open(master_filename, O_RDONLY|O_BINARY);
1201 msg("<fatal> Failed to open %s\n", master_filename);
1204 ret = swf_ReadSWF(fi, &master);
1206 msg("<fatal> Failed to read from %s\n", master_filename);
1209 swf_RemoveJPEGTables(&master);
1210 removeCommonTags(&master);
1211 msg("<debug> Read %d bytes from masterfile\n", ret);
1215 for(t=0;t<numslaves;t++) {
1216 msg("<verbose> slave entity(%d) %s (%s \"%s\")\n", t+1, slave_filename[t],
1217 slave_isframe[t]?"frame":"object", slave_name[t]);
1224 msg("<error> --dummy (-d) implies there are zero slave objects. You supplied %d.", numslaves);
1228 slave_filename[0] = "!!dummy!!";
1229 slave_name[0] = "!!dummy!!";
1230 slave_isframe[0] = 0;
1233 if (config.alloctest)
1235 char*bitmap = malloc(sizeof(char)*65536);
1236 memset(bitmap, 0, 65536*sizeof(char));
1237 memset(bitmap, 1, 101*sizeof(char));
1238 swf_Relocate(&master, bitmap);
1241 // makestackmaster(&newswf);
1248 msg("<error> You must have at least two objects.");
1250 msg("<error> You must have at least one slave entity.");
1253 for(t = 0; t < numslaves; t++)
1255 config.movex = slave_movex[t];
1256 config.movey = slave_movey[t];
1257 config.scalex = slave_scalex[t];
1258 config.scaley = slave_scaley[t];
1259 config.isframe = slave_isframe[t];
1261 msg("<notice> Combine [%s]%s and [%s]%s", master_name, master_filename,
1262 slave_name[t], slave_filename[t]);
1266 fi = open(slave_filename[t], O_RDONLY|O_BINARY);
1268 msg("<fatal> Failed to open %s\n", slave_filename[t]);
1271 ret = swf_ReadSWF(fi, &slave);
1273 msg("<fatal> Failed to read from %s\n", slave_filename[t]);
1276 msg("<debug> Read %d bytes from slavefile\n", ret);
1278 swf_RemoveJPEGTables(&slave);
1279 removeCommonTags(&slave);
1283 memset(&slave, 0, sizeof(slave));
1284 slave.firstTag = swf_InsertTag(0, ST_END);
1285 slave.frameRate = 0;
1286 slave.fileVersion = 0;
1287 slave.frameCount = 0;
1290 combine(&master, slave_name[t], &slave, &newswf);
1293 if(config.dummy && !config.hassizex && !config.hassizey && !config.mastermovex && !config.mastermovey) {
1294 newswf.movieSize.xmin = newswf.movieSize.xmin*config.masterscalex;
1295 newswf.movieSize.ymin = newswf.movieSize.ymin*config.masterscaley;
1296 newswf.movieSize.xmax = newswf.movieSize.xmax*config.masterscalex;
1297 newswf.movieSize.ymax = newswf.movieSize.ymax*config.masterscaley;
1301 if(!newswf.fileVersion)
1302 newswf.fileVersion = 4;
1304 fi = open(outputname, O_BINARY|O_RDWR|O_TRUNC|O_CREAT, 0777);
1307 if(newswf.fileVersion < 6)
1308 newswf.fileVersion = 6;
1309 newswf.compressed = 1;
1310 swf_WriteSWF(fi, &newswf);
1312 newswf.compressed = -1; // don't compress
1313 swf_WriteSWF(fi, &newswf);