| 1 | /** |
| 2 | * Copyright (c) 2006-2023 LOVE Development Team |
| 3 | * |
| 4 | * This software is provided 'as-is', without any express or implied |
| 5 | * warranty. In no event will the authors be held liable for any damages |
| 6 | * arising from the use of this software. |
| 7 | * |
| 8 | * Permission is granted to anyone to use this software for any purpose, |
| 9 | * including commercial applications, and to alter it and redistribute it |
| 10 | * freely, subject to the following restrictions: |
| 11 | * |
| 12 | * 1. The origin of this software must not be misrepresented; you must not |
| 13 | * claim that you wrote the original software. If you use this software |
| 14 | * in a product, an acknowledgment in the product documentation would be |
| 15 | * appreciated but is not required. |
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | * misrepresented as being the original software. |
| 18 | * 3. This notice may not be removed or altered from any source distribution. |
| 19 | **/ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | // LOVE |
| 24 | #include "common/config.h" |
| 25 | #include "common/StringMap.h" |
| 26 | #include "common/math.h" |
| 27 | #include "image/ImageData.h" |
| 28 | #include "image/CompressedImageData.h" |
| 29 | #include "Texture.h" |
| 30 | |
| 31 | namespace love |
| 32 | { |
| 33 | namespace graphics |
| 34 | { |
| 35 | |
| 36 | class Image : public Texture |
| 37 | { |
| 38 | public: |
| 39 | |
| 40 | static love::Type type; |
| 41 | |
| 42 | enum MipmapsType |
| 43 | { |
| 44 | MIPMAPS_NONE, |
| 45 | MIPMAPS_DATA, |
| 46 | MIPMAPS_GENERATED, |
| 47 | }; |
| 48 | |
| 49 | enum SettingType |
| 50 | { |
| 51 | SETTING_MIPMAPS, |
| 52 | SETTING_LINEAR, |
| 53 | SETTING_DPI_SCALE, |
| 54 | SETTING_MAX_ENUM |
| 55 | }; |
| 56 | |
| 57 | struct Settings |
| 58 | { |
| 59 | bool mipmaps = false; |
| 60 | bool linear = false; |
| 61 | float dpiScale = 1.0f; |
| 62 | }; |
| 63 | |
| 64 | struct Slices |
| 65 | { |
| 66 | public: |
| 67 | |
| 68 | Slices(TextureType textype); |
| 69 | |
| 70 | void clear(); |
| 71 | void set(int slice, int mipmap, love::image::ImageDataBase *data); |
| 72 | love::image::ImageDataBase *get(int slice, int mipmap) const; |
| 73 | |
| 74 | void add(love::image::CompressedImageData *cdata, int startslice, int startmip, bool addallslices, bool addallmips); |
| 75 | |
| 76 | int getSliceCount(int mip = 0) const; |
| 77 | int getMipmapCount(int slice = 0) const; |
| 78 | |
| 79 | MipmapsType validate() const; |
| 80 | |
| 81 | TextureType getTextureType() const { return textureType; } |
| 82 | |
| 83 | private: |
| 84 | |
| 85 | TextureType textureType; |
| 86 | |
| 87 | // For 2D/Cube/2DArray texture types, each element in the data array has |
| 88 | // an array of mipmap levels. For 3D texture types, each mipmap level |
| 89 | // has an array of layers. |
| 90 | std::vector<std::vector<StrongRef<love::image::ImageDataBase>>> data; |
| 91 | |
| 92 | }; // Slices |
| 93 | |
| 94 | virtual ~Image(); |
| 95 | |
| 96 | void replacePixels(love::image::ImageDataBase *d, int slice, int mipmap, int x, int y, bool reloadmipmaps); |
| 97 | void replacePixels(const void *data, size_t size, int slice, int mipmap, const Rect &rect, bool reloadmipmaps); |
| 98 | |
| 99 | bool isFormatLinear() const; |
| 100 | bool isCompressed() const; |
| 101 | MipmapsType getMipmapsType() const; |
| 102 | |
| 103 | static int imageCount; |
| 104 | |
| 105 | static bool getConstant(const char *in, SettingType &out); |
| 106 | static bool getConstant(SettingType in, const char *&out); |
| 107 | static const char *getConstant(SettingType in); |
| 108 | static std::vector<std::string> getConstants(SettingType); |
| 109 | |
| 110 | protected: |
| 111 | |
| 112 | Image(const Slices &data, const Settings &settings); |
| 113 | Image(TextureType textype, PixelFormat format, int width, int height, int slices, const Settings &settings); |
| 114 | |
| 115 | void uploadImageData(love::image::ImageDataBase *d, int level, int slice, int x, int y); |
| 116 | virtual void uploadByteData(PixelFormat pixelformat, const void *data, size_t size, int level, int slice, const Rect &r) = 0; |
| 117 | |
| 118 | virtual void generateMipmaps() = 0; |
| 119 | |
| 120 | // The settings used to initialize this Image. |
| 121 | Settings settings; |
| 122 | |
| 123 | Slices data; |
| 124 | |
| 125 | MipmapsType mipmapsType; |
| 126 | bool sRGB; |
| 127 | |
| 128 | // True if the image wasn't able to be properly created and it had to fall |
| 129 | // back to a default texture. |
| 130 | bool usingDefaultTexture; |
| 131 | |
| 132 | private: |
| 133 | |
| 134 | Image(const Slices &data, const Settings &settings, bool validatedata); |
| 135 | |
| 136 | void init(PixelFormat fmt, int w, int h, const Settings &settings); |
| 137 | |
| 138 | static StringMap<SettingType, SETTING_MAX_ENUM>::Entry settingTypeEntries[]; |
| 139 | static StringMap<SettingType, SETTING_MAX_ENUM> settingTypes; |
| 140 | |
| 141 | }; // Image |
| 142 | |
| 143 | } // graphics |
| 144 | } // love |
| 145 | |