Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. // ****************************************************************************
  2. // * This file is part of the HqMAME 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 MAME library (or with modified *
  8. // * versions of MAME that use the same license as MAME), and distribute      *
  9. // * linked combinations including the two. You must obey the GNU General     *
  10. // * Public License in all respects for all of the code used other than MAME. *
  11. // * If you modify this file, you may extend this exception to your version   *
  12. // * of the file, but you are not obligated to do so. If you do not wish to   *
  13. // * do so, delete this exception statement from your version.                *
  14. // ****************************************************************************
  15.  
  16. #ifndef XBRZ_TOOLS_H_825480175091875
  17. #define XBRZ_TOOLS_H_825480175091875
  18.  
  19. #include <cassert>
  20. #include <algorithm>
  21. #include <type_traits>
  22.  
  23.  
  24. namespace xbrz
  25. {
  26. template <uint32_t N> inline
  27. unsigned char getByte(uint32_t val) {
  28.         return static_cast<unsigned char>((val >> (8 * N)) & 0xff);
  29. }
  30.  
  31. inline unsigned char getAlpha(uint32_t pix) {
  32.         return getByte<3>(pix);
  33. }
  34. inline unsigned char getRed  (uint32_t pix) {
  35.         return getByte<2>(pix);
  36. }
  37. inline unsigned char getGreen(uint32_t pix) {
  38.         return getByte<1>(pix);
  39. }
  40. inline unsigned char getBlue (uint32_t pix) {
  41.         return getByte<0>(pix);
  42. }
  43.  
  44. inline uint32_t makePixel(unsigned char a, unsigned char r, unsigned char g, unsigned char b) {
  45.         return (a << 24) | (r << 16) | (g << 8) | b;
  46. }
  47. inline uint32_t makePixel(                 unsigned char r, unsigned char g, unsigned char b) {
  48.         return (r << 16) | (g << 8) | b;
  49. }
  50.  
  51. inline uint32_t rgb555to888(uint16_t pix) {
  52.         return ((pix & 0x7C00) << 9) | ((pix & 0x03E0) << 6) | ((pix & 0x001F) << 3);
  53. }
  54. inline uint32_t rgb565to888(uint16_t pix) {
  55.         return ((pix & 0xF800) << 8) | ((pix & 0x07E0) << 5) | ((pix & 0x001F) << 3);
  56. }
  57.  
  58. inline uint16_t rgb888to555(uint32_t pix) {
  59.         return static_cast<uint16_t>(((pix & 0xF80000) >> 9) | ((pix & 0x00F800) >> 6) | ((pix & 0x0000F8) >> 3));
  60. }
  61. inline uint16_t rgb888to565(uint32_t pix) {
  62.         return static_cast<uint16_t>(((pix & 0xF80000) >> 8) | ((pix & 0x00FC00) >> 5) | ((pix & 0x0000F8) >> 3));
  63. }
  64.  
  65.  
  66. template <class Pix> inline
  67. Pix* byteAdvance(Pix* ptr, int bytes)
  68. {
  69.         using PixNonConst = typename std::remove_cv<Pix>::type;
  70.         using PixByte     = typename std::conditional<std::is_same<Pix, PixNonConst>::value, char, const char>::type;
  71.  
  72.         static_assert(std::is_integral<PixNonConst>::value, "Pix* is expected to be cast-able to char*");
  73.  
  74.         return reinterpret_cast<Pix*>(reinterpret_cast<PixByte*>(ptr) + bytes);
  75. }
  76.  
  77.  
  78. //fill block  with the given color
  79. template <class Pix> inline
  80. void fillBlock(Pix* trg, int pitch, Pix col, int blockWidth, int blockHeight)
  81. {
  82.         //for (int y = 0; y < blockHeight; ++y, trg = byteAdvance(trg, pitch))
  83.         //    std::fill(trg, trg + blockWidth, col);
  84.  
  85.         for (int y = 0; y < blockHeight; ++y, trg = byteAdvance(trg, pitch))
  86.                 for (int x = 0; x < blockWidth; ++x)
  87.                         trg[x] = col;
  88. }
  89.  
  90.  
  91. enum class SliceType
  92. {
  93.         SOURCE,
  94.         TARGET,
  95. };
  96.  
  97. template <class PixSrc, class PixTrg, class PixConverter>
  98. void nearestNeighborScale(const PixSrc* src, int srcWidth, int srcHeight, int srcPitch,
  99.                           /**/ PixTrg* trg, int trgWidth, int trgHeight, int trgPitch,
  100.                           SliceType st, int yFirst, int yLast, PixConverter pixCvrt /*convert PixSrc to PixTrg*/)
  101. {
  102.         static_assert(std::is_integral<PixSrc>::value, "PixSrc* is expected to be cast-able to char*");
  103.         static_assert(std::is_integral<PixTrg>::value, "PixTrg* is expected to be cast-able to char*");
  104.  
  105.         static_assert(std::is_same<decltype(pixCvrt(PixSrc())), PixTrg>::value, "PixConverter returning wrong pixel format");
  106.  
  107.         if (srcPitch < srcWidth * static_cast<int>(sizeof(PixSrc))  ||
  108.             trgPitch < trgWidth * static_cast<int>(sizeof(PixTrg)))
  109.         {
  110.                 assert(false);
  111.                 return;
  112.         }
  113.  
  114.         switch (st)
  115.         {
  116.         case SliceType::SOURCE:
  117.                 //nearest-neighbor (going over source image - fast for upscaling, since source is read only once
  118.                 yFirst = std::max(yFirst, 0);
  119.                 yLast  = std::min(yLast, srcHeight);
  120.                 if (yFirst >= yLast || trgWidth <= 0 || trgHeight <= 0) return;
  121.  
  122.                 for (int y = yFirst; y < yLast; ++y)
  123.                 {
  124.                         //mathematically: ySrc = floor(srcHeight * yTrg / trgHeight)
  125.                         // => search for integers in: [ySrc, ySrc + 1) * trgHeight / srcHeight
  126.  
  127.                         //keep within for loop to support MT input slices!
  128.                         const int yTrg_first = ( y      * trgHeight + srcHeight - 1) / srcHeight;//=ceil(y * trgHeight / srcHeight)
  129.                         const int yTrg_last  = ((y + 1) * trgHeight + srcHeight - 1) / srcHeight;//=ceil(((y + 1) * trgHeight) / srcHeight)
  130.                         const int blockHeight = yTrg_last - yTrg_first;
  131.  
  132.                         if (blockHeight > 0)
  133.                         {
  134.                                 const PixSrc* srcLine = byteAdvance(src, y * srcPitch);
  135.                                 /**/ PixTrg* trgLine = byteAdvance(trg, yTrg_first * trgPitch);
  136.                                 int xTrg_first = 0;
  137.  
  138.                                 for (int x = 0; x < srcWidth; ++x)
  139.                                 {
  140.                                         const int xTrg_last = ((x + 1) * trgWidth + srcWidth - 1) / srcWidth;
  141.                                         const int blockWidth = xTrg_last - xTrg_first;
  142.                                         if (blockWidth > 0)
  143.                                         {
  144.                                                 xTrg_first = xTrg_last;
  145.  
  146.                                                 const auto trgPix = pixCvrt(srcLine[x]);
  147.                                                 fillBlock(trgLine, trgPitch, trgPix, blockWidth, blockHeight);
  148.                                                 trgLine += blockWidth;
  149.                                         }
  150.                                 }
  151.                         }
  152.                 }
  153.                 break;
  154.  
  155.         case SliceType::TARGET:
  156.                 //nearest-neighbor (going over target image - slow for upscaling, since source is read multiple times missing out on cache! Fast for similar image sizes!)
  157.                 yFirst = std::max(yFirst, 0);
  158.                 yLast  = std::min(yLast, trgHeight);
  159.                 if (yFirst >= yLast || srcHeight <= 0 || srcWidth <= 0) return;
  160.  
  161.                 for (int y = yFirst; y < yLast; ++y)
  162.                 {
  163.                         PixTrg* trgLine = byteAdvance(trg, y * trgPitch);
  164.                         const int ySrc = srcHeight * y / trgHeight;
  165.                         const PixSrc* srcLine = byteAdvance(src, ySrc * srcPitch);
  166.                         for (int x = 0; x < trgWidth; ++x)
  167.                         {
  168.                                 const int xSrc = srcWidth * x / trgWidth;
  169.                                 trgLine[x] = pixCvrt(srcLine[xSrc]);
  170.                         }
  171.                 }
  172.                 break;
  173.         }
  174. }
  175. }
  176.  
  177. #endif //XBRZ_TOOLS_H_825480175091875
  178.