IImage.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * IImage.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../lib/filesystem/ResourcePath.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class PlayerColor;
  14. class Rect;
  15. class Point;
  16. class ColorRGBA;
  17. VCMI_LIB_NAMESPACE_END
  18. struct SDL_Surface;
  19. struct SDL_Palette;
  20. class ColorFilter;
  21. class ISharedImage;
  22. /// Defines which blit method will be selected when image is used for rendering
  23. enum class EImageBlitMode : uint8_t
  24. {
  25. /// Preferred for images that don't need any background
  26. /// Indexed or RGBA: Image can have no transparency and can be only used as background
  27. OPAQUE,
  28. /// Preferred for images that may need transparency
  29. /// Indexed: Image can have only a single color as transparency and has no semi-transparent areas
  30. /// RGBA: full alpha transparency range, e.g. shadows
  31. COLORKEY,
  32. /// Full transparency including shadow, but treated as a single image
  33. /// Indexed: Image can have alpha transparency, e.g. shadow
  34. /// RGBA: full alpha transparency range, e.g. shadows
  35. /// Upscaled form: single image, no option to display shadow separately
  36. SIMPLE,
  37. /// RGBA, may consist from 2 separate parts: base and shadow, overlay not preset or treated as part of body
  38. WITH_SHADOW,
  39. /// RGBA, may consist from 3 separate parts: base, shadow, and overlay
  40. WITH_SHADOW_AND_SELECTION,
  41. WITH_SHADOW_AND_FLAG_COLOR,
  42. /// RGBA, contains only body, with shadow and overlay disabled
  43. ONLY_BODY_HIDE_SELECTION,
  44. ONLY_BODY_HIDE_FLAG_COLOR,
  45. /// RGBA, contains only body, with shadow disabled and overlay treated as part of body
  46. ONLY_BODY_IGNORE_OVERLAY,
  47. /// RGBA, contains only shadow
  48. ONLY_SHADOW_HIDE_SELECTION,
  49. ONLY_SHADOW_HIDE_FLAG_COLOR,
  50. /// RGBA, contains only overlay
  51. ONLY_SELECTION,
  52. ONLY_FLAG_COLOR,
  53. };
  54. enum class EScalingAlgorithm : int8_t
  55. {
  56. NEAREST,
  57. BILINEAR,
  58. XBRZ_OPAQUE, // xbrz, image edges are considered to have same color as pixel inside image. Only for integer scaling
  59. XBRZ_ALPHA // xbrz, image edges are considered to be transparent. Only for integer scaling
  60. };
  61. /// Base class for images for use in client code.
  62. /// This class represents current state of image, with potential transformations applied, such as player coloring
  63. class IImage
  64. {
  65. public:
  66. //draws image on surface "where" at position
  67. virtual void draw(SDL_Surface * where, const Point & pos, const Rect * src, int scalingFactor) const = 0;
  68. virtual void scaleTo(const Point & size, EScalingAlgorithm algorithm) = 0;
  69. virtual void exportBitmap(const boost::filesystem::path & path) const = 0;
  70. //Change palette to specific player
  71. virtual void playerColored(const PlayerColor & player) = 0;
  72. //test transparency of specific pixel
  73. virtual bool isTransparent(const Point & coords) const = 0;
  74. virtual Rect contentRect() const = 0;
  75. virtual Point dimensions() const = 0;
  76. int width() const;
  77. int height() const;
  78. //only indexed bitmaps, 16 colors maximum
  79. virtual void shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove) = 0;
  80. virtual void adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask) = 0;
  81. virtual void setAlpha(uint8_t value) = 0;
  82. //only indexed bitmaps with 7 special colors
  83. virtual void setOverlayColor(const ColorRGBA & color) = 0;
  84. virtual ~IImage() = default;
  85. };
  86. /// Base class for image data, mostly for internal use
  87. /// Represents unmodified pixel data, usually loaded from file
  88. /// This image can be shared between multiple image handlers (IImage instances)
  89. class ISharedImage
  90. {
  91. public:
  92. virtual Point dimensions() const = 0;
  93. virtual void exportBitmap(const boost::filesystem::path & path, SDL_Palette * palette) const = 0;
  94. virtual bool isTransparent(const Point & coords) const = 0;
  95. virtual Rect contentRect() const = 0;
  96. virtual void scaledDraw(SDL_Surface * where, SDL_Palette * palette, const Point & scaling, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const = 0;
  97. virtual void draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const = 0;
  98. /// Returns true if this image is still loading and can't be used
  99. virtual bool isLoading() const = 0;
  100. virtual ~ISharedImage() = default;
  101. virtual const SDL_Palette * getPalette() const = 0;
  102. [[nodiscard]] virtual std::shared_ptr<const ISharedImage> horizontalFlip() const = 0;
  103. [[nodiscard]] virtual std::shared_ptr<const ISharedImage> verticalFlip() const = 0;
  104. [[nodiscard]] virtual std::shared_ptr<const ISharedImage> scaleInteger(int factor, SDL_Palette * palette, EImageBlitMode blitMode) const = 0;
  105. [[nodiscard]] virtual std::shared_ptr<const ISharedImage> scaleTo(const Point & size, SDL_Palette * palette) const = 0;
  106. };