| 1 | #ifndef ENGINE_IMAGE_H |
| 2 | #define ENGINE_IMAGE_H |
| 3 | |
| 4 | #include <base/color.h> |
| 5 | |
| 6 | #include <cstdint> |
| 7 | |
| 8 | /** |
| 9 | * Represents an image that has been loaded into main memory. |
| 10 | */ |
| 11 | class CImageInfo |
| 12 | { |
| 13 | private: |
| 14 | bool m_IsAllocated = false; |
| 15 | |
| 16 | public: |
| 17 | /** |
| 18 | * Defines the format of image data. |
| 19 | */ |
| 20 | enum EImageFormat |
| 21 | { |
| 22 | FORMAT_UNDEFINED = -1, |
| 23 | FORMAT_RGB = 0, |
| 24 | FORMAT_RGBA = 1, |
| 25 | FORMAT_R = 2, |
| 26 | FORMAT_RA = 3, |
| 27 | }; |
| 28 | |
| 29 | CImageInfo() = default; |
| 30 | |
| 31 | /** |
| 32 | * Move assignment. |
| 33 | */ |
| 34 | CImageInfo &operator=(CImageInfo &&Other); |
| 35 | |
| 36 | /** |
| 37 | * Move constructor. |
| 38 | */ |
| 39 | CImageInfo(CImageInfo &&Other); |
| 40 | |
| 41 | /** |
| 42 | * Forbid copies. |
| 43 | */ |
| 44 | CImageInfo(const CImageInfo &) = delete; |
| 45 | |
| 46 | /** |
| 47 | * Forbid copies. |
| 48 | */ |
| 49 | CImageInfo &operator=(const CImageInfo &) = delete; |
| 50 | |
| 51 | /** |
| 52 | * Destructor, which automatically frees memory. |
| 53 | */ |
| 54 | ~CImageInfo(); |
| 55 | |
| 56 | /** |
| 57 | * Width of the image. |
| 58 | */ |
| 59 | size_t m_Width = 0; |
| 60 | |
| 61 | /** |
| 62 | * Height of the image. |
| 63 | */ |
| 64 | size_t m_Height = 0; |
| 65 | |
| 66 | /** |
| 67 | * Format of the image. |
| 68 | * |
| 69 | * @see EImageFormat |
| 70 | */ |
| 71 | EImageFormat m_Format = FORMAT_UNDEFINED; |
| 72 | |
| 73 | /** |
| 74 | * Pointer to the image data. |
| 75 | */ |
| 76 | uint8_t *m_pData = nullptr; |
| 77 | |
| 78 | /** |
| 79 | * Creates the image data, does not zero it. |
| 80 | */ |
| 81 | void Allocate(); |
| 82 | |
| 83 | /** |
| 84 | * Creates the image data and fills it with zero. |
| 85 | */ |
| 86 | void AllocateFillZero(); |
| 87 | |
| 88 | /** |
| 89 | * Frees the image data and clears all info. |
| 90 | */ |
| 91 | void Free(); |
| 92 | |
| 93 | /** |
| 94 | * Returns the pixel size in bytes for the given image format. |
| 95 | * |
| 96 | * @param Format Image format, must not be `FORMAT_UNDEFINED`. |
| 97 | * |
| 98 | * @return Size of one pixel with the given image format. |
| 99 | */ |
| 100 | static size_t PixelSize(EImageFormat Format); |
| 101 | |
| 102 | /** |
| 103 | * Returns a readable name for the given image format. |
| 104 | * |
| 105 | * @param Format Image format. |
| 106 | * |
| 107 | * @return Readable name for the given image format. |
| 108 | */ |
| 109 | static const char *FormatName(EImageFormat Format); |
| 110 | |
| 111 | /** |
| 112 | * Returns the pixel size in bytes for the format of this image. |
| 113 | * |
| 114 | * @return Size of one pixel with the format of this image. |
| 115 | * |
| 116 | * @remark The format must not be `FORMAT_UNDEFINED`. |
| 117 | */ |
| 118 | size_t PixelSize() const; |
| 119 | |
| 120 | /** |
| 121 | * Returns a readable name for the format of this image. |
| 122 | * |
| 123 | * @return Readable name for the format of this image. |
| 124 | */ |
| 125 | const char *FormatName() const; |
| 126 | |
| 127 | /** |
| 128 | * Returns the size of the data, as derived from the width, height and pixel size. |
| 129 | * |
| 130 | * @return Expected size of the image data. |
| 131 | */ |
| 132 | size_t DataSize() const; |
| 133 | |
| 134 | /** |
| 135 | * Returns whether this image is equal to the given image |
| 136 | * in width, height, format and data. |
| 137 | * |
| 138 | * @param Other The image to compare with. |
| 139 | * |
| 140 | * @return `true` if the images are identical, `false` otherwise. |
| 141 | */ |
| 142 | bool DataEquals(const CImageInfo &Other) const; |
| 143 | |
| 144 | /** |
| 145 | * Returns the color of the pixel at the specified position. |
| 146 | * |
| 147 | * @param x The x-coordinate to read from. |
| 148 | * @param y The y-coordinate to read from. |
| 149 | * |
| 150 | * @return Pixel color converted to normalized RGBA. |
| 151 | */ |
| 152 | ColorRGBA PixelColor(size_t x, size_t y) const; |
| 153 | |
| 154 | /** |
| 155 | * Sets the color of the pixel at the specified position. |
| 156 | * |
| 157 | * @param x The x-coordinate to write to. |
| 158 | * @param y The y-coordinate to write to. |
| 159 | * @param Color The normalized RGBA color to write. |
| 160 | */ |
| 161 | void SetPixelColor(size_t x, size_t y, ColorRGBA Color) const; |
| 162 | |
| 163 | /** |
| 164 | * Copies a rectangle of image data from the given image to this image. |
| 165 | * |
| 166 | * @param SrcImage The image to copy data from. |
| 167 | * @param SrcX The x-offset in the source image. |
| 168 | * @param SrcY The y-offset in the source image. |
| 169 | * @param Width The width of the rectangle to copy. |
| 170 | * @param Height The height of the rectangle to copy. |
| 171 | * @param DestX The x-offset in the destination image (this). |
| 172 | * @param DestY The y-offset in the destination image (this). |
| 173 | */ |
| 174 | void CopyRectFrom(const CImageInfo &SrcImage, size_t SrcX, size_t SrcY, size_t Width, size_t Height, size_t DestX, size_t DestY) const; |
| 175 | |
| 176 | /** |
| 177 | * Creates a complete, independent copy of this image and its data. |
| 178 | * |
| 179 | * @return Copy of this image with copied data. |
| 180 | * |
| 181 | * @remark This image must be valid. |
| 182 | * @remark The copied image must be freed separately. |
| 183 | */ |
| 184 | CImageInfo DeepCopy() const; |
| 185 | }; |
| 186 | |
| 187 | #endif |
| 188 | |