From 01d7518ef25649f2eb560668499f7d9934b2e91c Mon Sep 17 00:00:00 2001 From: divverent Date: Mon, 12 Nov 2007 08:21:33 +0000 Subject: [PATCH] added cvar r_fixtrans_auto to automatically call fixtrans on all textures that are loaded git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7685 d7cf8633-e32d-0410-b094-e92efae38249 --- cl_particles.c | 4 ++-- gl_draw.c | 4 ++-- gl_rmain.c | 14 +++++++------- host_cmd.c | 2 ++ image.c | 25 +++++++++++++++++++++---- image.h | 5 +++-- model_brush.c | 2 +- r_lightning.c | 2 +- r_shadow.c | 2 +- r_sky.c | 8 ++++---- 10 files changed, 44 insertions(+), 24 deletions(-) diff --git a/cl_particles.c b/cl_particles.c index 52509ad2..0762614b 100644 --- a/cl_particles.c +++ b/cl_particles.c @@ -1954,7 +1954,7 @@ static void R_InitParticleTexture (void) // we invert it again during the blendfunc to make it work... #ifndef DUMPPARTICLEFONT - particlefonttexture = loadtextureimage(particletexturepool, "particles/particlefont.tga", 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE); + particlefonttexture = loadtextureimage(particletexturepool, "particles/particlefont.tga", 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE, true); if (!particlefonttexture) #endif { @@ -2097,7 +2097,7 @@ static void R_InitParticleTexture (void) } #ifndef DUMPPARTICLEFONT - particletexture[tex_beam].texture = loadtextureimage(particletexturepool, "particles/nexbeam.tga", 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE); + particletexture[tex_beam].texture = loadtextureimage(particletexturepool, "particles/nexbeam.tga", 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE, true); if (!particletexture[tex_beam].texture) #endif { diff --git a/gl_draw.c b/gl_draw.c index 2f01464d..b600d6df 100644 --- a/gl_draw.c +++ b/gl_draw.c @@ -342,11 +342,11 @@ cachepic_t *Draw_CachePic (const char *path, qboolean persistent) flags |= TEXF_CLAMP; // load a high quality image from disk if possible - pic->tex = loadtextureimage(drawtexturepool, path, 0, 0, false, flags | (gl_texturecompression_2d.integer ? TEXF_COMPRESS : 0)); + pic->tex = loadtextureimage(drawtexturepool, path, 0, 0, false, flags | (gl_texturecompression_2d.integer ? TEXF_COMPRESS : 0), true); if (pic->tex == NULL && !strncmp(path, "gfx/", 4)) { // compatibility with older versions which did not require gfx/ prefix - pic->tex = loadtextureimage(drawtexturepool, path + 4, 0, 0, false, flags | (gl_texturecompression_2d.integer ? TEXF_COMPRESS : 0)); + pic->tex = loadtextureimage(drawtexturepool, path + 4, 0, 0, false, flags | (gl_texturecompression_2d.integer ? TEXF_COMPRESS : 0), true); } // if a high quality image was loaded, set the pic's size to match it, just // in case there's no low quality version to get the size from diff --git a/gl_rmain.c b/gl_rmain.c index cbc74a3c..86d9833a 100644 --- a/gl_rmain.c +++ b/gl_rmain.c @@ -1524,7 +1524,7 @@ skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboole if (skinframe && skinframe->base) return skinframe; - basepixels = loadimagepixels(name, complain, 0, 0); + basepixels = loadimagepixels(name, complain, 0, 0, true); if (basepixels == NULL) return NULL; @@ -1569,13 +1569,13 @@ skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboole // _norm is the name used by tenebrae and has been adopted as standard if (loadnormalmap) { - if ((pixels = loadimagepixels(va("%s_norm", skinframe->basename), false, 0, 0)) != NULL) + if ((pixels = loadimagepixels(va("%s_norm", skinframe->basename), false, 0, 0, false)) != NULL) { skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL); Mem_Free(pixels); pixels = NULL; } - else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixels(va("%s_bump", skinframe->basename), false, 0, 0)) != NULL) + else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixels(va("%s_bump", skinframe->basename), false, 0, 0, false)) != NULL) { pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4); Image_HeightmapToNormalmap(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value); @@ -1594,10 +1594,10 @@ skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboole // _luma is supported for tenebrae compatibility // (I think it's a very stupid name, but oh well) // _glow is the preferred name - if (loadglow && ((pixels = loadimagepixels(va("%s_glow", skinframe->basename), false, 0, 0)) != NULL || (pixels = loadimagepixels(va("%s_luma", skinframe->basename), false, 0, 0)) != NULL)) {skinframe->glow = R_LoadTexture2D (r_main_texturepool, va("%s_glow", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_glow.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;} - if (loadgloss && (pixels = loadimagepixels(va("%s_gloss", skinframe->basename), false, 0, 0)) != NULL) {skinframe->gloss = R_LoadTexture2D (r_main_texturepool, va("%s_gloss", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_gloss.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;} - if (loadpantsandshirt && (pixels = loadimagepixels(va("%s_pants", skinframe->basename), false, 0, 0)) != NULL) {skinframe->pants = R_LoadTexture2D (r_main_texturepool, va("%s_pants", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;} - if (loadpantsandshirt && (pixels = loadimagepixels(va("%s_shirt", skinframe->basename), false, 0, 0)) != NULL) {skinframe->shirt = R_LoadTexture2D (r_main_texturepool, va("%s_shirt", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;} + if (loadglow && ((pixels = loadimagepixels(va("%s_glow", skinframe->basename), false, 0, 0, false)) != NULL || (pixels = loadimagepixels(va("%s_luma", skinframe->basename), false, 0, 0, false)) != NULL)) {skinframe->glow = R_LoadTexture2D (r_main_texturepool, va("%s_glow", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_glow.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;} + if (loadgloss && (pixels = loadimagepixels(va("%s_gloss", skinframe->basename), false, 0, 0, false)) != NULL) {skinframe->gloss = R_LoadTexture2D (r_main_texturepool, va("%s_gloss", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_gloss.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;} + if (loadpantsandshirt && (pixels = loadimagepixels(va("%s_pants", skinframe->basename), false, 0, 0, false)) != NULL) {skinframe->pants = R_LoadTexture2D (r_main_texturepool, va("%s_pants", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;} + if (loadpantsandshirt && (pixels = loadimagepixels(va("%s_shirt", skinframe->basename), false, 0, 0, false)) != NULL) {skinframe->shirt = R_LoadTexture2D (r_main_texturepool, va("%s_shirt", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;} if (basepixels) Mem_Free(basepixels); diff --git a/host_cmd.c b/host_cmd.c index 578a2b87..706d24d0 100644 --- a/host_cmd.c +++ b/host_cmd.c @@ -30,6 +30,7 @@ cvar_t rcon_address = {0, "rcon_address", "", "server address to send rcon comma cvar_t team = {CVAR_USERINFO | CVAR_SAVE, "team", "none", "QW team (4 character limit, example: blue)"}; cvar_t skin = {CVAR_USERINFO | CVAR_SAVE, "skin", "", "QW player skin name (example: base)"}; cvar_t noaim = {CVAR_USERINFO | CVAR_SAVE, "noaim", "1", "QW option to disable vertical autoaim"}; +cvar_t r_fixtrans_auto = {0, "r_fixtrans_auto", "0", "automatically fixtrans textures (when set to 2, it also saves the fixed versions to a fixtrans directory)"}; qboolean allowcheats = false; extern qboolean host_shuttingdown; @@ -2498,6 +2499,7 @@ void Host_InitCommands (void) Cmd_AddCommand ("pingplreport", Host_PingPLReport_f, "command sent by server containing client ping and packet loss values for scoreboard, triggered by pings command from client (not used by QW servers)"); Cmd_AddCommand ("fixtrans", Image_FixTransparentPixels_f, "change alpha-zero pixels in an image file to sensible values, and write out a new TGA (warning: SLOW)"); + Cvar_RegisterVariable (&r_fixtrans_auto); Cvar_RegisterVariable (&team); Cvar_RegisterVariable (&skin); diff --git a/image.c b/image.c index 7539acdd..dd2d1591 100644 --- a/image.c +++ b/image.c @@ -836,7 +836,8 @@ imageformat_t imageformats_other[] = {NULL, NULL} }; -unsigned char *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight) +int fixtransparentpixels(unsigned char *data, int w, int h); +unsigned char *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight, qboolean allowFixtrans) { fs_offset_t filesize; imageformat_t *firstformat, *format; @@ -884,6 +885,22 @@ unsigned char *loadimagepixels (const char *filename, qboolean complain, int mat Con_Printf("loaded image %s (%dx%d)\n", name, image_width, image_height); if (developer_memorydebug.integer) Mem_CheckSentinelsGlobal(); + if(allowFixtrans && r_fixtrans_auto.integer) + { + int n = fixtransparentpixels(data, image_width, image_height); + if(n) + { + Con_Printf("- had to fix %s (%d pixels changed)\n", name, n); + if(r_fixtrans_auto.integer >= 2) + { + char outfilename[MAX_QPATH], buf[MAX_QPATH]; + Image_StripImageExtension(name, buf, sizeof(buf)); + dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf); + Image_WriteTGARGBA(outfilename, image_width, image_height, data); + Con_Printf("- %s written.\n", outfilename); + } + } + } return data; } else @@ -907,11 +924,11 @@ unsigned char *loadimagepixels (const char *filename, qboolean complain, int mat return NULL; } -rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags) +rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, qboolean allowFixtrans) { unsigned char *data; rtexture_t *rt; - if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight))) + if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight, allowFixtrans))) return 0; rt = R_LoadTexture2D(pool, filename, image_width, image_height, data, TEXTYPE_RGBA, flags, NULL); Mem_Free(data); @@ -1059,7 +1076,7 @@ void Image_FixTransparentPixels_f(void) Con_Printf("Processing %s... ", filename); Image_StripImageExtension(filename, buf, sizeof(buf)); dpsnprintf(outfilename, sizeof(outfilename), "fixtrans/%s.tga", buf); - if(!(data = loadimagepixels(filename, true, 0, 0))) + if(!(data = loadimagepixels(filename, true, 0, 0, false))) return; if((n = fixtransparentpixels(data, image_width, image_height))) { diff --git a/image.h b/image.h index 3a0052e8..47f5f4cf 100644 --- a/image.h +++ b/image.h @@ -23,10 +23,10 @@ void Image_StripImageExtension (const char *in, char *out, size_t size_out); unsigned char *LoadTGA (const unsigned char *f, int filesize, int matchwidth, int matchheight); // loads a texture, as pixel data -unsigned char *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight); +unsigned char *loadimagepixels (const char *filename, qboolean complain, int matchwidth, int matchheight, qboolean allowFixtrans); // loads a texture, as a texture -rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags); +rtexture_t *loadtextureimage (rtexturepool_t *pool, const char *filename, int matchwidth, int matchheight, qboolean complain, int flags, qboolean allowFixtrans); // writes a RGB TGA that is already upside down (which TGA wants) qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const unsigned char *data, unsigned char *buffer); @@ -47,6 +47,7 @@ void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *ou // console command to fix the colors of transparent pixels (to prevent weird borders) void Image_FixTransparentPixels_f(void); +extern cvar_t r_fixtrans_auto; #endif diff --git a/model_brush.c b/model_brush.c index 0b1293ae..ebcd7bf1 100644 --- a/model_brush.c +++ b/model_brush.c @@ -1510,7 +1510,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l) { if (loadmodel->isworldmodel) { - data = loadimagepixels(tx->name, false, 0, 0); + data = loadimagepixels(tx->name, false, 0, 0, false); if (data) { R_Q1BSP_LoadSplitSky(data, image_width, image_height, 4); diff --git a/r_lightning.c b/r_lightning.c index c8ff41f4..4438ba77 100644 --- a/r_lightning.c +++ b/r_lightning.c @@ -25,7 +25,7 @@ void r_lightningbeams_start(void) void r_lightningbeams_setupqmbtexture(void) { - r_lightningbeamqmbtexture = loadtextureimage(r_lightningbeamtexturepool, "textures/particles/lightning.pcx", 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE); + r_lightningbeamqmbtexture = loadtextureimage(r_lightningbeamtexturepool, "textures/particles/lightning.pcx", 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE, false); if (r_lightningbeamqmbtexture == NULL) Cvar_SetValueQuick(&r_lightningbeam_qmbtexture, false); } diff --git a/r_shadow.c b/r_shadow.c index 01f23c28..4230cbf5 100644 --- a/r_shadow.c +++ b/r_shadow.c @@ -3215,7 +3215,7 @@ rtexture_t *R_Shadow_LoadCubemap(const char *basename) // generate an image name based on the base and and suffix dpsnprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix); // load it - if ((image_rgba = loadimagepixels(name, false, cubemapsize, cubemapsize))) + if ((image_rgba = loadimagepixels(name, false, cubemapsize, cubemapsize, false))) { // an image loaded, make sure width and height are equal if (image_width == image_height) diff --git a/r_sky.c b/r_sky.c index 340be966..a33b5346 100644 --- a/r_sky.c +++ b/r_sky.c @@ -102,13 +102,13 @@ int R_LoadSkyBox(void) success = 0; for (i=0; i<6; i++) { - if (dpsnprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0))) + if (dpsnprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0, false))) { - if (dpsnprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0))) + if (dpsnprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0, false))) { - if (dpsnprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0))) + if (dpsnprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0, false))) { - if (dpsnprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0))) + if (dpsnprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0, false))) continue; } } -- 2.39.2