-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLoadTLG.cpp
More file actions
584 lines (511 loc) · 16.5 KB
/
LoadTLG.cpp
File metadata and controls
584 lines (511 loc) · 16.5 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
//---------------------------------------------------------------------------
/*
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"
*/
//---------------------------------------------------------------------------
// TLG5/6 decoder
//---------------------------------------------------------------------------
#include <stdlib.h>
#include <hook_init.h>
#include "GraphicsLoaderIntf.h"
#include "tp_stub.h"
#define TVPTLGLoadError TJS_W("TLG read error/%1")
#define TVP_TLG6_H_BLOCK_SIZE 8
#define TVP_TLG6_W_BLOCK_SIZE 8
/*
TLG5:
Lossless graphics compression method designed for very fast decoding
speed.
TLG6:
Lossless/near-lossless graphics compression method which is designed
for high compression ratio and faster decoding. Decoding speed is
somewhat slower than TLG5 because the algorithm is much more complex
than TLG5. Though, the decoding speed (using SSE enabled code) is
about 20 times faster than JPEG2000 lossless mode (using JasPer
library) while the compression ratio can beat or compete with it.
Summary of compression algorithm is described in
environ/win32/krdevui/tpc/tlg6/TLG6Saver.cpp
(in Japanese).
*/
//---------------------------------------------------------------------------
// TLG5 loading handler
//---------------------------------------------------------------------------
void TVPLoadTLG5(void* formatdata, void *callbackdata,
tTVPGraphicSizeCallback sizecallback,
tTVPGraphicScanLineCallback scanlinecallback,
tTJSBinaryStream *src,
tjs_int keyidx,
tTVPGraphicLoadMode mode)
{
// load TLG v5.0 lossless compressed graphic
if(mode != glmNormal)
TVPThrowExceptionMessage(TVPTLGLoadError,
TJS_W("TLG cannot be used as universal transition rule, province(_p) or mask(_m) images."));
unsigned char mark[12];
tjs_int width, height, colors, blockheight;
src->ReadBuffer(mark, 1);
colors = mark[0];
width = src->ReadI32LE();
height = src->ReadI32LE();
blockheight = src->ReadI32LE();
if(colors != 3 && colors != 4)
TVPThrowExceptionMessage(TVPTLGLoadError, TJS_W("Unsupported color type."));
int blockcount = (int)((height - 1) / blockheight) + 1;
// skip block size section
src->SetPosition(src->GetPosition() + blockcount * sizeof(tjs_uint32));
// decomperss
sizecallback(callbackdata, width, height);
tjs_uint8 *inbuf = NULL;
tjs_uint8 *outbuf[4];
tjs_uint8 text[4096] = { 0 };
tjs_int r = 0;
for(int i = 0; i < colors; i++) outbuf[i] = NULL;
try
{
inbuf = (tjs_uint8*)TJSAlignedAlloc(blockheight * width + 10, 4);
for(tjs_int i = 0; i < colors; i++)
outbuf[i] = (tjs_uint8*)TJSAlignedAlloc(blockheight * width + 10, 4);
tjs_uint8 *prevline = NULL;
for(tjs_int y_blk = 0; y_blk < height; y_blk += blockheight)
{
// read file and decompress
for(tjs_int c = 0; c < colors; c++)
{
src->ReadBuffer(mark, 1);
tjs_int size;
size = src->ReadI32LE();
if(mark[0] == 0)
{
// modified LZSS compressed data
src->ReadBuffer(inbuf, size);
r = TVPTLG5DecompressSlide(outbuf[c], inbuf, size, text, r);
}
else
{
// raw data
src->ReadBuffer(outbuf[c], size);
}
}
// compose colors and store
tjs_int y_lim = y_blk + blockheight;
if(y_lim > height) y_lim = height;
tjs_uint8 * outbufp[4];
for(tjs_int c = 0; c < colors; c++) outbufp[c] = outbuf[c];
for(tjs_int y = y_blk; y < y_lim; y++)
{
tjs_uint8 *current = (tjs_uint8*)scanlinecallback(callbackdata, y);
tjs_uint8 *current_org = current;
if(prevline)
{
// not first line
switch(colors)
{
case 3:
TVPTLG5ComposeColors3To4(current, prevline, outbufp, width);
outbufp[0] += width; outbufp[1] += width;
outbufp[2] += width;
break;
case 4:
TVPTLG5ComposeColors4To4(current, prevline, outbufp, width);
outbufp[0] += width; outbufp[1] += width;
outbufp[2] += width; outbufp[3] += width;
break;
}
}
else
{
// first line
switch(colors)
{
case 3:
for(tjs_int pr = 0, pg = 0, pb = 0, x = 0;
x < width; x++)
{
tjs_int b = outbufp[2][x];
tjs_int g = outbufp[1][x];
tjs_int r = outbufp[0][x];
b += g; r += g;
0[current++] = pb += b;
0[current++] = pg += g;
0[current++] = pr += r;
0[current++] = 0xff;
}
outbufp[0] += width;
outbufp[1] += width;
outbufp[2] += width;
break;
case 4:
for(tjs_int pr = 0, pg = 0, pb = 0, pa = 0, x = 0;
x < width; x++)
{
tjs_int b = outbufp[2][x];
tjs_int g = outbufp[1][x];
tjs_int r = outbufp[0][x];
tjs_int a = outbufp[3][x];
b += g; r += g;
0[current++] = pb += b;
0[current++] = pg += g;
0[current++] = pr += r;
0[current++] = pa += a;
}
outbufp[0] += width;
outbufp[1] += width;
outbufp[2] += width;
outbufp[3] += width;
break;
}
}
scanlinecallback(callbackdata, -1);
prevline = current_org;
}
}
}
catch(...)
{
if(inbuf) TJSAlignedDealloc(inbuf);
for(tjs_int i = 0; i < colors; i++)
if(outbuf[i]) TJSAlignedDealloc(outbuf[i]);
throw;
}
if(inbuf) TJSAlignedDealloc(inbuf);
for(tjs_int i = 0; i < colors; i++)
if(outbuf[i]) TJSAlignedDealloc(outbuf[i]);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// TLG6 loading handler
//---------------------------------------------------------------------------
void TVPLoadTLG6(void* formatdata, void *callbackdata,
tTVPGraphicSizeCallback sizecallback,
tTVPGraphicScanLineCallback scanlinecallback,
tTJSBinaryStream *src,
tjs_int keyidx,
tTVPGraphicLoadMode mode)
{
// load TLG v6.0 lossless/near-lossless compressed graphic
// if(palettized)
// TVPThrowExceptionMessage(TVPTLGLoadError,
// TJS_W("TLG cannot be used as universal transition rule, province(_p) or mask(_m) images."));
unsigned char buf[12];
src->ReadBuffer(buf, 4);
tjs_int colors = buf[0]; // color component count
if(colors != 1 && colors != 4 && colors != 3)
TVPThrowExceptionMessage(TVPTLGLoadError,
(ttstr(TJS_W("Unsupported color count : ")) + ttstr((int)colors)).c_str() );
if(buf[1] != 0) // data flag
TVPThrowExceptionMessage(TVPTLGLoadError,
TJS_W("Data flag must be 0 (any flags are not yet supported)"));
if(buf[2] != 0) // color type (currently always zero)
TVPThrowExceptionMessage(TVPTLGLoadError,
(ttstr(TJS_W("Unsupported color type : ")) + ttstr((int)buf[1])).c_str() );
if(buf[3] != 0) // external golomb table (currently always zero)
TVPThrowExceptionMessage(TVPTLGLoadError,
TJS_W("External golomb bit length table is not yet supported."));
tjs_int width, height;
width = src->ReadI32LE();
height = src->ReadI32LE();
tjs_int max_bit_length;
max_bit_length = src->ReadI32LE();
// set destination size
sizecallback(callbackdata, width, height);
// compute some values
tjs_int x_block_count = (tjs_int)((width - 1)/ TVP_TLG6_W_BLOCK_SIZE) + 1;
tjs_int y_block_count = (tjs_int)((height - 1)/ TVP_TLG6_H_BLOCK_SIZE) + 1;
tjs_int main_count = width / TVP_TLG6_W_BLOCK_SIZE;
tjs_int fraction = width - main_count * TVP_TLG6_W_BLOCK_SIZE;
// prepare memory pointers
tjs_uint8 *bit_pool = NULL;
tjs_uint32 *pixelbuf = NULL; // pixel buffer
tjs_uint8 *filter_types = NULL;
tjs_uint8 LZSS_text[4096];
tjs_uint32 *zeroline = NULL;
tjs_uint32 *tmpline = nullptr;
tjs_uint8 *grayline;
try
{
// allocate memories
bit_pool = (tjs_uint8 *)TJSAlignedAlloc(max_bit_length / 8 + 5, 4);
pixelbuf = (tjs_uint32 *)TJSAlignedAlloc(
sizeof(tjs_uint32) * width * TVP_TLG6_H_BLOCK_SIZE + 1, 4);
filter_types = (tjs_uint8 *)TJSAlignedAlloc(
x_block_count * y_block_count, 4);
zeroline = (tjs_uint32 *)TJSAlignedAlloc(width * sizeof(tjs_uint32), 4);
// initialize zero line (virtual y=-1 line)
TVPFillARGB(zeroline, width, colors==3?0xff000000:0x00000000);
// 0xff000000 for colors=3 makes alpha value opaque
// initialize LZSS text (used by chroma filter type codes)
{
tjs_uint32 *p = (tjs_uint32*)LZSS_text;
for(tjs_uint32 i = 0; i < 32*0x01010101; i+=0x01010101)
{
for(tjs_uint32 j = 0; j < 16*0x01010101; j+=0x01010101)
p[0] = i, p[1] = j, p += 2;
}
}
// read chroma filter types.
// chroma filter types are compressed via LZSS as used by TLG5.
{
tjs_int inbuf_size = src->ReadI32LE();
tjs_uint8* inbuf = (tjs_uint8*)TJSAlignedAlloc(inbuf_size, 4);
try
{
src->ReadBuffer(inbuf, inbuf_size);
TVPTLG5DecompressSlide(filter_types, inbuf, inbuf_size,
LZSS_text, 0);
}
catch(...)
{
TJSAlignedDealloc(inbuf);
throw;
}
TJSAlignedDealloc(inbuf);
}
// for each horizontal block group ...
tjs_uint32 *prevline = zeroline;
for(tjs_int y = 0; y < height; y += TVP_TLG6_H_BLOCK_SIZE)
{
tjs_int ylim = y + TVP_TLG6_H_BLOCK_SIZE;
if(ylim >= height) ylim = height;
tjs_int pixel_count = (ylim - y) * width;
// decode values
for(tjs_int c = 0; c < colors; c++)
{
// read bit length
tjs_int bit_length = src->ReadI32LE();
// get compress method
int method = (bit_length >> 30)&3;
bit_length &= 0x3fffffff;
// compute byte length
tjs_int byte_length = bit_length / 8;
if(bit_length % 8) byte_length++;
// read source from input
src->ReadBuffer(bit_pool, byte_length);
// decode values
// two most significant bits of bitlength are
// entropy coding method;
// 00 means Golomb method,
// 01 means Gamma method (not yet suppoted),
// 10 means modified LZSS method (not yet supported),
// 11 means raw (uncompressed) data (not yet supported).
switch(method)
{
case 0:
if(c == 0 && colors != 1)
TVPTLG6DecodeGolombValuesForFirst((tjs_int8*)pixelbuf,
pixel_count, bit_pool);
else
TVPTLG6DecodeGolombValues((tjs_int8*)pixelbuf + c,
pixel_count, bit_pool);
break;
default:
TVPThrowExceptionMessage(TVPTLGLoadError,
TJS_W("Unsupported entropy coding method"));
}
}
// for each line
unsigned char * ft =
filter_types + (y / TVP_TLG6_H_BLOCK_SIZE)*x_block_count;
int skipbytes = (ylim-y)*TVP_TLG6_W_BLOCK_SIZE;
for(int yy = y; yy < ylim; yy++)
{
tjs_uint32* curline;
if (mode == glmNormal)
curline = (tjs_uint32*)scanlinecallback(callbackdata, yy);
else {
if (!tmpline) tmpline = new tjs_uint32[width * 2];
curline = tmpline + ((yy & 1) ? width : 0);
grayline = (tjs_uint8*)scanlinecallback(callbackdata, yy);
}
int dir = (yy&1)^1;
int oddskip = ((ylim - yy -1) - (yy-y));
if(main_count)
{
int start =
((width < TVP_TLG6_W_BLOCK_SIZE) ? width : TVP_TLG6_W_BLOCK_SIZE) *
(yy - y);
TVPTLG6DecodeLine(
prevline,
curline,
width,
main_count,
ft,
skipbytes,
pixelbuf + start, colors==3?0xff000000:0, oddskip, dir);
}
if(main_count != x_block_count)
{
int ww = fraction;
if(ww > TVP_TLG6_W_BLOCK_SIZE) ww = TVP_TLG6_W_BLOCK_SIZE;
int start = ww * (yy - y);
TVPTLG6DecodeLineGeneric(
prevline,
curline,
width,
main_count,
x_block_count,
ft,
skipbytes,
pixelbuf + start, colors==3?0xff000000:0, oddskip, dir);
}
prevline = curline;
if (mode != glmNormal) {
for (int x = 0; x < width; ++x) {
grayline[x] = curline[x] & 0xFF; // red -> lumi
}
}
scanlinecallback(callbackdata, -1);
}
}
}
catch(...)
{
if(bit_pool) TJSAlignedDealloc(bit_pool);
if(pixelbuf) TJSAlignedDealloc(pixelbuf);
if(filter_types) TJSAlignedDealloc(filter_types);
if(zeroline) TJSAlignedDealloc(zeroline);
if (tmpline) delete[] tmpline;
throw;
}
if(bit_pool) TJSAlignedDealloc(bit_pool);
if(pixelbuf) TJSAlignedDealloc(pixelbuf);
if(filter_types) TJSAlignedDealloc(filter_types);
if(zeroline) TJSAlignedDealloc(zeroline);
if (tmpline) delete[] tmpline;
}
//---------------------------------------------------------------------------
// TLG loading handler
//---------------------------------------------------------------------------
static void TVPInternalLoadTLG(void* formatdata, void *callbackdata, tTVPGraphicSizeCallback sizecallback,
tTVPGraphicScanLineCallback scanlinecallback, tTVPMetaInfoPushCallback metainfopushcallback,
tTJSBinaryStream *src, tjs_int keyidx, tTVPGraphicLoadMode mode)
{
// read header
unsigned char mark[12];
src->ReadBuffer(mark, 11);
// check for TLG raw data
if(!memcmp("TLG5.0\x00raw\x1a\x00", mark, 11))
{
TVPLoadTLG5(formatdata, callbackdata, sizecallback,
scanlinecallback, src, keyidx, mode);
}
else if(!memcmp("TLG6.0\x00raw\x1a\x00", mark, 11))
{
TVPLoadTLG6(formatdata, callbackdata, sizecallback,
scanlinecallback, src, keyidx, mode);
}
else
{
TVPThrowExceptionMessage(TVPTLGLoadError,
TJS_W("Invalid TLG header or unsupported TLG version."));
}
}
//---------------------------------------------------------------------------
void TVPLoadTLG(void* formatdata, void *callbackdata, tTVPGraphicSizeCallback sizecallback,
tTVPGraphicScanLineCallback scanlinecallback, tTVPMetaInfoPushCallback metainfopushcallback,
tTJSBinaryStream *src, tjs_int keyidx, tTVPGraphicLoadMode mode)
{
// read header
unsigned char mark[12];
src->ReadBuffer(mark, 11);
// check for TLG0.0 sds
if(!memcmp("TLG0.0\x00sds\x1a\x00", mark, 11))
{
// read TLG0.0 Structured Data Stream
// TLG0.0 SDS tagged data is simple "NAME=VALUE," string;
// Each NAME and VALUE have length:content expression.
// eg: 4:LEFT=2:20,3:TOP=3:120,4:TYPE=1:3,
// The last ',' cannot be ommited.
// Each string (name and value) must be encoded in utf-8.
// read raw data size
tjs_uint rawlen = src->ReadI32LE();
// try to load TLG raw data
TVPInternalLoadTLG(formatdata, callbackdata, sizecallback,
scanlinecallback, metainfopushcallback, src, keyidx, mode);
// seek to meta info data point
src->Seek(rawlen + 11 + 4, TJS_BS_SEEK_SET);
// read tag data
while(true)
{
char chunkname[4];
if(4 != src->Read(chunkname, 4)) break;
// cannot read more
tjs_uint chunksize = src->ReadI32LE();
if(!memcmp(chunkname, "tags", 4))
{
// tag information
char *tag = NULL;
char *name = NULL;
char *value = NULL;
try
{
tag = new char [chunksize + 1];
src->ReadBuffer(tag, chunksize);
tag[chunksize] = 0;
if(metainfopushcallback)
{
const char *tagp = tag;
const char *tagp_lim = tag + chunksize;
while(tagp < tagp_lim)
{
tjs_uint namelen = 0;
while(*tagp >= '0' && *tagp <= '9')
namelen = namelen * 10 + *tagp - '0', tagp++;
if(*tagp != ':') TVPThrowExceptionMessage(TVPTLGLoadError,
TJS_W("Malformed TLG SDS tag structure, missing colon after name length"));
tagp ++;
name = new char [namelen + 1];
memcpy(name, tagp, namelen);
name[namelen] = '\0';
tagp += namelen;
if(*tagp != '=') TVPThrowExceptionMessage(TVPTLGLoadError,
TJS_W("Malformed TLG SDS tag structure, missing equals after name"));
tagp++;
tjs_uint valuelen = 0;
while(*tagp >= '0' && *tagp <= '9')
valuelen = valuelen * 10 + *tagp - '0', tagp++;
if(*tagp != ':') TVPThrowExceptionMessage(TVPTLGLoadError,
TJS_W("Malformed TLG SDS tag structure, missing colon after value length"));
tagp++;
value = new char [valuelen + 1];
memcpy(value, tagp, valuelen);
value[valuelen] = '\0';
tagp += valuelen;
if(*tagp != ',') TVPThrowExceptionMessage(TVPTLGLoadError,
TJS_W("Malformed TLG SDS tag structure, missing comma after a tag"));
tagp++;
// insert into name-value pairs ... TODO: utf-8 decode
metainfopushcallback(callbackdata, ttstr(name), ttstr(value));
delete [] name, name = NULL;
delete [] value, value = NULL;
}
}
}
catch(...)
{
if(tag) delete [] tag;
if(name) delete [] name;
if(value) delete [] value;
throw;
}
if(tag) delete [] tag;
if(name) delete [] name;
if(value) delete [] value;
}
else
{
// skip the chunk
src->SetPosition(src->GetPosition() + chunksize);
}
} // while
}
else
{
src->Seek(0, TJS_BS_SEEK_SET); // rewind
// try to load TLG raw data
TVPInternalLoadTLG(formatdata, callbackdata, sizecallback,
scanlinecallback, metainfopushcallback, src, keyidx, mode);
}
}
//---------------------------------------------------------------------------