2023-07-30 16:00:55 +02:00
|
|
|
#include "./image_loader_sdl_bmp.hpp"
|
|
|
|
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
|
2023-08-02 19:24:51 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2023-07-30 16:00:55 +02:00
|
|
|
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;
|
2023-10-04 02:11:06 +02:00
|
|
|
res.file_ext = "bmp";
|
2023-07-30 16:00:55 +02:00
|
|
|
|
|
|
|
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;
|
2023-10-04 02:11:06 +02:00
|
|
|
res.file_ext = "bmp";
|
2023-07-30 16:00:55 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2023-08-02 19:24:51 +02:00
|
|
|
std::cout << "IL_SDLBMP: loaded img " << res.width << "x" << res.height << "\n";
|
|
|
|
|
2023-07-30 16:00:55 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|