Browse Source

Renamed TGA methods, added docs for TGA

Thomas Buck 11 years ago
parent
commit
93b065ccab
4 changed files with 107 additions and 144 deletions
  1. 82
    103
      include/tga.h
  2. 3
    3
      src/Texture.cpp
  3. 20
    36
      src/tga.cpp
  4. 2
    2
      test/tga.cpp

+ 82
- 103
include/tga.h View File

1
-/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
2
-/*================================================================
1
+/*!
2
+ * \file include/tga.h
3
+ * \brief TGA image reader/writer
3
  *
4
  *
4
- * Project : Freyja
5
- * Author  : Mongoose
6
- * Website : http://www.westga.edu/~stu7440/
7
- * Email   : stu7440@westga.edu
8
- * Object  :
9
- * License : GPL See file COPYING, also (C) 2000 Mongoose
10
- * Comments: TGA plug-in
11
- *
12
- *
13
- *           This file was generated using Mongoose's C++
14
- *           template generator script.  <stu7440@westga.edu>
15
- *
16
- *-- History ------------------------------------------------
17
- *
18
- * 2000-10-15:
19
- * Mongoose - Created
20
- ================================================================*/
21
-
5
+ * \author Mongoose
6
+ * \author xythobuz
7
+ */
22
 #ifndef _TGA_H
8
 #ifndef _TGA_H
23
 #define _TGA_H
9
 #define _TGA_H
24
 
10
 
25
 #include <stdio.h>
11
 #include <stdio.h>
26
 
12
 
27
-
28
-typedef enum
29
-{
30
-  TGA_TYPE__NO_DATA    = 0,
31
-  TGA_TYPE__MAPPED     = 1,
32
-  TGA_TYPE__COLOR      = 2,
33
-  TGA_TYPE__GREYSCALE  = 3,
34
-  TGA_TYPE__MAPPED_RLE = 9,
35
-  TGA_TYPE__COLOR_RLE  = 10
36
-  //  TGA_TYPE__GREYSCALE_COMPRESSED  = 11,
37
-  //  TGA_TYPE__COLOR_HUFFMAN_DELTA_RLE  = 32,
38
-  //  TGA_TYPE__COLOR_HUFFMAN_DELTA_RLE_4PASS  = 33
39
-
13
+/*!
14
+ * \brief Possible TGA image types
15
+ */
16
+typedef enum {
17
+    TGA_TYPE__NO_DATA    = 0,
18
+    TGA_TYPE__MAPPED     = 1,
19
+    TGA_TYPE__COLOR      = 2,
20
+    TGA_TYPE__GREYSCALE  = 3,
21
+    TGA_TYPE__MAPPED_RLE = 9,
22
+    TGA_TYPE__COLOR_RLE  = 10
23
+    // TGA_TYPE__GREYSCALE_COMPRESSED  = 11,
24
+    // TGA_TYPE__COLOR_HUFFMAN_DELTA_RLE  = 32,
25
+    // TGA_TYPE__COLOR_HUFFMAN_DELTA_RLE_4PASS  = 33
40
 } tga_type_t;
26
 } tga_type_t;
41
 
27
 
