-
Notifications
You must be signed in to change notification settings - Fork 2
/
oRGBAImage.cpp
364 lines (304 loc) · 8.48 KB
/
oRGBAImage.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* omnis.xcomp.framework
* =====================
*
* oRGBAImage.cpp
*
* RGBA bitmap class
*
* Bastiaan Olij
*
* https://github.com/BastiaanOlij/omnis.xcomp.framework
*/
#include "oRGBAImage.h"
// Include our support library
#define STBI_FAILURE_USERMSG
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
// #define STB_DEFINE
// #include "stb/stb.h"
// initialise our buffer..
void oRGBAImage::initBuffer(qlong pWidth, qlong pHeight) {
// free any buffer we still have...
if (mBuffer != NULL) {
MEMfree(mBuffer);
mWidth = 0;
mHeight = 0;
mBuffer = NULL;
}
if ((pWidth != 0) && (pHeight != 0)) {
mBuffer = (sPixel *)MEMmalloc(sizeof(sPixel) * pWidth * pHeight);
if (mBuffer != NULL) {
mWidth = pWidth;
mHeight = pHeight;
};
};
};
// init empty bitmap
oRGBAImage::oRGBAImage() {
mWidth = 0;
mHeight = 0;
mBuffer = NULL;
};
// create empty bitmap of these dimentions
oRGBAImage::oRGBAImage(qlong pWidth, qlong pHeight) {
mWidth = 0;
mHeight = 0;
mBuffer = NULL;
initBuffer(pWidth, pHeight);
if (mBuffer != NULL) {
memset(mBuffer, 0xFF, sizeof(sPixel) * mWidth * mHeight);
};
};
// create empty bitmap of these dimentions with a default color
oRGBAImage::oRGBAImage(qlong pWidth, qlong pHeight, qcol pColor) {
mWidth = 0;
mHeight = 0;
mBuffer = NULL;
initBuffer(pWidth, pHeight);
if (mBuffer != NULL) {
sPixel pixel;
// get real RGB value..
qlong col = GDIgetRealColor(pColor);
// split into RGBA components
// need to check if this is the same on windows..
pixel.mR = col & 0xFF;
pixel.mG = (col >> 8) & 0xFF;
pixel.mB = (col >> 16) & 0xFF;
// pixel.mA = (col >> 24) & 0xFF;
pixel.mA = 255; // note, not supplied by omnis generally so we ignore...
for (qlong index = 0; index < (mWidth * mHeight); index++) {
mBuffer[index] = pixel;
};
};
};
// construct as a copy
oRGBAImage::oRGBAImage(const oRGBAImage &pCopy) {
mWidth = 0;
mHeight = 0;
mBuffer = NULL;
copy(pCopy);
};
// construct from a binary image (see STB library for supported formats)
oRGBAImage::oRGBAImage(qbyte *pBuffer, qlong pSize) {
mWidth = 0;
mHeight = 0;
mBuffer = NULL;
copy(pBuffer, pSize);
};
// destruct and free up memory
oRGBAImage::~oRGBAImage() {
initBuffer(0, 0);
};
// get a pointer to our image buffer
const sPixel *oRGBAImage::imageBuffer() const {
return mBuffer;
};
// width of our image
qlong oRGBAImage::width() const {
return mWidth;
};
// height of our image
qlong oRGBAImage::height() const {
return mHeight;
};
// copy another image
void oRGBAImage::copy(const oRGBAImage &pCopy) {
initBuffer(pCopy.width(), pCopy.height());
if (mBuffer != NULL) {
memcpy(mBuffer, pCopy.imageBuffer(), sizeof(sPixel) * mWidth * mHeight);
};
};
// cpoy a binary image into our image (see STB library for supported formats)
bool oRGBAImage::copy(qbyte *pBuffer, qlong pSize) {
bool successful = false;
int X = 0, Y = 0, Comp = 0;
// this will decompress our image and
unsigned char *pixeldata = stbi_load_from_memory(pBuffer, pSize, &X, &Y, &Comp, 4);
if (pixeldata == NULL) {
// Need to implement stbi_failure_reason() to get more info...
oBaseComponent::addToTraceLog("copy: can't decode image data - %s", stbi_failure_reason());
} else {
if (Comp == 3) {
initBuffer(X, Y);
if (mBuffer != NULL) {
qbyte *data = pixeldata;
for (qlong i = 0; i < X * Y; i++) {
mBuffer[i].mR = *data;
data++;
mBuffer[i].mG = *data;
data++;
mBuffer[i].mB = *data;
data++;
mBuffer[i].mA = 255;
};
successful = true;
};
} else if (Comp == 4) {
// oBaseComponent::addToTraceLog("copy: loaded image %i, %i - %i", X, Y, Comp);
// in theory we should be able to use our pixel data directly but we'll take the safe approach and copy it into our own buffer
initBuffer(X, Y);
if (mBuffer != NULL) {
memcpy(mBuffer, pixeldata, 4 * X * Y);
successful = true;
};
} else {
oBaseComponent::addToTraceLog("copy: couldn't convert image to RGBA");
};
// and free up...
stbi_image_free(pixeldata);
};
return successful;
};
// returns our image as a PNG (calling method is responsible for freeing up the memory using free)
qbyte *oRGBAImage::asPNG(int &pLen) {
// note that according to the STB documentation to compression used by this PNG logic isn't very good
// its implemented for simplicity but it does what we need it to do...
// you can always have Omnis recompress it as Omnis has a full PNG implementation
int len;
unsigned char *png = stbi_write_png_to_mem((unsigned char *)mBuffer, 0, mWidth, mHeight, 4, &len);
if (png == NULL) {
pLen = 0;
oBaseComponent::addToTraceLog("asPNG: Couldn't convert to PNG");
} else {
pLen = len;
};
return png;
};
// returns interpolated pixel
sPixel oRGBAImage::getPixel(float pX, float pY) const {
sPixel pixel;
if (mBuffer != NULL) {
if (pX < 0.0) pX = 0.0; // ???
if (pY < 0.0) pY = 0.0; // ???
qlong intX = (qlong)floor(pX);
qlong intY = (qlong)floor(pY);
bool interpolate = true;
// make sure we stay within our buffer...
if (intX >= (mWidth - 1)) {
intX = mWidth - 1;
interpolate = false;
};
if (intY >= (mHeight - 1)) {
intY = mHeight - 1;
interpolate = false;
};
// calculate our top/left offset
qlong offset = intY;
offset *= mWidth;
offset += intX;
// check if we need to interpolate
if (interpolate) {
float fractX = pX - intX;
float fractY = pY - intY;
if ((fractX > 0.0) || (fractY > 0.0)) {
sPixel topcol = interpolatePixel(mBuffer[offset], mBuffer[offset + 1], fractX);
offset += mWidth;
sPixel bottomcol = interpolatePixel(mBuffer[offset], mBuffer[offset + 1], fractX);
pixel = interpolatePixel(topcol, bottomcol, fractY);
} else {
pixel = mBuffer[offset];
};
} else {
pixel = mBuffer[offset];
};
} else {
pixel.mR = 0;
pixel.mG = 0;
pixel.mB = 0;
pixel.mA = 0;
};
return pixel;
};
////////////////////////////////////////////////////////////////////////////////////
// operators
////////////////////////////////////////////////////////////////////////////////////
// (X,Y) operator (direct access to pixel)
sPixel &oRGBAImage::operator()(qlong pX, qlong pY) {
static sPixel pixel;
if (mBuffer != NULL) {
if ((pX >= 0) && (pX < mWidth) && (pY >= 0) && (pY < mHeight)) {
return mBuffer[pX + (pY * mWidth)];
};
};
pixel.mR = 0;
pixel.mG = 0;
pixel.mB = 0;
pixel.mA = 0;
return pixel;
};
////////////////////////////////////////////////////////////////////////////////////
// static functions
////////////////////////////////////////////////////////////////////////////////////
sPixel oRGBAImage::mixPixel(sPixel pBase, sPixel pAdd, qbyte pAlpha) {
if ((pAlpha == 255) && (pAdd.mA == 255)) {
// opaque pixel...
return pAdd;
} else if ((pAlpha == 0) || (pAdd.mA == 0)) {
// fully transparent pixel
return pBase;
} else {
sPixel result;
qlong value, alpha = pAlpha;
alpha *= pAdd.mA;
result.mA = 255;
// Red
value = pBase.mR;
value *= (255 * 255) - alpha;
result.mR = value >> 16;
value = pAdd.mR;
value *= alpha;
result.mR += (value >> 16);
// Green
value = pBase.mG;
value *= (255 * 255) - alpha;
result.mG = value >> 16;
value = pAdd.mG;
value *= alpha;
result.mG += (value >> 16);
// blue
value = pBase.mB;
value *= (255 * 255) - alpha;
result.mB = value >> 16;
value = pAdd.mB;
value *= alpha;
result.mB += (value >> 16);
return result;
};
};
// interpolate pixel
sPixel oRGBAImage::interpolatePixel(sPixel pA, sPixel pB, float pFract) {
sPixel result;
float delta;
if (pA.mR == pB.mR) {
result.mR = pA.mR;
} else {
delta = (float)(pB.mR - pA.mR);
delta *= pFract;
result.mR = (qbyte)(pA.mR + delta);
};
if (pA.mG == pB.mG) {
result.mG = pA.mG;
} else {
delta = (float)(pB.mG - pA.mG);
delta *= pFract;
result.mG = (qbyte)(pA.mG + delta);
};
if (pA.mB == pB.mB) {
result.mB = pA.mB;
} else {
delta = (float)(pB.mB - pA.mB);
delta *= pFract;
result.mB = (qbyte)(pA.mB + delta);
};
if (pA.mA == pB.mA) {
result.mA = pA.mA;
} else {
delta = (float)(pB.mA - pA.mA);
delta *= pFract;
result.mA = (qbyte)(pA.mA + delta);
};
return result;
};