-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtgainfo.c
333 lines (286 loc) · 7.25 KB
/
tgainfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* This program prints information about 24 bit TARGA files.
*
* This is generic ANSI C, and should compile with any ANSI compliant
* compiler (e.g. gcc, Borland, Microsoft, or Lattice).
*
* History:
* 1.1 Added code for counting number of colors.
* 1.0 First version.
*/
#define VERSION "1.1"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tgadefs.h"
#if __MSDOS__
#define my_malloc(x) farmalloc((long)(x))
#define my_free(x) farfree(x)
#include <conio.h>
#include <alloc.h>
#else
#define my_malloc(x) malloc(x)
#define my_free(x) free(x)
#endif
#ifndef PATHMAX
#define PATHMAX 256
#endif
#define NO 0
#define YES 1
char *infilename; /* input file name */
int count_colors; /* flag to tell whether or not to count colors */
Pixel *srcfile; /* buffer holding loaded file */
unsigned int image_w; /* width of image in pixels from TGA header */
unsigned int image_h; /* height of image in pixels from TGA header */
int bits_per_pixel; /* bits per pixel from TGA file header */
int bytes_in_name; /* bytes in filename at end of TGA header */
int tga_flags; /* TGA file flags */
int cmap_type; /* color map type */
int sub_type; /* TGA file sub type */
int cmap_len; /* length of color map */
char *progname; /* name the program was invoked with (should be "tgainfo") */
void read_file( char * );
void
usage(void)
{
printf("%s Version %s\n\n", progname, VERSION);
printf("Usage: %s [-colors] file.tga\n", progname);
exit(2);
}
int hflip_flag, vflip_flag; /* here for historical reasons (I snarfed the code from elsewhere) */
int
main(int argc, char **argv)
{
hflip_flag = NO; /* default option is no hflip */
vflip_flag = NO; /* default option is no vflip */
count_colors = NO;
progname = *argv++;
if (!*progname) { /* if for some reason the runtime library didn't get our name... */
progname = "tgainfo"; /* assume this is our name */
}
argc--;
while (*argv) {
if (**argv != '-') break;
if (!strcmp(*argv, "-colors")) {
count_colors = YES;
} else {
usage(); /* illegal option */
}
argv++; argc--;
}
if (argc != 1) { /* should be exactly one argument left, the input file name */
usage();
}
read_file(argv[0]);
return 0;
}
void
err_eof(void)
{
fprintf(stderr, "Error: unexpected EOF\n");
exit(1);
}
/* State info for reading RLE-coded pixels; both counts must be init to 0 */
static int block_count; /* # of pixels remaining in RLE block */
static int dup_pixel_count; /* # of times to duplicate previous pixel */
static void (*read_pixel)(FILE *fhandle, Pixel *place);
static char tga_pixel[4];
/*
* read_rle_pixel: read a pixel from an RLE encoded .TGA file
*/
static void
read_rle_pixel(FILE *fhandle, Pixel *place)
{
int i;
/* if we're in the middle of reading a duplicate pixel */
if (dup_pixel_count > 0) {
dup_pixel_count--;
place->blue = tga_pixel[0];
place->green = tga_pixel[1];
place->red = tga_pixel[2];
return;
}
/* should we read an RLE block header? */
if (--block_count < 0) {
i = fgetc(fhandle);
if (i < 0) err_eof();
if (i & 0x80) {
dup_pixel_count = i & 0x7f; /* number of duplications after this one */
block_count = 0; /* then a new block header */
} else {
block_count = i & 0x7f; /* this many unduplicated pixels */
}
}
place->blue = tga_pixel[0] = fgetc(fhandle);
place->green = tga_pixel[1] = fgetc(fhandle);
place->red = tga_pixel[2] = fgetc(fhandle);
}
/*
* read a pixel from an uncompressed .TGA file
*/
static void
read_norm_pixel(FILE *fhandle, Pixel *place)
{
int i;
place->blue = fgetc(fhandle);
place->green = fgetc(fhandle);
i = fgetc(fhandle);
if (i < 0) err_eof();
place->red = i;
}
void
read_row(FILE *fhandle, Pixel *place)
{
int i;
void (*rpixel)(FILE *, Pixel *);
rpixel = read_pixel;
if (hflip_flag) {
place += image_w;
for (i = 0; i < image_w; i++) {
place--;
(*rpixel)(fhandle, place);
}
} else {
for (i = 0; i < image_w; i++) {
(*rpixel)(fhandle, place);
place++;
}
}
}
long color_count[32768];
/*
* routines to convert to/from color indexes
*/
static INLINE int
HASH(Pixel pix)
{
int index;
index = ((int)pix.red>>3) << 5;
index |= (pix.green>>3);
index = (index << 5)| (pix.blue>>3);
return index;
}
static INLINE Pixel
UNHASH(int i)
{
Pixel p;
p.blue = (i & 0x1f) << 3;
i = i >> 5;
p.green = (i & 0x1f) << 3;
i = i >> 5;
p.red = (i & 0x1f) << 3;
return p;
}
/*
* count how often each color occurs
*/
static long
do_count(Pixel *pix, size_t numpixels)
{
int index;
long numcolors;
numcolors = 0;
while (numpixels) {
index = HASH(*pix);
if (color_count[index] == 0) {
color_count[index]++;
numcolors++;
}
pix++;
numpixels--;
}
return numcolors;
}
void
read_file(char *infile)
{
FILE *fhandle;
int c;
Pixel *row_pixels;
long i;
block_count = dup_pixel_count = 0;
fhandle = fopen(infile, "rb");
if (!fhandle) {
perror(infile);
exit(1);
}
bytes_in_name = fgetc(fhandle);
cmap_type = fgetc(fhandle);
sub_type = fgetc(fhandle);
c = fgetc(fhandle); /* skip bytes 3 and 4 */
c = fgetc(fhandle);
if (c < 0) err_eof();
cmap_len = fgetc(fhandle) + ((unsigned)fgetc(fhandle) << 8);
c = fgetc(fhandle); /* skip bytes 7 through 11 */
c = fgetc(fhandle);
c = fgetc(fhandle);
c = fgetc(fhandle);
c = fgetc(fhandle);
if (c < 0) err_eof();
image_w = fgetc(fhandle) + ((unsigned)fgetc(fhandle) << 8);
image_h = fgetc(fhandle) + ((unsigned)fgetc(fhandle) << 8);
bits_per_pixel = fgetc(fhandle);
if (bits_per_pixel < 0) err_eof();
tga_flags = fgetc(fhandle);
printf("%s is a %d by %d Targa file with %d bits per pixel\n", infile, image_w, image_h, bits_per_pixel);
if (!count_colors)
return;
/* if we are to count colors, read the file in */
if (cmap_type != 0) {
fprintf(stderr, "ERROR: Targa files with color maps not supported\n");
exit(1);
}
if (bits_per_pixel != 24) {
fprintf(stderr, "ERROR: Only 24 bit Targa files are supported\n");
exit(1);
}
if ((tga_flags & 0x20) == 0) { /* this picture is bottom-up */
vflip_flag = !vflip_flag;
}
if (tga_flags & 0xc0) {
fprintf(stderr, "ERROR: Interlaced Targa files are not supported\n");
exit(1);
}
/* figure out how to read source pixels */
if (sub_type > 8) {
/* an RLE-coded file */
read_pixel = read_rle_pixel;
sub_type -= 8;
} else {
read_pixel = read_norm_pixel;
}
if (sub_type == 1) {
fprintf(stderr, "ERROR: Colormapped Targa files not supported\n");
exit(1);
} else if (sub_type == 2) {
/* everything is OK */ ;
} else {
fprintf(stderr, "ERROR: Invalid or unsupported Targa file\n");
exit(1);
}
/* skip the image name */
for (i = 0; i < bytes_in_name; i++) {
c = fgetc(fhandle);
if (c < 0) err_eof();
}
srcfile = my_malloc(sizeof(Pixel) * (size_t)image_w*(size_t)image_h);
if (!srcfile) {
fprintf(stderr, "ERROR: insufficient memory for image\n");
exit(1);
}
if (vflip_flag) {
row_pixels = srcfile + image_h*(long)image_w;
for (i = 0; i < image_h; i++) {
row_pixels -= image_w;
read_row(fhandle, row_pixels);
}
} else {
row_pixels = srcfile;
for (i = 0; i < image_h; i++) {
read_row(fhandle, row_pixels);
row_pixels += image_w;
}
}
fclose(fhandle);
/* now that the file has been read, count the number of different colors in it */
printf("It has %ld distinct (15 bit) colors\n", do_count(srcfile, (size_t)image_w*(size_t)image_h));
}