• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javaandroidc++linuxc#windowsobjective-ccocoaqt誰得pythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

swfから画像を抽出するコマンドラインアプリケーション


Commit MetaInfo

Revisionc69b7c88b8e7998bbc28dad36325baeeacde6b30 (tree)
Zeit2016-09-16 00:27:31
Autormasakih <masakih@user...>
Commitermasakih

Log Message

BitsLossless2に対応した

Ändern Zusammenfassung

Diff

--- a/KanColleGraphicDivider/SWFStructure.h
+++ b/KanColleGraphicDivider/SWFStructure.h
@@ -33,6 +33,24 @@ typedef struct _HMSWFBitsJPEG3 {
3333 } HMSWFBitsJPEG3;
3434 #define HMSWFJPEG3HeaderSize 6
3535
36+typedef struct _HMSWFBitLossless2ColorTable {
37+ UInt8 colorTableSize;
38+ unsigned char data;
39+} HMSWFBitLossless2ColorTable;
40+
41+typedef struct _HMSWFBitsLossless2 {
42+ UInt16 charctorID;
43+ UInt8 bitmapFormat;
44+ UInt16 width;
45+ UInt16 height;
46+ union {
47+ HMSWFBitLossless2ColorTable colorTable;
48+ unsigned char data;
49+ } data;
50+} HMSWFBitsLossless2;
51+#define HMSWFLossless2HeaderSize 7
52+#define HMSWFLossless2ColorTableHeaderSize 8
53+
3654 #pragma pack()
3755
3856
--- a/KanColleGraphicDivider/main.m
+++ b/KanColleGraphicDivider/main.m
@@ -10,17 +10,22 @@
1010 #include "SWFStructure.h"
1111 #import "HMZlibData.h"
1212
13+
14+
1315 static NSString *sCurrentDir = nil;
1416 static NSString *sOriginalName = nil;
1517
16-void printLog(const char *fmt, ...) {
1718 #if 0
19+#define printLog(...) printLogF( __VA_ARGS__)
20+void printLogF(const char *fmt, ...) {
1821 va_list ap;
1922 va_start(ap, fmt);
2023 vfprintf(stderr, fmt, ap);
2124 va_end(ap);
22-#endif
2325 }
26+#else
27+#define printLog(...)
28+#endif
2429
2530 void printHex(const unsigned char *p) {
2631 for(int i=0;i<1;i++) {
@@ -48,7 +53,7 @@ void storeImage(const unsigned char *p, UInt32 length, int tagCount) {
4853 [pic writeToURL:url atomically:YES];
4954 }
5055
51-void storeDefineBitsJPEG3(const unsigned char *p, UInt32 length, int tagCount) {
56+void storeBitsJPEG3(const unsigned char *p, UInt32 length, int tagCount) {
5257 printLog("#### TYPE IS PICTURE ####\n\n");
5358 if(length < HMSWFJPEG3HeaderSize) return;
5459
@@ -127,6 +132,122 @@ void storeDefineBitsJPEG3(const unsigned char *p, UInt32 length, int tagCount) {
127132 [imageData writeToURL:url atomically:YES];
128133 }
129134
135+void storeBitLossless2ColorTable(const unsigned char *p, UInt32 length, int tagCount) {
136+ const HMSWFBitsLossless2 *data = (HMSWFBitsLossless2 *)p;
137+
138+ UInt8 mapSize = data->data.colorTable.colorTableSize + 1;
139+ printLog("color table size -> %d\n", mapSize);
140+ printLog("zipped image data size -> %d\n", length - HMSWFLossless2ColorTableHeaderSize);
141+
142+ NSData *zipedContentData = [NSData dataWithBytes:&data->data.colorTable.data length:length - HMSWFLossless2ColorTableHeaderSize];
143+ NSData *contentData = [zipedContentData inflate];
144+ printLog("unzipped image data size -> %d\n", contentData.length);
145+
146+ const UInt32 *mapP = (UInt32 *)contentData.bytes;
147+ const UInt8 *colorIndexP = (UInt8 *)(mapP + mapSize);
148+
149+#if 0
150+ printLog("MAP TABLE\n");
151+ for(int i = 0; i < mapSize; i++) {
152+ printLog("0x%04x ", mapP[i]);
153+ }
154+ printLog("\n\n");
155+#endif
156+
157+ // rowサイズは4bytesアライメント
158+ UInt8 skipBytes = data->width % 4;
159+
160+ // ARBGカラーマップからARBGビットマップを作成
161+ UInt32 *imageDataP = calloc(8 * 4, data->width * data->height);
162+ if(!imageDataP) {
163+ fprintf(stderr, "Can not allocate enough memory.\n");
164+ return;
165+ }
166+
167+ UInt32 *imageDataPixel = imageDataP;
168+ for(UInt16 h = 0; h < data->height; h++) {
169+ for(UInt16 w = 0; w < data->width; w++) {
170+ printLog("%d ", *colorIndexP);
171+ *imageDataPixel++ = mapP[*colorIndexP++];
172+ }
173+ colorIndexP += skipBytes;
174+ printLog("\n");
175+ }
176+
177+ // ARGBビットマップからNSBitmapImageRepを作成
178+ NSData *imageData = [NSData dataWithBytes:imageDataP length:8 * 4 * data->width * data->height];
179+ unsigned char *pp = (unsigned char *)imageData.bytes;
180+ NSBitmapImageRep *imageRef = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&pp
181+ pixelsWide:data->width
182+ pixelsHigh:data->height
183+ bitsPerSample:8
184+ samplesPerPixel:4
185+ hasAlpha:YES
186+ isPlanar:NO
187+ colorSpaceName:NSCalibratedRGBColorSpace
188+ bytesPerRow:data->width * 4
189+ bitsPerPixel:0];
190+ // PNGで保存
191+ NSData *tiffData = imageRef.TIFFRepresentation;
192+ if(!tiffData) {
193+ fprintf(stderr, "Can not create tiff image.\n");
194+ return;
195+ }
196+
197+ NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithData:tiffData];
198+ NSData *pndData = [rep representationUsingType:NSPNGFileType
199+ properties:@{}];
200+ NSString *path = [NSString stringWithFormat:@"%@-%d.png", sOriginalName, tagCount];
201+ path = [sCurrentDir stringByAppendingPathComponent:path];
202+ NSURL *url = [NSURL fileURLWithPath:path];
203+ [pndData writeToURL:url atomically:YES];
204+}
205+void storeBitsLossless2(const unsigned char *p, UInt32 length, int tagCount) {
206+ printLog("#### TYPE IS PICTURE ####\n\n");
207+ if(length < HMSWFLossless2HeaderSize) {
208+ fprintf(stderr, "length is too short.\n");
209+ return;
210+ }
211+
212+ const HMSWFBitsLossless2 *data = (HMSWFBitsLossless2 *)p;
213+
214+ if(data->bitmapFormat == 3) {
215+ storeBitLossless2ColorTable(p, length, tagCount);
216+ return;
217+ }
218+
219+ length -= HMSWFLossless2HeaderSize;
220+
221+ p = &data->data.data;
222+ NSData *zipedImageData = [NSData dataWithBytes:p length:length];
223+ NSData *imageData = [zipedImageData inflate];
224+ unsigned char *pp = (unsigned char *)imageData.bytes;
225+ NSBitmapImageRep *imageRef = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&pp
226+ pixelsWide:data->width
227+ pixelsHigh:data->height
228+ bitsPerSample:8 * 4
229+ samplesPerPixel:4
230+ hasAlpha:YES
231+ isPlanar:NO
232+ colorSpaceName:NSCalibratedRGBColorSpace
233+ bytesPerRow:data->width * 4
234+ bitsPerPixel:0];
235+ // PNGで保存
236+ NSData *tiffData = imageRef.TIFFRepresentation;
237+ if(!tiffData) {
238+ fprintf(stderr, "Can not create tiff image.\n");
239+ return;
240+ }
241+
242+ NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithData:tiffData];
243+ NSData *pndData = [rep representationUsingType:NSPNGFileType
244+ properties:@{}];
245+ NSString *path = [NSString stringWithFormat:@"%@-%d.png", sOriginalName, tagCount];
246+ path = [sCurrentDir stringByAppendingPathComponent:path];
247+ NSURL *url = [NSURL fileURLWithPath:path];
248+ [pndData writeToURL:url atomically:YES];
249+}
250+
130251 int main(int argc, const char * argv[]) {
131252 @autoreleasepool {
132253 NSProcessInfo *pInfo = [NSProcessInfo processInfo];
@@ -178,6 +299,7 @@ int main(int argc, const char * argv[]) {
178299 printLog("size -> %u (%x)\n", size, size);
179300 int offset = size * 4;
180301 offset += 5; // 上位5bit分
302+ // bit -> byte
181303 int div = offset / 8;
182304 int mod = offset % 8;
183305 offset = div + (mod == 0 ? 0 : 1); // アライメント
@@ -185,15 +307,11 @@ int main(int argc, const char * argv[]) {
185307 p += offset;
186308
187309 // fps: 8.8 fixed number.
188- UInt8 fpsE = *(UInt8 *)p;
189- p += 1;
190- UInt8 fps = *(UInt8 *)p;
191- p += 1;
192- printLog("fps -> %u.%u\n", fps, fpsE);
310+ printLog("fps -> %u.%u\n", *(UInt8 *)(p + 1), *(UInt8 *)p);
311+ p += 2;
193312
194313 // frame count
195- UInt16 frameCount = *(UInt16 *)p;
196- printLog("frame count -> %u\n", frameCount);
314+ printLog("frame count -> %u\n", *(UInt16 *)p);
197315 p += 2;
198316
199317 // タグの処理開始
@@ -224,13 +342,17 @@ int main(int argc, const char * argv[]) {
224342 break;
225343 case 35:
226344 @autoreleasepool {
227- storeDefineBitsJPEG3(p, length, tagCount);
345+ storeBitsJPEG3(p, length, tagCount);
346+ }
347+ break;
348+ case 36:
349+ @autoreleasepool {
350+ storeBitsLossless2(p, length, tagCount);
228351 }
229352 break;
230353 case 8:
231354 case 21:
232355 case 20:
233- case 36:
234356 case 90:
235357 @autoreleasepool {
236358 storeImage(p, length, tagCount);