Image: Share M8

This commit is contained in:
Denis Pauk 2022-10-13 08:42:12 +03:00
parent 11f5b9e475
commit f77e6940bd
5 changed files with 135 additions and 296 deletions

View file

@ -26,8 +26,8 @@
#include "../ref_shared.h"
struct image_s*
LoadWal(const char *origname, imagetype_t type, load_image_t loadImage)
struct image_s *
LoadWal(const char *origname, imagetype_t type, loadimage_t load_image)
{
int width, height, ofs, size;
struct image_s *image;
@ -63,7 +63,7 @@ LoadWal(const char *origname, imagetype_t type, load_image_t loadImage)
return NULL;
}
image = loadImage(name, (byte *)mt + ofs,
image = load_image(name, (byte *)mt + ofs,
width, 0,
height, 0,
(size - ofs), type, 8);
@ -73,6 +73,72 @@ LoadWal(const char *origname, imagetype_t type, load_image_t loadImage)
return image;
}
struct image_s *
LoadM8(const char *origname, imagetype_t type, loadimage_t load_image)
{
m8tex_t *mt;
int width, height, ofs, size, i;
struct image_s *image;
char name[256];
unsigned char *image_buffer = NULL;
FixFileExt(origname, "m8", 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(m8tex_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) != 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(name, 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)
{

View file

@ -1027,72 +1027,6 @@ R_LoadPic(char *name, byte *pic, int width, int realwidth,
return image;
}
static image_t *
LoadM8(const char *origname, imagetype_t type)
{
m8tex_t *mt;
int width, height, ofs, size;
image_t *image;
char name[256];
unsigned char *image_buffer = NULL;
FixFileExt(origname, "m8", 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 r_notexture;
}
if (size < sizeof(m8tex_t))
{
R_Printf(PRINT_ALL, "%s: can't load %s, small header\n", __func__, name);
ri.FS_FreeFile((void *)mt);
return r_notexture;
}
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 r_notexture;
}
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 r_notexture;
}
image_buffer = malloc (width * height * 4);
for(int i=0; i<width * height; 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 = R_LoadPic(name, image_buffer,
width, 0,
height, 0,
width * height, type, 32);
free(image_buffer);
ri.FS_FreeFile((void *)mt);
return image;
}
/*
* Finds or loads the given image
*/
@ -1238,12 +1172,12 @@ R_FindImage(char *name, imagetype_t type)
}
else if (strcmp(ext, "m8") == 0)
{
image = LoadM8(namewe, type);
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, (load_image_t)R_LoadPic);
image = (image_t *)LoadWal(namewe, type, (loadimage_t)R_LoadPic);
}
if (!image)
@ -1254,7 +1188,7 @@ R_FindImage(char *name, imagetype_t type)
}
else if (strcmp(ext, "m8") == 0)
{
image = LoadM8(name, type);
image = (image_t *)LoadM8(name, type, (loadimage_t)R_LoadPic);
if (!image)
{
@ -1264,7 +1198,7 @@ R_FindImage(char *name, imagetype_t type)
}
else /* gl_retexture is not set */
{
image = (image_t *)LoadWal(name, type, (load_image_t)R_LoadPic);
image = (image_t *)LoadWal(name, type, (loadimage_t)R_LoadPic);
if (!image)
{

View file

@ -597,71 +597,6 @@ GL3_LoadPic(char *name, byte *pic, int width, int realwidth,
return image;
}
static gl3image_t *
LoadM8(const char *origname, imagetype_t type)
{
m8tex_t *mt;
int width, height, ofs, size;
gl3image_t *image;
char name[256];
unsigned char *image_buffer = NULL;
FixFileExt(origname, "m8", 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 gl3_notexture;
}
if (size < sizeof(m8tex_t))
{
R_Printf(PRINT_ALL, "%s: can't load %s, small header\n", __func__, name);
ri.FS_FreeFile((void *)mt);
return gl3_notexture;
}
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 gl3_notexture;
}
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 gl3_notexture;
}
image_buffer = malloc (width * height * 4);
for(int i=0; i<width * height; 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 = GL3_LoadPic(name, image_buffer,
width, 0, height, 0,
width * height, type, 32);
free(image_buffer);
ri.FS_FreeFile((void *)mt);
return image;
}
/*
* Finds or loads the given image
*/
@ -807,12 +742,12 @@ GL3_FindImage(char *name, imagetype_t type)
}
else if (strcmp(ext, "m8") == 0)
{
image = LoadM8(namewe, type);
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, (load_image_t)GL3_LoadPic);
image = (gl3image_t *)LoadWal(namewe, type, (loadimage_t)GL3_LoadPic);
}
if (!image)
@ -823,7 +758,7 @@ GL3_FindImage(char *name, imagetype_t type)
}
else if (strcmp(ext, "m8") == 0)
{
image = LoadM8(name, type);
image = (gl3image_t *)LoadM8(name, type, (loadimage_t)GL3_LoadPic);
if (!image)
{
@ -833,7 +768,7 @@ GL3_FindImage(char *name, imagetype_t type)
}
else /* gl_retexture is not set */
{
image = (gl3image_t *)LoadWal(name, type, (load_image_t)GL3_LoadPic);
image = (gl3image_t *)LoadWal(name, type, (loadimage_t)GL3_LoadPic);
if (!image)
{

View file

@ -78,9 +78,10 @@ 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,
typedef struct image_s* (*loadimage_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 struct image_s* LoadWal(const char *origname, imagetype_t type, loadimage_t load_image);
struct image_s* LoadM8(const char *origname, imagetype_t type, 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);

View file

@ -21,7 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "header/local.h"
#define MAX_RIMAGES 1024
static image_t *r_whitetexture_mip = NULL;
static image_t *r_whitetexture_mip = NULL;
static image_t r_images[MAX_RIMAGES];
static int numr_images;
static int image_max = 0;
@ -204,6 +204,40 @@ Get_BestImageSize(const image_t *image, int *req_width, int *req_height)
}
}
static byte *d_16to8table = NULL; // 16 to 8 bit conversion table
static void
R_Convert32To8bit(const unsigned char* pic_in, pixel_t* pic_out, size_t size,
qboolean transparent)
{
size_t i;
if (!d_16to8table)
return;
for(i=0; i < size; i++)
{
if (pic_in[3] > 128 || !transparent)
{
unsigned int r, g, b, c;
r = ( pic_in[0] >> 3 ) & 31;
g = ( pic_in[1] >> 2 ) & 63;
b = ( pic_in[2] >> 3 ) & 31;
c = r | ( g << 5 ) | ( b << 11 );
pic_out[i] = d_16to8table[c & 0xFFFF];
}
else
{
pic_out[i] = TRANSPARENT_COLOR;
}
pic_in += 4;
}
}
/*
================
R_LoadPic
@ -244,14 +278,29 @@ R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int real
return NULL;
}
// some file types can have more data in file than code needs
if (data_size > full_size)
{
data_size = full_size;
}
image->transparent = false;
if (bits == 32)
{
R_Convert32To8bit(pic, image->pixels[0], data_size, type != it_wall);
}
else
{
memcpy(image->pixels[0], pic, data_size);
}
if (type != it_wall)
{
size_t i;
for (i=0 ; i<size ; i++)
{
if (pic[i] == 255)
if (image->pixels[0][i] == TRANSPARENT_COLOR)
{
image->transparent = true;
break;
@ -259,12 +308,6 @@ R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int real
}
}
if (data_size > full_size)
{
data_size = full_size;
}
memcpy(image->pixels[0], pic, data_size);
// restore mips
R_RestoreImagePointers(image, 0);
@ -277,8 +320,6 @@ R_LoadPic (char *name, byte *pic, int width, int realwidth, int height, int real
return image;
}
static byte *d_16to8table = NULL; // 16 to 8 bit conversion table
/*
* Apply color light to texture pixel
*
@ -320,144 +361,6 @@ R_ApplyLight(pixel_t pix, const light3_t light)
return d_16to8table[i_c & 0xFFFF];
}
static void
R_Convert32To8bit(const unsigned char* pic_in, pixel_t* pic_out, size_t size)
{
size_t i;
if (!d_16to8table)
return;
for(i=0; i < size; i++)
{
unsigned int r, g, b, c;
r = ( pic_in[0] >> 3 ) & 31;
g = ( pic_in[1] >> 2 ) & 63;
b = ( pic_in[2] >> 3 ) & 31;
c = r | ( g << 5 ) | ( b << 11 );
pic_out[i] = d_16to8table[c & 0xFFFF];
pic_in += 4;
}
}
static void
R_FixPalette(pixel_t* pixels, size_t size, const rgb_t* pallette)
{
pixel_t* convert = malloc(256);
size_t i;
if (!d_16to8table)
{
free(convert);
return;
}
for(i=0; i < 256; i ++)
{
unsigned int r, g, b, c;
r = ( pallette[i].r >> 3 ) & 31;
g = ( pallette[i].g >> 2 ) & 63;
b = ( pallette[i].b >> 3 ) & 31;
c = r | ( g << 5 ) | ( b << 11 );
convert[i] = d_16to8table[c & 0xFFFF];
}
for(i=0; i < size; i++)
{
pixels[i] = convert[pixels[i]];
}
free(convert);
}
/*
================
R_LoadM8
================
*/
static image_t *
R_LoadM8 (char *name, imagetype_t type)
{
m8tex_t *mt;
int ofs, file_size;
image_t *image;
int size;
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(m8tex_t))
{
R_Printf(PRINT_ALL, "%s: can't load %s, small header\n", __func__, name);
ri.FS_FreeFile ((void *)mt);
return r_notexture_mip;
}
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 r_notexture_mip;
}
image = R_FindFreeImage ();
strcpy (image->name, name);
image->width = LittleLong (mt->width[0]);
image->height = LittleLong (mt->height[0]);
image->asset_width = image->width;
image->asset_height = image->height;
image->type = type;
image->registration_sequence = registration_sequence;
ofs = LittleLong (mt->offsets[0]);
size = image->width * image->height * (256+64+16+4)/256;
if ((ofs <= 0) || (image->width <= 0) || (image->height <= 0) ||
((file_size - ofs) / image->width < image->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->pixels[0] = malloc (size);
image->pixels[1] = image->pixels[0] + image->width*image->height;
image->pixels[2] = image->pixels[1] + image->width*image->height/4;
image->pixels[3] = image->pixels[2] + image->width*image->height/16;
if (size > (file_size - ofs))
{
memcpy(image->pixels[0], (byte *)mt + ofs, file_size - ofs);
// looks short, restore everything from first image
R_ImageShrink(image->pixels[0], image->pixels[1],
image->height, image->height/2,
image->width, image->width/2);
R_ImageShrink(image->pixels[1], image->pixels[2],
image->height/2, image->height/4,
image->width/2, image->width/4);
R_ImageShrink(image->pixels[2], image->pixels[3],
image->height/4, image->height/8,
image->width/4, image->width/8);
}
else
{
memcpy ( image->pixels[0], (byte *)mt + ofs, size);
}
R_FixPalette(image->pixels[0], size, mt->palette);
ri.FS_FreeFile ((void *)mt);
return image;
}
static image_t *
R_LoadHiColorImage(char *name, const char* namewe, const char *ext, imagetype_t type)
{
@ -550,7 +453,7 @@ R_LoadHiColorImage(char *name, const char* namewe, const char *ext, imagetype_t
if (ResizeSTB(pic, width, height,
pic32, uploadwidth, uploadheight))
{
R_Convert32To8bit(pic32, pic8, uploadwidth * uploadheight);
R_Convert32To8bit(pic32, pic8, uploadwidth * uploadheight, type != it_wall);
image = R_LoadPic(name, pic8,
uploadwidth, realwidth,
uploadheight, realheight,
@ -560,7 +463,7 @@ R_LoadHiColorImage(char *name, const char* namewe, const char *ext, imagetype_t
}
else
{
R_Convert32To8bit(pic, pic8, width * height);
R_Convert32To8bit(pic, pic8, width * height, type != it_wall);
image = R_LoadPic(name, pic8,
width, width,
height, height,
@ -639,11 +542,11 @@ R_LoadImage(char *name, const char* namewe, const char *ext, imagetype_t type)
}
else if (strcmp(ext, "wal") == 0)
{
image = (image_t *)LoadWal(namewe, type, (load_image_t)R_LoadPic);
image = (image_t *)LoadWal(namewe, type, (loadimage_t)R_LoadPic);
}
else if (strcmp(ext, "m8") == 0)
{
image = R_LoadM8 (name, type);
image = (image_t *)LoadM8(namewe, type, (loadimage_t)R_LoadPic);
}
}