Debian Codegraph @

src/engine/image.h

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2020 - 2025 *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
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 // Image always contains an image layer and if image is not a single-layer then also a transform layer.
34 // - image layer contains visible pixels which are copy to a destination image
35 // - transform layer is used to apply some transformation to an image on which we draw the current one. For example, shadowing
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 // It's safe to cast to uint32_t as width and height are always >= 0
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 // Why do you want to get transform layer from the single-layer image?
78 assert( !_singleLayer );
79
80 return _singleLayer ? nullptr : _data.get() + width() * height();
81 }
82
83 const uint8_t * transform() const
84 {
85 // Why do you want to get transform layer from the single-layer image?
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(); // makes image fully transparent (transform layer is set to 1)
97
98 void clear(); // makes the image empty
99
100 // Fill 'image' layer with given value, setting 'transform' layer to 0.
101 void fill( const uint8_t value );
102
103 // This is an optional indicator for image processing functions.
104 // The whole image still consists of 2 layers but transform layer might be ignored in computations
105 bool singleLayer() const
106 {
107 return _singleLayer;
108 }
109
110 // BE CAREFUL! This method disables transform layer usage. Use only for display / video related images which are for end rendering purposes!
111 // The name of this method starts from _ on purpose to do not mix with other public methods.
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; // holds 2 image layers
123
124 // Only for images which are not used for any other operations except displaying on screen.
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 // Do nothing.
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 // Do nothing.
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 // Do nothing.
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 // This class is used in situations when we draw a window within another window
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 // Restores the original image if necessary, see the implementation for details
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 // Apply shadow that gradually reduces strength using 'in' image shape. Shadow is applied to the 'out' image.
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 // Generates a new image with a shadow of the shape of existing image. Shadow must have only (-x, +y) offset.
254 Sprite addShadow( const Sprite & in, const Point & shadowOffset, const uint8_t transformId );
255
256 // make sure that output image's transform layer doesn't have skipping values (transform == 1)
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 // apply palette only for image layer, it doesn't affect transform part
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 // Draw one image on another taking into account the transparency and shadows data in the transform layer.
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 // inPos must contain non-negative values
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 // Copies transform the layer from in to out. Both images must be of the same size.
289 void copyTransformLayer( const Image & in, Image & out );
290 // Copies transform the layer from in to out.
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 // Make a transition to "in" image from left to right or vertically - from top to bottom using dithering (https://en.wikipedia.org/wiki/Dither).
296 // The direction of transition can be reversed.
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 // skipFactor is responsible for non-solid line. You can interpret it as skip every N pixel
303 void DrawBorder( Image & image, const uint8_t value, const uint32_t skipFactor = 0 );
304
305 // roi is an optional parameter when you need to draw in a small than image area
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 // Every image in the array must be the same size. Make sure that pointers aren't nullptr!
314 Image ExtractCommonPattern( const std::vector<const Image *> & input );
315
316 // Please use GetColorId function if you want to use an RGB value
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 // Return ROI with pixels which are not skipped and not used for shadow creation. 1 is to skip, 2 - 5 types of shadows
330 Rect GetActiveROI( const Image & image, const uint8_t minTransformValue = 6 );
331
332 // Returns a closest color ID from the original game's palette
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 // This function does NOT check transform layer. If you intent to replace few colors at the same image please use ApplyPalette to be more efficient.
338 void ReplaceColorId( Image & image, const uint8_t oldColorId, const uint8_t newColorId );
339
340 // Use this function only when you need to convert pixel value into transform layer
341 void ReplaceColorIdByTransformId( Image & image, const uint8_t colorId, const uint8_t transformId );
342
343 // Use this function only when you need to convert transform value into non-transparent pixel with the given color.
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 // Please use value from the main palette only
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 // Please set value not bigger than 13!
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 }