Image: Share code for load Wal

This commit is contained in:
Denis Pauk 2022-10-13 00:59:15 +03:00
parent ec83adcba5
commit 11f5b9e475
9 changed files with 124 additions and 177 deletions

View file

@ -26,9 +26,52 @@
#include "../ref_shared.h"
/*
* NOTE: LoadWal() is in *_image.c because it needs the renderer-specific image_t
*/
struct image_s*
LoadWal(const char *origname, imagetype_t type, load_image_t loadImage)
{
int width, height, ofs, size;
struct image_s *image;
char name[256];
miptex_t *mt;
FixFileExt(origname, "wal", name, sizeof(name));
size = ri.FS_LoadFile(name, (void **)&mt);
if (!mt)
{
R_Printf(PRINT_ALL, "%s: can't load %s\n", __func__, name);
return NULL;
}
if (size < sizeof(miptex_t))
{
R_Printf(PRINT_ALL, "%s: can't load %s, small header\n", __func__, name);
ri.FS_FreeFile((void *)mt);
return NULL;
}
width = LittleLong(mt->width);
height = LittleLong(mt->height);
ofs = LittleLong(mt->offsets[0]);
if ((ofs <= 0) || (width <= 0) || (height <= 0) ||
(((size - ofs) / height) < width))
{
R_Printf(PRINT_ALL, "%s: can't load %s, small body\n", __func__, name);
ri.FS_FreeFile((void *)mt);
return NULL;
}
image = loadImage(name, (byte *)mt + ofs,
width, 0,
height, 0,
(size - ofs), type, 8);
ri.FS_FreeFile((void *)mt);
return image;
}
void
GetWalInfo(const char *origname, int *width, int *height)

View file

@ -855,7 +855,7 @@ R_Upload8(byte *data, int width, int height, qboolean mipmap, qboolean is_sky)
*/
image_t *
R_LoadPic(char *name, byte *pic, int width, int realwidth,
int height, int realheight, imagetype_t type, int bits)
int height, int realheight, size_t data_size, imagetype_t type, int bits)
{
image_t *image;
int i;
@ -895,7 +895,7 @@ R_LoadPic(char *name, byte *pic, int width, int realwidth,
if (strlen(name) >= sizeof(image->name))
{
ri.Sys_Error(ERR_DROP, "Draw_LoadPic: \"%s\" is too long", name);
ri.Sys_Error(ERR_DROP, "%s: \"%s\" is too long", __func__, name);
}
strcpy(image->name, name);
@ -1027,50 +1027,6 @@ R_LoadPic(char *name, byte *pic, int width, int realwidth,
return image;
}
static image_t *
LoadWal(char *origname, imagetype_t type)
{
miptex_t *mt;
int width, height, ofs, size;
image_t *image;
char name[256];
FixFileExt(origname, "wal", name, sizeof(name));
size = ri.FS_LoadFile(name, (void **)&mt);
if (!mt)
{
R_Printf(PRINT_ALL, "LoadWal: can't load %s\n", name);
return r_notexture;
}
if (size < sizeof(miptex_t))
{
R_Printf(PRINT_ALL, "LoadWal: can't load %s, small header\n", name);
ri.FS_FreeFile((void *)mt);
return r_notexture;
}
width = LittleLong(mt->width);
height = LittleLong(mt->height);
ofs = LittleLong(mt->offsets[0]);
if ((ofs <= 0) || (width <= 0) || (height <= 0) ||
(((size - ofs) / height) < width))
{
R_Printf(PRINT_ALL, "LoadWal: can't load %s, small body\n", name);
ri.FS_FreeFile((void *)mt);
return r_notexture;
}
image = R_LoadPic(name, (byte *)mt + ofs, width, 0, height, 0, type, 8);
ri.FS_FreeFile((void *)mt);
return image;
}
static image_t *
LoadM8(const char *origname, imagetype_t type)
{
@ -1126,7 +1082,10 @@ LoadM8(const char *origname, imagetype_t type)
image_buffer[i * 4 + 3] = value == 255 ? 0 : 255;
}
image = R_LoadPic(name, image_buffer, width, 0, height, 0, type, 32);
image = R_LoadPic(name, image_buffer,
width, 0,
height, 0,
width * height, type, 32);
free(image_buffer);
ri.FS_FreeFile((void *)mt);
@ -1208,8 +1167,10 @@ R_FindImage(char *name, imagetype_t type)
|| LoadSTB(namewe, "jpg", &pic, &width, &height) )
{
/* upload tga or png or jpg */
image = R_LoadPic(name, pic, width, realwidth, height,
realheight, type, 32);
image = R_LoadPic(name, pic,
width, realwidth,
height, realheight,
width * height, type, 32);
}
else
{
@ -1223,7 +1184,10 @@ R_FindImage(char *name, imagetype_t type)
}
/* Upload the PCX */
image = R_LoadPic(name, pic, width, 0, height, 0, type, 8);
image = R_LoadPic(name, pic,
width, 0,
height, 0,
width * height, type, 8);
}
}
else /* gl_retexture is not set */
@ -1235,7 +1199,10 @@ R_FindImage(char *name, imagetype_t type)
return NULL;
}
image = R_LoadPic(name, pic, width, 0, height, 0, type, 8);
image = R_LoadPic(name, pic,
width, 0,
height, 0,
width * height, type, 8);
}
}
else if (strcmp(ext, "wal") == 0 || strcmp(ext, "m8") == 0)
@ -1264,8 +1231,10 @@ R_FindImage(char *name, imagetype_t type)
|| LoadSTB(namewe, "jpg", &pic, &width, &height) )
{
/* upload tga or png or jpg */
image = R_LoadPic(name, pic, width, realwidth, height,
realheight, type, 32);
image = R_LoadPic(name, pic,
width, realwidth,
height, realheight,
width * height, type, 32);
}
else if (strcmp(ext, "m8") == 0)
{
@ -1274,7 +1243,7 @@ R_FindImage(char *name, imagetype_t type)
else
{
/* WAL if no TGA/PNG/JPEG available (exists always) */
image = LoadWal(namewe, type);
image = (image_t *)LoadWal(namewe, type, (load_image_t)R_LoadPic);
}
if (!image)
@ -1295,7 +1264,7 @@ R_FindImage(char *name, imagetype_t type)
}
else /* gl_retexture is not set */
{
image = LoadWal(name, type);
image = (image_t *)LoadWal(name, type, (load_image_t)R_LoadPic);
if (!image)
{
@ -1335,7 +1304,10 @@ R_FindImage(char *name, imagetype_t type)
if(LoadSTB(name, ext, &pic, &width, &height))
{
image = R_LoadPic(name, pic, width, realwidth, height, realheight, type, 32);
image = R_LoadPic(name, pic,
width, realwidth,
height, realheight,
width * height, type, 32);
}
else
{

View file

@ -99,7 +99,7 @@ R_InitParticleTexture(void)
}
r_particletexture = R_LoadPic("***particle***", (byte *)partData,
16, 0, 16, 0, it_sprite, 32);
16, 0, 16, 0, 16 * 16, it_sprite, 32);
/* also use this for bad textures, but without alpha */
for (x = 0; x < 8; x++)
@ -114,7 +114,7 @@ R_InitParticleTexture(void)
}
r_notexture = R_LoadPic("***r_notexture***", (byte *)notexData,
8, 0, 8, 0, it_wall, 32);
8, 0, 8, 0, 8 * 8, it_wall, 32);
}
void

