LCOV - code coverage report
Current view: top level - engine/render - imagedata.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 15.6 % 442 69
Test Date: 2025-10-13 07:05:02 Functions: 28.6 % 42 12

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

Generated by: LCOV version 2.0-1