ソースを参照

tga methods now take filename instead of FILE *

Thomas Buck 10年前
コミット
07888bff23
4個のファイルの変更47行の追加26行の削除
  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 ファイルの表示

10
 
10
 
11
 /*!
11
 /*!
12
  * \brief Check if a file is a valid TGA image
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
  * \returns 0 if valid, else error condition
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
  * \brief Load a TGA image from file
19
  * \brief Load a TGA image from file
20
- * \param f valid image file
20
+ * \param filename path to file
21
  * \param image Where the pixmap will be stored (or NULL)
21
  * \param image Where the pixmap will be stored (or NULL)
22
  * \param width where the width will be stored
22
  * \param width where the width will be stored
23
  * \param height where the height will be stored
23
  * \param height where the height will be stored
24
  * \param type where the type will be stored (tga_type_t)
24
  * \param type where the type will be stored (tga_type_t)
25
  * \returns 0 on success, else error condition
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
                 unsigned int *width, unsigned int *height, char *type);
28
                 unsigned int *width, unsigned int *height, char *type);
29
 
29
 
30
 /*!
30
 /*!
31
  * \brief Save a pixel buffer into a file on disk
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
  * \param image pixmap to be stored
33
  * \param image pixmap to be stored
34
  * \param width width of pixmap/image
34
  * \param width width of pixmap/image
35
  * \param height height of pixmap/image
35
  * \param height height of pixmap/image
36
  * \param type tga type to use
36
  * \param type tga type to use
37
  * \returns 0 on success, else error condition
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
                 unsigned int width, unsigned int height, char type);
40
                 unsigned int width, unsigned int height, char type);
41
 
41
 
42
 #endif
42
 #endif

+ 2
- 5
src/Render.cpp ファイルの表示

65
     // Capture frame buffer
65
     // Capture frame buffer
66
     glReadPixels(0, 0, getWindow().getWidth(), getWindow().getHeight(), GL_BGR_EXT, GL_UNSIGNED_BYTE, image);
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
     delete [] filename;
71
     delete [] filename;
75
     delete [] image;
72
     delete [] image;

+ 2
- 9
src/TextureManager.cpp ファイルの表示

301
 }
301
 }
302
 
302
 
303
 int TextureManager::loadTGA(const char *filename) {
303
 int TextureManager::loadTGA(const char *filename) {
304
-    FILE *f;
305
     unsigned char *image = NULL;
304
     unsigned char *image = NULL;
306
     unsigned char *image2 = NULL;
305
     unsigned char *image2 = NULL;
307
     unsigned int w, h;
306
     unsigned int w, h;
311
     assert(filename != NULL);
310
     assert(filename != NULL);
312
     assert(filename[0] != '\0');
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
         image2 = scaleBuffer(image, &w, &h, (type == 2) ? 32 : 24);
316
         image2 = scaleBuffer(image, &w, &h, (type == 2) ? 32 : 24);
322
 
317
 
332
 
327
 
333
             delete [] image;
328
             delete [] image;
334
         }
329
         }
335
-
336
-        fclose(f);
337
     }
330
     }
338
 
331
 
339
     if (id == -1) {
332
     if (id == -1) {

+ 37
- 6
src/utils/tga.cpp ファイルの表示

39
     unsigned char desc_flags;       //!< Various magic bits
39
     unsigned char desc_flags;       //!< Various magic bits
40
 } tga_t;
40
 } tga_t;
41
 
41
 
42
-int tgaCheck(FILE *f) {
42
+int tgaCheck(const char *filename) {
43
     char buffer[10];
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
     /* Read the header */
54
     /* Read the header */
48
     fseek(f, 0, SEEK_SET);
55
     fseek(f, 0, SEEK_SET);
53
                     //buffer[2] == TGA_TYPE__GREYSCALE ||
60
                     //buffer[2] == TGA_TYPE__GREYSCALE ||
54
                     buffer[2] == TGA_TYPE__COLOR_RLE))) {
61
                     buffer[2] == TGA_TYPE__COLOR_RLE))) {
55
         printf("tgaCheck> Inavlid or unknown TGA format.\n");
62
         printf("tgaCheck> Inavlid or unknown TGA format.\n");
63
+        fclose(f);
56
         return -2;
64
         return -2;
57
     }