View file

@ -282,7 +282,7 @@ void COM_StripExtension(char *in, char *out);
void R_SwapBuffers(int);
image_t *R_LoadPic(char *name, byte *pic, int width, int realwidth,
int height, int realheight, imagetype_t type, int bits);
int height, int realheight, size_t data_size, imagetype_t type, int bits);
image_t *R_FindImage(char *name, imagetype_t type);
void R_TextureMode(char *string);
void R_ImageList_f(void);

View file

@ -376,7 +376,8 @@ FloodFillSkin(byte *skin, int skinwidth, int skinheight)
*/
gl3image_t *
GL3_LoadPic(char *name, byte *pic, int width, int realwidth,
int height, int realheight, imagetype_t type, int bits)
int height, int realheight, size_t data_size,
imagetype_t type, int bits)
{
gl3image_t *image = NULL;
GLuint texNum=0;
@ -416,7 +417,7 @@ GL3_LoadPic(char *name, byte *pic, int width, int realwidth,
if (strlen(name) >= sizeof(image->name))
{
ri.Sys_Error(ERR_DROP, "GL3_LoadPic: \"%s\" is too long", name);
ri.Sys_Error(ERR_DROP, "%s: \"%s\" is too long", __func__, name);
}
strcpy(image->name, name);
@ -596,50 +597,6 @@ GL3_LoadPic(char *name, byte *pic, int width, int realwidth,
return image;
}
static gl3image_t *
LoadWal(const char *origname, imagetype_t type)
{
miptex_t *mt;
int width, height, ofs, size;
gl3image_t *image;
char name[256];
FixFileExt(origname, "wal", name, sizeof(name));
size = ri.FS_LoadFile(name, (void **)&mt);
if (!mt)
{
R_Printf(PRINT_ALL, "LoadWal: can't load %s\n", name);
return gl3_notexture;
}
if (size < sizeof(miptex_t))
{
R_Printf(PRINT_ALL, "LoadWal: can't load %s, small header\n", name);
ri.FS_FreeFile((void *)mt);
return gl3_notexture;
}
width = LittleLong(mt->width);
height = LittleLong(mt->height);
ofs = LittleLong(mt->offsets[0]);
if ((ofs <= 0) || (width <= 0) || (height <= 0) ||
(((size - ofs) / height) < width))
{
R_Printf(PRINT_ALL, "LoadWal: can't load %s, small body\n", name);
ri.FS_FreeFile((void *)mt);
return gl3_notexture;
}
image = GL3_LoadPic(name, (byte *)mt + ofs, width, 0, height, 0, type, 8);
ri.FS_FreeFile((void *)mt);
return image;
}
static gl3image_t *
LoadM8(const char *origname, imagetype_t type)
{
@ -668,7 +625,7 @@ LoadM8(const char *origname, imagetype_t type)
if (LittleLong (mt->version) != M8_VERSION)
{
R_Printf(PRINT_ALL, "LoadWal: can't load %s, wrong magic value.\n", name);
R_Printf(PRINT_ALL, "%s: can't load %s, wrong magic value.\n", __func__, name);
ri.FS_FreeFile ((void *)mt);
return gl3_notexture;
}
@ -695,7 +652,9 @@ LoadM8(const char *origname, imagetype_t type)
image_buffer[i * 4 + 3] = value == 255 ? 0 : 255;
}
image = GL3_LoadPic(name, image_buffer, width, 0, height, 0, type, 32);
image = GL3_LoadPic(name, image_buffer,
width, 0, height, 0,
width * height, type, 32);
free(image_buffer);
ri.FS_FreeFile((void *)mt);
@ -777,8 +736,10 @@ GL3_FindImage(char *name, imagetype_t type)
|| LoadSTB(namewe, "jpg", &pic, &width, &height) )
{
/* upload tga or png or jpg */
image = GL3_LoadPic(name, pic, width, realwidth, height,
realheight, type, 32);
image = GL3_LoadPic(name, pic,
width, realwidth,
height, realheight,
width * height, type, 32);
}
else
{
@ -792,7 +753,10 @@ GL3_FindImage(char *name, imagetype_t type)
}
/* Upload the PCX */
image = GL3_LoadPic(name, pic, width, 0, height, 0, type, 8);
image = GL3_LoadPic(name, pic,
width, 0,
height, 0,
width * height, type, 8);
}
}
else /* gl_retexture is not set */
@ -804,7 +768,10 @@ GL3_FindImage(char *name, imagetype_t type)
return NULL;
}
image = GL3_LoadPic(name, pic, width, 0, height, 0, type, 8);
image = GL3_LoadPic(name, pic,
width, 0,
height, 0,
width * height, type, 8);
}
}
else if (strcmp(ext, "wal") == 0 || strcmp(ext, "m8") == 0)
@ -833,7 +800,10 @@ GL3_FindImage(char *name, imagetype_t type)
|| LoadSTB(namewe, "jpg", &pic, &width, &height) )
{
/* upload tga or png or jpg */
image = GL3_LoadPic(name, pic, width, realwidth, height, realheight, type, 32);
image = GL3_LoadPic(name, pic,
width, realwidth,
height, realheight,
width * height, type, 32);
}
else if (strcmp(ext, "m8") == 0)
{
@ -842,7 +812,7 @@ GL3_FindImage(char *name, imagetype_t type)
else
{
/* WAL if no TGA/PNG/JPEG available (exists always) */
image = LoadWal(namewe, type);
image = (gl3image_t *)LoadWal(namewe, type, (load_image_t)GL3_LoadPic);
}
if (!image)
@ -863,7 +833,7 @@ GL3_FindImage(char *name, imagetype_t type)
}
else /* gl_retexture is not set */
{
image = LoadWal(name, type);
image = (gl3image_t *)LoadWal(name, type, (load_image_t)GL3_LoadPic);
if (!image)
{
@ -903,7 +873,10 @@ GL3_FindImage(char *name, imagetype_t type)
if(LoadSTB(name, ext, &pic, &width, &height))
{
image = GL3_LoadPic(name, pic, width, realwidth, height, realheight, type, 32);
image = GL3_LoadPic(name, pic,
width, realwidth,
height, realheight,
width * height, type, 32);
} else {
return NULL;
}

View file

@ -103,7 +103,7 @@ GL3_InitParticleTexture(void)
}
gl3_particletexture = GL3_LoadPic("***particle***", (byte *)data,
8, 0, 8, 0, it_sprite, 32);
8, 0, 8, 0, 8 * 8, it_sprite, 32);
/* also use this for bad textures, but without alpha */
for (x = 0; x < 8; x++)
@ -118,7 +118,7 @@ GL3_InitParticleTexture(void)
}
gl3_notexture = GL3_LoadPic("***r_notexture***", (byte *)data,
8, 0, 8, 0, it_wall, 32);
8, 0, 8, 0, 8 * 8, it_wall, 32);
}
void

