-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimageUtil.cpp
530 lines (478 loc) · 12.9 KB
/
imageUtil.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include "stdafx.h"
#include <list>
#include "math.h"
extern "C"
{
#include "png.h"
#include "pngstruct.h"
}
extern void *(__cdecl *noxAlloc)(int Size);
extern void (__cdecl *noxFree)(void *Ptr);
struct imgArrayItemAnimRec
{
int field_0;
void *vbagIdArrPtr;
char vbagIdArrSize;
char field_9;
char isLooped;
char gap_b[1];
int field_C;
};
struct imgArrayItem
{
char imageName[32];
char animDirNameMB[32];
char gap_40[32];
int imageVbagId;// äëÿ ñîáñòâåííûõ êàðòèíîê - òóò óêàçàòåëü
char auxImageType;
char imgFlagsMB;
short field_66;
};
struct imageH /// óêàçàòåëü íà ýòó ñòðóêòóðó âîçâðàùàåò gLoadItem
{
void* dataPtr;
int field_4;
unsigned short vbagId;
// 0x80 çíà÷èò çàãðóæåíî
// 0x40 õç, îñòàëüíûå - òèï èçîáðàæåíèÿ
// 2 - îáû÷íûé 16áèòíûé ðàñòð öâåòîâ
// 3 - RLE ïîïóëÿðíûé ñ ïðîçðà÷íîñòüþ âðîäå (menuBg,BorderCornerUL)
// 7 - 16áèòíûé ðàñòð öâåòîâ ñ ïðåäîáðàáîòêîé, îòêëþ÷åí (íå ìîæåò áûòü íàðèñîâàí)
//
char typeFlags;
char field_B;
};
struct Sprite2
{
int W,H,dX,dY;
byte Some;
};
imgArrayItem **imgArray; //óêàçàòåëü íà ìàññèâ vbag
int *imgArraySize;
int *imgArrayNextId; // ôàêòè÷åñêèé ðàçìåð ìàññèâà vbag (ñîçäàåòñÿ íàìè)
namespace
{
struct ImgVectorRec
{
imgArrayItem *Items;
int Size;
int Used;
ImgVectorRec(int Size_,void *Data):
Items((imgArrayItem *)Data),
Size(Size_),
Used(0)
{
}
};
typedef std::list<ImgVectorRec> ImgVectorList;
ImgVectorList imgVector;
int __cdecl gLoadImgSearchImpl(const char *SearchFor)
{
imgArrayItem *P = *imgArray;
imgArrayItem *E = P + *imgArrayNextId;
for (int i=0;P!=E;P++,i++)
{
if (0==strncmp(P->imageName,SearchFor,sizeof(P->imageName)))
return i;
}
for (ImgVectorList::iterator I=imgVector.begin();I!=imgVector.end();I++)
{
P = I->Items;
E = P + I->Used;
for (int i=0;P!=E;P++,i++)
{
if (0==strncmp(P->imageName,SearchFor,sizeof(P->imageName)))
{
//íàøëè
return P->imageVbagId;
}
}
}
return 0;
}
void __declspec(naked) gLoadImgSearch()
{
__asm
{
push edx
push eax
call gLoadImgSearchImpl
add esp,4
pop edx // êîëè÷åñòâî ñïðàéòîâ â âáàãå
test eax,eax
jz l1
cmp eax,edx
jge l2
mov edi,eax
push 0x0042F9D0
ret
l2:
// ñëó÷àé êîãäà êàñòîì
pop edi
pop esi
pop ebp
pop ebx
ret
l1:
push 0x0042F9C9 // ñëó÷àé êîãäà íå íàéäåíî
ret
}
}
void imgAddImage(const char *Name,void *Data)
{
const int Growth=32;
imgArrayItem *P=0;
for (ImgVectorList::iterator I=imgVector.begin();I!=imgVector.end();I++)
{
if (I->Size>I->Used)
{
P=I->Items + I->Used;
I->Used++; // ñåé÷àñ äîáàâèì
break;
}
}
if (P==NULL) /// íàäî äîáàâèòü íîâûõ
{
imgVector.push_back(
ImgVectorRec(Growth,malloc(Growth*sizeof(imgArrayItem) ))
);
P=imgVector.back().Items;
imgVector.back().Used=1;/// ñåé÷àñ äîáàâèì
}
memset(P,0,sizeof(*P));
strncpy(P->imageName,Name,sizeof(P->imageName));
P->imageVbagId=(int)Data;
}
struct PngData
{
png_structp png_ptr;
png_infop info_ptr;
png_bytep *row_pointers;
int width;
int height;
int color;
};
struct PngReadData
{
void *Buf;
void *Current;
void *End;
PngReadData(void *Ptr,int Size):Buf(Ptr),Current(Ptr),End((char*)Ptr+Size){}
};
bool imgPngStart(PngData *png,PngReadData *readData);
void imgPngFinish(PngData *png);
/// args: srcPath[,imgName=srcPath /*[,imgType=2]*/ ]
int imgLoadImage(lua_State*L)
{
lua_settop(L,3);
if (!lua_isstring(L,1))
{
lua_pushstring(L,"wrong args!");
lua_error_(L);
}
imageH *H= (imageH *)noxAlloc(sizeof(imageH));
const char *imgName=lua_tostring(L,2);
if (imgName==NULL)
imgName=lua_tostring(L,1);
imgAddImage(imgName,H);
Sprite2 *SpritePtr=NULL;
png_structp png_ptr;
png_infop info_ptr;
PngData png;
size_t Size;
void *Data;
if (!fsRead(lua_tostring(L,1),Data,Size))
return 0;
PngReadData readData(Data,Size);
if (!imgPngStart(&png,&readData))
{
return 0;
}
int Width=png.width;
int Height=png.height;
bool useAlpha=(png.color==PNG_COLOR_TYPE_RGBA);
int imgType=(useAlpha?3:2);
int BlockSize=0;
if (useAlpha)
BlockSize=sizeof(Sprite2) + 3 * Width * Height;
// ïðåäïîëàãàþ ÷òî íå áóäåò ÷åðåç îäèí A/C (â õóäøåì ñëó÷àå ðàçìåð áóäåò 6xWxH)
else
BlockSize=sizeof(Sprite2) + 2 * Width * Height ;
H->dataPtr=noxAlloc(BlockSize);
H->typeFlags=0x80 | imgType;// çàãðóæåíî â óêàçàòåëü ôîðìàò, 2
H->vbagId=1;// äëÿ ïðèìåðà
SpritePtr=(Sprite2*)H->dataPtr;
SpritePtr->W=Width;
SpritePtr->H=Height;
SpritePtr->dX=0;
SpritePtr->dY=0;
SpritePtr->Some=0;
short *P=(short*)(((byte*)H->dataPtr)+sizeof(Sprite2));
//#define RGB2(r,g,b) ((((r)&0x1F)<<11)|(((g)&0x3F)<<5)|((b)&0x1F))
#define RGB4(r,g,b,a) ((((r)>>3)<<11)|(((g)>>3)<<6)|(((b)>>3)<<1)|((a?1:0)))
#define RGB3(r,g,b) ((((r)>>3)<<11)|(((g)>>2)<<5)|((b)>>3))
if (useAlpha)
{
for (int j=0;j<Height;j++)
{
png_bytep Src=png.row_pointers[j];
int Counter=0; //ñ÷åò÷èê ïîâòîðîâ
Byte *PP=NULL;
enum EncodeMode
{
emUnk=0,
emSkip=1,
emPlain=2,
emTrans=5,
}State=emUnk;
for (int i=0;i<Width;i++)
{
if (State==emUnk)
{
if (Counter>0 && PP!=NULL)
{
*PP=Counter;
Counter=0;
}
PP=(Byte*)P;
if (Src[3]<0x40)
State=emSkip;
else if ( Src[3]> 0xC0)
State=emPlain;
else
State=emTrans;
*(PP++)=State;
*PP=0;
P++;
i--;
continue;
}
else if (State==emSkip)
{
if (Src[3]<0x40)
{
if ( ++Counter==255)
State=emUnk;
Src+=4;
continue;
}
State=emUnk;
i--;
}
else if (State==emPlain)
{
if (Src[3]>0xC0)
{
if ( ++Counter==255)
State=emUnk;
*(P++)=RGB3(Src[0],Src[1],Src[2]);
Src+=4;
continue;
}
State=emUnk;
i--;
}
else if (State==emTrans)
{
if ( (Src[3]>0x40) && Src[3]<0xC0)
{
if ( ++Counter==255)
State=emUnk;
*(P++)=RGB4(Src[0],Src[1],Src[2],Src[3]);
Src+=4;
continue;
}
State=emUnk;
i--;
}
}
if (Counter>0 && PP!=NULL)
*PP=Counter;
}
}
else
{
for (int j=0;j<Height;j++)
{
png_bytep Src=png.row_pointers[j];
for (int i=0;i<Width;i++)
{
*(P++)=RGB3(Src[0],Src[1],Src[2]);
Src+=3;
}
}
}
imgPngFinish(&png);
lua_pushlightuserdata(L,H);
return 1;
}
void PNGCAPI error_fn(png_structp, png_const_charp S)
{
throw S;
}
void PNGCAPI warn_fn(png_structp, png_const_charp S)
{
}
void PNGCAPI read_fn(png_structp png_ptr, png_bytep Data, png_size_t Size)
{
PngReadData *readData=(PngReadData *)png_ptr->io_ptr;
if (readData==NULL)
return;
if (Size > (char*)readData->End - (char*)readData->Current) // ñëèøêîì ìíîãî, ñòîëüêî íåò
return;
memcpy(Data,readData->Current,Size);
readData->Current=((char*)readData->Current)+Size;
}
void* PNGCAPI pngMyMalloc(png_structp png_ptr, png_size_t Size)
{
return malloc(Size);
}
void pngMyFree(png_structp png_ptr, void *Ptr)
{
return free(Ptr);
}
bool imgPngStart(PngData *png,PngReadData *readData)
{
png_structp &png_ptr=png->png_ptr;
png_infop &info_ptr=png->info_ptr;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
memset(png,0,sizeof(*png));
/* Create and initialize the png_struct with the desired error handler
* functions. If you want to use the default stderr and longjump method,
* you can supply NULL for the last three parameters. We also supply the
* the compiler header file version, so that we know if the application
* was compiled with a compatible version of the library. REQUIRED
*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,&error_fn, &warn_fn);
if (png_ptr == NULL)
{
return (ERROR);
}
try
{
png_set_mem_fn(png_ptr,NULL,pngMyMalloc,pngMyFree);
/* Allocate/initialize the memory for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, NULL, NULL);
return (ERROR);
}
/* Set error handling if you are using the setjmp/longjmp method (this is
* the normal method of doing things with libpng). REQUIRED unless you
* set up your own error handlers in the png_create_read_struct() earlier.
*/
/* if (setjmp(png_jmpbuf(png_ptr)))
{
// Free all of the memory associated with the png_ptr and info_ptr
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
// If we get here, we had a problem reading the file
return (ERROR);
}
_CrtCheckMemory();*/
/* If you are using replacement read functions, instead of calling
* png_init_io() here you would call:
*/
png_set_read_fn(png_ptr, readData, &read_fn);
/* The call to png_read_info() gives us all of the information from the
* PNG file before the first IDAT (image data chunk). REQUIRED
*/
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
png->width=width;
png->height=height;
png->color=color_type;
/* Set up the data transformations you want. Note that these are all
* optional. Only call them if you want/need them. Many of the
* transformations only work on specific types of images, and many
* are mutually exclusive.
*/
/* Tell libpng to strip 16 bit/color files down to 8 bits/color */
png_set_strip_16(png_ptr);
/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
* byte into separate bytes (useful for paletted and grayscale images).
*/
png_set_packing(png_ptr);
/* Expand paletted colors into true RGB triplets */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
/* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
/* Expand paletted or RGB images with transparency to full alpha channels
* so the data will be available as RGBA quartets.
*/
// if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
// png_set_tRNS_to_alpha(png_ptr);
/* Add filler (or alpha) byte (before/after each RGB triplet) */
//png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE);
/* Turn on interlace handling. REQUIRED if you are not using
* png_read_image(). To see how to handle interlacing passes,
* see the png_read_row() method below:
*/
int number_passes = png_set_interlace_handling(png_ptr);
/* Allocate the memory to hold the image using the fields of info_ptr. */
/* The easiest way to read the image: */
png_bytep *row_pointers= (png_bytep *)malloc(sizeof(png_bytep)*height);
/* Clear the pointer array */
for (int row = 0; row < height; row++)
{
row_pointers[row] = (png_bytep)malloc( png_get_rowbytes(png_ptr,info_ptr)); //(png_bytep) png_malloc(png_ptr, png_get_rowbytes(png_ptr,info_ptr));
}
int dataSize=32 + png_get_rowbytes(png_ptr,info_ptr);
png_bytep Data=(png_bytep )malloc( dataSize);
memset(Data,0,dataSize);
// png_read_image(png_ptr, row_pointers);
for (int pass = 0; pass < number_passes; pass++)
{
for (int y = 0; y < height; y++)
{
png_read_row(png_ptr, row_pointers[y] , NULL);//row_pointers[y]
}
}
png->row_pointers=row_pointers;
}
catch(const char *S)
{
_CrtCheckMemory();
conPrintI(S);
}
return 1;
}
void imgPngFinish(PngData *png)
{
png_structp &png_ptr(png->png_ptr);
png_infop &info_ptr(png->info_ptr);
try
{
png_bytep *row_pointers=png->row_pointers;
int height=png->height;
for (int row = 0; row < height; row++)
{
free(row_pointers[row]);
}
free(row_pointers);
/* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
png_read_end(png_ptr, info_ptr);
/* At this point you have read the entire image */
/* Clean up after the read, and free any memory allocated - REQUIRED */
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
}
catch (const char *S)
{
conPrintI(S);
}
memset(png,0,sizeof(*png));
}
}
extern void InjectJumpTo(DWORD Addr,void *Fn);
void ImageUtilInit()
{
ASSIGN(imgArray,0x00694868);
ASSIGN(imgArraySize,0x0069486C );
ASSIGN(imgArrayNextId,0x00694870);
InjectJumpTo(0x0042F982,gLoadImgSearch);
registerclient("imgLoad",imgLoadImage); // ãðóçèòü äèíàìè÷åñêè èçîáðàæåíèå
}