-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphicsLoaderIntf.cpp
More file actions
2248 lines (2003 loc) · 62 KB
/
GraphicsLoaderIntf.cpp
File metadata and controls
2248 lines (2003 loc) · 62 KB
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------------
/*
TVP2 ( T Visual Presenter 2 ) A script authoring tool
Copyright (C) 2000-2007 W.Dee <dee@kikyou.info> and contributors
See details of license at "license.txt"
*/
//---------------------------------------------------------------------------
// Graphics Loader ( loads graphic format from storage )
//---------------------------------------------------------------------------
#include <stdlib.h>
#include "hook_init.h"
#include "GraphicsLoaderIntf.h"
#include "LoadTLG.h"
#include <assert.h>
#include "ncbind.hpp"
#include <memory>
//---------------------------------------------------------------------------
struct VCLCallRouter {
void *callbackdata;
tTVPMetaInfoPushCallbackVCL metainfopushcallback;
tTVPGraphicSizeCallbackVCL sizecallback;
tTVPGraphicScanLineCallbackVCL scanlinecallback;
VCLCallRouter(
void * data,
tTVPGraphicSizeCallbackVCL f1,
tTVPGraphicScanLineCallbackVCL f2,
tTVPMetaInfoPushCallbackVCL f3)
: callbackdata(data)
, sizecallback(f1)
, scanlinecallback(f2)
, metainfopushcallback(f3)
{}
};
static __declspec(naked) void *VCLCallRouter_ScanLineCallback(void *callbackdata, tjs_int y) {
__asm {
mov ecx, [esp + 4]
mov edx, [esp + 8]
mov eax, [ecx]
call[ecx + 0Ch]
retn
}
}
// static void *VCLCallRouter_ScanLineCallback(void *callbackdata, tjs_int y) {
// VCLCallRouter* router = (VCLCallRouter*)callbackdata;
// return router->scanlinecallback(y, router->callbackdata);
// }
static void __declspec(naked) VCLCallRouter_SizeCallback(void *callbackdata, tjs_uint w, tjs_uint h) {
__asm {
push ebp
mov ebp, esp
push ebx
mov ebx, [ebp + 8]
mov edx, [ebp + 0Ch]
mov ecx, [ebp + 10h]
mov eax, [ebx]
call [ebx + 8]
pop ebx
pop ebp
retn
}
}
// static void VCLCallRouter_SizeCallback(void *callbackdata, tjs_uint w, tjs_uint h) {
// VCLCallRouter* router = (VCLCallRouter*)callbackdata;
// return router->sizecallback(h, w, router->callbackdata);
// }
static void __declspec(naked) VCLCallRouter_MetaInfoPushCallback(void *callbackdata, const ttstr & name, const ttstr & value) {
__asm {
push ebp
mov ebp, esp
push ebx
mov ebx, [ebp + 8]
mov edx, [ebp + 0Ch]
mov ecx, [ebp + 10h]
mov eax, [ebx]
call [ebx + 4]
pop ebx
pop ebp
retn
}
}
// static void VCLCallRouter_MetaInfoPushCallback(void *callbackdata, const ttstr & name, const ttstr & value) {
// VCLCallRouter* router = (VCLCallRouter*)callbackdata;
// return router->metainfopushcallback(value, name, router->callbackdata);
// }
static void* PNGHandler;
static void* BMPHandler;
static void* TLGHandler;
static void* JPGHandler;
static void* TVPInternalLoadGraphicRoute(
tTVPGraphicSizeCallback sizecallback,
void *callbackdata,
tTVPGraphicLoadMode mode,
tjs_int32 keyidx,
tTJSBinaryStream *src,
tTVPMetaInfoPushCallback metainfopushcallback,
tTVPGraphicScanLineCallback scanlinecallback
//,void* formatdata
)
{
tjs_uint64 origSrcPos = src->GetPosition();
tjs_uint8 magic[16] = {0};
src->ReadBuffer(magic, sizeof(magic));
src->SetPosition(origSrcPos);
#define CALL_LOAD_FUNC_AND_RET(f) f(nullptr/*formatdata*/, callbackdata, sizecallback, scanlinecallback, metainfopushcallback, src, keyidx, mode);
//#define CALL_HANDLER_AND_RET(f) f(sizecallback, callbackdata, mode, keyidx, src, metainfopushcallback, scanlinecallback); return;
if(magic[0] == 'B' && magic[1] == 'M') {
/*CALL_HANDLER_AND_RET*/return (BMPHandler);
//CALL_LOAD_FUNC_AND_RET(TVPLoadBMP);
} else if(
magic[0] == 0x89 &&
magic[1] == 'P' &&
magic[2] == 'N' &&
magic[3] == 'G'
) {
/*CALL_HANDLER_AND_RET*/return (PNGHandler);
//CALL_LOAD_FUNC_AND_RET(TVPLoadPNG);
} else if(
magic[0] == 'T' &&
magic[1] == 'L' &&
magic[2] == 'G'
) {
//if (mode == glmGrayscale) {
CALL_LOAD_FUNC_AND_RET(TVPLoadTLG);
// } else {
// CALL_HANDLER_AND_RET(TLGHandler);
// }
//CALL_LOAD_FUNC_AND_RET(TVPLoadTLG);
} else if (
magic[0] == 'B' &&
magic[1] == 'P' &&
magic[2] == 'G'){
CALL_LOAD_FUNC_AND_RET(TVPLoadBPG);
} else if(
magic[0] == 0xFF &&
magic[1] == 0xD8 &&
magic[2] == 0xFF &&
magic[3] >= 0xE0 && magic[3] <= 0xEF
) {
/*CALL_HANDLER_AND_RET*/ return (JPGHandler);
//CALL_LOAD_FUNC_AND_RET(TVPLoadJPEG);
} else if (!memcmp(magic, "RIFF", 4) && !memcmp(magic + 8, "WEBPVP8", 7)){
CALL_LOAD_FUNC_AND_RET(TVPLoadWEBP);
} else {
TVPThrowExceptionMessage(
TJS_W("Unsupported in-built graphic format."));
}
#undef CALL_LOAD_FUNC_AND_RET
return nullptr;
}
static void __fastcall TVPLoadGraphicRouteVCL(
tTVPGraphicSizeCallbackVCL sizecallback,
void *callbackdata,
tTVPGraphicLoadMode mode,
tjs_int32 keyidx,
tTJSBinaryStream *src,
tTVPMetaInfoPushCallbackVCL metainfopushcallback,
tTVPGraphicScanLineCallbackVCL scanlinecallback
//,void* formatdata
) {
VCLCallRouter router(callbackdata, sizecallback, scanlinecallback, metainfopushcallback);
void *handler =
TVPInternalLoadGraphicRoute(
VCLCallRouter_SizeCallback, &router, mode, keyidx, src,
VCLCallRouter_MetaInfoPushCallback, VCLCallRouter_ScanLineCallback
);
if (handler) {
((tTVPGraphicLoadingHandlerVCL)handler)(sizecallback, callbackdata,
mode, keyidx, src, metainfopushcallback, scanlinecallback);
}
}
static void __cdecl TVPLoadGraphicRouteCdecl(void* formatdata,
void *callbackdata,
tTVPGraphicSizeCallback sizecallback,
tTVPGraphicScanLineCallback scanlinecallback,
tTVPMetaInfoPushCallback metainfopushcallback,
tTJSBinaryStream *src,
tjs_int32 keyidx,
tTVPGraphicLoadMode mode)
{
void *handler =
TVPInternalLoadGraphicRoute(
sizecallback, callbackdata, mode, keyidx, src,
metainfopushcallback, scanlinecallback
);
if (handler) {
((tTVPGraphicLoadingHandler)handler)(formatdata, callbackdata,
sizecallback, scanlinecallback, metainfopushcallback, src, keyidx, mode);
}
}
static void *_TVPLoadGraphicRouteFunc = nullptr;
static __declspec(naked) void __fastcall TVPLoadGraphicRoute(
tTVPGraphicSizeCallbackVCL sizecallback,
void *callbackdata,
tTVPGraphicLoadMode mode,
tjs_int32 keyidx,
tTJSBinaryStream *src,
tTVPMetaInfoPushCallbackVCL metainfopushcallback,
tTVPGraphicScanLineCallbackVCL scanlinecallback
//,void* formatdata
) {
__asm {
cmp _TVPLoadGraphicRouteFunc, 0;
je InitLoadGraphicRoute;
jmp _TVPLoadGraphicRouteFunc;
InitLoadGraphicRoute:
cmp dword ptr[esp + 08h], 0FFFFFFFFh;
je UseVCLFunc;
UseCdeclFunc:
mov _TVPLoadGraphicRouteFunc, offset TVPLoadGraphicRouteCdecl;
jmp _TVPLoadGraphicRouteFunc;
UseVCLFunc:
mov _TVPLoadGraphicRouteFunc, offset TVPLoadGraphicRouteVCL;
jmp _TVPLoadGraphicRouteFunc;
}
}
// static __declspec(naked) void TVPLoadGraphicRoute(
// void* formatdata,
// void *callbackdata,
// tTVPGraphicSizeCallback sizecallback,
// tTVPGraphicScanLineCallback scanlinecallback,
// tTVPMetaInfoPushCallback metainfopushcallback,
// tTJSBinaryStream *src,
// tjs_int32 keyidx,
// tTVPGraphicLoadMode mode)
// {
// __asm {
// push dword ptr[esp + 04h]
// push dword ptr[esp + 0Ch]
// push dword ptr[esp + 14h]
// push dword ptr[esp + 1Ch]
// push dword ptr[esp + 24h]
// push ecx
// push edx
// push eax
// call _TVPLoadGraphicRoute
// retn 14h
// }
// }
//---------------------------------------------------------------------------
// Graphics Format Management
//---------------------------------------------------------------------------
void InstallGraphicType(tTJSHashTable<ttstr, tTVPGraphicHandlerType> *Hash) {
PNGHandler = Hash->Find(TJS_W(".png"))->Handler;
BMPHandler = Hash->Find(TJS_W(".bmp"))->Handler;
JPGHandler = Hash->Find(TJS_W(".jpg"))->Handler;
TLGHandler = Hash->Find(TJS_W(".tlg"))->Handler;
// register some native-supported formats
Hash->Clear();
#define ADD(ext, func, p) Hash->Add(ext, tTVPGraphicHandlerType(\
ext, func, p));
ADD(
TJS_W(".dib"), TVPLoadGraphicRoute, NULL);
ADD(
TJS_W(".jpeg"), TVPLoadGraphicRoute, NULL);
ADD(
TJS_W(".jif"), TVPLoadGraphicRoute, NULL);
// ADD(
// TJS_W(".eri"), TVPLoadERI, NULL);
ADD(
TJS_W(".tlg5"), TVPLoadGraphicRoute, NULL);
ADD(
TJS_W(".tlg6"), TVPLoadGraphicRoute, NULL);
ADD(
TJS_W(".webp"), TVPLoadGraphicRoute, NULL);
ADD(
TJS_W(".bmp"), TVPLoadGraphicRoute, NULL);
ADD(
TJS_W(".jpg"), TVPLoadGraphicRoute, NULL);
ADD(
TJS_W(".tlg"), TVPLoadGraphicRoute, NULL);
ADD(
TJS_W(".png"), TVPLoadGraphicRoute, NULL);
}
/*
loading handlers return whether the image contains an alpha channel.
*/
#define TVP_REVRGB(v) ((v & 0xFF00FF00) | ((v >> 16) & 0xFF) | ((v & 0xFF) << 16))
static void TVPReverseRGB(tjs_uint32 *src, unsigned int len)
{
tjs_uint32 *dest = src + len;
while (src < dest) {
tjs_uint32 d = *src;
*src = TVP_REVRGB(d);
++src;
}
}
//---------------------------------------------------------------------------
// BMP loading handler
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#define TVP_BMP_READ_LINE_MAX 8
void TVPInternalLoadBMP(void *callbackdata,
tTVPGraphicSizeCallback sizecallback,
tTVPGraphicScanLineCallback scanlinecallback,
TVP_WIN_BITMAPINFOHEADER &bi,
const tjs_uint8 *palsrc,
tTJSBinaryStream * src,
tjs_int keyidx,
tTVPBMPAlphaType alphatype,
tTVPGraphicLoadMode mode)
{
// mostly taken ( but totally re-written ) from SDL,
// http://www.libsdl.org/
// TODO: only checked on Win32 platform
if (bi.biSize == 12)
{
// OS/2
bi.biCompression = BI_RGB;
bi.biClrUsed = 1 << bi.biBitCount;
}
tjs_uint16 orgbitcount = bi.biBitCount;
if (bi.biBitCount == 1 || bi.biBitCount == 4)
{
bi.biBitCount = 8;
}
switch (bi.biCompression)
{
case BI_RGB:
// if there are no masks, use the defaults
break; // use default
/*
if( bf.bfOffBits == ( 14 + bi.biSize) )
{
}
// fall through -- read the RGB masks
*/
#define TVPImageLoadError TJS_W("Read Image Error/%1")
case BI_BITFIELDS:
TVPThrowExceptionMessage(TVPImageLoadError, TJS_W("bit fields not supported"));
default:
TVPThrowExceptionMessage(TVPImageLoadError, TJS_W("copmresssed bmp not supported"));
}
// load palette
tjs_uint32 palette[256]; // (msb) argb (lsb)
if (orgbitcount <= 8)
{
if (bi.biClrUsed == 0) bi.biClrUsed = 1 << orgbitcount;
if (bi.biSize == 12)
{
// read OS/2 palette
for (tjs_uint i = 0; i < bi.biClrUsed; i++)
{
palette[i] = palsrc[0] + (palsrc[1] << 8) + (palsrc[2] << 16) +
0xff000000;
palsrc += 3;
}
} else
{
// read Windows palette
for (tjs_uint i = 0; i<bi.biClrUsed; i++)
{
palette[i] = palsrc[0] + (palsrc[1] << 8) + (palsrc[2] << 16) +
0xff000000;
// we assume here that the palette's unused segment is useless.
// fill it with 0xff ( = completely opaque )
palsrc += 4;
}
}
if (mode == glmGrayscale)
{
TVPDoGrayScale(palette, 256);
}
if (keyidx != -1)
{
// if color key by palette index is specified
palette[keyidx & 0xff] &= 0x00ffffff; // make keyidx transparent
}
} else
{
if (mode == glmPalettized)
TVPThrowExceptionMessage(TVPImageLoadError, TJS_W("unsupported color mode for palett image"));
}
tjs_int height;
height = bi.biHeight<0 ? -bi.biHeight : bi.biHeight;
// positive value of bi.biHeight indicates top-down DIB
sizecallback(callbackdata, bi.biWidth, height);
tjs_int pitch;
pitch = (((bi.biWidth * orgbitcount) + 31) & ~31) / 8;
tjs_uint8 *readbuf = (tjs_uint8 *)TJSAlignedAlloc(pitch * TVP_BMP_READ_LINE_MAX, 4);
tjs_uint8 *buf;
tjs_int bufremain = 0;
try
{
// process per a line
tjs_int src_y = 0;
tjs_int dest_y;
if (bi.biHeight>0) dest_y = bi.biHeight - 1; else dest_y = 0;
for (; src_y < height; src_y++)
{
if (bufremain == 0)
{
tjs_int remain = height - src_y;
tjs_int read_lines = remain > TVP_BMP_READ_LINE_MAX ?
TVP_BMP_READ_LINE_MAX : remain;
src->ReadBuffer(readbuf, pitch * read_lines);
bufremain = read_lines;
buf = readbuf;
}
void *scanline = scanlinecallback(callbackdata, dest_y);
if (!scanline) break;
switch (orgbitcount)
{
// convert pixel format
case 1:
if (mode == glmPalettized)
{
TVPBLExpand1BitTo8Bit(
(tjs_uint8*)scanline,
(tjs_uint8*)buf, bi.biWidth);
} else if (mode == glmGrayscale)
{
TVPBLExpand1BitTo8BitPal(
(tjs_uint8*)scanline,
(tjs_uint8*)buf, bi.biWidth, palette);
} else
{
TVPBLExpand1BitTo32BitPal(
(tjs_uint32*)scanline,
(tjs_uint8*)buf, bi.biWidth, palette);
}
break;
case 4:
if (mode == glmPalettized)
{
TVPBLExpand4BitTo8Bit(
(tjs_uint8*)scanline,
(tjs_uint8*)buf, bi.biWidth);
} else if (mode == glmGrayscale)
{
TVPBLExpand4BitTo8BitPal(
(tjs_uint8*)scanline,
(tjs_uint8*)buf, bi.biWidth, palette);
} else
{
TVPBLExpand4BitTo32BitPal(
(tjs_uint32*)scanline,
(tjs_uint8*)buf, bi.biWidth, palette);
}
break;
case 8:
if (mode == glmPalettized)
{
// intact copy
memcpy(scanline, buf, bi.biWidth);
} else
if (mode == glmGrayscale)
{
// convert to grayscale
TVPBLExpand8BitTo8BitPal(
(tjs_uint8*)scanline,
(tjs_uint8*)buf, bi.biWidth, palette);
} else
{
TVPBLExpand8BitTo32BitPal(
(tjs_uint32*)scanline,
(tjs_uint8*)buf, bi.biWidth, palette);
}
break;
case 15:
case 16:
if (mode == glmGrayscale)
{
TVPBLConvert15BitTo8Bit(
(tjs_uint8*)scanline,
(tjs_uint16*)buf, bi.biWidth);
} else
{
TVPBLConvert15BitTo32Bit(
(tjs_uint32*)scanline,
(tjs_uint16*)buf, bi.biWidth);
}
break;
case 24:
if (mode == glmGrayscale)
{
TVPBLConvert24BitTo8Bit(
(tjs_uint8*)scanline,
(tjs_uint8*)buf, bi.biWidth);
} else
{
TVPBLConvert24BitTo32Bit(
(tjs_uint32*)scanline,
(tjs_uint8*)buf, bi.biWidth);
}
break;
case 32:
if (mode == glmGrayscale)
{
TVPBLConvert32BitTo8Bit(
(tjs_uint8*)scanline,
(tjs_uint32*)buf, bi.biWidth);
} else
{
if (alphatype == batNone)
{
// alpha channel is not given by the bitmap.
// destination alpha is filled with 255.
TVPBLConvert32BitTo32Bit_NoneAlpha(
(tjs_uint32*)scanline,
(tjs_uint32*)buf, bi.biWidth);
} else if (alphatype == batMulAlpha)
{
// this is the TVP native representation of the alpha channel.
// simply copy from the buffer.
TVPBLConvert32BitTo32Bit_MulAddAlpha(
(tjs_uint32*)scanline,
(tjs_uint32*)buf, bi.biWidth);
} else if (alphatype == batAddAlpha)
{
// this is alternate representation of the alpha channel,
// this must be converted to TVP native representation.
TVPBLConvert32BitTo32Bit_AddAlpha(
(tjs_uint32*)scanline,
(tjs_uint32*)buf, bi.biWidth);
}
}
break;
}
scanlinecallback(callbackdata, -1); // image was written
if (bi.biHeight>0) dest_y--; else dest_y++;
buf += pitch;
bufremain--;
}
}
catch (...)
{
TJSAlignedDealloc(readbuf);
throw;
}
TJSAlignedDealloc(readbuf);
}
//---------------------------------------------------------------------------
void TVPLoadBMP(void* formatdata, void *callbackdata, tTVPGraphicSizeCallback sizecallback,
tTVPGraphicScanLineCallback scanlinecallback, tTVPMetaInfoPushCallback metainfopushcallback,
tTJSBinaryStream *src, tjs_int keyidx, tTVPGraphicLoadMode mode)
{
// Windows BMP Loader
// mostly taken ( but totally re-written ) from SDL,
// http://www.libsdl.org/
// TODO: only checked in Win32 platform
tjs_uint64 firstpos = src->GetPosition();
// check the magic
tjs_uint8 magic[2];
src->ReadBuffer(magic, 2);
if (magic[0] != TJS_N('B') || magic[1] != TJS_N('M'))
TVPThrowExceptionMessage(TVPImageLoadError, TJS_W("not windows bmp"));
// read the BITMAPFILEHEADER
TVP_WIN_BITMAPFILEHEADER bf;
bf.bfSize = src->ReadI32LE();
bf.bfReserved1 = src->ReadI16LE();
bf.bfReserved2 = src->ReadI16LE();
bf.bfOffBits = src->ReadI32LE();
// read the BITMAPINFOHEADER
TVP_WIN_BITMAPINFOHEADER bi;
bi.biSize = src->ReadI32LE();
if (bi.biSize == 12)
{
// OS/2 Bitmap
memset(&bi, 0, sizeof(bi));
bi.biWidth = (tjs_uint32)src->ReadI16LE();
bi.biHeight = (tjs_uint32)src->ReadI16LE();
bi.biPlanes = src->ReadI16LE();
bi.biBitCount = src->ReadI16LE();
bi.biClrUsed = 1 << bi.biBitCount;
} else if (bi.biSize == 40)
{
// Windows Bitmap
bi.biWidth = src->ReadI32LE();
bi.biHeight = src->ReadI32LE();
bi.biPlanes = src->ReadI16LE();
bi.biBitCount = src->ReadI16LE();
bi.biCompression = src->ReadI32LE();
bi.biSizeImage = src->ReadI32LE();
bi.biXPelsPerMeter = src->ReadI32LE();
bi.biYPelsPerMeter = src->ReadI32LE();
bi.biClrUsed = src->ReadI32LE();
bi.biClrImportant = src->ReadI32LE();
} else
{
TVPThrowExceptionMessage(TVPImageLoadError, TJS_W("unsupported header version"));
}
// load palette
tjs_int palsize = (bi.biBitCount <= 8) ?
((bi.biClrUsed == 0 ? (1 << bi.biBitCount) : bi.biClrUsed) *
((bi.biSize == 12) ? 3 : 4)) : 0; // bi.biSize == 12 ( OS/2 palette )
tjs_uint8 *palette = NULL;
if (palsize) palette = new tjs_uint8[palsize];
try
{
src->ReadBuffer(palette, palsize);
src->SetPosition(firstpos + bf.bfOffBits);
TVPInternalLoadBMP(callbackdata, sizecallback, scanlinecallback,
bi, palette, src, keyidx, batMulAlpha, mode);
}
catch (...)
{
if (palette) delete[] palette;
throw;
}
if (palette) delete[] palette;
}
//---------------------------------------------------------------------------
extern "C"
{
#define XMD_H
#include <libjpeg/jinclude.h>
#include <libjpeg/jpeglib.h>
#include <libjpeg/jerror.h>
}
#include "GraphicsLoaderIntf.h"
#define TVPJPEGLoadError TJS_W("JPEG read error/%1")
//---------------------------------------------------------------------------
// JPEG loading handler
//---------------------------------------------------------------------------
tTVPJPEGLoadPrecision TVPJPEGLoadPrecision = jlpMedium;
//---------------------------------------------------------------------------
struct my_error_mgr
{
struct jpeg_error_mgr pub;
};
//---------------------------------------------------------------------------
METHODDEF(void)
my_error_exit(j_common_ptr cinfo)
{
TVPThrowExceptionMessage(TVPJPEGLoadError,
(ttstr(TJS_W("error code : ")) + ttstr(cinfo->err->msg_code)).c_str());
}
//---------------------------------------------------------------------------
METHODDEF(void)
my_emit_message(j_common_ptr c, int n)
{
}
//---------------------------------------------------------------------------
METHODDEF(void)
my_output_message(j_common_ptr c)
{
}
//---------------------------------------------------------------------------
METHODDEF(void)
my_format_message(j_common_ptr c, char* m)
{
}
//---------------------------------------------------------------------------
METHODDEF(void)
my_reset_error_mgr(j_common_ptr c)
{
c->err->num_warnings = 0;
c->err->msg_code = 0;
}
//---------------------------------------------------------------------------
#define BUFFER_SIZE 8192
struct my_source_mgr
{
jpeg_source_mgr pub;
JOCTET * buffer;
tTJSBinaryStream * stream;
boolean start_of_file;
};
//---------------------------------------------------------------------------
METHODDEF(void)
init_source(j_decompress_ptr cinfo)
{
// ??
my_source_mgr * src = (my_source_mgr*)cinfo->src;
src->start_of_file = TRUE;
}
//---------------------------------------------------------------------------
METHODDEF(boolean)
fill_input_buffer(j_decompress_ptr cinfo)
{
my_source_mgr * src = (my_source_mgr*)cinfo->src;
int nbytes = src->stream->Read(src->buffer, BUFFER_SIZE);
if (nbytes <= 0)
{
if (src->start_of_file)
ERREXIT(cinfo, JERR_INPUT_EMPTY);
WARNMS(cinfo, JWRN_JPEG_EOF);
src->buffer[0] = (JOCTET)0xFF;
src->buffer[1] = (JOCTET)JPEG_EOI;
nbytes = 2;
}
src->pub.next_input_byte = src->buffer;
src->pub.bytes_in_buffer = nbytes;
src->start_of_file = FALSE;
return TRUE;
}
//---------------------------------------------------------------------------
METHODDEF(void)
skip_input_data(j_decompress_ptr cinfo, long num_bytes)
{
my_source_mgr * src = (my_source_mgr*)cinfo->src;
if (num_bytes > 0) {
while (num_bytes > (long)src->pub.bytes_in_buffer) {
num_bytes -= (long)src->pub.bytes_in_buffer;
fill_input_buffer(cinfo);
/* note that we assume that fill_input_buffer will never return FALSE,
* so suspension need not be handled.
*/
}
src->pub.next_input_byte += (size_t)num_bytes;
src->pub.bytes_in_buffer -= (size_t)num_bytes;
}
}
//---------------------------------------------------------------------------
METHODDEF(void)
term_source(j_decompress_ptr cinfo)
{
/* no work necessary here */
}
//---------------------------------------------------------------------------
GLOBAL(void)
jpeg_TStream_src(j_decompress_ptr cinfo, tTJSBinaryStream * infile)
{
my_source_mgr * src;
if (cinfo->src == NULL) { /* first time for this JPEG object? */
cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
SIZEOF(my_source_mgr));
src = (my_source_mgr *)cinfo->src;
src->buffer = (JOCTET *)
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
BUFFER_SIZE * SIZEOF(JOCTET));
}
src = (my_source_mgr *)cinfo->src;
src->pub.init_source = init_source;
src->pub.fill_input_buffer = fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
src->pub.term_source = term_source;
src->stream = infile;
src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
src->pub.next_input_byte = NULL; /* until buffer loaded */
}
//---------------------------------------------------------------------------
void TVPLoadJPEG(void* formatdata, void *callbackdata, tTVPGraphicSizeCallback sizecallback,
tTVPGraphicScanLineCallback scanlinecallback, tTVPMetaInfoPushCallback metainfopushcallback,
tTJSBinaryStream *src, tjs_int keyidx, tTVPGraphicLoadMode mode)
{
// JPEG loading handler
// JPEG does not support palettized image
if (mode == glmPalettized)
TVPThrowExceptionMessage(TVPJPEGLoadError,
TJS_W("Unsupported color type for palattized image"));
// prepare variables
jpeg_decompress_struct cinfo;
my_error_mgr jerr;
JSAMPARRAY buffer;
//tjs_int row_stride;
// error handling
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
jerr.pub.emit_message = my_emit_message;
jerr.pub.output_message = my_output_message;
jerr.pub.format_message = my_format_message;
jerr.pub.reset_error_mgr = my_reset_error_mgr;
// create decompress object
jpeg_create_decompress(&cinfo);
// set data source
jpeg_TStream_src(&cinfo, src);
// read the header
jpeg_read_header(&cinfo, TRUE);
// decompress option
switch (TVPJPEGLoadPrecision)
{
case jlpLow:
cinfo.dct_method = JDCT_IFAST;
cinfo.do_fancy_upsampling = FALSE;
break;
case jlpMedium:
//cinfo.dct_method = JDCT_IFAST;
cinfo.dct_method = JDCT_ISLOW;
cinfo.do_fancy_upsampling = TRUE;
break;
case jlpHigh:
cinfo.dct_method = JDCT_FLOAT;
cinfo.do_fancy_upsampling = TRUE;
break;
}
if (mode == glmGrayscale) cinfo.out_color_space = JCS_GRAYSCALE;
// start decompression
jpeg_start_decompress(&cinfo);
try
{
sizecallback(callbackdata, cinfo.output_width, cinfo.output_height);
#if 1
if (mode == glmNormal && cinfo.out_color_space == JCS_RGB) {
buffer = new JSAMPROW[cinfo.output_height];
for (unsigned int i = 0; i < cinfo.output_height; i++) {
buffer[i] = (JSAMPLE*)scanlinecallback(callbackdata, i);
}
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, buffer + cinfo.output_scanline, cinfo.output_height - cinfo.output_scanline);
}
delete[] buffer;
for (unsigned int i = 0; i < cinfo.output_height; i++) {
scanlinecallback(callbackdata, i);
scanlinecallback(callbackdata, -1);
}
} else
#endif
{
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr)&cinfo, JPOOL_IMAGE,
cinfo.output_width * cinfo.output_components + 3,
cinfo.rec_outbuf_height);
while (cinfo.output_scanline < cinfo.output_height)
{
tjs_int startline = cinfo.output_scanline;
jpeg_read_scanlines(&cinfo, buffer, cinfo.rec_outbuf_height);
tjs_int endline = cinfo.output_scanline;
tjs_int bufline;
tjs_int line;
for (line = startline, bufline = 0; line < endline; line++, bufline++)
{
void *scanline =
scanlinecallback(callbackdata, line);
if (!scanline) break;
// color conversion
if (mode == glmGrayscale)
{
// write through
memcpy(scanline,
buffer[bufline], cinfo.output_width);
} else
{
if (cinfo.out_color_space == JCS_RGB)
{
#if 1
memcpy(scanline,
buffer[bufline], cinfo.output_width*sizeof(tjs_uint32));
#else
// expand 24bits to 32bits
TVPConvert24BitTo32Bit(
(tjs_uint32*)scanline,
(tjs_uint8*)buffer[bufline], cinfo.output_width);
#endif
} else
{
// expand 8bits to 32bits
TVPExpand8BitTo32BitGray(
(tjs_uint32*)scanline,
(tjs_uint8*)buffer[bufline], cinfo.output_width);
}
}
scanlinecallback(callbackdata, -1);
}
if (line != endline) break; // interrupted by !scanline
}
}
}
catch (...)
{
jpeg_destroy_decompress(&cinfo);
throw;
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
}
//---------------------------------------------------------------------------
struct stream_destination_mgr {
struct jpeg_destination_mgr pub; /* public fields */
tTJSBinaryStream* stream;
JOCTET* buffer; /* buffer start address */
int bufsize; /* size of buffer */
int bufsizeinit;/* size of buffer */
size_t datasize; /* final size of compressed data */
int* outsize; /* user pointer to datasize */
};
typedef stream_destination_mgr* stream_dest_ptr;
METHODDEF(void) JPEG_write_init_destination(j_compress_ptr cinfo) {
stream_dest_ptr dest = (stream_dest_ptr)cinfo->dest;
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = dest->bufsize;
dest->datasize = 0; /* reset output size */
}
METHODDEF(boolean) JPEG_write_empty_output_buffer(j_compress_ptr cinfo) {
stream_dest_ptr dest = (stream_dest_ptr)cinfo->dest;
// 懌傝側偔側偭偨傜搑拞彂偒崬傒
size_t wrotelen = dest->bufsizeinit - dest->pub.free_in_buffer;
dest->stream->WriteBuffer(dest->buffer, wrotelen);
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = dest->bufsize;
return TRUE;
}
METHODDEF(void) JPEG_write_term_destination(j_compress_ptr cinfo) {
stream_dest_ptr dest = (stream_dest_ptr)cinfo->dest;
dest->datasize = dest->bufsize - dest->pub.free_in_buffer;
if (dest->outsize) *dest->outsize += (int)dest->datasize;
}
METHODDEF(void) JPEG_write_stream(j_compress_ptr cinfo, JOCTET* buffer, int bufsize, int* outsize, tTJSBinaryStream* stream) {
stream_dest_ptr dest;
/* first call for this instance - need to setup */
if (cinfo->dest == 0) {
cinfo->dest = (struct jpeg_destination_mgr*)(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT, sizeof(stream_destination_mgr));
}
dest = (stream_dest_ptr)cinfo->dest;
dest->stream = stream;
dest->bufsize = bufsize;