From c0b57c30bde655416f06c07cdf773b349e6c8167 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sun, 30 Jul 2023 16:00:55 +0200 Subject: [PATCH] add sdl bmp image loader --- src/CMakeLists.txt | 22 +++++++----- src/image_loader.hpp | 26 ++++++++++++++ src/image_loader_sdl_bmp.cpp | 53 ++++++++++++++++++++++++++++ src/image_loader_sdl_bmp.hpp | 9 +++++ src/sdlrenderer_texture_uploader.cpp | 3 +- 5 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 src/image_loader.hpp create mode 100644 src/image_loader_sdl_bmp.cpp create mode 100644 src/image_loader_sdl_bmp.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a4a3863..13d4cda 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,22 +1,26 @@ cmake_minimum_required(VERSION 3.9 FATAL_ERROR) add_executable(tomato - main.cpp - icon.rc + ./main.cpp + ./icon.rc - screen.hpp - start_screen.hpp - start_screen.cpp - main_screen.hpp - main_screen.cpp + ./screen.hpp + ./start_screen.hpp + ./start_screen.cpp + ./main_screen.hpp + ./main_screen.cpp ./tox_client.hpp ./tox_client.cpp ./auto_dirty.hpp ./auto_dirty.cpp - theme.hpp - texture_uploader.hpp + ./image_loader.hpp + ./image_loader_sdl_bmp.hpp + ./image_loader_sdl_bmp.cpp + + ./theme.hpp + ./texture_uploader.hpp ./sdlrenderer_texture_uploader.hpp ./sdlrenderer_texture_uploader.cpp ./sdl_clipboard_utils.hpp diff --git a/src/image_loader.hpp b/src/image_loader.hpp new file mode 100644 index 0000000..830a8db --- /dev/null +++ b/src/image_loader.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +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 data; + }; + std::vector frames; + }; + virtual ImageResult loadFromMemoryRGBA(const uint8_t* data, uint64_t data_size) = 0; +}; + diff --git a/src/image_loader_sdl_bmp.cpp b/src/image_loader_sdl_bmp.cpp new file mode 100644 index 0000000..34661cd --- /dev/null +++ b/src/image_loader_sdl_bmp.cpp @@ -0,0 +1,53 @@ +#include "./image_loader_sdl_bmp.hpp" + +#include + +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; +} + diff --git a/src/image_loader_sdl_bmp.hpp b/src/image_loader_sdl_bmp.hpp new file mode 100644 index 0000000..99cb730 --- /dev/null +++ b/src/image_loader_sdl_bmp.hpp @@ -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; +}; + diff --git a/src/sdlrenderer_texture_uploader.cpp b/src/sdlrenderer_texture_uploader.cpp index 6f9684f..d114a9c 100644 --- a/src/sdlrenderer_texture_uploader.cpp +++ b/src/sdlrenderer_texture_uploader.cpp @@ -13,8 +13,7 @@ uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t wi (void*)data, width, height, 4*width, - //SDL_PIXELFORMAT_RGBA8888 - SDL_PIXELFORMAT_ABGR8888 // little endian + SDL_PIXELFORMAT_RGBA32 // auto big/little ); assert(surf); // TODO: add error reporting