forked from iamgreaser/iceball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img.c
160 lines (137 loc) · 3.49 KB
/
img.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
/*
This file is part of Iceball.
Iceball is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Iceball is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Iceball. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
uint32_t img_convert_color_to_32(uint32_t v, int bits)
{
switch(bits)
{
case 16:
return 0xFF000000*((v>>15)&1)
+ ((v&0x001F)<<3)
+ ((v&0x03E0)<<6)
+ ((v&0x7C00)<<9);
case 24:
return 0xFF000000 + (v&0x00FFFFFF);
case 32:
return v;
default:
// not supported, just return 0xFF000000
return 0xFF000000;
}
}
void img_free(img_t *img)
{
free(img);
}
img_t *img_parse_tga(int len, const char *data)
{
// TODO: make this routine safer
// it's possible to crash this in a whole bunch of ways
const char *p = data;
const char *dend = data+len;
int x,y,i;
img_tgahead_t head;
// read header
memcpy(&head, p, sizeof(img_tgahead_t));
p += sizeof(img_tgahead_t);
// skip ID field
p += head.idlen;
// jump to palette
// load palette if necessary
uint32_t *palette = (head.cmtype == 1 ? malloc(head.cmlen*4) : NULL);
if(palette != NULL)
for(i = 0; i < head.cmlen; i++)
{
// TODO check what happens when the offset is different
uint32_t tmp_col;
int tclen = ((head.cmbpp-1)>>3)+1;
memcpy(&tmp_col, p, tclen);
p += tclen;
palette[i] = img_convert_color_to_32(tmp_col, head.cmbpp);
//printf("%6i %08X\n", i, palette[i]);
}
// allocate + stash
img_t *img = malloc(sizeof(img_t)+4*head.width*head.height);
// TODO: check if NULL
img->head = head;
img->udtype = UD_IMG;
// copy stuff
int bplen = ((head.bpp-1)>>3)+1;
int idx = (head.flags & 32 ? 0 : head.height-1)*head.width;
for(y = 0; y < head.height; y++)
{
if(head.imgtype & 8)
{
// RLE
x = 0;
uint32_t tmp_col;
while(x < head.width)
{
int rle = (int)(uint8_t)(*p++);
if(rle & 0x80)
{
rle &= 0x7F;
memcpy(&tmp_col, p, bplen);
p += bplen;
// TODO: clip at width
for(i = 0; i <= rle; i++, x++)
img->pixels[idx++] = tmp_col;
} else {
// TODO: clip at width
for(i = 0; i <= rle; i++, x++)
{
memcpy(&tmp_col, p, bplen);
p += bplen;
img->pixels[idx++] = tmp_col;
}
}
}
} else {
// raw
uint32_t tmp_col;
for(x = 0; x < head.width; x++)
{
memcpy(&tmp_col, p, bplen);
p += bplen;
img->pixels[idx++] = tmp_col;
}
}
if(!(head.flags & 32))
idx -= 2*head.width;
}
// convert pixels
if((head.imgtype&7) == 1)
{
for(i = head.width*head.height-1; i >= 0; i--)
img->pixels[i] = palette[(img->pixels[i] + head.cmoffs) % head.cmlen];
//printf("cm %i %i\n", head.cmoffs, head.cmlen);
} else {
for(i = head.width*head.height-1; i >= 0; i--)
img->pixels[i] = img_convert_color_to_32(img->pixels[i], head.bpp);
}
// free palette
free(palette);
// now return!
return img;
}
img_t *img_load_tga(const char *fname)
{
int flen;
char *buf = net_fetch_file(fname, &flen);
if(buf == NULL)
return NULL;
img_t *ret = img_parse_tga(flen, buf);
free(buf);
return ret;
}