Debian Codegraph @

src/cdogs/stb/SDL_stbimage.h

raw symbols SCIP
1 /*
2 * A small header-only library to load an image into a RGB(A) SDL_Surface*,
3 * like a stripped down version of SDL_Image, but using stb_image.h to decode
4 * images and thus without any further external dependencies.
5 * Supports all filetypes supported by stb_image (JPEG, PNG, TGA, BMP, PSD, ...
6 * See stb_image.h for details).
7 *
8 * (C) 2015-2021 Daniel Gibson
9 *
10 * Homepage: https://github.com/DanielGibson/Snippets/
11 *
12 * Dependencies:
13 * libSDL2 http://www.libsdl.org
14 * stb_image.h https://github.com/nothings/stb
15 *
16 * Usage:
17 * Put this file and stb_image.h somewhere in your project.
18 * In *one* of your .c/.cpp files, do
19 * #define SDL_STBIMAGE_IMPLEMENTATION
20 * #include "SDL_stbimage.h"
21 * to create the implementation of this library in that file.
22 * You can just #include "SDL_stbimage.h" (without the #define) in other source
23 * files to use it there. (See also below this comment for an usage example)
24 * This header implicitly #includes <SDL.h> and "stb_image.h".
25 *
26 * NOTE: When using SDL3, either #include <SDL3/SDL.h> before this header,
27 * or #define SDL_STBIMG_SDL3 before including it.
28 *
29 * You can #define SDL_STBIMG_DEF before including this header if you want to
30 * prepend anything to the function signatures (like "static", "inline",
31 * "__declspec(dllexport)", ...)
32 * Example: #define SDL_STBIMG_DEF static inline
33 *
34 * By default, this deactivates stb_image's load from file functions via
35 * #define STBI_NO_STDIO, as they use stdio.h and that adds a dependency to the
36 * CRT on windows and with SDL you're better off using SDL_RWops, incl. SDL_RWFromFile()
37 * (or the SDL3 equivalents: SDL_IOStream, SDL_IOFromFile())
38 * If you wanna use stbi_load(), stbi_info(), stbi_load_from_file() etc anyway, do
39 * #define SDL_STBIMG_ALLOW_STDIO
40 * before including this header.
41 * (Note that all the STBIMG_* functions of this lib will work without it)
42 *
43 * stb_image.h uses assert.h by default. You can #define STBI_ASSERT(x)
44 * before the implementation-#include of SDL_stbimage.h to avoid that.
45 * By default stb_image supports HDR images, for that it needs pow() from libm.
46 * If you don't need HDR (it can't be loaded into a SDL_Surface anyway),
47 * #define STBI_NO_LINEAR and #define STBI_NO_HDR before including this header.
48 *
49 * License:
50 * This software is dual-licensed to the public domain and under the following
51 * license: you are granted a perpetual, irrevocable license to copy, modify,
52 * publish, and distribute this file as you see fit.
53 * No warranty implied; use at your own risk.
54 *
55 * So you can do whatever you want with this code, including copying it
56 * (or parts of it) into your own source.
57 * No need to mention me or this "license" in your code or docs, even though
58 * it would be appreciated, of course.
59 */
60
61 #if 0 // Usage Example:
62 #define SDL_STBIMAGE_IMPLEMENTATION
63 #include "SDL_stbimage.h"
64
65 void yourFunction(const char* imageFilePath)
66 {
67 SDL_Surface* surf = STBIMG_Load(imageFilePath);
68 if(surf == NULL) {
69 printf("ERROR: Couldn't load %s, reason: %s\n", imageFilePath, SDL_GetError());
70 exit(1);
71 }
72
73 // ... do something with surf ...
74
75 SDL_FreeSurface(surf); // or SDL_DestroySurface(surf); for SDL3
76 }
77 #endif // 0 (usage example)
78
79
80 #ifndef _SDL_STBIMAGE_H
81 #define _SDL_STBIMAGE_H
82
83 // for SDL3, you either can #define SDL_STBIMG_SDL3 yourself
84 // OR #include <SDL3/SDL.h> yourself before this header
85 // (the following lines handle for the second case)
86 #ifndef SDL_VERSION_ATLEAST
87 #ifdef SDL_STBIMG_SDL3
88 // in SDL3 this is the official way to include SDL.h
89 #include <SDL3/SDL.h>
90 #else // SDL2 and SDL1.2
91 // if you really think you need <SDL2/SDL.h> here instead.. feel free to change it,
92 // but the cool kids have path/to/include/SDL2/ in their compilers include path.
93 // (meaning, that's how was supposed to be done for SDL2 and what sdl2-config supports)
94 #include <SDL.h>
95 #endif
96 // else if it *is* defined, SDL.h was already included before including this header.
97 // now, after including the headers, it definitely is available.
98 #endif // SDL_VERSION_ATLEAST not defined
99
100 #if !SDL_VERSION_ATLEAST(3, 0, 0) && !defined(__cpluscplus)
101 // allow using bool. In C++ it's a builtin type,
102 // in SDL3 SDL_stdinc.h (through SDL.h) makes sure it's available
103 #include <stdbool.h>
104 #endif
105
106 #ifndef SDL_STBIMG_ALLOW_STDIO
107 #define STBI_NO_STDIO // don't need STDIO, will use SDL_RWops to open files
108 #endif
109 #include "stb_image.h"
110
111 // this allows you to prepend stuff to function signatures, e.g. "static"
112 #ifndef SDL_STBIMG_DEF
113 // by default it's empty
114 #define SDL_STBIMG_DEF
115 #endif // DG_MISC_DEF
116
117
118 #ifdef __cplusplus
119 extern "C" {
120 #endif
121
122 // loads the image file at the given path into a RGB(A) SDL_Surface
123 // Returns NULL on error, use SDL_GetError() to get more information.
124 SDL_STBIMG_DEF SDL_Surface* STBIMG_Load(const char* file);
125
126 // loads the image file in the given memory buffer into a RGB(A) SDL_Surface
127 // Returns NULL on error, use SDL_GetError() to get more information.
128 SDL_STBIMG_DEF SDL_Surface* STBIMG_LoadFromMemory(const unsigned char* buffer, int length);
129
130
131 // loads an image file into a RGB(A) SDL_Surface from a seekable SDL_IOStream/SDL_RWops (src)
132 // if you set freesrc to true, SDL_CloseIO(src)/SDL_RWclose(src) will be executed after reading.
133 // Returns NULL on error, use SDL_GetError() to get more information.
134 #if SDL_VERSION_ATLEAST(3, 0, 0)
135 SDL_STBIMG_DEF SDL_Surface* STBIMG_Load_IO(SDL_IOStream* src, bool freesrc);
136 #else // SDL2 or SDL1.2
137 SDL_STBIMG_DEF SDL_Surface* STBIMG_Load_RW(SDL_RWops* src, bool freesrc);
138 #endif
139
140 // Creates an SDL_Surface* using the raw RGB(A) pixelData with given width/height
141 // (this doesn't use stb_image and is just a simple SDL_CreateSurfaceFrom()-wrapper)
142 // ! It must be byte-wise 24bit RGB ("888", bytesPerPixel=3) !
143 // ! or byte-wise 32bit RGBA ("8888", bytesPerPixel=4) data !
144 // If freeWithSurface is true, SDL_FreeSurface() will free the pixelData
145 // you passed with SDL_free() - NOTE that you should only do that if pixelData
146 // was allocated with SDL_malloc(), SDL_calloc() or SDL_realloc()!
147 // Returns NULL on error (in that case pixelData won't be freed!),
148 // use SDL_GetError() to get more information.
149 SDL_STBIMG_DEF SDL_Surface* STBIMG_CreateSurface(unsigned char* pixelData, int width, int height,
150 int bytesPerPixel, bool freeWithSurface);
151
152 #if SDL_MAJOR_VERSION > 1
153 // loads the image file at the given path into a RGB(A) SDL_Texture
154 // Returns NULL on error, use SDL_GetError() to get more information.
155 SDL_STBIMG_DEF SDL_Texture*
156 STBIMG_LoadTexture(SDL_Renderer* renderer, const char* file);
157
158 // loads the image file in the given memory buffer into a RGB(A) SDL_Texture
159 // Returns NULL on error, use SDL_GetError() to get more information.
160 SDL_STBIMG_DEF SDL_Texture*
161 STBIMG_LoadTextureFromMemory(SDL_Renderer* renderer, const unsigned char* buffer, int length);
162
163 // loads an image file into a RGB(A) SDL_Texture from a seekable SDL_IOStream/SDL_RWops (src)
164 // if you set freesrc to true, SDL_CloseIO(src)/SDL_RWclose(src) will be executed after reading.
165 // Returns NULL on error, use SDL_GetError() to get more information.
166 #if SDL_VERSION_ATLEAST(3, 0, 0)
167 SDL_STBIMG_DEF SDL_Texture*
168 STBIMG_LoadTexture_IO(SDL_Renderer* renderer, SDL_IOStream* src, bool freesrc);
169 #else // SDL2
170 SDL_STBIMG_DEF SDL_Texture*
171 STBIMG_LoadTexture_RW(SDL_Renderer* renderer, SDL_RWops* src, bool freesrc);
172 #endif
173
174 // Creates an SDL_Texture* using the raw RGB(A) pixelData with given width/height
175 // (this doesn't use stb_image and is just a simple SDL_CreateSurfaceFrom()-wrapper)
176 // ! It must be byte-wise 24bit RGB ("888", bytesPerPixel=3) !
177 // ! or byte-wise 32bit RGBA ("8888", bytesPerPixel=4) data !
178 // Returns NULL on error, use SDL_GetError() to get more information.
179 SDL_STBIMG_DEF SDL_Texture*
180 STBIMG_CreateTexture(SDL_Renderer* renderer, const unsigned char* pixelData,
181 int width, int height, int bytesPerPixel);
182 #endif // SDL_MAJOR_VERSION > 1
183
184
185 typedef struct {
186 #if SDL_VERSION_ATLEAST(3, 0, 0)
187 SDL_IOStream* src;
188 #else
189 SDL_RWops* src;
190 #endif
191 stbi_io_callbacks stb_cbs;
192 int atEOF; // defaults to 0; 1: reached EOF or error on read, 2: error on seek
193 } STBIMG_stbio_RWops;
194
195 // creates stbi_io_callbacks and userdata to use stbi_*_from_callbacks() directly,
196 // especially useful to use SDL_IOStream/SDL_RWops with stb_image, without using SDL_Surface
197 // src must be readable and seekable!
198 // Returns false on error (SDL_GetError() will give you info), else true
199 // NOTE: If you want to use src twice (e.g. for info and load), remember to rewind
200 // it by seeking back to its initial position and resetting out->atEOF to 0
201 // inbetween the uses!
202 #if SDL_VERSION_ATLEAST(3, 0, 0)
203 SDL_STBIMG_DEF bool STBIMG_stbi_callback_from_IO(SDL_IOStream* src, STBIMG_stbio_RWops* out);
204 #else
205 SDL_STBIMG_DEF bool STBIMG_stbi_callback_from_RW(SDL_RWops* src, STBIMG_stbio_RWops* out);
206 #endif
207
208 #if 0 // Use STBIMG_stbi_callback_from_RW() like this:
209 SDL_RWops* src = ...; // wherever it's from
210 STBIMG_stbio_RWops io;
211 if(!STBIMG_stbi_callback_from_RW(src, &io)) {
212 printf("ERROR creating stbio callbacks: %s\n", SDL_GetError());
213 exit(1);
214 }
215 Sint64 origSrcPosition = SDL_RWtell(src);
216 int w, h, fmt;
217 if(!stbi_info_from_callbacks(&io.stb_cbs, &io, &w, &h, &fmt)) {
218 printf("stbi_info_from_callbacks() failed, reason: %s\n", stbi_failure_reason());
219 exit(1);
220 }
221 printf("image is %d x %d pixels with %d bytes per pixel\n", w, h, fmt);
222
223 // rewind src before using it again in stbi_load_from_callbacks()
224 if(SDL_RWseek(src, origSrcPosition, RW_SEEK_SET) < 0)
225 {
226 printf("ERROR: src not be seekable!\n");
227 exit(1);
228 }
229 io.atEOF = 0; // remember to reset atEOF, too!
230
231 unsigned char* data;
232 data = stbi_load_from_callbacks(&io.stb_cbs, &io, &w, &h, &fmt, 0);
233 if(data == NULL) {
234 printf("stbi_load_from_callbacks() failed, reason: %s\n", stbi_failure_reason());
235 exit(1);
236 }
237 // ... do something with data ...
238 stbi_image_free(data);
239 #endif // 0 (STBIMG_stbi_callback_from_RW() example)
240
241
242 #if SDL_MAJOR_VERSION > 1
243 // loads an image file into a RGB(A) SDL_Surface from a SDL_RWops/SDL_IOStream (src)
244 // - without using SDL_RWseek()/SDL_SeekIO, for streams that don't support or are slow
245 // at seeking. It reads everything into a buffer and calls STBIMG_LoadFromMemory()
246 // You should probably only use this if you *really* have performance problems
247 // because of seeking or your src doesn't support SDL_RWseek()/SDL_SeekIO, but SDL_RWsize()/SDL_GetIOSize
248 // src must at least support SDL_RWread()/SDL_ReadIO() and SDL_RWsize()/SDL_GetIOSize
249 // if you set freesrc to true, SDL_RWclose(src)/SDL_CloseIO(src) will be executed after reading.
250 // Returns NULL on error, use SDL_GetError() to get more information.
251 #if SDL_VERSION_ATLEAST(3, 0, 0)
252 SDL_STBIMG_DEF SDL_Surface* STBIMG_Load_IO_noSeek(SDL_IOStream* src, bool freesrc);
253 #else
254 SDL_STBIMG_DEF SDL_Surface* STBIMG_Load_RW_noSeek(SDL_RWops* src, bool freesrc);
255 #endif
256
257 // the same for textures (you should probably not use this one, either..)
258 #if SDL_VERSION_ATLEAST(3, 0, 0)
259 SDL_STBIMG_DEF SDL_Texture* STBIMG_LoadTexture_IO_noSeek(SDL_Renderer* renderer, SDL_IOStream* src, bool freesrc);
260 #else
261 SDL_STBIMG_DEF SDL_Texture* STBIMG_LoadTexture_RW_noSeek(SDL_Renderer* renderer, SDL_RWops* src, bool freesrc);
262 #endif
263
264 #endif // SDL_MAJOR_VERSION > 1
265
266 #ifdef __cplusplus
267 } // extern "C"
268 #endif
269
270 // provide backwards-compatibility for the function names
271 #if SDL_VERSION_ATLEAST(3, 0, 0)
272 #define STBIMG_Load_RW STBIMG_Load_IO
273 #define STBIMG_LoadTexture_RW STBIMG_LoadTexture_IO
274 #define STBIMG_stbi_callback_from_RW STBIMG_stbi_callback_from_IO
275 #define STBIMG_Load_RW_noSeek STBIMG_Load_IO_noSeek
276 #define STBIMG_LoadTexture_RW_noSeek STBIMG_LoadTexture_IO_noSeek
277 #endif
278
279 #endif // _SDL_STBIMAGE_H
280
281
282 // ############# Below: Implementation ###############
283
284
285 #ifdef SDL_STBIMAGE_IMPLEMENTATION
286
287 // make stb_image use SDL_malloc etc, so SDL_FreeSurface() can SDL_free()
288 // the data allocated by stb_image
289 #define STBI_MALLOC SDL_malloc
290 #define STBI_REALLOC SDL_realloc
291 #define STBI_FREE SDL_free
292 #define STB_IMAGE_IMPLEMENTATION
293 #ifndef SDL_STBIMG_ALLOW_STDIO
294 #define STBI_NO_STDIO // don't need STDIO, will use SDL_RWops to open files
295 #endif
296 #include "stb_image.h"
297
298 // Note: In the header part above I kept things simple, but here
299 // I'm using macros to unify SDL1.2/SDL2 and SDL3 RWops/IOStream code
300 #if SDL_VERSION_ATLEAST(3, 0, 0)
301
302 #define _STBIMG_RWops SDL_IOStream
303
304 #define _STBIMG_RW_SEEK_CUR SDL_IO_SEEK_CUR
305 #define _STBIMG_RW_SEEK_SET SDL_IO_SEEK_SET
306
307 #define _STBIMG_RWread(CTX, PTR, SIZE) SDL_ReadIO(CTX, PTR, SIZE)
308 #define _STBIMG_RWseek(CTX, OFFSET, WHENCE) SDL_SeekIO(CTX, OFFSET, WHENCE)
309 #define _STBIMG_RWtell(CTX) SDL_TellIO(CTX)
310 #define _STBIMG_RWclose(CTX) SDL_CloseIO(CTX)
311 #define _STBIMG_RWsize(CTX) SDL_GetIOSize(CTX)
312 #define _STBIMG_RWFromFile(FILENAME, MODE) SDL_IOFromFile(FILENAME, MODE)
313
314 #else // SDL2 or SDL1.2
315
316 #define _STBIMG_RWops SDL_RWops
317
318 #define _STBIMG_RW_SEEK_CUR RW_SEEK_CUR
319 #define _STBIMG_RW_SEEK_SET RW_SEEK_SET
320
321 #define _STBIMG_RWread(CTX, PTR, SIZE) SDL_RWread(CTX, PTR, 1, SIZE)
322 #define _STBIMG_RWseek(CTX, OFFSET, WHENCE) SDL_RWseek(CTX, OFFSET, WHENCE)
323 #define _STBIMG_RWtell(CTX) SDL_RWtell(CTX)
324 #define _STBIMG_RWclose(CTX) SDL_RWclose(CTX)
325 #define _STBIMG_RWsize(CTX) SDL_RWsize(CTX)
326 #define _STBIMG_RWFromFile(FILENAME, MODE) SDL_RWFromFile(FILENAME, MODE)
327
328 #endif
329
330 typedef struct {
331 unsigned char* data;
332 int w;
333 int h;
334 int format; // 3: RGB, 4: RGBA
335 } _STBIMG_image;
336
337 static SDL_Surface* _STBIMG_CreateSurfaceImpl(_STBIMG_image img, int freeWithSurface)
338 {
339 SDL_Surface* surf = NULL;
340
341 #if SDL_VERSION_ATLEAST(3, 0, 0)
342
343 Uint32 format = (img.format == STBI_rgb) ? SDL_PIXELFORMAT_RGB24 : SDL_PIXELFORMAT_RGBA32;
344 surf = SDL_CreateSurfaceFrom(img.w, img.h, format, img.data, img.format*img.w);
345
346 #elif SDL_VERSION_ATLEAST(2, 0, 5)
347
348 // SDL 2.0.5 introduced SDL_CreateRGBSurfaceWithFormatFrom() and SDL_PIXELFORMAT_RGBA32
349 // which makes this code much simpler.
350
351 Uint32 format = (img.format == STBI_rgb) ? SDL_PIXELFORMAT_RGB24 : SDL_PIXELFORMAT_RGBA32;
352
353 surf = SDL_CreateRGBSurfaceWithFormatFrom((void*)img.data, img.w, img.h,
354 img.format*8, img.format*img.w, format);
355
356 #else // older SDL2 version without SDL_CreateRGBSurfaceWithFormatFrom()
357
358 Uint32 rmask, gmask, bmask, amask;
359 // ok, the following is pretty stupid.. SDL_CreateRGBSurfaceFrom() pretends to use
360 // a void* for the data, but it's really treated as endian-specific Uint32*
361 // and there isn't even an SDL_PIXELFORMAT_* for 32bit byte-wise RGBA
362 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
363 int shift = (img.format == STBI_rgb) ? 8 : 0;
364 rmask = 0xff000000 >> shift;
365 gmask = 0x00ff0000 >> shift;
366 bmask = 0x0000ff00 >> shift;
367 amask = 0x000000ff >> shift;
368 #else // little endian, like x86
369 rmask = 0x000000ff;
370 gmask = 0x0000ff00;
371 bmask = 0x00ff0000;
372 amask = (img.format == STBI_rgb) ? 0 : 0xff000000;
373 #endif
374
375 surf = SDL_CreateRGBSurfaceFrom((void*)img.data, img.w, img.h,
376 img.format*8, img.format*img.w,
377 rmask, gmask, bmask, amask);
378 #endif // SDL_VERSION_ATLEAST
379
380 if(surf == NULL)
381 {
382 // hopefully SDL_CreateRGBSurfaceFrom() has set an sdl error
383 return NULL;
384 }
385
386 if(freeWithSurface)
387 {
388 // SDL_Surface::flags is documented to be read-only.. but if the pixeldata
389 // has been allocated with SDL_malloc()/SDL_calloc()/SDL_realloc() this
390 // should work (and it currently does) + @icculus said it's reasonably safe:
391 // https://twitter.com/icculus/status/667036586610139137 :-)
392 // clear the SDL_PREALLOC flag, so SDL_FreeSurface() free()s the data passed from img.data
393 #if SDL_VERSION_ATLEAST(3, 0, 0)
394 // I hope that's still true for SDL3 :-p
395 surf->flags &= ~SDL_SURFACE_PREALLOCATED;
396 #else
397 surf->flags &= ~SDL_PREALLOC;
398 #endif
399 }
400
401 return surf;
402 }
403
404
405 SDL_STBIMG_DEF SDL_Surface* STBIMG_LoadFromMemory(const unsigned char* buffer, int length)
406 {
407 _STBIMG_image img = {0};
408 int bppToUse = 0;
409 int inforet = 0;
410 SDL_Surface* ret = NULL;
411
412 if(buffer == NULL)
413 {
414 SDL_SetError("STBIMG_LoadFromMemory(): passed buffer was NULL!");
415 return NULL;
416 }
417 if(length <= 0)
418 {
419 SDL_SetError("STBIMG_LoadFromMemory(): passed invalid length: %d!", length);
420 return NULL;
421 }
422
423 inforet = stbi_info_from_memory(buffer, length, &img.w, &img.h, &img.format);
424 if(!inforet)
425 {
426 SDL_SetError("STBIMG_LoadFromMemory(): Couldn't get image info: %s!\n", stbi_failure_reason());
427 return NULL;
428 }
429
430 // no alpha => use RGB, else use RGBA
431 bppToUse = (img.format == STBI_grey || img.format == STBI_rgb) ? STBI_rgb : STBI_rgb_alpha;
432
433 img.data = stbi_load_from_memory(buffer, length, &img.w, &img.h, &img.format, bppToUse);
434 if(img.data == NULL)
435 {
436 SDL_SetError("STBIMG_LoadFromMemory(): Couldn't load image: %s!\n", stbi_failure_reason());
437 return NULL;
438 }
439 img.format = bppToUse;
440
441 ret = _STBIMG_CreateSurfaceImpl(img, 1);
442
443 if(ret == NULL)
444 {
445 // no need to log an error here, it was an SDL error which should still be available through SDL_GetError()
446 SDL_free(img.data);
447 return NULL;
448 }
449
450 return ret;
451 }
452
453
454 // fill 'data' with 'size' bytes. return number of bytes actually read
455 static int _STBIMG_io_read(void* user, char* data, int size)
456 {
457 STBIMG_stbio_RWops* io = (STBIMG_stbio_RWops*)user;
458
459 size_t ret = _STBIMG_RWread(io->src, data, size*sizeof(char));
460 if(ret == 0)
461 {
462 // we're at EOF or some error happend
463 io->atEOF = 1;
464 }
465 return (int)ret*sizeof(char);
466 }
467
468 // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
469 static void _STBIMG_io_skip(void* user, int n)
470 {
471 STBIMG_stbio_RWops* io = (STBIMG_stbio_RWops*)user;
472
473 if(_STBIMG_RWseek(io->src, n, _STBIMG_RW_SEEK_CUR) == -1)
474 {
475 // an error happened during seeking, hopefully setting EOF will make stb_image abort
476 io->atEOF = 2; // set this to 2 for "aborting because seeking failed" (stb_image only cares about != 0)
477 }
478 }
479
480 // returns nonzero if we are at end of file/data
481 static int _STBIMG_io_eof(void* user)
482 {
483 STBIMG_stbio_RWops* io = (STBIMG_stbio_RWops*)user;
484 return io->atEOF;
485 }
486
487 #if SDL_VERSION_ATLEAST(3, 0, 0)
488 SDL_STBIMG_DEF bool STBIMG_stbi_callback_from_IO(_STBIMG_RWops* src, STBIMG_stbio_RWops* out)
489 #else
490 SDL_STBIMG_DEF bool STBIMG_stbi_callback_from_RW(_STBIMG_RWops* src, STBIMG_stbio_RWops* out)
491 #endif
492 {
493 if(out == NULL)
494 {
495 SDL_SetError("STBIMG_stbi_callback_from_RW(): out must not be NULL!");
496 return false;
497 }
498
499 // make sure out is at least initialized to something deterministic
500 memset(out, 0, sizeof(*out));
501
502 if(src == NULL)
503 {
504 SDL_SetError("STBIMG_stbi_callback_from_RW(): src must not be NULL!");
505 return false;
506 }
507
508 out->src = src;
509 out->atEOF = 0;
510 out->stb_cbs.read = _STBIMG_io_read;
511 out->stb_cbs.skip = _STBIMG_io_skip;
512 out->stb_cbs.eof = _STBIMG_io_eof;
513
514 return true;
515 }
516
517 #if SDL_VERSION_ATLEAST(3, 0, 0)
518 SDL_STBIMG_DEF SDL_Surface* STBIMG_Load_IO(_STBIMG_RWops* src, bool freesrc)
519 #else
520 SDL_STBIMG_DEF SDL_Surface* STBIMG_Load_RW(_STBIMG_RWops* src, bool freesrc)
521 #endif
522 {
523 _STBIMG_image img = {0};
524 int bppToUse = 0;
525 int inforet = 0;
526 SDL_Surface* ret = NULL;
527 Sint64 srcOffset = 0;
528
529 STBIMG_stbio_RWops cbData;
530
531 if(src == NULL)
532 {
533 SDL_SetError("STBIMG_Load_RW(): src was NULL!");
534 return NULL;
535 }
536
537 srcOffset = _STBIMG_RWtell(src);
538 if(srcOffset < 0)
539 {
540 SDL_SetError("STBIMG_Load_RW(): src must be seekable, maybe use STBIMG_Load_RW_noSeek() instead!");
541 // TODO: or do that automatically? but I think the user should be aware of what they're doing
542 goto end;
543 }
544
545 #if SDL_VERSION_ATLEAST(3, 0, 0)
546 if(!STBIMG_stbi_callback_from_IO(src, &cbData))
547 #else
548 if(!STBIMG_stbi_callback_from_RW(src, &cbData))
549 #endif
550 {
551 goto end;
552 }
553
554 inforet = stbi_info_from_callbacks(&cbData.stb_cbs, &cbData, &img.w, &img.h, &img.format);
555 if(!inforet)
556 {
557 if(cbData.atEOF == 2) SDL_SetError("STBIMG_Load_RW(): src must be seekable!");
558 else SDL_SetError("STBIMG_Load_RW(): Couldn't get image info: %s!\n", stbi_failure_reason());
559 goto end;
560 }
561
562 // rewind src so stbi_load_from_callbacks() will start reading from the beginning again
563 if(_STBIMG_RWseek(src, srcOffset, _STBIMG_RW_SEEK_SET) < 0)
564 {
565 SDL_SetError("STBIMG_Load_RW(): src must be seekable!");
566 goto end;
567 }
568
569 cbData.atEOF = 0; // we've rewinded (rewound?)
570
571 // no alpha => use RGB, else use RGBA
572 bppToUse = (img.format == STBI_grey || img.format == STBI_rgb) ? STBI_rgb : STBI_rgb_alpha;
573
574 img.data = stbi_load_from_callbacks(&cbData.stb_cbs, &cbData, &img.w, &img.h, &img.format, bppToUse);
575 if(img.data == NULL)
576 {
577 SDL_SetError("STBIMG_Load_RW(): Couldn't load image: %s!\n", stbi_failure_reason());
578 goto end;
579 }
580 img.format = bppToUse;
581
582 ret = _STBIMG_CreateSurfaceImpl(img, 1);
583
584 if(ret == NULL)
585 {
586 // no need to log an error here, it was an SDL error which should still be available through SDL_GetError()
587 SDL_free(img.data);
588 img.data = NULL;
589 goto end;
590 }
591
592 end:
593 if(freesrc)
594 {
595 _STBIMG_RWclose(src);
596 }
597 else if(img.data == NULL)
598 {
599 // if data is still NULL, there was an error and we should probably
600 // seek src back to where it was when this function was called
601 _STBIMG_RWseek(src, srcOffset, _STBIMG_RW_SEEK_SET);
602 }
603
604 return ret;
605 }
606
607 #if SDL_MAJOR_VERSION > 1
608
609 #if SDL_VERSION_ATLEAST(3, 0, 0)
610 SDL_STBIMG_DEF SDL_Surface* STBIMG_Load_IO_noSeek(_STBIMG_RWops* src, bool freesrc)
611 #else
612 SDL_STBIMG_DEF SDL_Surface* STBIMG_Load_RW_noSeek(_STBIMG_RWops* src, bool freesrc)
613 #endif
614 {
615 unsigned char* buf = NULL;
616 Sint64 fileSize = 0, bytesRead=0;
617 SDL_Surface* ret = NULL;
618
619 if(src == NULL)
620 {
621 SDL_SetError("STBIMG_Load_RW_noSeek(): src was NULL!");
622 return NULL;
623 }
624
625 fileSize = _STBIMG_RWsize(src);
626 if(fileSize < 0)
627 {
628 goto end; // SDL should have set an error already
629 }
630 else if(fileSize == 0)
631 {
632 SDL_SetError("STBIMG_Load_RW_noSeek(): SDL_RWsize(src) returned 0 => empty file/stream?!");
633 goto end;
634 }
635 else if(fileSize > 0x7FFFFFFF)
636 {
637 // stb_image.h uses ints for all sizes, so we can't support more
638 // (but >2GB images are insane anyway)
639 SDL_SetError("STBIMG_Load_RW_noSeek(): SDL_RWsize(src) too big (> 2GB)!");
640 goto end;
641 }
642
643 buf = (unsigned char*)SDL_malloc((size_t)fileSize);
644 if(buf == NULL)
645 {
646 SDL_SetError("STBIMG_Load_RW_noSeek(): Couldn't allocate buffer to read src into!");
647 goto end;
648 }
649
650 while(bytesRead < fileSize) {
651 size_t read = _STBIMG_RWread(src, buf+bytesRead, (size_t)(fileSize-bytesRead));
652 if(read == 0) {
653 // TODO: set error? SDL_RWread/SDL_ReadIO should set one, I think?
654 goto end;
655 }
656 bytesRead += read;
657 }
658
659 // if that fails, STBIMG_LoadFromMemory() has set an SDL error
660 // and ret is NULL, so nothing special to do for us
661 ret = STBIMG_LoadFromMemory(buf, (int)fileSize);
662
663 end:
664 if(freesrc)
665 {
666 _STBIMG_RWclose(src);
667 }
668
669 SDL_free(buf);
670 return ret;
671 }
672 #endif // SDL_MAJOR_VERSION > 1
673
674
675 SDL_STBIMG_DEF SDL_Surface* STBIMG_Load(const char* file)
676 {
677 _STBIMG_RWops* src = _STBIMG_RWFromFile(file, "rb");
678 if(src == NULL) return NULL;
679 #if SDL_VERSION_ATLEAST(3, 0, 0)
680 return STBIMG_Load_IO(src, 1);
681 #else
682 return STBIMG_Load_RW(src, 1);
683 #endif
684 }
685
686
687 SDL_STBIMG_DEF SDL_Surface* STBIMG_CreateSurface(unsigned char* pixelData, int width, int height, int bytesPerPixel, bool freeWithSurface)
688 {
689 _STBIMG_image img;
690
691 if(pixelData == NULL)
692 {
693 SDL_SetError("STBIMG_CreateSurface(): passed pixelData was NULL!");
694 return NULL;
695 }
696 if(bytesPerPixel != 3 && bytesPerPixel != 4)
697 {
698 SDL_SetError("STBIMG_CreateSurface(): passed bytesPerPixel = %d, only 3 (24bit RGB) and 4 (32bit RGBA) are allowed!", bytesPerPixel);
699 return NULL;
700 }
701 if(width <= 0 || height <= 0)
702 {
703 SDL_SetError("STBIMG_CreateSurface(): width and height must be > 0!");
704 return NULL;
705 }
706
707 img.data = pixelData;
708 img.w = width;
709 img.h = height;
710 img.format = bytesPerPixel;
711
712 return _STBIMG_CreateSurfaceImpl(img, freeWithSurface);
713 }
714
715 #if SDL_MAJOR_VERSION > 1
716 static SDL_Texture* _STBIMG_SurfToTex(SDL_Renderer* renderer, SDL_Surface* surf)
717 {
718 SDL_Texture* ret = NULL;
719 if(surf != NULL)
720 {
721 ret = SDL_CreateTextureFromSurface(renderer, surf);
722
723 #if SDL_VERSION_ATLEAST(3, 0, 0)
724 SDL_DestroySurface(surf); // not needed anymore, it's copied into tex
725 #else
726 SDL_FreeSurface(surf); // not needed anymore, it's copied into tex
727 #endif
728 }
729 // if surf is NULL, whatever tried to create it should have called SDL_SetError(),
730 // if SDL_CreateTextureFromSurface() returned NULL it should have set an error
731 // so whenever this returns NULL, the user should be able to get a useful
732 // error-message with SDL_GetError().
733 return ret;
734 }
735
736 SDL_STBIMG_DEF SDL_Texture*
737 STBIMG_LoadTexture(SDL_Renderer* renderer, const char* file)
738 {
739 return _STBIMG_SurfToTex(renderer, STBIMG_Load(file));
740 }
741
742 SDL_STBIMG_DEF SDL_Texture*
743 STBIMG_LoadTextureFromMemory(SDL_Renderer *renderer, const unsigned char* buffer, int length)
744 {
745 return _STBIMG_SurfToTex(renderer, STBIMG_LoadFromMemory(buffer, length));
746 }
747
748 #if SDL_VERSION_ATLEAST(3, 0, 0)
749 SDL_STBIMG_DEF SDL_Texture*
750 STBIMG_LoadTexture_IO(SDL_Renderer* renderer, _STBIMG_RWops* src, bool freesrc)
751 {
752 return _STBIMG_SurfToTex(renderer, STBIMG_Load_IO(src, freesrc));
753 }
754 #else
755 SDL_STBIMG_DEF SDL_Texture*
756 STBIMG_LoadTexture_RW(SDL_Renderer* renderer, _STBIMG_RWops* src, bool freesrc)
757 {
758 return _STBIMG_SurfToTex(renderer, STBIMG_Load_RW(src, freesrc));
759 }
760 #endif
761
762 SDL_STBIMG_DEF SDL_Texture*
763 STBIMG_CreateTexture(SDL_Renderer* renderer, const unsigned char* pixelData,
764 int width, int height, int bytesPerPixel)
765 {
766 SDL_Surface* surf = STBIMG_CreateSurface((unsigned char*)pixelData, width, height, bytesPerPixel, false);
767 return _STBIMG_SurfToTex(renderer, surf);
768 }
769
770 #if SDL_VERSION_ATLEAST(3, 0, 0)
771 SDL_STBIMG_DEF SDL_Texture*
772 STBIMG_LoadTexture_IO_noSeek(SDL_Renderer* renderer, _STBIMG_RWops* src, bool freesrc)
773 {
774 return _STBIMG_SurfToTex(renderer, STBIMG_Load_IO_noSeek(src, freesrc));
775 }
776 #else
777 SDL_STBIMG_DEF SDL_Texture*
778 STBIMG_LoadTexture_RW_noSeek(SDL_Renderer* renderer, _STBIMG_RWops* src, bool freesrc)
779 {
780 return _STBIMG_SurfToTex(renderer, STBIMG_Load_RW_noSeek(src, freesrc));
781 }
782 #endif
783
784 #endif // SDL_MAJOR_VERSION > 1
785
786 #endif // SDL_STBIMAGE_IMPLEMENTATION