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