Debian Codegraph @

code/renderercommon/tr_image_pvr.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "tr_common.h"
24
25 typedef struct gbix {
26 unsigned int magic;
27 unsigned int len;
28 } gbix_t;
29
30 typedef struct pvr {
31 unsigned int magic;
32 unsigned int len_file;
33 unsigned int type;
34 unsigned short width;
35 unsigned short height;
36 } pvr_t;
37
38 typedef unsigned int (*pvr_pixel_func_t)(unsigned short color);
39 typedef byte *(*pvr_image_func_t)(pvr_t *pvr, int offset, qboolean detwiddle, pvr_pixel_func_t pixel_func);
40
41 enum {
42 PVR_PIXEL_TYPE_ARGB1555 = 0,
43 PVR_PIXEL_TYPE_RGB565 = 1,
44 PVR_PIXEL_TYPE_ARGB4444 = 2
45 };
46
47 enum {
48 PVR_IMAGE_TYPE_TWIDDLED = 1,
49 PVR_IMAGE_TYPE_TWIDDLED_MM = 2,
50 PVR_IMAGE_TYPE_VQ = 3,
51 PVR_IMAGE_TYPE_VQ_MM = 4,
52 PVR_IMAGE_TYPE_RECTANGULAR = 9,
53 PVR_IMAGE_TYPE_RECTANGULAR_MM = 10
54 };
55
56 static int log2approx(int x)
57 {
58 switch (x)
59 {
60 case 8: return 3;
61 case 16: return 4;
62 case 32: return 5;
63 case 64: return 6;
64 case 128: return 7;
65 case 256: return 8;
66 case 512: return 9;
67 case 1024: return 10;
68 default: return -1;
69 }
70 }
71
72 static int pvr_detwiddle(int x, int y, int w, int h)
73 {
74 int wmax, hmax;
75 int i, idx = 0;
76
77 wmax = log2approx(w);
78 hmax = log2approx(h);
79
80 if (wmax < 0 || hmax < 0)
81 return -1;
82
83 for (i = 0; i < 10; i++)
84 {
85 if (i < wmax && i < hmax)
86 {
87 idx |= ((y >> i) & 1) << (i * 2 + 0);
88 idx |= ((x >> i) & 1) << (i * 2 + 1);
89 }
90 else if (i < wmax)
91 {
92 idx |= ((x >> i) & 1) << (i + hmax);
93 }
94 else if (i < hmax)
95 {
96 idx |= ((y >> i) & 1) << (i + wmax);
97 }
98 else
99 {
100 break;
101 }
102 }
103
104 return idx;
105 }
106
107 static unsigned int argb1555_to_rgba8888(unsigned short color)
108 {
109 unsigned char r, g, b, a;
110 r = ((color >> 10) & 31) << 4;
111 g = ((color >> 5) & 31) << 4;
112 b = ((color >> 0) & 31) << 4;
113 a = ((color >> 15) & 1) * 255;
114 return (a << 24) | (b << 16) | (g << 8) | r;
115 }
116
117 static unsigned int rgb565_to_rgba8888(unsigned short color)
118 {
119 unsigned char r, g, b, a;
120 r = ((color >> 11) & 31) << 3;
121 g = ((color >> 5) & 63) << 2;
122 b = ((color >> 0) & 31) << 3;
123 a = 255;
124 return (a << 24) | (b << 16) | (g << 8) | r;
125 }
126
127 static unsigned int argb4444_to_rgba8888(unsigned short color)
128 {
129 unsigned char r, g, b, a;
130 r = ((color >> 8) & 15) << 4;
131 g = ((color >> 4) & 15) << 4;
132 b = ((color >> 0) & 15) << 4;
133 a = ((color >> 12) & 15) | 0xF;
134 return (a << 24) | (b << 16) | (g << 8) | r;
135 }
136
137 static int mm_offset(int w)
138 {
139 switch (w)
140 {
141 case 1: return 0x00006;
142 case 2: return 0x00008;
143 case 4: return 0x00010;
144 case 8: return 0x00030;
145 case 16: return 0x000B0;
146 case 32: return 0x002B0;
147 case 64: return 0x00AB0;
148 case 128: return 0x02AB0;
149 case 256: return 0x0AAB0;
150 case 512: return 0x2AAB0;
151 case 1024: return 0xAAAB0;
152 default: return -1;
153 }
154 }
155
156 static int mm_offset_vq(int w)
157 {
158 switch (w)
159 {
160 case 1: return 0x00000;
161 case 2: return 0x00001;
162 case 4: return 0x00002;
163 case 8: return 0x00006;
164 case 16: return 0x00016;
165 case 32: return 0x00056;
166 case 64: return 0x00156;
167 case 128: return 0x00556;
168 case 256: return 0x01556;
169 case 512: return 0x05556;
170 case 1024: return 0x15556;
171 default: return -1;
172 }
173 }
174
175 static byte *decode_mm(pvr_t *pvr, pvr_image_func_t image_func, pvr_pixel_func_t pixel_func, qboolean vq, qboolean detwiddle)
176 {
177 int offset = vq ? mm_offset_vq(pvr->width) : mm_offset(pvr->width);
178 return image_func(pvr, offset, detwiddle, pixel_func);
179 }
180
181 static byte *decode(pvr_t *pvr, int offset, qboolean detwiddle, pvr_pixel_func_t pixel_func)
182 {
183 int x, y;
184 unsigned int *rgba32;
185 byte *ret;
186
187 ret = (byte *)ri.Malloc(pvr->width * pvr->height * sizeof(unsigned int));
188 rgba32 = (unsigned int *)ret;
189
190 for (y = 0; y < pvr->height; y++)
191 {
192 for (x = 0; x < pvr->width; x++)
193 {
194 unsigned short color;
195 int ofs;
196 if (detwiddle)
197 ofs = offset + pvr_detwiddle(x, y, pvr->width, pvr->height) * 2;
198 else
199 ofs = offset + (y * pvr->width + x) * 2;
200
201 if (ofs < 0)
202 {
203 ri.Free(ret);
204 return NULL;
205 }
206
207 color = *(unsigned short *)(((byte *)(pvr + 1)) + ofs);
208
209 rgba32[y * pvr->width + x] = pixel_func(color);
210 }
211 }
212
213 return ret;
214 }
215
216 static byte *decode_vq(pvr_t *pvr, int offset, qboolean detwiddle, pvr_pixel_func_t pixel_func)
217 {
218 int x, y;
219 unsigned int *rgba32;
220 unsigned short *codebook;
221 unsigned char *indices;
222 byte *ret;
223
224 ret = (byte *)ri.Malloc(pvr->width * pvr->height * (pixel_func ? sizeof(unsigned int) : sizeof(unsigned short)));
225 rgba32 = (unsigned int *)ret;
226
227 codebook = (unsigned short *)(pvr + 1);
228 indices = ((unsigned char *)(codebook + 1024)) + offset;
229
230 for (y = 0; y < pvr->height / 2; y++)
231 {
232 for (x = 0; x < pvr->width / 2; x++)
233 {
234 unsigned short *colors;
235 int a, b, c, d;
236 int idx;
237 if (detwiddle)
238 idx = pvr_detwiddle(x, y, pvr->width, pvr->height);
239 else
240 idx = y * (pvr->width / 2) + x;
241
242 if (idx < 0)
243 {
244 ri.Free(ret);
245 return NULL;
246 }
247
248 colors = &codebook[indices[idx] * 4];
249
250 a = ((y * 2) + 0) * pvr->width + ((x * 2) + 0);
251 b = ((y * 2) + 1) * pvr->width + ((x * 2) + 0);
252 c = ((y * 2) + 0) * pvr->width + ((x * 2) + 1);
253 d = ((y * 2) + 1) * pvr->width + ((x * 2) + 1);
254
255 rgba32[a] = pixel_func(colors[0]);
256 rgba32[b] = pixel_func(colors[1]);
257 rgba32[c] = pixel_func(colors[2]);
258 rgba32[d] = pixel_func(colors[3]);
259 }
260 }
261
262 return ret;
263 }
264
265 void R_LoadPVR(const char *name, byte **pic, int *width, int *height)
266 {
267 unsigned int length;
268 void *buffer;
269 byte *ptr;
270 pvr_t *pvr;
271 int pixel_type, image_type;
272 pvr_pixel_func_t pixel_func;
273 byte *ret = NULL;
274
275 *pic = NULL;
276 if (width)
277 *width = 0;
278 if (height)
279 *height = 0;
280
281 // load the file
282 length = ri.FS_ReadFile((char *)name, &buffer);
283 if (!buffer || length < 0)
284 return;
285
286 ptr = (byte *)buffer;
287
288 // skip global index
289 if (memcmp(ptr, "GBIX", 4) == 0)
290 {
291 gbix_t *gbix = (gbix_t *)ptr;
292 ptr += sizeof(gbix_t) + LittleLong(gbix->len);
293 }
294
295 // check magic identifier
296 if (memcmp(ptr, "PVRT", 4) != 0)
297 ri.Error(ERR_DROP, "LoadPVR: magic identifier does not match expected (%s)", name);
298
299 // fix up header
300 pvr = (pvr_t *)ptr;
301 pvr->len_file = LittleLong(pvr->len_file);
302 pvr->type = LittleLong(pvr->type);
303 pvr->width = LittleShort(pvr->width);
304 pvr->height = LittleShort(pvr->height);
305
306 // break out type values
307 pixel_type = pvr->type & 0xFF;
308 image_type = (pvr->type & 0xFF00) >> 8;
309
310 // get pixel function
311 switch (pixel_type)
312 {
313 case PVR_PIXEL_TYPE_ARGB1555:
314 {
315 pixel_func = argb1555_to_rgba8888;
316 break;
317 }
318 case PVR_PIXEL_TYPE_RGB565:
319 {
320 pixel_func = rgb565_to_rgba8888;
321 break;
322 }
323 case PVR_PIXEL_TYPE_ARGB4444:
324 {
325 pixel_func = argb4444_to_rgba8888;
326 break;
327 }
328 default:
329 {
330 ri.Error(ERR_DROP, "LoadPVR: unsupported pixel type 0x%02x (%s)", pixel_type, name);
331 break;
332 }
333 }
334
335 // decompress image
336 switch (image_type)
337 {
338 case PVR_IMAGE_TYPE_TWIDDLED:
339 {
340 ret = decode(pvr, 0, qtrue, pixel_func);
341 break;
342 }
343 case PVR_IMAGE_TYPE_TWIDDLED_MM:
344 {
345 ret = decode_mm(pvr, decode, pixel_func, qfalse, qtrue);
346 break;
347 }
348 case PVR_IMAGE_TYPE_VQ:
349 {
350 ret = decode_vq(pvr, 0, qtrue, pixel_func);
351 break;
352 }
353 case PVR_IMAGE_TYPE_VQ_MM:
354 {
355 ret = decode_mm(pvr, decode_vq, pixel_func, qtrue, qtrue);
356 break;
357 }
358 case PVR_IMAGE_TYPE_RECTANGULAR:
359 {
360 ret = decode(pvr, 0, qfalse, pixel_func);
361 break;
362 }
363 case PVR_IMAGE_TYPE_RECTANGULAR_MM:
364 {
365 ret = decode_mm(pvr, decode, pixel_func, qfalse, qfalse);
366 break;
367 }
368 default:
369 {
370 ri.Error(ERR_DROP, "LoadPVR: unsupported image type 0x%02x (%s)", image_type, name);
371 break;
372 }
373 }
374
375 // clean up
376 ri.FS_FreeFile(buffer);
377
378 // something failed
379 if (!ret)
380 return;
381
382 // return stuff
383 *pic = ret;
384 if (width)
385 *width = pvr->width;
386 if (height)
387 *height = pvr->height;
388 }