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"
19 #define _ZLIB_INCLUDED_
24 char * destfilename = "output.swf";
28 char* extractframes = 0;
29 char* extractjpegids = 0;
30 char* extractpngids = 0;
32 char* extractname = 0;
36 struct options_t options[] =
50 int args_callback_option(char*name,char*val)
52 if(!strcmp(name, "V")) {
53 printf("swfextract - part of %s %s\n", PACKAGE, VERSION);
56 else if(!strcmp(name, "o")) {
60 else if(!strcmp(name, "i")) {
63 fprintf(stderr, "You can only supply either name or id\n");
68 else if(!strcmp(name, "n")) {
71 fprintf(stderr, "You can only supply either name or id\n");
76 else if(!strcmp(name, "v")) {
80 else if(!strcmp(name, "j")) {
82 fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
88 #ifdef _ZLIB_INCLUDED_
89 else if(!strcmp(name, "p")) {
91 fprintf(stderr, "Only one --pngs argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
98 else if(!strcmp(name, "f")) {
102 else if(!strcmp(name, "w")) {
107 printf("Unknown option: -%s\n", name);
113 int args_callback_longoption(char*name,char*val)
115 return args_long2shortoption(options, name, val);
117 void args_callback_usage(char*name)
119 printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name);
120 printf("\t-v , --verbose\t\t\t Be more verbose\n");
121 printf("\t-o , --output filename\t\t set output filename\n");
122 printf("\t-n , --name name\t\t instance name of the object to extract\n");
123 printf("\t-i , --id IDs\t\t\t ID of the object to extract\n");
124 printf("\t-j , --jpeg IDs\t\t\t IDs of the JPEG pictures to extract\n");
125 #ifdef _ZLIB_INCLUDED_
126 printf("\t-p , --pngs IDs\t\t\t IDs of the PNG pictures to extract\n");
128 printf("\t-f , --frame frames\t\t frame numbers to extract\n");
129 printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames (use with -f)\n");
130 printf("\t-V , --version\t\t\t Print program version and exit\n");
132 int args_callback_command(char*name,char*val)
135 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
142 U8 mainr,maing,mainb;
143 /* 1 = used, not expanded,
145 5 = wanted, not expanded
153 void idcallback(void*data)
155 if(!(used[*(U16*)data]&1)) {
157 used[*(U16*)data] |= 1;
161 void enumerateIDs(TAG*tag, void(*callback)(void*))
167 data = (U8*)malloc(len);
168 *(U16*)data = (tag->id<<6)+63;
169 *(U32*)&data[2] = tag->len;
170 memcpy(&data[6], tag->data, tag->len);
173 data = (U8*)malloc(len);
174 *(U16*)data = (tag->id<<6)+tag->len;
175 memcpy(&data[2], tag->data, tag->len);
177 map_ids_mem(data, len, callback);
180 void extractTag(SWF*swf, char*filename)
191 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
193 newswf.fileVersion = swf->fileVersion;
194 newswf.frameRate = swf->frameRate;
195 newswf.movieSize = swf->movieSize;
197 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
198 desttag = newswf.firstTag;
202 swf_SetRGB(desttag,&rgb);
206 for(t=0;t<65536;t++) {
207 if(used[t] && !(used[t]&2)) {
208 if(tags[t]->id==ST_DEFINESPRITE) {
210 while(tag->id != ST_END)
212 enumerateIDs(tag, idcallback);
217 enumerateIDs(tags[t], idcallback);
224 srctag = swf->firstTag;
227 while(srctag && (srctag->id || sprite)) {
232 if(srctag->id == ST_END) {
235 if(srctag->id == ST_DEFINESPRITE)
237 if(swf_isDefiningTag(srctag)) {
238 int id = swf_GetDefineID(srctag);
242 if (((srctag->id == ST_PLACEOBJECT ||
243 srctag->id == ST_PLACEOBJECT2 ||
244 srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
245 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
252 if(srctag->id == ST_REMOVEOBJECT) {
253 if(!used[swf_GetPlaceID(srctag)])
258 TAG*ttag = (TAG*)malloc(sizeof(TAG));
259 desttag = swf_InsertTag(desttag, srctag->id);
260 desttag->len = desttag->memsize = srctag->len;
261 desttag->data = malloc(srctag->len);
262 memcpy(desttag->data, srctag->data, srctag->len);
267 srctag = srctag->next;
270 if(!extractframes && !hollow)
271 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
273 desttag = swf_InsertTag(desttag,ST_END);
275 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT, 0644);
276 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
279 swf_FreeTags(&newswf); // cleanup
282 void listObjects(SWF*swf)
288 char*names[] = {"Shapes","MovieClips","JPEGs","PNGs","Sounds","Frames"};
289 printf("Objects in file %s:\n",filename);
297 (tag->id == ST_DEFINESHAPE ||
298 tag->id == ST_DEFINESHAPE2 ||
299 tag->id == ST_DEFINESHAPE3)) {
301 sprintf(text,"%d", swf_GetDefineID(tag));
304 if(tag->id == ST_DEFINESPRITE) {
307 sprintf(text,"%d", swf_GetDefineID(tag));
310 while(tag->id != ST_END)
314 if(t == 2 && (tag->id == ST_DEFINEBITS ||
315 tag->id == ST_DEFINEBITSJPEG2 ||
316 tag->id == ST_DEFINEBITSJPEG3)) {
318 sprintf(text,"%d", swf_GetDefineID(tag));
321 if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
322 tag->id == ST_DEFINEBITSLOSSLESS2)) {
324 sprintf(text,"%d", swf_GetDefineID(tag));
328 if(t == 4 && (tag->id == ST_DEFINESOUND)) {
330 sprintf(text,"%d", swf_GetDefineID(tag));
333 if(t == 5 && (tag->id == ST_SHOWFRAME)) {
335 sprintf(text,"%d", frame);
343 printf("%s: ", names[t]);
357 void handlejpegtables(TAG*tag)
359 if(tag->id == ST_JPEGTABLES) {
360 jpegtables = tag->data;
361 jpegtablessize = tag->len;
365 FILE* save_fopen(char* name, char* mode)
367 FILE*fi = fopen(name, mode);
369 fprintf(stderr, "Error: Couldn't open %s\n", name);
375 int findjpegboundary(U8*data, int len)
390 /* extract jpeg data out of a tag */
391 void handlejpeg(TAG*tag)
395 sprintf(name, "pic%d.jpeg", *(U16*)tag->data);
396 /* swf jpeg images have two streams, which both start with ff d8 and
397 end with ff d9. The following code handles sorting the middle
398 <ff d9 ff d8> bytes out, so that one stream remains */
399 if(tag->id == ST_DEFINEBITS && tag->len>2 && jpegtables) {
400 fi = save_fopen(name, "wb");
401 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
402 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
405 if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
407 int pos = findjpegboundary(&tag->data[2], tag->len-2);
411 fi = save_fopen(name, "wb");
412 fwrite(&tag->data[2], pos-2, 1, fi);
413 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
416 if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
417 U32 end = *(U32*)&tag->data[2]+6;
418 int pos = findjpegboundary(&tag->data[6], tag->len-6);
422 fi = save_fopen(name, "wb");
423 fwrite(&tag->data[6], pos-6, 1, fi);
424 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
429 #ifdef _ZLIB_INCLUDED_
432 static U32*crc32_table = 0;
433 static void make_crc32_table(void)
438 crc32_table = (U32*)malloc(1024);
440 for (t = 0; t < 256; t++) {
443 for (s = 0; s < 8; s++) {
444 c = (0xedb88320L*(c&1)) ^ (c >> 1);
449 static void png_write_byte(FILE*fi, U8 byte)
451 fwrite(&byte,1,1,fi);
452 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
454 static void png_start_chunk(FILE*fi, char*type, int len)
456 U8 mytype[4]={0,0,0,0};
457 U32 mylen = REVERSESWAP32(len);
458 memcpy(mytype,type,strlen(type));
459 fwrite(&mylen, 4, 1, fi);
461 png_write_byte(fi,mytype[0]);
462 png_write_byte(fi,mytype[1]);
463 png_write_byte(fi,mytype[2]);
464 png_write_byte(fi,mytype[3]);
466 static void png_write_bytes(FILE*fi, U8*bytes, int len)
470 png_write_byte(fi,bytes[t]);
472 static void png_write_dword(FILE*fi, U32 dword)
474 png_write_byte(fi,dword>>24);
475 png_write_byte(fi,dword>>16);
476 png_write_byte(fi,dword>>8);
477 png_write_byte(fi,dword);
479 static void png_end_chunk(FILE*fi)
481 U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
486 /* extract a lossless image (png) out of a tag
487 This routine was originally meant to be a one-pager. I just
488 didn't know png is _that_ much fun. :) -mk
490 void handlelossless(TAG*tag)
507 U8 head[] = {137,80,78,71,13,10,26,10};
509 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
517 if(tag->id != ST_DEFINEBITSLOSSLESS &&
518 tag->id != ST_DEFINEBITSLOSSLESS2)
522 format = swf_GetU8(tag);
523 if(format == 3) bpp = 8;
524 if(format == 4) bpp = 16;
525 if(format == 5) bpp = 32;
526 if(format!=3 && format!=5) {
528 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
530 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
533 width = swf_GetU16(tag);
534 height = swf_GetU16(tag);
535 if(format == 3) cols = swf_GetU8(tag) + 1;
536 // this is what format means according to the flash specification. (which is
538 // if(format == 4) cols = swf_GetU16(tag) + 1;
539 // if(format == 5) cols = swf_GetU32(tag) + 1;
542 logf("<verbose> Width %d", width);
543 logf("<verbose> Height %d", height);
544 logf("<verbose> Format %d", format);
545 logf("<verbose> Cols %d", cols);
546 logf("<verbose> Bpp %d", bpp);
548 datalen = (width*height*bpp/8+cols*8);
553 data = malloc(datalen);
554 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
555 } while(error == Z_BUF_ERROR);
557 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
560 logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
563 data2 = malloc(datalen2);
564 palette = (RGBA*)malloc(cols*sizeof(RGBA));
566 for(t=0;t<cols;t++) {
567 palette[t].r = data[pos++];
568 palette[t].g = data[pos++];
569 palette[t].b = data[pos++];
571 palette[t].a = data[pos++];
575 sprintf(name, "pic%d.png", id);
576 fi = save_fopen(name, "wb");
577 fwrite(head,sizeof(head),1,fi);
579 png_start_chunk(fi, "IHDR", 13);
580 png_write_dword(fi,width);
581 png_write_dword(fi,height);
582 png_write_byte(fi,8);
584 png_write_byte(fi,3); //indexed
586 png_write_byte(fi,2); //rgb
589 png_write_byte(fi,0); //compression mode
590 png_write_byte(fi,0); //filter mode
591 png_write_byte(fi,0); //interlace mode
595 png_start_chunk(fi, "PLTE", 768);
597 png_write_byte(fi,palette[t].r);
598 png_write_byte(fi,palette[t].g);
599 png_write_byte(fi,palette[t].b);
606 int srcwidth = width * (bpp/8);
607 datalen3 = width*height*4;
608 data3 = (U8*)malloc(datalen3);
609 for(y=0;y<height;y++)
611 data3[pos2++]=0; //filter type
613 // 32 bit to 24 bit "conversion"
614 for(x=0;x<width;x++) {
615 data3[pos2++]=data[pos+1];
616 data3[pos2++]=data[pos+2];
617 data3[pos2++]=data[pos+3];
618 pos+=4; //ignore padding byte
622 for(x=0;x<srcwidth;x++)
623 data3[pos2++]=data[pos++];
626 pos+=((srcwidth+3)&~3)-srcwidth; //align
631 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
632 fprintf(stderr, "zlib error in pic %d\n", id);
635 logf("<verbose> Compressed data is %d bytes", datalen2);
636 png_start_chunk(fi, "IDAT", datalen2);
637 png_write_bytes(fi,data2,datalen2);
639 png_start_chunk(fi, "IEND", 0);
648 int main (int argc,char ** argv)
657 char listavailable = 0;
658 processargs(argc, argv);
660 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids)
665 fprintf(stderr, "You must supply a filename.\n");
668 initLog(0,-1,0,0,-1, verbose);
670 f = open(filename,O_RDONLY);
674 perror("Couldn't open file: ");
677 if (swf_ReadSWF(f,&swf) < 0)
679 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
698 tagused = (char*)malloc(tagnum);
699 memset(tagused, 0, tagnum);
700 memset(used, 0, 65536);
701 memset(depths, 0, 65536);
706 if(swf_isAllowedSpriteTag(tag)) {
708 if(extractframes && is_in_range(frame, extractframes)) {
710 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
711 depths[swf_GetDepth(tag)] = 1;
713 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
714 int depth = swf_GetDepth(tag);
717 depths[swf_GetDepth(tag)] = 0;
720 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
721 (depths[swf_GetDepth(tag)]) && hollow) {
723 depths[swf_GetDepth(tag)] = 0;
727 enumerateIDs(tag, idcallback);
733 if(tag->id == ST_JPEGTABLES)
734 handlejpegtables(tag);
736 if(swf_isDefiningTag(tag)) {
737 int id = swf_GetDefineID(tag);
739 if(extractids && is_in_range(id, extractids)) {
743 if(extractjpegids && is_in_range(id, extractjpegids)) {
746 #ifdef _ZLIB_INCLUDED_
747 if(extractpngids && is_in_range(id, extractpngids)) {
752 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
753 mainr = tag->data[0];
754 maing = tag->data[1];
755 mainb = tag->data[2];
757 else if(tag->id == ST_PLACEOBJECT2) {
758 char*name = swf_GetName(tag);
759 if(name && extractname && !strcmp(name, extractname)) {
760 int id = swf_GetPlaceID(tag);
764 depths[swf_GetDepth(tag)] = 1;
767 else if(tag->id == ST_SHOWFRAME) {
775 if(tag->id == ST_DEFINESPRITE) {
776 while(tag->id != ST_END) {
785 extractTag(&swf, destfilename);