FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
GLKeeper/src/BitmapImage.cpp at main · codenamecpp/GLKeeper · GitHub
codenamecpp
/
GLKeeper
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
40
Code
Issues
0
Pull requests
0
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Security and quality
Insights
Expand file tree
Breadcrumbs
GLKeeper
/
src
/
BitmapImage.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
366 lines (302 loc) · 8.7 KB
Breadcrumbs
GLKeeper
/
src
/
BitmapImage.cpp
Copy path
File metadata and controls
366 lines (302 loc) · 8.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
365
366
#
include
"
stdafx.h
"
#
include
"
BitmapImage.h
"
#
include
"
stb_image.h
"
#
include
"
stb_image_write.h
"
//
////////////////////////////////////////////////////////////////////////
struct
BitmapImage
::PixelsAllocator
{};
//
////////////////////////////////////////////////////////////////////////
BitmapImage::BitmapImage
()
: mPixelFormat(ePixelFormat_Null)
{}
BitmapImage::BitmapImage
(
const
std::string& theFilePath)
: mPixelFormat(ePixelFormat_Null)
{
LoadFromFile
(theFilePath);
}
BitmapImage::BitmapImage
(BitmapImage&& other)
{
Swap
(other);
}
BitmapImage::~BitmapImage
()
{
Clear
();
}
void
BitmapImage::SetMip
(MipLevel& mipmap,
const
Point2D& dims,
unsigned
char
* pixels, PixelsAllocator* bitsAllocator)
{
FreeMip
(mipmap);
if
(pixels ==
nullptr
)
{
cxx_assert
(bitsAllocator ==
nullptr
);
bitsAllocator =
nullptr
;
const
int
dataLength =
GetBytesPerPixel
(
mPixelFormat
) * (dims.
x
* dims.
y
);
//
allocate memory
pixels =
new
unsigned
char
[dataLength];
}
mipmap.
mDims
= dims;
mipmap.
mPixels
= pixels;
mipmap.
mAllocator
= bitsAllocator;
}
void
BitmapImage::FreeMip
(MipLevel& mipmap)
{
if
(mipmap.
mPixels
)
{
if
(mipmap.
mAllocator
)
{
stbi_image_free
(mipmap.
mPixels
);
}
else
{
//
manual allocated
delete []
mipmap.
mPixels
;
}
}
mipmap.
mDims
= {};
mipmap.
mPixels
=
nullptr
;
mipmap.
mAllocator
=
nullptr
;
}
void
BitmapImage::ReleaseMips
()
{
//
do not release very first mip
while
(
GetMipsCount
() >
1
)
{
FreeMip
(
mMipmaps
.
back
());
mMipmaps
.
pop_back
();
}
}
bool
BitmapImage::AddMipLevel
(
const
Point2D& dims,
unsigned
char
** thePixels)
{
if
(!
HasContent
())
{
cxx_assert
(
false
);
return
false
;
}
cxx_assert
((dims.
x
<
mMipmaps
.
back
().
mDims
.
x
) || (dims.
y
<
mMipmaps
.
back
().
mDims
.
y
));
MipLevel& newMip =
mMipmaps
.
emplace_back
();
SetMip
(newMip, dims,
nullptr
);
cxx_assert
(newMip.
mPixels
);
if
(newMip.
mPixels
==
nullptr
)
{
mMipmaps
.
pop_back
();
return
false
;
}
if
(thePixels)
{
*thePixels = newMip.
mPixels
;
}
return
true
;
}
bool
BitmapImage::ResizeToPowerOfTwo
()
{
if
(!
HasContent
())
return
false
;
for
(
int
imipmap =
0
; imipmap <
GetMipsCount
(); ++imipmap)
{
MipLevel& srcMip =
mMipmaps
[imipmap];
Point2D dimsPOT
{
cxx::get_next_pot
(srcMip.
mDims
.
x
),
cxx::get_next_pot
(srcMip.
mDims
.
y
)
};
if
(srcMip.
mDims
== dimsPOT)
continue
;
MipLevel tempMip;
SetMip
(tempMip, dimsPOT,
nullptr
,
nullptr
);
cxx_assert
(tempMip.
mPixels
);
if
(tempMip.
mPixels
==
nullptr
)
return
false
;
//
copy data
const
Point2D copyDims {
std::min
(srcMip.
mDims
.
x
, dimsPOT.
x
),
std::min
(srcMip.
mDims
.
y
, dimsPOT.
y
),
};
const
unsigned
int
numBytesPerPixel =
GetBytesPerPixel
(
mPixelFormat
);
const
unsigned
int
copyBytesPerLine = copyDims.
x
* numBytesPerPixel;
const
int
strideDst = dimsPOT.
x
* numBytesPerPixel;
const
int
strideSrc = srcMip.
mDims
.
x
* numBytesPerPixel;
unsigned
char
* ptrDst = tempMip.
mPixels
;
unsigned
char
* ptrSrc = srcMip.
mPixels
;
for
(
int
iLine =
0
; iLine < copyDims.
y
; ++iLine)
{
::memcpy
(ptrDst, ptrSrc, copyBytesPerLine);
ptrDst += strideDst;
ptrSrc += strideSrc;
}
//
extent horz
if
(dimsPOT.
x
> srcMip.
mDims
.
x
)
{
unsigned
char
* ptrLineStart = tempMip.
mPixels
;
for
(
int
iLine =
0
; iLine < copyDims.
y
; ++iLine)
{
ptrSrc = ptrLineStart + (copyBytesPerLine - numBytesPerPixel);
ptrDst = ptrLineStart + copyBytesPerLine;
for
(
int
iPixel = copyDims.
x
; iPixel < dimsPOT.
x
; ++ iPixel)
{
::memcpy
(ptrDst, ptrSrc, numBytesPerPixel);
ptrDst += numBytesPerPixel;
}
ptrLineStart += strideDst;
}
}
//
extent vert
if
(dimsPOT.
y
> srcMip.
mDims
.
y
)
{
ptrDst = tempMip.
mPixels
+ strideDst * copyDims.
y
;
ptrSrc = (ptrDst - strideDst);
//
last line
for
(
int
iLine = copyDims.
y
; iLine < dimsPOT.
y
; ++iLine)
{
::memcpy
(ptrDst, ptrSrc, strideDst);
ptrDst += strideDst;
}
}
FreeMip
(srcMip);
srcMip = tempMip;
}
return
true
;
}
bool
BitmapImage::IsPOT
()
const
{
if
(
mMipmaps
.
empty
())
return
false
;
for
(
const
MipLevel& roller:
mMipmaps
)
{
cxx_assert
(roller.
mPixels
);
if
(roller.
mPixels
==
nullptr
)
continue
;
bool
isPowerOfTwo =
cxx::is_pot
(roller.
mDims
.
x
) &&
cxx::is_pot
(roller.
mDims
.
y
);
if
(!isPowerOfTwo)
return
false
;
}
return
true
;
}
bool
BitmapImage::Create
(ePixelFormat thePixelFormat,
const
Point2D& dims,
unsigned
char
* thePixels)
{
Clear
();
if
(thePixelFormat == ePixelFormat_Null)
return
false
;
cxx_assert
(dims.
x
>
0
);
cxx_assert
(dims.
y
>
0
);
if
(dims.
x
<
1
|| dims.
y
<
1
)
return
false
;
mPixelFormat
= thePixelFormat;
MipLevel& baseMip =
mMipmaps
.
emplace_back
();
SetMip
(baseMip, dims, thePixels);
cxx_assert
(baseMip.
mPixels
);
if
(baseMip.
mPixels
==
nullptr
)
{
Clear
();
return
false
;
}
return
true
;
}
bool
BitmapImage::Create
(ePixelFormat thePixelFormat,
const
Point2D& dims, Color32 fillColor)
{
if
(
Create
(thePixelFormat, dims,
nullptr
))
{
::memset
(
mMipmaps
[
0
].
mPixels
, fillColor.
mRGBA
, dims.x * dims.y *
GetBytesPerPixel
(
mPixelFormat
));
return
true
;
}
return
false
;
}
bool
BitmapImage::LoadFromFile
(
const
std::string& theFilePath, ePixelFormat forceImageFormat)
{
Clear
();
int
imagew;
int
imageh;
int
imagecomponents;
int
forcecomponents =
0
;
if
(forceImageFormat != ePixelFormat_Null)
{
forcecomponents =
GetBytesPerPixel
(forceImageFormat);
}
stbi_uc* pImageContent =
stbi_load
(theFilePath.
c_str
(), &imagew, &imageh, &imagecomponents, forcecomponents);
if
(!pImageContent)
{
cxx_assert
(
false
);
return
false
;
}
mPixelFormat
= forceImageFormat;
//
pixels allocator is reserved
static
PixelsAllocator dummyAllocator;
MipLevel& baseMip =
mMipmaps
.
emplace_back
();
SetMip
(baseMip, Point2D { imagew, imageh }, pImageContent, &dummyAllocator);
cxx_assert
(baseMip.
mPixels
);
if
(baseMip.
mPixels
==
nullptr
)
{
Clear
();
return
false
;
}
return
true
;
}
bool
BitmapImage::LoadFromMemory
(
const
unsigned
char
* dataBuffer,
int
dataLength, ePixelFormat forceImageFormat)
{
Clear
();
int
imagew;
int
imageh;
int
imagecomponents;
int
forcecomponents =
0
;
if
(forceImageFormat != ePixelFormat_Null)
{
forcecomponents =
GetBytesPerPixel
(forceImageFormat);
}
stbi_uc* pImageContent =
stbi_load_from_memory
(dataBuffer, dataLength, &imagew, &imageh, &imagecomponents, forcecomponents);
if
(!pImageContent)
{
cxx_assert
(
false
);
return
false
;
}
mPixelFormat
= forceImageFormat;
//
pixels allocator is reserved
static
PixelsAllocator dummyAllocator;
MipLevel& baseMip =
mMipmaps
.
emplace_back
();
SetMip
(baseMip, Point2D { imagew, imageh }, pImageContent, &dummyAllocator);
cxx_assert
(baseMip.
mPixels
);
if
(baseMip.
mPixels
==
nullptr
)
{
Clear
();
return
false
;
}
return
true
;
}
bool
BitmapImage::SaveToFile
(
const
std::string& filePath)
const
{
return
SaveToFile
(filePath,
0
);
}
bool
BitmapImage::SaveToFile
(
const
std::string& filePath,
int
mipMap)
const
{
if
(!
HasContent
())
return
false
;
if
(!
HasMipLevel
(mipMap))
{
cxx_assert
(
false
);
return
false
;
}
int
componentsCount =
GetBytesPerPixel
(
mPixelFormat
);
int
strideBytes =
mMipmaps
[mipMap].
mDims
.
x
* componentsCount;
return
stbi_write_png
(filePath.
c_str
(),
mMipmaps
[mipMap].
mDims
.
x
,
mMipmaps
[mipMap].
mDims
.
y
, componentsCount,
mMipmaps
[mipMap].
mPixels
, strideBytes) >
0
;
}
void
BitmapImage::SetHasAlphaHint
(
bool
hasAlpha)
{
mHasAlphaHint
= hasAlpha;
}
void
BitmapImage::Clear
()
{
mPixelFormat
= ePixelFormat_Null;
mHasAlphaHint
=
false
;
//
free mips
for
(MipLevel& roller:
mMipmaps
)
{
FreeMip
(roller);
}
mMipmaps
.
clear
();
}
void
BitmapImage::Swap
(BitmapImage& other)
{
if
(
this
!= &other)
{
std::swap
(
mPixelFormat
, other.
mPixelFormat
);
std::swap
(
mMipmaps
, other.
mMipmaps
);
}
}
Back
|
FazBrowse Home
|
New Git URL