| 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 |
#if 0 |
| 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 |
|
| 74 |
|
| 75 |
SDL_FreeSurface(surf); |
| 76 |
} |
| 77 |
#endif |
| 78 |
|
| 79 |
|
| 80 |
#ifndef _SDL_STBIMAGE_H |
| 81 |
#define _SDL_STBIMAGE_H |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
#ifndef SDL_VERSION_ATLEAST |
| 87 |
#ifdef SDL_STBIMG_SDL3 |
| 88 |
|
| 89 |
#include <SDL3/SDL.h> |
| 90 |
#else |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
#include <SDL.h> |
| 95 |
#endif |
| 96 |
|
| 97 |
|
| 98 |
#endif |
| 99 |
|
| 100 |
#if !SDL_VERSION_ATLEAST(3, 0, 0) && !defined(__cpluscplus) |
| 101 |
|
| 102 |
|
| 103 |
#include <stdbool.h> |
| 104 |
#endif |
| 105 |
|
| 106 |
#ifndef SDL_STBIMG_ALLOW_STDIO |
| 107 |
#define STBI_NO_STDIO |
| 108 |
#endif |
| 109 |
#include "stb_image.h" |
| 110 |
|
| 111 |
|
| 112 |
#ifndef SDL_STBIMG_DEF |
| 113 |
|
| 114 |
#define SDL_STBIMG_DEF |
| 115 |
#endif |
| 116 |
|
| 117 |
|
| 118 |
#ifdef __cplusplus |
| 119 |
extern "C" { |
| 120 |
#endif |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
SDL_STBIMG_DEF SDL_Surface* STBIMG_Load(const char* file); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
SDL_STBIMG_DEF SDL_Surface* STBIMG_LoadFromMemory(const unsigned char* buffer, int length); |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
#if SDL_VERSION_ATLEAST(3, 0, 0) |
| 135 |
SDL_STBIMG_DEF SDL_Surface* STBIMG_Load_IO(SDL_IOStream* src, bool freesrc); |
| 136 |
#else |
| 137 |
SDL_STBIMG_DEF SDL_Surface* STBIMG_Load_RW(SDL_RWops* src, bool freesrc); |
| 138 |
#endif |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 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 |
|
| 154 |
|
| 155 |
SDL_STBIMG_DEF SDL_Texture* |
| 156 |
STBIMG_LoadTexture(SDL_Renderer* renderer, const char* file); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
SDL_STBIMG_DEF SDL_Texture* |
| 161 |
STBIMG_LoadTextureFromMemory(SDL_Renderer* renderer, const unsigned char* buffer, int length); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 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 |
| 170 |
SDL_STBIMG_DEF SDL_Texture* |
| 171 |
STBIMG_LoadTexture_RW(SDL_Renderer* renderer, SDL_RWops* src, bool freesrc); |
| 172 |
#endif |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 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 |
| 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; |
| 193 |
} STBIMG_stbio_RWops; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 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 |
| 209 |
SDL_RWops* src = ...; |
| 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 |
|
| 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; |
| 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 |
|
| 238 |
stbi_image_free(data); |
| 239 |
#endif |
| 240 |
|
| 241 |
|
| 242 |
#if SDL_MAJOR_VERSION > 1 |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 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 |
|
| 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 |
| 265 |
|
| 266 |
#ifdef __cplusplus |
| 267 |
} |
| 268 |
#endif |
| 269 |
|
| 270 |
|
| 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 |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
#ifdef SDL_STBIMAGE_IMPLEMENTATION |
| 286 |
|
| 287 |
|
| 288 |
|
| 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 |
| 295 |
#endif |
| 296 |
#include "stb_image.h" |
| 297 |
|
| 298 |
|
| 299 |
|
| 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 |
| 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; |
| 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 |
|
| 349 |
|
| 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 |
| 357 |
|
| 358 |
Uint32 rmask, gmask, bmask, amask; |
| 359 |
|
| 360 |
|
| 361 |
|
| 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 |
| 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 |
| 379 |
|
| 380 |
if(surf == NULL) |
| 381 |
{ |
| 382 |
|
| 383 |
return NULL; |
| 384 |
} |
| 385 |
|
| 386 |
if(freeWithSurface) |
| 387 |
{ |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
#if SDL_VERSION_ATLEAST(3, 0, 0) |
| 394 |
|
| 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 |
|
| 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 |
|
| 446 |
SDL_free(img.data); |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
|
| 450 |
return ret; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 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 |
|
| 463 |
io->atEOF = 1; |
| 464 |
} |
| 465 |
return (int)ret*sizeof(char); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 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 |
|
| 476 |
io->atEOF = 2; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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; |
| 570 |
|
| 571 |
|
| 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 |
|
| 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 |
|
| 600 |
|
| 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; |
| 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 |
|
| 638 |
|
| 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 |
|
| 654 |
goto end; |
| 655 |
} |
| 656 |
bytesRead += read; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 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 |
| 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); |
| 725 |
#else |
| 726 |
SDL_FreeSurface(surf); |
| 727 |
#endif |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 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 |
| 785 |
|
| 786 |
#endif |