-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSurface.cpp
More file actions
245 lines (208 loc) · 6.36 KB
/
Surface.cpp
File metadata and controls
245 lines (208 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/******************************************************************************************
* Chili DirectX Framework Version 16.10.01 *
* Surface.cpp *
* Copyright 2016 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of The Chili DirectX Framework. *
* *
* The Chili DirectX Framework is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili DirectX Framework is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili DirectX Framework. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#define FULL_WINTARD
#include "Surface.h"
#include <algorithm>
namespace Gdiplus
{
using std::min;
using std::max;
}
#include <gdiplus.h>
#include <sstream>
#pragma comment( lib,"gdiplus.lib" )
Surface::Surface(unsigned int width, unsigned int height) noexcept
:
pBuffer(std::make_unique<Color[]>(width* height)),
width(width),
height(height)
{}
Surface& Surface::operator=(Surface&& donor) noexcept
{
width = donor.width;
height = donor.height;
pBuffer = std::move(donor.pBuffer);
donor.pBuffer = nullptr;
return *this;
}
Surface::Surface(Surface&& source) noexcept
:
pBuffer(std::move(source.pBuffer)),
width(source.width),
height(source.height)
{}
Surface::~Surface()
{}
void Surface::Clear(Color fillValue) noexcept
{
memset(pBuffer.get(), fillValue.dword, width * height * sizeof(Color));
}
void Surface::PutPixel(unsigned int x, unsigned int y, Color c) noexcept(!IS_DEBUG)
{
assert(x >= 0);
assert(y >= 0);
assert(x < width);
assert(y < height);
pBuffer[y * width + x] = c;
}
Surface::Color Surface::GetPixel(unsigned int x, unsigned int y) const noexcept(!IS_DEBUG)
{
assert(x >= 0);
assert(y >= 0);
assert(x < width);
assert(y < height);
return pBuffer[y * width + x];
}
unsigned int Surface::GetWidth() const noexcept
{
return width;
}
unsigned int Surface::GetHeight() const noexcept
{
return height;
}
Surface::Color* Surface::GetBufferPtr() noexcept
{
return pBuffer.get();
}
const Surface::Color* Surface::GetBufferPtr() const noexcept
{
return pBuffer.get();
}
const Surface::Color* Surface::GetBufferPtrConst() const noexcept
{
return pBuffer.get();
}
Surface Surface::FromFile(const std::string& name)
{
unsigned int width = 0;
unsigned int height = 0;
std::unique_ptr<Color[]> pBuffer;
{
// convert filenam to wide string (for Gdiplus)
wchar_t wideName[512];
mbstowcs_s(nullptr, wideName, name.c_str(), _TRUNCATE);
Gdiplus::Bitmap bitmap(wideName);
if (bitmap.GetLastStatus() != Gdiplus::Status::Ok)
{
std::stringstream ss;
ss << "Loading image [" << name << "]: failed to load.";
throw Exception(__LINE__, __FILE__, ss.str());
}
width = bitmap.GetWidth();
height = bitmap.GetHeight();
pBuffer = std::make_unique<Color[]>(width * height);
for (unsigned int y = 0; y < height; y++)
{
for (unsigned int x = 0; x < width; x++)
{
Gdiplus::Color c;
bitmap.GetPixel(x, y, &c);
pBuffer[y * width + x] = c.GetValue();
}
}
}
return Surface(width, height, std::move(pBuffer));
}
void Surface::Save(const std::string& filename) const
{
auto GetEncoderClsid = [&filename](const WCHAR* format, CLSID* pClsid) -> void
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
Gdiplus::ImageCodecInfo* pImageCodecInfo = nullptr;
Gdiplus::GetImageEncodersSize(&num, &size);
if (size == 0)
{
std::stringstream ss;
ss << "Saving surface to [" << filename << "]: failed to get encoder; size == 0.";
throw Exception(__LINE__, __FILE__, ss.str());
}
pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == nullptr)
{
std::stringstream ss;
ss << "Saving surface to [" << filename << "]: failed to get encoder; failed to allocate memory.";
throw Exception(__LINE__, __FILE__, ss.str());
}
GetImageEncoders(num, size, pImageCodecInfo);
for (UINT j = 0; j < num; ++j)
{
if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return;
}
}
free(pImageCodecInfo);
std::stringstream ss;
ss << "Saving surface to [" << filename <<
"]: failed to get encoder; failed to find matching encoder.";
throw Exception(__LINE__, __FILE__, ss.str());
};
CLSID bmpID;
GetEncoderClsid(L"image/bmp", &bmpID);
// convert filenam to wide string (for Gdiplus)
wchar_t wideName[512];
mbstowcs_s(nullptr, wideName, filename.c_str(), _TRUNCATE);
Gdiplus::Bitmap bitmap(width, height, width * sizeof(Color), PixelFormat32bppARGB, (BYTE*)pBuffer.get());
if (bitmap.Save(wideName, &bmpID, nullptr) != Gdiplus::Status::Ok)
{
std::stringstream ss;
ss << "Saving surface to [" << filename << "]: failed to save.";
throw Exception(__LINE__, __FILE__, ss.str());
}
}
void Surface::Copy(const Surface& src) noexcept(!IS_DEBUG)
{
assert(width == src.width);
assert(height == src.height);
memcpy(pBuffer.get(), src.pBuffer.get(), width * height * sizeof(Color));
}
Surface::Surface(unsigned int width, unsigned int height, std::unique_ptr<Color[]> pBufferParam) noexcept
:
width(width),
height(height),
pBuffer(std::move(pBufferParam))
{}
// surface exception stuff
Surface::Exception::Exception(int line, const char* file, std::string note) noexcept
:
MainException(line, file),
note(std::move(note))
{}
const char* Surface::Exception::what() const noexcept
{
std::ostringstream oss;
oss << MainException::what() << std::endl
<< "[Note] " << GetNote();
whatBuffer = oss.str();
return whatBuffer.c_str();
}
const char* Surface::Exception::GetType() const noexcept
{
return "Chili Graphics Exception";
}
const std::string& Surface::Exception::GetNote() const noexcept
{
return note;
}