View file

@ -440,7 +440,8 @@ extern void GL3_TextureMode(char *string);
extern void GL3_Bind(GLuint texnum);
extern void GL3_BindLightmap(int lightmapnum);
extern gl3image_t *GL3_LoadPic(char *name, byte *pic, int width, int realwidth,
int height, int realheight, imagetype_t type, int bits);
int height, int realheight, size_t data_size,
imagetype_t type, int bits);
extern gl3image_t *GL3_FindImage(char *name, imagetype_t type);
extern gl3image_t *GL3_RegisterSkin(char *name);
extern void GL3_ShutdownImages(void);

View file

@ -78,6 +78,9 @@ typedef enum
extern void R_Printf(int level, const char* msg, ...) PRINTF_ATTR(2, 3);
/* Shared images load */
typedef struct image_s* (*load_image_t)(const char *name, byte *pic, int width, int realwidth,
int height, int realheight, size_t data_size, imagetype_t type, int bits);
extern struct image_s* LoadWal(const char *origname, imagetype_t type, load_image_t loadImage);
extern void FixFileExt(const char *origname, const char *ext, char *filename, size_t size);
extern void GetPCXPalette(byte **colormap, unsigned *d_8to24table);
extern void LoadPCX(const char *origname, byte **pic, byte **palette, int *width, int *height);

