|
| 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 pvr { |
| 26 | + unsigned char magic[4]; |
| 27 | + unsigned int len_file; |
| 28 | + unsigned int type; |
| 29 | + unsigned short width; |
| 30 | + unsigned short height; |
| 31 | + unsigned short codebook[1024]; |
| 32 | + unsigned char indices[1]; |
| 33 | +} pvr_t; |
| 34 | + |
| 35 | +static int log2approx(int x) |
| 36 | +{ |
| 37 | + switch (x) |
| 38 | + { |
| 39 | + case 8: return 3; |
| 40 | + case 16: return 4; |
| 41 | + case 32: return 5; |
| 42 | + case 64: return 6; |
| 43 | + case 128: return 7; |
| 44 | + case 256: return 8; |
| 45 | + case 512: return 9; |
| 46 | + case 1024: return 10; |
| 47 | + default: return -1; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +static int from_xy(int x, int y, int w, int h) |
| 52 | +{ |
| 53 | + int wmax, hmax; |
| 54 | + int i, idx = 0; |
| 55 | + |
| 56 | + wmax = log2approx(w); |
| 57 | + hmax = log2approx(h); |
| 58 | + |
| 59 | + if (wmax < 0 || hmax < 0) |
| 60 | + return -1; |
| 61 | + |
| 62 | + for (i = 0; i < 10; i++) |
| 63 | + { |
| 64 | + if (i < wmax && i < hmax) |
| 65 | + { |
| 66 | + idx |= ((y >> i) & 1) << (i * 2 + 0); |
| 67 | + idx |= ((x >> i) & 1) << (i * 2 + 1); |
| 68 | + } |
| 69 | + else if (i < wmax) |
| 70 | + { |
| 71 | + idx |= ((x >> i) & 1) << (i + hmax); |
| 72 | + } |
| 73 | + else if (i < hmax) |
| 74 | + { |
| 75 | + idx |= ((y >> i) & 1) << (i + wmax); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return idx; |
| 84 | +} |
| 85 | + |
| 86 | +static unsigned int rgb565_to_rgba24(unsigned short color) |
| 87 | +{ |
| 88 | + unsigned char r, g, b, a; |
| 89 | + r = ((color >> 11) & 31) << 3; |
| 90 | + g = ((color >> 5) & 63) << 2; |
| 91 | + b = ((color >> 0) & 31) << 3; |
| 92 | + a = 255; |
| 93 | + return (a << 24) | (b << 16) | (g << 8) | r; |
| 94 | +} |
| 95 | + |
| 96 | +void R_LoadPVR(const char *name, byte **pic, int *width, int *height) |
| 97 | +{ |
| 98 | + unsigned int length; |
| 99 | + void *buffer; |
| 100 | + pvr_t *pvr; |
| 101 | + unsigned int *rgba; |
| 102 | + int x, y; |
| 103 | + |
| 104 | + *pic = NULL; |
| 105 | + if (width) |
| 106 | + *width = 0; |
| 107 | + if (height) |
| 108 | + *height = 0; |
| 109 | + |
| 110 | + // load the file |
| 111 | + length = ri.FS_ReadFile((char *)name, &buffer); |
| 112 | + if (!buffer || length < 0) |
| 113 | + return; |
| 114 | + |
| 115 | + // check magic identifier |
| 116 | + pvr = (pvr_t *)buffer; |
| 117 | + if (memcmp(pvr->magic, "PVRT", sizeof(pvr->magic)) != 0) |
| 118 | + ri.Error(ERR_DROP, "LoadPVR: magic identifier does not match expected (%s)", name); |
| 119 | + |
| 120 | + // fix up header |
| 121 | + pvr->len_file = LittleLong(pvr->len_file); |
| 122 | + pvr->type = LittleLong(pvr->type); |
| 123 | + pvr->width = LittleShort(pvr->width); |
| 124 | + pvr->height = LittleShort(pvr->height); |
| 125 | + |
| 126 | + // allocate rgba buffer |
| 127 | + rgba = (unsigned int *)ri.Malloc(pvr->width * pvr->height * 4); |
| 128 | + |
| 129 | + // decompress image |
| 130 | + for (y = 0; y < pvr->height / 2; y++) |
| 131 | + { |
| 132 | + for (x = 0; x < pvr->width / 2; x++) |
| 133 | + { |
| 134 | + unsigned short *colors; |
| 135 | + int a, b, c, d; |
| 136 | + int idx = from_xy(x, y, pvr->width, pvr->height); |
| 137 | + |
| 138 | + if (idx < 0) |
| 139 | + ri.Error(ERR_DROP, "LoadPVR: invalid data passed to decompressor (%s)", name); |
| 140 | + |
| 141 | + colors = &pvr->codebook[pvr->indices[idx] * 4]; |
| 142 | + |
| 143 | + a = ((y * 2) + 0) * pvr->width + ((x * 2) + 0); |
| 144 | + b = ((y * 2) + 1) * pvr->width + ((x * 2) + 0); |
| 145 | + c = ((y * 2) + 0) * pvr->width + ((x * 2) + 1); |
| 146 | + d = ((y * 2) + 1) * pvr->width + ((x * 2) + 1); |
| 147 | + |
| 148 | + rgba[a] = rgb565_to_rgba24(colors[0]); |
| 149 | + rgba[b] = rgb565_to_rgba24(colors[1]); |
| 150 | + rgba[c] = rgb565_to_rgba24(colors[2]); |
| 151 | + rgba[d] = rgb565_to_rgba24(colors[3]); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // clean up |
| 156 | + ri.FS_FreeFile(buffer); |
| 157 | + |
| 158 | + // return stuff |
| 159 | + *pic = (byte *)rgba; |
| 160 | + if (width) |
| 161 | + *width = pvr->width; |
| 162 | + if (height) |
| 163 | + *height = pvr->height; |
| 164 | +} |
0 commit comments