LCOV - code coverage report
Current view: top level - engine/render - imagedata.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 15.3 % 451 69
Test Date: 2026-07-09 06:56:18 Functions: 27.9 % 43 12

            Line data    Source code
       1              : /**
       2              :  * @file imagedata.cpp
       3              :  * @brief texture information class definitions
       4              :  *
       5              :  * This file implements a class containing the associated date with a texture image.
       6              :  * It is only used by texture.cpp and shaderparam.cpp.
       7              :  */
       8              : #include "../libprimis-headers/cube.h"
       9              : #include "../../shared/geomexts.h"
      10              : #include "../../shared/glexts.h"
      11              : 
      12              : #include "imagedata.h"
      13              : #include "renderwindow.h"
      14              : #include "shaderparam.h"
      15              : #include "texture.h"
      16              : 
      17              : #include "interface/control.h"
      18              : #include "interface/console.h"
      19              : #include "interface/cs.h" //for stringslice
      20              : 
      21              : namespace
      22              : {
      23              :     //helper function for texturedata
      24            0 :     vec parsevec(const char *arg)
      25              :     {
      26            0 :         vec v(0, 0, 0);
      27            0 :         uint i = 0;
      28            0 :         for(; arg[0] && (!i || arg[0]=='/') && i<3; arg += std::strcspn(arg, "/,><"), i++)
      29              :         {
      30            0 :             if(i)
      31              :             {
      32            0 :                 arg++;
      33              :             }
      34            0 :             v[i] = std::atof(arg);
      35              :         }
      36            0 :         if(i==1)
      37              :         {
      38            0 :             v.y = v.z = v.x;
      39              :         }
      40            0 :         return v;
      41              :     }
      42              : 
      43              :     //helper function for texturedata
      44            0 :     bool canloadsurface(const char *name)
      45              :     {
      46            0 :         stream *f = openfile(name, "rb");
      47            0 :         if(!f)
      48              :         {
      49            0 :             return false;
      50              :         }
      51            0 :         delete f;
      52            0 :         return true;
      53              :     }
      54              : 
      55            0 :     char *copystring(char *d, const stringslice &s, size_t len)
      56              :     {
      57            0 :         size_t slen = min(size_t(s.len), len-1);
      58            0 :         std::memcpy(d, s.str, slen);
      59            0 :         d[slen] = 0;
      60            0 :         return d;
      61              :     }
      62              : }
      63              : 
      64            9 : ImageData::ImageData()
      65            9 :     : data(nullptr), owner(nullptr), freefunc(nullptr)
      66            9 : {}
      67              : 
      68              : 
      69            1 : ImageData::ImageData(int nw, int nh, int nbpp, int nlevels, int nalign, GLenum ncompressed)
      70              : {
      71            1 :     setdata(nullptr, nw, nh, nbpp, nlevels, nalign, ncompressed);
      72            1 : }
      73              : 
      74           10 : ImageData::~ImageData()
      75              : {
      76           10 :     cleanup();
      77           10 : }
      78              : 
      79            1 : int ImageData::width() const
      80              : {
      81            1 :     return w;
      82              : }
      83              : 
      84            2 : int ImageData::height() const
      85              : {
      86            2 :     return h;
      87              : }
      88              : 
      89            2 : int ImageData::depth() const
      90              : {
      91            2 :     return bpp;
      92              : }
      93              : 
      94            2 : void ImageData::setdata(uchar *ndata, int nw, int nh, int nbpp, int nlevels, int nalign, GLenum ncompressed)
      95              : {
      96            2 :     w = nw;
      97            2 :     h = nh;
      98            2 :     bpp = nbpp;
      99            2 :     levels = nlevels;
     100            2 :     align = nalign;
     101            2 :     pitch = align ? 0 : w*bpp;
     102            2 :     compressed = ncompressed;
     103            2 :     data = ndata ? ndata : new uchar[calcsize()];
     104            2 :     if(!ndata)
     105              :     {
     106            1 :         owner = this;
     107            1 :         freefunc = nullptr;
     108              :     }
     109            2 : }
     110              : 
     111            0 : int ImageData::calclevelsize(int level) const
     112              : {
     113            0 :     return ((std::max(w>>level, 1)+align-1)/align)*
     114            0 :            ((std::max(h>>level, 1)+align-1)/align)*
     115            0 :            bpp;
     116              : }
     117              : 
     118            1 : int ImageData::calcsize() const
     119              : {
     120            1 :     if(!align)
     121              :     {
     122            1 :         return w*h*bpp;
     123              :     }
     124            0 :     int lw = w,
     125            0 :         lh = h,
     126            0 :         size = 0;
     127            0 :     for(int i = 0; i < levels; ++i)
     128              :     {
     129            0 :         if(lw<=0)
     130              :         {
     131            0 :             lw = 1;
     132              :         }
     133            0 :         if(lh<=0)
     134              :         {
     135            0 :             lh = 1;
     136              :         }
     137            0 :         size += ((lw+align-1)/align)*((lh+align-1)/align)*bpp;
     138            0 :         if(lw*lh==1)
     139              :         {
     140            0 :             break;
     141              :         }
     142            0 :         lw >>= 1;
     143            0 :         lh >>= 1;
     144              :     }
     145            0 :     return size;
     146              : }
     147              : 
     148           10 : void ImageData::disown()
     149              : {
     150           10 :     data = nullptr;
     151           10 :     owner = nullptr;
     152           10 :     freefunc = nullptr;
     153           10 : }
     154              : 
     155           10 : void ImageData::cleanup()
     156              : {
     157           10 :     if(owner==this)
     158              :     {
     159            1 :         delete[] data;
     160              :     }
     161            9 :     else if(freefunc)
     162              :     {
     163            1 :         (*freefunc)(owner);
     164              :     }
     165           10 :     disown();
     166           10 : }
     167              : 
     168            0 : void ImageData::replace(ImageData &d)
     169              : {
     170            0 :     cleanup();
     171            0 :     *this = d;
     172            0 :     if(owner == &d)
     173              :     {
     174            0 :         owner = this;
     175              :     }
     176            0 :     d.disown();
     177            0 : }
     178              : 
     179            1 : void ImageData::wraptex(SDL_Surface *s)
     180              : {
     181            1 :     setdata(static_cast<uchar *>(s->pixels), s->w, s->h, s->format->BytesPerPixel);
     182            1 :     pitch = s->pitch;
     183            1 :     owner = s;
     184            1 :     freefunc = reinterpret_cast<void (*)(void *)>(SDL_FreeSurface);
     185            1 : }
     186              : 
     187              : /**
     188              :  * @brief Manpulates a texture pixel-by-pixel
     189              :  *
     190              :  * For each pixel in the source, executes an arbitrary effect, passed as the body
     191              :  * parameter.
     192              :  *
     193              :  * @param t imagedata object, destination of texture write
     194              :  * @param body code to execute
     195              :  */
     196              : #define WRITE_TEX(t, body) do \
     197              :     { \
     198              :         uchar *dstrow = t.data; \
     199              :         for(int y = 0; y < t.h; ++y) \
     200              :         { \
     201              :             for(uchar *dst = dstrow, *end = &dstrow[t.w*t.bpp]; dst < end; dst += t.bpp) \
     202              :             { \
     203              :                 body; \
     204              :             } \
     205              :             dstrow += t.pitch; \
     206              :         } \
     207              :     } while(0)
     208              : 
     209              : /**
     210              :  * @brief Manpulates one texture using another texture's data.
     211              :  *
     212              :  * For each pixel in the source, executes an arbitrary effect, passed as the body
     213              :  * parameter.
     214              :  *
     215              :  * @param t imagedata object, destination of texture write
     216              :  * @param s imagedata object, source of texture write
     217              :  * @param body code to execute
     218              :  */
     219              : #define READ_WRITE_TEX(t, s, body) do \
     220              :     { \
     221              :         uchar *dstrow = t.data, *srcrow = s.data; \
     222              :         for(int y = 0; y < t.h; ++y) \
     223              :         { \
     224              :             for(uchar *dst = dstrow, *src = srcrow, *end = &srcrow[s.w*s.bpp]; src < end; dst += t.bpp, src += s.bpp) \
     225              :             { \
     226              :                 body; \
     227              :             } \
     228              :             dstrow += t.pitch; \
     229              :             srcrow += s.pitch; \
     230              :         } \
     231              :     } while(0)
     232              : 
     233              : #define READ_2_WRITE_TEX(t, s1, src1, s2, src2, body) do \
     234              :     { \
     235              :         uchar *dstrow = t.data, *src1row = s1.data, *src2row = s2.data; \
     236              :         for(int y = 0; y < t.h; ++y) \
     237              :         { \
     238              :             for(uchar *dst = dstrow, *end = &dstrow[t.w*t.bpp], *src1 = src1row, *src2 = src2row; dst < end; dst += t.bpp, src1 += s1.bpp, src2 += s2.bpp) \
     239              :             { \
     240              :                 body; \
     241              :             } \
     242              :             dstrow += t.pitch; \
     243              :             src1row += s1.pitch; \
     244              :             src2row += s2.pitch; \
     245              :         } \
     246              :     } while(0)
     247              : 
     248              : #define READ_WRITE_RGB_TEX(t, s, body) \
     249              :     { \
     250              :         if(t.bpp >= 3) \
     251              :         { \
     252              :             READ_WRITE_TEX(t, s, body); \
     253              :         } \
     254              :         else \
     255              :         { \
     256              :             ImageData rgb(t.w, t.h, 3); \
     257              :             READ_2_WRITE_TEX(rgb, t, orig, s, src, { dst[0] = dst[1] = dst[2] = orig[0]; body; }); \
     258              :             t.replace(rgb); \
     259              :         } \
     260              :     }
     261              : 
     262            0 : void ImageData::forcergbimage()
     263              : {
     264            0 :     if(bpp >= 3)
     265              :     {
     266            0 :         return;
     267              :     }
     268            0 :     ImageData d(w, h, 3);
     269            0 :     READ_WRITE_TEX(d, ((*this)), { dst[0] = dst[1] = dst[2] = src[0]; });
     270            0 :     replace(d);
     271            0 : }
     272              : 
     273              : /**
     274              :  * @brief Writes data to pixels of an ImageData
     275              :  *
     276              :  * Attempts to write to four channels (r,g,b,a) of a texture; if there are at least
     277              :  * 4 channels already in the destination of the texture operation, uses READ_WRITE_TEX.
     278              :  *
     279              :  * @param t imagedata object, destination of texture write
     280              :  * @param s imagedata object, source of texture data
     281              :  * @param body code to execute
     282              :  */
     283              : #define READ_WRITE_RGBA_TEX(t, s, body) \
     284              :     { \
     285              :         if(t.bpp >= 4) \
     286              :         { \
     287              :             READ_WRITE_TEX(t, s, body); \
     288              :         } \
     289              :         else \
     290              :         { \
     291              :             ImageData rgba(t.w, t.h, 4); \
     292              :             if(t.bpp==3) \
     293              :             { \
     294              :                 READ_2_WRITE_TEX(rgba, t, orig, s, src, { dst[0] = orig[0]; dst[1] = orig[1]; dst[2] = orig[2]; body; }); \
     295              :             } \
     296              :             else \
     297              :             { \
     298              :                 READ_2_WRITE_TEX(rgba, t, orig, s, src, { dst[0] = dst[1] = dst[2] = orig[0]; body; }); \
     299              :             } \
     300              :             t.replace(rgba); \
     301              :         } \
     302              :     }
     303              : 
     304            0 : void ImageData::swizzleimage()
     305              : {
     306            0 :     if(bpp==2)
     307              :     {
     308            0 :         ImageData d(w, h, 4);
     309            0 :         READ_WRITE_TEX(d, (*this), { dst[0] = dst[1] = dst[2] = src[0]; dst[3] = src[1]; });
     310            0 :         replace(d);
     311            0 :     }
     312            0 :     else if(bpp==1)
     313              :     {
     314            0 :         ImageData d(w, h, 3);
     315            0 :         READ_WRITE_TEX(d, (*this), { dst[0] = dst[1] = dst[2] = src[0]; });
     316            0 :         replace(d);
     317            0 :     }
     318            0 : }
     319              : 
     320            0 : void ImageData::scaleimage(int newwidth, int newheight)
     321              : {
     322            0 :     ImageData d(newwidth, newheight, bpp);
     323            0 :     scaletexture(data, newwidth, newheight, bpp, pitch, d.data, newwidth, newheight);
     324            0 :     replace(d);
     325            0 : }
     326              : 
     327            0 : void ImageData::texreorient(bool flipx, bool flipy, bool swapxy, int type)
     328              : {
     329            0 :     ImageData d(swapxy ? h : w, swapxy ? w : h, bpp, levels, align, compressed);
     330            0 :     switch(compressed)
     331              :     {
     332              :         default:
     333            0 :             if(type == Tex_Normal && bpp >= 3)
     334              :             {
     335            0 :                 reorientnormals(data, w, h, bpp, pitch, d.data, flipx, flipy, swapxy);
     336              :             }
     337              :             else
     338              :             {
     339            0 :                 reorienttexture(data, w, h, bpp, pitch, d.data, flipx, flipy, swapxy);
     340              :             }
     341            0 :             break;
     342              :     }
     343            0 :     replace(d);
     344            0 : }
     345              : 
     346            0 : void ImageData::texrotate(int numrots, int type)
     347              : {
     348            0 :     if(numrots>=1 && numrots<=7)
     349              :     {
     350            0 :         const TexRotation &r = texrotations[numrots];
     351            0 :         texreorient(r.flipx, r.flipy, r.swapxy, type);
     352              :     }
     353            0 : }
     354              : 
     355            0 : void ImageData::texoffset(int xoffset, int yoffset)
     356              : {
     357            0 :     xoffset = std::max(xoffset, 0);
     358            0 :     xoffset %= w;
     359            0 :     yoffset = std::max(yoffset, 0);
     360            0 :     yoffset %= h;
     361            0 :     if(!xoffset && !yoffset)
     362              :     {
     363            0 :         return;
     364              :     }
     365            0 :     ImageData d(w, h, bpp);
     366            0 :     uchar *src = data;
     367            0 :     for(int y = 0; y < h; ++y)
     368              :     {
     369            0 :         uchar *dst = static_cast<uchar *>(d.data)+((y+yoffset)%d.h)*d.pitch;
     370            0 :         std::memcpy(dst+xoffset*bpp, src, (w-xoffset)*bpp);
     371            0 :         std::memcpy(dst, src+(w-xoffset)*bpp, xoffset*bpp);
     372            0 :         src += pitch;
     373              :     }
     374            0 :     replace(d);
     375            0 : }
     376              : 
     377            0 : void ImageData::texcrop(int x, int y, int w, int h)
     378              : {
     379            0 :     x = std::clamp(x, 0, w);
     380            0 :     y = std::clamp(y, 0, h);
     381            0 :     w = std::min(w < 0 ? w : w, w - x);
     382            0 :     h = std::min(h < 0 ? h : h, h - y);
     383            0 :     if(!w || !h)
     384              :     {
     385            0 :         return;
     386              :     }
     387            0 :     ImageData d(w, h, bpp);
     388            0 :     uchar *src = data + y*pitch + x*bpp,
     389            0 :           *dst = d.data;
     390            0 :     for(int y = 0; y < h; ++y)
     391              :     {
     392            0 :         std::memcpy(dst, src, w*bpp);
     393            0 :         src += pitch;
     394            0 :         dst += d.pitch;
     395              :     }
     396            0 :     replace(d);
     397            0 : }
     398              : 
     399            0 : void ImageData::texmad(const vec &mul, const vec &add)
     400              : {
     401            0 :     if(bpp < 3 && (mul.x != mul.y || mul.y != mul.z || add.x != add.y || add.y != add.z))
     402              :     {
     403            0 :         swizzleimage();
     404              :     }
     405            0 :     int maxk = std::min(static_cast<int>(bpp), 3);
     406            0 :     WRITE_TEX((*this),
     407              :         for(int k = 0; k < maxk; ++k)
     408              :         {
     409              :             dst[k] = static_cast<uchar>(std::clamp(dst[k]*mul[k] + 255*add[k], 0.0f, 255.0f));
     410              :         }
     411              :     );
     412            0 : }
     413              : 
     414            0 : void ImageData::texcolorify(const vec &color, vec weights)
     415              : {
     416            0 :     if(bpp < 3)
     417              :     {
     418            0 :         return;
     419              :     }
     420            0 :     if(weights.iszero())
     421              :     {
     422            0 :         weights = vec(0.21f, 0.72f, 0.07f);
     423              :     }
     424            0 :     WRITE_TEX((*this),
     425              :         float lum = dst[0]*weights.x + dst[1]*weights.y + dst[2]*weights.z;
     426              :         for(int k = 0; k < 3; ++k)
     427              :         {
     428              :             dst[k] = static_cast<uchar>(std::clamp(lum*color[k], 0.0f, 255.0f));
     429              :         }
     430              :     );
     431              : }
     432              : 
     433            0 : void ImageData::texcolormask(const vec &color1, const vec &color2)
     434              : {
     435            0 :     if(bpp < 4)
     436              :     {
     437            0 :         return;
     438              :     }
     439            0 :     ImageData d(w, h, 3);
     440            0 :     READ_WRITE_TEX(d, (*this),
     441              :         vec color;
     442              :         color.lerp(color2, color1, src[3]/255.0f);
     443              :         for(int k = 0; k < 3; ++k)
     444              :         {
     445              :             dst[k] = static_cast<uchar>(std::clamp(color[k]*src[k], 0.0f, 255.0f));
     446              :         }
     447              :     );
     448            0 :     replace(d);
     449            0 : }
     450              : 
     451            0 : void ImageData::texdup(int srcchan, int dstchan)
     452              : {
     453            0 :     if(srcchan==dstchan || std::max(srcchan, dstchan) >= bpp)
     454              :     {
     455            0 :         return;
     456              :     }
     457            0 :     WRITE_TEX((*this), dst[dstchan] = dst[srcchan]);
     458              : }
     459              : 
     460            0 : void ImageData::texmix(int c1, int c2, int c3, int c4)
     461              : {
     462            0 :     int numchans = c1 < 0 ? 0 : (c2 < 0 ? 1 : (c3 < 0 ? 2 : (c4 < 0 ? 3 : 4)));
     463            0 :     if(numchans <= 0)
     464              :     {
     465            0 :         return;
     466              :     }
     467            0 :     ImageData d(w, h, numchans);
     468            0 :     READ_WRITE_TEX(d, (*this),
     469              :         switch(numchans)
     470              :         {
     471              :             case 4:
     472              :             {
     473              :                 dst[3] = src[c4];
     474              :                 break;
     475              :             }
     476              :             case 3:
     477              :             {
     478              :                 dst[2] = src[c3];
     479              :                 break;
     480              :             }
     481              :             case 2:
     482              :             {
     483              :                 dst[1] = src[c2];
     484              :                 break;
     485              :             }
     486              :             case 1:
     487              :             {
     488              :                 dst[0] = src[c1];
     489              :                 break;
     490              :             }
     491              :         }
     492              :     );
     493            0 :     replace(d);
     494            0 : }
     495              : 
     496            0 : void ImageData::texgrey()
     497              : {
     498            0 :     if(bpp <= 2)
     499              :     {
     500            0 :         return;
     501              :     }
     502            0 :     ImageData d(w, h, bpp >= 4 ? 2 : 1);
     503            0 :     if(bpp >= 4)
     504              :     {
     505            0 :         READ_WRITE_TEX(d, (*this),
     506              :             dst[0] = src[0];
     507              :             dst[1] = src[3];
     508              :         );
     509              :     }
     510              :     else
     511              :     {
     512            0 :         READ_WRITE_TEX(d, (*this), dst[0] = src[0]);
     513              :     }
     514            0 :     replace(d);
     515            0 : }
     516              : 
     517            0 : void ImageData::texpremul()
     518              : {
     519            0 :     switch(bpp)
     520              :     {
     521            0 :         case 2:
     522            0 :             WRITE_TEX((*this),
     523              :                 dst[0] = static_cast<uchar>((static_cast<uint>(dst[0])*static_cast<uint>(dst[1]))/255);
     524              :             );
     525            0 :             break;
     526            0 :         case 4:
     527            0 :             WRITE_TEX((*this),
     528              :                 uint alpha = dst[3];
     529              :                 dst[0] = static_cast<uchar>((static_cast<uint>(dst[0])*alpha)/255);
     530              :                 dst[1] = static_cast<uchar>((static_cast<uint>(dst[1])*alpha)/255);
     531              :                 dst[2] = static_cast<uchar>((static_cast<uint>(dst[2])*alpha)/255);
     532              :             );
     533            0 :             break;
     534              :     }
     535            0 : }
     536              : 
     537            0 : void ImageData::texagrad(float x2, float y2, float x1, float y1)
     538              : {
     539            0 :     if(bpp != 2 && bpp != 4)
     540              :     {
     541            0 :         return;
     542              :     }
     543            0 :     y1 = 1 - y1;
     544            0 :     y2 = 1 - y2;
     545            0 :     float minx = 1,
     546            0 :           miny = 1,
     547            0 :           maxx = 1,
     548            0 :           maxy = 1;
     549            0 :     if(x1 != x2)
     550              :     {
     551            0 :         minx = (0 - x1) / (x2 - x1);
     552            0 :         maxx = (1 - x1) / (x2 - x1);
     553              :     }
     554            0 :     if(y1 != y2)
     555              :     {
     556            0 :         miny = (0 - y1) / (y2 - y1);
     557            0 :         maxy = (1 - y1) / (y2 - y1);
     558              :     }
     559            0 :     float dx = (maxx - minx)/std::max(w-1, 1),
     560            0 :           dy = (maxy - miny)/std::max(h-1, 1),
     561            0 :           cury = miny;
     562            0 :     for(uchar *dstrow = data + bpp - 1, *endrow = dstrow + h*pitch; dstrow < endrow; dstrow += pitch)
     563              :     {
     564            0 :         float curx = minx;
     565            0 :         for(uchar *dst = dstrow, *end = &dstrow[w*bpp]; dst < end; dst += bpp)
     566              :         {
     567            0 :             dst[0] = static_cast<uchar>(dst[0]*std::clamp(curx, 0.0f, 1.0f)*std::clamp(cury, 0.0f, 1.0f));
     568            0 :             curx += dx;
     569              :         }
     570            0 :         cury += dy;
     571              :     }
     572              : }
     573              : 
     574            0 : void ImageData::texblend(const ImageData &s0, const ImageData &m0)
     575              : {
     576            0 :     ImageData s(s0),
     577            0 :               m(m0);
     578            0 :     if(s.w != w || s.h != h)
     579              :     {
     580            0 :         s.scaleimage(w, h);
     581              :     }
     582            0 :     if(m.w != w || m.h != h)
     583              :     {
     584            0 :         m.scaleimage(w, h);
     585              :     }
     586              :     if(&s == &m)
     587              :     {
     588              :         if(s.bpp == 2)
     589              :         {
     590              :             if(bpp >= 3)
     591              :             {
     592              :                 s.swizzleimage();
     593              :             }
     594              :         }
     595              :         else if(s.bpp == 4)
     596              :         {
     597              :             if(bpp < 3)
     598              :             {
     599              :                 swizzleimage();
     600              :             }
     601              :         }
     602              :         else
     603              :         {
     604              :             return;
     605              :         }
     606              :         //need to declare int for each var because it's inside a macro body
     607              :         if(bpp < 3)
     608              :         {
     609              :             READ_WRITE_TEX((*this), s,
     610              :                 int srcblend = src[1];
     611              :                 int dstblend = 255 - srcblend;
     612              :                 dst[0] = static_cast<uchar>((dst[0]*dstblend + src[0]*srcblend)/255);
     613              :             );
     614              :         }
     615              :         else
     616              :         {
     617              :             READ_WRITE_TEX((*this), s,
     618              :                 int srcblend = src[3];
     619              :                 int dstblend = 255 - srcblend;
     620              :                 dst[0] = static_cast<uchar>((dst[0]*dstblend + src[0]*srcblend)/255);
     621              :                 dst[1] = static_cast<uchar>((dst[1]*dstblend + src[1]*srcblend)/255);
     622              :                 dst[2] = static_cast<uchar>((dst[2]*dstblend + src[2]*srcblend)/255);
     623              :             );
     624              :         }
     625              :     }
     626              :     else
     627              :     {
     628            0 :         if(s.bpp < 3)
     629              :         {
     630            0 :             if(bpp >= 3)
     631              :             {
     632            0 :                 s.swizzleimage();
     633              :             }
     634              :         }
     635            0 :         else if(bpp < 3)
     636              :         {
     637            0 :             swizzleimage();
     638              :         }
     639            0 :         if(bpp < 3)
     640              :         {
     641            0 :             READ_2_WRITE_TEX((*this), s, src, m, mask,
     642              :                 int srcblend = mask[0];
     643              :                 int dstblend = 255 - srcblend;
     644              :                 dst[0] = static_cast<uchar>((dst[0]*dstblend + src[0]*srcblend)/255);
     645              :             );
     646              :         }
     647              :         else
     648              :         {
     649            0 :             READ_2_WRITE_TEX((*this), s, src, m, mask,
     650              :                 int srcblend = mask[0];
     651              :                 int dstblend = 255 - srcblend;
     652              :                 dst[0] = static_cast<uchar>((dst[0]*dstblend + src[0]*srcblend)/255);
     653              :                 dst[1] = static_cast<uchar>((dst[1]*dstblend + src[1]*srcblend)/255);
     654              :                 dst[2] = static_cast<uchar>((dst[2]*dstblend + src[2]*srcblend)/255);
     655              :             );
     656              :         }
     657              :     }
     658            0 : }
     659              : 
     660            0 : void ImageData::addglow(const ImageData &g, const vec &glowcolor)
     661              : {
     662            0 :     if(g.bpp < 3)
     663              :     {
     664            0 :         READ_WRITE_RGB_TEX((*this), g,
     665              :             for(int k = 0; k < 3; ++k)
     666              :             {
     667              :                 dst[k] = std::clamp(static_cast<int>(dst[k]) + static_cast<int>(src[0]*glowcolor[k]), 0, 255);
     668              :             }
     669              :         );
     670              :     }
     671              :     else
     672              :     {
     673            0 :         READ_WRITE_RGB_TEX((*this), g,
     674              :             for(int k = 0; k < 3; ++k)
     675              :             {
     676              :                 dst[k] = std::clamp(static_cast<int>(dst[k]) + static_cast<int>(src[k]*glowcolor[k]), 0, 255);
     677              :             }
     678              :         );
     679              :     }
     680            0 : }
     681              : 
     682            0 : void ImageData::mergespec(const ImageData &s)
     683              : {
     684            0 :     if(s.bpp < 3)
     685              :     {
     686            0 :         READ_WRITE_RGBA_TEX((*this), s,
     687              :             dst[3] = src[0];
     688              :         );
     689              :     }
     690              :     else
     691              :     {
     692            0 :         READ_WRITE_RGBA_TEX((*this), s,
     693              :             dst[3] = (static_cast<int>(src[0]) + static_cast<int>(src[1]) + static_cast<int>(src[2]))/3;
     694              :         );
     695              :     }
     696            0 : }
     697              : 
     698            0 : void ImageData::mergedepth(const ImageData &z)
     699              : {
     700            0 :     READ_WRITE_RGBA_TEX((*this), z,
     701              :         dst[3] = src[0];
     702              :     );
     703            0 : }
     704              : 
     705            0 : void ImageData::mergealpha(const ImageData &s)
     706              : {
     707            0 :     if(s.bpp < 3)
     708              :     {
     709            0 :         READ_WRITE_RGBA_TEX((*this), s,
     710              :             dst[3] = src[0];
     711              :         );
     712              :     }
     713              :     else
     714              :     {
     715            0 :         READ_WRITE_RGBA_TEX((*this), s,
     716              :             dst[3] = src[3];
     717              :         );
     718              :     }
     719            0 : }
     720              : 
     721            0 : void ImageData::collapsespec()
     722              : {
     723            0 :     ImageData d(w, h, 1);
     724            0 :     if(bpp >= 3)
     725              :     {
     726            0 :         READ_WRITE_TEX(d, (*this),
     727              :         {
     728              :             dst[0] = (static_cast<int>(src[0]) + static_cast<int>(src[1]) + static_cast<int>(src[2]))/3;
     729              :         });
     730              :     }
     731              :     else
     732              :     {
     733            0 :         READ_WRITE_TEX(d, (*this), { dst[0] = src[0]; });
     734              :     }
     735            0 :     replace(d);
     736            0 : }
     737              : 
     738            0 : void ImageData::texnormal(int emphasis)
     739              : {
     740            0 :     ImageData d(w, h, 3);
     741            0 :     const uchar *src = data;
     742            0 :     uchar *dst = d.data;
     743            0 :     for(int y = 0; y < h; ++y)
     744              :     {
     745            0 :         for(int x = 0; x < w; ++x)
     746              :         {
     747            0 :             vec normal(0.0f, 0.0f, 255.0f/emphasis);
     748            0 :             normal.x += src[y*pitch + ((x+w-1)%w)*bpp];
     749            0 :             normal.x -= src[y*pitch + ((x+1)%w)*bpp];
     750            0 :             normal.y += src[((y+h-1)%h)*pitch + x*bpp];
     751            0 :             normal.y -= src[((y+1)%h)*pitch + x*bpp];
     752            0 :             normal.normalize();
     753            0 :             *(dst++) = static_cast<uchar>(127.5f + normal.x*127.5f);
     754            0 :             *(dst++) = static_cast<uchar>(127.5f + normal.y*127.5f);
     755            0 :             *(dst++) = static_cast<uchar>(127.5f + normal.z*127.5f);
     756              :         }
     757              :     }
     758            0 :     replace(d);
     759            0 : }
     760              : 
     761            0 : bool ImageData::matchstring(std::string_view s, size_t len, std::string_view d)
     762              : {
     763            0 :     return len == d.size() && !std::memcmp(s.data(), d.data(), d.size());
     764              : }
     765              : 
     766            9 : bool ImageData::texturedata(const char *tname, bool msg, int * const compress, int * const wrap, const char *tdir, int ttype)
     767              : {
     768            0 :     auto parsetexcommands = [] (const char *&cmds, const char *&cmd, size_t &len, std::array<const char *, 4> &arg)
     769              :     {
     770            0 :         const char *end = nullptr;
     771            0 :         cmd = &cmds[1];
     772            0 :         end = std::strchr(cmd, '>');
     773            0 :         if(!end)
     774              :         {
     775            0 :             return true;
     776              :         }
     777            0 :         cmds = std::strchr(cmd, '<');
     778            0 :         len = std::strcspn(cmd, ":,><");
     779            0 :         for(int i = 0; i < 4; ++i)
     780              :         {
     781            0 :             arg[i] = std::strchr(i ? arg[i-1] : cmd, i ? ',' : ':');
     782            0 :             if(!arg[i] || arg[i] >= end)
     783              :             {
     784            0 :                 arg[i] = "";
     785              :             }
     786              :             else
     787              :             {
     788            0 :                 arg[i]++;
     789              :             }
     790              :         }
     791            0 :         return false;
     792              :     };
     793              : 
     794            9 :     const char *cmds = nullptr,
     795            9 :                *file = tname;
     796            9 :     if(tname[0]=='<')
     797              :     {
     798            0 :         cmds = tname;
     799            0 :         file = std::strrchr(tname, '>');
     800            0 :         if(!file)
     801              :         {
     802            0 :             if(msg)
     803              :             {
     804            0 :                 conoutf(Console_Error, "could not load <> modified texture %s", tname);
     805              :             }
     806            0 :             return false;
     807              :         }
     808            0 :         file++;
     809              :     }
     810              :     string pname;
     811            9 :     if(tdir)
     812              :     {
     813            0 :         formatstring(pname, "%s/%s", tdir, file);
     814            0 :         file = path(pname);
     815              :     }
     816            9 :     for(const char *pcmds = cmds; pcmds;)
     817              :     {
     818            0 :         const char *cmd = nullptr;
     819            0 :         size_t len = 0;
     820            0 :         std::array<const char *, 4> arg = { nullptr, nullptr, nullptr, nullptr };
     821              : 
     822            0 :         if(parsetexcommands(pcmds, cmd, len, arg))
     823              :         {
     824            0 :             break;
     825              :         }
     826              : 
     827            0 :         if(matchstring(cmd, len, "stub"))
     828              :         {
     829            0 :             return canloadsurface(file);
     830              :         }
     831              :     }
     832            9 :     if(msg)
     833              :     {
     834            4 :         renderprogress(loadprogress, file);
     835              :     }
     836            9 :     if(!data)
     837              :     {
     838            9 :         SDL_Surface *s = loadsurface(file);
     839            9 :         if(!s)
     840              :         {
     841            8 :             if(msg)
     842              :             {
     843            4 :                 conoutf(Console_Error, "could not load texture %s", file);
     844              :             }
     845            8 :             return false;
     846              :         }
     847            1 :         int surfacebpp = s->format->BitsPerPixel;
     848            1 :         if(surfacebpp%8 || !texformat(surfacebpp/8))
     849              :         {
     850            0 :             SDL_FreeSurface(s);
     851            0 :             conoutf(Console_Error, "texture must be 8, 16, 24, or 32 bpp: %s", file);
     852            0 :             return false;
     853              :         }
     854            1 :         if(std::max(s->w, s->h) > (1<<12))
     855              :         {
     856            0 :             SDL_FreeSurface(s);
     857            0 :             conoutf(Console_Error, "texture size exceeded %dx%d pixels: %s", 1<<12, 1<<12, file);
     858            0 :             return false;
     859              :         }
     860            1 :         wraptex(s);
     861              :     }
     862              : 
     863            1 :     while(cmds)
     864              :     {
     865            0 :         const char *cmd = nullptr;
     866            0 :         size_t len = 0;
     867            0 :         std::array<const char *, 4> arg = { nullptr, nullptr, nullptr, nullptr };
     868              : 
     869            0 :         if(parsetexcommands(cmds, cmd, len, arg))
     870              :         {
     871            0 :             break;
     872              :         }
     873              : 
     874            0 :         if(compressed)
     875              :         {
     876            0 :             goto compressed; //see `compressed` nested between else & if (yes it's ugly)
     877              :         }
     878            0 :         if(matchstring(cmd, len, "mad"))
     879              :         {
     880            0 :             texmad(parsevec(arg[0]), parsevec(arg[1]));
     881              :         }
     882            0 :         else if(matchstring(cmd, len, "colorify"))
     883              :         {
     884            0 :             texcolorify(parsevec(arg[0]), parsevec(arg[1]));
     885              :         }
     886            0 :         else if(matchstring(cmd, len, "colormask"))
     887              :         {
     888            0 :             texcolormask(parsevec(arg[0]), *arg[1] ? parsevec(arg[1]) : vec(1, 1, 1));
     889              :         }
     890            0 :         else if(matchstring(cmd, len, "normal"))
     891              :         {
     892            0 :             int emphasis = std::atoi(arg[0]);
     893            0 :             texnormal(emphasis > 0 ? emphasis : 3);
     894              :         }
     895            0 :         else if(matchstring(cmd, len, "dup"))
     896              :         {
     897            0 :             texdup(std::atoi(arg[0]), std::atoi(arg[1]));
     898              :         }
     899            0 :         else if(matchstring(cmd, len, "offset"))
     900              :         {
     901            0 :             texoffset(std::atoi(arg[0]), std::atoi(arg[1]));
     902              :         }
     903            0 :         else if(matchstring(cmd, len, "rotate"))
     904              :         {
     905            0 :             texrotate(std::atoi(arg[0]), ttype);
     906              :         }
     907            0 :         else if(matchstring(cmd, len, "reorient"))
     908              :         {
     909            0 :             texreorient(std::atoi(arg[0])>0, std::atoi(arg[1])>0, std::atoi(arg[2])>0, ttype);
     910              :         }
     911            0 :         else if(matchstring(cmd, len, "crop"))
     912              :         {
     913            0 :             texcrop(std::atoi(arg[0]), std::atoi(arg[1]), *arg[2] ? std::atoi(arg[2]) : -1, *arg[3] ? std::atoi(arg[3]) : -1);
     914              :         }
     915            0 :         else if(matchstring(cmd, len, "mix"))
     916              :         {
     917            0 :             texmix(*arg[0] ? std::atoi(arg[0]) : -1, *arg[1] ? std::atoi(arg[1]) : -1, *arg[2] ? std::atoi(arg[2]) : -1, *arg[3] ? std::atoi(arg[3]) : -1);
     918              :         }
     919            0 :         else if(matchstring(cmd, len, "grey"))
     920              :         {
     921            0 :             texgrey();
     922              :         }
     923            0 :         else if(matchstring(cmd, len, "premul"))
     924              :         {
     925            0 :             texpremul();
     926              :         }
     927            0 :         else if(matchstring(cmd, len, "agrad"))
     928              :         {
     929            0 :             texagrad(std::atof(arg[0]), std::atof(arg[1]), std::atof(arg[2]), std::atof(arg[3]));
     930              :         }
     931            0 :         else if(matchstring(cmd, len, "blend"))
     932              :         {
     933            0 :             ImageData src, mask;
     934              :             string srcname, maskname;
     935            0 :             copystring(srcname, stringslice(arg[0], std::strcspn(arg[0], ":,><")), 260);
     936            0 :             copystring(maskname, stringslice(arg[1], std::strcspn(arg[1], ":,><")), 260);
     937            0 :             if(srcname[0] && src.texturedata(srcname, false, nullptr, nullptr, tdir, ttype) && (!maskname[0] || mask.texturedata(maskname, false, nullptr, nullptr, tdir, ttype)))
     938              :             {
     939            0 :                 texblend(src, maskname[0] ? mask : src);
     940              :             }
     941            0 :         }
     942            0 :         else if(matchstring(cmd, len, "thumbnail"))
     943              :         {
     944            0 :             int xdim = std::atoi(arg[0]),
     945            0 :                 ydim = std::atoi(arg[1]);
     946            0 :             if(xdim <= 0 || xdim > (1<<12))
     947              :             {
     948            0 :                 xdim = 64;
     949              :             }
     950            0 :             if(ydim <= 0 || ydim > (1<<12))
     951              :             {
     952            0 :                 ydim = xdim; //default 64
     953              :             }
     954            0 :             if(w > xdim || h > ydim)
     955              :             {
     956            0 :                 scaleimage(xdim, ydim);
     957              :             }
     958              :         }
     959            0 :         else if(matchstring(cmd, len, "compress"))
     960              :         {
     961            0 :             int scale = std::atoi(arg[0]);
     962            0 :             if(compress)
     963              :             {
     964            0 :                 *compress = scale;
     965              :             }
     966              :         }
     967            0 :         else if(matchstring(cmd, len, "nocompress"))
     968              :         {
     969            0 :             if(compress)
     970              :             {
     971            0 :                 *compress = -1;
     972              :             }
     973              :         }
     974              :         // note that the else/if in else-if is separated by a goto breakpoint
     975              :         else
     976            0 :     compressed:
     977            0 :         if(matchstring(cmd, len, "mirror"))
     978              :         {
     979            0 :             if(wrap)
     980              :             {
     981            0 :                 *wrap |= 0x300;
     982              :             }
     983              :         }
     984            0 :         else if(matchstring(cmd, len, "noswizzle"))
     985              :         {
     986            0 :             if(wrap)
     987              :             {
     988            0 :                 *wrap |= 0x10000;
     989              :             }
     990              :         }
     991              :     }
     992            1 :     return true;
     993              : }
     994              : 
     995            0 : bool ImageData::texturedata(const Slot &slot, const Slot::Tex &tex, bool msg, int *compress, int *wrap)
     996              : {
     997            0 :     return texturedata(tex.name, msg, compress, wrap, slot.texturedir(), tex.type);
     998              : }
     999              : 
    1000            0 : void ImageData::reorientnormals(uchar * RESTRICT src, int sw, int sh, int surfacebpp, int stride, uchar * RESTRICT dst, bool flipx, bool flipy, bool swapxy)
    1001              : {
    1002            0 :     int stridex = surfacebpp,
    1003            0 :         stridey = surfacebpp;
    1004            0 :     if(swapxy)
    1005              :     {
    1006            0 :         stridex *= sh;
    1007              :     }
    1008              :     else
    1009              :     {
    1010            0 :         stridey *= sw;
    1011              :     }
    1012            0 :     if(flipx)
    1013              :     {
    1014            0 :         dst += (sw-1)*stridex;
    1015            0 :         stridex = -stridex;
    1016              :     }
    1017            0 :     if(flipy)
    1018              :     {
    1019            0 :         dst += (sh-1)*stridey;
    1020            0 :         stridey = -stridey;
    1021              :     }
    1022            0 :     uchar *srcrow = src;
    1023            0 :     for(int i = 0; i < sh; ++i)
    1024              :     {
    1025            0 :         for(uchar *curdst = dst, *src = srcrow, *end = &srcrow[sw*surfacebpp]; src < end;)
    1026              :         {
    1027            0 :             uchar nx = *src++, ny = *src++;
    1028            0 :             if(flipx)
    1029              :             {
    1030            0 :                 nx = 255-nx;
    1031              :             }
    1032            0 :             if(flipy)
    1033              :             {
    1034            0 :                 ny = 255-ny;
    1035              :             }
    1036            0 :             if(swapxy)
    1037              :             {
    1038            0 :                 std::swap(nx, ny);
    1039              :             }
    1040            0 :             curdst[0] = nx;
    1041            0 :             curdst[1] = ny;
    1042            0 :             curdst[2] = *src++;
    1043            0 :             if(surfacebpp > 3)
    1044              :             {
    1045            0 :                 curdst[3] = *src++;
    1046              :             }
    1047            0 :             curdst += stridex;
    1048              :         }
    1049            0 :         srcrow += stride;
    1050            0 :         dst += stridey;
    1051              :     }
    1052            0 : }
        

Generated by: LCOV version 2.0-1