From 50de5e091a2b203c1b1d157dfb3ccb9ae8e7117b Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Wed, 3 Jul 2024 23:53:16 +0300 Subject: [PATCH] renders: move m32 load to vid/image code --- src/client/refresh/files/stb.c | 5 +--- src/client/refresh/files/wal.c | 53 --------------------------------- src/client/refresh/ref_shared.h | 2 -- src/client/vid/image.c | 52 ++++++++++++++++++++++++++++++-- 4 files changed, 51 insertions(+), 61 deletions(-) diff --git a/src/client/refresh/files/stb.c b/src/client/refresh/files/stb.c index ea5a148b..cdf951bb 100644 --- a/src/client/refresh/files/stb.c +++ b/src/client/refresh/files/stb.c @@ -585,11 +585,8 @@ LoadImage_Ext(const char *name, const char* namewe, const char *ext, imagetype_t { image = LoadM8(name, namewe, type, load_image); } - else if (!strcmp(ext, "m32")) - { - image = LoadM32(name, namewe, type, load_image); - } else if (!strcmp(ext, "tga") || + !strcmp(ext, "m32") || !strcmp(ext, "png") || !strcmp(ext, "jpg")) { diff --git a/src/client/refresh/files/wal.c b/src/client/refresh/files/wal.c index dfbcee4a..d5773b4c 100644 --- a/src/client/refresh/files/wal.c +++ b/src/client/refresh/files/wal.c @@ -230,59 +230,6 @@ LoadSWL(const char *origname, byte **pic, byte **palette, int *width, int *heigh } } -struct image_s * -LoadM32(const char *origname, const char *namewe, imagetype_t type, - loadimage_t load_image) -{ - m32tex_t *mt; - int width, height, ofs, size; - struct image_s *image; - char name[256]; - - FixFileExt(namewe, "m32", name, sizeof(name)); - - size = ri.FS_LoadFile(name, (void **)&mt); - - if (!mt) - { - return NULL; - } - - if (size < sizeof(m32tex_t)) - { - R_Printf(PRINT_ALL, "%s: can't load %s, small header\n", __func__, name); - ri.FS_FreeFile((void *)mt); - return NULL; - } - - if (LittleLong (mt->version) != M32_VERSION) - { - R_Printf(PRINT_ALL, "%s: can't load %s, wrong magic value.\n", __func__, name); - ri.FS_FreeFile ((void *)mt); - return NULL; - } - - width = LittleLong (mt->width[0]); - height = LittleLong (mt->height[0]); - ofs = LittleLong (mt->offsets[0]); - - if ((ofs <= 0) || (width <= 0) || (height <= 0) || - (((size - ofs) / height) < (width * 4))) - { - R_Printf(PRINT_ALL, "%s: can't load %s, small body\n", __func__, name); - ri.FS_FreeFile((void *)mt); - return NULL; - } - - image = load_image(origname, (byte *)mt + ofs, - width, 0, - height, 0, - (size - ofs) / 4, type, 32); - ri.FS_FreeFile ((void *)mt); - - return image; -} - void GetWalInfo(const char *origname, int *width, int *height) { diff --git a/src/client/refresh/ref_shared.h b/src/client/refresh/ref_shared.h index bd2b23ef..74c68885 100644 --- a/src/client/refresh/ref_shared.h +++ b/src/client/refresh/ref_shared.h @@ -100,8 +100,6 @@ extern struct image_s* LoadWal(const char *origname, const char *namewe, imagety loadimage_t load_image); extern struct image_s* LoadM8(const char *origname, const char *namewe, imagetype_t type, loadimage_t load_image); -extern struct image_s* LoadM32(const char *origname, const char *namewe, imagetype_t type, - loadimage_t load_image); extern void LoadSWL(const char *origname, byte **pic, byte **palette, int *width, int *height); extern void FixFileExt(const char *origname, const char *ext, char *filename, size_t size); extern void LoadPCX(const char *origname, byte **pic, byte **palette, int *width, int *height); diff --git a/src/client/vid/image.c b/src/client/vid/image.c index ef05b3b4..d4ad5fa9 100644 --- a/src/client/vid/image.c +++ b/src/client/vid/image.c @@ -271,6 +271,45 @@ SWL_Decode(const byte *raw, int len, byte **pic, byte **palette, } } +static void +M32_Decode(const byte *raw, int len, byte **pic, int *width, int *height) +{ + m32tex_t *mt; + int ofs; + + mt = (m32tex_t *)raw; + + if (!mt) + { + return; + } + + if (len < sizeof(m32tex_t)) + { + Com_DPrintf("%s: can't load, small header\n", __func__); + return; + } + + if (LittleLong (mt->version) != M32_VERSION) + { + Com_DPrintf("%s: can't load, wrong magic value.\n", __func__); + return; + } + + *width = LittleLong (mt->width[0]); + *height = LittleLong (mt->height[0]); + ofs = LittleLong (mt->offsets[0]); + + if ((ofs <= 0) || (*width <= 0) || (*height <= 0) || + (((len - ofs) / *height) < (*width * 4))) + { + Com_DPrintf("%s: can't load, small body\n", __func__); + } + + *pic = malloc (len - ofs); + memcpy(*pic, (byte *)mt + ofs, len - ofs); +} + void VID_ImageDecode(const char *filename, byte **pic, byte **palette, int *width, int *height, int *bytesPerPixel) @@ -322,14 +361,23 @@ VID_ImageDecode(const char *filename, byte **pic, byte **palette, else { int sourceBytesPerPixel = 0; + /* other formats does not have palette directly */ if (palette) { *palette = NULL; } - *pic = stbi_load_from_memory(raw, len, width, height, - &sourceBytesPerPixel, STBI_rgb_alpha); + if (!strcmp(ext, "m32")) + { + M32_Decode(raw, len, pic, width, height); + printf("->%s:%p\n", filename, *pic); + } + else + { + *pic = stbi_load_from_memory(raw, len, width, height, + &sourceBytesPerPixel, STBI_rgb_alpha); + } if (*pic == NULL) {