Pārlūkot izejas kodu

Nicer style for tga.cpp

Thomas Buck 11 gadus atpakaļ
vecāks
revīzija
f8a352ed38
1 mainītis faili ar 51 papildinājumiem un 149 dzēšanām
  1. 51
    149
      src/tga.cpp

+ 51
- 149
src/tga.cpp Parādīt failu

17
 #include <memory_test.h>
17
 #include <memory_test.h>
18
 #endif
18
 #endif
19
 
19
 
20
-
21
-int tga_check(FILE *f)
22
-{
20
+int tga_check(FILE *f) {
23
     char buffer[10];
21
     char buffer[10];
24
 
22
 
25
-
26
-    if (!f)
27
-    {
23
+    if (!f) {
28
         perror("tga_check> Passed invalid file.\n");
24
         perror("tga_check> Passed invalid file.\n");
29
         return -1;
25
         return -1;
30
     }
26
     }
33
     fseek(f, 0, SEEK_SET);
29
     fseek(f, 0, SEEK_SET);
34
     fread(buffer, 8, 1, f);
30
     fread(buffer, 8, 1, f);
35
 
31
 
36
-    // buffer[1] = 0 - Means not color mapped ( 1 would mean mapped )
32
+    // buffer[1] = 0 - Means not color mapped (1 would mean mapped)
37
     if (!(buffer[1] == 0 && (buffer[2] == TGA_TYPE__COLOR ||
33
     if (!(buffer[1] == 0 && (buffer[2] == TGA_TYPE__COLOR ||
38
                     //buffer[2] == TGA_TYPE__GREYSCALE ||
34
                     //buffer[2] == TGA_TYPE__GREYSCALE ||
39
-                    buffer[2] == TGA_TYPE__COLOR_RLE)))
40
-    {
35
+                    buffer[2] == TGA_TYPE__COLOR_RLE))) {
41
         printf("tga_check> Inavlid or unknown TGA format.\n");
36
         printf("tga_check> Inavlid or unknown TGA format.\n");
42
         return -2;
37
         return -2;
43
     }
38
     }
44
-
45
     return 0;
39
     return 0;
46
 }
40
 }
47
 
41
 
