Browse Source

Removed duplicated TGA writing code

Thomas Buck 10 years ago
parent
commit
f66275b65c
5 changed files with 33 additions and 130 deletions
  1. 1
    0
      ChangeLog
  2. 7
    7
      include/utils/tga.h
  3. 1
    1
      src/OpenRaider.cpp
  4. 10
    108
      src/Texture.cpp
  5. 14
    14
      src/utils/tga.cpp

+ 1
- 0
ChangeLog View File

@@ -7,6 +7,7 @@
7 7
 
8 8
 	[ 20140307 ]
9 9
 	* Removed duplicated GL initialization code
10
+	* Removed duplicated TGA writing code from Texture
10 11
 
11 12
 	[ 20140306 ]
12 13
 	* Created utility library

+ 7
- 7
include/utils/tga.h View File

@@ -46,7 +46,7 @@ typedef struct {
46 46
  * \param f file to be checked
47 47
  * \returns 0 if valid, else error condition
48 48
  */
49
-int tga_check(FILE *f);
49
+int tgaCheck(FILE *f);
50 50
 
51 51
 /*!
52 52
  * \brief Load a TGA image from file
@@ -57,7 +57,7 @@ int tga_check(FILE *f);
57 57
  * \param type where the type will be stored (tga_type_t)
58 58
  * \returns 0 on success, else error condition
59 59
  */
60
-int tga_load(FILE *f, unsigned char **image,
60
+int tgaLoad(FILE *f, unsigned char **image,
61 61
                 unsigned int *width, unsigned int *height, char *type);
62 62
 
63 63
 /*!
@@ -66,10 +66,10 @@ int tga_load(FILE *f, unsigned char **image,
66 66
  * \param image pixmap to be stored
67 67
  * \param width width of pixmap/image
68 68
  * \param height height of pixmap/image
69
- * \param type tga_type_t to use
69
+ * \param type tga type to use
70 70
  * \returns 0 on success, else error condition
71 71
  */
72
-int tga_save(FILE *f, unsigned char *image,
72
+int tgaSave(FILE *f, unsigned char *image,
73 73
                 unsigned int width, unsigned int height, char type);
74 74
 
75 75
 /*!
@@ -77,13 +77,13 @@ int tga_save(FILE *f, unsigned char *image,
77 77
  * \param image pixmap to be stored
78 78
  * \param width width of pixmap/image
79 79
  * \param height height of pixmap/image
80
- * \param type tga_type_t to use
80
+ * \param type tga type to use
81 81
  * \param s format string for file path/name
82 82
  * \returns 0 on success, else error condition
83 83
  */
84
-int tga_save_filename(unsigned char *image,
84
+int tgaSaveFilename(unsigned char *image,
85 85
                         unsigned int width, unsigned int height,
86
-                        char type, char *s, ...)
86
+                        char type, const char *s, ...)
87 87
     __attribute__((format(printf, 5, 6)));
88 88
 
89 89
 #endif

+ 1
- 1
src/OpenRaider.cpp View File

@@ -3218,7 +3218,7 @@ void OpenRaider::consoleCommand(char *cmd)
3218 3218
         m_render.screenShot(sfilename);
3219 3219
         delete [] tmp;
3220 3220
         delete [] sfilename;
3221
-        print(true, "Took screenshot");
3221
+        print(false, "Took screenshot");
3222 3222
     }
3223 3223
     else if (rc_command("fullscreen", cmd))
3224 3224
     {

+ 10
- 108
src/Texture.cpp View File

@@ -232,16 +232,7 @@ int Texture::loadFontTTF(const char *filename,
232 232
     if (texture)
233 233
     {
234 234
 #ifdef DUMP_TTF_TGA
235
-        FILE *f = fopen("ttf_font.tga", "wb");
236
-        if (f)
237
-        {
238
-            tga_save(f, texture->texture, 256, 256, 4);
239
-            fclose(f);
240
-        }
241
-        else
242
-        {
243
-            perror("ttf_font.tga");
244
-        }
235
+        tgaSaveFilename(texture->texture, 256, 256, 4, "ttf_font.tga");
245 236
 #endif
246 237
 
247 238
         gFontTest = generateFont(texture);
@@ -792,37 +783,17 @@ void Texture::bindTextureId(unsigned int n)
792 783
 }
793 784
 
794 785
 
795
-void Texture::glScreenShot(char *base, unsigned int width, unsigned int height)
796
-{
786
+void Texture::glScreenShot(char *base, unsigned int width, unsigned int height) {
797 787
     FILE *f;
798
-    int sz = width*height;
799
-    unsigned char *image = new unsigned char[sz*3];
800
-    unsigned char *swap_row = new unsigned char[width*3];
788
+    int sz = width * height;
789
+    unsigned char *image = new unsigned char[sz * 3];
801 790
     char filename[1024];
802 791
     static int count = 0;
803 792
     bool done = false;
804
-    int i, j, size;
805
-    unsigned char comment_lenght;
806
-    unsigned char colormap_type;
807
-    unsigned char image_type;
808
-    unsigned short colormap_index;
809
-    unsigned short colormap_lenght;
810
-    unsigned char colormap_bbp;
811
-    unsigned short origin_x;
812
-    unsigned short origin_y;
813
-    unsigned short swidth;
814
-    unsigned short sheight;
815
-    char comment[32] = "Mongoose TGA 0.0.1\0";
816
-    unsigned char tmp, bpp, desc_flags;
817
-
818 793
 
819
-    if (!image || !width || !height)
820
-    {
794
+    if (!image || !width || !height) {
821 795
         if (image)
822
-        {
823 796
             delete [] image;
824
-        }
825
-        delete [] swap_row;
826 797
 
827 798
         printf("glScreenShot> ERROR: Couldn't allocate image!\n");
828 799
         return;
@@ -841,82 +812,13 @@ void Texture::glScreenShot(char *base, unsigned int width, unsigned int height)
841 812
             done = true;
842 813
     }
843 814
 
844
-    f = fopen(filename, "wb");
845
-
846
-    if (!f)
847
-    {
848
-        printf("glScreenShot> ERROR: Couldn't write screenshot.\n");
849
-        perror("glScreenShot> ERROR: ");
850
-        delete [] image;
851
-        delete [] swap_row;
852
-        return;
853
-    }
854
-
855 815
     // Capture frame buffer
856
-    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
857
-
858
-    // Flip vertical
859
-    for (i = 0, j = (int)height-1; i < (int)height/2; ++i, --j)
860
-    {
861
-        memcpy(swap_row, &image[i*width*3], width*3);
862
-        memcpy(&image[i*width*3], &image[j*width*3], width*3);
863
-        memcpy(&image[j*width*3], swap_row, width*3);
864
-    }
816
+    glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, image);
865 817
 
866
-    delete [] swap_row;
867
-
868
-    comment_lenght = (unsigned char)strlen(comment);
869
-    colormap_type = 0;
870
-    image_type = 2;
871
-    colormap_index = 0;
872
-    colormap_lenght = 0;
873
-    colormap_bbp = 0;
874
-    origin_x = origin_y = 0;
875
-    swidth = (unsigned short)width;
876
-    sheight = (unsigned short)height;
877
-    bpp = 24;
878
-    desc_flags = 32;
879
-
880
-    // Write TGA header
881
-    fwrite(&comment_lenght, 1, 1, f);
882
-    fwrite(&colormap_type, 1, 1, f);
883
-    fwrite(&image_type, 1, 1, f);
884
-    fwrite(&colormap_index, 2, 1, f);
885
-    fwrite(&colormap_lenght, 2, 1, f);
886
-    fwrite(&colormap_bbp, 1, 1, f);
887
-    fwrite(&origin_x, 2, 1, f);
888
-    fwrite(&origin_y, 2, 1, f);
889
-    fwrite(&swidth, 2, 1, f);
890
-    fwrite(&sheight, 2, 1, f);
891
-    fwrite(&bpp, 1, 1, f);
892
-    fwrite(&desc_flags, 1, 1, f);
893
-
894
-    // Write comment
895
-    fwrite(&comment, 1, comment_lenght, f);
896
-
897
-    size = width * height * 3;
898
-
899
-    for (i = 0; i < size; i += 3)
900
-    {
901
-        tmp = image[i];
902
-        image[i] = image[i + 2];
903
-        image[i + 2] = tmp;
904
-    }
905
-
906
-    // Write image data
907
-    if (fwrite(image, size, 1, f) < 1)
908
-    {
909
-        perror("glScreenShot> Disk write failed.\n");
910
-        fclose(f);
911
-        delete [] image;
912
-        return;
913
-    }
914
-
915
-    fclose(f);
818
+    tgaSaveFilename(image, width, height, 0, "%s", filename);
819
+    printf("Took screenshot '%s'.\n", filename);
916 820
 
917 821
     delete [] image;
918
-
919
-    printf("Took screenshot '%s'.\n", filename);
920 822
 }
921 823
 
922 824
 
@@ -936,9 +838,9 @@ int Texture::loadTGA(const char *filename)
936 838
     {
937 839
         perror("Couldn't load file");
938 840
     }
939
-    else if (!tga_check(f))
841
+    else if (!tgaCheck(f))
940 842
     {
941
-        tga_load(f, &image, &w, &h, &type);
843
+        tgaLoad(f, &image, &w, &h, &type);
942 844
 
943 845
         type += 2;
944 846
 

+ 14
- 14
src/utils/tga.cpp View File

@@ -14,11 +14,11 @@
14 14
 
15 15
 #include "utils/tga.h"
16 16
 
17
-int tga_check(FILE *f) {
17
+int tgaCheck(FILE *f) {
18 18
     char buffer[10];
19 19
 
20 20
     if (!f) {
21
-        perror("tga_check> Passed invalid file.\n");
21
+        perror("tgaCheck> Passed invalid file.\n");
22 22
         return -1;
23 23
     }
24 24
 
@@ -30,13 +30,13 @@ int tga_check(FILE *f) {
30 30
     if (!(buffer[1] == 0 && (buffer[2] == TGA_TYPE__COLOR ||
31 31
                     //buffer[2] == TGA_TYPE__GREYSCALE ||
32 32
                     buffer[2] == TGA_TYPE__COLOR_RLE))) {
33
-        printf("tga_check> Inavlid or unknown TGA format.\n");
33
+        printf("tgaCheck> Inavlid or unknown TGA format.\n");
34 34
         return -2;
35 35
     }
36 36
     return 0;
37 37
 }
38 38
 
39
-int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *height, char *type) {
39
+int tgaLoad(FILE *f, unsigned char **image, unsigned int *width, unsigned int *height, char *type) {
40 40
     tga_t header;
41 41
     char comment[256];
42 42
     unsigned char pixel[4];
@@ -47,7 +47,7 @@ int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *
47 47
     unsigned int i, j;
48 48
 
49 49
     if (!f) {
50
-        fprintf(stderr, "tga_load> Invalid parameters.\n");
50
+        fprintf(stderr, "tgaLoad> Invalid parameters.\n");
51 51
         return -1;
52 52
     }
53 53
 
@@ -122,7 +122,7 @@ int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *
122 122
     size = header.width * header.height;
123 123
 
124 124
     if (!size || (!(header.colormap_type == 0 && (header.image_type == 2 || header.image_type == 10)))) {
125
-        fprintf(stderr, "tga_load> Unknown image format.\n");
125
+        fprintf(stderr, "tgaLoad> Unknown image format.\n");
126 126
         return -2;
127 127
     }
128 128
 
@@ -162,7 +162,7 @@ int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *
162 162
                     break;
163 163
                 case TGA_TYPE__COLOR:
164 164
                     if (fread((*image), size, 1, f) < 1) {
165
-                        fprintf(stderr, "tga_load> Image fread failed.\n");
165
+                        fprintf(stderr, "tgaLoad> Image fread failed.\n");
166 166
                         delete [] *image;
167 167
                         return -4;
168 168
                     }
@@ -214,7 +214,7 @@ int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *
214 214
                     break;
215 215
                 case TGA_TYPE__COLOR:
216 216
                     if (fread((*image), size, 1, f) < 1) {
217
-                        fprintf(stderr, "tga_load> Image fread failed.\n");
217
+                        fprintf(stderr, "tgaLoad> Image fread failed.\n");
218 218
                         delete [] *image;
219 219
                         return -4;
220 220
                     }
@@ -239,7 +239,7 @@ int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *
239 239
             }
240 240
             break;
241 241
         case 8:
242
-            printf("tga_load> 8bpp Not implemented\n");
242
+            printf("tgaLoad> 8bpp Not implemented\n");
243 243
             break;
244 244
         default:
245 245
             ;
@@ -257,7 +257,7 @@ int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *
257 257
     return 0;
258 258
 }
259 259
 
260
-int tga_save(FILE *f, unsigned char *image, unsigned int width, unsigned int height, char type) {
260
+int tgaSave(FILE *f, unsigned char *image, unsigned int width, unsigned int height, char type) {
261 261
     tga_t header;
262 262
     unsigned int size;
263 263
     char comment[64];
@@ -265,7 +265,7 @@ int tga_save(FILE *f, unsigned char *image, unsigned int width, unsigned int hei
265 265
     //unsigned char tmp;
266 266
 
267 267
     if (!f || !image || !width || !height) {
268
-        fprintf(stderr, "tga_save> Invalid parameters.\n");
268
+        fprintf(stderr, "tgaSave> Invalid parameters.\n");
269 269
         return -1;
270 270
     }
271 271
 
@@ -350,13 +350,13 @@ int tga_save(FILE *f, unsigned char *image, unsigned int width, unsigned int hei
350 350
 
351 351
     // Write image data
352 352
     if (fwrite(image, size, 1, f) < 1) {
353
-        perror("tga_save> Disk write failed.\n");
353
+        perror("tgaSave> Disk write failed.\n");
354 354
         return -2;
355 355
     }
356 356
     return 0;
357 357
 }
358 358
 
359
-int tga_save_filename(unsigned char *image, unsigned int width, unsigned int height, char type, char *s, ...) {
359
+int tgaSaveFilename(unsigned char *image, unsigned int width, unsigned int height, char type, const char *s, ...) {
360 360
     char buffer[1024];
361 361
     FILE *f;
362 362
     int v;
@@ -369,7 +369,7 @@ int tga_save_filename(unsigned char *image, unsigned int width, unsigned int hei
369 369
         perror(buffer);
370 370
         return -1;
371 371
     }
372
-    v = tga_save(f, image, width, height, type);
372
+    v = tgaSave(f, image, width, height, type);
373 373
     fclose(f);
374 374
     return v;
375 375
 }

Loading…
Cancel
Save