Image.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright (c) 2006-2020 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. #pragma once
  21. // LOVE
  22. #include "common/config.h"
  23. #include "common/StringMap.h"
  24. #include "common/math.h"
  25. #include "image/ImageData.h"
  26. #include "image/CompressedImageData.h"
  27. #include "Texture.h"
  28. namespace love
  29. {
  30. namespace graphics
  31. {
  32. class Image : public Texture
  33. {
  34. public:
  35. static love::Type type;
  36. enum MipmapsType
  37. {
  38. MIPMAPS_NONE,
  39. MIPMAPS_DATA,
  40. MIPMAPS_GENERATED,
  41. };
  42. enum SettingType
  43. {
  44. SETTING_MIPMAPS,
  45. SETTING_LINEAR,
  46. SETTING_DPI_SCALE,
  47. SETTING_MAX_ENUM
  48. };
  49. struct Settings
  50. {
  51. bool mipmaps = false;
  52. bool linear = false;
  53. float dpiScale = 1.0f;
  54. };
  55. struct Slices
  56. {
  57. public:
  58. Slices(TextureType textype);
  59. void clear();
  60. void set(int slice, int mipmap, love::image::ImageDataBase *data);
  61. love::image::ImageDataBase *get(int slice, int mipmap) const;
  62. void add(love::image::CompressedImageData *cdata, int startslice, int startmip, bool addallslices, bool addallmips);
  63. int getSliceCount(int mip = 0) const;
  64. int getMipmapCount(int slice = 0) const;
  65. MipmapsType validate() const;
  66. TextureType getTextureType() const { return textureType; }
  67. private:
  68. TextureType textureType;
  69. // For 2D/Cube/2DArray texture types, each element in the data array has
  70. // an array of mipmap levels. For 3D texture types, each mipmap level
  71. // has an array of layers.
  72. std::vector<std::vector<StrongRef<love::image::ImageDataBase>>> data;
  73. }; // Slices
  74. virtual ~Image();
  75. void replacePixels(love::image::ImageDataBase *d, int slice, int mipmap, int x, int y, bool reloadmipmaps);
  76. void replacePixels(const void *data, size_t size, int slice, int mipmap, const Rect &rect, bool reloadmipmaps);
  77. bool isFormatLinear() const;
  78. bool isCompressed() const;
  79. MipmapsType getMipmapsType() const;
  80. static int imageCount;
  81. static bool getConstant(const char *in, SettingType &out);
  82. static bool getConstant(SettingType in, const char *&out);
  83. static const char *getConstant(SettingType in);
  84. static std::vector<std::string> getConstants(SettingType);
  85. protected:
  86. Image(const Slices &data, const Settings &settings);
  87. Image(TextureType textype, PixelFormat format, int width, int height, int slices, const Settings &settings);
  88. void uploadImageData(love::image::ImageDataBase *d, int level, int slice, int x, int y);
  89. virtual void uploadByteData(PixelFormat pixelformat, const void *data, size_t size, int level, int slice, const Rect &r, love::image::ImageDataBase *imgd = nullptr) = 0;
  90. virtual void generateMipmaps() = 0;
  91. // The settings used to initialize this Image.
  92. Settings settings;
  93. MipmapsType mipmapsType;
  94. bool sRGB;
  95. // True if the image wasn't able to be properly created and it had to fall
  96. // back to a default texture.
  97. bool usingDefaultTexture;
  98. private:
  99. Image(const Slices &data, const Settings &settings, bool validatedata);
  100. void init(PixelFormat fmt, int w, int h, const Settings &settings);
  101. static StringMap<SettingType, SETTING_MAX_ENUM>::Entry settingTypeEntries[];
  102. static StringMap<SettingType, SETTING_MAX_ENUM> settingTypes;
  103. }; // Image
  104. } // graphics
  105. } // love