From 6de0e06c4a0208880245b933fa58c358ddca03cf Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Sun, 7 Jul 2024 14:53:23 +0300 Subject: [PATCH] images: move m8 decode to vid/images --- src/client/refresh/files/stb.c | 8 ++-- src/client/refresh/files/wal.c | 66 --------------------------------- src/client/refresh/ref_shared.h | 2 - src/client/vid/image.c | 55 ++++++++++++++++++++++++++- 4 files changed, 57 insertions(+), 74 deletions(-) diff --git a/src/client/refresh/files/stb.c b/src/client/refresh/files/stb.c index 502b9e86..ae44ad9c 100644 --- a/src/client/refresh/files/stb.c +++ b/src/client/refresh/files/stb.c @@ -500,7 +500,9 @@ LoadImage_Ext(const char *name, const char* namewe, const char *ext, imagetype_t if (!image) { - if (!strcmp(ext, "pcx") || !strcmp(ext, "swl")) + if (!strcmp(ext, "pcx") || + !strcmp(ext, "m8") || + !strcmp(ext, "swl")) { int width = 0, height = 0, realwidth = 0, realheight = 0; byte *pic = NULL; @@ -581,10 +583,6 @@ LoadImage_Ext(const char *name, const char* namewe, const char *ext, imagetype_t { image = LoadWal(name, namewe, type, load_image); } - else if (!strcmp(ext, "m8")) - { - image = LoadM8(name, namewe, type, load_image); - } else if (!strcmp(ext, "tga") || !strcmp(ext, "m32") || !strcmp(ext, "png") || diff --git a/src/client/refresh/files/wal.c b/src/client/refresh/files/wal.c index b47065d1..6095da2e 100644 --- a/src/client/refresh/files/wal.c +++ b/src/client/refresh/files/wal.c @@ -143,72 +143,6 @@ LoadWal(const char *origname, const char *namewe, imagetype_t type, return image; } -struct image_s * -LoadM8(const char *origname, const char *namewe, imagetype_t type, - loadimage_t load_image) -{ - int width, height, ofs, size, i; - byte *image_buffer = NULL; - struct image_s *image; - char name[256]; - m8tex_t *mt; - - FixFileExt(namewe, "m8", name, sizeof(name)); - - size = ri.FS_LoadFile(name, (void **)&mt); - - if (!mt) - { - return NULL; - } - - if (size < sizeof(*mt)) - { - 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) != M8_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)) - { - R_Printf(PRINT_ALL, "%s: can't load %s, small body\n", __func__, name); - ri.FS_FreeFile((void *)mt); - return NULL; - } - - image_buffer = malloc ((size - ofs) * 4); - for(i=0; i<(size - ofs); i++) - { - unsigned char value = *((byte *)mt + ofs + i); - image_buffer[i * 4 + 0] = mt->palette[value].r; - image_buffer[i * 4 + 1] = mt->palette[value].g; - image_buffer[i * 4 + 2] = mt->palette[value].b; - image_buffer[i * 4 + 3] = value == 255 ? 0 : 255; - } - - image = load_image(origname, image_buffer, - width, 0, - height, 0, - (size - ofs), type, 32); - free(image_buffer); - - 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 946bdc42..e46e515a 100644 --- a/src/client/refresh/ref_shared.h +++ b/src/client/refresh/ref_shared.h @@ -98,8 +98,6 @@ typedef struct image_s* (*loadimage_t)(const char *name, byte *pic, int width, i int height, int realheight, size_t data_size, imagetype_t type, int bits); extern struct image_s* LoadWal(const char *origname, const char *namewe, imagetype_t type, loadimage_t load_image); -extern struct image_s* LoadM8(const char *origname, const char *namewe, imagetype_t type, - loadimage_t load_image); extern void FixFileExt(const char *origname, const char *ext, char *filename, size_t size); extern void GetPCXInfo(const char *origname, int *width, int *height); extern void GetWalInfo(const char *name, int *width, int *height); diff --git a/src/client/vid/image.c b/src/client/vid/image.c index a9baca11..62ecaf91 100644 --- a/src/client/vid/image.c +++ b/src/client/vid/image.c @@ -294,7 +294,7 @@ SWL_Decode(const char *name, const byte *raw, int len, byte **pic, byte **palett int *width, int *height) { sinmiptex_t *mt; - int ofs, i; + int ofs; mt = (sinmiptex_t *)raw; @@ -327,6 +327,8 @@ SWL_Decode(const char *name, const byte *raw, int len, byte **pic, byte **palett if (palette) { + int i; + *palette = malloc(768); for (i = 0; i < 256; i ++) { @@ -376,6 +378,52 @@ M32_Decode(const char *name, const byte *raw, int len, byte **pic, int *width, i memcpy(*pic, (byte *)mt + ofs, len - ofs); } +static void +M8_Decode(const char *name, const byte *raw, int len, byte **pic, byte **palette, + int *width, int *height) +{ + m8tex_t *mt; + int ofs; + + mt = (m8tex_t *)raw; + + if (!mt) + { + return; + } + + if (len < sizeof(*mt)) + { + Com_Printf("%s: can't load %s, small header\n", __func__, name); + return; + } + + if (LittleLong (mt->version) != M8_VERSION) + { + Com_Printf("%s: can't load %s, wrong magic value.\n", __func__, name); + 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)) + { + Com_Printf("%s: can't load %s, small body\n", __func__, name); + return; + } + + *pic = malloc(len - ofs); + memcpy(*pic, (byte *)mt + ofs, len - ofs); + if (palette) + { + *palette = malloc(768); + memcpy(*palette, mt->palette, 768); + } +} + void VID_ImageDecode(const char *filename, byte **pic, byte **palette, int *width, int *height, int *bytesPerPixel) @@ -419,6 +467,11 @@ VID_ImageDecode(const char *filename, byte **pic, byte **palette, *bytesPerPixel = 1; } + else if (!strcmp(ext, "m8")) + { + M8_Decode(filename, raw, len, pic, palette, width, height); + *bytesPerPixel = 1; + } else if (!strcmp(ext, "swl")) { SWL_Decode(filename, raw, len, pic, palette, width, height);