X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;ds=sidebyside;f=image.c;h=7ef2326b0c5af4ea41f57a7cba31f5588920da49;hb=c77cc6c6dfd662e2baf695cee6d8e906b0764ea5;hp=fb553d07d2a0e61ed233814d7341fa0a1ef0cf00;hpb=ff46d6ff516fda192c5adc55a5c9b82007545bd2;p=xonotic%2Fdarkplaces.git diff --git a/image.c b/image.c index fb553d07..7ef2326b 100644 --- a/image.c +++ b/image.c @@ -7,43 +7,99 @@ int image_width; int image_height; -void Image_CopyMux(qbyte *outpixels, const qbyte *inpixels, int width, int height, int flipx, int flipy, int flipdiagonal, int numincomponents, int numoutcomponents, int *inputcomponentindices) +#if 1 +// written by LordHavoc in a readable way, optimized by Vic, further optimized by LordHavoc (the non-special index case), readable version preserved below this +void Image_CopyMux(qbyte *outpixels, const qbyte *inpixels, int inputwidth, int inputheight, qboolean inputflipx, qboolean inputflipy, qboolean inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices) +{ + int index, c, x, y; + const qbyte *in, *line; + int row_inc = (inputflipy ? -inputwidth : inputwidth) * numinputcomponents, col_inc = (inputflipx ? -1 : 1) * numinputcomponents; + int row_ofs = (inputflipy ? (inputheight - 1) * inputwidth * numinputcomponents : 0), col_ofs = (inputflipx ? (inputwidth - 1) * numinputcomponents : 0); + + for (c = 0; c < numoutputcomponents; c++) + if (outputinputcomponentindices[c] & 0x80000000) + break; + if (c < numoutputcomponents) + { + // special indices used + if (inputflipdiagonal) + { + for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc) + for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numinputcomponents) + for (c = 0; c < numoutputcomponents; c++) + outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index]; + } + else + { + for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc) + for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numinputcomponents) + for (c = 0; c < numoutputcomponents; c++) + outpixels[c] = ((index = outputinputcomponentindices[c]) & 0x80000000) ? index : in[index]; + } + } + else + { + // special indices not used + if (inputflipdiagonal) + { + for (x = 0, line = inpixels + col_ofs; x < inputwidth; x++, line += col_inc) + for (y = 0, in = line + row_ofs; y < inputheight; y++, in += row_inc, outpixels += numinputcomponents) + for (c = 0; c < numoutputcomponents; c++) + outpixels[c] = in[outputinputcomponentindices[c]]; + } + else + { + for (y = 0, line = inpixels + row_ofs; y < inputheight; y++, line += row_inc) + for (x = 0, in = line + col_ofs; x < inputwidth; x++, in += col_inc, outpixels += numinputcomponents) + for (c = 0; c < numoutputcomponents; c++) + outpixels[c] = in[outputinputcomponentindices[c]]; + } + } +} +#else +// intentionally readable version +void Image_CopyMux(qbyte *outpixels, const qbyte *inpixels, int inputwidth, int inputheight, qboolean inputflipx, qboolean inputflipy, qboolean inputflipdiagonal, int numoutputcomponents, int numinputcomponents, int *outputinputcomponentindices) { int index, c, x, y; const qbyte *in, *inrow, *incolumn; - if (flipdiagonal) + if (inputflipdiagonal) { - for (y = 0;y < height;y++) + for (x = 0;x < inputwidth;x++) { - incolumn = inpixels + (flipx ? width - 1 - y : y) * numincomponents; - for (x = 0;x < width;x++) + for (y = 0;y < inputheight;y++) { - in = incolumn + (flipy ? height - 1 - x : x) * width * numincomponents; - for (c = 0;c < numoutcomponents;c++) + in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents; + for (c = 0;c < numoutputcomponents;c++) { - index = inputcomponentindices[c]; - *outpixels++ = (index & 0x80000000) ? (index - 0x8000000) : in[index]; + index = outputinputcomponentindices[c]; + if (index & 0x80000000) + *outpixels++ = index; + else + *outpixels++ = in[index]; } } } } else { - for (y = 0;y < height;y++) + for (y = 0;y < inputheight;y++) { - inrow = inpixels + (flipy ? height - 1 - y : y) * width * numincomponents; - for (x = 0;x < width;x++) + for (x = 0;x < inputwidth;x++) { - in = inrow + (flipx ? width - 1 - x : x) * numincomponents; - for (c = 0;c < numoutcomponents;c++) + in = inpixels + ((inputflipy ? inputheight - 1 - y : y) * inputwidth + (inputflipx ? inputwidth - 1 - x : x)) * numinputcomponents; + for (c = 0;c < numoutputcomponents;c++) { - index = inputcomponentindices[c]; - *outpixels++ = (index & 0x80000000) ? (index - 0x8000000) : in[index]; + index = outputinputcomponentindices[c]; + if (index & 0x80000000) + *outpixels++ = index; + else + *outpixels++ = in[index]; } } } } } +#endif void Image_GammaRemapRGB(const qbyte *in, qbyte *out, int pixels, const qbyte *gammar, const qbyte *gammag, const qbyte *gammab) { @@ -655,13 +711,18 @@ qbyte *loadimagepixels (const char *filename, qboolean complain, int matchwidth, for (i = 0;imageformats[i].formatstring;i++) { sprintf (name, imageformats[i].formatstring, basename); - if ((f = FS_LoadFile(name, true)) && (data = imageformats[i].loadfunc(f, matchwidth, matchheight))) + f = FS_LoadFile(name, tempmempool, true); + if (f) { + data = imageformats[i].loadfunc(f, matchwidth, matchheight); Mem_Free(f); - Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height); - if (developer_memorydebug.integer) - Mem_CheckSentinelsGlobal(); - return data; + if (data) + { + Con_DPrintf("loaded image %s (%dx%d)\n", name, image_width, image_height); + if (developer_memorydebug.integer) + Mem_CheckSentinelsGlobal(); + return data; + } } } if (complain) @@ -792,14 +853,12 @@ rtexture_t *loadtextureimagebumpasnmap (rtexturepool_t *pool, const char *filena return rt; } -qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data) +qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int height, const qbyte *data, qbyte *buffer) { qboolean ret; - qbyte *buffer, *out; + qbyte *out; const qbyte *in, *end; - buffer = Mem_Alloc(tempmempool, width*height*3 + 18); - memset (buffer, 0, 18); buffer[2] = 2; // uncompressed type buffer[12] = (width >> 0) & 0xFF; @@ -820,7 +879,6 @@ qboolean Image_WriteTGARGB_preflipped (const char *filename, int width, int heig } ret = FS_WriteFile (filename, buffer, width*height*3 + 18 ); - Mem_Free(buffer); return ret; } @@ -873,6 +931,7 @@ void Image_WriteTGARGBA (const char *filename, int width, int height, const qbyt buffer[14] = (height >> 0) & 0xFF; buffer[15] = (height >> 8) & 0xFF; buffer[16] = 32; // pixel size + buffer[17] = 8; // transparent flag? (seems to be needed by gimp) // swap rgba to bgra and flip upside down out = buffer + 18; @@ -1249,7 +1308,7 @@ void Image_Resample (const void *indata, int inwidth, int inheight, int indepth, Mem_Free(resamplerow1); resamplerowsize = outwidth*4; if (!resamplemempool) - resamplemempool = Mem_AllocPool("Image Scaling Buffer"); + resamplemempool = Mem_AllocPool("Image Scaling Buffer", 0, NULL); resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2); resamplerow2 = resamplerow1 + resamplerowsize; } @@ -1444,8 +1503,8 @@ void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *ou n[1] = dv[0][2]*dv[1][0]-dv[0][0]*dv[1][2]; n[2] = dv[0][0]*dv[1][1]-dv[0][1]*dv[1][0]; */ - n[0] = ((p1[0] + p1[1] + p1[2]) - (p0[0] + p0[1] + p0[2])); - n[1] = ((p0[0] + p0[1] + p0[2]) - (p2[0] + p2[1] + p2[2])); + n[0] = ((p0[0] + p0[1] + p0[2]) - (p1[0] + p1[1] + p1[2])); + n[1] = ((p2[0] + p2[1] + p2[2]) - (p0[0] + p0[1] + p0[2])); n[2] = ibumpscale; VectorNormalize(n); /*