-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTgaTool.cpp
494 lines (413 loc) · 13 KB
/
TgaTool.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
/**
@brief TGAグラフィックデータ ツール for C++
@file TgaTool.cpp
@version 0.9
@date 2015 5/9
@author Fumi.Iseki (C)
*/
#include "TgaTool.h"
using namespace jbxl;
/**
void TGAImage::init(void)
メンバ変数を初期化する.
*/
void TGAImage::init(void)
{
xs = 0;
ys = 0;
col = 0;
length = 0;
state = 0;
gp = NULL;
memset(hd, 0, TGA_HEADER_SIZE);
memset(ft, 0, TGA_FOOTER_SIZE);
int len = (int)strlen(TGA_FOOTER_STR) + 1;
int pos = TGA_FOOTER_SIZE - len;
memcpy(ft + pos, TGA_FOOTER_STR, len);
return;
}
/**
bool TGAImage::isNull(void)
グラフィックデータを持いないか?
*/
bool TGAImage::isNull(void)
{
if (gp==NULL) return true;
return false;
}
/**
void TGAImage::clear(void)
*/
void TGAImage::clear(void)
{
if (gp!=NULL) memset(gp, 0, xs*ys*col);
return;
}
/**
void TGAImage::fill(uByte v)
*/
void TGAImage::fill(uByte v)
{
if (gp!=NULL) memset(gp, v, xs*ys*col);
return;
}
/**
void TGAImage::free(void)
グラフィックデータを開放する
*/
void TGAImage::free(void)
{
if (gp!=NULL) ::free(gp);
init();
return;
}
/**
void TGAImage::setzero(int x, int y, int c)
*/
void TGAImage::setzero(int x, int y, int c)
{
getm(x, y, c);
if (gp==NULL) return;
xs = x;
ys = y;
col = c;
state = 0;
memset(gp, 0, xs*ys*col);
return;
}
/**
void TGAImage::getm(int x, int y, int c)
*/
void TGAImage::getm(int x, int y, int c)
{
gp = (uByte*)malloc(x*y*c);
if (gp==NULL) {
state = JBXL_GRAPH_MEMORY_ERROR;
return;
}
memset(gp, 0, x*y*c);
return;
}
/////////////////////////////////////////////////////////////////////////////////////////
/**
TGAImage jbxl::readTGAFile(const char* fname)
TGAファイルを読み込んで,TGAImage構造体へデータを格納する.
@param fname 読み込むファイル名
@return TGAImage データ.gp==NULL の場合,@b state に情報が入る.
@retval JBXL_GRAPH_OPFILE_ERROR @b state: ファイルオープンエラー
@retval JBXL_GRAPH_HEADER_ERROR @b state: 不正ファイル(TGAファイルでない?)
@retval JBXL_GRAPH_MEMORY_ERROR @b state: メモリエラー
*/
TGAImage jbxl::readTGAFile(const char* fname)
{
TGAImage tga;
FILE* fp;
fp = fopen(fname, "rb");
if (fp==NULL) {
tga.gp = NULL;
tga.state = JBXL_GRAPH_OPFILE_ERROR;
return tga;
}
tga = readTGAData(fp);
fclose(fp);
return tga;
}
/**
TGAImage jbxl::readTGAData(FILE* fp)
TGAファイルを読み込んで,TGAImage構造体へデータを格納する.
読み込んだデータは非圧縮状態.
@param fp 読み込むファイルの記述子
@return TGAImage データ.gp==NULL の場合,@b state に情報が入る.
@retval JBXL_GRAPH_OPFILE_ERROR @b state: ファイルオープンエラー
@retval JBXL_GRAPH_HEADER_ERROR @b state: 不正ファイル(TGAファイルでない?)
@retval JBXL_GRAPH_MEMORY_ERROR @b state: メモリエラー
@retval JBXL_GRAPH_IVDFMT_ERROR @b state: サポート外のデータ形式
*/
TGAImage jbxl::readTGAData(FILE* fp)
{
TGAImage tga;
int rle = FALSE;
fseek(fp, 0, 0);
tga.free();
size_t ret = fread(&tga.hd, TGA_HEADER_SIZE, 1, fp);
if (ret<=0) {
DEBUG_MODE PRINT_MESG("JBXL::readTGAData: ERROR: File read Error\n");
tga.state = JBXL_FILE_READ_ERROR;
return tga;
}
int kind = (int)tga.hd[2];
if (kind == 2) {
tga.col = 3;
}
else if (kind == 3) {
tga.col = 1;
}
else if (kind == 10) {
tga.col = 3;
rle = TRUE;
}
else if (kind == 11) {
tga.col = 1;
rle = TRUE;
}
else {
DEBUG_MODE PRINT_MESG("JBXL::readTGAData: ERROR: no supported File Format (%d)\n", kind);
tga.state = JBXL_GRAPH_IVDFMT_ERROR;
return tga;
}
if (is_little_endian()) {
tga.xs = tga.hd[12] + tga.hd[13]*256;
tga.ys = tga.hd[14] + tga.hd[15]*256;
}
else {
tga.xs = tga.hd[12]*256 + tga.hd[13];
tga.ys = tga.hd[14]*256 + tga.hd[15];
}
uByte alpha = tga.hd[17] & 0x0f; // αチャンネル
if (alpha != 0x08 && alpha != 0x00) {
DEBUG_MODE PRINT_MESG("JBXL::readTGAData: ERROR: unkonwn Alpha Channel (0x%02x)\n", alpha);
tga.state = JBXL_GRAPH_IVDFMT_ERROR;
return tga;
}
else if (alpha == 0x08) {
tga.col++;
}
if (tga.col*8 != (int)tga.hd[16]) {
tga.col = (int)tga.hd[16]/8;
PRINT_MESG("JBXL::readTGAData: Warning: Not Match Color Num! set color num = %d\n", tga.col);
}
PRINT_MESG("JBXL::readTGAData: TGA File (%d, %d, %d)\n", tga.xs, tga.ys, tga.col);
int datasize = tga.xs*tga.ys*tga.col;
tga.length = datasize;
tga.gp = (uByte*)malloc(datasize);
if (tga.gp==NULL) {
PRINT_MESG("JBXL::readTGAData: ERROR: out of Memory!\n");
tga.state = JBXL_GRAPH_MEMORY_ERROR;
return tga;
}
memset(tga.gp, 0, datasize);
fseek(fp, (int)tga.hd[0], SEEK_CUR);
if (rle) {
// RLE
int size = 0;
uByte chunk;
uByte buf[LBUF];
while (size<datasize && !feof(fp)) {
ret = fread(&chunk, 1, 1, fp);
if (ret<=0) {
DEBUG_MODE PRINT_MESG("JBXL::readTGAData: ERROR: File read Error\n");
tga.state = JBXL_FILE_READ_ERROR;
return tga;
}
int rep = (int)(chunk & 0x7f) + 1;
if (chunk & 0x80) {
ret = fread(buf, tga.col, 1, fp);
if (ret<=0) {
tga.state = JBXL_FILE_READ_ERROR;
return tga;
}
for (int j=0; j<rep; j++) {
for (int i=0; i<tga.col; i++) tga.gp[size++] = buf[i];
}
}
else {
if (tga.col*rep<LBUF) {
ret = fread(buf, tga.col*rep, 1, fp);
if (ret<=0) {
tga.state = JBXL_FILE_READ_ERROR;
return tga;
}
for (int i=0; i<tga.col*rep; i++) tga.gp[size++] = buf[i];
}
else {
for (int j=0; j<rep; j++) {
ret = fread(buf, tga.col, 1, fp);
if (ret<=0) {
tga.state = JBXL_FILE_READ_ERROR;
return tga;
}
for (int i=0; i<tga.col; i++) tga.gp[size++] = buf[i];
}
}
}
}
if (size!=datasize) {
DEBUG_MODE PRINT_MESG("JBXL::readTGAData: ERROR: unpack RLE failed. (%d != %d), format = %d\n", size, datasize, kind);
tga.state = JBXL_GRAPH_FILESZ_ERROR;
return tga;
}
tga.hd[2] &= ~0x08; // 非圧縮状態
}
else {
ret = fread(tga.gp, datasize, 1, fp);
if (ret<=0) {
tga.state = JBXL_FILE_READ_ERROR;
return tga;
}
}
if (!feof(fp)) {
ret = fread(tga.ft, TGA_FOOTER_SIZE, 1, fp);
if (ret<=0) {
tga.state = JBXL_FILE_READ_ERROR;
return tga;
}
}
tga.state = 0;
return tga;
}
/**
int jbxl::writeTGAFile(const char* fname, TGAImage* tga)
tga の画像データを fnameに書き出す.
@param fname ファイル名
@param tga 保存する TGAデータ
@retval 0 正常終了
@retval JBXL_GRAPH_OPFILE_ERROR ファイルオープンエラー
@retval JBXL_GRAPH_HEADER_ERROR 不正ファイル(TGAファイルでない?)
@retval JBXL_GRAPH_MEMORY_ERROR メモリエラー
@retval JBXL_GRAPH_NODATA_ERROR tga にデータが無い
@retval JBXL_GRAPH_IVDARG_ERROR ファイル名が NULL
@retval JBXL_GRAPH_IVDCOLOR_ERROR サポート外のチャンネル数
*/
int jbxl::writeTGAFile(const char* fname, TGAImage* tga)
{
FILE* fp;
int ret;
if (fname==NULL) return JBXL_GRAPH_IVDARG_ERROR;
if (tga->col<=0 || tga->col>4) return JBXL_GRAPH_IVDCOLOR_ERROR;
if (tga->gp==NULL) return JBXL_GRAPH_NODATA_ERROR;
fp = fopen(fname, "wb");
if (fp==NULL) {
return JBXL_GRAPH_OPFILE_ERROR;
}
ret = writeTGAData(fp, tga);
fclose(fp);
return ret;
}
/**
int jbxl::writeTGAData(FILE* fp, TGAImage* tga)
tga の画像データを fpに書き出す.
@param fp ファイル記述子
@param tga 保存する TGAデータ
@retval 0 正常終了
@retval JBXL_GRAPH_OPFILE_ERROR ファイルオープンエラー
@retval JBXL_GRAPH_HEADER_ERROR 不正ファイル(TGAファイルでない?)
@retval JBXL_GRAPH_MEMORY_ERROR メモリエラー
@retval JBXL_GRAPH_NODATA_ERROR tga にデータが無い
@retval JBXL_GRAPH_IVDCOLOR_ERROR サポート外のチャンネル数
@retval JBXL_GRAPH_IVDFMT_ERROR サポート外のデータ形式
*/
int jbxl::writeTGAData(FILE* fp, TGAImage* tga)
{
if (fp==NULL) return JBXL_GRAPH_OPFILE_ERROR;
if (tga->col<=0 || tga->col>4) return JBXL_GRAPH_IVDCOLOR_ERROR;
if (tga->gp==NULL) return JBXL_GRAPH_NODATA_ERROR;
fwrite(tga->hd, TGA_HEADER_SIZE, 1, fp);
fwrite(tga->gp, tga->length, 1, fp);
fwrite(tga->ft, TGA_FOOTER_SIZE, 1, fp);
return 0;
}
/**
int jbxl::setupTGAData(TGAImage* tga, bool rle)
TGAのヘッダを設定し直して,必要なら RLEを行う.
@param tga TGAデータへのポインタ
@param rle RLE(連長圧縮)を行うかどうか
@retval 0 正常終了
@retval <0 エラーコード
*/
int jbxl::setupTGAData(TGAImage* tga, bool rle)
{
if (tga->col<=0 || tga->col>4) return JBXL_GRAPH_IVDCOLOR_ERROR;
memset(tga->hd, 0, TGA_HEADER_SIZE);
if (tga->col==3 || tga->col==4) tga->hd[2] = 2; // Full Color
else if (tga->col==1 || tga->col==2) tga->hd[2] = 3; // Gray Scale
else return JBXL_GRAPH_IVDFMT_ERROR;
if (is_little_endian()) {
tga->hd[12] = (uByte)(tga->xs%256);
tga->hd[13] = (uByte)(tga->xs/256);
tga->hd[14] = (uByte)(tga->ys%256);
tga->hd[15] = (uByte)(tga->ys/256);
}
else {
tga->hd[12] = (uByte)(tga->xs/256);
tga->hd[13] = (uByte)(tga->xs%256);
tga->hd[14] = (uByte)(tga->ys/256);
tga->hd[15] = (uByte)(tga->ys%256);
}
tga->hd[16] = tga->col*8; // depth
tga->hd[17] = 0x20; // 0x20: Y方向:Top->Down
tga->hd[10] = tga->hd[14]; // Y posion for 0x20
tga->hd[11] = tga->hd[15]; //
if (tga->col==2 || tga->col==4) {
tga->hd[17] |= 0x08; // 0x08: αチャンネル深度
}
tga->length = tga->xs*tga->ys*tga->col; // データ(gp)長
if (rle && tga->gp!=NULL) {
int len = tga->xs*tga->ys;
uByte* index = (uByte*)malloc(len);
if (index==NULL) return JBXL_GRAPH_MEMORY_ERROR;
memset(index, 0, len);
int idx = 0;
int p = 0;
while (p<len-1) {
if (!memcmp(tga->gp + p*tga->col, tga->gp + (p+1)*tga->col, tga->col)) {
index[idx]++;
if (index[idx]==127) {
idx = p + 2;
p++;
}
}
else {
idx = p + 1;
}
p++;
}
// 変換後のサイズを計算
int n = 0;
p = 0;
while (p < len-1) {
n += tga->col + 1;
if (index[p]!=0x00) p += index[p];
p++;
}
if ((float)n < len*tga->col*0.8f) {
DEBUG_MODE PRINT_MESG("JBXL::writeTGAData: exec RLE (%d -> %d)\n", len*tga->col, n);
uByte* buf = (uByte*)malloc(len*tga->col);
if (buf==NULL) {
::free(index);
return JBXL_GRAPH_MEMORY_ERROR;
}
memset(buf, 0, len*tga->col);
//
int i = 0;
int j = 0;
while (i < len-1) {
if (index[i]!=0x00) {
buf[j++] = 0x80 + index[i];
memcpy(buf+j, tga->gp + i*tga->col, tga->col);
j += tga->col;
i += index[i];
}
else {
buf[j++] = 0x00;
memcpy(buf+j, tga->gp + i*tga->col, tga->col);
j += tga->col;
}
i++;
}
//
if (j==n) {
tga->hd[2] |= 0x08;
tga->length = n;
::free(tga->gp);
tga->gp = buf;
}
else {
DEBUG_MODE PRINT_MESG("JBXL::writeTGAData: Warning: missmatch RLE size (%d != %d)\n", j, n);
DEBUG_MODE PRINT_MESG("JBXL::writeTGAData: Warning: stop RLE!\n");
::free(buf);
}
::free(index);
}
}
return 0;
}