View file

@ -212,7 +212,7 @@ R_LoadPic
*/
static image_t *
R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int realheight,
size_t data_size, imagetype_t type)
size_t data_size, imagetype_t type, int bits)
{
image_t *image;
size_t size, full_size;
@ -277,56 +277,6 @@ R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int real
return image;
}
/*
================
R_LoadWal
================
*/
static image_t *
R_LoadWal (char *name, imagetype_t type)
{
miptex_t *mt;
int ofs;
image_t *image;
size_t file_size, width, height;
file_size = ri.FS_LoadFile (name, (void **)&mt);
if (!mt)
{
R_Printf(PRINT_ALL, "%s: can't load %s\n", __func__, name);
return r_notexture_mip;
}
if (file_size < sizeof(miptex_t))
{
R_Printf(PRINT_ALL, "%s: can't load %s, small header\n", __func__, name);
ri.FS_FreeFile((void *)mt);
return r_notexture_mip;
}
width = LittleLong (mt->width);
height = LittleLong (mt->height);
ofs = LittleLong(mt->offsets[0]);
/* width/height are unsigned */
if ((ofs <= 0) || (width == 0) || (height == 0) ||
((file_size - ofs) / width < height))
{
R_Printf(PRINT_ALL, "%s: can't load %s, small body\n", __func__, name);
ri.FS_FreeFile((void *)mt);
return r_notexture_mip;
}
image = R_LoadPic(name, (byte *)mt + ofs,
width, width,
height, height,
(file_size - ofs), type);
ri.FS_FreeFile((void *)mt);
return image;
}
static byte *d_16to8table = NULL; // 16 to 8 bit conversion table
/*
@ -604,7 +554,7 @@ R_LoadHiColorImage(char *name, const char* namewe, const char *ext, imagetype_t
image = R_LoadPic(name, pic8,
uploadwidth, realwidth,
uploadheight, realheight,
uploadwidth * uploadheight, type);
uploadwidth * uploadheight, type, 8);
}
free(pic32);
}
@ -614,7 +564,7 @@ R_LoadHiColorImage(char *name, const char* namewe, const char *ext, imagetype_t
image = R_LoadPic(name, pic8,
width, width,
height, height,
width * height, type);
width * height, type, 8);
}
free(pic8);
}
@ -670,7 +620,7 @@ R_LoadImage(char *name, const char* namewe, const char *ext, imagetype_t type)
image = R_LoadPic(name, scaled,
width, realwidth,
height, realheight,
width * height, type);
width * height, type, 8);
free(scaled);
}
else
@ -678,7 +628,7 @@ R_LoadImage(char *name, const char* namewe, const char *ext, imagetype_t type)
image = R_LoadPic(name, pic,
width, width,
height, height,
width * height, type);
width * height, type, 8);
}
if (palette)
@ -689,7 +639,7 @@ R_LoadImage(char *name, const char* namewe, const char *ext, imagetype_t type)
}
else if (strcmp(ext, "wal") == 0)
{
image = R_LoadWal(name, type);
image = (image_t *)LoadWal(namewe, type, (load_image_t)R_LoadPic);
}
else if (strcmp(ext, "m8") == 0)
{
@ -697,6 +647,11 @@ R_LoadImage(char *name, const char* namewe, const char *ext, imagetype_t type)
}
}
if (!image)
{
image = r_notexture_mip;
}
return image;
}