add sdl bmp image loader

This commit is contained in:
Green Sky 2023-07-30 16:00:55 +02:00
parent 42b3866753
commit c0b57c30bd
No known key found for this signature in database
5 changed files with 102 additions and 11 deletions

View File

@ -1,22 +1,26 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR) cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
add_executable(tomato add_executable(tomato
main.cpp ./main.cpp
icon.rc ./icon.rc
screen.hpp ./screen.hpp
start_screen.hpp ./start_screen.hpp
start_screen.cpp ./start_screen.cpp
main_screen.hpp ./main_screen.hpp
main_screen.cpp ./main_screen.cpp
./tox_client.hpp ./tox_client.hpp
./tox_client.cpp ./tox_client.cpp
./auto_dirty.hpp ./auto_dirty.hpp
./auto_dirty.cpp ./auto_dirty.cpp
theme.hpp ./image_loader.hpp
texture_uploader.hpp ./image_loader_sdl_bmp.hpp
./image_loader_sdl_bmp.cpp
./theme.hpp
./texture_uploader.hpp
./sdlrenderer_texture_uploader.hpp ./sdlrenderer_texture_uploader.hpp
./sdlrenderer_texture_uploader.cpp ./sdlrenderer_texture_uploader.cpp
./sdl_clipboard_utils.hpp ./sdl_clipboard_utils.hpp

26
src/image_loader.hpp Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include <cstdint>
#include <vector>
struct ImageLoaderI {
virtual ~ImageLoaderI(void) {}
struct ImageInfo {
uint32_t width {0};
uint32_t height {0};
};
virtual ImageInfo loadInfoFromMemory(const uint8_t* data, uint64_t data_size) = 0;
struct ImageResult {
uint32_t width {0};
uint32_t height {0};
struct Frame {
int32_t ms {0};
std::vector<uint8_t> data;
};
std::vector<Frame> frames;
};
virtual ImageResult loadFromMemoryRGBA(const uint8_t* data, uint64_t data_size) = 0;
};

View File

@ -0,0 +1,53 @@
#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;
}

View File

@ -0,0 +1,9 @@
#pragma once
#include "./image_loader.hpp"
struct ImageLoaderSDLBMP : public ImageLoaderI {
ImageInfo loadInfoFromMemory(const uint8_t* data, uint64_t data_size) override;
ImageResult loadFromMemoryRGBA(const uint8_t* data, uint64_t data_size) override;
};

View File

@ -13,8 +13,7 @@ uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t wi
(void*)data, (void*)data,
width, height, width, height,
4*width, 4*width,
//SDL_PIXELFORMAT_RGBA8888 SDL_PIXELFORMAT_RGBA32 // auto big/little
SDL_PIXELFORMAT_ABGR8888 // little endian
); );
assert(surf); // TODO: add error reporting assert(surf); // TODO: add error reporting