From 18bc78833538fb88e73b811390b154b8f0de0756 Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Fri, 14 Oct 2022 14:06:26 +0300 Subject: [PATCH] Image: Share LoadImage --- src/client/refresh/files/stb.c | 74 +++++++++++- src/client/refresh/files/wal.c | 3 - src/client/refresh/gl1/gl1_image.c | 184 ++--------------------------- src/client/refresh/gl3/gl3_image.c | 182 ++-------------------------- src/client/refresh/ref_shared.h | 4 +- src/client/refresh/soft/sw_image.c | 134 ++++++++------------- 6 files changed, 142 insertions(+), 439 deletions(-) diff --git a/src/client/refresh/files/stb.c b/src/client/refresh/files/stb.c index 4dc817ce..f898238f 100644 --- a/src/client/refresh/files/stb.c +++ b/src/client/refresh/files/stb.c @@ -435,8 +435,8 @@ scale3x(const byte *src, byte *dst, int width, int height) } } -struct image_s * -LoadHiColorImage(char *name, const char* namewe, const char *ext, +static struct image_s * +LoadHiColorImage(const char *name, const char* namewe, const char *ext, imagetype_t type, loadimage_t load_image) { int realwidth = 0, realheight = 0; @@ -490,3 +490,73 @@ LoadHiColorImage(char *name, const char* namewe, const char *ext, return image; } + +struct image_s * +LoadImage(const char *name, const char* namewe, const char *ext, imagetype_t type, + qboolean r_retexturing, loadimage_t load_image) +{ + struct image_s *image = NULL; + + // with retexturing and not skin + if (r_retexturing) + { + image = LoadHiColorImage(name, namewe, ext, type, load_image); + } + + if (!image) + { + if (!strcmp(ext, "pcx")) + { + byte *pic = NULL; + byte *palette = NULL; + int width = 0, height = 0; + + LoadPCX (namewe, &pic, &palette, &width, &height); + if (!pic) + return NULL; + + image = load_image(name, pic, + width, width, + height, height, + width * height, type, 8); + + if (palette) + { + free(palette); + } + free(pic); + } + else if (!strcmp(ext, "wal")) + { + image = LoadWal(namewe, type, load_image); + } + else if (!strcmp(ext, "m8")) + { + image = LoadM8(namewe, type, load_image); + } + else if (!strcmp(ext, "m32")) + { + image = LoadM32(namewe, type, load_image); + } + else if (!strcmp(ext, "tga") || + !strcmp(ext, "png") || + !strcmp(ext, "jpg")) + { + byte *pic = NULL; + int width = 0, height = 0; + + if (LoadSTB (namewe, ext, &pic, &width, &height) && pic) + { + image = load_image(name, pic, + width, width, + height, height, + width * height, + type, 32); + + free(pic); + } + } + } + + return image; +} diff --git a/src/client/refresh/files/wal.c b/src/client/refresh/files/wal.c index d7075909..ec72afdc 100644 --- a/src/client/refresh/files/wal.c +++ b/src/client/refresh/files/wal.c @@ -40,7 +40,6 @@ LoadWal(const char *origname, imagetype_t type, loadimage_t load_image) if (!mt) { - R_Printf(PRINT_ALL, "%s: can't load %s\n", __func__, name); return NULL; } @@ -88,7 +87,6 @@ LoadM8(const char *origname, imagetype_t type, loadimage_t load_image) if (!mt) { - R_Printf(PRINT_ALL, "%s: can't load %s\n", __func__, name); return NULL; } @@ -153,7 +151,6 @@ LoadM32(const char *origname, imagetype_t type, loadimage_t load_image) if (!mt) { - R_Printf(PRINT_ALL, "%s: can't load %s\n", __func__, name); return NULL; } diff --git a/src/client/refresh/gl1/gl1_image.c b/src/client/refresh/gl1/gl1_image.c index 23d6b0ad..04b05157 100644 --- a/src/client/refresh/gl1/gl1_image.c +++ b/src/client/refresh/gl1/gl1_image.c @@ -1035,11 +1035,8 @@ R_FindImage(char *name, imagetype_t type) { image_t *image; int i, len; - byte *pic; - int width, height; char *ptr; char namewe[256]; - int realwidth = 0, realheight = 0; const char* ext; if (!name) @@ -1081,181 +1078,16 @@ R_FindImage(char *name, imagetype_t type) } } - /* load the pic from disk */ - pic = NULL; + // + // load the pic from disk + // + image = (image_t *)LoadImage(name, namewe, ext, type, + r_retexturing->value, (loadimage_t)R_LoadPic); - if (strcmp(ext, "pcx") == 0) + if (!image) { - if (r_retexturing->value) - { - GetPCXInfo(name, &realwidth, &realheight); - if(realwidth == 0) - { - /* No texture found */ - return NULL; - } - - /* try to load a tga, png or jpg (in that order/priority) */ - if ( LoadSTB(namewe, "tga", &pic, &width, &height) - || LoadSTB(namewe, "png", &pic, &width, &height) - || LoadSTB(namewe, "jpg", &pic, &width, &height) ) - { - /* upload tga or png or jpg */ - image = R_LoadPic(name, pic, - width, realwidth, - height, realheight, - width * height, type, 32); - } - else - { - /* PCX if no TGA/PNG/JPEG available (exists always) */ - LoadPCX(name, &pic, NULL, &width, &height); - - if (!pic) - { - /* No texture found */ - return NULL; - } - - /* Upload the PCX */ - image = R_LoadPic(name, pic, - width, 0, - height, 0, - width * height, type, 8); - } - } - else /* gl_retexture is not set */ - { - LoadPCX(name, &pic, NULL, &width, &height); - - if (!pic) - { - return NULL; - } - - image = R_LoadPic(name, pic, - width, 0, - height, 0, - width * height, type, 8); - } - } - else if (strcmp(ext, "wal") == 0 || strcmp(ext, "m8") == 0) - { - if (r_retexturing->value) - { - /* Get size of the original texture */ - if (strcmp(ext, "m8") == 0) - { - GetM8Info(name, &realwidth, &realheight); - } - else - { - GetWalInfo(name, &realwidth, &realheight); - } - - if(realwidth == 0) - { - /* No texture found */ - return NULL; - } - - /* try to load a tga, png or jpg (in that order/priority) */ - if ( LoadSTB(namewe, "tga", &pic, &width, &height) - || LoadSTB(namewe, "png", &pic, &width, &height) - || LoadSTB(namewe, "jpg", &pic, &width, &height) ) - { - /* upload tga or png or jpg */ - image = R_LoadPic(name, pic, - width, realwidth, - height, realheight, - width * height, type, 32); - } - else if (strcmp(ext, "m8") == 0) - { - image = (image_t *)LoadM8(namewe, type, (loadimage_t)R_LoadPic); - } - else - { - /* WAL if no TGA/PNG/JPEG available (exists always) */ - image = (image_t *)LoadWal(namewe, type, (loadimage_t)R_LoadPic); - } - - if (!image) - { - /* No texture found */ - return NULL; - } - } - else if (strcmp(ext, "m8") == 0) - { - image = (image_t *)LoadM8(name, type, (loadimage_t)R_LoadPic); - - if (!image) - { - /* No texture found */ - return NULL; - } - } - else /* gl_retexture is not set */ - { - image = (image_t *)LoadWal(name, type, (loadimage_t)R_LoadPic); - - if (!image) - { - /* No texture found */ - return NULL; - } - } - } - else if (strcmp(ext, "tga") == 0 || strcmp(ext, "png") == 0 || strcmp(ext, "jpg") == 0) - { - char tmp_name[256]; - - realwidth = 0; - realheight = 0; - - strcpy(tmp_name, namewe); - strcat(tmp_name, ".wal"); - GetWalInfo(tmp_name, &realwidth, &realheight); - - if (realwidth == 0 || realheight == 0) { - strcpy(tmp_name, namewe); - strcat(tmp_name, ".m8"); - GetM8Info(tmp_name, &realwidth, &realheight); - } - - if (realwidth == 0 || realheight == 0) { - /* It's a sky or model skin. */ - strcpy(tmp_name, namewe); - strcat(tmp_name, ".pcx"); - GetPCXInfo(tmp_name, &realwidth, &realheight); - } - - /* TODO: not sure if not having realwidth/heigth is bad - a tga/png/jpg - * was requested, after all, so there might be no corresponding wal/pcx? - * if (realwidth == 0 || realheight == 0) return NULL; - */ - - if(LoadSTB(name, ext, &pic, &width, &height)) - { - image = R_LoadPic(name, pic, - width, realwidth, - height, realheight, - width * height, type, 32); - } - else - { - return NULL; - } - } - else - { - return NULL; - } - - if (pic) - { - free(pic); + R_Printf(PRINT_ALL, "%s: can't load %s\n", __func__, name); + image = r_notexture; } return image; diff --git a/src/client/refresh/gl3/gl3_image.c b/src/client/refresh/gl3/gl3_image.c index a3b30ee9..3f6409b5 100644 --- a/src/client/refresh/gl3/gl3_image.c +++ b/src/client/refresh/gl3/gl3_image.c @@ -605,11 +605,8 @@ GL3_FindImage(char *name, imagetype_t type) { gl3image_t *image; int i, len; - byte *pic; - int width, height; char *ptr; char namewe[256]; - int realwidth = 0, realheight = 0; const char* ext; if (!name) @@ -651,179 +648,16 @@ GL3_FindImage(char *name, imagetype_t type) } } - /* load the pic from disk */ - pic = NULL; + // + // load the pic from disk + // + image = (gl3image_t *)LoadImage(name, namewe, ext, type, + r_retexturing->value, (loadimage_t)GL3_LoadPic); - if (strcmp(ext, "pcx") == 0) + if (!image) { - if (r_retexturing->value) - { - GetPCXInfo(name, &realwidth, &realheight); - if(realwidth == 0) - { - /* No texture found */ - return NULL; - } - - /* try to load a tga, png or jpg (in that order/priority) */ - if ( LoadSTB(namewe, "tga", &pic, &width, &height) - || LoadSTB(namewe, "png", &pic, &width, &height) - || LoadSTB(namewe, "jpg", &pic, &width, &height) ) - { - /* upload tga or png or jpg */ - image = GL3_LoadPic(name, pic, - width, realwidth, - height, realheight, - width * height, type, 32); - } - else - { - /* PCX if no TGA/PNG/JPEG available (exists always) */ - LoadPCX(name, &pic, NULL, &width, &height); - - if (!pic) - { - /* No texture found */ - return NULL; - } - - /* Upload the PCX */ - image = GL3_LoadPic(name, pic, - width, 0, - height, 0, - width * height, type, 8); - } - } - else /* gl_retexture is not set */ - { - LoadPCX(name, &pic, NULL, &width, &height); - - if (!pic) - { - return NULL; - } - - image = GL3_LoadPic(name, pic, - width, 0, - height, 0, - width * height, type, 8); - } - } - else if (strcmp(ext, "wal") == 0 || strcmp(ext, "m8") == 0) - { - if (r_retexturing->value) - { - /* Get size of the original texture */ - if (strcmp(ext, "m8") == 0) - { - GetM8Info(name, &realwidth, &realheight); - } - else - { - GetWalInfo(name, &realwidth, &realheight); - } - - if(realwidth == 0) - { - /* No texture found */ - return NULL; - } - - /* try to load a tga, png or jpg (in that order/priority) */ - if ( LoadSTB(namewe, "tga", &pic, &width, &height) - || LoadSTB(namewe, "png", &pic, &width, &height) - || LoadSTB(namewe, "jpg", &pic, &width, &height) ) - { - /* upload tga or png or jpg */ - image = GL3_LoadPic(name, pic, - width, realwidth, - height, realheight, - width * height, type, 32); - } - else if (strcmp(ext, "m8") == 0) - { - image = (gl3image_t *)LoadM8(namewe, type, (loadimage_t)GL3_LoadPic); - } - else - { - /* WAL if no TGA/PNG/JPEG available (exists always) */ - image = (gl3image_t *)LoadWal(namewe, type, (loadimage_t)GL3_LoadPic); - } - - if (!image) - { - /* No texture found */ - return NULL; - } - } - else if (strcmp(ext, "m8") == 0) - { - image = (gl3image_t *)LoadM8(name, type, (loadimage_t)GL3_LoadPic); - - if (!image) - { - /* No texture found */ - return NULL; - } - } - else /* gl_retexture is not set */ - { - image = (gl3image_t *)LoadWal(name, type, (loadimage_t)GL3_LoadPic); - - if (!image) - { - /* No texture found */ - return NULL; - } - } - } - else if (strcmp(ext, "tga") == 0 || strcmp(ext, "png") == 0 || strcmp(ext, "jpg") == 0) - { - char tmp_name[256]; - - realwidth = 0; - realheight = 0; - - strcpy(tmp_name, namewe); - strcat(tmp_name, ".wal"); - GetWalInfo(tmp_name, &realwidth, &realheight); - - if (realwidth == 0 || realheight == 0) { - strcpy(tmp_name, namewe); - strcat(tmp_name, ".m8"); - GetM8Info(tmp_name, &realwidth, &realheight); - } - - if (realwidth == 0 || realheight == 0) { - /* It's a sky or model skin. */ - strcpy(tmp_name, namewe); - strcat(tmp_name, ".pcx"); - GetPCXInfo(tmp_name, &realwidth, &realheight); - } - - /* TODO: not sure if not having realwidth/heigth is bad - a tga/png/jpg - * was requested, after all, so there might be no corresponding wal/pcx? - * if (realwidth == 0 || realheight == 0) return NULL; - */ - - if(LoadSTB(name, ext, &pic, &width, &height)) - { - image = GL3_LoadPic(name, pic, - width, realwidth, - height, realheight, - width * height, type, 32); - } else { - return NULL; - } - } - else - { - return NULL; - } - - if (pic) - { - free(pic); + R_Printf(PRINT_ALL, "%s: can't load %s\n", __func__, name); + image = gl3_notexture; } return image; diff --git a/src/client/refresh/ref_shared.h b/src/client/refresh/ref_shared.h index b787eb85..a88f481e 100644 --- a/src/client/refresh/ref_shared.h +++ b/src/client/refresh/ref_shared.h @@ -83,8 +83,8 @@ typedef struct image_s* (*loadimage_t)(const char *name, byte *pic, int width, i extern struct image_s* LoadWal(const char *origname, imagetype_t type, loadimage_t load_image); extern struct image_s* LoadM8(const char *origname, imagetype_t type, loadimage_t load_image); extern struct image_s* LoadM32(const char *origname, imagetype_t type, loadimage_t load_image); -extern struct image_s* LoadHiColorImage(char *name, const char* namewe, const char *ext, - imagetype_t type, loadimage_t load_image); +extern struct image_s* LoadImage(const char *name, const char* namewe, const char *ext, imagetype_t type, + qboolean r_retexturing, loadimage_t load_image); 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); diff --git a/src/client/refresh/soft/sw_image.c b/src/client/refresh/soft/sw_image.c index 8dbd7160..5ee070ed 100644 --- a/src/client/refresh/soft/sw_image.c +++ b/src/client/refresh/soft/sw_image.c @@ -317,7 +317,19 @@ static image_t * R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int realheight, size_t data_size, imagetype_t type, int bits) { - if (bits == 32 && data_size > 0) + if (!realwidth || !realheight) + { + realwidth = width; + realheight = height; + } + + if (data_size <= 0 || !width || !height) + { + return NULL; + } + + /* Code used with HIColor calls */ + if (bits == 32) { image_t *image; byte *pic8; @@ -364,7 +376,6 @@ R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int real // resize image pic32 = malloc(uploadwidth * uploadheight * 4); - printf("%s: %dx%d -> %dx%d\n", name, width, height, uploadwidth, uploadheight); if (ResizeSTB(pic, width, height, pic32, uploadwidth, uploadheight)) { @@ -389,8 +400,36 @@ R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int real return image; } else + /* used with WAL and 8bit textures */ { - return R_LoadPic8 (name, pic, width, realwidth, height, realheight, data_size, type); + if (r_scale8bittextures->value && type == it_pic) + { + byte *scaled = NULL; + image_t *image; + + scaled = malloc(width * height * 4); + if (!scaled) + return NULL; + + scale2x(pic, scaled, width, height); + width *= 2; + height *= 2; + + image = R_LoadPic8(name, scaled, + width, realwidth, + height, realheight, + width * height, type); + free(scaled); + + return image; + } + else + { + return R_LoadPic8 (name, pic, + width, realwidth, + height, realheight, + data_size, type); + } } } @@ -435,84 +474,6 @@ R_ApplyLight(pixel_t pix, const light3_t light) return d_16to8table[i_c & 0xFFFF]; } -static image_t * -R_LoadImage(char *name, const char* namewe, const char *ext, imagetype_t type) -{ - image_t *image = NULL; - - // with retexturing and not skin - if (r_retexturing->value) - { - image = (image_t *)LoadHiColorImage(name, namewe, ext, type, - (loadimage_t)R_LoadPic); - } - - if (!image) - { - if (strcmp(ext, "pcx") == 0) - { - byte *pic = NULL; - byte *palette = NULL; - int width = 0, height = 0; - - LoadPCX (name, &pic, &palette, &width, &height); - if (!pic) - return NULL; - - if (r_scale8bittextures->value && type == it_pic) - { - byte *scaled = NULL; - int realwidth, realheight; - - // save original size - realwidth = width; - realheight = height; - - scaled = malloc(width * height * 4); - if (!scaled) - return NULL; - - scale2x(pic, scaled, width, height); - width *= 2; - height *= 2; - image = R_LoadPic(name, scaled, - width, realwidth, - height, realheight, - width * height, type, 8); - free(scaled); - } - else - { - image = R_LoadPic(name, pic, - width, width, - height, height, - width * height, type, 8); - } - - if (palette) - { - free(palette); - } - free(pic); - } - else if (strcmp(ext, "wal") == 0) - { - image = (image_t *)LoadWal(namewe, type, (loadimage_t)R_LoadPic); - } - else if (strcmp(ext, "m8") == 0) - { - image = (image_t *)LoadM8(namewe, type, (loadimage_t)R_LoadPic); - } - } - - if (!image) - { - image = r_notexture_mip; - } - - return image; -} - /* =============== R_FindImage @@ -577,7 +538,16 @@ R_FindImage(char *name, imagetype_t type) // // load the pic from disk // - return R_LoadImage(name, namewe, ext, type); + image = (image_t *)LoadImage(name, namewe, ext, type, + r_retexturing->value, (loadimage_t)R_LoadPic); + + if (!image) + { + R_Printf(PRINT_ALL, "%s: can't load %s\n", __func__, name); + image = r_notexture_mip; + } + + return image; } /*