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
/*
SDL_image: An example image loading library for use with SDL
Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.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.
*/
#include <SDL3_image/SDL_image.h>
#ifdef USE_STBIMAGE
#define malloc SDL_malloc
#define realloc SDL_realloc
#define free SDL_free
#undef memcpy
#define memcpy SDL_memcpy
#undef memset
#define memset SDL_memset
#undef strcmp
#define strcmp SDL_strcmp
#undef strncmp
#define strncmp SDL_strncmp
#define strtol SDL_strtol
#define abs SDL_abs
#define pow SDL_pow
#define ldexp SDL_scalbn
#define STB_IMAGE_STATIC
#define STBI_NO_THREAD_LOCALS
#define STBI_FAILURE_USERMSG
#if defined(__ARM_NEON)
#define STBI_NEON
#endif
#define STBI_NO_STDIO
#define STBI_ONLY_PNG
#define STBI_ONLY_JPEG
#define STBI_NO_GIF
#define STBI_NO_HDR
#define STBI_NO_LINEAR
#define STBI_ASSERT SDL_assert
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
static int IMG_LoadSTB_IO_read(void *user, char *data, int size)
{
size_t amount = SDL_ReadIO((SDL_IOStream*)user, data, size);
return (int)amount;
}
static void IMG_LoadSTB_IO_skip(void *user, int n)
{
SDL_SeekIO((SDL_IOStream*)user, n, SDL_IO_SEEK_CUR);
}
static int IMG_LoadSTB_IO_eof(void *user)
{
SDL_IOStream *src = (SDL_IOStream*)user;
return SDL_GetIOStatus(src) == SDL_IO_STATUS_EOF;
}
SDL_Surface *IMG_LoadSTB_IO(SDL_IOStream *src)
{
Sint64 start;
Uint8 magic[26];
int w, h, format;
stbi_uc *pixels;
stbi_io_callbacks rw_callbacks;
SDL_Surface *surface = NULL;
bool use_palette = false;
unsigned int palette_colors[256];
if (!src) {
/* The error message has been set in SDL_IOFromFile */
return NULL;
}
start = SDL_TellIO(src);
if (SDL_ReadIO(src, magic, sizeof(magic)) == sizeof(magic)) {
const Uint8 PNG_COLOR_INDEXED = 3;
if (magic[0] == 0x89 &&
magic[1] == 'P' &&
magic[2] == 'N' &&
magic[3] == 'G' &&
magic[12] == 'I' &&
magic[13] == 'H' &&
magic[14] == 'D' &&
magic[15] == 'R' &&
magic[25] == PNG_COLOR_INDEXED) {
use_palette = true;
}
}
SDL_SeekIO(src, start, SDL_IO_SEEK_SET);
/* Load the image data */
rw_callbacks.read = IMG_LoadSTB_IO_read;
rw_callbacks.skip = IMG_LoadSTB_IO_skip;
rw_callbacks.eof = IMG_LoadSTB_IO_eof;
w = h = format = 0; /* silence warning */
if (use_palette) {
/* Unused palette entries will be opaque white */
SDL_memset(palette_colors, 0xff, sizeof(palette_colors));
pixels = stbi_load_from_callbacks_with_palette(
&rw_callbacks,
src,
&w,
&h,
palette_colors,
SDL_arraysize(palette_colors)
);
} else {
pixels = stbi_load_from_callbacks(
&rw_callbacks,
src,
&w,
&h,
&format,
STBI_default
);
}
if (!pixels) {
SDL_SeekIO(src, start, SDL_IO_SEEK_SET);
return NULL;
}
if (use_palette) {
surface = SDL_CreateSurfaceFrom(
w,
h,
SDL_PIXELFORMAT_INDEX8,
pixels,
w
);
if (surface) {
bool has_colorkey = false;
int colorkey_index = -1;
bool has_alpha = false;
SDL_Palette *palette = SDL_CreateSurfacePalette(surface);
if (palette) {
int i;
Uint8 *palette_bytes = (Uint8 *)palette_colors;
for (i = 0; i < palette->ncolors; i++) {
palette->colors[i].r = *palette_bytes++;
palette->colors[i].g = *palette_bytes++;
palette->colors[i].b = *palette_bytes++;
palette->colors[i].a = *palette_bytes++;
if (palette->colors[i].a != SDL_ALPHA_OPAQUE) {
if (palette->colors[i].a == SDL_ALPHA_TRANSPARENT && !has_colorkey) {
has_colorkey = true;
colorkey_index = i;
} else {
/* Partial opacity or multiple colorkeys */
has_alpha = true;
}
}
}
}
if (has_alpha) {
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
} else if (has_colorkey) {
/* remove redundant pixel alpha before setting colorkey */
palette->colors[colorkey_index].a = SDL_ALPHA_OPAQUE;
SDL_SetSurfaceColorKey(surface, true, colorkey_index);
}
/* FIXME: This sucks. It'd be better to allocate the surface first, then
* write directly to the pixel buffer:
* https://github.com/nothings/stb/issues/58
* -flibit
*/
surface->flags &= ~SDL_SURFACE_PREALLOCATED;
}
} else if (format == STBI_grey || format == STBI_rgb || format == STBI_rgb_alpha) {
surface = SDL_CreateSurfaceFrom(
w,
h,
(format == STBI_rgb_alpha) ? SDL_PIXELFORMAT_RGBA32 :
(format == STBI_rgb) ? SDL_PIXELFORMAT_RGB24 :
SDL_PIXELFORMAT_INDEX8,
pixels,
w * format
);
if (surface) {
/* Set a grayscale palette for gray images */
if (surface->format == SDL_PIXELFORMAT_INDEX8) {
SDL_Palette *palette = SDL_CreateSurfacePalette(surface);
if (palette) {
int i;
for (i = 0; i < palette->ncolors; i++) {
palette->colors[i].r = (Uint8)i;
palette->colors[i].g = (Uint8)i;
palette->colors[i].b = (Uint8)i;
}
}
}
/* FIXME: This sucks. It'd be better to allocate the surface first, then
* write directly to the pixel buffer:
* https://github.com/nothings/stb/issues/58
* -flibit
*/
surface->flags &= ~SDL_SURFACE_PREALLOCATED;
}
} else if (format == STBI_grey_alpha) {
surface = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_RGBA32);
if (surface) {
Uint8 *src_ptr = pixels;
Uint8 *dst = (Uint8 *)surface->pixels;
int skip = surface->pitch - (surface->w * 4);
int row, col;
for (row = 0; row < h; ++row) {
for (col = 0; col < w; ++col) {
Uint8 c = *src_ptr++;
Uint8 a = *src_ptr++;
*dst++ = c;
*dst++ = c;
*dst++ = c;
*dst++ = a;
}
dst += skip;
}
stbi_image_free(pixels);
}
} else {
SDL_SetError("Unknown image format: %d", format);
}
if (!surface) {
/* The error message should already be set */
stbi_image_free(pixels); /* calls SDL_free() */
SDL_SeekIO(src, start, SDL_IO_SEEK_SET);
}
return surface;
}
#endif /* USE_STBIMAGE */