48
-
49
-int tga_load(FILE *f, unsigned char **image,
50
-        unsigned int *width, unsigned int *height, char *type)
51
-{
42
+int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *height, char *type) {
52
     tga_t header;
43
     tga_t header;
53
     char comment[256];
44
     char comment[256];
54
     unsigned char pixel[4];
45
     unsigned char pixel[4];
58
     unsigned int size;
49
     unsigned int size;
59
     unsigned int i, j;
50
     unsigned int i, j;
60
 
51
 
61
-
62
-    if (!f)
63
-    {
52
+    if (!f) {
64
         fprintf(stderr, "tga_load> Invalid parameters.\n");
53
         fprintf(stderr, "tga_load> Invalid parameters.\n");
65
         return -1;
54
         return -1;
66
     }
55
     }
101
     *width = header.width;
90
     *width = header.width;
102
     *height = header.height;
91
     *height = header.height;
103
 
92
 
104
-    switch (header.bpp)
105
-    {
93
+    switch (header.bpp) {
106
         case 32:
94
         case 32:
107
-            *type = 2;//32;
95
+            *type = 2; //32;
108
             break;
96
             break;
109
         case 24:
97
         case 24:
110
-            *type = 1;//24;
98
+            *type = 1; //24;
111
             break;
99
             break;
112
         case 8:
100
         case 8:
113
-            *type = 0;//8;
114
-            break;
115
         default:
101
         default:
116
-            *type = 0;
102
+            *type = 0; //8;
103
+            break;
117
     }
104
     }
118
 
105
 
119
 #ifdef DEBUG_TGA
106
 #ifdef DEBUG_TGA
124
 #endif
111
 #endif
125
 
112
 
126
     // Comments can be 0 - 255
113
     // Comments can be 0 - 255
127
-    if (header.comment_lenght)
128
-    {
114
+    if (header.comment_lenght) {
129
         fread(&comment, 1, header.comment_lenght, f);
115
         fread(&comment, 1, header.comment_lenght, f);
130
-
131
-        for (i = 0; i < 255; ++i)
132
-        {
133
-            if (comment[i] > 32 && comment[i] < 127)
134
-            {
135
-            }
136
-            else
137
-            {
116
+        for (i = 0; i < 255; ++i) {
117
+            if (!(comment[i] > 32 && comment[i] < 127))
138
                 comment[i] = 183; // print a dot for invalid text
118
                 comment[i] = 183; // print a dot for invalid text
139
-            }
140
         }
119
         }
141
-
142
         comment[255] = 0;
120
         comment[255] = 0;
143
-
144
         printf("Comment: '%s'\n", comment);
121
         printf("Comment: '%s'\n", comment);
145
     }
122
     }
146
 
123
 
124
+    *image = NULL;
147
     size = header.width * header.height;
125
     size = header.width * header.height;
148
 
126
 
149
-    if (!size || (!(header.colormap_type == 0 &&
150
-                    (header.image_type == 2 || header.image_type == 10))))
151
-    {
127
+    if (!size || (!(header.colormap_type == 0 && (header.image_type == 2 || header.image_type == 10)))) {
152
         fprintf(stderr, "tga_load> Unknown image format.\n");
128
         fprintf(stderr, "tga_load> Unknown image format.\n");
153
         return -2;
129
         return -2;
154
     }
130
     }
155
 
131
 
156
-    *image = NULL;
157
-
158
     // Mongoose: Added 'screen origin bit' support back here
132
     // Mongoose: Added 'screen origin bit' support back here
159
-    if (!(header.desc_flags & 32))
160
-    {
133
+    if (!(header.desc_flags & 32)) {
161
         must_flip = true;
134
         must_flip = true;
162
     }
135
     }
163
 
136
 
164
-    switch (header.bpp)
165
-    {
137
+    switch (header.bpp) {
166
         case 32:
138
         case 32:
167
             size *= 4;
139
             size *= 4;
168
             *image = new unsigned char [size];
140
             *image = new unsigned char [size];
169
-
170
-            switch (header.image_type)
171
-            {
141
+            switch (header.image_type) {
172
                 case TGA_TYPE__COLOR_RLE:
142
                 case TGA_TYPE__COLOR_RLE:
173
-                    for (i = 0; i < size;)
174
-                    {
143
+                    for (i = 0; i < size;) {
175
                         fread(&packet, 1, 1, f);
144
                         fread(&packet, 1, 1, f);
176
-
177
-                        if (packet & 0x80)  // Run Lenght
178
-                        {
145
+                        if (packet & 0x80) { // Run Length
179
                             packet = (packet &0x7F) + 1;
146
                             packet = (packet &0x7F) + 1;
180
-
181
                             fread(&pixel, 4, 1, f);
147
                             fread(&pixel, 4, 1, f);
182
-
183
-                            for (j = 0; j < packet; j++)
184
-                            {
148
+                            for (j = 0; j < packet; j++) {
185
                                 (*image)[i++] = pixel[2];
149
                                 (*image)[i++] = pixel[2];
186
                                 (*image)[i++] = pixel[1];
150
                                 (*image)[i++] = pixel[1];
187
                                 (*image)[i++] = pixel[0];
151
                                 (*image)[i++] = pixel[0];
188
                                 (*image)[i++] = pixel[3];
152
                                 (*image)[i++] = pixel[3];
189
                             }
153
                             }
190
-                        }
191
-                        else // RAW
192
-                        {
154
+                        } else { // RAW
193
                             packet = (packet &0x7F) + 1;
155
                             packet = (packet &0x7F) + 1;
194
-
195
-                            for (j = 0; j < packet; j++)
196
-                            {
156
+                            for (j = 0; j < packet; j++) {
197
                                 fread(&pixel, 4, 1, f);
157
                                 fread(&pixel, 4, 1, f);
198
-
199
                                 (*image)[i++] = pixel[2];
158
                                 (*image)[i++] = pixel[2];
200
                                 (*image)[i++] = pixel[1];
159
                                 (*image)[i++] = pixel[1];
201
                                 (*image)[i++] = pixel[0];
160
                                 (*image)[i++] = pixel[0];
205
                     }
164
                     }
206
                     break;
165
                     break;
207
                 case TGA_TYPE__COLOR:
166
                 case TGA_TYPE__COLOR:
208
-                    if (fread((*image), size, 1, f) < 1)
209
-                    {
167
+                    if (fread((*image), size, 1, f) < 1) {
210
                         fprintf(stderr, "tga_load> Image fread failed.\n");
168
                         fprintf(stderr, "tga_load> Image fread failed.\n");
211
                         delete [] *image;
169
                         delete [] *image;
212
                         return -4;
170
                         return -4;
213
                     }
171
                     }
214
-
215
-                    for (i = 0; i < size; i += 4)
216
-                    {
172
+                    for (i = 0; i < size; i += 4) {
217
                         tmp = (*image)[i];
173
                         tmp = (*image)[i];
218
                         (*image)[i] = (*image)[i + 2];
174
                         (*image)[i] = (*image)[i + 2];
219
                         (*image)[i + 2] = tmp;
175
                         (*image)[i + 2] = tmp;
222
                 default:
178
                 default:
223
                     ;
179
                     ;
224
             }
180
             }
225
-
226
-            if (must_flip)
227
-            {
181
+            if (must_flip) {
228
                 swap_row = new unsigned char [header.width * 4];
182
                 swap_row = new unsigned char [header.width * 4];
229
-
230
-                for (i = 0, j = header.height-1; (int)i < header.height/2; i++, j--)
231
-                {
183
+                for (i = 0, j = header.height-1; (int)i < header.height/2; i++, j--) {
232
                     memcpy(swap_row, &(*image)[i*header.width*4], header.width*4);
184
                     memcpy(swap_row, &(*image)[i*header.width*4], header.width*4);
233
                     memcpy(&(*image)[i*header.width*4], &(*image)[j*header.width*4],
185
                     memcpy(&(*image)[i*header.width*4], &(*image)[j*header.width*4],
234
                             header.width*4);
186
                             header.width*4);
235
                     memcpy(&(*image)[j*header.width*4], swap_row, header.width*4);
187
                     memcpy(&(*image)[j*header.width*4], swap_row, header.width*4);
236
                 }
188
                 }
237
-
238
                 delete [] swap_row;
189
                 delete [] swap_row;
239
             }
190
             }
240
             break;
191
             break;
241
         case 24:
192
         case 24:
242
             size *= 3;
193
             size *= 3;
243
             *image = new unsigned char [size];
194
             *image = new unsigned char [size];
244
-
245
-            switch (header.image_type)
246
-            {
195
+            switch (header.image_type) {
247
                 case TGA_TYPE__COLOR_RLE:
196
                 case TGA_TYPE__COLOR_RLE:
248
-                    for (i = 0; i < size;)
249
-                    {
197
+                    for (i = 0; i < size;) {
250
                         fread(&packet, 1, 1, f);
198
                         fread(&packet, 1, 1, f);
251
-
252
-                        if (packet & 0x80)  // Run Lenght
253
-                        {
199
+                        if (packet & 0x80) { // Run Length
254
                             packet = (packet &0x7F) + 1;
200
                             packet = (packet &0x7F) + 1;
255
-
256
                             fread(&pixel, 3, 1, f);
201
                             fread(&pixel, 3, 1, f);
257
-
258
-                            for (j = 0; j < packet; j++)
259
-                            {
202
+                            for (j = 0; j < packet; j++) {
260
                                 (*image)[i++] = pixel[2];
203
                                 (*image)[i++] = pixel[2];
261
                                 (*image)[i++] = pixel[1];
204
                                 (*image)[i++] = pixel[1];
262
                                 (*image)[i++] = pixel[0];
205
                                 (*image)[i++] = pixel[0];
263
                             }
206
                             }
264
-                        }
265
-                        else // RAW
266
-                        {
207
+                        } else { // RAW
267
                             packet = (packet &0x7F) + 1;
208
                             packet = (packet &0x7F) + 1;
268
-
269
-                            for (j = 0; j < packet; j++)
270
-                            {
209
+                            for (j = 0; j < packet; j++) {
271
                                 fread(&pixel, 3, 1, f);
210
                                 fread(&pixel, 3, 1, f);
272
-
273
                                 (*image)[i++] = pixel[2];
211
                                 (*image)[i++] = pixel[2];
274
                                 (*image)[i++] = pixel[1];
212
                                 (*image)[i++] = pixel[1];
275
                                 (*image)[i++] = pixel[0];
213
                                 (*image)[i++] = pixel[0];
278
                     }
216
                     }
279
                     break;
217
                     break;
280
                 case TGA_TYPE__COLOR:
218
                 case TGA_TYPE__COLOR:
281
-                    if (fread((*image), size, 1, f) < 1)
282
-                    {
219
+                    if (fread((*image), size, 1, f) < 1) {
283
                         fprintf(stderr, "tga_load> Image fread failed.\n");
220
                         fprintf(stderr, "tga_load> Image fread failed.\n");
284
                         delete [] *image;
221
                         delete [] *image;
285
                         return -4;
222
                         return -4;
286
                     }
223
                     }
287
-
288
-                    for (i = 0; i < size; i += 3)
289
-                    {
224
+                    for (i = 0; i < size; i += 3) {
290
                         tmp = (*image)[i];
225
                         tmp = (*image)[i];
291
                         (*image)[i] = (*image)[i + 2];
226
                         (*image)[i] = (*image)[i + 2];
292
                         (*image)[i + 2] = tmp;
227
                         (*image)[i + 2] = tmp;
295
                 default:
230
                 default:
296
                     ;
231
                     ;
297
             }
232
             }
298
-
299
-            if (must_flip)
300
-            {
233
+            if (must_flip) {
301
                 swap_row = new unsigned char [header.width * 3];
234
                 swap_row = new unsigned char [header.width * 3];
302
-
303
-                for (i = 0, j = header.height - 1; (int)i < header.height / 2; i++, j--)
304
-                {
235
+                for (i = 0, j = header.height - 1; (int)i < header.height / 2; i++, j--) {
305
                     memcpy(swap_row, &(*image)[i*header.width*3], header.width*3);
236
                     memcpy(swap_row, &(*image)[i*header.width*3], header.width*3);
306
                     memcpy(&(*image)[i*header.width*3], &(*image)[j*header.width*3],
237
                     memcpy(&(*image)[i*header.width*3], &(*image)[j*header.width*3],
307
                             header.width*3);
238
                             header.width*3);
308
                     memcpy(&(*image)[j*header.width*3], swap_row, header.width*3);
239
                     memcpy(&(*image)[j*header.width*3], swap_row, header.width*3);
309
                 }
240
                 }
310
-
311
                 delete [] swap_row;
241
                 delete [] swap_row;
312
             }
242
             }
313
-
314
             break;
243
             break;
315
         case 8:
244
         case 8:
316
             printf("tga_load> 8bpp Not implemented\n");
245
             printf("tga_load> 8bpp Not implemented\n");
321
 
250
 
322
 #ifdef DEBUG_TGA
251
 #ifdef DEBUG_TGA
323
     char c;
252
     char c;
324
-
325
-    printf("Comment:\n");
326
-
327
-    while (fread(&c, 1, 1, f) == 1)
328
-    {
253
+    printf("TGA Comment: ");
254
+    while (fread(&c, 1, 1, f) == 1) {
329
         printf("%c", c);
255
         printf("%c", c);
330
     }
256
     }
331
-
332
     printf("\n");
257
     printf("\n");
333
 #endif
258
 #endif
334
 
259
 
335
     return 0;
260
     return 0;
336
 }
261
 }
337
 
262
 
338
-
339
-int tga_save(FILE *f, unsigned char *image,
340
-        unsigned int width, unsigned int height, char type)
341
-{
263
+int tga_save(FILE *f, unsigned char *image, unsigned int width, unsigned int height, char type) {
342
     tga_t header;
264
     tga_t header;
343
     unsigned int size;
265
     unsigned int size;
344
-    //  unsigned int i;
345
-    //  unsigned char tmp;
346
     char comment[64];
266
     char comment[64];
267
+    //unsigned int i;
268
+    //unsigned char tmp;
347
 
269
 
348
-
349
-    if (!f || !image || !width || !height)
350
-    {
270
+    if (!f || !image || !width || !height) {
351
         fprintf(stderr, "tga_save> Invalid parameters.\n");
271
         fprintf(stderr, "tga_save> Invalid parameters.\n");
352
         return -1;
272
         return -1;
353
     }
273
     }
354
 
274
 
355
-    // Mongoose 2002.01.10, Heh, kind of silly
356
-    strncpy(comment, "Mongoose TGA 20030711", 63);
275
+    strncpy(comment, "OpenRaider TGA", 63);
357
     comment[63] = 0;
276
     comment[63] = 0;
358
 
277
 
359
     header.comment_lenght = strlen(comment);
278
     header.comment_lenght = strlen(comment);
370
 
289
 
371
     header.desc_flags = 0;
290
     header.desc_flags = 0;
372
 
291
 
373
-    switch (type)
374
-    {
292
+    switch (type) {
375
         case 4:
293
         case 4:
376
             header.image_type = TGA_TYPE__COLOR;
294
             header.image_type = TGA_TYPE__COLOR;
377
             header.desc_flags = 32;
295
             header.desc_flags = 32;
406
     // Write comment
324
     // Write comment
407
     fwrite(&comment, 1, header.comment_lenght, f);
325
     fwrite(&comment, 1, header.comment_lenght, f);
408
 
326
 
409
-    size = header.width * header.height;
410
-
411
-    switch (header.bpp)
412
-    {
327
+    switch (header.bpp) {
413
         case 32:
328
         case 32:
414
             size = header.width * header.height * 4;
329
             size = header.width * header.height * 4;
415
 
330
 
431
             //}
346
             //}
432
             break;
347
             break;
433
         case 8:
348
         case 8:
349
+        default:
434
             size = header.width * header.height;
350
             size = header.width * header.height;
435
             break;
351
             break;
436
     }
352
     }
437
 
353
 
438
     // Write image data
354
     // Write image data
439
-    if (fwrite(image, size, 1, f) < 1)
440
-    {
355
+    if (fwrite(image, size, 1, f) < 1) {
441
         perror("tga_save> Disk write failed.\n");
356
         perror("tga_save> Disk write failed.\n");
442
         return -2;
357
         return -2;
443
     }
358
     }
444
-
445
     return 0;
359
     return 0;
446
 }
360
 }
447
 
361
 
448
-
449
-int tga_save_filename(unsigned char *image,
450
-        unsigned int width, unsigned int height,
451
-        char type,
452
-        char *s, ...)
453
-{
362
+int tga_save_filename(unsigned char *image, unsigned int width, unsigned int height, char type, char *s, ...) {
454
     char buffer[1024];
363
     char buffer[1024];
455
     FILE *f;
364
     FILE *f;
456
     int v;
365
     int v;
457
     va_list args;
366
     va_list args;
458
-
459
-
460
     va_start(args, s);
367
     va_start(args, s);
461
     vsnprintf(buffer, 1023, s, args);
368
     vsnprintf(buffer, 1023, s, args);
462
     va_end(args);
369
     va_end(args);
463
-
464
     f = fopen(buffer, "wb");
370
     f = fopen(buffer, "wb");
465
-
466
-    if (!f)
467
-    {
371
+    if (!f) {
468
         perror(buffer);
372
         perror(buffer);
469
         return -1;
373
         return -1;
470
     }
374
     }
471
-
472
     v = tga_save(f, image, width, height, type);
375
     v = tga_save(f, image, width, height, type);
473
     fclose(f);
376
     fclose(f);
474
-
475
     return v;
377
     return v;
476
 }
378
 }
477
 
379
 

Notiek ielāde…
Atcelt
Saglabāt