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 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 "../lib/jpeg.h"
29 #include "../lib/png.h"
33 #define _ZLIB_INCLUDED_
38 char * destfilename
= "output.swf";
42 char* extractframes
= 0;
43 char* extractjpegids
= 0;
44 char* extractfontids
= 0;
45 char* extractpngids
= 0;
46 char* extractsoundids
= 0;
47 char* extractmp3ids
= 0;
48 char* extractbinaryids
= 0;
49 char* extractanyids
= 0;
52 char* extractname
= 0;
55 char originalplaceobjects
= 0;
59 char *outputformat
= NULL
;
61 struct options_t options
[] =
85 int args_callback_option(char*name
,char*val
)
87 if(!strcmp(name
, "V")) {
88 printf("swfextract - part of %s %s\n", PACKAGE
, VERSION
);
91 else if(!strcmp(name
, "o")) {
95 else if(!strcmp(name
, "i")) {
99 fprintf(stderr
, "You can only supply either name or id\n");
104 else if(!strcmp(name
, "n")) {
108 fprintf(stderr
, "You can only supply either name or id\n");
113 else if(!strcmp(name
, "v")) {
117 else if(!strcmp(name
, "m")) {
122 else if(!strcmp(name
, "M")) {
123 if(extractsoundids
) {
124 fprintf(stderr
, "Only one --embeddedmp3 argument is allowed. (Try to use a range, e.g. -M 1,2,3)\n");
131 else if(!strcmp(name
, "j")) {
133 fprintf(stderr
, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
136 /* TODO: count number of IDs in val range */
138 extractjpegids
= val
;
141 else if(!strcmp(name
, "F")) {
143 fprintf(stderr
, "Only one --font argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
147 extractfontids
= val
;
150 else if(!strcmp(name
, "s")) {
151 if(extractsoundids
) {
152 fprintf(stderr
, "Only one --sound argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
156 extractsoundids
= val
;
159 else if(!strcmp(name
, "b")) {
160 if(extractbinaryids
) {
161 fprintf(stderr
, "Only one --binary argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
165 extractbinaryids
= val
;
168 #ifdef _ZLIB_INCLUDED_
169 else if(!strcmp(name
, "p")) {
171 fprintf(stderr
, "Only one --png argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
179 else if(!strcmp(name
, "a")) {
184 else if(!strcmp(name
, "f")) {
189 else if(!strcmp(name
, "P")) {
190 originalplaceobjects
= 1;
193 else if(!strcmp(name
, "0")) {
197 else if(!strcmp(name
, "w")) {
201 else if (!strcmp(name
, "O")) {
206 printf("Unknown option: -%s\n", name
);
212 int args_callback_longoption(char*name
,char*val
)
214 return args_long2shortoption(options
, name
, val
);
216 void args_callback_usage(char*name
)
218 printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name
);
219 printf("\t-v , --verbose\t\t\t Be more verbose\n");
220 printf("\t-o , --output filename\t\t set output filename\n");
221 printf("\t-V , --version\t\t\t Print program version and exit\n\n");
222 printf("SWF Subelement extraction:\n");
223 printf("\t-n , --name name\t\t instance name of the object (SWF Define) to extract\n");
224 printf("\t-i , --id ID\t\t\t ID of the object, shape or movieclip to extract\n");
225 printf("\t-f , --frame frames\t\t frame numbers to extract\n");
226 printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames\n");
227 printf("\t \t\t\t (use with -f)\n");
228 printf("\t-P , --placeobject\t\t\t Insert original placeobject into output file\n");
229 printf("\t \t\t\t (use with -i)\n");
230 printf("SWF Font/Text extraction:\n");
231 printf("\t-F , --font ID\t\t\t Extract font(s)\n");
232 printf("Picture extraction:\n");
233 printf("\t-j , --jpeg ID\t\t\t Extract JPEG picture(s)\n");
234 #ifdef _ZLIB_INCLUDED_
235 printf("\t-p , --pngs ID\t\t\t Extract PNG picture(s)\n");
238 printf("Sound extraction:\n");
239 printf("\t-m , --mp3\t\t\t Extract main mp3 stream\n");
240 printf("\t-M , --embeddedmp3\t\t\t Extract embedded mp3 stream(s)\n");
241 printf("\t-s , --sound ID\t\t\t Extract Sound(s)\n");
243 int args_callback_command(char*name
,char*val
)
246 fprintf(stderr
, "Only one file allowed. You supplied at least two. (%s and %s)\n",
253 void prepare_name(char *buf
, size_t len
, const char *prefix
,
254 const char *suffix
, int idx
) {
255 if (outputformat
!=NULL
) {
256 // override default file name formatting
257 // make sure single-file behavior is not used
259 // Other parts of codebase use vsnprintf, so I assume snprintf
260 // is available on all platforms that swftools currently works on.
261 // We need to check for buffer overflows now that the user is
262 // supplying the format string.
263 snprintf(buf
,len
,outputformat
,idx
,suffix
);
265 // use default file name formatting, unchanged
266 sprintf(buf
,"%s%d.%s",prefix
,idx
,suffix
);
270 U8 mainr
,maing
,mainb
;
271 /* 1 = used, not expanded,
273 5 = wanted, not expanded
280 int extractname_id
= -1;
282 void idcallback(void*data
)
284 if(!(used
[GET16(data
)]&1)) {
286 used
[GET16(data
)] |= 1;
290 void enumerateIDs(TAG
*tag
, void(*callback
)(void*))
296 data = (U8*)malloc(len);
297 PUT16(data, (tag->id<<6)+63);
298 *(U8*)&data[2] = tag->len;
299 *(U8*)&data[3] = tag->len>>8;
300 *(U8*)&data[4] = tag->len>>16;
301 *(U8*)&data[5] = tag->len>>24;
302 memcpy(&data[6], tag->data, tag->len);
305 data = (U8*)malloc(len);
306 PUT16(data, (tag->id<<6)+tag->len);
307 memcpy(&data[2], tag->data, tag->len);
309 map_ids_mem(data, len, callback);
311 int num
= swf_GetNumUsedIDs(tag
);
312 int *ptr
= malloc(sizeof(int)*num
);
314 swf_GetUsedIDs(tag
, ptr
);
316 callback(&tag
->data
[ptr
[t
]]);
319 void moveToZero(TAG
*tag
)
321 if(!swf_isPlaceTag(tag
))
324 swf_GetPlaceObject(tag
, &obj
);
327 swf_ResetTag(tag
, tag
->id
);
328 swf_SetPlaceObject(tag
, &obj
);
331 void extractTag(SWF
*swf
, char*filename
)
343 memset(&newswf
,0x00,sizeof(SWF
)); // set global movie parameters
345 newswf
.fileVersion
= swf
->fileVersion
;
346 newswf
.frameRate
= swf
->frameRate
;
347 newswf
.movieSize
= swf
->movieSize
;
348 if(movetozero
&& originalplaceobjects
) {
349 newswf
.movieSize
.xmax
= swf
->movieSize
.xmax
- swf
->movieSize
.xmin
;
350 newswf
.movieSize
.ymax
= swf
->movieSize
.ymax
- swf
->movieSize
.ymin
;
351 newswf
.movieSize
.xmin
= 0;
352 newswf
.movieSize
.ymin
= 0;
355 newswf
.firstTag
= swf_InsertTag(NULL
,ST_SETBACKGROUNDCOLOR
);
356 desttag
= newswf
.firstTag
;
360 swf_SetRGB(desttag
,&rgb
);
362 swf_GetRect(0, &objectbbox
);
366 for(t
=0;t
<65536;t
++) {
367 if(used
[t
] && !(used
[t
]&2)) {
369 msg("<warning> ID %d is referenced, but never defined.", t
);
370 } else if(tags
[t
]->id
==ST_DEFINESPRITE
) {
372 while(tag
->id
!= ST_END
)
374 enumerateIDs(tag
, idcallback
);
379 enumerateIDs(tags
[t
], idcallback
);
386 srctag
= swf
->firstTag
;
389 while(srctag
&& (srctag
->id
|| sprite
)) {
394 if(srctag
->id
== ST_END
) {
397 if(srctag
->id
== ST_DEFINESPRITE
)
399 if(srctag
->id
== ST_JPEGTABLES
)
401 if(swf_isDefiningTag(srctag
)) {
402 int id
= swf_GetDefineID(srctag
);
406 b
= swf_GetDefineBBox(srctag
);
407 swf_ExpandRect2(&objectbbox
, &b
);
410 if ((((swf_isPlaceTag(srctag
) && originalplaceobjects
)
411 || srctag
->id
== ST_STARTSOUND
) && (used
[swf_GetPlaceID(srctag
)]&4) ) ||
412 (swf_isPseudoDefiningTag(srctag
) && used
[swf_GetDefineID(srctag
)]) ||
419 if(srctag
->id
== ST_REMOVEOBJECT
) {
420 if(!used
[swf_GetPlaceID(srctag
)])
425 TAG
*ttag
= (TAG
*)malloc(sizeof(TAG
));
426 desttag
= swf_InsertTag(desttag
, srctag
->id
);
427 desttag
->len
= desttag
->memsize
= srctag
->len
;
428 desttag
->data
= malloc(srctag
->len
);
429 memcpy(desttag
->data
, srctag
->data
, srctag
->len
);
430 if(movetozero
&& swf_isPlaceTag(desttag
)) {
437 srctag
= srctag
->next
;
440 if(!extractframes
&& !hollow
) {
441 if(!originalplaceobjects
&& (extractids
||extractname_id
>=0)) {
447 memset(&bbox
, 0, sizeof(SRECT
));
449 for(t
=0;t
<65536;t
++) {
450 if(is_in_range(t
, extractids
)) {
457 printf("warning! You should use the -P when extracting multiple objects\n");
461 /* if there is only one object, we will scale it.
462 So let's figure out its bounding box */
463 TAG
*tag
= swf
->firstTag
;
465 if(swf_isDefiningTag(tag
) && tag
->id
!= ST_DEFINESPRITE
) {
466 if(swf_GetDefineID(tag
) == id
)
467 bbox
= swf_GetDefineBBox(tag
);
472 newswf
.movieSize
.xmin
= 0;
473 newswf
.movieSize
.ymin
= 0;
474 newswf
.movieSize
.xmax
= 512*20;
475 newswf
.movieSize
.ymax
= 512*20;
477 if((objectbbox
.xmin
|objectbbox
.ymin
|objectbbox
.xmax
|objectbbox
.ymax
)!=0)
478 newswf
.movieSize
= objectbbox
;
481 if(extractname_id
>=0) {
482 desttag
= swf_InsertTag(desttag
, ST_PLACEOBJECT2
);
483 swf_ObjectPlace(desttag
, extractname_id
, extractname_id
, 0,0,extractname
);
485 for(t
=0;t
<65536;t
++) {
486 if(is_in_range(t
, extractids
)) {
488 desttag
= swf_InsertTag(desttag
, ST_PLACEOBJECT2
);
489 swf_GetMatrix(0, &m
);
491 int width
= bbox
.xmax
- bbox
.xmin
;
492 int height
= bbox
.ymax
- bbox
.ymin
;
493 int max
= width
>height
?width
:height
;
497 m
.sx
= (512*20*65536)/max
;
498 m
.sy
= (512*20*65536)/max
;
500 //newswf.movieSize = swf_TurnRect(newswf.movieSize, &m);
502 swf_ObjectPlace(desttag
, t
, t
, &m
,0,0);
507 desttag
= swf_InsertTag(desttag
,ST_SHOWFRAME
);
509 desttag
= swf_InsertTag(desttag
,ST_END
);
511 f
= open(filename
, O_TRUNC
|O_WRONLY
|O_CREAT
|O_BINARY
, 0644);
512 if FAILED(swf_WriteSWF(f
,&newswf
)) fprintf(stderr
,"WriteSWF() failed.\n");
515 swf_FreeTags(&newswf
); // cleanup
518 int isOfType(int t
, TAG
*tag
)
521 if(t
== 0 && (tag
->id
== ST_DEFINESHAPE
||
522 tag
->id
== ST_DEFINESHAPE2
||
523 tag
->id
== ST_DEFINESHAPE3
)) {
526 if(t
==1 && tag
->id
== ST_DEFINESPRITE
) {
529 if(t
== 2 && (tag
->id
== ST_DEFINEBITS
||
530 tag
->id
== ST_DEFINEBITSJPEG2
||
531 tag
->id
== ST_DEFINEBITSJPEG3
)) {
534 if(t
== 3 && (tag
->id
== ST_DEFINEBITSLOSSLESS
||
535 tag
->id
== ST_DEFINEBITSLOSSLESS2
)) {
538 if(t
== 4 && (tag
->id
== ST_DEFINESOUND
)) {
541 if(t
== 5 && (tag
->id
== ST_DEFINEFONT
|| tag
->id
== ST_DEFINEFONT2
|| tag
->id
== ST_DEFINEFONT3
)) {
544 if (t
== 6 && (tag
->id
== ST_DEFINEBINARY
)) {
547 if (t
== 7 && (tag
->id
== ST_DEFINESPRITE
)) {
548 int wasFolded
= swf_IsFolded(tag
);
552 swf_UnFoldSprite(tag
);
554 while(tag
->id
!= ST_END
) {
556 if(tag
->id
== ST_SOUNDSTREAMHEAD
|| tag
->id
== ST_SOUNDSTREAMHEAD2
) {
563 swf_FoldSprite(toFold
);
569 void listObjects(SWF
*swf
)
575 char*names
[] = {"Shape", "MovieClip", "JPEG", "PNG", "Sound", "Font", "Binary", "Embedded MP3"};
576 char*options
[] = {"-i", "-i", "-j", "-p", "-s", "-F","-b","-M"};
578 printf("Objects in file %s:\n",filename
);
580 for(t
=0;t
<sizeof(names
)/sizeof(names
[0]);t
++) {
582 int lastid
= -2, lastprint
=-1;
587 if(tag
->id
== ST_SOUNDSTREAMHEAD
|| tag
->id
== ST_SOUNDSTREAMHEAD2
)
596 printf(" [%s] %d %s%s: ID(s) ", options
[t
], nr
, names
[t
], nr
>1?"s":"");
601 char show
= isOfType(t
,tag
);
607 id
= swf_GetDefineID(tag
);
612 if(first
|| !follow
) {
617 if(lastprint
+ 1 == lastid
)
618 printf(", %d, %d", lastid
, id
);
620 printf("-%d, %d", lastid
, id
);
630 if(lastprint
+ 1 == lastid
)
631 printf(", %d", lastid
);
633 printf("-%d", lastid
);
639 printf(" [-f] %d Frames: ID(s) 0-%d\n", frame
, frame
);
641 printf(" [-f] 1 Frame: ID(s) 0\n");
644 printf(" [-m] 1 MP3 Soundstream\n");
647 int handlefont(SWF
*swf
, TAG
*tag
)
652 char*filename
= name
;
655 id
= swf_GetDefineID(tag
);
656 prepare_name(name
, sizeof(name
), "font", "swf", id
);
658 filename
= destfilename
;
661 swf_FontExtract(swf
, id
, &f
);
663 if (!extractanyids
) {
664 printf("Couldn't extract font %d\n", id
);
669 swf_WriteFont(f
, filename
);
674 static char has_jpegtables
=0;
675 static U8
*jpegtables
= 0;
676 static int jpegtablessize
= 0;
678 void handlejpegtables(TAG
*tag
)
680 if(tag
->id
== ST_JPEGTABLES
) {
681 jpegtables
= tag
->data
;
682 jpegtablessize
= tag
->len
;
687 FILE* save_fopen(char* name
, char* mode
)
689 FILE*fi
= fopen(name
, mode
);
691 fprintf(stderr
, "Error: Couldn't open %s\n", name
);
697 int findjpegboundary(U8
*data
, int len
)
701 for(t
=0;t
<len
-4;t
++) {
712 /* extract jpeg data out of a tag */
713 int handlejpeg(TAG
*tag
)
716 char*filename
= name
;
719 if(tag
->id
!= ST_DEFINEBITSJPEG3
) {
720 prepare_name(name
, sizeof(name
), "pic", "jpg", GET16(tag
->data
));
722 filename
= destfilename
;
723 if(!strcmp(filename
,"output.swf"))
724 filename
= "output.jpg";
727 prepare_name(name
, sizeof(name
), "pic", "png", GET16(tag
->data
));
729 filename
= destfilename
;
730 if(!strcmp(filename
,"output.swf"))
731 filename
= "output.png";
735 /* swf jpeg images have two streams, which both start with ff d8 and
736 end with ff d9. The following code handles sorting the middle
737 <ff d9 ff d8> bytes out, so that one stream remains */
738 if(tag
->id
== ST_DEFINEBITSJPEG
&& tag
->len
>2 && has_jpegtables
) {
739 fi
= save_fopen(filename
, "wb");
740 if(jpegtablessize
>=2) {
741 fwrite(jpegtables
, 1, jpegtablessize
-2, fi
); //don't write end tag (ff,d8)
742 fwrite(&tag
->data
[2+2], tag
->len
-2-2, 1, fi
); //don't write start tag (ff,d9)
744 fwrite(tag
->data
+2, tag
->len
-2, 1, fi
);
748 else if(tag
->id
== ST_DEFINEBITSJPEG2
&& tag
->len
>2) {
750 int pos
= findjpegboundary(&tag
->data
[2], tag
->len
-2);
753 fi
= save_fopen(filename
, "wb");
754 fwrite(&tag
->data
[2], pos
-2, 1, fi
);
755 fwrite(&tag
->data
[pos
+4], end
-(pos
+4), 1, fi
);
758 fi
= save_fopen(filename
, "wb");
759 fwrite(&tag
->data
[2], end
-2, 1, fi
);
763 else if(tag
->id
== ST_DEFINEBITSJPEG3
&& tag
->len
>6) {
764 U32 end
= GET32(&tag
->data
[2])+6;
765 int pos
= findjpegboundary(&tag
->data
[6], end
);
766 if(end
>= tag
->len
) {
767 msg("<error> zlib data out of bounds in definebitsjpeg3");
771 /* TODO: do we actually need this? */
772 memmove(&tag
->data
[pos
], &tag
->data
[pos
+4], end
-(pos
+4));
775 unsigned width
=0, height
=0;
776 jpeg_load_from_mem(&tag
->data
[6], end
-6, &image
, &width
, &height
);
778 uLongf datalen
= width
*height
;
779 Bytef
*data
= malloc(datalen
);
781 int error
= uncompress(data
, &datalen
, &tag
->data
[end
], (uLong
)(tag
->len
- end
));
783 fprintf(stderr
, "Zlib error %d\n", error
);
786 int t
, size
= width
*height
;
787 for(t
=0;t
<size
;t
++) {
788 image
[t
*4+0] = data
[t
];
791 png_write(filename
, image
, width
, height
);
795 int id
= GET16(tag
->data
);
796 if (!extractanyids
) {
797 fprintf(stderr
, "Object %d is not a JPEG picture!\n", id
);
805 #ifdef _ZLIB_INCLUDED_
808 static U32
*crc32_table
= 0;
809 static void make_crc32_table(void)
814 crc32_table
= (U32
*)malloc(1024);
816 for (t
= 0; t
< 256; t
++) {
819 for (s
= 0; s
< 8; s
++) {
820 c
= (0xedb88320L
*(c
&1)) ^ (c
>> 1);
825 static inline void png_write_byte(FILE*fi
, U8 byte
)
827 fwrite(&byte
,1,1,fi
);
828 mycrc32
= crc32_table
[(mycrc32
^ byte
) & 0xff] ^ (mycrc32
>> 8);
830 static void png_start_chunk(FILE*fi
, char*type
, int len
)
832 U8 mytype
[4]={0,0,0,0};
833 U32 mylen
= BE_32_TO_NATIVE(len
);
834 memcpy(mytype
,type
,strlen(type
));
835 fwrite(&mylen
, 4, 1, fi
);
837 png_write_byte(fi
,mytype
[0]);
838 png_write_byte(fi
,mytype
[1]);
839 png_write_byte(fi
,mytype
[2]);
840 png_write_byte(fi
,mytype
[3]);
842 static void png_write_bytes(FILE*fi
, U8
*bytes
, int len
)
846 png_write_byte(fi
,bytes
[t
]);
848 static void png_write_dword(FILE*fi
, U32 dword
)
850 png_write_byte(fi
,dword
>>24);
851 png_write_byte(fi
,dword
>>16);
852 png_write_byte(fi
,dword
>>8);
853 png_write_byte(fi
,dword
);
855 static void png_end_chunk(FILE*fi
)
857 U32 tmp
= BE_32_TO_NATIVE((mycrc32
^0xffffffff));
862 /* extract a lossless image (png) out of a tag
863 This routine was originally meant to be a one-pager. I just
864 didn't know png is _that_ much fun. :) -mk
866 int handlelossless(TAG
*tag
)
869 char*filename
= name
;
884 U8 head
[] = {137,80,78,71,13,10,26,10};
886 char alpha
= tag
->id
== ST_DEFINEBITSLOSSLESS2
;
894 if(tag
->id
!= ST_DEFINEBITSLOSSLESS
&&
895 tag
->id
!= ST_DEFINEBITSLOSSLESS2
) {
896 int id
= GET16(tag
->data
);
897 if (!extractanyids
) {
898 fprintf(stderr
, "Object %d is not a PNG picture!\n",id
);
905 format
= swf_GetU8(tag
);
906 if(format
== 3) bpp
= 8;
907 if(format
== 4) bpp
= 16;
908 if(format
== 5) bpp
= 32;
909 if(format
!=3 && format
!=5) {
911 fprintf(stderr
, "Can't handle 16-bit palette images yet (image %d)\n",id
);
913 fprintf(stderr
, "Unknown image type %d in image %d\n", format
, id
);
916 width
= swf_GetU16(tag
);
917 height
= swf_GetU16(tag
);
918 if(format
== 3) cols
= swf_GetU8(tag
) + 1;
919 // this is what format means according to the flash specification. (which is
921 // if(format == 4) cols = swf_GetU16(tag) + 1;
922 // if(format == 5) cols = swf_GetU32(tag) + 1;
925 msg("<verbose> Width %d", width
);
926 msg("<verbose> Height %d", height
);
927 msg("<verbose> Format %d", format
);
928 msg("<verbose> Cols %d", cols
);
929 msg("<verbose> Bpp %d", bpp
);
931 datalen
= (width
*height
*bpp
/8+cols
*8);
936 data
= malloc(datalen
);
937 error
= uncompress (data
, &datalen
, &tag
->data
[tag
->pos
], tag
->len
-tag
->pos
);
938 } while(error
== Z_BUF_ERROR
);
940 fprintf(stderr
, "Zlib error %d (image %d)\n", error
, id
);
943 msg("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen
, (3+alpha
)*cols
);
945 datalen2
= datalen
+16;
946 data2
= malloc(datalen2
);
947 palette
= (RGBA
*)malloc(cols
*sizeof(RGBA
));
949 for(t
=0;t
<cols
;t
++) {
950 palette
[t
].r
= data
[pos
++];
951 palette
[t
].g
= data
[pos
++];
952 palette
[t
].b
= data
[pos
++];
954 palette
[t
].a
= data
[pos
++];
958 prepare_name(name
, sizeof(name
), "pic", "png", id
);
960 filename
= destfilename
;
961 if(!strcmp(filename
,"output.swf"))
962 filename
= "output.png";
964 fi
= save_fopen(filename
, "wb");
965 fwrite(head
,sizeof(head
),1,fi
);
967 png_start_chunk(fi
, "IHDR", 13);
968 png_write_dword(fi
,width
);
969 png_write_dword(fi
,height
);
970 png_write_byte(fi
,8);
972 png_write_byte(fi
,3); //indexed
973 else if(format
== 5 && alpha
==0)
974 png_write_byte(fi
,2); //rgb
975 else if(format
== 5 && alpha
==1)
976 png_write_byte(fi
,6); //rgba
979 png_write_byte(fi
,0); //compression mode
980 png_write_byte(fi
,0); //filter mode
981 png_write_byte(fi
,0); //interlace mode
985 png_start_chunk(fi
, "PLTE", 768);
988 png_write_byte(fi
,palette
[t
].r
);
989 png_write_byte(fi
,palette
[t
].g
);
990 png_write_byte(fi
,palette
[t
].b
);
995 /* write alpha palette */
996 png_start_chunk(fi
, "tRNS", 256);
998 png_write_byte(fi
,palette
[t
].a
);
1006 int srcwidth
= width
* (bpp
/8);
1007 datalen3
= (width
*4+5)*height
;
1008 data3
= (U8
*)malloc(datalen3
);
1009 for(y
=0;y
<height
;y
++)
1011 data3
[pos2
++]=0; //filter type
1014 // 32 bit to 24 bit "conversion"
1015 for(x
=0;x
<width
;x
++) {
1016 data3
[pos2
++]=data
[pos
+1];
1017 data3
[pos2
++]=data
[pos
+2];
1018 data3
[pos2
++]=data
[pos
+3];
1019 pos
+=4; //ignore padding byte
1022 for(x
=0;x
<width
;x
++) {
1023 data3
[pos2
++]=data
[pos
+1];
1024 data3
[pos2
++]=data
[pos
+2];
1025 data3
[pos2
++]=data
[pos
+3];
1026 data3
[pos2
++]=data
[pos
+0]; //alpha
1032 for(x
=0;x
<srcwidth
;x
++)
1033 data3
[pos2
++]=data
[pos
++];
1036 pos
+=((srcwidth
+3)&~3)-srcwidth
; //align
1041 if(compress (data2
, &datalen2
, data3
, datalen3
) != Z_OK
) {
1042 fprintf(stderr
, "zlib error in pic %d\n", id
);
1045 msg("<verbose> Compressed data is %d bytes", datalen2
);
1046 png_start_chunk(fi
, "IDAT", datalen2
);
1047 png_write_bytes(fi
,data2
,datalen2
);
1049 png_start_chunk(fi
, "IEND", 0);
1059 static FILE*mp3file
=0;
1060 void handlesoundstream(TAG
*tag
)
1062 char*filename
= "output.mp3";
1063 if(numextracts
==1) {
1064 filename
= destfilename
;
1065 if(!strcmp(filename
,"output.swf"))
1066 filename
= "output.mp3";
1069 case ST_SOUNDSTREAMHEAD
:
1070 if((tag
->data
[1]&0x30) == 0x20) { //mp3 compression
1071 mp3file
= fopen(filename
, "wb");
1072 msg("<notice> Writing mp3 data to %s",filename
);
1075 msg("<error> Soundstream is not mp3");
1077 case ST_SOUNDSTREAMHEAD2
:
1078 if((tag
->data
[1]&0x30) == 0x20) {//mp3 compression
1079 mp3file
= fopen(filename
, "wb");
1080 msg("<notice> Writing mp3 data to %s",filename
);
1083 msg("<error> Soundstream is not mp3 (2)");
1085 case ST_SOUNDSTREAMBLOCK
:
1087 fwrite(&tag
->data
[4],tag
->len
-4,1,mp3file
);
1092 int handledefinesound(TAG
*tag
)
1097 char*filename
= buf
;
1102 int rate
,bits
,stereo
;
1103 char*rates
[] = {"5500","11025","22050","44100"};
1104 id
= swf_GetU16(tag
); //id
1106 flags
= swf_GetU8(tag
);
1108 rate
= (flags
>>2)&3;
1109 bits
= flags
&2?16:8;
1112 samples
= swf_GetU32(tag
);
1116 if(format
== 2) { // mp3
1117 swf_GetU16(tag
); //numsamples_seek
1119 } else if(format
== 0) { // raw
1120 printf("Sound is RAW, format: %s samples/sec, %d bit, %s\n", rates
[rate
], bits
, stereo
?"stereo":"mono");
1121 // TODO: convert to WAV
1123 } else if(format
== 1) { // adpcm
1124 printf("Sound is ADPCM, format: %s samples/sec, %d bit, %s\n", rates
[rate
], bits
, stereo
?"stereo":"mono");
1125 extension
= "adpcm";
1129 prepare_name(buf
, sizeof(buf
), "sound", extension
, id
);
1130 if(numextracts
==1) {
1131 filename
= destfilename
;
1132 if(!strcmp(filename
,"output.swf")) {
1133 sprintf(buf
, "output.%s", extension
);
1137 fi
= save_fopen(filename
, "wb");
1138 fwrite(&tag
->data
[tag
->pos
], tag
->len
- tag
->pos
, 1, fi
);
1143 int handlebinary(TAG
*tag
) {
1146 char *filename
= buf
;
1147 int len
= tag
->memsize
;
1148 int dx
= 6; // offset to binary data
1149 if (tag
->id
!=ST_DEFINEBINARY
) {
1150 if (!extractanyids
) {
1151 fprintf(stderr
, "Object %d is not a binary entity!\n",
1156 prepare_name(buf
, sizeof(buf
), "binary", "bin", GET16(tag
->data
));
1157 if(numextracts
==1) {
1158 filename
= destfilename
;
1159 if(!strcmp(filename
,"output.swf")) {
1160 sprintf(buf
, "output.bin");
1164 fout
= fopen(filename
, "wb");
1165 fwrite(tag
->data
+dx
,len
-dx
,1,fout
);
1170 int handleembeddedmp3(TAG
*tag
) {
1174 if (tag
->id
!=ST_DEFINESPRITE
) {
1175 if (!extractanyids
) {
1176 fprintf(stderr
, "Object %d is not a sprite entity!\n",
1182 wasFolded
= swf_IsFolded(tag
);
1186 swf_UnFoldSprite(tag
);
1188 while(tag
->id
!= ST_END
) {
1190 if(tag
->id
== ST_SOUNDSTREAMHEAD
||
1191 tag
->id
== ST_SOUNDSTREAMHEAD2
||
1192 tag
->id
== ST_SOUNDSTREAMBLOCK
) {
1193 handlesoundstream(tag
);
1198 swf_FoldSprite(toFold
);
1203 int main (int argc
,char ** argv
)
1212 char listavailable
= 0;
1213 processargs(argc
, argv
);
1215 if(!extractframes
&& !extractids
&& ! extractname
&& !extractjpegids
&& !extractpngids
1216 && !extractmp3
&& !extractsoundids
&& !extractfontids
&& !extractbinaryids
1217 && !extractanyids
&& !extractmp3ids
)
1220 if(!originalplaceobjects
&& movetozero
) {
1221 fprintf(stderr
, "Error: -0 (--movetozero) can only be used in conjunction with -P (--placeobject)\n");
1227 fprintf(stderr
, "You must supply a filename.\n");
1230 initLog(0,-1,0,0,-1, verbose
);
1232 f
= open(filename
,O_RDONLY
|O_BINARY
);
1236 perror("Couldn't open file: ");
1239 if (swf_ReadSWF(f
,&swf
) < 0)
1241 fprintf(stderr
, "%s is not a valid SWF file or contains errors.\n",filename
);
1260 tagused
= (char*)malloc(tagnum
);
1261 memset(tagused
, 0, tagnum
);
1262 memset(used
, 0, 65536);
1263 memset(depths
, 0, 65536);
1268 if(swf_isAllowedSpriteTag(tag
)) {
1270 if(extractframes
&& is_in_range(frame
, extractframes
)) {
1272 if(tag
->id
== ST_PLACEOBJECT
|| tag
->id
== ST_PLACEOBJECT2
) {
1273 depths
[swf_GetDepth(tag
)] = 1;
1275 if(tag
->id
== ST_REMOVEOBJECT
|| tag
->id
== ST_REMOVEOBJECT2
) {
1276 int depth
= swf_GetDepth(tag
);
1279 depths
[swf_GetDepth(tag
)] = 0;
1282 if((tag
->id
== ST_REMOVEOBJECT
|| tag
->id
== ST_REMOVEOBJECT2
) &&
1283 (depths
[swf_GetDepth(tag
)]) && hollow
) {
1285 depths
[swf_GetDepth(tag
)] = 0;
1289 enumerateIDs(tag
, idcallback
);
1291 tagused
[tagnum
] = 1;
1295 if(tag
->id
== ST_SOUNDSTREAMHEAD
||
1296 tag
->id
== ST_SOUNDSTREAMHEAD2
||
1297 tag
->id
== ST_SOUNDSTREAMBLOCK
) {
1299 handlesoundstream(tag
);
1302 if(tag
->id
== ST_JPEGTABLES
) {
1303 handlejpegtables(tag
);
1306 if(swf_isDefiningTag(tag
)) {
1307 int id
= swf_GetDefineID(tag
);
1309 if(extractids
&& is_in_range(id
, extractids
)) {
1313 if(extractfontids
&& is_in_range(id
, extractfontids
)) {
1314 handlefont(&swf
, tag
);
1316 if(extractjpegids
&& is_in_range(id
, extractjpegids
)) {
1319 if(extractsoundids
&& is_in_range(id
, extractsoundids
)) {
1320 handledefinesound(tag
);
1322 if(extractmp3ids
&& is_in_range(id
, extractmp3ids
)) {
1323 handleembeddedmp3(tag
);
1325 if(extractbinaryids
&& is_in_range(id
, extractbinaryids
)) {
1328 #ifdef _ZLIB_INCLUDED_
1329 if(extractpngids
&& is_in_range(id
, extractpngids
)) {
1330 handlelossless(tag
);
1333 if(extractanyids
&& is_in_range(id
, extractanyids
)) {
1334 if (handlefont(&swf
,tag
)) {
1336 } else if (handlejpeg(tag
)) {
1338 } else if (handlebinary(tag
)) {
1340 #ifdef _ZLIB_INCLUDED_
1341 } else if (handlelossless(tag
)) {
1344 } else if (handledefinesound(tag
)) {
1345 // Not sure if sound code checks carefully for type.
1347 } else if (handleembeddedmp3(tag
)) {
1350 printf("#%d not processed\n", id
);
1354 else if (tag
->id
== ST_SETBACKGROUNDCOLOR
) {
1355 mainr
= tag
->data
[0];
1356 maing
= tag
->data
[1];
1357 mainb
= tag
->data
[2];
1359 else if(swf_isPlaceTag(tag
) && tag
->id
!= ST_PLACEOBJECT
) {
1360 char*name
= swf_GetName(tag
);
1361 if(name
&& extractname
&& !strcmp(name
, extractname
)) {
1362 int id
= swf_GetPlaceID(tag
);
1365 if(originalplaceobjects
) {
1366 tagused
[tagnum
] = 1;
1368 depths
[swf_GetDepth(tag
)] = 1;
1369 extractname_id
= id
;
1372 else if(tag
->id
== ST_SHOWFRAME
) {
1375 tagused
[tagnum
] = 1;
1380 if(tag
->id
== ST_DEFINESPRITE
) {
1381 while(tag
->id
!= ST_END
) {
1390 extractTag(&swf
, destfilename
);
1396 msg("<error> Didn't find a soundstream in file");