28
+/*!
29
+ * \brief Data structure representing TGA file header
30
+ */
31
+typedef struct {
32
+    unsigned char comment_lenght;       /*!< Number of bytes in comment */
33
+    unsigned char colormap_type;        /*!< 0 No colormap; 1 Has colormap  */
34
+    unsigned char image_type;           /*!< 1 Colormapped; 2 Unmapped;
35
+                                             9 Colormapped RLE; 10 Unmapped RLE */
36
+    unsigned short colormap_index;      /*!< Index of first color map entry */
37
+    unsigned short colormap_lenght;     /*!< Number of color map entries */
38
+    unsigned char colormap_bbp;         /*!< 16, 24, or 32 bits per pixel format */
39
+    unsigned short origin_x;            /*!< X coor of lower-left corner */
40
+    unsigned short origin_y;            /*!< Y coor of lower-left corner */
41
+    unsigned short width;               /*!< Width in pixels */
42
+    unsigned short height;              /*!< Height in pixels */
43
+    unsigned char bpp;                  /*!< Number of bits in a pixel index */
44
+    unsigned char desc_flags;           /*!< Various magic bits */
45
+} tga_t;
46
+
47
+/*!
48
+ * \brief Check if a file is a valid TGA image
49
+ * \param f file to be checked
50
+ * \returns 0 if valid, else error condition
51
+ */
52
+int tga_check(FILE *f);
53
+
54
+/*!
55
+ * \brief Load a TGA image from file
56
+ * \param f valid image file
57
+ * \param image Where the pixmap will be stored (or NULL)
58
+ * \param width where the width will be stored
59
+ * \param height where the height will be stored
60
+ * \param type where the type will be stored (tga_type_t)
61
+ * \returns 0 on success, else error condition
62
+ */
63
+int tga_load(FILE *f, unsigned char **image,
64
+        unsigned int *width, unsigned int *height, char *type);
65
+
66
+/*!
67
+ * \brief Save a pixel buffer into a file on disk
68
+ * \param f file in which image will be saved
69
+ * \param image pixmap to be stored
70
+ * \param width width of pixmap/image
71
+ * \param height height of pixmap/image
72
+ * \param type tga_type_t to use
73
+ * \returns 0 on success, else error condition
74
+ */
75
+int tga_save(FILE *f, unsigned char *image,
76
+        unsigned int width, unsigned int height, char type);
77
+
78
+/*!
79
+ * \brief Save a pixel buffer into a file on disk
80
+ * \param image pixmap to be stored
81
+ * \param width width of pixmap/image
82
+ * \param height height of pixmap/image
83
+ * \param type tga_type_t to use
84
+ * \param s format string for file path/name
85
+ * \returns 0 on success, else error condition
86
+ */
87
+int tga_save_filename(unsigned char *image,
88
+        unsigned int width, unsigned int height,
89
+        char type,
90
+        char *s, ...);
42
 
91
 
43
-typedef struct mtk_image_tga_s
44
-{
45
-  unsigned char comment_lenght;       /* Number of bytes in comment */
46
-  unsigned char colormap_type;        /* 0 No colormap; 1 Has colormap  */
47
-  unsigned char image_type;           /* 1 Colormapped, 2 Unmapped;
48
-                     9 Colormapped RLE; 10 Unmapped RLE */
49
-
50
-  unsigned short colormap_index;      /* Index of first color map entry */
51
-  unsigned short colormap_lenght;     /* Number of color map entries */
52
-  unsigned char colormap_bbp;         /* 16, 24, or 32 bits per pixel format */
53
-
54
-  unsigned short origin_x;            /* X coor of lower-left corner */
55
-  unsigned short origin_y;            /* Y coor of lower-left corner */
56
-  unsigned short width;               /* Width in pixels */
57
-  unsigned short height;              /* Height in pixels */
58
-  unsigned char bpp;                  /* Number of bits in a pixel index */
59
-  unsigned char desc_flags;           /* Various magic bits */
60
-
61
-} mtk_image_tga_t;
62
-
63
-
64
-  int mtk_image__tga_check(FILE *f);
65
-  /*------------------------------------------------------
66
-   * Pre  : Filename is to be checked
67
-   * Post : Returns 0 if valid, else error condition
68
-   *
69
-   *-- History ------------------------------------------
70
-   *
71
-   * 2001.04.01:
72
-   * Mongoose - Created
73
-   ------------------------------------------------------*/
74
-
75
-  int mtk_image__tga_load(FILE *f, unsigned char **image,
76
-             unsigned int *width, unsigned int *height, char *type);
77
-  /*------------------------------------------------------
78
-   * Pre  : Filename is a valid image file
79
-   * Post : Image is either a valid pixmap or NULL
80
-   *
81
-   *-- History ------------------------------------------
82
-   *
83
-   * 2001.04.01:
84
-   * Mongoose - Created
85
-   ------------------------------------------------------*/
86
-
87
-  int mtk_image__tga_save(FILE *f, unsigned char *image,
88
-              unsigned int width, unsigned int height, char type);
89
-  /*------------------------------------------------------
90
-   * Pre  :
91
-   * Post : Image is saved to diskfile if valid and can
92
-   *
93
-   *-- History ------------------------------------------
94
-   *
95
-   * 2001.04.01:
96
-   * Mongoose - Created
97
-   ------------------------------------------------------*/
98
-
99
-
100
-  int mtk_image__tga_save_filename(unsigned char *image,
101
-                   unsigned int width, unsigned int height,
102
-                   char type,
103
-                   char *s, ...);
104
-  /*------------------------------------------------------
105
-   * Pre  :
106
-   * Post : Image is saved to diskfile if valid and can
107
-   *
108
-   *-- History ------------------------------------------
109
-   *
110
-   * 2003.07.11:
111
-   * Mongoose - Created
112
-   ------------------------------------------------------*/
113
 #endif
