|
@@ -39,10 +39,17 @@ typedef struct {
|
39
|
39
|
unsigned char desc_flags; //!< Various magic bits
|
40
|
40
|
} tga_t;
|
41
|
41
|
|
42
|
|
-int tgaCheck(FILE *f) {
|
|
42
|
+int tgaCheck(const char *filename) {
|
43
|
43
|
char buffer[10];
|
44
|
44
|
|
45
|
|
- assert(f != NULL);
|
|
45
|
+ assert(filename != NULL);
|
|
46
|
+ assert(filename[0] != '\0');
|
|
47
|
+
|
|
48
|
+ FILE *f = fopen(filename, "rb");
|
|
49
|
+ if (!f) {
|
|
50
|
+ printf("tgaCheck> File not found\n");
|
|
51
|
+ return -1;
|
|
52
|
+ }
|
46
|
53
|
|
47
|
54
|
/* Read the header */
|
48
|
55
|
fseek(f, 0, SEEK_SET);
|
|
@@ -53,12 +60,15 @@ int tgaCheck(FILE *f) {
|
53
|
60
|
//buffer[2] == TGA_TYPE__GREYSCALE ||
|
54
|
61
|
buffer[2] == TGA_TYPE__COLOR_RLE))) {
|
55
|
62
|
printf("tgaCheck> Inavlid or unknown TGA format.\n");
|
|
63
|
+ fclose(f);
|
56
|
64
|
return -2;
|
57
|
65
|
}
|
|
66
|
+
|
|
67
|
+ fclose(f);
|
58
|
68
|
return 0;
|
59
|
69
|
}
|
60
|
70
|
|
61
|
|
-int tgaLoad(FILE *f, unsigned char **image, unsigned int *width, unsigned int *height, char *type) {
|
|
71
|
+int tgaLoad(const char *filename, unsigned char **image, unsigned int *width, unsigned int *height, char *type) {
|
62
|
72
|
tga_t header;
|
63
|
73
|
char comment[256];
|
64
|
74
|
unsigned char pixel[4];
|
|
@@ -68,12 +78,19 @@ int tgaLoad(FILE *f, unsigned char **image, unsigned int *width, unsigned int *h
|
68
|
78
|
unsigned int size;
|
69
|
79
|
unsigned int i, j;
|
70
|
80
|
|
71
|
|
- assert(f != NULL);
|
|
81
|
+ assert(filename != NULL);
|
|
82
|
+ assert(filename[0] != '\0');
|
72
|
83
|
assert(image != NULL);
|
73
|
84
|
assert(width != NULL);
|
74
|
85
|
assert(height != NULL);
|
75
|
86
|
assert(type != NULL);
|
76
|
87
|
|
|
88
|
+ FILE *f = fopen(filename, "rb");
|
|
89
|
+ if (!f) {
|
|
90
|
+ printf("tgaLoad> File not found\n");
|
|
91
|
+ return -1;
|
|
92
|
+ }
|
|
93
|
+
|
77
|
94
|
fseek(f, 0, SEEK_SET);
|
78
|
95
|
|
79
|
96
|
// Read TGA header
|
|
@@ -146,6 +163,7 @@ int tgaLoad(FILE *f, unsigned char **image, unsigned int *width, unsigned int *h
|
146
|
163
|
|
147
|
164
|
if (!size || (!(header.colormap_type == 0 && (header.image_type == 2 || header.image_type == 10)))) {
|
148
|
165
|
fprintf(stderr, "tgaLoad> Unknown image format.\n");
|
|
166
|
+ fclose(f);
|
149
|
167
|
return -2;
|
150
|
168
|
}
|
151
|
169
|
|
|
@@ -187,6 +205,7 @@ int tgaLoad(FILE *f, unsigned char **image, unsigned int *width, unsigned int *h
|
187
|
205
|
if (fread((*image), size, 1, f) < 1) {
|
188
|
206
|
fprintf(stderr, "tgaLoad> Image fread failed.\n");
|
189
|
207
|
delete [] *image;
|
|
208
|
+ fclose(f);
|
190
|
209
|
return -4;
|
191
|
210
|
}
|
192
|
211
|
for (i = 0; i < size; i += 4) {
|
|
@@ -239,6 +258,7 @@ int tgaLoad(FILE *f, unsigned char **image, unsigned int *width, unsigned int *h
|
239
|
258
|
if (fread((*image), size, 1, f) < 1) {
|
240
|
259
|
fprintf(stderr, "tgaLoad> Image fread failed.\n");
|
241
|
260
|
delete [] *image;
|
|
261
|
+ fclose(f);
|
242
|
262
|
return -4;
|
243
|
263
|
}
|
244
|
264
|
for (i = 0; i < size; i += 3) {
|
|
@@ -277,21 +297,29 @@ int tgaLoad(FILE *f, unsigned char **image, unsigned int *width, unsigned int *h
|
277
|
297
|
printf("\n");
|
278
|
298
|
#endif
|
279
|
299
|
|
|
300
|
+ fclose(f);
|
280
|
301
|
return 0;
|
281
|
302
|
}
|
282
|
303
|
|
283
|
|
-int tgaSave(FILE *f, unsigned char *image, unsigned int width, unsigned int height, char type) {
|
|
304
|
+int tgaSave(const char *filename, unsigned char *image, unsigned int width, unsigned int height, char type) {
|
284
|
305
|
tga_t header;
|
285
|
306
|
unsigned int size;
|
286
|
307
|
char comment[64];
|
287
|
308
|
//unsigned int i;
|
288
|
309
|
//unsigned char tmp;
|
289
|
310
|
|
290
|
|
- assert(f != NULL);
|
|
311
|
+ assert(filename != NULL);
|
|
312
|
+ assert(filename[0] != '\0');
|
291
|
313
|
assert(image != NULL);
|
292
|
314
|
assert(width > 0);
|
293
|
315
|
assert(height > 0);
|
294
|
316
|
|
|
317
|
+ FILE *f = fopen(filename, "wb");
|
|
318
|
+ if (!f) {
|
|
319
|
+ printf("tgaSave> File not found\n");
|
|
320
|
+ return -1;
|
|
321
|
+ }
|
|
322
|
+
|
295
|
323
|
strncpy(comment, "OpenRaider TGA", 63);
|
296
|
324
|
comment[63] = 0;
|
297
|
325
|
|
|
@@ -374,8 +402,11 @@ int tgaSave(FILE *f, unsigned char *image, unsigned int width, unsigned int heig
|
374
|
402
|
// Write image data
|
375
|
403
|
if (fwrite(image, size, 1, f) < 1) {
|
376
|
404
|
perror("tgaSave> Disk write failed.\n");
|
|
405
|
+ fclose(f);
|
377
|
406
|
return -2;
|
378
|
407
|
}
|
|
408
|
+
|
|
409
|
+ fclose(f);
|
379
|
410
|
return 0;
|
380
|
411
|
}
|
381
|
412
|
|