| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <cassert> |
| 24 |
#include <cstdint> |
| 25 |
#include <memory> |
| 26 |
#include <utility> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include "math_base.h" |
| 30 |
|
| 31 |
namespace fheroes2 |
| 32 |
{ |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class Image |
| 37 |
{ |
| 38 |
public: |
| 39 |
Image() = default; |
| 40 |
|
| 41 |
Image( const int32_t width, const int32_t height ) |
| 42 |
{ |
| 43 |
Image::resize( width, height ); |
| 44 |
} |
| 45 |
|
| 46 |
Image( const Image & image ) |
| 47 |
{ |
| 48 |
copy( image ); |
| 49 |
} |
| 50 |
|
| 51 |
Image( Image && image ) noexcept; |
| 52 |
|
| 53 |
virtual ~Image() = default; |
| 54 |
|
| 55 |
Image & operator=( const Image & image ); |
| 56 |
Image & operator=( Image && image ) noexcept; |
| 57 |
|
| 58 |
virtual void resize( const int32_t width_, const int32_t height_ ); |
| 59 |
|
| 60 |
|
| 61 |
int32_t width() const |
| 62 |
{ |
| 63 |
return _width; |
| 64 |
} |
| 65 |
|
| 66 |
int32_t height() const |
| 67 |
{ |
| 68 |
return _height; |
| 69 |
} |
| 70 |
|
| 71 |
virtual uint8_t * image(); |
| 72 |
|
| 73 |
virtual const uint8_t * image() const; |
| 74 |
|
| 75 |
uint8_t * transform() |
| 76 |
{ |
| 77 |
|
| 78 |
assert( !_singleLayer ); |
| 79 |
|
| 80 |
return _singleLayer ? nullptr : _data.get() + width() * height(); |
| 81 |
} |
| 82 |
|
| 83 |
const uint8_t * transform() const |
| 84 |
{ |
| 85 |
|
| 86 |
assert( !_singleLayer ); |
| 87 |
|
| 88 |
return _singleLayer ? nullptr : _data.get() + width() * height(); |
| 89 |
} |
| 90 |
|
| 91 |
bool empty() const |
| 92 |
{ |
| 93 |
return !_data; |
| 94 |
} |
| 95 |
|
| 96 |
void reset(); |
| 97 |
|
| 98 |
void clear(); |
| 99 |
|
| 100 |
|
| 101 |
void fill( const uint8_t value ); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
bool singleLayer() const |
| 106 |
{ |
| 107 |
return _singleLayer; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
void _disableTransformLayer() |
| 113 |
{ |
| 114 |
_singleLayer = true; |
| 115 |
} |
| 116 |
|
| 117 |
private: |
| 118 |
void copy( const Image & image ); |
| 119 |
|
| 120 |
int32_t _width{ 0 }; |
| 121 |
int32_t _height{ 0 }; |
| 122 |
std::unique_ptr<uint8_t[]> _data; |
| 123 |
|
| 124 |
|
| 125 |
bool _singleLayer{ false }; |
| 126 |
}; |
| 127 |
|
| 128 |
class Sprite : public Image |
| 129 |
{ |
| 130 |
public: |
| 131 |
Sprite() = default; |
| 132 |
Sprite( const int32_t width, const int32_t height, const int32_t x = 0, const int32_t y = 0 ) |
| 133 |
: Image( width, height ) |
| 134 |
, _x( x ) |
| 135 |
, _y( y ) |
| 136 |
{ |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
explicit Sprite( const Image & image, const int32_t x = 0, const int32_t y = 0 ) |
| 141 |
: Image( image ) |
| 142 |
, _x( x ) |
| 143 |
, _y( y ) |
| 144 |
{ |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
explicit Sprite( Image && image, const int32_t x = 0, const int32_t y = 0 ) |
| 149 |
: Image( std::move( image ) ) |
| 150 |
, _x( x ) |
| 151 |
, _y( y ) |
| 152 |
{ |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
Sprite( const Sprite & sprite ) = default; |
| 157 |
Sprite( Sprite && sprite ) noexcept; |
| 158 |
|
| 159 |
~Sprite() override = default; |
| 160 |
|
| 161 |
Sprite & operator=( const Sprite & sprite ); |
| 162 |
Sprite & operator=( Sprite && sprite ) noexcept; |
| 163 |
|
| 164 |
Sprite & operator=( Image && image ) noexcept; |
| 165 |
|
| 166 |
int32_t x() const |
| 167 |
{ |
| 168 |
return _x; |
| 169 |
} |
| 170 |
|
| 171 |
int32_t y() const |
| 172 |
{ |
| 173 |
return _y; |
| 174 |
} |
| 175 |
|
| 176 |
virtual void setPosition( const int32_t x_, const int32_t y_ ); |
| 177 |
|
| 178 |
private: |
| 179 |
int32_t _x{ 0 }; |
| 180 |
int32_t _y{ 0 }; |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
class ImageRestorer final |
| 185 |
{ |
| 186 |
public: |
| 187 |
explicit ImageRestorer( Image & image ); |
| 188 |
ImageRestorer( Image & image, const int32_t x_, const int32_t y_, const int32_t width, const int32_t height ); |
| 189 |
|
| 190 |
ImageRestorer( const ImageRestorer & ) = delete; |
| 191 |
ImageRestorer & operator=( const ImageRestorer & ) = delete; |
| 192 |
|
| 193 |
|
| 194 |
~ImageRestorer() |
| 195 |
{ |
| 196 |
if ( !_isRestored ) { |
| 197 |
restore(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
void update( const int32_t x_, const int32_t y_, const int32_t width, const int32_t height ); |
| 202 |
|
| 203 |
int32_t x() const |
| 204 |
{ |
| 205 |
return _x; |
| 206 |
} |
| 207 |
|
| 208 |
int32_t y() const |
| 209 |
{ |
| 210 |
return _y; |
| 211 |
} |
| 212 |
|
| 213 |
int32_t width() const |
| 214 |
{ |
| 215 |
return _width; |
| 216 |
} |
| 217 |
|
| 218 |
int32_t height() const |
| 219 |
{ |
| 220 |
return _height; |
| 221 |
} |
| 222 |
|
| 223 |
Rect rect() const |
| 224 |
{ |
| 225 |
return { _x, _y, _width, _height }; |
| 226 |
} |
| 227 |
|
| 228 |
void restore(); |
| 229 |
|
| 230 |
void reset() |
| 231 |
{ |
| 232 |
_isRestored = true; |
| 233 |
} |
| 234 |
|
| 235 |
private: |
| 236 |
Image & _image; |
| 237 |
Image _copy; |
| 238 |
|
| 239 |
int32_t _x{ 0 }; |
| 240 |
int32_t _y{ 0 }; |
| 241 |
int32_t _width{ 0 }; |
| 242 |
int32_t _height{ 0 }; |
| 243 |
|
| 244 |
void _updateRoi(); |
| 245 |
|
| 246 |
bool _isRestored{ false }; |
| 247 |
}; |
| 248 |
|
| 249 |
|
| 250 |
void addGradientShadow( const Sprite & in, Image & out, const Point & outPos, const Point & shadowOffset ); |
| 251 |
void addGradientShadowForArea( Image & out, const Point & outPos, const int32_t areaWidth, const int32_t areaHeight, const int32_t shadowOffset ); |
| 252 |
|
| 253 |
|
| 254 |
Sprite addShadow( const Sprite & in, const Point & shadowOffset, const uint8_t transformId ); |
| 255 |
|
| 256 |
|
| 257 |
void AlphaBlit( const Image & in, Image & out, const uint8_t alphaValue, const bool flip = false ); |
| 258 |
void AlphaBlit( const Image & in, Image & out, int32_t outX, int32_t outY, const uint8_t alphaValue, const bool flip = false ); |
| 259 |
void AlphaBlit( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, const uint8_t alphaValue, |
| 260 |
const bool flip = false ); |
| 261 |
|
| 262 |
|
| 263 |
void ApplyPalette( Image & image, const std::vector<uint8_t> & palette ); |
| 264 |
void ApplyPalette( const Image & in, Image & out, const std::vector<uint8_t> & palette ); |
| 265 |
void ApplyPalette( Image & image, const uint8_t paletteId ); |
| 266 |
void ApplyPalette( const Image & in, Image & out, const uint8_t paletteId ); |
| 267 |
void ApplyPalette( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, uint8_t paletteId ); |
| 268 |
void ApplyPalette( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, |
| 269 |
const std::vector<uint8_t> & palette ); |
| 270 |
|
| 271 |
void ApplyAlpha( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, const uint8_t alpha ); |
| 272 |
|
| 273 |
void ApplyTransform( Image & image, int32_t x, int32_t y, int32_t width, int32_t height, const uint8_t transformId ); |
| 274 |
|
| 275 |
|
| 276 |
void Blit( const Image & in, Image & out, const bool flip = false ); |
| 277 |
void Blit( const Image & in, Image & out, const Rect & outRoi, const bool flip = false ); |
| 278 |
void Blit( const Image & in, Image & out, const int32_t outX, const int32_t outY, const bool flip = false ); |
| 279 |
void Blit( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, const bool flip = false ); |
| 280 |
|
| 281 |
|
| 282 |
void Blit( const Image & in, const Point & inPos, Image & out, const Point & outPos, const Size & size, bool flip = false ); |
| 283 |
|
| 284 |
void Copy( const Image & in, Image & out ); |
| 285 |
void Copy( const Image & in, const int32_t inX, const int32_t inY, Image & out, const Rect & outRoi ); |
| 286 |
void Copy( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height ); |
| 287 |
|
| 288 |
|
| 289 |
void copyTransformLayer( const Image & in, Image & out ); |
| 290 |
|
| 291 |
void copyTransformLayer( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height ); |
| 292 |
|
| 293 |
Sprite CreateContour( const Image & image, const uint8_t value ); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
void CreateDitheringTransition( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, |
| 298 |
const bool isVertical, const bool isReverse ); |
| 299 |
|
| 300 |
Sprite Crop( const Image & image, int32_t x, int32_t y, int32_t width, int32_t height ); |
| 301 |
|
| 302 |
|
| 303 |
void DrawBorder( Image & image, const uint8_t value, const uint32_t skipFactor = 0 ); |
| 304 |
|
| 305 |
|
| 306 |
void DrawLine( Image & image, const Point & start, const Point & end, const uint8_t value, const Rect & roi = Rect() ); |
| 307 |
|
| 308 |
void DrawRect( Image & image, const Rect & roi, const uint8_t value ); |
| 309 |
|
| 310 |
void DivideImageBySquares( const Point & spriteOffset, const Image & original, const int32_t squareSize, std::vector<Point> & outputSquareId, |
| 311 |
std::vector<std::pair<Point, Rect>> & outputImageInfo ); |
| 312 |
|
| 313 |
|
| 314 |
Image ExtractCommonPattern( const std::vector<const Image *> & input ); |
| 315 |
|
| 316 |
|
| 317 |
void Fill( Image & image, int32_t x, int32_t y, int32_t width, int32_t height, const uint8_t colorId ); |
| 318 |
|
| 319 |
void FillTransform( Image & image, int32_t x, int32_t y, int32_t width, int32_t height, const uint8_t transformId ); |
| 320 |
|
| 321 |
Image FilterOnePixelNoise( const Image & input ); |
| 322 |
|
| 323 |
bool FitToRoi( const Image & in, Point & inPos, const Image & out, Point & outPos, Size & outputSize, const Rect & outputRoi ); |
| 324 |
|
| 325 |
Image Flip( const Image & in, const bool horizontally, const bool vertically ); |
| 326 |
void Flip( const Image & in, int32_t inX, int32_t inY, Image & out, int32_t outX, int32_t outY, int32_t width, int32_t height, const bool horizontally, |
| 327 |
const bool vertically ); |
| 328 |
|
| 329 |
|
| 330 |
Rect GetActiveROI( const Image & image, const uint8_t minTransformValue = 6 ); |
| 331 |
|
| 332 |
|
| 333 |
uint8_t GetColorId( const uint8_t red, const uint8_t green, const uint8_t blue ); |
| 334 |
|
| 335 |
Sprite makeShadow( const Sprite & in, const Point & shadowOffset, const uint8_t transformId ); |
| 336 |
|
| 337 |
|
| 338 |
void ReplaceColorId( Image & image, const uint8_t oldColorId, const uint8_t newColorId ); |
| 339 |
|
| 340 |
|
| 341 |
void ReplaceColorIdByTransformId( Image & image, const uint8_t colorId, const uint8_t transformId ); |
| 342 |
|
| 343 |
|
| 344 |
void ReplaceTransformIdByColorId( Image & image, const uint8_t transformId, const uint8_t colorId ); |
| 345 |
|
| 346 |
void Resize( const Image & in, Image & out ); |
| 347 |
|
| 348 |
void Resize( const Image & in, const int32_t inX, const int32_t inY, const int32_t widthRoiIn, const int32_t heightRoiIn, Image & out, const int32_t outX, |
| 349 |
const int32_t outY, const int32_t widthRoiOut, const int32_t heightRoiOut ); |
| 350 |
|
| 351 |
|
| 352 |
void SetPixel( Image & image, const int32_t x, const int32_t y, const uint8_t value ); |
| 353 |
|
| 354 |
void SetPixel( Image & image, const std::vector<Point> & points, const uint8_t value ); |
| 355 |
|
| 356 |
|
| 357 |
void SetTransformPixel( Image & image, const int32_t x, const int32_t y, const uint8_t value ); |
| 358 |
|
| 359 |
Image Stretch( const Image & in, int32_t inX, int32_t inY, int32_t widthIn, int32_t heightIn, const int32_t widthOut, const int32_t heightOut ); |
| 360 |
|
| 361 |
void SubpixelResize( const Image & in, Image & out ); |
| 362 |
|
| 363 |
void SubpixelResize( const Image & in, const int32_t inX, const int32_t inY, const int32_t widthRoiIn, const int32_t heightRoiIn, Image & out, const int32_t outX, |
| 364 |
const int32_t outY, const int32_t widthRoiOut, const int32_t heightRoiOut ); |
| 365 |
|
| 366 |
void Transpose( const Image & in, Image & out ); |
| 367 |
|
| 368 |
void updateShadow( Image & image, const Point & shadowOffset, const uint8_t transformId, const bool connectCorners ); |
| 369 |
} |