|
@@ -17,14 +17,10 @@
|
17
|
17
|
#include <memory_test.h>
|
18
|
18
|
#endif
|
19
|
19
|
|
20
|
|
-
|
21
|
|
-int tga_check(FILE *f)
|
22
|
|
-{
|
|
20
|
+int tga_check(FILE *f) {
|
23
|
21
|
char buffer[10];
|
24
|
22
|
|
25
|
|
-
|
26
|
|
- if (!f)
|
27
|
|
- {
|
|
23
|
+ if (!f) {
|
28
|
24
|
perror("tga_check> Passed invalid file.\n");
|
29
|
25
|
return -1;
|
30
|
26
|
}
|
|
@@ -33,22 +29,17 @@ int tga_check(FILE *f)
|
33
|
29
|
fseek(f, 0, SEEK_SET);
|
34
|
30
|
fread(buffer, 8, 1, f);
|
35
|
31
|
|
36
|
|
- // buffer[1] = 0 - Means not color mapped ( 1 would mean mapped )
|
|
32
|
+ // buffer[1] = 0 - Means not color mapped (1 would mean mapped)
|
37
|
33
|
if (!(buffer[1] == 0 && (buffer[2] == TGA_TYPE__COLOR ||
|
38
|
34
|
//buffer[2] == TGA_TYPE__GREYSCALE ||
|
39
|
|
- buffer[2] == TGA_TYPE__COLOR_RLE)))
|
40
|
|
- {
|
|
35
|
+ buffer[2] == TGA_TYPE__COLOR_RLE))) {
|
41
|
36
|
printf("tga_check> Inavlid or unknown TGA format.\n");
|
42
|
37
|
return -2;
|
43
|
38
|
}
|
44
|
|
-
|
45
|
39
|
return 0;
|
46
|
40
|
}
|
47
|
41
|
|
48
|
|
-
|
49
|
|
-int tga_load(FILE *f, unsigned char **image,
|
50
|
|
- unsigned int *width, unsigned int *height, char *type)
|
51
|
|
-{
|
|
42
|
+int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *height, char *type) {
|
52
|
43
|
tga_t header;
|
53
|
44
|
char comment[256];
|
54
|
45
|
unsigned char pixel[4];
|
|
@@ -58,9 +49,7 @@ int tga_load(FILE *f, unsigned char **image,
|
58
|
49
|
unsigned int size;
|
59
|
50
|
unsigned int i, j;
|
60
|
51
|
|
61
|
|
-
|
62
|
|
- if (!f)
|
63
|
|
- {
|
|
52
|
+ if (!f) {
|
64
|
53
|
fprintf(stderr, "tga_load> Invalid parameters.\n");
|
65
|
54
|
return -1;
|
66
|
55
|
}
|
|
@@ -101,19 +90,17 @@ int tga_load(FILE *f, unsigned char **image,
|
101
|
90
|
*width = header.width;
|
102
|
91
|
*height = header.height;
|
103
|
92
|
|
104
|
|
- switch (header.bpp)
|
105
|
|
- {
|
|
93
|
+ switch (header.bpp) {
|
106
|
94
|
case 32:
|
107
|
|
- *type = 2;//32;
|
|
95
|
+ *type = 2; //32;
|
108
|
96
|
break;
|
109
|
97
|
case 24:
|
110
|
|
- *type = 1;//24;
|
|
98
|
+ *type = 1; //24;
|
111
|
99
|
break;
|
112
|
100
|
case 8:
|
113
|
|
- *type = 0;//8;
|
114
|
|
- break;
|
115
|
101
|
default:
|
116
|
|
- *type = 0;
|
|
102
|
+ *type = 0; //8;
|
|
103
|
+ break;
|
117
|
104
|
}
|
118
|
105
|
|
119
|
106
|
#ifdef DEBUG_TGA
|
|
@@ -124,78 +111,50 @@ int tga_load(FILE *f, unsigned char **image,
|
124
|
111
|
#endif
|
125
|
112
|
|
126
|
113
|
// Comments can be 0 - 255
|
127
|
|
- if (header.comment_lenght)
|
128
|
|
- {
|
|
114
|
+ if (header.comment_lenght) {
|
129
|
115
|
fread(&comment, 1, header.comment_lenght, f);
|
130
|
|
-
|
131
|
|
- for (i = 0; i < 255; ++i)
|
132
|
|
- {
|
133
|
|
- if (comment[i] > 32 && comment[i] < 127)
|
134
|
|
- {
|
135
|
|
- }
|
136
|
|
- else
|
137
|
|
- {
|
|
116
|
+ for (i = 0; i < 255; ++i) {
|
|
117
|
+ if (!(comment[i] > 32 && comment[i] < 127))
|
138
|
118
|
comment[i] = 183; // print a dot for invalid text
|
139
|
|
- }
|
140
|
119
|
}
|
141
|
|
-
|
142
|
120
|
comment[255] = 0;
|
143
|
|
-
|
144
|
121
|
printf("Comment: '%s'\n", comment);
|
145
|
122
|
}
|
146
|
123
|
|
|
124
|
+ *image = NULL;
|
147
|
125
|
size = header.width * header.height;
|
148
|
126
|
|
149
|
|
- if (!size || (!(header.colormap_type == 0 &&
|
150
|
|
- (header.image_type == 2 || header.image_type == 10))))
|
151
|
|
- {
|
|
127
|
+ if (!size || (!(header.colormap_type == 0 && (header.image_type == 2 || header.image_type == 10)))) {
|
152
|
128
|
fprintf(stderr, "tga_load> Unknown image format.\n");
|
153
|
129
|
return -2;
|
154
|
130
|
}
|
155
|
131
|
|
156
|
|
- *image = NULL;
|
157
|
|
-
|
158
|
132
|
// Mongoose: Added 'screen origin bit' support back here
|
159
|
|
- if (!(header.desc_flags & 32))
|
160
|
|
- {
|
|
133
|
+ if (!(header.desc_flags & 32)) {
|
161
|
134
|
must_flip = true;
|
162
|
135
|
}
|
163
|
136
|
|
164
|
|
- switch (header.bpp)
|
165
|
|
- {
|
|
137
|
+ switch (header.bpp) {
|
166
|
138
|
case 32:
|
167
|
139
|
size *= 4;
|
168
|
140
|
*image = new unsigned char [size];
|
169
|
|
-
|
170
|
|
- switch (header.image_type)
|
171
|
|
- {
|
|
141
|
+ switch (header.image_type) {
|
172
|
142
|
case TGA_TYPE__COLOR_RLE:
|
173
|
|
- for (i = 0; i < size;)
|
174
|
|
- {
|
|
143
|
+ for (i = 0; i < size;) {
|
175
|
144
|
fread(&packet, 1, 1, f);
|
176
|
|
-
|
177
|
|
- if (packet & 0x80) // Run Lenght
|
178
|
|
- {
|
|
145
|
+ if (packet & 0x80) { // Run Length
|
179
|
146
|
packet = (packet &0x7F) + 1;
|
180
|
|
-
|
181
|
147
|
fread(&pixel, 4, 1, f);
|
182
|
|
-
|
183
|
|
- for (j = 0; j < packet; j++)
|
184
|
|
- {
|
|
148
|
+ for (j = 0; j < packet; j++) {
|
185
|
149
|
(*image)[i++] = pixel[2];
|
186
|
150
|
(*image)[i++] = pixel[1];
|
187
|
151
|
(*image)[i++] = pixel[0];
|
188
|
152
|
(*image)[i++] = pixel[3];
|
189
|
153
|
}
|
190
|
|
- }
|
191
|
|
- else // RAW
|
192
|
|
- {
|
|
154
|
+ } else { // RAW
|
193
|
155
|
packet = (packet &0x7F) + 1;
|
194
|
|
-
|
195
|
|
- for (j = 0; j < packet; j++)
|
196
|
|
- {
|
|
156
|
+ for (j = 0; j < packet; j++) {
|
197
|
157
|
fread(&pixel, 4, 1, f);
|
198
|
|
-
|
199
|
158
|
(*image)[i++] = pixel[2];
|
200
|
159
|
(*image)[i++] = pixel[1];
|
201
|
160
|
(*image)[i++] = pixel[0];
|
|
@@ -205,15 +164,12 @@ int tga_load(FILE *f, unsigned char **image,
|
205
|
164
|
}
|
206
|
165
|
break;
|
207
|
166
|
case TGA_TYPE__COLOR:
|
208
|
|
- if (fread((*image), size, 1, f) < 1)
|
209
|
|
- {
|
|
167
|
+ if (fread((*image), size, 1, f) < 1) {
|
210
|
168
|
fprintf(stderr, "tga_load> Image fread failed.\n");
|
211
|
169
|
delete [] *image;
|
212
|
170
|
return -4;
|
213
|
171
|
}
|
214
|
|
-
|
215
|
|
- for (i = 0; i < size; i += 4)
|
216
|
|
- {
|
|
172
|
+ for (i = 0; i < size; i += 4) {
|
217
|
173
|
tmp = (*image)[i];
|
218
|
174
|
(*image)[i] = (*image)[i + 2];
|
219
|
175
|
(*image)[i + 2] = tmp;
|
|
@@ -222,54 +178,36 @@ int tga_load(FILE *f, unsigned char **image,
|
222
|
178
|
default:
|
223
|
179
|
;
|
224
|
180
|
}
|
225
|
|
-
|
226
|
|
- if (must_flip)
|
227
|
|
- {
|
|
181
|
+ if (must_flip) {
|
228
|
182
|
swap_row = new unsigned char [header.width * 4];
|
229
|
|
-
|
230
|
|
- for (i = 0, j = header.height-1; (int)i < header.height/2; i++, j--)
|
231
|
|
- {
|
|
183
|
+ for (i = 0, j = header.height-1; (int)i < header.height/2; i++, j--) {
|
232
|
184
|
memcpy(swap_row, &(*image)[i*header.width*4], header.width*4);
|
233
|
185
|
memcpy(&(*image)[i*header.width*4], &(*image)[j*header.width*4],
|
234
|
186
|
header.width*4);
|
235
|
187
|
memcpy(&(*image)[j*header.width*4], swap_row, header.width*4);
|
236
|
188
|
}
|
237
|
|
-
|
238
|
189
|
delete [] swap_row;
|
239
|
190
|
}
|
240
|
191
|
break;
|
241
|
192
|
case 24:
|
242
|
193
|
size *= 3;
|
243
|
194
|
*image = new unsigned char [size];
|
244
|
|
-
|
245
|
|
- switch (header.image_type)
|
246
|
|
- {
|
|
195
|
+ switch (header.image_type) {
|
247
|
196
|
case TGA_TYPE__COLOR_RLE:
|
248
|
|
- for (i = 0; i < size;)
|
249
|
|
- {
|
|
197
|
+ for (i = 0; i < size;) {
|
250
|
198
|
fread(&packet, 1, 1, f);
|
251
|
|
-
|
252
|
|
- if (packet & 0x80) // Run Lenght
|
253
|
|
- {
|
|
199
|
+ if (packet & 0x80) { // Run Length
|
254
|
200
|
packet = (packet &0x7F) + 1;
|
255
|
|
-
|
256
|
201
|
fread(&pixel, 3, 1, f);
|
257
|
|
-
|
258
|
|
- for (j = 0; j < packet; j++)
|
259
|
|
- {
|
|
202
|
+ for (j = 0; j < packet; j++) {
|
260
|
203
|
(*image)[i++] = pixel[2];
|
261
|
204
|
(*image)[i++] = pixel[1];
|
262
|
205
|
(*image)[i++] = pixel[0];
|
263
|
206
|
}
|
264
|
|
- }
|
265
|
|
- else // RAW
|
266
|
|
- {
|
|
207
|
+ } else { // RAW
|
267
|
208
|
packet = (packet &0x7F) + 1;
|
268
|
|
-
|
269
|
|
- for (j = 0; j < packet; j++)
|
270
|
|
- {
|
|
209
|
+ for (j = 0; j < packet; j++) {
|
271
|
210
|
fread(&pixel, 3, 1, f);
|
272
|
|
-
|
273
|
211
|
(*image)[i++] = pixel[2];
|
274
|
212
|
(*image)[i++] = pixel[1];
|
275
|
213
|
(*image)[i++] = pixel[0];
|
|
@@ -278,15 +216,12 @@ int tga_load(FILE *f, unsigned char **image,
|
278
|
216
|
}
|
279
|
217
|
break;
|
280
|
218
|
case TGA_TYPE__COLOR:
|
281
|
|
- if (fread((*image), size, 1, f) < 1)
|
282
|
|
- {
|
|
219
|
+ if (fread((*image), size, 1, f) < 1) {
|
283
|
220
|
fprintf(stderr, "tga_load> Image fread failed.\n");
|
284
|
221
|
delete [] *image;
|
285
|
222
|
return -4;
|
286
|
223
|
}
|
287
|
|
-
|
288
|
|
- for (i = 0; i < size; i += 3)
|
289
|
|
- {
|
|
224
|
+ for (i = 0; i < size; i += 3) {
|
290
|
225
|
tmp = (*image)[i];
|
291
|
226
|
(*image)[i] = (*image)[i + 2];
|
292
|
227
|
(*image)[i + 2] = tmp;
|
|
@@ -295,22 +230,16 @@ int tga_load(FILE *f, unsigned char **image,
|
295
|
230
|
default:
|
296
|
231
|
;
|
297
|
232
|
}
|
298
|
|
-
|
299
|
|
- if (must_flip)
|
300
|
|
- {
|
|
233
|
+ if (must_flip) {
|
301
|
234
|
swap_row = new unsigned char [header.width * 3];
|
302
|
|
-
|
303
|
|
- for (i = 0, j = header.height - 1; (int)i < header.height / 2; i++, j--)
|
304
|
|
- {
|
|
235
|
+ for (i = 0, j = header.height - 1; (int)i < header.height / 2; i++, j--) {
|
305
|
236
|
memcpy(swap_row, &(*image)[i*header.width*3], header.width*3);
|
306
|
237
|
memcpy(&(*image)[i*header.width*3], &(*image)[j*header.width*3],
|
307
|
238
|
header.width*3);
|
308
|
239
|
memcpy(&(*image)[j*header.width*3], swap_row, header.width*3);
|
309
|
240
|
}
|
310
|
|
-
|
311
|
241
|
delete [] swap_row;
|
312
|
242
|
}
|
313
|
|
-
|
314
|
243
|
break;
|
315
|
244
|
case 8:
|
316
|
245
|
printf("tga_load> 8bpp Not implemented\n");
|
|
@@ -321,39 +250,29 @@ int tga_load(FILE *f, unsigned char **image,
|
321
|
250
|
|
322
|
251
|
#ifdef DEBUG_TGA
|
323
|
252
|
char c;
|
324
|
|
-
|
325
|
|
- printf("Comment:\n");
|
326
|
|
-
|
327
|
|
- while (fread(&c, 1, 1, f) == 1)
|
328
|
|
- {
|
|
253
|
+ printf("TGA Comment: ");
|
|
254
|
+ while (fread(&c, 1, 1, f) == 1) {
|
329
|
255
|
printf("%c", c);
|
330
|
256
|
}
|
331
|
|
-
|
332
|
257
|
printf("\n");
|
333
|
258
|
#endif
|
334
|
259
|
|
335
|
260
|
return 0;
|
336
|
261
|
}
|
337
|
262
|
|
338
|
|
-
|
339
|
|
-int tga_save(FILE *f, unsigned char *image,
|
340
|
|
- unsigned int width, unsigned int height, char type)
|
341
|
|
-{
|
|
263
|
+int tga_save(FILE *f, unsigned char *image, unsigned int width, unsigned int height, char type) {
|
342
|
264
|
tga_t header;
|
343
|
265
|
unsigned int size;
|
344
|
|
- // unsigned int i;
|
345
|
|
- // unsigned char tmp;
|
346
|
266
|
char comment[64];
|
|
267
|
+ //unsigned int i;
|
|
268
|
+ //unsigned char tmp;
|
347
|
269
|
|
348
|
|
-
|
349
|
|
- if (!f || !image || !width || !height)
|
350
|
|
- {
|
|
270
|
+ if (!f || !image || !width || !height) {
|
351
|
271
|
fprintf(stderr, "tga_save> Invalid parameters.\n");
|
352
|
272
|
return -1;
|
353
|
273
|
}
|
354
|
274
|
|
355
|
|
- // Mongoose 2002.01.10, Heh, kind of silly
|
356
|
|
- strncpy(comment, "Mongoose TGA 20030711", 63);
|
|
275
|
+ strncpy(comment, "OpenRaider TGA", 63);
|
357
|
276
|
comment[63] = 0;
|
358
|
277
|
|
359
|
278
|
header.comment_lenght = strlen(comment);
|
|
@@ -370,8 +289,7 @@ int tga_save(FILE *f, unsigned char *image,
|
370
|
289
|
|
371
|
290
|
header.desc_flags = 0;
|
372
|
291
|
|
373
|
|
- switch (type)
|
374
|
|
- {
|
|
292
|
+ switch (type) {
|
375
|
293
|
case 4:
|
376
|
294
|
header.image_type = TGA_TYPE__COLOR;
|
377
|
295
|
header.desc_flags = 32;
|
|
@@ -406,10 +324,7 @@ int tga_save(FILE *f, unsigned char *image,
|
406
|
324
|
// Write comment
|
407
|
325
|
fwrite(&comment, 1, header.comment_lenght, f);
|
408
|
326
|
|
409
|
|
- size = header.width * header.height;
|
410
|
|
-
|
411
|
|
- switch (header.bpp)
|
412
|
|
- {
|
|
327
|
+ switch (header.bpp) {
|
413
|
328
|
case 32:
|
414
|
329
|
size = header.width * header.height * 4;
|
415
|
330
|
|
|
@@ -431,47 +346,34 @@ int tga_save(FILE *f, unsigned char *image,
|
431
|
346
|
//}
|
432
|
347
|
break;
|
433
|
348
|
case 8:
|
|
349
|
+ default:
|
434
|
350
|
size = header.width * header.height;
|
435
|
351
|
break;
|
436
|
352
|
}
|
437
|
353
|
|
438
|
354
|
// Write image data
|
439
|
|
- if (fwrite(image, size, 1, f) < 1)
|
440
|
|
- {
|
|
355
|
+ if (fwrite(image, size, 1, f) < 1) {
|
441
|
356
|
perror("tga_save> Disk write failed.\n");
|
442
|
357
|
return -2;
|
443
|
358
|
}
|
444
|
|
-
|
445
|
359
|
return 0;
|
446
|
360
|
}
|
447
|
361
|
|
448
|
|
-
|
449
|
|
-int tga_save_filename(unsigned char *image,
|
450
|
|
- unsigned int width, unsigned int height,
|
451
|
|
- char type,
|
452
|
|
- char *s, ...)
|
453
|
|
-{
|
|
362
|
+int tga_save_filename(unsigned char *image, unsigned int width, unsigned int height, char type, char *s, ...) {
|
454
|
363
|
char buffer[1024];
|
455
|
364
|
FILE *f;
|
456
|
365
|
int v;
|
457
|
366
|
va_list args;
|
458
|
|
-
|
459
|
|
-
|
460
|
367
|
va_start(args, s);
|
461
|
368
|
vsnprintf(buffer, 1023, s, args);
|
462
|
369
|
va_end(args);
|
463
|
|
-
|
464
|
370
|
f = fopen(buffer, "wb");
|
465
|
|
-
|
466
|
|
- if (!f)
|
467
|
|
- {
|
|
371
|
+ if (!f) {
|
468
|
372
|
perror(buffer);
|
469
|
373
|
return -1;
|
470
|
374
|
}
|
471
|
|
-
|
472
|
375
|
v = tga_save(f, image, width, height, type);
|
473
|
376
|
fclose(f);
|
474
|
|
-
|
475
|
377
|
return v;
|
476
|
378
|
}
|
477
|
379
|
|