65
     }
66
+
67
+    fclose(f);
58
     return 0;
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
     tga_t header;
72
     tga_t header;
63
     char comment[256];
73
     char comment[256];
64
     unsigned char pixel[4];
74
     unsigned char pixel[4];
68
     unsigned int size;
78
     unsigned int size;
69
     unsigned int i, j;
79
     unsigned int i, j;
70
 
80
 
71
-    assert(f != NULL);
81
+    assert(filename != NULL);
82
+    assert(filename[0] != '\0');
72
     assert(image != NULL);
83
     assert(image != NULL);
73
     assert(width != NULL);
84
     assert(width != NULL);
74
     assert(height != NULL);
85
     assert(height != NULL);
75
     assert(type != NULL);
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
     fseek(f, 0, SEEK_SET);
94
     fseek(f, 0, SEEK_SET);
78
 
95
 
79
     // Read TGA header
96
     // Read TGA header
146
 
163
 
147
     if (!size || (!(header.colormap_type == 0 && (header.image_type == 2 || header.image_type == 10)))) {
164
     if (!size || (!(header.colormap_type == 0 && (header.image_type == 2 || header.image_type == 10)))) {
148
         fprintf(stderr, "tgaLoad> Unknown image format.\n");
165
         fprintf(stderr, "tgaLoad> Unknown image format.\n");
166
+        fclose(f);
149
         return -2;
167
         return -2;
150
     }
168
     }
151
 
169
 
187
                     if (fread((*image), size, 1, f) < 1) {
205
                     if (fread((*image), size, 1, f) < 1) {
188
                         fprintf(stderr, "tgaLoad> Image fread failed.\n");
206
                         fprintf(stderr, "tgaLoad> Image fread failed.\n");
189
                         delete [] *image;
207
                         delete [] *image;
208
+                        fclose(f);
190
                         return -4;
209
                         return -4;
191
                     }
210
                     }
192
                     for (i = 0; i < size; i += 4) {
211
                     for (i = 0; i < size; i += 4) {
239
                     if (fread((*image), size, 1, f) < 1) {
258
                     if (fread((*image), size, 1, f) < 1) {
240
                         fprintf(stderr, "tgaLoad> Image fread failed.\n");
259
                         fprintf(stderr, "tgaLoad> Image fread failed.\n");
241
                         delete [] *image;
260
                         delete [] *image;
261
+                        fclose(f);
242
                         return -4;
262
                         return -4;
243
                     }
263
                     }
244
                     for (i = 0; i < size; i += 3) {
264
                     for (i = 0; i < size; i += 3) {
277
     printf("\n");
297
     printf("\n");
278
 #endif
298
 #endif
279
 
299
 
300
+    fclose(f);
280
     return 0;
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
     tga_t header;
305
     tga_t header;
285
     unsigned int size;
306
     unsigned int size;
286
     char comment[64];
307
     char comment[64];
287
     //unsigned int i;
308
     //unsigned int i;
288
     //unsigned char tmp;
309
     //unsigned char tmp;
289
 
310
 
290
-    assert(f != NULL);
311
+    assert(filename != NULL);
312
+    assert(filename[0] != '\0');
291
     assert(image != NULL);
313
     assert(image != NULL);
292
     assert(width > 0);
314
     assert(width > 0);
293
     assert(height > 0);
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
     strncpy(comment, "OpenRaider TGA", 63);
323
     strncpy(comment, "OpenRaider TGA", 63);
296
     comment[63] = 0;
324
     comment[63] = 0;
297
 
325
 
374
     // Write image data
402
     // Write image data
375
     if (fwrite(image, size, 1, f) < 1) {
403
     if (fwrite(image, size, 1, f) < 1) {
376
         perror("tgaSave> Disk write failed.\n");
404
         perror("tgaSave> Disk write failed.\n");
405
+        fclose(f);
377
         return -2;
406
         return -2;
378
     }
407
     }
408
+
409
+    fclose(f);
379
     return 0;
410
     return 0;
380
 }
411
 }
381
 
412
 

読み込み中…
キャンセル
保存