-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.c
475 lines (375 loc) · 11.3 KB
/
image.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* -------------------------------------------------------------------------------
Copyright (C) 1999-2006 Id Software, Inc. and contributors.
For a list of contributors, see the accompanying CONTRIBUTORS file.
This file is part of GtkRadiant.
GtkRadiant 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 2 of the License, or
(at your option) any later version.
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
----------------------------------------------------------------------------------
This code has been altered significantly from its original form, to support
several games based on the Quake III Arena engine, in the form of "Q3Map2."
------------------------------------------------------------------------------- */
/* marker */
#define IMAGE_C
/* dependencies */
#include "kmap2.h"
/* -------------------------------------------------------------------------------
this file contains image pool management with reference counting. note: it isn't
reentrant, so only call it from init/shutdown code or wrap calls in a mutex
------------------------------------------------------------------------------- */
/*
LoadDDSBuffer()
loads a dxtc (1, 3, 5) dds buffer into a valid rgba image
*/
#if 0
static void LoadDDSBuffer(byte * buffer, int size, byte ** pixels, int *width, int *height)
{
int w, h;
ddsPF_t pf;
/* dummy check */
if(buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL)
return;
/* null out */
*pixels = 0;
*width = 0;
*height = 0;
/* get dds info */
if(DDSGetInfo((ddsBuffer_t *) buffer, &w, &h, &pf))
{
Sys_Printf("WARNING: Invalid DDS texture\n");
return;
}
/* only certain types of dds textures are supported */
if(pf != DDS_PF_ARGB8888 && pf != DDS_PF_DXT1 && pf != DDS_PF_DXT3 && pf != DDS_PF_DXT5)
{
Sys_Printf("WARNING: Only DDS texture formats ARGB8888, DXT1, DXT3, and DXT5 are supported (%d)\n", pf);
return;
}
/* create image pixel buffer */
*width = w;
*height = h;
*pixels = safe_malloc(w * h * 4);
/* decompress the dds texture */
DDSDecompress((ddsBuffer_t *) buffer, *pixels);
}
#endif
/*
PNGReadData()
callback function for libpng to read from a memory buffer
note: this function is a total hack, as it reads/writes the png struct directly!
*/
#if 0
typedef struct pngBuffer_s
{
byte *buffer;
int size, offset;
}
pngBuffer_t;
void PNGReadData(png_struct * png, png_byte * buffer, png_size_t size)
{
pngBuffer_t *pb = (pngBuffer_t *) png_get_io_ptr(png);
if((pb->offset + size) > pb->size)
size = (pb->size - pb->offset);
memcpy(buffer, &pb->buffer[pb->offset], size);
pb->offset += size;
//% Sys_Printf( "Copying %d bytes from 0x%08X to 0x%08X (offset: %d of %d)\n", size, &pb->buffer[ pb->offset ], buffer, pb->offset, pb->size );
}
/*
LoadPNGBuffer()
loads a png file buffer into a valid rgba image
*/
static void LoadPNGBuffer(byte * buffer, int size, byte ** pixels, int *width, int *height)
{
png_struct *png;
png_info *info, *end;
pngBuffer_t pb;
int i, bitDepth, colorType, channels;
png_uint_32 w, h;
byte **rowPointers;
/* dummy check */
if(buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL)
return;
/* null out */
*pixels = 0;
*width = 0;
*height = 0;
/* determine if this is a png file */
if(png_sig_cmp(buffer, 0, 8) != 0)
{
Sys_Printf("WARNING: Invalid PNG file\n");
return;
}
/* create png structs */
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(png == NULL)
{
Sys_Printf("WARNING: Unable to create PNG read struct\n");
return;
}
info = png_create_info_struct(png);
if(info == NULL)
{
Sys_Printf("WARNING: Unable to create PNG info struct\n");
png_destroy_read_struct(&png, NULL, NULL);
return;
}
end = png_create_info_struct(png);
if(end == NULL)
{
Sys_Printf("WARNING: Unable to create PNG end info struct\n");
png_destroy_read_struct(&png, &info, NULL);
return;
}
/* set read callback */
pb.buffer = buffer;
pb.size = size;
pb.offset = 0;
png_set_read_fn(png, &pb, PNGReadData);
png->io_ptr = &pb; /* hack! */
/* set error longjmp */
if(setjmp(png->jmpbuf))
{
Sys_Printf("WARNING: An error occurred reading PNG image\n");
png_destroy_read_struct(&png, &info, &end);
return;
}
/* fixme: add proper i/o stuff here */
/* read png info */
png_read_info(png, info);
/* read image header chunk */
png_get_IHDR(png, info, &w, &h, &bitDepth, &colorType, NULL, NULL, NULL);
/* read number of channels */
channels = png_get_channels(png, info);
/* the following will probably bork on certain types of png images, but hey... */
/* force indexed/gray/trans chunk to rgb */
if((colorType == PNG_COLOR_TYPE_PALETTE && bitDepth <= 8) ||
(colorType == PNG_COLOR_TYPE_GRAY && bitDepth <= 8) || png_get_valid(png, info, PNG_INFO_tRNS))
png_set_expand(png);
/* strip 16bpc -> 8bpc */
if(bitDepth == 16)
png_set_strip_16(png);
/* pad rgb to rgba */
if(bitDepth == 8 && colorType == PNG_COLOR_TYPE_RGB)
png_set_filler(png, 255, PNG_FILLER_AFTER);
/* create image pixel buffer */
*width = w;
*height = h;
*pixels = safe_malloc(w * h * 4);
/* create row pointers */
rowPointers = safe_malloc(h * sizeof(byte *));
for(i = 0; i < h; i++)
rowPointers[i] = *pixels + (i * w * 4);
/* read the png */
png_read_image(png, rowPointers);
/* clean up */
free(rowPointers);
png_destroy_read_struct(&png, &info, &end);
}
#endif
/*
ImageInit()
implicitly called by every function to set up image list
*/
static void ImageInit(void)
{
int i;
if(numImages <= 0)
{
/* clear images (fixme: this could theoretically leak) */
memset(images, 0, sizeof(images));
/* generate *bogus image */
images[0].name = safe_malloc(strlen(DEFAULT_IMAGE) + 1);
strcpy(images[0].name, DEFAULT_IMAGE);
images[0].filename = safe_malloc(strlen(DEFAULT_IMAGE) + 1);
strcpy(images[0].filename, DEFAULT_IMAGE);
images[0].width = 64;
images[0].height = 64;
images[0].refCount = 1;
images[0].pixels = safe_malloc(64 * 64 * 4);
for(i = 0; i < (64 * 64 * 4); i++)
images[0].pixels[i] = 255;
}
}
/*
ImageFree()
frees an rgba image
*/
void ImageFree(image_t * image)
{
/* dummy check */
if(image == NULL)
return;
/* decrement refcount */
image->refCount--;
/* free? */
if(image->refCount <= 0)
{
if(image->name != NULL)
free(image->name);
image->name = NULL;
if(image->filename != NULL)
free(image->filename);
image->filename = NULL;
free(image->pixels);
image->width = 0;
image->height = 0;
numImages--;
}
}
/*
ImageFind()
finds an existing rgba image and returns a pointer to the image_t struct or NULL if not found
*/
image_t *ImageFind(const char *filename)
{
int i;
char name[1024];
/* init */
ImageInit();
/* dummy check */
if(filename == NULL || filename[0] == '\0')
return NULL;
/* strip file extension off name */
strcpy(name, filename);
StripExtension(name);
/* search list */
for(i = 0; i < MAX_IMAGES; i++)
{
if(images[i].name != NULL && !strcmp(name, images[i].name))
return &images[i];
}
/* no matching image found */
return NULL;
}
/*
ImageLoad()
loads an rgba image and returns a pointer to the image_t struct or NULL if not found
*/
image_t *ImageLoad(const char *filename)
{
int i;
image_t *image;
char name[1024];
int size;
byte *buffer = NULL;
/* init */
ImageInit();
/* dummy check */
if(filename == NULL || filename[0] == '\0')
return NULL;
/* strip file extension off name */
strcpy(name, filename);
StripExtension(name);
/* try to find existing image */
image = ImageFind(name);
if(image != NULL)
{
image->refCount++;
return image;
}
/* none found, so find first non-null image */
image = NULL;
for(i = 0; i < MAX_IMAGES; i++)
{
if(images[i].name == NULL)
{
image = &images[i];
break;
}
}
/* too many images? */
if(image == NULL)
Error("MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES);
/* set it up */
image->name = safe_malloc(strlen(name) + 1);
strcpy(image->name, name);
#if 1
/* attempt to load tga */
StripExtension(name);
strcat(name, ".tga");
size = vfsLoadFile((const char *)name, (void **)&buffer, 0);
if(size > 0)
LoadTGABuffer(buffer, &image->pixels, &image->width, &image->height);
else
{
#if 1
/* attempt to load png */
StripExtension(name);
strcat(name, ".png");
size = vfsLoadFile((const char *)name, (void **)&buffer, 0);
if(size > 0)
LoadPNGBuffer(buffer, &image->pixels, &image->width, &image->height);
else
{
#if 1
/* attempt to load jpg */
StripExtension(name);
strcat(name, ".jpg");
size = vfsLoadFile((const char *)name, (void **)&buffer, 0);
if(size > 0)
{
LoadJPGBuffer((const char *)name, buffer, size, &image->pixels, &image->width, &image->height);
if(image->pixels == NULL)
Sys_Printf("WARNING: LoadJPGBuffer: '%s'\n", image->name);
}
else
{
#if 0
/* attempt to load dds */
StripExtension(name);
strcat(name, ".dds");
size = vfsLoadFile((const char *)name, (void **)&buffer, 0);
if(size > 0)
{
LoadDDSBuffer(buffer, size, &image->pixels, &image->width, &image->height);
/* debug code */
#if 0
{
ddsPF_t pf;
DDSGetInfo((ddsBuffer_t *) buffer, NULL, NULL, &pf);
Sys_Printf("pf = %d\n", pf);
if(image->width > 0)
{
StripExtension(name);
strcat(name, "_converted.tga");
WriteTGA("C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width,
image->height);
}
}
#endif // dds debug
}
#endif // ignore dds
}
#endif // ignore jpg dds
}
#endif // ignore png jpg dds
}
#endif // ignore all images
/* free file buffer */
free(buffer);
/* make sure everything's kosher */
if(size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL)
{
//% Sys_Printf( "size = %d width = %d height = %d pixels = 0x%08x (%s)\n",
//% size, image->width, image->height, image->pixels, name );
free(image->name);
image->name = NULL;
return NULL;
}
/* set filename */
image->filename = safe_malloc(strlen(name) + 1);
strcpy(image->filename, name);
/* set count */
image->refCount = 1;
numImages++;
/* return the image */
return image;
}