Image: Share code for add file extension

This commit is contained in:
Denis Pauk 2022-10-12 22:04:38 +03:00
parent 768139ad6a
commit ec83adcba5
7 changed files with 47 additions and 62 deletions

View File

@ -117,7 +117,7 @@ fixQuitScreen(byte* px)
}
void
LoadPCX(char *origname, byte **pic, byte **palette, int *width, int *height)
LoadPCX(const char *origname, byte **pic, byte **palette, int *width, int *height)
{
byte *raw;
pcx_t *pcx;
@ -129,13 +129,7 @@ LoadPCX(char *origname, byte **pic, byte **palette, int *width, int *height)
byte *out, *pix;
char filename[256];
Q_strlcpy(filename, origname, sizeof(filename));
/* Add the extension */
if (strcmp(COM_FileExtension(filename), "pcx"))
{
Q_strlcat(filename, ".pcx", sizeof(filename));
}
FixFileExt(origname, "pcx", filename, sizeof(filename));
*pic = NULL;
@ -276,7 +270,7 @@ LoadPCX(char *origname, byte **pic, byte **palette, int *width, int *height)
*pic = NULL;
}
else if(pcx_width == 319 && pcx_height == 239
&& Q_strcasecmp(origname, "pics/quit.pcx") == 0
&& Q_strcasecmp(filename, "pics/quit.pcx") == 0
&& Com_BlockChecksum(pcx, len) == 3329419434u)
{
// it's the quit screen, and the baseq2 one (identified by checksum)
@ -293,10 +287,13 @@ LoadPCX(char *origname, byte **pic, byte **palette, int *width, int *height)
}
void
GetPCXInfo(char *filename, int *width, int *height)
GetPCXInfo(const char *origname, int *width, int *height)
{
pcx_t *pcx;
byte *raw;
char filename[256];
FixFileExt(origname, "pcx", filename, sizeof(filename));
ri.FS_LoadFile(filename, (void **)&raw);

View File

@ -47,6 +47,22 @@
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"
/*
* Add extension to file name
*/
void
FixFileExt(const char *origname, const char *ext, char *filename, size_t size)
{
Q_strlcpy(filename, origname, size);
/* Add the extension */
if (strcmp(COM_FileExtension(filename), ext))
{
Q_strlcat(filename, ".", size);
Q_strlcat(filename, ext, size);
}
}
/*
* origname: the filename to be opened, might be without extension
* type: extension of the type we wanna open ("jpg", "png" or "tga")
@ -57,14 +73,7 @@ LoadSTB(const char *origname, const char* type, byte **pic, int *width, int *hei
{
char filename[256];
Q_strlcpy(filename, origname, sizeof(filename));
/* Add the extension */
if (strcmp(COM_FileExtension(filename), type) != 0)
{
Q_strlcat(filename, ".", sizeof(filename));
Q_strlcat(filename, type, sizeof(filename));
}
FixFileExt(origname, type, filename, sizeof(filename));
*pic = NULL;

View File

@ -31,12 +31,15 @@
*/
void
GetWalInfo(char *name, int *width, int *height)
GetWalInfo(const char *origname, int *width, int *height)
{
miptex_t *mt;
int size;
char filename[256];
size = ri.FS_LoadFile(name, (void **)&mt);
FixFileExt(origname, "wal", filename, sizeof(filename));
size = ri.FS_LoadFile(filename, (void **)&mt);
if (!mt)
{
@ -58,19 +61,21 @@ GetWalInfo(char *name, int *width, int *height)
}
void
GetM8Info(char *name, int *width, int *height)
GetM8Info(const char *origname, int *width, int *height)
{
m8tex_t *mt;
int size;
char filename[256];
size = ri.FS_LoadFile(name, (void **)&mt);
FixFileExt(origname, "m8", filename, sizeof(filename));
size = ri.FS_LoadFile(filename, (void **)&mt);
if (!mt)
{
return;
}
if (size < sizeof(m8tex_t) || LittleLong (mt->version) != M8_VERSION)
{
ri.FS_FreeFile((void *)mt);

View File

@ -1035,13 +1035,7 @@ LoadWal(char *origname, imagetype_t type)
image_t *image;
char name[256];
Q_strlcpy(name, origname, sizeof(name));
/* Add the extension */
if (strcmp(COM_FileExtension(name), "wal"))
{
Q_strlcat(name, ".wal", sizeof(name));
}
FixFileExt(origname, "wal", name, sizeof(name));
size = ri.FS_LoadFile(name, (void **)&mt);
@ -1078,7 +1072,7 @@ LoadWal(char *origname, imagetype_t type)
}
static image_t *
LoadM8(char *origname, imagetype_t type)
LoadM8(const char *origname, imagetype_t type)
{
m8tex_t *mt;
int width, height, ofs, size;
@ -1086,13 +1080,7 @@ LoadM8(char *origname, imagetype_t type)
char name[256];
unsigned char *image_buffer = NULL;
Q_strlcpy(name, origname, sizeof(name));
/* Add the extension */
if (strcmp(COM_FileExtension(name), "m8"))
{
Q_strlcat(name, ".m8", sizeof(name));
}
FixFileExt(origname, "m8", name, sizeof(name));
size = ri.FS_LoadFile(name, (void **)&mt);

View File

@ -597,20 +597,14 @@ GL3_LoadPic(char *name, byte *pic, int width, int realwidth,
}
static gl3image_t *
LoadWal(char *origname, imagetype_t type)
LoadWal(const char *origname, imagetype_t type)
{
miptex_t *mt;
int width, height, ofs, size;
gl3image_t *image;
char name[256];
Q_strlcpy(name, origname, sizeof(name));
/* Add the extension */
if (strcmp(COM_FileExtension(name), "wal"))
{
Q_strlcat(name, ".wal", sizeof(name));
}
FixFileExt(origname, "wal", name, sizeof(name));
size = ri.FS_LoadFile(name, (void **)&mt);
@ -647,7 +641,7 @@ LoadWal(char *origname, imagetype_t type)
}
static gl3image_t *
LoadM8(char *origname, imagetype_t type)
LoadM8(const char *origname, imagetype_t type)
{
m8tex_t *mt;
int width, height, ofs, size;
@ -655,13 +649,7 @@ LoadM8(char *origname, imagetype_t type)
char name[256];
unsigned char *image_buffer = NULL;
Q_strlcpy(name, origname, sizeof(name));
/* Add the extension */
if (strcmp(COM_FileExtension(name), "m8"))
{
Q_strlcat(name, ".m8", sizeof(name));
}
FixFileExt(origname, "m8", name, sizeof(name));
size = ri.FS_LoadFile(name, (void **)&mt);

View File

@ -78,9 +78,12 @@ typedef enum
extern void R_Printf(int level, const char* msg, ...) PRINTF_ATTR(2, 3);
/* Shared images load */
extern void GetPCXPalette (byte **colormap, unsigned *d_8to24table);
extern void LoadPCX(char *origname, byte **pic, byte **palette, int *width, int *height);
extern void GetPCXInfo(char *filename, int *width, int *height);
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);
extern void GetPCXInfo(const char *origname, int *width, int *height);
extern void GetWalInfo(const char *name, int *width, int *height);
extern void GetM8Info(const char *name, int *width, int *height);
extern qboolean LoadSTB(const char *origname, const char* type, byte **pic, int *width, int *height);
extern qboolean ResizeSTB(const byte *input_pixels, int input_width, int input_height,
@ -89,9 +92,6 @@ extern void SmoothColorImage(unsigned *dst, size_t size, size_t rstep);
extern void scale2x(const byte *src, byte *dst, int width, int height);
extern void scale3x(const byte *src, byte *dst, int width, int height);
extern void GetWalInfo(char *name, int *width, int *height);
extern void GetM8Info(char *name, int *width, int *height);
extern float Mod_RadiusFromBounds(const vec3_t mins, const vec3_t maxs);
extern const byte* Mod_DecompressVis(const byte *in, int row);

View File

@ -585,8 +585,6 @@ void RE_Draw_TileClear (int x, int y, int w, int h, char *name);
void RE_Draw_Fill (int x, int y, int w, int h, int c);
void RE_Draw_FadeScreen (void);
void LoadPCX (char *filename, byte **pic, byte **palette, int *width, int *height);
extern byte d_8to24table[256 * 4];
void R_InitImages(void);
void R_ShutdownImages(void);