added HAVE_FREETYPE_FT2BUILD_H.
[swftools.git] / src / swfextract.c
blob9393ff62958efd8240b71e5a7149a6021585ab84
1 /* swfextract.c
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 */
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include "../lib/rfxswf.h"
26 #include "../lib/args.h"
27 #include "../lib/log.h"
28 #ifdef HAVE_ZLIB_H
29 #ifdef HAVE_LIBZ
30 #include "zlib.h"
31 #define _ZLIB_INCLUDED_
32 #endif
33 #endif
35 char * filename = 0;
36 char * destfilename = "output.swf";
37 int verbose = 3;
39 char* extractids = 0;
40 char* extractframes = 0;
41 char* extractjpegids = 0;
42 char* extractfontids = 0;
43 char* extractpngids = 0;
44 char* extractsoundids = 0;
45 char extractmp3 = 0;
47 char* extractname = 0;
49 char hollow = 0;
50 char originalplaceobjects = 0;
52 int numextracts = 0;
54 struct options_t options[] =
56 {"o","output"},
57 {"w","hollow"},
58 {"v","verbose"},
59 {"i","id"},
60 {"j","jpegs"},
61 {"p","pngs"},
62 {"P","placeobject"},
63 {"m","mp3"},
64 {"s","sound"},
65 {"n","name"},
66 {"f","frame"},
67 {"F","font"},
68 {"V","version"},
69 {0,0}
73 int args_callback_option(char*name,char*val)
75 if(!strcmp(name, "V")) {
76 printf("swfextract - part of %s %s\n", PACKAGE, VERSION);
77 exit(0);
79 else if(!strcmp(name, "o")) {
80 destfilename = val;
81 return 1;
83 else if(!strcmp(name, "i")) {
84 extractids = val;
85 numextracts++;
86 if(extractname) {
87 fprintf(stderr, "You can only supply either name or id\n");
88 exit(1);
90 return 1;
92 else if(!strcmp(name, "n")) {
93 extractname = val;
94 numextracts++;
95 if(extractids) {
96 fprintf(stderr, "You can only supply either name or id\n");
97 exit(1);
99 return 1;
101 else if(!strcmp(name, "v")) {
102 verbose ++;
103 return 0;
105 else if(!strcmp(name, "m")) {
106 extractmp3 = 1;
107 numextracts++;
108 return 0;
110 else if(!strcmp(name, "j")) {
111 if(extractjpegids) {
112 fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
113 exit(1);
115 numextracts++;
116 extractjpegids = val;
117 return 1;
119 else if(!strcmp(name, "F")) {
120 if(extractfontids) {
121 fprintf(stderr, "Only one --font argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
122 exit(1);
124 numextracts++;
125 extractfontids = val;
126 return 1;
128 else if(!strcmp(name, "s")) {
129 if(extractsoundids) {
130 fprintf(stderr, "Only one --sound argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
131 exit(1);
133 numextracts++;
134 extractsoundids = val;
135 return 1;
137 #ifdef _ZLIB_INCLUDED_
138 else if(!strcmp(name, "p")) {
139 if(extractpngids) {
140 fprintf(stderr, "Only one --png argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
141 exit(1);
143 numextracts++;
144 extractpngids = val;
145 return 1;
147 #endif
148 else if(!strcmp(name, "f")) {
149 numextracts++;
150 extractframes = val;
151 return 1;
153 else if(!strcmp(name, "P")) {
154 originalplaceobjects = 1;
155 return 0;
157 else if(!strcmp(name, "w")) {
158 hollow = 1;
159 return 0;
161 else {
162 printf("Unknown option: -%s\n", name);
163 exit(1);
166 return 0;
168 int args_callback_longoption(char*name,char*val)
170 return args_long2shortoption(options, name, val);
172 void args_callback_usage(char*name)
174 printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name);
175 printf("\t-v , --verbose\t\t\t Be more verbose\n");
176 printf("\t-o , --output filename\t\t set output filename\n");
177 printf("\t-V , --version\t\t\t Print program version and exit\n\n");
178 printf("SWF Subelement extraction:\n");
179 printf("\t-n , --name name\t\t instance name of the object (SWF Define) to extract\n");
180 printf("\t-i , --id ID\t\t\t ID of the object, shape or movieclip to extract\n");
181 printf("\t-f , --frame frames\t\t frame numbers to extract\n");
182 printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames\n");
183 printf("\t \t\t\t (use with -f)\n");
184 printf("\t-P , --placeobject\t\t\t Insert original placeobject into output file\n");
185 printf("\t \t\t\t (use with -i)\n");
186 printf("SWF Font/Text extraction:\n");
187 printf("\t-F , --font ID\t\t\t Extract font(s)\n");
188 printf("Picture extraction:\n");
189 printf("\t-j , --jpeg ID\t\t\t Extract JPEG picture(s)\n");
190 #ifdef _ZLIB_INCLUDED_
191 printf("\t-p , --pngs ID\t\t\t Extract PNG picture(s)\n");
192 #endif
193 printf("\n");
194 printf("Sound extraction:\n");
195 printf("\t-m , --mp3\t\t\t Extract main mp3 stream\n");
196 printf("\t-s , --sound ID\t\t\t Extract Sound(s)\n");
198 int args_callback_command(char*name,char*val)
200 if(filename) {
201 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
202 filename, name);
204 filename = name;
205 return 0;
208 U8 mainr,maing,mainb;
209 /* 1 = used, not expanded,
210 3 = used, expanded
211 5 = wanted, not expanded
212 7 = wanted, expanded
214 char used[65536];
215 TAG*tags[65536];
216 int changed;
217 char * tagused;
218 int extractname_id = -1;
220 void idcallback(void*data)
222 if(!(used[GET16(data)]&1)) {
223 changed = 1;
224 used[GET16(data)] |= 1;
228 void enumerateIDs(TAG*tag, void(*callback)(void*))
230 /* U8*data;
231 int len = tag->len;
232 if(tag->len>=64) {
233 len += 6;
234 data = (U8*)malloc(len);
235 PUT16(data, (tag->id<<6)+63);
236 *(U8*)&data[2] = tag->len;
237 *(U8*)&data[3] = tag->len>>8;
238 *(U8*)&data[4] = tag->len>>16;
239 *(U8*)&data[5] = tag->len>>24;
240 memcpy(&data[6], tag->data, tag->len);
241 } else {
242 len += 2;
243 data = (U8*)malloc(len);
244 PUT16(data, (tag->id<<6)+tag->len);
245 memcpy(&data[2], tag->data, tag->len);
247 map_ids_mem(data, len, callback);
249 int num = swf_GetNumUsedIDs(tag);
250 int *ptr = malloc(sizeof(int)*num);
251 int t;
252 swf_GetUsedIDs(tag, ptr);
253 for(t=0;t<num;t++)
254 callback(&tag->data[ptr[t]]);
257 void extractTag(SWF*swf, char*filename)
259 SWF newswf;
260 TAG*desttag;
261 TAG*srctag;
262 RGBA rgb;
263 SRECT objectbbox;
264 char sprite;
265 int f;
266 int t;
267 int tagnum;
268 int copy = 0;
269 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
271 newswf.fileVersion = swf->fileVersion;
272 newswf.frameRate = swf->frameRate;
273 newswf.movieSize = swf->movieSize;
275 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
276 desttag = newswf.firstTag;
277 rgb.r = mainr;
278 rgb.g = maing;
279 rgb.b = mainb;
280 swf_SetRGB(desttag,&rgb);
282 swf_GetRect(0, &objectbbox);
284 do {
285 changed = 0;
286 for(t=0;t<65536;t++) {
287 if(used[t] && !(used[t]&2)) {
288 if(tags[t]==0) {
289 msg("<warning> ID %d is referenced, but never defined.", t);
290 } else if(tags[t]->id==ST_DEFINESPRITE) {
291 TAG*tag = tags[t];
292 while(tag->id != ST_END)
294 enumerateIDs(tag, idcallback);
295 tag = tag->next;
298 else
299 enumerateIDs(tags[t], idcallback);
300 used[t] |= 2;
304 while(changed);
306 srctag = swf->firstTag;
307 tagnum = 0;
308 sprite = 0;
309 while(srctag && (srctag->id || sprite)) {
310 int reset = 0;
311 if(!sprite) {
312 copy = 0;
314 if(srctag->id == ST_END) {
315 sprite = 0;
317 if(srctag->id == ST_DEFINESPRITE)
318 sprite = 1;
319 if(swf_isDefiningTag(srctag)) {
320 int id = swf_GetDefineID(srctag);
321 if(used[id]) {
322 SRECT b;
323 copy = 1;
324 b = swf_GetDefineBBox(srctag);
325 swf_ExpandRect2(&objectbbox, &b);
327 } else
328 if (((((srctag->id == ST_PLACEOBJECT || srctag->id == ST_PLACEOBJECT2) && originalplaceobjects)
329 || srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
330 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
331 (tagused[tagnum]))
333 if(copy == 0)
334 reset = 1;
335 copy = 1;
337 if(srctag->id == ST_REMOVEOBJECT) {
338 if(!used[swf_GetPlaceID(srctag)])
339 copy = 0;
342 if(copy) {
343 TAG*ttag = (TAG*)malloc(sizeof(TAG));
344 desttag = swf_InsertTag(desttag, srctag->id);
345 desttag->len = desttag->memsize = srctag->len;
346 desttag->data = malloc(srctag->len);
347 memcpy(desttag->data, srctag->data, srctag->len);
348 if(reset)
349 copy = 0;
352 srctag = srctag->next;
353 tagnum ++;
355 if(!extractframes && !hollow) {
356 if(!originalplaceobjects && (extractids||extractname_id>=0)) {
357 int t;
358 int s=0;
359 if((objectbbox.xmin|objectbbox.ymin|objectbbox.xmax|objectbbox.ymax)!=0)
360 newswf.movieSize = objectbbox;
361 if(extractname_id>=0) {
362 desttag = swf_InsertTag(desttag, ST_PLACEOBJECT2);
363 swf_ObjectPlace(desttag, extractname_id, extractname_id, 0,0,extractname);
364 } else {
365 for(t=0;t<65536;t++) {
366 if(is_in_range(t, extractids)) {
367 desttag = swf_InsertTag(desttag, ST_PLACEOBJECT2);
368 swf_ObjectPlace(desttag, t, t, 0,0,0);
369 s++;
370 if(s==2)
371 printf("warning! You should use the -P when extracting multiple objects\n");
376 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
378 desttag = swf_InsertTag(desttag,ST_END);
380 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT|O_BINARY, 0644);
381 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
382 close(f);
384 swf_FreeTags(&newswf); // cleanup
387 int isOfType(int t, TAG*tag)
389 int show = 0;
390 if(t == 0 && (tag->id == ST_DEFINESHAPE ||
391 tag->id == ST_DEFINESHAPE2 ||
392 tag->id == ST_DEFINESHAPE3)) {
393 show = 1;
395 if(t==1 && tag->id == ST_DEFINESPRITE) {
396 show = 1;
398 if(t == 2 && (tag->id == ST_DEFINEBITS ||
399 tag->id == ST_DEFINEBITSJPEG2 ||
400 tag->id == ST_DEFINEBITSJPEG3)) {
401 show = 1;
403 if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
404 tag->id == ST_DEFINEBITSLOSSLESS2)) {
405 show = 1;
407 if(t == 4 && (tag->id == ST_DEFINESOUND)) {
408 show = 1;
410 if(t == 5 && (tag->id == ST_DEFINEFONT || tag->id == ST_DEFINEFONT2)) {
411 show = 1;
413 return show;
416 void listObjects(SWF*swf)
418 TAG*tag;
419 char first;
420 int t;
421 int frame = 0;
422 char*names[] = {"Shape", "MovieClip", "JPEG", "PNG", "Sound", "Font"};
423 printf("Objects in file %s:\n",filename);
424 swf_FoldAll(swf);
425 for(t=0;t<sizeof(names)/sizeof(names[0]);t++) {
426 int nr=0;
427 int lastid = -2, lastprint=-1;
428 int follow=0;
429 tag = swf->firstTag;
430 first = 1;
431 while(tag) {
432 if(isOfType(t,tag))
433 nr++;
434 tag = tag->next;
436 if(!nr)
437 continue;
439 printf(" %d %s%s: ID(s) ", nr, names[t], nr>1?"s":"");
441 tag = swf->firstTag;
442 while(tag) {
443 char text[80];
444 char show = isOfType(t,tag);
445 int id;
446 if(!show) {
447 tag = tag->next;
448 continue;
450 id = swf_GetDefineID(tag);
452 if(id == lastid+1) {
453 follow=1;
454 } else {
455 if(first || !follow) {
456 if(!first)
457 printf(", ");
458 printf("%d", id);
459 } else {
460 if(lastprint + 1 == lastid)
461 printf(", %d, %d", lastid, id);
462 else
463 printf("-%d, %d", lastid, id);
465 lastprint = id;
466 first = 0;
467 follow = 0;
469 lastid = id;
470 tag=tag->next;
472 if(follow) {
473 if(lastprint + 1 == lastid)
474 printf(", %d", lastid);
475 else
476 printf("-%d", lastid);
478 printf("\n");
481 if(frame)
482 printf(" %d Frames: ID(s) 0-%d\n", frame, frame);
483 else
484 printf(" 1 Frame: ID(s) 0\n");
487 void handlefont(SWF*swf, TAG*tag)
489 SWFFONT* f=0;
490 U16 id;
491 char name[80];
492 char*filename = name;
493 int t;
495 id = swf_GetDefineID(tag);
496 sprintf(name, "font%d.swf", id);
497 if(numextracts==1) {
498 filename = destfilename;
501 swf_FontExtract(swf, id, &f);
502 if(!f) {
503 printf("Couldn't extract font %d\n", id);
504 return;
506 if(!f->layout)
507 swf_FontCreateLayout(f);
509 swf_WriteFont(f, filename);
510 swf_FontFree(f);
513 U8*jpegtables = 0;
514 int jpegtablessize;
516 void handlejpegtables(TAG*tag)
518 if(tag->id == ST_JPEGTABLES) {
519 jpegtables = tag->data;
520 jpegtablessize = tag->len;
524 FILE* save_fopen(char* name, char* mode)
526 FILE*fi = fopen(name, mode);
527 if(!fi) {
528 fprintf(stderr, "Error: Couldn't open %s\n", name);
529 exit(1);
531 return fi;
534 int findjpegboundary(U8*data, int len)
536 int t;
537 int pos=-1;
538 for(t=0;t<len;t++) {
539 if(data[t ]==0xff &&
540 data[t+1]==0xd9 &&
541 data[t+2]==0xff &&
542 data[t+3]==0xd8) {
543 pos = t;
546 return pos;
549 /* extract jpeg data out of a tag */
550 void handlejpeg(TAG*tag)
552 char name[80];
553 char*filename = name;
554 FILE*fi;
556 sprintf(name, "pic%d.jpg", GET16(tag->data));
557 if(numextracts==1) {
558 filename = destfilename;
559 if(!strcmp(filename,"output.swf"))
560 filename = "output.jpg";
562 /* swf jpeg images have two streams, which both start with ff d8 and
563 end with ff d9. The following code handles sorting the middle
564 <ff d9 ff d8> bytes out, so that one stream remains */
565 if(tag->id == ST_DEFINEBITS && tag->len>2 && jpegtables) {
566 fi = save_fopen(filename, "wb");
567 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
568 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
569 fclose(fi);
571 else if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
572 int end = tag->len;
573 int pos = findjpegboundary(&tag->data[2], tag->len-2);
574 if(pos<0)
575 return;
576 pos+=2;
577 fi = save_fopen(filename, "wb");
578 fwrite(&tag->data[2], pos-2, 1, fi);
579 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
580 fclose(fi);
582 else if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
583 U32 end = GET32(&tag->data[2])+6;
584 int pos = findjpegboundary(&tag->data[6], tag->len-6);
585 if(pos<0)
586 return;
587 pos+=6;
588 fi = save_fopen(filename, "wb");
589 fwrite(&tag->data[6], pos-6, 1, fi);
590 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
591 fclose(fi);
593 else {
594 int id = GET16(tag->data);
595 fprintf(stderr, "Object %d is not a JPEG picture!\n",id);
596 exit(1);
600 #ifdef _ZLIB_INCLUDED_
601 static U32 mycrc32;
603 static U32*crc32_table = 0;
604 static void make_crc32_table(void)
606 int t;
607 if(crc32_table)
608 return;
609 crc32_table = (U32*)malloc(1024);
611 for (t = 0; t < 256; t++) {
612 U32 c = t;
613 int s;
614 for (s = 0; s < 8; s++) {
615 c = (0xedb88320L*(c&1)) ^ (c >> 1);
617 crc32_table[t] = c;
620 static inline void png_write_byte(FILE*fi, U8 byte)
622 fwrite(&byte,1,1,fi);
623 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
625 static void png_start_chunk(FILE*fi, char*type, int len)
627 U8 mytype[4]={0,0,0,0};
628 U32 mylen = REVERSESWAP32(len);
629 memcpy(mytype,type,strlen(type));
630 fwrite(&mylen, 4, 1, fi);
631 mycrc32=0xffffffff;
632 png_write_byte(fi,mytype[0]);
633 png_write_byte(fi,mytype[1]);
634 png_write_byte(fi,mytype[2]);
635 png_write_byte(fi,mytype[3]);
637 static void png_write_bytes(FILE*fi, U8*bytes, int len)
639 int t;
640 for(t=0;t<len;t++)
641 png_write_byte(fi,bytes[t]);
643 static void png_write_dword(FILE*fi, U32 dword)
645 png_write_byte(fi,dword>>24);
646 png_write_byte(fi,dword>>16);
647 png_write_byte(fi,dword>>8);
648 png_write_byte(fi,dword);
650 static void png_end_chunk(FILE*fi)
652 U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
653 fwrite(&tmp,4,1,fi);
657 /* extract a lossless image (png) out of a tag
658 This routine was originally meant to be a one-pager. I just
659 didn't know png is _that_ much fun. :) -mk
661 void handlelossless(TAG*tag)
663 char name[80];
664 char*filename = name;
665 FILE*fi;
666 int width, height;
667 int crc;
668 int id;
669 int t;
670 U8 bpp = 1;
671 U8 format;
672 U8 tmp;
673 U8* data=0;
674 U8* data2=0;
675 U8* data3=0;
676 U32 datalen;
677 U32 datalen2;
678 U32 datalen3;
679 U8 head[] = {137,80,78,71,13,10,26,10};
680 int cols;
681 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
682 RGBA* palette;
683 int pos;
684 int error;
685 U32 tmp32;
687 make_crc32_table();
689 if(tag->id != ST_DEFINEBITSLOSSLESS &&
690 tag->id != ST_DEFINEBITSLOSSLESS2) {
691 int id = GET16(tag->data);
692 fprintf(stderr, "Object %d is not a PNG picture!\n",id);
693 exit(1);
696 id =swf_GetU16(tag);
697 format = swf_GetU8(tag);
698 if(format == 3) bpp = 8;
699 if(format == 4) bpp = 16;
700 if(format == 5) bpp = 32;
701 if(format!=3 && format!=5) {
702 if(format==4)
703 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
704 else
705 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
706 return;
708 width = swf_GetU16(tag);
709 height = swf_GetU16(tag);
710 if(format == 3) cols = swf_GetU8(tag) + 1;
711 // this is what format means according to the flash specification. (which is
712 // clearly wrong)
713 // if(format == 4) cols = swf_GetU16(tag) + 1;
714 // if(format == 5) cols = swf_GetU32(tag) + 1;
715 else cols = 0;
717 msg("<verbose> Width %d", width);
718 msg("<verbose> Height %d", height);
719 msg("<verbose> Format %d", format);
720 msg("<verbose> Cols %d", cols);
721 msg("<verbose> Bpp %d", bpp);
723 datalen = (width*height*bpp/8+cols*8);
724 do {
725 if(data)
726 free(data);
727 datalen+=4096;
728 data = malloc(datalen);
729 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
730 } while(error == Z_BUF_ERROR);
731 if(error != Z_OK) {
732 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
733 return;
735 msg("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
736 pos = 0;
737 datalen2 = datalen;
738 data2 = malloc(datalen2);
739 palette = (RGBA*)malloc(cols*sizeof(RGBA));
741 for(t=0;t<cols;t++) {
742 palette[t].r = data[pos++];
743 palette[t].g = data[pos++];
744 palette[t].b = data[pos++];
745 if(alpha) {
746 palette[t].a = data[pos++];
750 sprintf(name, "pic%d.png", id);
751 if(numextracts==1) {
752 filename = destfilename;
753 if(!strcmp(filename,"output.swf"))
754 filename = "output.png";
756 fi = save_fopen(filename, "wb");
757 fwrite(head,sizeof(head),1,fi);
759 png_start_chunk(fi, "IHDR", 13);
760 png_write_dword(fi,width);
761 png_write_dword(fi,height);
762 png_write_byte(fi,8);
763 if(format == 3)
764 png_write_byte(fi,3); //indexed
765 else if(format == 5 && alpha==0)
766 png_write_byte(fi,2); //rgb
767 else if(format == 5 && alpha==1)
768 png_write_byte(fi,6); //rgba
769 else return;
771 png_write_byte(fi,0); //compression mode
772 png_write_byte(fi,0); //filter mode
773 png_write_byte(fi,0); //interlace mode
774 png_end_chunk(fi);
776 if(format == 3) {
777 png_start_chunk(fi, "PLTE", 768);
779 for(t=0;t<256;t++) {
780 png_write_byte(fi,palette[t].r);
781 png_write_byte(fi,palette[t].g);
782 png_write_byte(fi,palette[t].b);
784 png_end_chunk(fi);
787 int pos2 = 0;
788 int x,y;
789 int srcwidth = width * (bpp/8);
790 datalen3 = width*height*4;
791 data3 = (U8*)malloc(datalen3);
792 for(y=0;y<height;y++)
794 data3[pos2++]=0; //filter type
795 if(bpp==32) {
796 if(!alpha) {
797 // 32 bit to 24 bit "conversion"
798 for(x=0;x<width;x++) {
799 data3[pos2++]=data[pos+1];
800 data3[pos2++]=data[pos+2];
801 data3[pos2++]=data[pos+3];
802 pos+=4; //ignore padding byte
804 } else {
805 for(x=0;x<width;x++) {
806 data3[pos2++]=data[pos+1];
807 data3[pos2++]=data[pos+2];
808 data3[pos2++]=data[pos+3];
809 data3[pos2++]=data[pos+0]; //alpha
810 pos+=4;
814 else {
815 for(x=0;x<srcwidth;x++)
816 data3[pos2++]=data[pos++];
819 pos+=((srcwidth+3)&~3)-srcwidth; //align
821 datalen3=pos2;
824 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
825 fprintf(stderr, "zlib error in pic %d\n", id);
826 return;
828 msg("<verbose> Compressed data is %d bytes", datalen2);
829 png_start_chunk(fi, "IDAT", datalen2);
830 png_write_bytes(fi,data2,datalen2);
831 png_end_chunk(fi);
832 png_start_chunk(fi, "IEND", 0);
833 png_end_chunk(fi);
835 free(data);
836 free(data2);
837 free(data3);
839 #endif
841 FILE*mp3file;
842 void handlesoundstream(TAG*tag)
844 char*filename = "output.mp3";
845 if(numextracts==1) {
846 filename = destfilename;
847 if(!strcmp(filename,"output.swf"))
848 filename = "output.mp3";
850 switch(tag->id) {
851 case ST_SOUNDSTREAMHEAD:
852 if((tag->data[1]&0x30) == 0x20) { //mp3 compression
853 mp3file = fopen(filename, "wb");
854 msg("<notice> Writing mp3 data to %s",filename);
856 else
857 msg("<error> Soundstream is not mp3");
858 break;
859 case ST_SOUNDSTREAMHEAD2:
860 if((tag->data[1]&0x30) == 0x20) {//mp3 compression
861 mp3file = fopen(filename, "wb");
862 msg("<notice> Writing mp3 data to %s",filename);
864 else
865 msg("<error> Soundstream is not mp3 (2)");
866 break;
867 case ST_SOUNDSTREAMBLOCK:
868 if(mp3file)
869 fwrite(&tag->data[4],tag->len-4,1,mp3file);
870 break;
874 void handledefinesound(TAG*tag)
876 U8 flags;
877 U32 samples;
878 char buf[128];
879 char*filename = buf;
880 FILE*fi;
881 char*extension = 0;
882 int format;
883 U16 id;
884 int rate,bits,stereo;
885 char*rates[] = {"5500","11025","22050","44100"};
886 id = swf_GetU16(tag); //id
888 flags = swf_GetU8(tag);
889 format = flags>>4;
890 rate = (flags>>2)&3;
891 bits = flags&2?16:8;
892 stereo = flags&1;
894 samples = swf_GetU32(tag);
896 extension = "raw";
898 if(format == 2) { // mp3
899 swf_GetU16(tag); //numsamples_seek
900 extension = "mp3";
901 } else if(format == 0) { // raw
902 printf("Sound is RAW, format: %s samples/sec, %d bit, %s\n", rates[rate], bits, stereo?"stereo":"mono");
903 // TODO: convert to WAV
904 extension = "raw";
905 } else if(format == 1) { // adpcm
906 printf("Sound is ADPCM, format: %s samples/sec, %d bit, %s\n", rates[rate], bits, stereo?"stereo":"mono");
907 extension = "adpcm";
909 sprintf(buf, "sound%d.%s", id, extension);
910 if(numextracts==1) {
911 filename = destfilename;
912 if(!strcmp(filename,"output.swf")) {
913 sprintf(buf, "output.%s", extension);
914 filename = buf;
917 fi = save_fopen(filename, "wb");
918 fwrite(&tag->data[tag->pos], tag->len - tag->pos, 1, fi);
919 fclose(fi);
922 int main (int argc,char ** argv)
924 TAG*tag;
925 SWF swf;
926 int f;
927 int found = 0;
928 int frame = 0;
929 int tagnum = 0;
930 char depths[65536];
931 char listavailable = 0;
932 processargs(argc, argv);
934 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
935 && !extractmp3 && !extractsoundids && !extractfontids)
936 listavailable = 1;
938 if(!filename)
940 fprintf(stderr, "You must supply a filename.\n");
941 return 1;
943 initLog(0,-1,0,0,-1, verbose);
945 f = open(filename,O_RDONLY|O_BINARY);
947 if (f<0)
949 perror("Couldn't open file: ");
950 exit(1);
952 if (swf_ReadSWF(f,&swf) < 0)
954 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
955 close(f);
956 exit(1);
958 close(f);
960 if(listavailable) {
961 listObjects(&swf);
962 swf_FreeTags(&swf);
963 return 0;
966 tag = swf.firstTag;
967 tagnum = 0;
968 while(tag) {
969 tagnum ++;
970 tag = tag->next;
973 tagused = (char*)malloc(tagnum);
974 memset(tagused, 0, tagnum);
975 memset(used, 0, 65536);
976 memset(depths, 0, 65536);
978 tag = swf.firstTag;
979 tagnum = 0;
980 while(tag) {
981 if(swf_isAllowedSpriteTag(tag)) {
982 int write = 0;
983 if(extractframes && is_in_range(frame, extractframes)) {
984 write = 1;
985 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
986 depths[swf_GetDepth(tag)] = 1;
988 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
989 int depth = swf_GetDepth(tag);
990 if(!depths[depth])
991 write = 0;
992 depths[swf_GetDepth(tag)] = 0;
994 } else {
995 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
996 (depths[swf_GetDepth(tag)]) && hollow) {
997 write = 1;
998 depths[swf_GetDepth(tag)] = 0;
1001 if(write) {
1002 enumerateIDs(tag, idcallback);
1003 found = 1;
1004 tagused[tagnum] = 1;
1008 if(tag->id == ST_SOUNDSTREAMHEAD ||
1009 tag->id == ST_SOUNDSTREAMHEAD2 ||
1010 tag->id == ST_SOUNDSTREAMBLOCK) {
1011 if(extractmp3)
1012 handlesoundstream(tag);
1015 if(tag->id == ST_JPEGTABLES)
1016 handlejpegtables(tag);
1018 if(swf_isDefiningTag(tag)) {
1019 int id = swf_GetDefineID(tag);
1020 tags[id] = tag;
1021 if(extractids && is_in_range(id, extractids)) {
1022 used[id] = 5;
1023 found = 1;
1025 if(extractfontids && is_in_range(id, extractfontids)) {
1026 handlefont(&swf, tag);
1028 if(extractjpegids && is_in_range(id, extractjpegids)) {
1029 handlejpeg(tag);
1031 if(extractsoundids && is_in_range(id, extractsoundids)) {
1032 handledefinesound(tag);
1034 #ifdef _ZLIB_INCLUDED_
1035 if(extractpngids && is_in_range(id, extractpngids)) {
1036 handlelossless(tag);
1038 #endif
1040 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
1041 mainr = tag->data[0];
1042 maing = tag->data[1];
1043 mainb = tag->data[2];
1045 else if(tag->id == ST_PLACEOBJECT2) {
1046 char*name = swf_GetName(tag);
1047 if(name && extractname && !strcmp(name, extractname)) {
1048 int id = swf_GetPlaceID(tag);
1049 used[id] = 5;
1050 found = 1;
1051 tagused[tagnum] = 1;
1052 depths[swf_GetDepth(tag)] = 1;
1053 extractname_id = id;
1056 else if(tag->id == ST_SHOWFRAME) {
1057 frame ++;
1058 if(hollow) {
1059 tagused[tagnum] = 1;
1060 found = 1;
1064 if(tag->id == ST_DEFINESPRITE) {
1065 while(tag->id != ST_END) {
1066 tag = tag->next;
1067 tagnum ++;
1070 tag = tag->next;
1071 tagnum ++;
1073 if (found)
1074 extractTag(&swf, destfilename);
1076 if(mp3file)
1077 fclose(mp3file);
1079 swf_FreeTags(&swf);
1080 return 0;