1 // ****************************************************************************
2 // * This file is part of the xBRZ project. It is distributed under           *
3 // * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0         *
4 // * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved          *
5 // *                                                                          *
6 // * Additionally and as a special exception, the author gives permission     *
7 // * to link the code of this program with the following libraries            *
8 // * (or with modified versions that use the same licenses), and distribute   *
9 // * linked combinations including the two: MAME, FreeFileSync, Snes9x        *
10 // * You must obey the GNU General Public License in all respects for all of  *
11 // * the code used other than MAME, FreeFileSync, Snes9x.                     *
12 // * If you modify this file, you may extend this exception to your version   *
13 // * of the file, but you are not obligated to do so. If you do not wish to   *
14 // * do so, delete this exception statement from your version.                *
15 // ****************************************************************************
16 
17 #ifndef XBRZ_TOOLS_H_825480175091875
18 #define XBRZ_TOOLS_H_825480175091875
19 
20 #include <cassert>
21 #include <algorithm>
22 #include <type_traits>
23 
24 
25 namespace xbrz
26 {
27 template <uint32_t N> inline
getByte(uint32_t val)28 unsigned char getByte(uint32_t val) { return static_cast<unsigned char>((val >> (8 * N)) & 0xff); }
29 
getAlpha(uint32_t pix)30 inline unsigned char getAlpha(uint32_t pix) { return getByte<3>(pix); }
getRed(uint32_t pix)31 inline unsigned char getRed  (uint32_t pix) { return getByte<2>(pix); }
getGreen(uint32_t pix)32 inline unsigned char getGreen(uint32_t pix) { return getByte<1>(pix); }
getBlue(uint32_t pix)33 inline unsigned char getBlue (uint32_t pix) { return getByte<0>(pix); }
34 
makePixel(unsigned char a,unsigned char r,unsigned char g,unsigned char b)35 inline uint32_t makePixel(unsigned char a, unsigned char r, unsigned char g, unsigned char b) { return (a << 24) | (r << 16) | (g << 8) | b; }
makePixel(unsigned char r,unsigned char g,unsigned char b)36 inline uint32_t makePixel(                 unsigned char r, unsigned char g, unsigned char b) { return             (r << 16) | (g << 8) | b; }
37 
rgb555to888(uint16_t pix)38 inline uint32_t rgb555to888(uint16_t pix) { return ((pix & 0x7C00) << 9) | ((pix & 0x03E0) << 6) | ((pix & 0x001F) << 3); }
rgb565to888(uint16_t pix)39 inline uint32_t rgb565to888(uint16_t pix) { return ((pix & 0xF800) << 8) | ((pix & 0x07E0) << 5) | ((pix & 0x001F) << 3); }
40 
rgb888to555(uint32_t pix)41 inline uint16_t rgb888to555(uint32_t pix) { return static_cast<uint16_t>(((pix & 0xF80000) >> 9) | ((pix & 0x00F800) >> 6) | ((pix & 0x0000F8) >> 3)); }
rgb888to565(uint32_t pix)42 inline uint16_t rgb888to565(uint32_t pix) { return static_cast<uint16_t>(((pix & 0xF80000) >> 8) | ((pix & 0x00FC00) >> 5) | ((pix & 0x0000F8) >> 3)); }
43 
44 
45 template <class Pix> inline
byteAdvance(Pix * ptr,int bytes)46 Pix* byteAdvance(Pix* ptr, int bytes)
47 {
48     using PixNonConst = typename std::remove_cv<Pix>::type;
49     using PixByte     = typename std::conditional<std::is_same<Pix, PixNonConst>::value, char, const char>::type;
50 
51     static_assert(std::is_integral<PixNonConst>::value, "Pix* is expected to be cast-able to char*");
52 
53     return reinterpret_cast<Pix*>(reinterpret_cast<PixByte*>(ptr) + bytes);
54 }
55 
56 
57 //fill block  with the given color
58 template <class Pix> inline
fillBlock(Pix * trg,int pitch,Pix col,int blockWidth,int blockHeight)59 void fillBlock(Pix* trg, int pitch, Pix col, int blockWidth, int blockHeight)
60 {
61     //for (int y = 0; y < blockHeight; ++y, trg = byteAdvance(trg, pitch))
62     //    std::fill(trg, trg + blockWidth, col);
63 
64     for (int y = 0; y < blockHeight; ++y, trg = byteAdvance(trg, pitch))
65         for (int x = 0; x < blockWidth; ++x)
66             trg[x] = col;
67 }
68 
69 
70 //nearest-neighbor (going over target image - slow for upscaling, since source is read multiple times missing out on cache! Fast for similar image sizes!)
71 template <class PixSrc, class PixTrg, class PixConverter>
nearestNeighborScale(const PixSrc * src,int srcWidth,int srcHeight,int srcPitch,PixTrg * trg,int trgWidth,int trgHeight,int trgPitch,int yFirst,int yLast,PixConverter pixCvrt)72 void nearestNeighborScale(const PixSrc* src, int srcWidth, int srcHeight, int srcPitch,
73                           /**/  PixTrg* trg, int trgWidth, int trgHeight, int trgPitch,
74                           int yFirst, int yLast, PixConverter pixCvrt /*convert PixSrc to PixTrg*/)
75 {
76     static_assert(std::is_integral<PixSrc>::value, "PixSrc* is expected to be cast-able to char*");
77     static_assert(std::is_integral<PixTrg>::value, "PixTrg* is expected to be cast-able to char*");
78 
79     static_assert(std::is_same<decltype(pixCvrt(PixSrc())), PixTrg>::value, "PixConverter returning wrong pixel format");
80 
81     if (srcPitch < srcWidth * static_cast<int>(sizeof(PixSrc))  ||
82         trgPitch < trgWidth * static_cast<int>(sizeof(PixTrg)))
83     {
84         assert(false);
85         return;
86     }
87 
88     yFirst = std::max(yFirst, 0);
89     yLast  = std::min(yLast, trgHeight);
90     if (yFirst >= yLast || srcHeight <= 0 || srcWidth <= 0) return;
91 
92     for (int y = yFirst; y < yLast; ++y)
93     {
94         const int ySrc = srcHeight * y / trgHeight;
95         const PixSrc* const srcLine = byteAdvance(src, ySrc * srcPitch);
96         PixTrg*       const trgLine = byteAdvance(trg, y    * trgPitch);
97 
98         for (int x = 0; x < trgWidth; ++x)
99         {
100             const int xSrc = srcWidth * x / trgWidth;
101             trgLine[x] = pixCvrt(srcLine[xSrc]);
102         }
103     }
104 }
105 
106 
107 //nearest-neighbor (going over source image - fast for upscaling, since source is read only once
108 template <class PixSrc, class PixTrg, class PixConverter>
nearestNeighborScaleOverSource(const PixSrc * src,int srcWidth,int srcHeight,int srcPitch,PixTrg * trg,int trgWidth,int trgHeight,int trgPitch,int yFirst,int yLast,PixConverter pixCvrt)109 void nearestNeighborScaleOverSource(const PixSrc* src, int srcWidth, int srcHeight, int srcPitch,
110                                     /**/  PixTrg* trg, int trgWidth, int trgHeight, int trgPitch,
111                                     int yFirst, int yLast, PixConverter pixCvrt /*convert PixSrc to PixTrg*/)
112 {
113     static_assert(std::is_integral<PixSrc>::value, "PixSrc* is expected to be cast-able to char*");
114     static_assert(std::is_integral<PixTrg>::value, "PixTrg* is expected to be cast-able to char*");
115 
116     static_assert(std::is_same<decltype(pixCvrt(PixSrc())), PixTrg>::value, "PixConverter returning wrong pixel format");
117 
118     if (srcPitch < srcWidth * static_cast<int>(sizeof(PixSrc))  ||
119         trgPitch < trgWidth * static_cast<int>(sizeof(PixTrg)))
120     {
121         assert(false);
122         return;
123     }
124 
125     yFirst = std::max(yFirst, 0);
126     yLast  = std::min(yLast, srcHeight);
127     if (yFirst >= yLast || trgWidth <= 0 || trgHeight <= 0) return;
128 
129     for (int y = yFirst; y < yLast; ++y)
130     {
131         //mathematically: ySrc = floor(srcHeight * yTrg / trgHeight)
132         // => search for integers in: [ySrc, ySrc + 1) * trgHeight / srcHeight
133 
134         //keep within for loop to support MT input slices!
135         const int yTrgFirst = ( y      * trgHeight + srcHeight - 1) / srcHeight; //=ceil(y * trgHeight / srcHeight)
136         const int yTrgLast  = ((y + 1) * trgHeight + srcHeight - 1) / srcHeight; //=ceil(((y + 1) * trgHeight) / srcHeight)
137         const int blockHeight = yTrgLast - yTrgFirst;
138 
139         if (blockHeight > 0)
140         {
141             const PixSrc* srcLine = byteAdvance(src, y         * srcPitch);
142             /**/  PixTrg* trgLine = byteAdvance(trg, yTrgFirst * trgPitch);
143             int xTrgFirst = 0;
144 
145             for (int x = 0; x < srcWidth; ++x)
146             {
147                 const int xTrgLast = ((x + 1) * trgWidth + srcWidth - 1) / srcWidth;
148                 const int blockWidth = xTrgLast - xTrgFirst;
149                 if (blockWidth > 0)
150                 {
151                     xTrgFirst = xTrgLast;
152 
153                     const auto trgPix = pixCvrt(srcLine[x]);
154                     fillBlock(trgLine, trgPitch, trgPix, blockWidth, blockHeight);
155                     trgLine += blockWidth;
156                 }
157             }
158         }
159     }
160 }
161 
162 
163 template <class PixTrg, class PixConverter>
bilinearScale(const uint32_t * src,int srcWidth,int srcHeight,int srcPitch,PixTrg * trg,int trgWidth,int trgHeight,int trgPitch,int yFirst,int yLast,PixConverter pixCvrt)164 void bilinearScale(const uint32_t* src, int srcWidth, int srcHeight, int srcPitch,
165                    /**/    PixTrg* trg, int trgWidth, int trgHeight, int trgPitch,
166                    int yFirst, int yLast, PixConverter pixCvrt /*convert uint32_t to PixTrg*/)
167 {
168     static_assert(std::is_integral<PixTrg>::value,                            "PixTrg* is expected to be cast-able to char*");
169     static_assert(std::is_same<decltype(pixCvrt(uint32_t())), PixTrg>::value, "PixConverter returning wrong pixel format");
170 
171     if (srcPitch < srcWidth * static_cast<int>(sizeof(uint32_t)) ||
172         trgPitch < trgWidth * static_cast<int>(sizeof(PixTrg)))
173     {
174         assert(false);
175         return;
176     }
177 
178     yFirst = std::max(yFirst, 0);
179     yLast  = std::min(yLast, trgHeight);
180     if (yFirst >= yLast || srcHeight <= 0 || srcWidth <= 0) return;
181 
182     const double scaleX = static_cast<double>(trgWidth ) / srcWidth;
183     const double scaleY = static_cast<double>(trgHeight) / srcHeight;
184 
185     //perf notes:
186     //    -> double-based calculation is (slightly) faster than float
187     //    -> precalculation gives significant boost; std::vector<> memory allocation is negligible!
188     struct CoeffsX
189     {
190         int     x1 = 0;
191         int     x2 = 0;
192         double xx1 = 0;
193         double x2x = 0;
194     };
195     std::vector<CoeffsX> buf(trgWidth);
196     for (int x = 0; x < trgWidth; ++x)
197     {
198         const int x1 = srcWidth * x / trgWidth;
199         int x2 = x1 + 1;
200         if (x2 == srcWidth) --x2;
201 
202         const double xx1 = x / scaleX - x1;
203         const double x2x = 1 - xx1;
204 		CoeffsX tmp;
205 		tmp.x1 = x1;
206 		tmp.x2 = x2;
207 		tmp.xx1 = xx1;
208 		tmp.x2x = x2x;
209         buf[x] = tmp;
210     }
211 
212     for (int y = yFirst; y < yLast; ++y)
213     {
214         const int y1 = srcHeight * y / trgHeight;
215         int y2 = y1 + 1;
216         if (y2 == srcHeight) --y2;
217 
218         const double yy1 = y / scaleY - y1;
219         const double y2y = 1 - yy1;
220 
221         const uint32_t* const srcLine     = byteAdvance(src, y1 * srcPitch);
222         const uint32_t* const srcLineNext = byteAdvance(src, y2 * srcPitch);
223         PixTrg*         const trgLine     = byteAdvance(trg, y  * trgPitch);
224 
225         for (int x = 0; x < trgWidth; ++x)
226         {
227             //perf: do NOT "simplify" the variable layout without measurement!
228             const int     x1 = buf[x].x1;
229             const int     x2 = buf[x].x2;
230             const double xx1 = buf[x].xx1;
231             const double x2x = buf[x].x2x;
232 
233             const double x2xy2y = x2x * y2y;
234             const double xx1y2y = xx1 * y2y;
235             const double x2xyy1 = x2x * yy1;
236             const double xx1yy1 = xx1 * yy1;
237 
238             auto interpolate = [=](int offset)
239             {
240                 /*
241                     https://en.wikipedia.org/wiki/Bilinear_interpolation
242                     (c11(x2 - x) + c21(x - x1)) * (y2 - y ) +
243                     (c12(x2 - x) + c22(x - x1)) * (y  - y1)
244                 */
245                 const auto c11 = (srcLine    [x1] >> (8 * offset)) & 0xff;
246                 const auto c21 = (srcLine    [x2] >> (8 * offset)) & 0xff;
247                 const auto c12 = (srcLineNext[x1] >> (8 * offset)) & 0xff;
248                 const auto c22 = (srcLineNext[x2] >> (8 * offset)) & 0xff;
249 
250                 return c11 * x2xy2y + c21 * xx1y2y +
251                        c12 * x2xyy1 + c22 * xx1yy1;
252             };
253 
254             const double bi = interpolate(0);
255             const double gi = interpolate(1);
256             const double ri = interpolate(2);
257             const double ai = interpolate(3);
258 
259             const auto b = static_cast<uint32_t>(bi + 0.5);
260             const auto g = static_cast<uint32_t>(gi + 0.5);
261             const auto r = static_cast<uint32_t>(ri + 0.5);
262             const auto a = static_cast<uint32_t>(ai + 0.5);
263 
264             const uint32_t trgPix = (a << 24) | (r << 16) | (g << 8) | b;
265 
266             trgLine[x] = pixCvrt(trgPix);
267         }
268     }
269 }
270 }
271 
272 #endif //XBRZ_TOOLS_H_825480175091875
273