| 1 | #include <base/system.h> |
| 2 | |
| 3 | #include <engine/image.h> |
| 4 | |
| 5 | CImageInfo::CImageInfo(CImageInfo &&Other) |
| 6 | { |
| 7 | *this = std::move(Other); |
| 8 | } |
| 9 | |
| 10 | CImageInfo &CImageInfo::operator=(CImageInfo &&Other) |
| 11 | { |
| 12 | m_Width = Other.m_Width; |
| 13 | m_Height = Other.m_Height; |
| 14 | m_Format = Other.m_Format; |
| 15 | m_pData = Other.m_pData; |
| 16 | Other.m_Width = 0; |
| 17 | Other.m_Height = 0; |
| 18 | Other.m_Format = FORMAT_UNDEFINED; |
| 19 | Other.m_pData = nullptr; |
| 20 | return *this; |
| 21 | } |
| 22 | |
| 23 | void CImageInfo::Free() |
| 24 | { |
| 25 | m_Width = 0; |
| 26 | m_Height = 0; |
| 27 | m_Format = FORMAT_UNDEFINED; |
| 28 | free(ptr: m_pData); |
| 29 | m_pData = nullptr; |
| 30 | } |
| 31 | |
| 32 | size_t CImageInfo::PixelSize(EImageFormat Format) |
| 33 | { |
| 34 | dbg_assert(Format != FORMAT_UNDEFINED, "Format undefined" ); |
| 35 | static const size_t s_aSizes[] = {3, 4, 1, 2}; |
| 36 | return s_aSizes[(int)Format]; |
| 37 | } |
| 38 | |
| 39 | const char *CImageInfo::FormatName(EImageFormat Format) |
| 40 | { |
| 41 | static const char *s_apNames[] = {"UNDEFINED" , "RGBA" , "RGB" , "R" , "RA" }; |
| 42 | return s_apNames[(int)Format + 1]; |
| 43 | } |
| 44 | |
| 45 | size_t CImageInfo::PixelSize() const |
| 46 | { |
| 47 | return PixelSize(Format: m_Format); |
| 48 | } |
| 49 | |
| 50 | const char *CImageInfo::FormatName() const |
| 51 | { |
| 52 | return FormatName(Format: m_Format); |
| 53 | } |
| 54 | |
| 55 | size_t CImageInfo::DataSize() const |
| 56 | { |
| 57 | return m_Width * m_Height * PixelSize(Format: m_Format); |
| 58 | } |
| 59 | |
| 60 | bool CImageInfo::DataEquals(const CImageInfo &Other) const |
| 61 | { |
| 62 | if(m_Width != Other.m_Width || m_Height != Other.m_Height || m_Format != Other.m_Format) |
| 63 | return false; |
| 64 | if(m_pData == nullptr && Other.m_pData == nullptr) |
| 65 | return true; |
| 66 | if(m_pData == nullptr || Other.m_pData == nullptr) |
| 67 | return false; |
| 68 | return mem_comp(a: m_pData, b: Other.m_pData, size: DataSize()) == 0; |
| 69 | } |
| 70 | |
| 71 | ColorRGBA CImageInfo::PixelColor(size_t x, size_t y) const |
| 72 | { |
| 73 | const size_t PixelStartIndex = (x + m_Width * y) * PixelSize(); |
| 74 | |
| 75 | ColorRGBA Color; |
| 76 | if(m_Format == FORMAT_R) |
| 77 | { |
| 78 | Color.r = Color.g = Color.b = 1.0f; |
| 79 | Color.a = m_pData[PixelStartIndex] / 255.0f; |
| 80 | } |
| 81 | else if(m_Format == FORMAT_RA) |
| 82 | { |
| 83 | Color.r = Color.g = Color.b = m_pData[PixelStartIndex] / 255.0f; |
| 84 | Color.a = m_pData[PixelStartIndex + 1] / 255.0f; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | Color.r = m_pData[PixelStartIndex + 0] / 255.0f; |
| 89 | Color.g = m_pData[PixelStartIndex + 1] / 255.0f; |
| 90 | Color.b = m_pData[PixelStartIndex + 2] / 255.0f; |
| 91 | if(m_Format == FORMAT_RGBA) |
| 92 | { |
| 93 | Color.a = m_pData[PixelStartIndex + 3] / 255.0f; |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | Color.a = 1.0f; |
| 98 | } |
| 99 | } |
| 100 | return Color; |
| 101 | } |
| 102 | |
| 103 | void CImageInfo::SetPixelColor(size_t x, size_t y, ColorRGBA Color) const |
| 104 | { |
| 105 | const size_t PixelStartIndex = (x + m_Width * y) * PixelSize(); |
| 106 | |
| 107 | if(m_Format == FORMAT_R) |
| 108 | { |
| 109 | m_pData[PixelStartIndex] = round_to_int(f: Color.a * 255.0f); |
| 110 | } |
| 111 | else if(m_Format == FORMAT_RA) |
| 112 | { |
| 113 | m_pData[PixelStartIndex] = round_to_int(f: (Color.r + Color.g + Color.b) / 3.0f * 255.0f); |
| 114 | m_pData[PixelStartIndex + 1] = round_to_int(f: Color.a * 255.0f); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | m_pData[PixelStartIndex + 0] = round_to_int(f: Color.r * 255.0f); |
| 119 | m_pData[PixelStartIndex + 1] = round_to_int(f: Color.g * 255.0f); |
| 120 | m_pData[PixelStartIndex + 2] = round_to_int(f: Color.b * 255.0f); |
| 121 | if(m_Format == FORMAT_RGBA) |
| 122 | { |
| 123 | m_pData[PixelStartIndex + 3] = round_to_int(f: Color.a * 255.0f); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void CImageInfo::CopyRectFrom(const CImageInfo &SrcImage, size_t SrcX, size_t SrcY, size_t Width, size_t Height, size_t DestX, size_t DestY) const |
| 129 | { |
| 130 | const size_t PixelSize = SrcImage.PixelSize(); |
| 131 | const size_t CopySize = Width * PixelSize; |
| 132 | for(size_t Y = 0; Y < Height; ++Y) |
| 133 | { |
| 134 | const size_t SrcOffset = ((SrcY + Y) * SrcImage.m_Width + SrcX) * PixelSize; |
| 135 | const size_t DestOffset = ((DestY + Y) * m_Width + DestX) * PixelSize; |
| 136 | mem_copy(dest: &m_pData[DestOffset], source: &SrcImage.m_pData[SrcOffset], size: CopySize); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | CImageInfo CImageInfo::DeepCopy() const |
| 141 | { |
| 142 | const size_t Size = DataSize(); |
| 143 | |
| 144 | CImageInfo Copy; |
| 145 | Copy.m_Width = m_Width; |
| 146 | Copy.m_Height = m_Height; |
| 147 | Copy.m_Format = m_Format; |
| 148 | Copy.m_pData = static_cast<uint8_t *>(malloc(size: Size)); |
| 149 | mem_copy(dest: Copy.m_pData, source: m_pData, size: Size); |
| 150 | return Copy; |
| 151 | } |
| 152 | |