/
Bitmap.cpp
352 lines (271 loc) · 10.4 KB
/
Bitmap.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
tdogl::Bitmap
Copyright 2012 Thomas Dalling - http://tomdalling.com/
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "Bitmap.h"
#include <stdexcept>
//uses stb_image to try load files
#define STBI_FAILURE_USERMSG
#include <stb_image.c>
using namespace tdogl;
inline unsigned char AverageRGB(unsigned char rgb[3]) {
return (unsigned char)(((double)rgb[0] + (double)rgb[1] + (double)rgb[2]) / 3.0);
}
static void Grayscale2GrayscaleAlpha(unsigned char* src, unsigned char* dest){
dest[0] = src[0];
dest[1] = 255;
}
static void Grayscale2RGB(unsigned char* src, unsigned char* dest){
dest[0] = src[0];
dest[1] = src[0];
dest[2] = src[0];
}
static void Grayscale2RGBA(unsigned char* src, unsigned char* dest){
dest[0] = src[0];
dest[1] = src[0];
dest[2] = src[0];
dest[3] = 255;
}
static void GrayscaleAlpha2Grayscale(unsigned char* src, unsigned char* dest){
dest[0] = src[0];
}
static void GrayscaleAlpha2RGB(unsigned char* src, unsigned char* dest){
dest[0] = src[0];
dest[1] = src[0];
dest[2] = src[0];
}
static void GrayscaleAlpha2RGBA(unsigned char* src, unsigned char* dest){
dest[0] = src[0];
dest[1] = src[0];
dest[2] = src[0];
dest[3] = src[1];
}
static void RGB2Grayscale(unsigned char* src, unsigned char* dest){
dest[0] = AverageRGB(src);
}
static void RGB2GrayscaleAlpha(unsigned char* src, unsigned char* dest){
dest[0] = AverageRGB(src);
dest[1] = 255;
}
static void RGB2RGBA(unsigned char* src, unsigned char* dest){
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = 255;
}
static void RGBA2Grayscale(unsigned char* src, unsigned char* dest){
dest[0] = AverageRGB(src);
}
static void RGBA2GrayscaleAlpha(unsigned char* src, unsigned char* dest){
dest[0] = AverageRGB(src);
dest[1] = src[3];
}
static void RGBA2RGB(unsigned char* src, unsigned char* dest){
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
}
typedef void(*FormatConverterFunc)(unsigned char*, unsigned char*);
static FormatConverterFunc ConverterFuncForFormats(Bitmap::Format srcFormat, Bitmap::Format destFormat){
if(srcFormat == destFormat)
throw std::runtime_error("Just use memcpy if pixel formats are the same");
switch(srcFormat){
case Bitmap::Format_Grayscale:
switch(destFormat){
case Bitmap::Format_GrayscaleAlpha: return Grayscale2GrayscaleAlpha;
case Bitmap::Format_RGB: return Grayscale2RGB;
case Bitmap::Format_RGBA: return Grayscale2RGBA;
default:
throw std::runtime_error("Unhandled bitmap format");
}
break;
case Bitmap::Format_GrayscaleAlpha:
switch(destFormat){
case Bitmap::Format_Grayscale: return GrayscaleAlpha2Grayscale;
case Bitmap::Format_RGB: return GrayscaleAlpha2RGB;
case Bitmap::Format_RGBA: return GrayscaleAlpha2RGBA;
default:
throw std::runtime_error("Unhandled bitmap format");
}
break;
case Bitmap::Format_RGB:
switch(destFormat){
case Bitmap::Format_Grayscale: return RGB2Grayscale;
case Bitmap::Format_GrayscaleAlpha: return RGB2GrayscaleAlpha;
case Bitmap::Format_RGBA: return RGB2RGBA;
default:
throw std::runtime_error("Unhandled bitmap format");
}
break;
case Bitmap::Format_RGBA:
switch(destFormat){
case Bitmap::Format_Grayscale: return RGBA2Grayscale;
case Bitmap::Format_GrayscaleAlpha: return RGBA2GrayscaleAlpha;
case Bitmap::Format_RGB: return RGBA2RGB;
default:
throw std::runtime_error("Unhandled bitmap format");
}
break;
default:
throw std::runtime_error("Unhandled bitmap format");
}
}
/*
* Misc funcs
*/
inline unsigned GetPixelOffset(unsigned col, unsigned row, unsigned width, unsigned height, Bitmap::Format format) {
return (row*width + col)*format;
}
inline bool RectsOverlap(unsigned srcCol, unsigned srcRow, unsigned destCol, unsigned destRow, unsigned width, unsigned height){
unsigned colDiff = srcCol > destCol ? srcCol - destCol : destCol - srcCol;
if(colDiff < width)
return true;
unsigned rowDiff = srcRow > destRow ? srcRow - destRow : destRow - srcRow;
if(rowDiff < height)
return true;
return false;
}
/*
* Bitmap class
*/
Bitmap::Bitmap(unsigned width,
unsigned height,
Format format,
const unsigned char* pixels) :
_pixels(NULL)
{
_set(width, height, format, pixels);
}
Bitmap::~Bitmap() {
if(_pixels) free(_pixels);
}
Bitmap Bitmap::bitmapFromFile(std::string filePath) {
int width, height, channels;
unsigned char* pixels = stbi_load(filePath.c_str(), &width, &height, &channels, 0);
if(!pixels) throw std::runtime_error(stbi_failure_reason());
Bitmap bmp(width, height, (Format)channels, pixels);
stbi_image_free(pixels);
return bmp;
}
Bitmap::Bitmap(const Bitmap& other) :
_pixels(NULL)
{
_set(other._width, other._height, other._format, other._pixels);
}
Bitmap& Bitmap::operator = (const Bitmap& other) {
_set(other._width, other._height, other._format, other._pixels);
return *this;
}
unsigned int Bitmap::width() const {
return _width;
}
unsigned int Bitmap::height() const {
return _height;
}
Bitmap::Format Bitmap::format() const {
return _format;
}
unsigned char* Bitmap::pixelBuffer() const {
return _pixels;
}
unsigned char* Bitmap::getPixel(unsigned int column, unsigned int row) const {
if(column >= _width || row >= _height)
throw std::runtime_error("Pixel coordinate out of bounds");
return _pixels + GetPixelOffset(column, row, _width, _height, _format);
}
void Bitmap::setPixel(unsigned int column, unsigned int row, const unsigned char* pixel) {
unsigned char* myPixel = getPixel(column, row);
memcpy(myPixel, pixel, _format);
}
void Bitmap::flipVertically() {
unsigned long rowSize = _format*_width;
unsigned char* rowBuffer = new unsigned char[rowSize];
unsigned halfRows = _height / 2;
for(unsigned rowIdx = 0; rowIdx < halfRows; ++rowIdx){
unsigned char* row = _pixels + GetPixelOffset(0, rowIdx, _width, _height, _format);
unsigned char* oppositeRow = _pixels + GetPixelOffset(0, _height - rowIdx - 1, _width, _height, _format);
memcpy(rowBuffer, row, rowSize);
memcpy(row, oppositeRow, rowSize);
memcpy(oppositeRow, rowBuffer, rowSize);
}
delete rowBuffer;
}
void Bitmap::rotate90CounterClockwise() {
unsigned char* newPixels = (unsigned char*) malloc(_format*_width*_height);
for(unsigned row = 0; row < _height; ++row){
for(unsigned col = 0; col < _width; ++col){
unsigned srcOffset = GetPixelOffset(col, row, _width, _height, _format);
unsigned destOffset = GetPixelOffset(row, _width - col - 1, _height, _width, _format);
memcpy(newPixels + destOffset, _pixels + srcOffset, _format); //copy one pixel
}
}
free(_pixels);
_pixels = newPixels;
unsigned swapTmp = _height;
_height = _width;
_width = swapTmp;
}
void Bitmap::copyRectFromBitmap(const Bitmap& src,
unsigned srcCol,
unsigned srcRow,
unsigned destCol,
unsigned destRow,
unsigned width,
unsigned height)
{
if (srcCol == 0 && srcRow == 0 && width == 0 && height == 0){
width = src.width();
height = src.height();
}
if(width == 0 || height == 0)
throw std::runtime_error("Can't copy zero height/width rectangle");
if(srcCol + width >= src.width() || srcRow + height >= src.height())
throw std::runtime_error("Rectangle doesn't fit within source bitmap");
if(destCol + width >= _width || destRow + height >= _height)
throw std::runtime_error("Rectangle doesn't fit within destination bitmap");
if(_pixels == src._pixels && RectsOverlap(srcCol, srcRow, destCol, destRow, width, height))
throw std::runtime_error("Source and destination are the same bitmap, and rects overlap. Not allowed!");
FormatConverterFunc converter = NULL;
if(_format != src._format)
converter = ConverterFuncForFormats(_format, src._format);
for(unsigned row = 0; row < height; ++row){
for(unsigned col = 0; col < width; ++col){
unsigned char* srcPixel = src._pixels + GetPixelOffset(srcCol + col, srcRow + row, src._width, src._height, src._format);
unsigned char* destPixel = _pixels + GetPixelOffset(destCol + col, destRow + row, _width, _height, _format);
if(converter){
converter(srcPixel, destPixel);
} else {
memcpy(destPixel, srcPixel, _format);
}
}
}
}
void Bitmap::_set(unsigned width,
unsigned height,
Format format,
const unsigned char* pixels)
{
if(width == 0) throw std::runtime_error("Zero width bitmap");
if(height == 0) throw std::runtime_error("Zero height bitmap");
if(format <= 0 || format > 4) throw std::runtime_error("Invalid bitmap format");
_width = width;
_height = height;
_format = format;
size_t newSize = _width * _height * _format;
if(_pixels){
_pixels = (unsigned char*)realloc(_pixels, newSize);
} else {
_pixels = (unsigned char*)malloc(newSize);
}
if(pixels)
memcpy(_pixels, pixels, newSize);
}