Quake II RTX doxygen  1.0 dev
image.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 1997-2001 Id Software, Inc.
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 
19 #include "sw.h"
20 
21 static byte gammatable[256];
22 
23 /*
24 ================
25 IMG_Unload
26 ================
27 */
28 void IMG_Unload(image_t *image)
29 {
30  Z_Free(image->pixels[0]);
31  image->pixels[0] = NULL;
32 }
33 
34 static void R_LightScaleTexture(byte *in, int inwidth, int inheight)
35 {
36  int i, c;
37  byte *p;
38 
39  p = in;
40  c = inwidth * inheight;
41 
42  for (i = 0; i < c; i++, p += TEX_BYTES) {
43  p[0] = gammatable[p[0]];
44  p[1] = gammatable[p[1]];
45  p[2] = gammatable[p[2]];
46  }
47 }
48 
49 /*
50 ================
51 IMG_Load
52 ================
53 */
54 void IMG_Load(image_t *image, byte *pic)
55 {
56  int i, c, b;
57  int width, height;
58 
59  width = image->upload_width;
60  height = image->upload_height;
61 
62  if (image->flags & IF_TURBULENT) {
63  image->width = TURB_SIZE;
64  image->height = TURB_SIZE;
65  }
66 
67  b = image->width * image->height;
68  c = width * height;
69 
70  if (image->type == IT_WALL) {
71  image->pixels[0] = R_Malloc(MIPSIZE(b) * TEX_BYTES);
72  image->pixels[1] = image->pixels[0] + b * TEX_BYTES;
73  image->pixels[2] = image->pixels[1] + b * TEX_BYTES / 4;
74  image->pixels[3] = image->pixels[2] + b * TEX_BYTES / 16;
75 
76  if (!(r_config.flags & QVF_GAMMARAMP))
78 
79  if (width == image->width && height == image->height) {
80  memcpy(image->pixels[0], pic, width * height * TEX_BYTES);
81  } else {
82  IMG_ResampleTexture(pic, width, height, image->pixels[0], image->width, image->height);
83  image->upload_width = image->width;
84  image->upload_height = image->height;
85  }
86 
87  IMG_MipMap(image->pixels[1], image->pixels[0], image->width >> 0, image->height >> 0);
88  IMG_MipMap(image->pixels[2], image->pixels[1], image->width >> 1, image->height >> 1);
89  IMG_MipMap(image->pixels[3], image->pixels[2], image->width >> 2, image->height >> 2);
90 
91  Z_Free(pic);
92  } else {
93  image->pixels[0] = pic;
94 
95  if (!(image->flags & IF_OPAQUE)) {
96  for (i = 0; i < c; i++) {
97  b = pic[i * TEX_BYTES + 3];
98  if (b != 255) {
99  image->flags |= IF_TRANSPARENT;
100  }
101  }
102  }
103  }
104 
105  if (image->type == IT_SKIN && !(r_config.flags & QVF_GAMMARAMP))
106  R_LightScaleTexture(image->pixels[0], width, height);
107 }
108 
110 {
111  int i, inf;
112  float g = vid_gamma->value;
113 
114  if (g == 1.0) {
115  for (i = 0; i < 256; i++)
116  gammatable[i] = i;
117  return;
118  }
119 
120  for (i = 0; i < 256; i++) {
121  inf = 255 * pow((i + 0.5) / 255.5, g) + 0.5;
122  gammatable[i] = clamp(inf, 0, 255);
123  }
124 }
125 
126 #define NTX 256
127 
128 static void R_CreateNotexture(void)
129 {
130  static byte buffer[MIPSIZE(NTX * NTX) * TEX_BYTES];
131  int x, y, m;
132  uint32_t *p;
133  image_t *ntx;
134 
135 // create a simple checkerboard texture for the default
136  ntx = R_NOTEXTURE;
137  ntx->type = IT_WALL;
138  ntx->flags = 0;
139  ntx->width = ntx->height = NTX;
140  ntx->upload_width = ntx->upload_height = NTX;
141  ntx->pixels[0] = buffer;
142  ntx->pixels[1] = ntx->pixels[0] + NTX * NTX * TEX_BYTES;
143  ntx->pixels[2] = ntx->pixels[1] + NTX * NTX * TEX_BYTES / 4;
144  ntx->pixels[3] = ntx->pixels[2] + NTX * NTX * TEX_BYTES / 16;
145 
146  for (m = 0; m < 4; m++) {
147  p = (uint32_t *)ntx->pixels[m];
148  for (y = 0; y < (NTX >> m); y++) {
149  for (x = 0; x < (NTX >> m); x++) {
150  if ((x ^ y) & (1 << (3 - m)))
151  *p++ = U32_BLACK;
152  else
153  *p++ = U32_WHITE;
154  }
155  }
156  }
157 }
158 
159 /*
160 ===============
161 R_InitImages
162 ===============
163 */
164 void R_InitImages(void)
165 {
167 
168  IMG_GetPalette();
169 
171 
173 }
174 
175 /*
176 ===============
177 R_ShutdownImages
178 ===============
179 */
181 {
182  IMG_FreeAll();
183 }
184 
R_ShutdownImages
void R_ShutdownImages(void)
Definition: image.c:180
IMG_GetPalette
void IMG_GetPalette(void)
Definition: images.c:1376
IMG_FreeAll
void IMG_FreeAll(void)
Definition: images.c:1343
height
static int height
Definition: physical_sky.c:39
image_t
struct image_s image_t
Definition: material.h:27
R_InitImages
void R_InitImages(void)
Definition: image.c:164
IMG_Load
void IMG_Load(image_t *image, byte *pic)
Definition: image.c:54
TEX_BYTES
#define TEX_BYTES
Definition: sw.h:50
IMG_MipMap
void IMG_MipMap(byte *out, byte *in, int width, int height)
Definition: images.c:623
IMG_ResampleTexture
void IMG_ResampleTexture(const byte *in, int inwidth, int inheight, byte *out, int outwidth, int outheight)
Definition: images.c:577
IMG_Unload
void IMG_Unload(image_t *image)
Definition: image.c:28
width
static int width
Definition: physical_sky.c:38
gammatable
static byte gammatable[256]
Definition: image.c:21
R_BuildGammaTable
void R_BuildGammaTable(void)
Definition: image.c:109
sw.h
Z_Free
void Z_Free(void *ptr)
Definition: zone.c:147
m
static struct mdfour * m
Definition: mdfour.c:32
NTX
#define NTX
Definition: image.c:126
TURB_SIZE
#define TURB_SIZE
Definition: sw.h:71
R_CreateNotexture
static void R_CreateNotexture(void)
Definition: image.c:128
c
statCounters_t c
Definition: main.c:30
registration_sequence
int registration_sequence
Definition: main.c:34
vid_gamma
cvar_t * vid_gamma
Definition: main.c:104
R_LightScaleTexture
static void R_LightScaleTexture(byte *in, int inwidth, int inheight)
Definition: image.c:34
r_config
refcfg_t r_config
Definition: refresh.c:401