LCOV - code coverage report
Current view: top level - engine/render - imagedata.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 15.5 % 444 69
Test Date: 2026-01-07 07:46:09 Functions: 28.6 % 42 12

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

Generated by: LCOV version 2.0-1