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;
45 char accelerated_blit;
63 struct config_t config;
65 char * master_filename = 0;
66 char * master_name = 0;
67 char * slave_filename[128];
68 char * slave_name[128];
71 float slave_scalex[128];
72 float slave_scaley[128];
73 char slave_isframe[128];
76 char * outputname = "output.swf";
78 int args_callback_option(char*name,char*val) {
84 else if(!strcmp(name,"l"))
89 else if (!strcmp(name, "o"))
94 else if (!strcmp(name, "v"))
99 else if (!strcmp(name, "a"))
104 else if (!strcmp(name, "A"))
106 config.alloctest = 1;
109 else if (!strcmp(name, "x"))
112 config.movex = (int)(x*20+0.5);
115 else if (!strcmp(name, "y"))
118 config.movey = (int)(y*20+0.5);
121 else if (!strcmp(name, "m"))
126 else if (!strcmp(name, "f"))
131 else if (!strcmp(name, "d"))
136 else if (!strcmp(name, "z"))
141 else if (!strcmp(name, "r"))
144 float rate = atof(val);
145 if ((rate < 0) ||(rate >= 256.0)) {
146 fprintf(stderr, "Error: You must specify a valid framerate between 1/256 and 255.\n");
149 config.framerate = (int)(rate*256);
152 else if (!strcmp(name, "X"))
154 config.sizex = atoi(val)*20;
158 else if (!strcmp(name, "Y"))
160 config.sizey = atoi(val)*20;
164 else if (!strcmp(name, "s"))
166 config.scalex = config.scaley = atoi(val)/100.0;
169 else if (!strcmp(name, "w"))
171 config.scalex = atoi(val)/100.0;
174 else if (!strcmp(name, "h"))
176 config.scaley = atoi(val)/100.0;
179 else if (!strcmp(name, "N"))
181 config.local_with_networking = 1;
184 else if (!strcmp(name, "L"))
186 config.local_with_filesystem = 1;
189 else if (!strcmp(name, "B"))
191 config.accelerated_blit = 1;
194 else if (!strcmp(name, "G"))
196 config.hardware_gpu = 1;
199 else if (!strcmp(name, "t") || !strcmp(name, "T"))
201 if(master_filename) {
202 fprintf(stderr, "error with arguments. Try --help.\n");
206 if(!strcmp(name,"T"))
208 master_filename = "__none__";
211 else if (!strcmp(name, "V"))
213 printf("swfcombine - part of %s %s\n", PACKAGE, VERSION);
218 fprintf(stderr, "Unknown option: -%s\n", name);
223 static struct options_t options[] = {
232 {"F", "flashversion"},
241 {"N", "local-with-networking"},
242 {"G", "hardware-gpu"},
243 {"B", "accelerated-blit"},
244 {"L", "local-with-filesystem"},
249 int args_callback_longoption(char*name,char*val) {
250 return args_long2shortoption(options, name, val);
253 int args_callback_command(char*name, char*val) {
254 char*myname = strdup(name);
256 filename = strchr(myname, '=');
261 // argument has no explicit name field. guess one from the file name
262 char*path = strrchr(myname, '/');
263 char*ext = strrchr(myname, '.');
264 if(!path) path = myname;
271 if(!master_filename) {
272 master_filename = filename;
273 master_name = myname;
274 config.mastermovex = config.movex;
275 config.mastermovey = config.movey;
276 config.masterscalex = config.scalex;
277 config.masterscaley = config.scaley;
278 config.movex = config.movey = 0;
279 config.scalex = config.scaley = 1.0;
281 msg("<verbose> slave entity %s (named \"%s\")\n", filename, myname);
283 slave_filename[numslaves] = filename;
284 slave_name[numslaves] = myname;
285 slave_movex[numslaves] = config.movex;
286 slave_movey[numslaves] = config.movey;
287 slave_scalex[numslaves] = config.scalex;
288 slave_scaley[numslaves] = config.scaley;
289 slave_isframe[numslaves] = config.isframe;
291 config.movex = config.movey = 0;
292 config.scalex = config.scaley = 1.0;
298 void args_callback_usage(char *name)
301 printf("Usage: %s [-rXYomlcv] [-f] masterfile [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
302 printf("OR: %s [-rXYomv] --stack[1] [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
303 printf("OR: %s [-rXYov] --cat [-xysf] [(name1|#id1)=]slavefile1 .. [-xysf] [(nameN|#idN)=]slavefileN\n", name);
304 printf("OR: %s [-rXYomlcv] --dummy [-xys] [file]\n", name);
306 printf("-o , --output <outputfile> explicitly specify output file. (otherwise, output.swf will be used)\n");
307 printf("-t , --stack place each slave in a seperate frame (no master movie)\n");
308 printf("-T , --stack1 place each slave in the first frame (no master movie)\n");
309 printf("-m , --merge Don't store the slaves in Sprites/MovieClips\n");
310 printf("-a , --cat concatenate all slave files (no master movie)\n");
311 printf("-l , --overlay Don't remove any master objects, only overlay new objects\n");
312 printf("-c , --clip Clip the slave objects by the corresponding master objects\n");
313 printf("-v , --verbose Be verbose. Use more than one -v for greater effect \n");
314 printf("-F , --flashversion Set the flash version of the output file.\n");
315 printf("-d , --dummy Don't require slave objects (for changing movie attributes)\n");
316 printf("-f , --frame The following identifier is a frame or framelabel, not an id or objectname\n");
317 printf("-x , --movex <xpos> x Adjust position of slave by <xpos> pixels\n");
318 printf("-y , --movey <ypos> y Adjust position of slave by <ypos> pixels\n");
319 printf("-s , --scale <scale> Adjust size of slave by <scale> percent (e.g. 100%% = original size)\n");
320 printf("-r , --rate <fps> Set movie framerate to <fps> (frames/sec)\n");
321 printf("-X , --width <width> Force movie bbox width to <width> (default: use master width (not with -t))\n");
322 printf("-Y , --height <height> Force movie bbox height to <height> (default: use master height (not with -t))\n");
323 printf("-N , --local-with-networking Make output file \"local-with-networking\"\n");
324 printf("-G , --hardware-gpu Set the \"use hardware gpu\" bit in the output file\n");
325 printf("-B , --accelerated-blit Set the \"use accelerated blit\" bit in the output file\n");
326 printf("-L , --local-with-filesystem Make output file \"local-with-filesystem\"\n");
327 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
331 void removeCommonTags(SWF * swf)
333 TAG*tag = swf->firstTag;
335 if(tag->id == ST_SCENEDESCRIPTION ||
336 tag->id == ST_FILEATTRIBUTES ||
337 tag->id == ST_REFLEX) {
338 tag = swf_DeleteTag(swf, tag);
345 static void makestackmaster(SWF*swf)
350 int fileversion = config.zlib?6:3;
352 U32 fileAttributes = 0;
355 memset(&box, 0, sizeof(box));
357 /* scan all slaves for bounding box */
358 for(t=numslaves-1;t>=0;t--)
362 int fi=open(slave_filename[t],O_RDONLY|O_BINARY);
364 if(fi<0 || swf_ReadSWF(fi, &head)<0) {
365 msg("<fatal> Couldn't open/read %s.", slave_filename[t]);
369 swf_RemoveJPEGTables(&head);
370 fileAttributes |= head.fileAttributes;
371 removeCommonTags(&head);
373 msg("<verbose> File %s has bounding box %d:%d:%d:%d\n",
375 head.movieSize.xmin, head.movieSize.ymin,
376 head.movieSize.xmax, head.movieSize.ymax);
380 if(tag->id == ST_SETBACKGROUNDCOLOR && tag->len>=3) {
381 rgb.r = tag->data[0];
382 rgb.g = tag->data[1];
383 rgb.b = tag->data[2];
387 frameRate = head.frameRate;
388 if(head.fileVersion > fileversion)
389 fileversion = head.fileVersion;
391 box = head.movieSize;
393 if(head.movieSize.xmin < box.xmin)
394 box.xmin = head.movieSize.xmin;
395 if(head.movieSize.ymin < box.ymin)
396 box.ymin = head.movieSize.ymin;
397 if(head.movieSize.xmax > box.xmax)
398 box.xmax = head.movieSize.xmax;
399 if(head.movieSize.ymax > box.ymax)
400 box.ymax = head.movieSize.ymax;
402 msg("<verbose> New master bounding box is %d:%d:%d:%d\n",
408 memset(swf, 0, sizeof(SWF));
409 swf->fileVersion = fileversion;
410 swf->movieSize = box;
411 swf->frameRate = frameRate;
412 swf->fileAttributes = fileAttributes;
414 swf->firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
416 swf_SetRGB(tag, &rgb);
418 for(t=0;t<numslaves;t++)
421 sprintf(buf, "Frame%02d", t);
422 slave_name[t] = strdup(buf);
424 tag = swf_InsertTag(tag, ST_DEFINESPRITE);
425 swf_SetU16(tag, t+1);
427 tag = swf_InsertTag(tag, ST_END);
428 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
429 swf_ObjectPlace(tag, t+1, 1+t,0,0, slave_name[t]);
431 if(!config.stack1 || t == numslaves-1) {
432 tag = swf_InsertTag(tag, ST_SHOWFRAME);
437 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
438 swf_SetU16(tag, 1+t);
441 tag = swf_InsertTag(tag, ST_END);
442 msg("<verbose> temporary SWF created");
445 static char* slavename = 0;
446 static int slaveid = -1;
447 static int slaveframe = -1;
448 static char masterbitmap[65536];
449 static char depthbitmap[65536];
451 #define FLAGS_WRITEDEFINES 1
452 #define FLAGS_WRITENONDEFINES 2
453 #define FLAGS_WRITESPRITE 4
454 #define FLAGS_WRITESLAVE 8
456 int get_free_id(char*bitmap)
467 void jpeg_assert(SWF*master, SWF*slave)
469 /* TODO: if there's a jpegtable found, store it
470 and handle it together with the flash file
473 /* check that master and slave don't have both
474 jpegtables (which would be fatal) */
476 TAG *mpos=0, *spos=0;
479 mtag = master->firstTag;
480 stag = slave->firstTag;
483 if(mtag->id == ST_JPEGTABLES)
489 if(stag->id == ST_JPEGTABLES)
495 if(spos->len == mpos->len &&
496 !memcmp(spos->data, mpos->data, mpos->len))
498 // ok, both have jpegtables, but they're identical.
499 // delete one and don't throw an error
500 swf_DeleteTag(slave, spos);
505 msg("<error> Master and slave have incompatible JPEGTABLES.");
509 TAG* write_sprite_defines(TAG*tag, SWF*sprite)
511 TAG*rtag = sprite->firstTag;
512 while(rtag && rtag->id!=ST_END) {
513 if(!swf_isAllowedSpriteTag(rtag)) {
514 msg("<debug> processing sprite tag %02x", tag->id);
515 if(swf_isDefiningTag(rtag))
517 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
519 tag = swf_InsertTag(tag, rtag->id);
520 swf_SetBlock(tag, rtag->data, rtag->len);
522 else if(swf_isPseudoDefiningTag(rtag))
524 msg("<debug> [sprite defs] write tag %02x (%d bytes in body)",
526 tag = swf_InsertTag(tag, rtag->id);
527 swf_SetBlock(tag, rtag->data, rtag->len);
533 /* if we get here, jpeg_assert has already run,
534 ensuring this is the only one of it's kind,
535 so we may safely write it out */
536 tag = swf_InsertTag(tag, rtag->id);
537 swf_SetBlock(tag, rtag->data, rtag->len);
539 case ST_EXPORTASSETS:
540 msg("<debug> deliberately ignoring EXPORTASSETS tag");
542 case ST_ENABLEDEBUGGER:
543 msg("<debug> deliberately ignoring ENABLEDEBUGGER tag");
545 case ST_SETBACKGROUNDCOLOR:
546 msg("<debug> deliberately ignoring BACKGROUNDCOLOR tag");
549 msg("<debug> deliberately ignoring SHOWFRAME tag");
552 msg("<debug> deliberately ignoring REFLEX tag");
557 msg("<notice> found tag %d. This is a Generator template, isn't it?", rtag->id);
560 msg("<notice> funny tag: %d is neither defining nor sprite", rtag->id);
569 void changedepth(TAG*tag, int add)
571 if(tag->id == ST_PLACEOBJECT)
572 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
573 if(tag->id == ST_PLACEOBJECT2)
574 PUT16(&tag->data[1],GET16(&tag->data[1])+add);
575 if(tag->id == ST_REMOVEOBJECT)
576 PUT16(&tag->data[2],GET16(&tag->data[2])+add);
577 if(tag->id == ST_REMOVEOBJECT2)
578 PUT16(&tag->data[0],GET16(&tag->data[0])+add);
579 if(tag->id == ST_PLACEOBJECT2) {
582 swf_SetTagPos(tag, 0);
583 flags = swf_GetU8(tag);
584 if(flags&2) swf_GetU16(tag); //id
585 if(flags&4) swf_GetMatrix(tag, 0);
586 if(flags&8) swf_GetCXForm(tag, 0,1);
587 if(flags&16) swf_GetU16(tag); //ratio
589 swf_ResetReadBits(tag);
590 printf("%d->%d\n", GET16(&tag->data[tag->pos]),
591 GET16(&tag->data[tag->pos])+add);
592 PUT16(&tag->data[tag->pos],GET16(&tag->data[tag->pos])+add);
594 msg("<warning> Depth relocation not fully working yet with clipdepths", tag->id);
598 void matrix_adjust(MATRIX*m, int movex, int movey, float scalex, float scaley, int scalepos)
600 m->sx = (int)(m->sx*scalex);
601 m->sy = (int)(m->sy*scaley);
602 m->r1 = (int)(m->r1*scalex);
603 m->r0 = (int)(m->r0*scaley);
612 void write_changepos(TAG*output, TAG*tag, int movex, int movey, float scalex, float scaley, int scalepos)
614 if(movex || movey || scalex != 1.0 || scaley != 1.0)
618 case ST_PLACEOBJECT2: {
621 swf_GetMatrix(0, &m);
625 flags = swf_GetU8(tag);
626 swf_SetU8(output, flags|4);
627 swf_SetU16(output, swf_GetU16(tag)); //depth
630 swf_SetU16(output, swf_GetU16(tag)); //id
634 swf_GetMatrix(tag, &m);
636 swf_GetMatrix(0, &m);
638 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
639 swf_SetMatrix(output, &m);
641 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
643 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
646 case ST_PLACEOBJECT: {
648 swf_SetU16(output, swf_GetU16(tag)); //id
649 swf_SetU16(output, swf_GetU16(tag)); //depth
651 swf_GetMatrix(tag, &m);
652 matrix_adjust(&m, movex, movey, scalex, scaley, scalepos);
653 swf_SetMatrix(output, &m);
655 if (tag->readBit) { tag->pos++; tag->readBit = 0; } //swf_ResetReadBits(tag);
657 swf_SetBlock(output, &tag->data[tag->pos], tag->len - tag->pos);
661 swf_SetBlock(output, tag->data, tag->len);
666 swf_SetBlock(output, tag->data, tag->len);
670 TAG* write_sprite(TAG*tag, SWF*sprite, int spriteid, int replaceddefine)
672 TAG* definespritetag;
676 definespritetag = tag = swf_InsertTag(tag, ST_DEFINESPRITE);
677 swf_SetU16(tag, spriteid);
678 swf_SetU16(tag, sprite->frameCount);
679 msg ("<notice> sprite id is %d", spriteid);
681 tmp = sprite->frameCount;
682 msg("<debug> %d frames to go",tmp);
685 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
686 swf_SetU8(tag, 2+64); //flags: character+clipdepth
687 swf_SetU16(tag, 0); //depth
688 swf_SetU16(tag, replaceddefine); //id
689 swf_SetU16(tag, 65535); //clipdepth
692 if(config.overlay && !config.isframe) {
693 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
694 swf_SetU8(tag, 2); //flags: character
695 swf_SetU16(tag, 1); //depth
696 swf_SetU16(tag, replaceddefine); //id
699 rtag = sprite->firstTag;
700 while(rtag && rtag->id!=ST_END)
702 if (swf_isAllowedSpriteTag(rtag)) {
704 msg("<debug> [sprite main] write tag %02x (%d bytes in body)",
705 rtag->id, rtag->len);
706 tag = swf_InsertTag(tag, rtag->id);
707 write_changepos(tag, rtag, config.movex, config.movey, config.scalex, config.scaley, 0);
709 if(config.clip || (config.overlay && !config.isframe))
710 changedepth(tag, +2);
712 if(tag->id == ST_SHOWFRAME)
715 msg("<debug> %d frames to go",tmp);
720 tag = swf_InsertTag(tag, ST_END);
724 static char tag_ok_for_slave(int id)
726 if(id == ST_SETBACKGROUNDCOLOR)
731 TAG* write_master(TAG*tag, SWF*master, SWF*slave, int spriteid, int replaceddefine, int flags)
736 int slavewritten = 0;
737 int deletedepth = -1;
739 TAG* rtag = master->firstTag;
740 TAG* stag = slave->firstTag;
742 while(rtag && rtag->id!=ST_END)
744 if(rtag->id == ST_SHOWFRAME && outputslave)
746 while(stag && stag->id!=ST_END) {
747 if(stag->id == ST_SHOWFRAME) {
752 if(tag_ok_for_slave(stag->id)) {
753 tag = swf_InsertTag(tag, stag->id);
754 write_changepos(tag, stag, config.movex, config.movey, config.scalex, config.scaley, 0);
759 if(rtag->id == ST_SHOWFRAME)
762 tag = swf_InsertTag(tag, ST_SHOWFRAME);
764 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
765 swf_SetU16(tag, deletedepth);
772 if(swf_isDefiningTag(rtag) && (flags&FLAGS_WRITEDEFINES))
774 msg("<debug> [master] write tag %02x (%d bytes in body)",
775 rtag->id, rtag->len);
776 if(swf_GetDefineID(rtag) == spriteid && !config.isframe)
780 tag = swf_InsertTag(tag, rtag->id);
781 swf_SetBlock(tag, rtag->data, rtag->len);
782 swf_SetDefineID(tag, replaceddefine);
784 /* don't write this tag */
785 msg("<verbose> replacing tag %d ID %d with sprite", rtag->id ,spriteid);
788 if(flags&FLAGS_WRITESPRITE)
790 msg("<debug> writing sprite defines");
791 tag = write_sprite_defines(tag, slave);
792 msg("<debug> writing sprite");
793 tag = write_sprite(tag, slave, spriteid, replaceddefine);
795 if(flags&FLAGS_WRITESLAVE)
797 msg("<debug> writing slave");
801 tag = swf_InsertTag(tag, rtag->id);
802 swf_SetBlock(tag, rtag->data, rtag->len);
805 if(frame == slaveframe) /* only happens with config.isframe: put slave at specific frame */
807 if(flags&FLAGS_WRITESLAVE) {
811 if((flags&FLAGS_WRITESPRITE) && !slavewritten)
813 int id = get_free_id(masterbitmap);
817 msg("<fatal> Can't combine --clip and --frame");
820 tag = write_sprite_defines(tag, slave);
821 tag = write_sprite(tag, slave, id, -1);
823 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
824 swf_SetU8(tag, 2); //flags: id
825 swf_SetU16(tag, depth);
831 if(!swf_isDefiningTag(rtag) && (flags&FLAGS_WRITENONDEFINES))
836 case ST_PLACEOBJECT2:
837 if(frame == slaveframe && !config.overlay)
839 case ST_REMOVEOBJECT:
840 /* place/removetags for the object we replaced
841 should be discarded, too, as the object to insert
844 if(spriteid>=0 && swf_GetPlaceID(rtag) == spriteid &&
845 !config.isframe && config.merge)
848 case ST_REMOVEOBJECT2:
852 msg("<debug> [master] write tag %02x (%d bytes in body)",
853 rtag->id, rtag->len);
854 tag = swf_InsertTag(tag, rtag->id);
855 write_changepos(tag, rtag, config.mastermovex, config.mastermovey, config.masterscalex, config.masterscaley, 1);
863 while(stag && stag->id!=ST_END)
865 if(tag_ok_for_slave(stag->id)) {
866 msg("<debug> [slave] write tag %02x (%d bytes in body), %.2f %.2f", rtag->id, rtag->len, config.movex /20.0, config.movey /20.0);
867 tag = swf_InsertTag(tag, stag->id);
868 write_changepos(tag, stag, config.movex, config.movey, config.scalex, config.scaley, 0);
872 if(!slavewritten && config.isframe && (flags&(FLAGS_WRITESLAVE|FLAGS_WRITESPRITE)))
875 msg("<warning> Frame %d doesn't exist in file. No substitution will occur",
878 msg("<warning> Frame \"%s\" doesn't exist in file. No substitution will occur",
881 tag = swf_InsertTag(tag, ST_END);
885 void adjustheader(SWF*swf)
888 swf->frameRate = config.framerate;
889 if(config.hassizex) {
890 swf->movieSize.xmax =
891 swf->movieSize.xmin + config.sizex;
893 if(config.hassizey) {
894 swf->movieSize.ymax =
895 swf->movieSize.ymin + config.sizey;
897 if(config.flashversion)
898 swf->fileVersion = config.flashversion;
901 void catcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
908 msg("<fatal> Can't combine --cat and --frame");
911 if(config.flashversion)
912 master->fileVersion = config.flashversion;
914 tag = master->firstTag;
917 if(swf_isDefiningTag(tag)) {
918 int defineid = swf_GetDefineID(tag);
919 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
920 masterbitmap[defineid] = 1;
925 swf_Relocate(slave, masterbitmap);
926 jpeg_assert(master, slave);
928 memcpy(newswf, master, sizeof(SWF));
929 adjustheader(newswf);
931 tag = newswf->firstTag = swf_InsertTag(0, ST_REFLEX); // to be removed later
933 depths = malloc(65536);
935 msg("<fatal> Couldn't allocate %d bytes of memory", 65536);
938 memset(depths, 0, 65536);
939 mtag = master->firstTag;
940 while(mtag && mtag->id!=ST_END)
944 msg("<debug> [master] write tag %02x (%d bytes in body)",
945 mtag->id, mtag->len);
947 case ST_PLACEOBJECT2:
949 case ST_PLACEOBJECT: {
950 depth = swf_GetDepth(mtag);
954 case ST_REMOVEOBJECT: {
955 depth = swf_GetDepth(mtag);
959 case ST_REMOVEOBJECT2: {
960 depth = swf_GetDepth(mtag);
965 tag = swf_InsertTag(tag, mtag->id);
966 swf_SetBlock(tag, mtag->data, mtag->len);
976 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
981 stag = slave->firstTag;
982 while(stag && stag->id!=ST_END)
984 msg("<debug> [slave] write tag %02x (%d bytes in body)",
985 stag->id, stag->len);
986 tag = swf_InsertTag(tag, stag->id);
987 swf_SetBlock(tag, stag->data, stag->len);
990 tag = swf_InsertTag(tag, ST_END);
992 swf_DeleteTag(newswf, tag);
995 void normalcombine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
998 int replaceddefine = -1;
1001 TAG * tag = master->firstTag;
1003 memset(depthbitmap, 0, sizeof(depthbitmap));
1008 int depth = swf_GetDepth(tag);
1010 depthbitmap[depth] = 1;
1012 if(swf_isDefiningTag(tag)) {
1013 int defineid = swf_GetDefineID(tag);
1014 msg("<debug> tagid %02x defines object %d", tag->id, defineid);
1015 masterbitmap[defineid] = 1;
1017 if (!slavename && defineid==slaveid) {
1019 spriteid = defineid;
1020 msg("<notice> Slave file attached to object %d.", defineid);
1023 } else if(tag->id == ST_EXPORTASSETS) {
1025 int num = swf_GetU16(tag);
1028 U16 id = swf_GetU16(tag);
1029 char*name = swf_GetString(tag);
1030 if(spriteid<0 && slavename && !strcmp(name,slavename)) {
1032 msg("<notice> Slave file attached to object %d exported as %s.", id, name);
1035 } else if(tag->id == ST_SYMBOLCLASS) {
1036 /* a symbolclass tag is like a define tag: it defines id 0000 */
1037 int num = swf_GetU16(tag);
1039 for(t=0;t<num;t++) {
1040 U16 id = swf_GetU16(tag);
1042 masterbitmap[id] = 1;
1046 } else if(tag->id == ST_PLACEOBJECT2) {
1047 char * name = swf_GetName(tag);
1048 int id = swf_GetPlaceID(tag);
1052 swf_GetPlaceObject(tag, &obj);
1053 swf_PlaceObjectFree(&obj);
1055 depthbitmap[obj.clipdepth] = 1;
1060 msg("<verbose> tagid %02x places object %d named \"%s\"", tag->id, id, name);
1062 msg("<verbose> tagid %02x places object %d (no name)", tag->id, id);
1064 if (name && slavename && !strcmp(name,slavename)) {
1067 msg("<notice> Slave file attached to named object %s (%d).", name, id);
1070 } else if(tag->id == ST_SHOWFRAME) {
1071 if(slaveframe>=0 && frame==slaveframe) {
1072 msg("<notice> Slave file attached to frame %d.", frame);
1075 } else if(tag->id == ST_FRAMELABEL) {
1076 char * name = tag->data;
1077 if(name && slavename && config.isframe && !strcmp(name, slavename)) {
1079 msg("<notice> Slave file attached to frame %d (%s).", frame, name);
1085 if (spriteid<0 && !config.isframe) {
1087 if(strcmp(slavename,"!!dummy!!")) {
1088 msg("<warning> Didn't find anything named %s in file. No substitutions will occur.", slavename);
1089 if(!strcmp(slavename, "swf")) {
1090 msg("<warning> (If you were trying to combine rfxview with a document, try replacing 'swf' with 'viewport'.");
1095 msg("<warning> Didn't find id %d in file. No substitutions will occur.", slaveid);
1096 spriteid = get_free_id(masterbitmap);
1099 swf_Relocate (slave, masterbitmap);
1102 swf_RelocateDepth (slave, depthbitmap);
1103 jpeg_assert(slave, master);
1106 replaceddefine = get_free_id(masterbitmap);
1110 memcpy(newswf, master, sizeof(SWF));
1111 adjustheader(newswf);
1113 newswf->firstTag = tag = swf_InsertTag(0, ST_REFLEX); // to be removed later
1115 if (config.antistream) {
1117 msg("<fatal> Can't combine --antistream and --merge");
1119 tag = write_sprite_defines(tag, slave);
1120 tag = write_sprite(tag, slave, spriteid, replaceddefine);
1121 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITEDEFINES);
1122 tag = write_master(tag, master, slave, spriteid, replaceddefine, FLAGS_WRITENONDEFINES);
1125 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1126 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESLAVE );
1128 tag = write_master(tag, master, slave, spriteid, replaceddefine,
1129 FLAGS_WRITEDEFINES|FLAGS_WRITENONDEFINES| FLAGS_WRITESPRITE );
1132 swf_DeleteTag(newswf, newswf->firstTag);
1135 void combine(SWF*master, char*slave_name, SWF*slave, SWF*newswf)
1137 slavename = slave_name;
1141 if(!master->fileVersion && slave)
1142 master->fileVersion = slave->fileVersion;
1144 master->fileAttributes |= slave->fileAttributes;
1146 swf_FoldAll(master);
1149 if(slavename[0] == '#')
1151 slaveid = atoi(&slavename[1]);
1157 if(slavename && slavename[0]!='#') {
1160 sscanf(slavename, "%d%n", &tmp, &len);
1161 if(len == strlen(slavename)) {
1162 /* if the name the slave should replace
1163 consists only of digits and the -f
1164 option is given, it probably is not
1165 a frame name but a frame number.
1173 slaveframe = slaveid;
1176 /* if id wasn't given as either #number or number,
1177 the name is a frame label. BTW: The user wouldn't necessarily have
1178 needed to supply the -f option in this case */
1182 msg("<debug> move x (%d)", config.movex);
1183 msg("<debug> move y (%d)", config.movey);
1184 msg("<debug> scale x (%f)", config.scalex);
1185 msg("<debug> scale y (%f)", config.scaley);
1186 msg("<debug> master move x (%d)", config.mastermovex);
1187 msg("<debug> master move y (%d)", config.mastermovey);
1188 msg("<debug> master scale x (%f)", config.masterscalex);
1189 msg("<debug> master scale y (%f)", config.masterscaley);
1190 msg("<debug> is frame (%d)", config.isframe);
1192 memset(masterbitmap, 0, sizeof(masterbitmap));
1195 return catcombine(master, slave_name, slave, newswf);
1197 return normalcombine(master, slave_name, slave, newswf);
1200 int main(int argn, char *argv[])
1209 config.antistream = 0;
1210 config.alloctest = 0;
1214 config.loglevel = 2;
1217 config.scalex = 1.0;
1218 config.scaley = 1.0;
1221 config.masterscalex = 1.0;
1222 config.masterscaley = 1.0;
1223 config.mastermovex = 0;
1224 config.mastermovey = 0;
1225 config.hassizex = 0;
1226 config.hassizey = 0;
1227 config.framerate = 0;
1233 processargs(argn, argv);
1234 initLog(0,-1,0,0,-1,config.loglevel);
1236 if(config.merge && config.cat) {
1237 msg("<error> Can't combine --cat and --merge");
1241 if(config.stack && config.cat) {
1242 msg("<error> Can't combine --cat and --stack");
1247 if(config.overlay) {
1248 msg("<error> Can't combine -l and -t");
1252 msg("<error> Can't combine -c and -t");
1255 msg("<verbose> (stacking) %d files found\n", numslaves);
1257 makestackmaster(&master);
1261 msg("<verbose> master entity %s (named \"%s\")\n", master_filename, master_name);
1262 fi = open(master_filename, O_RDONLY|O_BINARY);
1264 msg("<fatal> Failed to open %s\n", master_filename);
1267 ret = swf_ReadSWF(fi, &master);
1269 msg("<fatal> Failed to read from %s\n", master_filename);
1272 swf_RemoveJPEGTables(&master);
1273 removeCommonTags(&master);
1274 msg("<debug> Read %d bytes from masterfile\n", ret);
1278 for(t=0;t<numslaves;t++) {
1279 msg("<verbose> slave entity(%d) %s (%s \"%s\")\n", t+1, slave_filename[t],
1280 slave_isframe[t]?"frame":"object", slave_name[t]);
1287 msg("<error> --dummy (-d) implies there are zero slave objects. You supplied %d.", numslaves);
1291 slave_filename[0] = "!!dummy!!";
1292 slave_name[0] = "!!dummy!!";
1293 slave_isframe[0] = 0;
1296 if (config.alloctest)
1298 char*bitmap = malloc(sizeof(char)*65536);
1299 memset(bitmap, 0, 65536*sizeof(char));
1300 memset(bitmap, 1, 101*sizeof(char));
1301 swf_Relocate(&master, bitmap);
1304 // makestackmaster(&newswf);
1311 msg("<error> You must have at least two objects.");
1313 msg("<error> You must have at least one slave entity.");
1316 for(t = 0; t < numslaves; t++)
1318 config.movex = slave_movex[t];
1319 config.movey = slave_movey[t];
1320 config.scalex = slave_scalex[t];
1321 config.scaley = slave_scaley[t];
1322 config.isframe = slave_isframe[t];
1324 msg("<notice> Combine [%s]%s and [%s]%s", master_name, master_filename,
1325 slave_name[t], slave_filename[t]);
1329 fi = open(slave_filename[t], O_RDONLY|O_BINARY);
1331 msg("<fatal> Failed to open %s\n", slave_filename[t]);
1334 ret = swf_ReadSWF(fi, &slave);
1336 msg("<fatal> Failed to read from %s\n", slave_filename[t]);
1339 msg("<debug> Read %d bytes from slavefile\n", ret);
1341 swf_RemoveJPEGTables(&slave);
1342 removeCommonTags(&slave);
1346 memset(&slave, 0, sizeof(slave));
1347 slave.firstTag = swf_InsertTag(0, ST_END);
1348 slave.frameRate = 0;
1349 slave.fileVersion = 0;
1350 slave.frameCount = 0;
1353 combine(&master, slave_name[t], &slave, &newswf);
1356 if(config.dummy && !config.hassizex && !config.hassizey && !config.mastermovex && !config.mastermovey) {
1357 newswf.movieSize.xmin = newswf.movieSize.xmin*config.masterscalex;
1358 newswf.movieSize.ymin = newswf.movieSize.ymin*config.masterscaley;
1359 newswf.movieSize.xmax = newswf.movieSize.xmax*config.masterscalex;
1360 newswf.movieSize.ymax = newswf.movieSize.ymax*config.masterscaley;
1364 if(!newswf.fileVersion)
1365 newswf.fileVersion = 4;
1367 if(config.local_with_filesystem)
1368 newswf.fileAttributes &= ~FILEATTRIBUTE_USENETWORK;
1369 if(config.local_with_networking)
1370 newswf.fileAttributes |= FILEATTRIBUTE_USENETWORK;
1371 if(config.accelerated_blit)
1372 newswf.fileAttributes |= FILEATTRIBUTE_USEACCELERATEDBLIT;
1373 if(config.hardware_gpu)
1374 newswf.fileAttributes |= FILEATTRIBUTE_USEHARDWAREGPU;
1376 fi = open(outputname, O_BINARY|O_RDWR|O_TRUNC|O_CREAT, 0777);
1379 if(newswf.fileVersion < 6)
1380 newswf.fileVersion = 6;
1381 newswf.compressed = 1;
1382 swf_WriteSWF(fi, &newswf);
1384 newswf.compressed = -1; // don't compress
1385 swf_WriteSWF(fi, &newswf);