IImage.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. GRAYSCALE_BODY_HIDE_SELECTION,
  44. ONLY_BODY_HIDE_SELECTION,
  45. ONLY_BODY_HIDE_FLAG_COLOR,
  46. /// RGBA, contains only body, with shadow disabled and overlay treated as part of body
  47. ONLY_BODY_IGNORE_OVERLAY,
  48. /// RGBA, contains only shadow
  49. ONLY_SHADOW_HIDE_SELECTION,
  50. ONLY_SHADOW_HIDE_FLAG_COLOR,
  51. /// RGBA, contains only overlay
  52. ONLY_SELECTION,
  53. ONLY_FLAG_COLOR,
  54. };
  55. enum class EScalingAlgorithm : int8_t
  56. {
  57. NEAREST,
  58. BILINEAR,
  59. XBRZ_OPAQUE, // xbrz, image edges are considered to have same color as pixel inside image. Only for integer scaling
  60. XBRZ_ALPHA // xbrz, image edges are considered to be transparent. Only for integer scaling
  61. };
  62. /// Base class for images for use in client code.
  63. /// This class represents current state of image, with potential transformations applied, such as player coloring
  64. class IImage
  65. {
  66. public:
  67. //draws image on surface "where" at position
  68. virtual void draw(SDL_Surface * where, const Point & pos, const Rect * src, int scalingFactor) const = 0;
  69. virtual void scaleTo(const Point & size, EScalingAlgorithm algorithm) = 0;
  70. virtual void exportBitmap(const boost::filesystem::path & path) const = 0;
  71. //Change palette to specific player
  72. virtual void playerColored(const PlayerColor & player) = 0;
  73. //test transparency of specific pixel
  74. virtual bool isTransparent(const Point & coords) const = 0;
  75. virtual Rect contentRect() const = 0;
  76. virtual Point dimensions() const = 0;
  77. int width() const;
  78. int height() const;
  79. //only indexed bitmaps, 16 colors maximum
  80. virtual void shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove) = 0;
  81. virtual void adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask) = 0;
  82. virtual void setAlpha(uint8_t value) = 0;
  83. virtual void setOverlayColor(const ColorRGBA & color) = 0;
  84. virtual void setEffectColor(const ColorRGBA & color) = 0;
  85. virtual ~IImage() = default;
  86. };
  87. /// Base class for image data, mostly for internal use
  88. /// Represents unmodified pixel data, usually loaded from file
  89. /// This image can be shared between multiple image handlers (IImage instances)
  90. class ISharedImage
  91. {
  92. public:
  93. virtual Point dimensions() const = 0;
  94. virtual void exportBitmap(const boost::filesystem::path & path, SDL_Palette * palette) const = 0;
  95. virtual bool isTransparent(const Point & coords) const = 0;
  96. virtual Rect contentRect() const = 0;
  97. 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;
  98. 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;
  99. /// Returns true if this image is still loading and can't be used
  100. virtual bool isLoading() const = 0;
  101. /// When disabled upscaling needs to be done in sync (e.g. because there is no 1x base image)
  102. virtual void setAsyncUpscale(bool on) = 0;
  103. virtual bool getAsyncUpscale() const = 0;
  104. virtual ~ISharedImage() = default;
  105. virtual const SDL_Palette * getPalette() const = 0;
  106. [[nodiscard]] virtual std::shared_ptr<const ISharedImage> horizontalFlip() const = 0;
  107. [[nodiscard]] virtual std::shared_ptr<const ISharedImage> verticalFlip() const = 0;
  108. [[nodiscard]] virtual std::shared_ptr<const ISharedImage> scaleInteger(int factor, SDL_Palette * palette, EImageBlitMode blitMode) const = 0;
  109. [[nodiscard]] virtual std::shared_ptr<const ISharedImage> scaleTo(const Point & size, SDL_Palette * palette) const = 0;
  110. };