Kaynağa Gözat

tga methods now take filename instead of FILE *

Thomas Buck 10 yıl önce
ebeveyn
işleme
07888bff23
4 değiştirilmiş dosya ile 47 ekleme ve 26 silme
  1. 6
    6
      include/utils/tga.h
  2. 2
    5
      src/Render.cpp
  3. 2
    9
      src/TextureManager.cpp
  4. 37
    6
      src/utils/tga.cpp

+ 6
- 6
include/utils/tga.h Dosyayı Görüntüle

@@ -10,33 +10,33 @@
10 10
 
11 11
 /*!
12 12
  * \brief Check if a file is a valid TGA image
13
- * \param f file to be checked
13
+ * \param filename path to file
14 14
  * \returns 0 if valid, else error condition
15 15
  */
16
-int tgaCheck(FILE *f);
16
+int tgaCheck(const char *filename);
17 17
 
18 18
 /*!
19 19
  * \brief Load a TGA image from file
20
- * \param f valid image file
20
+ * \param filename path to file
21 21
  * \param image Where the pixmap will be stored (or NULL)
22 22
  * \param width where the width will be stored
23 23
  * \param height where the height will be stored
24 24
  * \param type where the type will be stored (tga_type_t)
25 25
  * \returns 0 on success, else error condition
26 26
  */
27
-int tgaLoad(FILE *f, unsigned char **image,
27
+int tgaLoad(const char *filename, unsigned char **image,
28 28
                 unsigned int *width, unsigned int *height, char *type);
29 29
 
30 30
 /*!
31 31
  * \brief Save a pixel buffer into a file on disk
32
- * \param f file in which image will be saved
32
+ * \param filename path to file
33 33
  * \param image pixmap to be stored
34 34
  * \param width width of pixmap/image
35 35
  * \param height height of pixmap/image
36 36
  * \param type tga type to use
37 37
  * \returns 0 on success, else error condition
38 38
  */
39
-int tgaSave(FILE *f, unsigned char *image,
39
+int tgaSave(const char *filename, unsigned char *image,
40 40
                 unsigned int width, unsigned int height, char type);
41 41
 
42 42
 #endif

+ 2
- 5
src/Render.cpp Dosyayı Görüntüle

@@ -65,11 +65,8 @@ void Render::screenShot(char *filenameBase)
65 65
     // Capture frame buffer
66 66
     glReadPixels(0, 0, getWindow().getWidth(), getWindow().getHeight(), GL_BGR_EXT, GL_UNSIGNED_BYTE, image);
67 67
 
68
-    FILE *f = fopen(filename, "wb");
69
-    if (f) {
70
-        tgaSave(f, image, getWindow().getWidth(), getWindow().getHeight(), 0);
71
-        printf("Took screenshot '%s'.\n", filename);
72
-    }
68
+    tgaSave(filename, image, getWindow().getWidth(), getWindow().getHeight(), 0);
69
+    printf("Took screenshot '%s'.\n", filename);
73 70
 
74 71
     delete [] filename;
75 72
     delete [] image;

+ 2
- 9
src/TextureManager.cpp Dosyayı Görüntüle

@@ -301,7 +301,6 @@ int TextureManager::loadPCX(const char *filename) {
301 301
 }
302 302
 
303 303
 int TextureManager::loadTGA(const char *filename) {
304
-    FILE *f;
305 304
     unsigned char *image = NULL;
306 305
     unsigned char *image2 = NULL;
307 306
     unsigned int w, h;
@@ -311,12 +310,8 @@ int TextureManager::loadTGA(const char *filename) {
311 310
     assert(filename != NULL);
312 311
     assert(filename[0] != '\0');
313 312
 
314
-    f = fopen(filename, "rb");
315
-
316
-    if (!f) {
317
-        perror("Couldn't load file");
318
-    } else if (!tgaCheck(f)) {
319
-        tgaLoad(f, &image, &w, &h, &type);
313
+    if (!tgaCheck(filename)) {
314
+        tgaLoad(filename, &image, &w, &h, &type);
320 315
 
321 316
         image2 = scaleBuffer(image, &w, &h, (type == 2) ? 32 : 24);
322 317
 
@@ -332,8 +327,6 @@ int TextureManager::loadTGA(const char *filename) {
332 327
 
333 328
             delete [] image;
334 329
         }
335
-
336
-        fclose(f);
337 330
     }
338 331
 
339 332
     if (id == -1) {

+ 37
- 6
src/utils/tga.cpp Dosyayı Görüntüle

@@ -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
 

Loading…
İptal
Kaydet