forked from Green-Sky/tomato
54 lines
1.2 KiB
C++
54 lines
1.2 KiB
C++
|
#include "./image_loader_sdl_bmp.hpp"
|
||
|
|
||
|
#include <SDL3/SDL.h>
|
||
|
|
||
|
ImageLoaderSDLBMP::ImageInfo ImageLoaderSDLBMP::loadInfoFromMemory(const uint8_t* data, uint64_t data_size) {
|
||
|
ImageInfo res;
|
||
|
|
||
|
auto* rw_ctx = SDL_RWFromConstMem(data, data_size);
|
||
|
|
||
|
SDL_Surface* surf = SDL_LoadBMP_RW(rw_ctx, SDL_TRUE);
|
||
|
if (surf == nullptr) {
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
res.width = surf->w;
|
||
|
res.height = surf->h;
|
||
|
|
||
|
SDL_DestroySurface(surf);
|
||
|
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
ImageLoaderSDLBMP::ImageResult ImageLoaderSDLBMP::loadFromMemoryRGBA(const uint8_t* data, uint64_t data_size) {
|
||
|
ImageResult res;
|
||
|
|
||
|
auto* rw_ctx = SDL_RWFromConstMem(data, data_size);
|
||
|
|
||
|
SDL_Surface* surf = SDL_LoadBMP_RW(rw_ctx, SDL_TRUE);
|
||
|
if (surf == nullptr) {
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
SDL_Surface* conv_surf = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_RGBA32);
|
||
|
SDL_DestroySurface(surf);
|
||
|
if (conv_surf == nullptr) {
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
res.width = surf->w;
|
||
|
res.height = surf->h;
|
||
|
|
||
|
SDL_LockSurface(conv_surf);
|
||
|
|
||
|
auto& new_frame = res.frames.emplace_back();
|
||
|
new_frame.ms = 0;
|
||
|
new_frame.data.insert(new_frame.data.cbegin(), (const uint8_t*)conv_surf->pixels, ((const uint8_t*)conv_surf->pixels) + (surf->w*surf->h*4));
|
||
|
|
||
|
SDL_UnlockSurface(conv_surf);
|
||
|
SDL_DestroySurface(conv_surf);
|
||
|
|
||
|
return res;
|
||
|
}
|
||
|
|