92
 #endif

+ 3
- 3
src/Texture.cpp View File

265
         FILE *f = fopen("font.tga", "wb");
265
         FILE *f = fopen("font.tga", "wb");
266
         if (f)
266
         if (f)
267
         {
267
         {
268
-            mtk_image__tga_save(f, texture->texture, 256, 256, 4);
268
+            tga_save(f, texture->texture, 256, 256, 4);
269
             fclose(f);
269
             fclose(f);
270
         }
270
         }
271
         else
271
         else
957
     {
957
     {
958
         perror("Couldn't load file");
958
         perror("Couldn't load file");
959
     }
959
     }
960
-    else if (!mtk_image__tga_check(f))
960
+    else if (!tga_check(f))
961
     {
961
     {
962
-        mtk_image__tga_load(f, &image, &w, &h, &type);
962
+        tga_load(f, &image, &w, &h, &type);
963
 
963
 
964
         type += 2;
964
         type += 2;
965
 
965
 

+ 20
- 36
src/tga.cpp View File

1
-/*================================================================
1
+/*!
2
+ * \file src/tga.cpp
3
+ * \brief TGA image reader/writer
2
  *
4
  *
3
- * Project : Freyja
4
- * Author  : Mongoose
5
- * Website : http://www.westga.edu/~stu7440/
6
- * Email   : stu7440@westga.edu
7
- * Object  :
8
- * License : GPL See file COPYING, also (C) 2000 Mongoose
9
- * Comments: TGA plug-in
5
+ * \todo type should pass more info (2 bits for RGBA|RGB|GREY; val for depth)
10
  *
6
  *
11
- *           TODO: type should pass more info
12
- *                 2 bits for RGBA | RGB | GREY
13
- *                 val for depth
14
- *
15
- *           This file was generated using Mongoose's C++
16
- *           template generator script.  <stu7440@westga.edu>
17
- *
18
- *-- History ------------------------------------------------
19
- *
20
- * 2001-10-25:
21
- * Mongoose - support for screen origin bit
22
- *
23
- * 2000-10-15:
24
- * Mongoose - Created
25
- ================================================================*/
7
+ * \author Mongoose
8
+ * \author xythobuz
9
+ */
26
 
10
 
27
 #include <string.h>
11
 #include <string.h>
28
 #include <stdarg.h>
12
 #include <stdarg.h>
34
 #endif
18
 #endif
35
 
19
 
36
 
20
 
37
-int mtk_image__tga_check(FILE *f)
21
+int tga_check(FILE *f)
38
 {
22
 {
39
     char buffer[10];
23
     char buffer[10];
40
 
24
 
62
 }
46
 }
63
 
47
 
64
 
48
 
65
-int mtk_image__tga_load(FILE *f, unsigned char **image,
49
+int tga_load(FILE *f, unsigned char **image,
66
         unsigned int *width, unsigned int *height, char *type)
50
         unsigned int *width, unsigned int *height, char *type)
67
 {
51
 {
68
-    mtk_image_tga_t header;
52
+    tga_t header;
69
     char comment[256];
53
     char comment[256];
70
     unsigned char pixel[4];
54
     unsigned char pixel[4];
71
     unsigned char *swap_row = NULL;
55
     unsigned char *swap_row = NULL;
77
 
61
 
78
     if (!f)
62
     if (!f)
79
     {
63
     {
80
-        fprintf(stderr, "mtk_image__tga_load> Invalid parameters.\n");
64
+        fprintf(stderr, "tga_load> Invalid parameters.\n");
81
         return -1;
65
         return -1;
82
     }
66
     }
83
 
67
 
165
     if (!size || (!(header.colormap_type == 0 &&
149
     if (!size || (!(header.colormap_type == 0 &&
166
                     (header.image_type == 2 || header.image_type == 10))))
150
                     (header.image_type == 2 || header.image_type == 10))))
167
     {
151
     {
168
-        fprintf(stderr, "mtk_image__tga_load> Unknown image format.\n");
152
+        fprintf(stderr, "tga_load> Unknown image format.\n");
169
         return -2;
153
         return -2;
170
     }
154
     }
171
 
155
 
223
                 case TGA_TYPE__COLOR:
207
                 case TGA_TYPE__COLOR:
224
                     if (fread((*image), size, 1, f) < 1)
208
                     if (fread((*image), size, 1, f) < 1)
225
                     {
209
                     {
226
-                        fprintf(stderr, "mtk_image__tga_load> Image fread failed.\n");
210
+                        fprintf(stderr, "tga_load> Image fread failed.\n");
227
                         delete [] *image;
211
                         delete [] *image;
228
                         return -4;
212
                         return -4;
229
                     }
213
                     }
296
                 case TGA_TYPE__COLOR:
280
                 case TGA_TYPE__COLOR:
297
                     if (fread((*image), size, 1, f) < 1)
281
                     if (fread((*image), size, 1, f) < 1)
298
                     {
282
                     {
299
-                        fprintf(stderr, "mtk_image__tga_load> Image fread failed.\n");
283
+                        fprintf(stderr, "tga_load> Image fread failed.\n");
300
                         delete [] *image;
284
                         delete [] *image;
301
                         return -4;
285
                         return -4;
302
                     }
286
                     }
352
 }
336
 }
353
 
337
 
354
 
338
 
355
-int mtk_image__tga_save(FILE *f, unsigned char *image,
339
+int tga_save(FILE *f, unsigned char *image,
356
         unsigned int width, unsigned int height, char type)
340
         unsigned int width, unsigned int height, char type)
357
 {
341
 {
358
-    mtk_image_tga_t header;
342
+    tga_t header;
359
     unsigned int size;
343
     unsigned int size;
360
     //  unsigned int i;
344
     //  unsigned int i;
361
     //  unsigned char tmp;
345
     //  unsigned char tmp;
364
 
348
 
365
     if (!f || !image || !width || !height)
349
     if (!f || !image || !width || !height)
366
     {
350
     {
367
-        fprintf(stderr, "mtk_image__tga_save> Invalid parameters.\n");
351
+        fprintf(stderr, "tga_save> Invalid parameters.\n");
368
         return -1;
352
         return -1;
369
     }
353
     }
370
 
354
 
454
     // Write image data
438
     // Write image data
455
     if (fwrite(image, size, 1, f) < 1)
439
     if (fwrite(image, size, 1, f) < 1)
456
     {
440
     {
457
-        perror("mtk_image__tga_save> Disk write failed.\n");
441
+        perror("tga_save> Disk write failed.\n");
458
         return -2;
442
         return -2;
459
     }
443
     }
460
 
444
 
462
 }
446
 }
463
 
447
 
464
 
448
 
465
-int mtk_image__tga_save_filename(unsigned char *image,
449
+int tga_save_filename(unsigned char *image,
466
         unsigned int width, unsigned int height,
450
         unsigned int width, unsigned int height,
467
         char type,
451
         char type,
468
         char *s, ...)
452
         char *s, ...)
485
         return -1;
469
         return -1;
486
     }
470
     }
