2 Allows to extract parts of the swf into a new file.
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This file is distributed under the GPL, see file COPYING for details */
13 #include "../lib/rfxswf.h"
14 #include "../lib/args.h"
18 char * destfilename = "output.swf";
22 char* extractframes = 0;
23 char* extractjpegids = 0;
25 char* extractname = 0;
29 struct options_t options[] =
42 int args_callback_option(char*name,char*val)
44 if(!strcmp(name, "V")) {
45 printf("swfextract - part of %s %s\n", PACKAGE, VERSION);
48 else if(!strcmp(name, "o")) {
52 else if(!strcmp(name, "i")) {
55 fprintf(stderr, "You can only supply either name or id\n");
60 else if(!strcmp(name, "n")) {
63 fprintf(stderr, "You can only supply either name or id\n");
68 else if(!strcmp(name, "v")) {
72 else if(!strcmp(name, "j")) {
74 fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
80 else if(!strcmp(name, "f")) {
84 else if(!strcmp(name, "w")) {
89 printf("Unknown option: -%s\n", name);
95 int args_callback_longoption(char*name,char*val)
97 return args_long2shortoption(options, name, val);
99 void args_callback_usage(char*name)
101 printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name);
102 printf("\t-v , --verbose\t\t\t Be more verbose\n");
103 printf("\t-o , --output filename\t\t set output filename\n");
104 printf("\t-n , --name name\t\t instance name of the object to extract\n");
105 printf("\t-i , --id IDs\t\t\t ID of the object to extract\n");
106 printf("\t-j , --jpeg IDs\t\t\t IDs of the jpeg pictures to extract\n");
107 printf("\t-f , --frame frames\t\t frame numbers to extract\n");
108 printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames (use with -f)\n");
109 printf("\t-V , --version\t\t\t Print program version and exit\n");
111 int args_callback_command(char*name,char*val)
114 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
121 U8 mainr,maing,mainb;
122 /* 1 = used, not expanded,
124 5 = wanted, not expanded
132 void idcallback(void*data)
134 if(!(used[*(U16*)data]&1)) {
136 used[*(U16*)data] |= 1;
140 void enumerateIDs(TAG*tag, void(*callback)(void*))
146 data = (U8*)malloc(len);
147 *(U16*)data = (tag->id<<6)+63;
148 *(U32*)&data[2] = tag->len;
149 memcpy(&data[6], tag->data, tag->len);
152 data = (U8*)malloc(len);
153 *(U16*)data = (tag->id<<6)+tag->len;
154 memcpy(&data[2], tag->data, tag->len);
156 map_ids_mem(data, len, callback);
159 void extractTag(SWF*swf, char*filename)
170 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
172 newswf.fileVersion = swf->fileVersion;
173 newswf.frameRate = swf->frameRate;
174 newswf.movieSize = swf->movieSize;
176 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
177 desttag = newswf.firstTag;
181 swf_SetRGB(desttag,&rgb);
185 for(t=0;t<65536;t++) {
186 if(used[t] && !(used[t]&2)) {
187 if(tags[t]->id==ST_DEFINESPRITE) {
189 while(tag->id != ST_END)
191 enumerateIDs(tag, idcallback);
196 enumerateIDs(tags[t], idcallback);
203 srctag = swf->firstTag;
206 while(srctag && (srctag->id || sprite)) {
211 if(srctag->id == ST_END) {
214 if(srctag->id == ST_DEFINESPRITE)
216 if(swf_isDefiningTag(srctag)) {
217 int id = swf_GetDefineID(srctag);
221 if (((srctag->id == ST_PLACEOBJECT ||
222 srctag->id == ST_PLACEOBJECT2 ||
223 srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
224 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
231 if(srctag->id == ST_REMOVEOBJECT) {
232 if(!used[swf_GetPlaceID(srctag)])
237 TAG*ttag = (TAG*)malloc(sizeof(TAG));
238 desttag = swf_InsertTag(desttag, srctag->id);
239 desttag->len = desttag->memsize = srctag->len;
240 desttag->data = malloc(srctag->len);
241 memcpy(desttag->data, srctag->data, srctag->len);
246 srctag = srctag->next;
249 if(!extractframes && !hollow)
250 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
252 desttag = swf_InsertTag(desttag,ST_END);
254 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT, 0644);
255 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
258 swf_FreeTags(&newswf); // cleanup
261 void listObjects(SWF*swf)
267 char*names[] = {"Shapes","MovieClips","JPEGs","Sounds","Frames"};
268 printf("Objects in file %s:\n",filename);
276 (tag->id == ST_DEFINESHAPE ||
277 tag->id == ST_DEFINESHAPE2 ||
278 tag->id == ST_DEFINESHAPE3)) {
280 sprintf(text,"%d", swf_GetDefineID(tag));
283 if(tag->id == ST_DEFINESPRITE) {
286 sprintf(text,"%d", swf_GetDefineID(tag));
289 while(tag->id != ST_END)
293 if(t == 2 && (tag->id == ST_DEFINEBITS ||
294 tag->id == ST_DEFINEBITSJPEG2 ||
295 tag->id == ST_DEFINEBITSJPEG3)) {
297 sprintf(text,"%d", swf_GetDefineID(tag));
300 if(t == 3 && (tag->id == ST_DEFINESOUND)) {
302 sprintf(text,"%d", swf_GetDefineID(tag));
305 if(t == 4 && (tag->id == ST_SHOWFRAME)) {
307 sprintf(text,"%d", frame);
315 printf("%s: ", names[t]);
329 void handlejpegtables(TAG*tag)
331 if(tag->id == ST_JPEGTABLES) {
332 jpegtables = tag->data;
333 jpegtablessize = tag->len;
337 FILE* save_fopen(char* name, char* mode)
339 FILE*fi = fopen(name, mode);
341 fprintf(stderr, "Error: Couldn't open %s\n", name);
347 int findjpegboundary(U8*data, int len)
362 /* extract jpeg data out of a tag */
363 void handlejpeg(TAG*tag)
367 sprintf(name, "pic%d.jpeg", *(U16*)tag->data);
368 /* swf jpeg images have two streams, which both start with ff d8 and
369 end with ff d9. The following code handles sorting the middle
370 <ff d9 ff d8> bytes out, so that one stream remains */
371 if(tag->id == ST_DEFINEBITS && tag->len>2 && jpegtables) {
372 fi = save_fopen(name, "wb");
373 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
374 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
377 if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
379 int pos = findjpegboundary(&tag->data[2], tag->len-2);
383 fi = save_fopen(name, "wb");
384 fwrite(&tag->data[2], pos-2, 1, fi);
385 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
388 if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
389 U32 end = *(U32*)&tag->data[2]+6;
390 int pos = findjpegboundary(&tag->data[6], tag->len-6);
394 fi = save_fopen(name, "wb");
395 fwrite(&tag->data[6], pos-6, 1, fi);
396 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
401 int main (int argc,char ** argv)
410 char listavailable = 0;
411 processargs(argc, argv);
413 if(!extractframes && !extractids && ! extractname && !extractjpegids)
418 fprintf(stderr, "You must supply a filename.\n");
421 initLog(0,-1,0,0,-1, verbose);
423 f = open(filename,O_RDONLY);
427 perror("Couldn't open file: ");
430 if (swf_ReadSWF(f,&swf) < 0)
432 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
451 tagused = (char*)malloc(tagnum);
452 memset(tagused, 0, tagnum);
453 memset(used, 0, 65536);
454 memset(depths, 0, 65536);
459 if(swf_isAllowedSpriteTag(tag)) {
461 if(extractframes && is_in_range(frame, extractframes)) {
463 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
464 depths[swf_GetDepth(tag)] = 1;
466 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
467 int depth = swf_GetDepth(tag);
470 depths[swf_GetDepth(tag)] = 0;
473 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
474 (depths[swf_GetDepth(tag)]) && hollow) {
476 depths[swf_GetDepth(tag)] = 0;
480 enumerateIDs(tag, idcallback);
486 if(tag->id == ST_JPEGTABLES)
487 handlejpegtables(tag);
489 if(swf_isDefiningTag(tag)) {
490 int id = swf_GetDefineID(tag);
492 if(extractids && is_in_range(id, extractids)) {
496 if(extractjpegids && is_in_range(id, extractjpegids)) {
500 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
501 mainr = tag->data[0];
502 maing = tag->data[1];
503 mainb = tag->data[2];
505 else if(tag->id == ST_PLACEOBJECT2) {
506 char*name = swf_GetName(tag);
507 if(name && extractname && !strcmp(name, extractname)) {
508 int id = swf_GetPlaceID(tag);
512 depths[swf_GetDepth(tag)] = 1;
515 else if(tag->id == ST_SHOWFRAME) {
523 if(tag->id == ST_DEFINESPRITE) {
524 while(tag->id != ST_END) {
533 extractTag(&swf, destfilename);