]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image.c
Tomaz made cl_avidemo screenshot saving use no mallocs
[xonotic/darkplaces.git] / image.c
diff --git a/image.c b/image.c
index fb553d07d2a0e61ed233814d7341fa0a1ef0cf00..6b5dd6a1774631a11c58df6915d43abfadde81ea 100644 (file)
--- 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;
 }