forked from NVIDIA/Q2RTX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.c
1454 lines (1176 loc) · 35.3 KB
/
images.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1997-2001 Id Software, Inc.
Copyright (C) 2003-2008 Andrey Nazarov
Copyright (C) 2019, NVIDIA CORPORATION. All rights reserved.
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//
// images.c -- image reading and writing functions
//
#include "shared/shared.h"
#include "common/common.h"
#include "common/cvar.h"
#include "common/files.h"
#include "refresh/images.h"
#include "format/pcx.h"
#include "format/wal.h"
#include "stb_image.h"
#include "stb_image_write.h"
#define R_COLORMAP_PCX "pics/colormap.pcx"
#define IMG_LOAD(x) \
static qerror_t IMG_Load##x(byte *rawdata, size_t rawlen, \
image_t *image, byte **pic)
#define IMG_SAVE(x) \
static qerror_t IMG_Save##x(qhandle_t f, const char *filename, \
byte *pic, int width, int height, int row_stride, int param)
void stbi_write(void *context, void *data, int size)
{
FS_Write(data, size, (qhandle_t)(size_t)context);
}
extern cvar_t* vid_rtx;
/*
====================================================================
IMAGE FLOOD FILLING
====================================================================
*/
typedef struct {
short x, y;
} floodfill_t;
// must be a power of 2
#define FLOODFILL_FIFO_SIZE 0x1000
#define FLOODFILL_FIFO_MASK (FLOODFILL_FIFO_SIZE - 1)
#define FLOODFILL_STEP(off, dx, dy) \
do { \
if (pos[off] == fillcolor) { \
pos[off] = 255; \
fifo[inpt].x = x + (dx); \
fifo[inpt].y = y + (dy); \
inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; \
} else if (pos[off] != 255) { \
fdc = pos[off]; \
} \
} while(0)
/*
=================
IMG_FloodFill
Fill background pixels so mipmapping doesn't have haloes
=================
*/
static void IMG_FloodFill(byte *skin, int skinwidth, int skinheight)
{
byte fillcolor = *skin; // assume this is the pixel to fill
floodfill_t fifo[FLOODFILL_FIFO_SIZE];
int inpt = 0, outpt = 0;
int filledcolor = 0; // FIXME: fixed black
// can't fill to filled color or to transparent color
// (used as visited marker)
if (fillcolor == filledcolor || fillcolor == 255) {
return;
}
fifo[inpt].x = 0, fifo[inpt].y = 0;
inpt = (inpt + 1) & FLOODFILL_FIFO_MASK;
while (outpt != inpt) {
int x = fifo[outpt].x, y = fifo[outpt].y;
int fdc = filledcolor;
byte *pos = &skin[x + skinwidth * y];
outpt = (outpt + 1) & FLOODFILL_FIFO_MASK;
if (x > 0) FLOODFILL_STEP(-1, -1, 0);
if (x < skinwidth - 1) FLOODFILL_STEP(1, 1, 0);
if (y > 0) FLOODFILL_STEP(-skinwidth, 0, -1);
if (y < skinheight - 1) FLOODFILL_STEP(skinwidth, 0, 1);
skin[x + skinwidth * y] = fdc;
}
}
/*
=================================================================
PCX LOADING
=================================================================
*/
static qerror_t IMG_DecodePCX(byte *rawdata, size_t rawlen, byte *pixels,
byte *palette, int *width, int *height)
{
byte *raw, *end;
dpcx_t *pcx;
int x, y, w, h, scan;
int dataByte, runLength;
//
// parse the PCX file
//
if (rawlen < sizeof(dpcx_t)) {
return Q_ERR_FILE_TOO_SMALL;
}
pcx = (dpcx_t *)rawdata;
if (pcx->manufacturer != 10 || pcx->version != 5) {
return Q_ERR_UNKNOWN_FORMAT;
}
if (pcx->encoding != 1 || pcx->bits_per_pixel != 8) {
return Q_ERR_INVALID_FORMAT;
}
w = (LittleShort(pcx->xmax) - LittleShort(pcx->xmin)) + 1;
h = (LittleShort(pcx->ymax) - LittleShort(pcx->ymin)) + 1;
if (w < 1 || h < 1 || w > 640 || h > 480) {
return Q_ERR_INVALID_FORMAT;
}
if (pcx->color_planes != 1) {
return Q_ERR_INVALID_FORMAT;
}
scan = LittleShort(pcx->bytes_per_line);
if (scan < w) {
return Q_ERR_INVALID_FORMAT;
}
//
// get palette
//
if (palette) {
if (rawlen < 768) {
return Q_ERR_FILE_TOO_SMALL;
}
memcpy(palette, (byte *)pcx + rawlen - 768, 768);
}
//
// get pixels
//
if (pixels) {
raw = pcx->data;
end = (byte *)pcx + rawlen;
for (y = 0; y < h; y++, pixels += w) {
for (x = 0; x < scan;) {
if (raw >= end)
return Q_ERR_BAD_RLE_PACKET;
dataByte = *raw++;
if ((dataByte & 0xC0) == 0xC0) {
runLength = dataByte & 0x3F;
if (x + runLength > scan)
return Q_ERR_BAD_RLE_PACKET;
if (raw >= end)
return Q_ERR_BAD_RLE_PACKET;
dataByte = *raw++;
} else {
runLength = 1;
}
while (runLength--) {
if (x < w)
pixels[x] = dataByte;
x++;
}
}
}
}
if (width)
*width = w;
if (height)
*height = h;
return Q_ERR_SUCCESS;
}
/*
===============
IMG_Unpack8
===============
*/
static int IMG_Unpack8(uint32_t *out, const uint8_t *in, int width, int height)
{
int x, y, p;
qboolean has_alpha = qfalse;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
p = *in;
if (p == 255) {
has_alpha = qtrue;
// transparent, so scan around for another color
// to avoid alpha fringes
if (y > 0 && *(in - width) != 255)
p = *(in - width);
else if (y < height - 1 && *(in + width) != 255)
p = *(in + width);
else if (x > 0 && *(in - 1) != 255)
p = *(in - 1);
else if (x < width - 1 && *(in + 1) != 255)
p = *(in + 1);
else if (y > 0 && x > 0 && *(in - width - 1) != 255)
p = *(in - width - 1);
else if (y > 0 && x < width - 1 && *(in - width + 1) != 255)
p = *(in - width + 1);
else if (y < height - 1 && x > 0 && *(in + width - 1) != 255)
p = *(in + width - 1);
else if (y < height - 1 && x < width - 1 && *(in + width + 1) != 255)
p = *(in + width + 1);
else
p = 0;
// copy rgb components
*out = d_8to24table[p] & U32_RGB;
} else {
*out = d_8to24table[p];
}
in++;
out++;
}
}
if (has_alpha)
return IF_PALETTED | IF_TRANSPARENT;
return IF_PALETTED | IF_OPAQUE;
}
IMG_LOAD(PCX)
{
byte buffer[640 * 480];
int w, h;
qerror_t ret;
ret = IMG_DecodePCX(rawdata, rawlen, buffer, NULL, &w, &h);
if (ret < 0)
return ret;
if (image->type == IT_SKIN)
IMG_FloodFill(buffer, w, h);
*pic = IMG_AllocPixels(w * h * 4);
image->upload_width = image->width = w;
image->upload_height = image->height = h;
image->flags |= IMG_Unpack8((uint32_t *)*pic, buffer, w, h);
return Q_ERR_SUCCESS;
}
/*
=================================================================
WAL LOADING
=================================================================
*/
IMG_LOAD(WAL)
{
miptex_t *mt;
size_t w, h, offset, size, endpos;
if (rawlen < sizeof(miptex_t)) {
return Q_ERR_FILE_TOO_SMALL;
}
mt = (miptex_t *)rawdata;
w = LittleLong(mt->width);
h = LittleLong(mt->height);
if (w < 1 || h < 1 || w > 512 || h > 512) {
return Q_ERR_INVALID_FORMAT;
}
size = w * h;
offset = LittleLong(mt->offsets[0]);
endpos = offset + size;
if (endpos < offset || endpos > rawlen) {
return Q_ERR_BAD_EXTENT;
}
*pic = IMG_AllocPixels(size * 4);
image->upload_width = image->width = w;
image->upload_height = image->height = h;
image->flags |= IMG_Unpack8((uint32_t *)*pic, (uint8_t *)mt + offset, w, h);
return Q_ERR_SUCCESS;
}
/*
=================================================================
STB_IMAGE LOADING
=================================================================
*/
IMG_LOAD(STB)
{
int w, h, channels;
byte* data = stbi_load_from_memory(rawdata, rawlen, &w, &h, &channels, 4);
if (!data)
return Q_ERR_LIBRARY_ERROR;
*pic = data;
image->upload_width = image->width = w;
image->upload_height = image->height = h;
if (channels == 3)
image->flags |= IF_OPAQUE;
return Q_ERR_SUCCESS;
}
/*
=================================================================
STB_IMAGE SAVING
=================================================================
*/
IMG_SAVE(TGA)
{
stbi_flip_vertically_on_write(1);
int ret = stbi_write_tga_to_func(stbi_write, (void*)(size_t)f, width, height, 3, pic);
if (ret)
return Q_ERR_SUCCESS;
return Q_ERR_LIBRARY_ERROR;
}
IMG_SAVE(JPG)
{
stbi_flip_vertically_on_write(1);
int ret = stbi_write_jpg_to_func(stbi_write, (void*)(size_t)f, width, height, 3, pic, param);
if (ret)
return Q_ERR_SUCCESS;
return Q_ERR_LIBRARY_ERROR;
}
IMG_SAVE(PNG)
{
stbi_flip_vertically_on_write(1);
int ret = stbi_write_png_to_func(stbi_write, (void*)(size_t)f, width, height, 3, pic, row_stride);
if (ret)
return Q_ERR_SUCCESS;
return Q_ERR_LIBRARY_ERROR;
}
/*
=========================================================
SCREEN SHOTS
=========================================================
*/
static cvar_t *r_screenshot_format;
static cvar_t *r_screenshot_quality;
static cvar_t* r_screenshot_compression;
static cvar_t* r_screenshot_message;
static qhandle_t create_screenshot(char *buffer, size_t size,
const char *name, const char *ext)
{
qhandle_t f;
qerror_t ret;
int i;
if (name && *name) {
// save to user supplied name
return FS_EasyOpenFile(buffer, size, FS_MODE_WRITE,
"screenshots/", name, ext);
}
// find a file name to save it to
for (i = 0; i < 1000; i++) {
Q_snprintf(buffer, size, "screenshots/quake%03d%s", i, ext);
ret = FS_FOpenFile(buffer, &f, FS_MODE_WRITE | FS_FLAG_EXCL);
if (f) {
return f;
}
if (ret != Q_ERR_EXIST) {
Com_EPrintf("Couldn't exclusively open %s for writing: %s\n",
buffer, Q_ErrorString(ret));
return 0;
}
}
Com_EPrintf("All screenshot slots are full.\n");
return 0;
}
static void make_screenshot(const char *name, const char *ext,
qerror_t (*save)(qhandle_t, const char *, byte *, int, int, int, int),
int param)
{
char buffer[MAX_OSPATH];
byte *pixels;
qerror_t ret;
qhandle_t f;
int w, h, rowbytes;
f = create_screenshot(buffer, sizeof(buffer), name, ext);
if (!f) {
return;
}
pixels = IMG_ReadPixels(&w, &h, &rowbytes);
ret = save(f, buffer, pixels, w, h, rowbytes, param);
FS_FreeTempMem(pixels);
FS_FCloseFile(f);
if (ret < 0) {
Com_EPrintf("Couldn't write %s: %s\n", buffer, Q_ErrorString(ret));
} else if(r_screenshot_message->integer) {
Com_Printf("Wrote %s\n", buffer);
}
}
/*
==================
IMG_ScreenShot_f
Standard function to take a screenshot. Saves in default format unless user
overrides format with a second argument. Screenshot name can't be
specified. This function is always compiled in to give a meaningful warning
if no formats are available.
==================
*/
static void IMG_ScreenShot_f(void)
{
const char *s;
if (Cmd_Argc() > 2) {
Com_Printf("Usage: %s [format]\n", Cmd_Argv(0));
return;
}
if (Cmd_Argc() > 1) {
s = Cmd_Argv(1);
} else {
s = r_screenshot_format->string;
}
if (*s == 'j') {
make_screenshot(NULL, ".jpg", IMG_SaveJPG,
r_screenshot_quality->integer);
return;
}
if (*s == 'p') {
make_screenshot(NULL, ".png", IMG_SavePNG,
r_screenshot_compression->integer);
return;
}
make_screenshot(NULL, ".tga", IMG_SaveTGA, 0);
}
/*
==================
IMG_ScreenShotXXX_f
Specialized function to take a screenshot in specified format. Screenshot name
can be also specified, as well as quality and compression options.
==================
*/
static void IMG_ScreenShotTGA_f(void)
{
if (Cmd_Argc() > 2) {
Com_Printf("Usage: %s [name]\n", Cmd_Argv(0));
return;
}
make_screenshot(Cmd_Argv(1), ".tga", IMG_SaveTGA, 0);
}
static void IMG_ScreenShotJPG_f(void)
{
int quality;
if (Cmd_Argc() > 3) {
Com_Printf("Usage: %s [name] [quality]\n", Cmd_Argv(0));
return;
}
if (Cmd_Argc() > 2) {
quality = atoi(Cmd_Argv(2));
} else {
quality = r_screenshot_quality->integer;
}
make_screenshot(Cmd_Argv(1), ".jpg", IMG_SaveJPG, quality);
}
static void IMG_ScreenShotPNG_f(void)
{
int compression;
if (Cmd_Argc() > 3) {
Com_Printf("Usage: %s [name] [compression]\n", Cmd_Argv(0));
return;
}
if (Cmd_Argc() > 2) {
compression = atoi(Cmd_Argv(2));
} else {
compression = r_screenshot_compression->integer;
}
make_screenshot(Cmd_Argv(1), ".png", IMG_SavePNG, compression);
}
/*
=========================================================
IMAGE PROCESSING
=========================================================
*/
void IMG_ResampleTexture(const byte *in, int inwidth, int inheight,
byte *out, int outwidth, int outheight)
{
int i, j;
const byte *inrow1, *inrow2;
unsigned frac, fracstep;
unsigned p1[MAX_TEXTURE_SIZE], p2[MAX_TEXTURE_SIZE];
const byte *pix1, *pix2, *pix3, *pix4;
float heightScale;
if (outwidth > MAX_TEXTURE_SIZE) {
Com_Error(ERR_FATAL, "%s: outwidth > %d", __func__, MAX_TEXTURE_SIZE);
}
fracstep = inwidth * 0x10000 / outwidth;
frac = fracstep >> 2;
for (i = 0; i < outwidth; i++) {
p1[i] = 4 * (frac >> 16);
frac += fracstep;
}
frac = 3 * (fracstep >> 2);
for (i = 0; i < outwidth; i++) {
p2[i] = 4 * (frac >> 16);
frac += fracstep;
}
heightScale = (float)inheight / outheight;
inwidth <<= 2;
for (i = 0; i < outheight; i++) {
inrow1 = in + inwidth * (int)((i + 0.25f) * heightScale);
inrow2 = in + inwidth * (int)((i + 0.75f) * heightScale);
for (j = 0; j < outwidth; j++) {
pix1 = inrow1 + p1[j];
pix2 = inrow1 + p2[j];
pix3 = inrow2 + p1[j];
pix4 = inrow2 + p2[j];
out[0] = (pix1[0] + pix2[0] + pix3[0] + pix4[0]) >> 2;
out[1] = (pix1[1] + pix2[1] + pix3[1] + pix4[1]) >> 2;
out[2] = (pix1[2] + pix2[2] + pix3[2] + pix4[2]) >> 2;
out[3] = (pix1[3] + pix2[3] + pix3[3] + pix4[3]) >> 2;
out += 4;
}
}
}
void IMG_MipMap(byte *out, byte *in, int width, int height)
{
int i, j;
width <<= 2;
height >>= 1;
for (i = 0; i < height; i++, in += width) {
for (j = 0; j < width; j += 8, out += 4, in += 8) {
out[0] = (in[0] + in[4] + in[width + 0] + in[width + 4]) >> 2;
out[1] = (in[1] + in[5] + in[width + 1] + in[width + 5]) >> 2;
out[2] = (in[2] + in[6] + in[width + 2] + in[width + 6]) >> 2;
out[3] = (in[3] + in[7] + in[width + 3] + in[width + 7]) >> 2;
}
}
}
/*
=========================================================
IMAGE MANAGER
=========================================================
*/
#define RIMAGES_HASH 256
static list_t r_imageHash[RIMAGES_HASH];
image_t r_images[MAX_RIMAGES];
int r_numImages;
uint32_t d_8to24table[256];
static const struct {
char ext[4];
qerror_t (*load)(byte *, size_t, image_t *, byte **);
} img_loaders[IM_MAX] = {
{ "pcx", IMG_LoadPCX },
{ "wal", IMG_LoadWAL },
{ "tga", IMG_LoadSTB },
{ "jpg", IMG_LoadSTB },
{ "png", IMG_LoadSTB }
};
static imageformat_t img_search[IM_MAX];
static int img_total;
static cvar_t *r_override_textures;
static cvar_t *r_texture_formats;
/*
===============
IMG_List_f
===============
*/
static void IMG_List_f(void)
{
int i;
image_t *image;
int texels, count;
if (Cmd_Argc() > 1) {
// save to file
char path[MAX_OSPATH];
qhandle_t f = FS_EasyOpenFile(path, sizeof(path), FS_MODE_WRITE | FS_FLAG_TEXT, "", Cmd_Argv(1), ".csv");
if (!f) {
Com_EPrintf("Error opening '%s'\n", path);
return;
}
for (i = 1, count = 0, image = r_images + 1; i < r_numImages; i++, image++) {
if (!image->registration_sequence)
continue;
char fmt[MAX_QPATH];
sprintf(fmt, "%%-%ds, %%-%ds, (%% 5d %% 5d), sRGB:%%d\n", MAX_QPATH, MAX_QPATH);
FS_FPrintf(f, fmt,
image->name,
image->filepath,
image->width,
image->height,
image->is_srgb);
}
FS_FCloseFile(f);
Com_Printf("Saved '%s'\n", path);
} else {
// dump to console
static const char types[8] = "PFMSWY??";
Com_Printf("------------------\n");
texels = count = 0;
for (i = 1, image = r_images + 1; i < r_numImages; i++, image++) {
if (!image->registration_sequence)
continue;
Com_Printf("%c%c%c%c %4i %4i %s: %s\n",
types[image->type > IT_MAX ? IT_MAX : image->type],
(image->flags & IF_TRANSPARENT) ? 'T' : ' ',
(image->flags & IF_SCRAP) ? 'S' : ' ',
(image->flags & IF_PERMANENT) ? '*' : ' ',
image->upload_width,
image->upload_height,
(image->flags & IF_PALETTED) ? "PAL" : "RGB",
image->name);
texels += image->upload_width * image->upload_height;
count++;
}
Com_Printf("Total images: %d (out of %d slots)\n", count, r_numImages);
Com_Printf("Total texels: %d (not counting mipmaps)\n", texels);
}
}
static image_t *alloc_image(void)
{
int i;
image_t *image;
// find a free image_t slot
for (i = 1, image = r_images + 1; i < r_numImages; i++, image++) {
if (!image->registration_sequence)
break;
}
if (i == r_numImages) {
if (r_numImages == MAX_RIMAGES)
return NULL;
r_numImages++;
}
return image;
}
// finds the given image of the given type.
// case and extension insensitive.
static image_t *lookup_image(const char *name,
imagetype_t type, unsigned hash, size_t baselen)
{
image_t *image;
// look for it
LIST_FOR_EACH(image_t, image, &r_imageHash[hash], entry) {
if (image->type != type) {
continue;
}
if (image->baselen != baselen) {
continue;
}
if (!FS_pathcmpn(image->name, name, baselen)) {
return image;
}
}
return NULL;
}
static int _try_image_format(imageformat_t fmt, image_t *image, byte **pic)
{
byte *data;
ssize_t len;
qerror_t ret;
// load the file
len = FS_LoadFile(image->name, (void **)&data);
if (!data) {
return len;
}
// decompress the image
ret = img_loaders[fmt].load(data, len, image, pic);
FS_FreeFile(data);
image->filepath[0] = 0;
if (ret >= 0) {
strcpy(image->filepath, image->name);
// record last modified time (skips reload when invoking IMG_ReloadAll)
image->last_modified = 0;
FS_LastModified(image->filepath, &image->last_modified);
}
return ret < 0 ? ret : fmt;
}
static int try_image_format(imageformat_t fmt, image_t *image, byte **pic)
{
// replace the extension
memcpy(image->name + image->baselen + 1, img_loaders[fmt].ext, 4);
return _try_image_format(fmt, image, pic);
}
// tries to load the image with a different extension
static int try_other_formats(imageformat_t orig, image_t *image, byte **pic)
{
imageformat_t fmt;
qerror_t ret;
int i;
// search through all the 32-bit formats
for (i = 0; i < img_total; i++) {
fmt = img_search[i];
if (fmt == orig) {
continue; // don't retry twice
}
ret = try_image_format(fmt, image, pic);
if (ret != Q_ERR_NOENT) {
return ret; // found something
}
}
// fall back to 8-bit formats
fmt = (image->type == IT_WALL) ? IM_WAL : IM_PCX;
if (fmt == orig) {
return Q_ERR_NOENT; // don't retry twice
}
return try_image_format(fmt, image, pic);
}
static void get_image_dimensions(imageformat_t fmt, image_t *image)
{
char buffer[MAX_QPATH];
ssize_t len;
miptex_t mt;
dpcx_t pcx;
qhandle_t f;
unsigned w, h;
memcpy(buffer, image->name, image->baselen + 1);
w = h = 0;
if (fmt == IM_WAL) {
memcpy(buffer + image->baselen + 1, "wal", 4);
FS_FOpenFile(buffer, &f, FS_MODE_READ);
if (f) {
len = FS_Read(&mt, sizeof(mt), f);
if (len == sizeof(mt)) {
w = LittleLong(mt.width);
h = LittleLong(mt.height);
}
FS_FCloseFile(f);
}
} else {
memcpy(buffer + image->baselen + 1, "pcx", 4);
FS_FOpenFile(buffer, &f, FS_MODE_READ);
if (f) {
len = FS_Read(&pcx, sizeof(pcx), f);
if (len == sizeof(pcx)) {
w = LittleShort(pcx.xmax) + 1;
h = LittleShort(pcx.ymax) + 1;
}
FS_FCloseFile(f);
}
}
if (w < 1 || h < 1 || w > 512 || h > 512) {
return;
}
image->width = w;
image->height = h;
}
static void r_texture_formats_changed(cvar_t *self)
{
char *s;
int i, j;
// reset the search order
img_total = 0;
// parse the string
for (s = self->string; *s; s++) {
switch (*s) {
case 't': case 'T': i = IM_TGA; break;
case 'j': case 'J': i = IM_JPG; break;
case 'p': case 'P': i = IM_PNG; break;
default: continue;
}
// don't let format to be specified more than once
for (j = 0; j < img_total; j++)
if (img_search[j] == i)
break;
if (j != img_total)
continue;
img_search[img_total++] = i;
if (img_total == IM_MAX) {
break;
}
}
}
qerror_t
load_img(const char *name, image_t *image)
{
byte *pic;
imageformat_t fmt;
qerror_t ret;
size_t len = strlen(name);
// must have an extension and at least 1 char of base name
if (len <= 4) {
return Q_ERR_NAMETOOSHORT;
}
if (name[len - 4] != '.') {
return Q_ERR_INVALID_PATH;
}
memcpy(image->name, name, len + 1);
image->baselen = len - 4;
image->type = 0;
image->flags = 0;
image->registration_sequence = 1;
// find out original extension
for (fmt = 0; fmt < IM_MAX; fmt++) {
if (!Q_stricmp(image->name + image->baselen + 1, img_loaders[fmt].ext)) {
break;
}
}
// load the pic from disk
pic = NULL;
// first try with original extension
ret = _try_image_format(fmt, image, &pic);
if (ret == Q_ERR_NOENT) {
// retry with remaining extensions
ret = try_other_formats(fmt, image, &pic);
}
// if we are replacing 8-bit texture with a higher resolution 32-bit
// texture, we need to recover original image dimensions
if (fmt <= IM_WAL && ret > IM_WAL) {
get_image_dimensions(fmt, image);
}
// if we are replacing 8-bit texture with a higher resolution 32-bit
// texture, we need to recover original image dimensions
if (fmt <= IM_WAL && ret > IM_WAL) {
get_image_dimensions(fmt, image);
}
if (ret < 0) {
memset(image, 0, sizeof(*image));
return ret;
}
#if USE_REF == REF_VKPT
image->pix_data = pic;
#endif
return Q_ERR_SUCCESS;
}
// finds or loads the given image, adding it to the hash table.
static qerror_t find_or_load_image(const char *name, size_t len,
imagetype_t type, imageflags_t flags,
image_t **image_p)
{
image_t *image;
byte *pic;
unsigned hash;
imageformat_t fmt;
qerror_t ret;