sculpto
image.h
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file image.h
3 * \brief Image container class implementation module.
4 *
5 * \author Sabitov Kirill
6 * \date 28 June 2022
7 *********************************************************************/
8
9#pragma once
10
11#include <stb_image.h>
12
13#include "base.h"
14
15namespace scl
16{
18 class image
19 {
20 private:
21 int Width {}, Height {};
22 int ComponentsCount {};
23 u8 *Data { nullptr };
24
25 public:
27 int GetWidth() const { return Width; }
29 int GetHeight() const { return Height; }
31 int GetComponentsCount() const { return ComponentsCount; }
33 const u8 *GetRawData() const { return Data; }
34
35 /*!*
36 * Check if image container filled with pixels function.
37 *
38 * \param None.
39 * \return is image empty flag.
40 */
41 bool IsEmpty() const { return Data == nullptr; }
42
43 public:
45 image() = default;
46
48 image(const image &Other)
49 {
50 if (Other.Data == nullptr) return;
51
52 Width = Other.Width;
53 Height = Other.Height;
54 ComponentsCount = Other.ComponentsCount;
55
56 size_t size = (size_t)Width * Height * ComponentsCount;
57 Data = new u8[size];
58 memcpy(Data, Other.Data, size);
59 }
60
62 image &operator= (const image &Other)
63 {
64 if (this == &Other || Other.Data == nullptr) return *this;
65 if (Data != nullptr) delete[] Data;
66
67 Width = Other.Width;
68 Height = Other.Height;
69 ComponentsCount = Other.ComponentsCount;
70
71 size_t size = (size_t)Width * Height * ComponentsCount;
72 Data = new u8[size];
73 memcpy(Data, Other.Data, size);
74 return *this;
75 }
76
77 /*!*
78 * Empty image specified size constructor.
79 *
80 * \param Width - width of creating image.
81 * \param Height - height of creating image.
82 * \param ComponentsCount - dimentions (components per pixel) count.
83 * \param AllocateMemory - flag, showing weather allocate mrmory for data buffer or not.
84 */
85 image(int Width, int Height, int ComponentsCount, bool AllocateMemory = true) :
86 Width(Width), Height(Height), ComponentsCount(ComponentsCount)
87 {
88 if (AllocateMemory) Allocate();
89 }
90
91 /*!*
92 * Image by pixels array constructor.
93 *
94 * \param Width - width of creating image.
95 * \param Height - height of creating image.
96 * \param ComponentsCount - dimentions (components per pixel) count.
97 * \param Data - image pixel array pointer with byte size [Width * Height * ComponentsCount].
98 */
99 image(int Width, int Height, int ComponentsCount, void *Data) :
100 Width(Width), Height(Height), ComponentsCount(ComponentsCount)
101 {
102 size_t size = (size_t)Width * Height * ComponentsCount;
103 this->Data = new u8[size];
104 memcpy(this->Data, Data, size);
105 }
106
107 /*!*
108 * Image constructor loading data from file.
109 *
110 * \param FileName - image file name.
111 */
112 image(const std::string &FileName)
113 {
114 this->Load(FileName);
115 }
116
119 {
120 Free();
121 }
122
123 /*!*
124 * Allocate memory for image data buffer (of current image size).
125 *
126 * \param None.
127 * \return None.
128 */
129 void Allocate()
130 {
131 Data = new u8[(u64)Width * Height * ComponentsCount];
132 }
133
134 /*!*
135 * Free memory of image data buffer.
136 *
137 * \param None.
138 * \return None.
139 */
140 void Free()
141 {
142 Width = Height = ComponentsCount = 0;
143 if (Data != nullptr) stbi_image_free(Data);
144 Data = nullptr;
145 }
146
147 /*!*
148 * Image load from file function.
149 *
150 * \param FileName - file name to load image from.
151 * \return success flag.
152 */
153 bool Load(const std::string &FileName)
154 {
155 // За каждым малоком должен быть free.... (CGSG FOREVER!!!)
156 if (Data != nullptr) delete[] Data;
157
158 // Load image, check if successful
159 int w, h, c;
160 Data = stbi_load(FileName.c_str(), &w, &h, &c, 0);
161 if (Data == nullptr)
162 {
163 SCL_CORE_ERROR("Error during loading image from file \"{}\"!", FileName);
164 Width = Height = ComponentsCount = 0;
165 if (Data) stbi_image_free(Data);
166 return false;
167 }
168
169 // If image loaded successful, set image data
170 Width = w, Height = h, ComponentsCount = c;
171 return true;
172 }
173
174 /*!*
175 * Flip image along X axis function.
176 *
177 * \param None.
178 * \return None.
179 */
181 {
182 u32 row_size = Width * ComponentsCount;
183 u8 *tmp = new u8[row_size];
184 for (u64 i = 0; i < Height / 2; ++i)
185 {
186 std::memmove(tmp, &Data[i * row_size], row_size);
187 std::memmove(&Data[i * row_size], &Data[(Height - i - 1) * row_size], row_size);
188 std::memmove(&Data[(Height - i - 1) * row_size], tmp, row_size);
189 }
190 delete[] tmp;
191 }
192 };
193}
Topology object basis class for mesh creating implementation module.
int GetWidth() const
Definition: image.h:27
int GetHeight() const
Definition: image.h:29
const u8 * GetRawData() const
Definition: image.h:33
image()=default
void Free()
Definition: image.h:140
int GetComponentsCount() const
Definition: image.h:31
image(const std::string &FileName)
Definition: image.h:112
image(const image &Other)
Definition: image.h:48
void FlipHorizontaly()
Definition: image.h:180
image(int Width, int Height, int ComponentsCount, void *Data)
Definition: image.h:99
void Allocate()
Definition: image.h:129
~image()
Definition: image.h:118
bool Load(const std::string &FileName)
Definition: image.h:153
bool IsEmpty() const
Definition: image.h:41
image(int Width, int Height, int ComponentsCount, bool AllocateMemory=true)
Definition: image.h:85
image & operator=(const image &Other)
Definition: image.h:62
#define SCL_CORE_ERROR(...)
Definition: log.h:44
Definition: base.h:33
uint32_t u32
Definition: math_common.h:21
uint64_t u64
Definition: math_common.h:23
uint8_t u8
Definition: math_common.h:17