From b0b61c117c6e2bc7693802f005a2888e7cc073c2 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Wed, 28 Dec 2022 11:23:00 -0300 Subject: refa Use QOI image format for textures --- Rakefile | 1 - src/core.cpp | 19 ----------- src/core.hpp | 1 - src/pgm_image.cpp | 70 ---------------------------------------- src/pgm_image.hpp | 34 ------------------- src/texture.cpp | 3 -- src/vk/texture.cpp | 44 ++++++++++--------------- test/src/main.rb | 2 +- test/textures/color_texture.png | Bin 3594 -> 0 bytes test/textures/color_texture.qoi | Bin 0 -> 16062 bytes 10 files changed, 18 insertions(+), 156 deletions(-) delete mode 100644 src/pgm_image.cpp delete mode 100644 src/pgm_image.hpp delete mode 100644 test/textures/color_texture.png create mode 100644 test/textures/color_texture.qoi diff --git a/Rakefile b/Rakefile index c5883e5..1e3bc5b 100644 --- a/Rakefile +++ b/Rakefile @@ -51,7 +51,6 @@ SPV_FILES = GLSL_FILES.ext('.spv') LIBRARIES = [ 'SDL2', - 'SDL2_image', 'SDL2_mixer', 'm', 'yaml-cpp' diff --git a/src/core.cpp b/src/core.cpp index bd5ea75..c4e03a4 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -174,24 +174,6 @@ unload_sdl(void *obj) SDL_Quit(); } -void -load_sdl_image(void *obj) -{ - int flags = IMG_INIT_JPG|IMG_INIT_PNG|IMG_INIT_TIF; - if(!(IMG_Init(flags) & flags)) - { - std::string error{"Could not initialize SDL image → "}; - error += IMG_GetError(); - throw CommandError{error}; - } -} - -void -unload_sdl_image(void *obj) -{ - IMG_Quit(); -} - void load_sdl_mixer(void *obj) { @@ -696,7 +678,6 @@ const CommandChain cg_sCore::loader{ {&load_mruby_symbols, nullptr}, {&load_game, nullptr}, {&load_sdl, &unload_sdl}, - {&load_sdl_image, &unload_sdl_image}, {&load_sdl_mixer, &unload_sdl_mixer}, {&load_sdl_open_audio, &unload_sdl_open_audio}, {&load_window, &unload_window}, diff --git a/src/core.hpp b/src/core.hpp index 5d92cfa..660af27 100644 --- a/src/core.hpp +++ b/src/core.hpp @@ -37,7 +37,6 @@ #include #include -#include #include #include "command.hpp" diff --git a/src/pgm_image.cpp b/src/pgm_image.cpp deleted file mode 100644 index 04ff9b9..0000000 --- a/src/pgm_image.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2022 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "pgm_image.hpp" - -#include -#include -#include -#include - -bool PGMImage_constructor(PGMImage *self, const char *file_path) -{ - self->data = nullptr; - int wh_pos; - std::string line; - std::ifstream file(file_path); - - if(!file) - { - self->error = "Failed to open PGM file"; - return false; - } - - // Read file magic. - std::getline(file, line); - if(line != "P5") - { - self->error = "PGM file contains a invalid magic"; - return false; - } - - // Read file comment. - std::getline(file, line); - - // Read file width and height. - std::getline(file, line); - wh_pos = line.find(" "); - std::from_chars(line.data(), line.data() + wh_pos, self->width); - std::from_chars( - line.data() + wh_pos + 1, line.data() + line.size(), self->height); - - // Read file maximum value. - std::getline(file, line); - std::from_chars(line.data(), line.data() + line.size(), self->max_value); - - // Read file values. - self->data_size = self->width * self->height; - self->data = new char[self->data_size]; - file.read(self->data, self->data_size); - - return true; -} - -void PGMImage_destructor(PGMImage *self) -{ - if(self->data != nullptr) delete[] self->data; -} diff --git a/src/pgm_image.hpp b/src/pgm_image.hpp deleted file mode 100644 index bda73ca..0000000 --- a/src/pgm_image.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2022 Frederico de Oliveira Linhares - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef CANDY_GEAR_PGM_IMAGE_H -#define CANDY_GEAR_PGM_IMAGE_H 1 - -struct PGMImage_s -{ - int width, height, max_value, data_size; - char *data; - const char *error; -}; - -typedef struct PGMImage_s PGMImage; - -bool -PGMImage_constructor(PGMImage *self, const char *file_path); -void -PGMImage_destructor(PGMImage *self); - -#endif /* CANDY_GEAR_PGM_IMAGE_H */ diff --git a/src/texture.cpp b/src/texture.cpp index ac2a8e4..ce1d479 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -17,7 +17,6 @@ #include "texture.hpp" #include "core.hpp" -#include "pgm_image.hpp" #include "vk/texture.hpp" void @@ -46,8 +45,6 @@ texture_alloc(mrb_state *mrb, mrb_value klass) static mrb_value cg_cTexture_from_image(mrb_state *mrb, mrb_value self) { - PGMImage pgm_img; - struct mrb_value texture = texture_alloc(mrb, self); const char *file_path; std::shared_ptr *ptr; diff --git a/src/vk/texture.cpp b/src/vk/texture.cpp index 54cf801..020dd21 100644 --- a/src/vk/texture.cpp +++ b/src/vk/texture.cpp @@ -16,6 +16,9 @@ #include "texture.hpp" +#define QOI_IMPLEMENTATION 0 +#include + #include "../command.hpp" #include "../core.hpp" #include "image.hpp" @@ -76,42 +79,29 @@ load_image(void *obj) { auto self = static_cast(obj); - SDL_Surface *image{nullptr}; - - // Load file image from file. - { - SDL_Surface *raw_surface{nullptr}; - raw_surface = IMG_Load(self->texture_path.c_str()); - if(raw_surface == nullptr) - { - std::string error{"Failed to load image. SDL2_image Error: "}; - error += IMG_GetError(); - throw CommandError{error}; - } + qoi_desc img_desc; + const int num_channels = 4; // all images are converted to RGBA + unsigned char *pixels; - image = SDL_ConvertSurfaceFormat(raw_surface, SDL_PIXELFORMAT_ARGB8888, 0); - if(image == nullptr) - { - std::string error{"Failed to convert image. SDL2 Error: "}; - error += SDL_GetError(); - throw CommandError{error}; - } + { // Load file image from file. + void *pixels_ptr = qoi_read(self->texture_path.c_str(), &img_desc, 4); + if(pixels_ptr == NULL) throw CommandError{"Failed to open image."}; - self->texture->width = static_cast(image->w); - self->texture->height = static_cast(image->h); + pixels = static_cast(pixels_ptr); + self->texture->width = static_cast(img_desc.width); + self->texture->height = static_cast(img_desc.height); self->texture->mip_levels = 1; - SDL_FreeSurface(raw_surface); + pixels = static_cast(pixels_ptr); } // Load file image into a vulkan buffer. size_t image_size{static_cast( - image->format->BytesPerPixel * image->w * image->h)}; + img_desc.width * img_desc.height * num_channels)}; VK::SourceBuffer source_image_buffer{ - cg_core.vk_device_with_swapchain, image->pixels, image_size}; + cg_core.vk_device_with_swapchain, pixels, image_size}; - // Create vulkan image. - { + { // Create vulkan image. try { VkExtent3D vk_extent3d{}; @@ -176,7 +166,7 @@ load_image(void *obj) } // Free resources. - SDL_FreeSurface(image); + QOI_FREE(pixels); } void diff --git a/test/src/main.rb b/test/src/main.rb index 53e92a0..33d0b1d 100644 --- a/test/src/main.rb +++ b/test/src/main.rb @@ -23,7 +23,7 @@ def config() end def init() - texture = CandyGear::Texture.from_image("textures/color_texture.png"); + texture = CandyGear::Texture.from_image("textures/color_texture.qoi"); mesh = CandyGear::Mesh.new("meshes/cube.cgmesh"); $color = CandyGear::Vector3D.new(0.8, 0.2, 0.2); diff --git a/test/textures/color_texture.png b/test/textures/color_texture.png deleted file mode 100644 index 2c02811..0000000 Binary files a/test/textures/color_texture.png and /dev/null differ diff --git a/test/textures/color_texture.qoi b/test/textures/color_texture.qoi new file mode 100644 index 0000000..9fa7256 Binary files /dev/null and b/test/textures/color_texture.qoi differ -- cgit v1.2.3