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

View file

@ -47,6 +47,22 @@
#define STB_IMAGE_RESIZE_IMPLEMENTATION #define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h" #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 * origname: the filename to be opened, might be without extension
* type: extension of the type we wanna open ("jpg", "png" or "tga") * 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]; char filename[256];
Q_strlcpy(filename, origname, sizeof(filename)); FixFileExt(origname, type, filename, sizeof(filename));
/* Add the extension */
if (strcmp(COM_FileExtension(filename), type) != 0)
{
Q_strlcat(filename, ".", sizeof(filename));
Q_strlcat(filename, type, sizeof(filename));
}
*pic = NULL; *pic = NULL;

View file

@ -31,12 +31,15 @@
*/ */
void void
GetWalInfo(char *name, int *width, int *height) GetWalInfo(const char *origname, int *width, int *height)
{ {
miptex_t *mt; miptex_t *mt;
int size; 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) if (!mt)
{ {
@ -58,19 +61,21 @@ GetWalInfo(char *name, int *width, int *height)
} }
void void
GetM8Info(char *name, int *width, int *height) GetM8Info(const char *origname, int *width, int *height)
{ {
m8tex_t *mt; m8tex_t *mt;
int size; 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) if (!mt)
{ {
return; return;
} }
if (size < sizeof(m8tex_t) || LittleLong (mt->version) != M8_VERSION) if (size < sizeof(m8tex_t) || LittleLong (mt->version) != M8_VERSION)
{ {
ri.FS_FreeFile((void *)mt); ri.FS_FreeFile((void *)mt);

View file

@ -1035,13 +1035,7 @@ LoadWal(char *origname, imagetype_t type)
image_t *image; image_t *image;
char name[256]; char name[256];
Q_strlcpy(name, origname, sizeof(name)); FixFileExt(origname, "wal", name, sizeof(name));
/* Add the extension */
if (strcmp(COM_FileExtension(name), "wal"))
{
Q_strlcat(name, ".wal", sizeof(name));
}
size = ri.FS_LoadFile(name, (void **)&mt); size = ri.FS_LoadFile(name, (void **)&mt);
@ -1078,7 +1072,7 @@ LoadWal(char *origname, imagetype_t type)
} }
static image_t * static image_t *
LoadM8(char *origname, imagetype_t type) LoadM8(const char *origname, imagetype_t type)
{ {
m8tex_t *mt; m8tex_t *mt;
int width, height, ofs, size; int width, height, ofs, size;
@ -1086,13 +1080,7 @@ LoadM8(char *origname, imagetype_t type)
char name[256]; char name[256];
unsigned char *image_buffer = NULL; unsigned char *image_buffer = NULL;
Q_strlcpy(name, origname, sizeof(name)); FixFileExt(origname, "m8", name, sizeof(name));
/* Add the extension */
if (strcmp(COM_FileExtension(name), "m8"))
{
Q_strlcat(name, ".m8", sizeof(name));
}
size = ri.FS_LoadFile(name, (void **)&mt); 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 * static gl3image_t *
LoadWal(char *origname, imagetype_t type) LoadWal(const char *origname, imagetype_t type)
{ {
miptex_t *mt; miptex_t *mt;
int width, height, ofs, size; int width, height, ofs, size;
gl3image_t *image; gl3image_t *image;
char name[256]; char name[256];
Q_strlcpy(name, origname, sizeof(name)); FixFileExt(origname, "wal", name, sizeof(name));
/* Add the extension */
if (strcmp(COM_FileExtension(name), "wal"))
{
Q_strlcat(name, ".wal", sizeof(name));
}
size = ri.FS_LoadFile(name, (void **)&mt); size = ri.FS_LoadFile(name, (void **)&mt);
@ -647,7 +641,7 @@ LoadWal(char *origname, imagetype_t type)
} }
static gl3image_t * static gl3image_t *
LoadM8(char *origname, imagetype_t type) LoadM8(const char *origname, imagetype_t type)
{ {
m8tex_t *mt; m8tex_t *mt;
int width, height, ofs, size; int width, height, ofs, size;
@ -655,13 +649,7 @@ LoadM8(char *origname, imagetype_t type)
char name[256]; char name[256];
unsigned char *image_buffer = NULL; unsigned char *image_buffer = NULL;
Q_strlcpy(name, origname, sizeof(name)); FixFileExt(origname, "m8", name, sizeof(name));
/* Add the extension */
if (strcmp(COM_FileExtension(name), "m8"))
{
Q_strlcat(name, ".m8", sizeof(name));
}
size = ri.FS_LoadFile(name, (void **)&mt); 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); extern void R_Printf(int level, const char* msg, ...) PRINTF_ATTR(2, 3);
/* Shared images load */ /* Shared images load */
extern void GetPCXPalette (byte **colormap, unsigned *d_8to24table); extern void FixFileExt(const char *origname, const char *ext, char *filename, size_t size);
extern void LoadPCX(char *origname, byte **pic, byte **palette, int *width, int *height); extern void GetPCXPalette(byte **colormap, unsigned *d_8to24table);
extern void GetPCXInfo(char *filename, int *width, int *height); 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 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, 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 scale2x(const byte *src, byte *dst, int width, int height);
extern void scale3x(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 float Mod_RadiusFromBounds(const vec3_t mins, const vec3_t maxs);
extern const byte* Mod_DecompressVis(const byte *in, int row); 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_Fill (int x, int y, int w, int h, int c);
void RE_Draw_FadeScreen (void); void RE_Draw_FadeScreen (void);
void LoadPCX (char *filename, byte **pic, byte **palette, int *width, int *height);
extern byte d_8to24table[256 * 4]; extern byte d_8to24table[256 * 4];
void R_InitImages(void); void R_InitImages(void);
void R_ShutdownImages(void); void R_ShutdownImages(void);