-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdpng2png.cpp
396 lines (340 loc) · 7.69 KB
/
dpng2png.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//by 銀の契约者
//github https://github.com/hz86/filepack
#include <io.h>
#include <tchar.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <Windows.h>
#include <Shlwapi.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib, "Gdiplus.lib")
#pragma comment(lib, "Shlwapi.lib")
typedef struct DPNGHEAD {
unsigned char signature[4];
unsigned int unknown1;
unsigned int entry_count;
unsigned int width;
unsigned int height;
} DPNGHEAD;
typedef struct DPNGENTRY {
unsigned int offset_x;
unsigned int offset_y;
unsigned int width;
unsigned int height;
unsigned int length;
unsigned int unknown1;
unsigned int unknown2;
} DPNGENTRY;
//读入文件
static unsigned char * get_file(wchar_t *file, unsigned int *len)
{
unsigned char *ret = NULL;
FILE *fp = _wfopen(file, L"rb");
if (fp)
{
fseek(fp, 0, SEEK_END);
unsigned int fplen = ftell(fp);
fseek(fp, 0, SEEK_SET);
ret = (unsigned char *)malloc(fplen);
fread(ret, 1, fplen, fp);
*len = fplen;
fclose(fp);
}
return ret;
}
//写出文件
static void put_file(wchar_t *file, unsigned char *data, unsigned int len)
{
wchar_t dir[1024];
wchar_t *pos = file;
while (1)
{
pos = wcschr(pos, '\\');
if (NULL == pos)
{
break;
}
wcsncpy(dir, file, pos - file);
dir[pos - file] = 0;
_wmkdir(dir);
pos++;
}
FILE *fp = _wfopen(file, L"wb");
fwrite(data, 1, len, fp);
fclose(fp);
}
//GDI+
static int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if (size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for (UINT j = 0; j < num; ++j)
{
if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
//转换
static int dpng_to_png(unsigned char *in, unsigned int len, unsigned char **out, unsigned int *outlen)
{
int ret = -1;
ULONG_PTR gdiplusToken;
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
unsigned int pos = 0;
DPNGHEAD *dpng = (DPNGHEAD *)(in + pos);
if (0 == memcmp(dpng->signature, "DPNG", 4))
{
Bitmap* bitmap = new Bitmap(dpng->width, dpng->height, PixelFormat32bppARGB);
Graphics *graphics = new Graphics(bitmap);
pos += sizeof(DPNGHEAD);
for (unsigned int i = 0; i < dpng->entry_count; i++)
{
DPNGENTRY *entry = (DPNGENTRY *)(in + pos);
pos += sizeof(DPNGENTRY);
//wchar_t file[1024];
//swprintf(file, L"%d.png", i);
//put_file(file, in + pos, entry->length);
IStream *entry_stream = SHCreateMemStream(in + pos, entry->length);
Image* image = new Image(entry_stream);
pos += entry->length;
Rect rect;
rect.X = entry->offset_x;
rect.Y = entry->offset_y;
rect.Width = entry->width;
rect.Height = entry->height;
graphics->DrawImage(image, rect);
delete image;
entry_stream->Release();
}
delete graphics;
CLSID format;
GetEncoderClsid(L"image/png", &format);
IStream *out_stream = SHCreateMemStream(NULL, 0);
bitmap->Save(out_stream, &format);
ULARGE_INTEGER stream_len;
LARGE_INTEGER stream_pos = { 0 };
out_stream->Seek(stream_pos, STREAM_SEEK_END, &stream_len);
out_stream->Seek(stream_pos, STREAM_SEEK_SET, NULL);
ULONG readlen;
*out = (unsigned char *)malloc(stream_len.LowPart);
out_stream->Read(*out, stream_len.LowPart, &readlen);
*outlen = readlen;
out_stream->Release();
delete bitmap;
ret = 0;
}
GdiplusShutdown(gdiplusToken);
return ret;
}
//通配符比较
int match_with_asterisk(wchar_t* str1, wchar_t* pattern)
{
if (str1 == NULL) {
return -1;
}
if (pattern == NULL) {
return -1;
}
int len1 = wcslen(str1);
int len2 = wcslen(pattern);
int p1 = 0, p2 = 0;
//用于分段标记,'*'分隔的字符串
int mark = 0;
while (p1 < len1 && p2 < len2)
{
if (pattern[p2] == '?')
{
p1++; p2++;
continue;
}
if (pattern[p2] == '*')
{
/*如果当前是*号,则mark前面一部分已经获得匹配,
*从当前点开始继续下一个块的匹配
*/
p2++;
mark = p2;
continue;
}
if (str1[p1] != pattern[p2])
{
if (p1 == 0 && p2 == 0)
{
/*
* 如果是首字符,特殊处理,不相同即匹配失败
*/
return -1;
}
/*
* pattern: ...*bdef*...
* ^
* mark
* ^
* p2
* ^
* new p2
* str1:.....bdcf...
* ^
* p1
* ^
* new p1
* 如上示意图所示,在比到e和c处不想等
* p2返回到mark处,
* p1需要返回到下一个位置。
* 因为*前已经获得匹配,所以mark打标之前的不需要再比较
*/
p1 -= p2 - mark - 1;
p2 = mark;
continue;
}
/*
* 此处处理相等的情况
*/
p1++;
p2++;
}
if (p2 == len2)
{
if (p1 == len1)
{
/*
* 两个字符串都结束了,说明模式匹配成功
*/
return 0;
}
if (pattern[p2 - 1] == '*')
{
/*
* str1还没有结束,但pattern的最后一个字符是*,所以匹配成功
*
*/
return 0;
}
}
while (p2 < len2)
{
/*
* pattern多出的字符只要有一个不是*,匹配失败
*
*/
if (pattern[p2] != '*') {
return -1;
}
p2++;
}
return -1;
}
//枚举文件
static void enum_file(wchar_t *in_path, wchar_t *findname, wchar_t **out_filename[], int *out_size)
{
wchar_t filename[1024];
wcscat(wcscpy(filename, in_path), L"\\*");
struct _wfinddata64_t data;
intptr_t handle = _wfindfirst64(filename, &data);
if (-1 != handle)
{
do
{
if (0 == wcscmp(L".", data.name) || 0 == wcscmp(L"..", data.name)) {
continue;
}
if (_A_SUBDIR == (data.attrib & _A_SUBDIR)) {
wcscat(wcscat(wcscpy(filename, in_path), L"\\"), data.name);
enum_file(filename, findname, out_filename, out_size);
}
else
{
if (0 == match_with_asterisk(data.name, findname))
{
if (NULL == *out_filename) {
*out_filename = (wchar_t **)malloc(sizeof(wchar_t *) * (*out_size + 1));
}
else {
*out_filename = (wchar_t **)realloc(*out_filename, sizeof(wchar_t *) * (*out_size + 1));
}
(*out_filename)[*out_size] = (wchar_t *)malloc(wcslen(in_path) * 2 + 2 + wcslen(data.name) * 2 + 2);
wcscat(wcscat(wcscpy((*out_filename)[*out_size], in_path), L"\\"), data.name);
(*out_size)++;
}
}
}
while (0 == _tfindnext64(handle, &data));
_findclose(handle);
}
}
int wmain(int argc, wchar_t* argv[])
{
_wsetlocale(LC_ALL, L"chs");
if (1 == argc)
{
wprintf(L"美少女万華鏡 罪と罰の少女 [dpng to png] tool\r\n\r\n");
wprintf(L"help \r\n");
wprintf(L"dpng2png -f in.png out.png #转换单个文件\r\n");
wprintf(L"dpng2png -a ./path #批量转换并覆盖原文件\r\n");
}
else if (3 == argc)
{
if (0 == wcscmp(L"-a", argv[1]))
{
int len = 0;
wchar_t **arr = NULL;
enum_file(argv[2], L"*.png", &arr, &len);
if (arr)
{
for (int i = 0; i < len; i++)
{
wprintf(L"%s\r\n", arr[i] + wcslen(argv[2]));
unsigned int datalen;
unsigned char *data = get_file(arr[i], &datalen);
unsigned char *out;
unsigned int outlen;
if (-1 != dpng_to_png(data, len, &out, &outlen))
{
put_file(arr[i], out, outlen);
free(out);
}
free(data);
}
for (int i = 0; i < len; i++) {
free(arr[i]);
}
free(arr);
}
}
}
else if (4 == argc)
{
if (0 == wcscmp(L"-f", argv[1]))
{
unsigned int len;
unsigned char *data = get_file(argv[2], &len);
unsigned char *out;
unsigned int outlen;
if (-1 != dpng_to_png(data, len, &out, &outlen))
{
put_file(argv[3], out, outlen);
free(out);
}
free(data);
}
}
return 0;
}