tomato-testing/src/image_loader_sdl_bmp.cpp

60 lines
1.3 KiB
C++
Raw Normal View History

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;
2024-03-28 17:13:44 +01:00
auto* ios = SDL_IOFromConstMem(data, data_size);
2023-07-30 16:00:55 +02:00
2024-03-28 17:13:44 +01:00
SDL_Surface* surf = SDL_LoadBMP_IO(ios, SDL_TRUE);
2023-07-30 16:00:55 +02:00
if (surf == nullptr) {
return res;
}
res.width = surf->w;
res.height = surf->h;
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;
2024-03-28 17:13:44 +01:00
auto* ios = SDL_IOFromConstMem(data, data_size);
2023-07-30 16:00:55 +02:00
2024-03-28 17:13:44 +01:00
SDL_Surface* surf = SDL_LoadBMP_IO(ios, SDL_TRUE);
2023-07-30 16:00:55 +02:00
if (surf == nullptr) {
return res;
}
2024-07-18 12:22:15 +02:00
SDL_Surface* conv_surf = SDL_ConvertSurface(surf, SDL_PIXELFORMAT_RGBA32);
2023-07-30 16:00:55 +02:00
SDL_DestroySurface(surf);
if (conv_surf == nullptr) {
return res;
}
res.width = surf->w;
res.height = surf->h;
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;
2024-05-28 22:38:52 +02:00
new_frame.data = {(const uint8_t*)conv_surf->pixels, ((const uint8_t*)conv_surf->pixels) + (surf->w*surf->h*4)};
2023-07-30 16:00:55 +02:00
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;
}