487
 
471
 
488
-    v = mtk_image__tga_save(f, image, width, height, type);
472
+    v = tga_save(f, image, width, height, type);
489
     fclose(f);
473
     fclose(f);
490
 
474
 
491
     return v;
475
     return v;

+ 2
- 2
test/tga.cpp View File

20
 
20
 
21
 TEST checkFile(FILE *f) {
21
 TEST checkFile(FILE *f) {
22
     ASSERTm("File wasn't opened.", f != NULL);
22
     ASSERTm("File wasn't opened.", f != NULL);
23
-    ASSERT_FALSEm("File is invalid?!", mtk_image__tga_check(f));
23
+    ASSERT_FALSEm("File is invalid?!", tga_check(f));
24
     PASS();
24
     PASS();
25
 }
25
 }
26
 
26
 
29
     unsigned int width, height;
29
     unsigned int width, height;
30
     char type;
30
     char type;
31
     ASSERTm("File wasn't opened.", f != NULL);
31
     ASSERTm("File wasn't opened.", f != NULL);
32
-    ASSERT_FALSEm("File couldn't be loaded!", mtk_image__tga_load(f, &image, &width, &height, &type));
32
+    ASSERT_FALSEm("File couldn't be loaded!", tga_load(f, &image, &width, &height, &type));
33
     printf("\nWidth: %u\nHeight: %u\nType: %d\n", width, height, type);
33
     printf("\nWidth: %u\nHeight: %u\nType: %d\n", width, height, type);
34
     PASS();
34
     PASS();
35
 }
35
 }

Loading…
Cancel
Save