FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SFML/src/SFML/Graphics/Image.cpp at master · githubhy/SFML · GitHub
githubhy
/
SFML
Public
forked from
SFML/SFML
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SFML
/
src
/
SFML
/
Graphics
/
Image.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
559 lines (471 loc) · 17.5 KB
Breadcrumbs
SFML
/
src
/
SFML
/
Graphics
/
Image.cpp
Copy path
File metadata and controls
559 lines (471 loc) · 17.5 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//
//////////////////////////////////////////////////////////
//
//
SFML - Simple and Fast Multimedia Library
//
Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
//
//
This software is provided 'as-is', without any express or implied warranty.
//
In no event will the authors be held liable for any damages arising from the use of this software.
//
//
Permission is granted to anyone to use this software for any purpose,
//
including commercial applications, and to alter it and redistribute it freely,
//
subject to the following restrictions:
//
//
1. The origin of this software must not be misrepresented;
//
you must not claim that you wrote the original software.
//
If you use this software in a product, an acknowledgment
//
in the product documentation would be appreciated but is not required.
//
//
2. Altered source versions must be plainly marked as such,
//
and must not be misrepresented as being the original software.
//
//
3. This notice may not be removed or altered from any source distribution.
//
//
//////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////
//
Headers
//
//////////////////////////////////////////////////////////
#
include
<
SFML/Graphics/Image.hpp
>
#
include
<
SFML/System/Err.hpp
>
#
include
<
SFML/System/InputStream.hpp
>
#
include
<
SFML/System/Utils.hpp
>
#
ifdef
SFML_SYSTEM_ANDROID
#
include
<
SFML/System/Android/Activity.hpp
>
#
include
<
SFML/System/Android/ResourceStream.hpp
>
#
endif
#
define
STB_IMAGE_IMPLEMENTATION
#
include
<
stb_image.h
>
#
define
STB_IMAGE_WRITE_IMPLEMENTATION
#
include
<
stb_image_write.h
>
#
include
<
algorithm
>
#
include
<
iomanip
>
#
include
<
memory
>
#
include
<
ostream
>
#
include
<
string
>
#
include
<
cassert
>
#
include
<
cstring
>
namespace
{
//
stb_image callbacks that operate on a sf::InputStream
int
read
(
void
* user,
char
* data,
int
size)
{
auto
& stream = *
static_cast
<sf::InputStream*>(user);
const
std::optional count = stream.
read
(data,
static_cast
<std::
size_t
>(size));
return
count ?
static_cast
<
int
>(*count) : -
1
;
}
void
skip
(
void
* user,
int
size)
{
auto
& stream = *
static_cast
<sf::InputStream*>(user);
if
(!stream.
seek
(stream.
tell
().
value
() +
static_cast
<std::
size_t
>(size)).
has_value
())
sf::err
() <<
"
Failed to seek image loader input stream
"
<< std::endl;
}
int
eof
(
void
* user)
{
auto
& stream = *
static_cast
<sf::InputStream*>(user);
return
stream.
tell
() >= stream.
getSize
();
}
//
stb_image callback for constructing a buffer
void
bufferFromCallback
(
void
* context,
void
* data,
int
size)
{
const
auto
* source =
static_cast
<std::
uint8_t
*>(data);
auto
* dest =
static_cast
<std::vector<std::
uint8_t
>*>(context);
std::copy
(source, source + size,
std::back_inserter
(*dest));
}
//
Deleter for STB pointers
struct
StbDeleter
{
void
operator
()(stbi_uc* image)
const
{
stbi_image_free
(image);
}
};
using
StbPtr = std::unique_ptr<stbi_uc, StbDeleter>;
}
//
namespace
namespace
sf
{
//
//////////////////////////////////////////////////////////
Image::Image
(
const
Vector2u& size,
const
Color& color)
{
if
(size.
x
&& size.
y
)
{
//
Create a new pixel buffer first for exception safety's sake
std::vector<std::
uint8_t
>
newPixels
(
static_cast
<std::
size_t
>(size.
x
) *
static_cast
<std::
size_t
>(size.
y
) *
4
);
//
Fill it with the specified color
std::
uint8_t
* ptr = newPixels.
data
();
std::
uint8_t
* end = ptr + newPixels.
size
();
while
(ptr < end)
{
*ptr++ = color.
r
;
*ptr++ = color.
g
;
*ptr++ = color.
b
;
*ptr++ = color.
a
;
}
//
Commit the new pixel buffer
m_pixels.
swap
(newPixels);
//
Assign the new size
m_size = size;
}
else
{
//
Dump the pixel buffer
std::vector<std::
uint8_t
>().
swap
(m_pixels);
//
Assign the new size
m_size = {};
}
}
//
//////////////////////////////////////////////////////////
Image::Image
(
const
Vector2u& size,
const
std::
uint8_t
* pixels)
{
if
(pixels && size.
x
&& size.
y
)
{
//
Create a new pixel buffer first for exception safety's sake
std::vector<std::
uint8_t
>
newPixels
(pixels, pixels + size.
x
* size.
y
*
4
);
//
Commit the new pixel buffer
m_pixels.
swap
(newPixels);
//
Assign the new size
m_size = size;
}
else
{
//
Dump the pixel buffer
std::vector<std::
uint8_t
>().
swap
(m_pixels);
//
Assign the new size
m_size = {};
}
}
//
//////////////////////////////////////////////////////////
Image::Image
(Vector2u size, std::vector<std::
uint8_t
>&& pixels) : m_size(size), m_pixels(std::move(pixels))
{
}
//
//////////////////////////////////////////////////////////
std::optional<Image>
Image::loadFromFile
(
const
std::filesystem::path& filename)
{
#
ifdef
SFML_SYSTEM_ANDROID
if
(
priv::getActivityStatesPtr
() !=
nullptr
)
{
priv::ResourceStream
stream
(filename);
return
loadFromStream
(stream);
}
#
endif
//
Load the image and get a pointer to the pixels in memory
int
width =
0
;
int
height =
0
;
int
channels =
0
;
const
auto
ptr =
StbPtr
(
stbi_load
(filename.
string
().
c_str
(), &width, &height, &channels, STBI_rgb_alpha));
if
(ptr)
{
return
Image
(
Vector2u
(
Vector2i
(width, height)), {ptr.
get
(), ptr.
get
() + width * height *
4
});
}
else
{
//
Error, failed to load the image
err
() <<
"
Failed to load image
\n
"
<<
formatDebugPathInfo
(filename) <<
"
\n
Reason:
"
<<
stbi_failure_reason
() << std::endl;
return
std::
nullopt
;
}
}
//
//////////////////////////////////////////////////////////
std::optional<Image>
Image::loadFromMemory
(
const
void
* data, std::
size_t
size)
{
//
Check input parameters
if
(data && size)
{
//
Load the image and get a pointer to the pixels in memory
int
width =
0
;
int
height =
0
;
int
channels =
0
;
const
auto
* buffer =
static_cast
<
const
unsigned
char
*>(data);
const
auto
ptr =
StbPtr
(
stbi_load_from_memory
(buffer,
static_cast
<
int
>(size), &width, &height, &channels, STBI_rgb_alpha));
if
(ptr)
{
return
Image
(
Vector2u
(
Vector2i
(width, height)), {ptr.
get
(), ptr.
get
() + width * height *
4
});
}
else
{
//
Error, failed to load the image
err
() <<
"
Failed to load image from memory. Reason:
"
<<
stbi_failure_reason
() << std::endl;
return
std::
nullopt
;
}
}
else
{
err
() <<
"
Failed to load image from memory, no data provided
"
<< std::endl;
return
std::
nullopt
;
}
}
//
//////////////////////////////////////////////////////////
std::optional<Image>
Image::loadFromStream
(InputStream& stream)
{
//
Make sure that the stream's reading position is at the beginning
if
(!stream.
seek
(
0
).
has_value
())
{
err
() <<
"
Failed to seek image stream
"
<< std::endl;
return
std::
nullopt
;
}
//
Setup the stb_image callbacks
stbi_io_callbacks callbacks;
callbacks.
read
= read;
callbacks.
skip
= skip;
callbacks.
eof
= eof;
//
Load the image and get a pointer to the pixels in memory
int
width =
0
;
int
height =
0
;
int
channels =
0
;
const
auto
ptr =
StbPtr
(
stbi_load_from_callbacks
(&callbacks, &stream, &width, &height, &channels, STBI_rgb_alpha));
if
(ptr)
{
return
Image
(
Vector2u
(
Vector2i
(width, height)), {ptr.
get
(), ptr.
get
() + width * height *
4
});
}
else
{
//
Error, failed to load the image
err
() <<
"
Failed to load image from stream. Reason:
"
<<
stbi_failure_reason
() << std::endl;
return
std::
nullopt
;
}
}
//
//////////////////////////////////////////////////////////
bool
Image::saveToFile
(
const
std::filesystem::path& filename)
const
{
//
Make sure the image is not empty
if
(!m_pixels.
empty
() && m_size.
x
>
0
&& m_size.
y
>
0
)
{
//
Deduce the image type from its extension
//
Extract the extension
const
std::filesystem::path extension = filename.
extension
();
const
Vector2i convertedSize =
Vector2i
(m_size);
if
(extension ==
"
.bmp
"
)
{
//
BMP format
if
(
stbi_write_bmp
(filename.
string
().
c_str
(), convertedSize.
x
, convertedSize.
y
,
4
, m_pixels.
data
()))
return
true
;
}
else
if
(extension ==
"
.tga
"
)
{
//
TGA format
if
(
stbi_write_tga
(filename.
string
().
c_str
(), convertedSize.
x
, convertedSize.
y
,
4
, m_pixels.
data
()))
return
true
;
}
else
if
(extension ==
"
.png
"
)
{
//
PNG format
if
(
stbi_write_png
(filename.
string
().
c_str
(), convertedSize.
x
, convertedSize.
y
,
4
, m_pixels.
data
(),
0
))
return
true
;
}
else
if
(extension ==
"
.jpg
"
|| extension ==
"
.jpeg
"
)
{
//
JPG format
if
(
stbi_write_jpg
(filename.
string
().
c_str
(), convertedSize.
x
, convertedSize.
y
,
4
, m_pixels.
data
(),
90
))
return
true
;
}
else
{
err
() <<
"
Image file extension
"
<< extension <<
"
not supported
\n
"
;
}
}
err
() <<
"
Failed to save image
\n
"
<<
formatDebugPathInfo
(filename) << std::endl;
return
false
;
}
//
//////////////////////////////////////////////////////////
std::optional<std::vector<std::
uint8_t
>>
Image::saveToMemory
(std::string_view format)
const
{
//
Make sure the image is not empty
if
(!m_pixels.
empty
() && m_size.
x
>
0
&& m_size.
y
>
0
)
{
//
Choose function based on format
const
std::string specified =
toLower
(
std::string
(format));
const
Vector2i convertedSize =
Vector2i
(m_size);
std::vector<std::
uint8_t
> buffer;
if
(specified ==
"
bmp
"
)
{
//
BMP format
if
(
stbi_write_bmp_to_func
(bufferFromCallback, &buffer, convertedSize.
x
, convertedSize.
y
,
4
, m_pixels.
data
()))
return
buffer;
}
else
if
(specified ==
"
tga
"
)
{
//
TGA format
if
(
stbi_write_tga_to_func
(bufferFromCallback, &buffer, convertedSize.
x
, convertedSize.
y
,
4
, m_pixels.
data
()))
return
buffer;
}
else
if
(specified ==
"
png
"
)
{
//
PNG format
if
(
stbi_write_png_to_func
(bufferFromCallback, &buffer, convertedSize.
x
, convertedSize.
y
,
4
, m_pixels.
data
(),
0
))
return
buffer;
}
else
if
(specified ==
"
jpg
"
|| specified ==
"
jpeg
"
)
{
//
JPG format
if
(
stbi_write_jpg_to_func
(bufferFromCallback, &buffer, convertedSize.
x
, convertedSize.
y
,
4
, m_pixels.
data
(),
90
))
return
buffer;
}
}
err
() <<
"
Failed to save image with format
"
<<
std::quoted
(format) << std::endl;
return
std::
nullopt
;
}
//
//////////////////////////////////////////////////////////
Vector2u
Image::getSize
()
const
{
return
m_size;
}
//
//////////////////////////////////////////////////////////
void
Image::createMaskFromColor
(
const
Color& color, std::
uint8_t
alpha)
{
//
Make sure that the image is not empty
if
(!m_pixels.
empty
())
{
//
Replace the alpha of the pixels that match the transparent color
std::
uint8_t
* ptr = m_pixels.
data
();
std::
uint8_t
* end = ptr + m_pixels.
size
();
while
(ptr < end)
{
if
((ptr[
0
] == color.
r
) && (ptr[
1
] == color.
g
) && (ptr[
2
] == color.
b
) && (ptr[
3
] == color.
a
))
ptr[
3
] = alpha;
ptr +=
4
;
}
}
}
//
//////////////////////////////////////////////////////////
[[nodiscard]]
bool
Image::copy
(
const
Image& source,
const
Vector2u& dest,
const
IntRect& sourceRect,
bool
applyAlpha)
{
//
Make sure that both images are valid
if
(source.
m_size
.
x
==
0
|| source.
m_size
.
y
==
0
|| m_size.
x
==
0
|| m_size.
y
==
0
)
return
false
;
//
Make sure the sourceRect components are non-negative before casting them to unsigned values
if
(sourceRect.
position
.
x
<
0
|| sourceRect.
position
.
y
<
0
|| sourceRect.
size
.
x
<
0
|| sourceRect.
size
.
y
<
0
)
return
false
;
Rect<
unsigned
int
>
srcRect
(sourceRect);
//
Use the whole source image as srcRect if the provided source rectangle is empty
if
(srcRect.
size
.
x
==
0
|| srcRect.
size
.
y
==
0
)
{
srcRect = Rect<
unsigned
int
>({
0
,
0
}, source.
m_size
);
}
//
Otherwise make sure the provided source rectangle fits into the source image
else
{
//
Checking the bottom right corner is enough because
//
left and top are non-negative and width and height are positive.
if
(source.
m_size
.
x
< srcRect.
position
.
x
+ srcRect.
size
.
x
|| source.
m_size
.
y
< srcRect.
position
.
y
+ srcRect.
size
.
y
)
return
false
;
}
//
Make sure the destination position is within this image bounds
if
(m_size.
x
<= dest.
x
|| m_size.
y
<= dest.
y
)
return
false
;
//
Then find the valid size of the destination rectangle
const
Vector2u
dstSize
(
std::min
(m_size.
x
- dest.
x
, srcRect.
size
.
x
),
std::min
(m_size.
y
- dest.
y
, srcRect.
size
.
y
));
//
Precompute as much as possible
const
std::
size_t
pitch =
static_cast
<std::
size_t
>(dstSize.
x
) *
4
;
const
unsigned
int
srcStride = source.
m_size
.
x
*
4
;
const
unsigned
int
dstStride = m_size.
x
*
4
;
const
std::
uint8_t
* srcPixels = source.
m_pixels
.
data
() + (srcRect.
position
.
x
+ srcRect.
position
.
y
* source.
m_size
.
x
) *
4
;
std::
uint8_t
* dstPixels = m_pixels.
data
() + (dest.
x
+ dest.
y
* m_size.
x
) *
4
;
//
Copy the pixels
if
(applyAlpha)
{
//
Interpolation using alpha values, pixel by pixel (slower)
for
(
unsigned
int
i =
0
; i < dstSize.
y
; ++i)
{
for
(
unsigned
int
j =
0
; j < dstSize.
x
; ++j)
{
//
Get a direct pointer to the components of the current pixel
const
std::
uint8_t
* src = srcPixels + j *
4
;
std::
uint8_t
* dst = dstPixels + j *
4
;
//
Interpolate RGBA components using the alpha values of the destination and source pixels
const
std::
uint8_t
srcAlpha = src[
3
];
const
std::
uint8_t
dstAlpha = dst[
3
];
const
auto
outAlpha =
static_cast
<std::
uint8_t
>(srcAlpha + dstAlpha - srcAlpha * dstAlpha /
255
);
dst[
3
] = outAlpha;
if
(outAlpha)
for
(
int
k =
0
; k <
3
; k++)
dst[k] =
static_cast
<std::
uint8_t
>((src[k] * srcAlpha + dst[k] * (outAlpha - srcAlpha)) / outAlpha);
else
for
(
int
k =
0
; k <
3
; k++)
dst[k] = src[k];
}
srcPixels += srcStride;
dstPixels += dstStride;
}
}
else
{
//
Optimized copy ignoring alpha values, row by row (faster)
for
(
unsigned
int
i =
0
; i < dstSize.
y
; ++i)
{
std::memcpy
(dstPixels, srcPixels, pitch);
srcPixels += srcStride;
dstPixels += dstStride;
}
}
return
true
;
}
//
//////////////////////////////////////////////////////////
void
Image::setPixel
(
const
Vector2u& coords,
const
Color& color)
{
assert
(coords.
x
< m_size.
x
&&
"
Image::setPixel() x coordinate is out of bounds
"
);
assert
(coords.
y
< m_size.
y
&&
"
Image::setPixel() y coordinate is out of bounds
"
);
const
auto
index = (coords.
x
+ coords.
y
* m_size.
x
) *
4
;
std::
uint8_t
* pixel = &m_pixels[index];
*pixel++ = color.
r
;
*pixel++ = color.
g
;
*pixel++ = color.
b
;
*pixel++ = color.
a
;
}
//
//////////////////////////////////////////////////////////
Color
Image::getPixel
(
const
Vector2u& coords)
const
{
assert
(coords.
x
< m_size.
x
&&
"
Image::getPixel() x coordinate is out of bounds
"
);
assert
(coords.
y
< m_size.
y
&&
"
Image::getPixel() y coordinate is out of bounds
"
);
const
auto
index = (coords.
x
+ coords.
y
* m_size.
x
) *
4
;
const
std::
uint8_t
* pixel = &m_pixels[index];
return
{pixel[
0
], pixel[
1
], pixel[
2
], pixel[
3
]};
}
//
//////////////////////////////////////////////////////////
const
std::
uint8_t
*
Image::getPixelsPtr
()
const
{
if
(!m_pixels.
empty
())
{
return
m_pixels.
data
();
}
else
{
err
() <<
"
Trying to access the pixels of an empty image
"
<< std::endl;
return
nullptr
;
}
}
//
//////////////////////////////////////////////////////////
void
Image::flipHorizontally
()
{
if
(!m_pixels.
empty
())
{
const
std::
size_t
rowSize = m_size.
x
*
4
;
for
(std::
size_t
y =
0
; y < m_size.
y
; ++y)
{
auto
left = m_pixels.
begin
() +
static_cast
<std::vector<std::
uint8_t
>::iterator::difference_type>(y * rowSize);
auto
right = m_pixels.
begin
() +
static_cast
<std::vector<std::
uint8_t
>::iterator::difference_type>((y +
1
) * rowSize -
4
);
for
(std::
size_t
x =
0
; x < m_size.
x
/
2
; ++x)
{
std::swap_ranges
(left, left +
4
, right);
left +=
4
;
right -=
4
;
}
}
}
}
//
//////////////////////////////////////////////////////////
void
Image::flipVertically
()
{
if
(!m_pixels.
empty
())
{
const
auto
rowSize =
static_cast
<std::vector<std::
uint8_t
>::iterator::difference_type>(m_size.
x
*
4
);
auto
top = m_pixels.
begin
();
auto
bottom = m_pixels.
end
() - rowSize;
for
(std::
size_t
y =
0
; y < m_size.
y
/
2
; ++y)
{
std::swap_ranges
(top, top + rowSize, bottom);
top += rowSize;
bottom -= rowSize;
}
}
}
}
//
namespace sf
Back
|
FazBrowse Home
|
New Git URL