retocat gif.cpp

This commit is contained in:
2025-03-16 12:44:38 +01:00
parent 4a07100e2a
commit b3215bf381
6 changed files with 429 additions and 359 deletions

View File

@@ -1,14 +1,15 @@
#include "texture.h"
#include <SDL2/SDL_error.h> // Para SDL_GetError
#include <SDL2/SDL_surface.h> // Para SDL_CreateRGBSurfaceWithFormatFrom
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream
#include <iostream> // Para cerr, cout
#include <stdexcept> // Para runtime_error
#include <string> // Para char_traits, operator<<, operator+
#include <vector> // Para vector
#include "gif.h" // Para LoadGif, LoadPalette
#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a...
#include "utils.h" // Para getFileName, printWithDots
#include <SDL2/SDL_error.h> // for SDL_GetError
#include <SDL2/SDL_surface.h> // for SDL_CreateRGBSurfaceWithFormatFrom
#include <stdint.h> // for uint32_t
#include <cstring> // for memcpy, size_t
#include <fstream> // for basic_ostream, operator<<, basic_ifstream
#include <iostream> // for cerr, cout
#include <stdexcept> // for runtime_error
#include <string> // for char_traits, operator<<, operator+
#include <vector> // for vector
#include "gif.h" // for Gif
#include "utils.h" // for getFileName, Color, printWithDots
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" // Para stbi_failure_reason, stbi_image_free
@@ -38,7 +39,6 @@ Texture::Texture(SDL_Renderer *renderer, const std::string &path)
// Añade la propia paleta del fichero a la lista
addPaletteFromFile(path_);
// setPaletteColor(0, 0, 0x00000000);
// Crea la textura, establece el BlendMode y copia la surface a la textura
createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
@@ -77,18 +77,11 @@ bool Texture::loadFromFile(const std::string &file_path)
int depth, pitch;
Uint32 pixel_format;
/*if (req_format == STBI_rgb)
{
depth = 24;
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
pixel_format = SDL_PIXELFORMAT_RGB24;
}
else*/
{ // STBI_rgb_alpha (RGBA)
depth = 32;
pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32;
}
// STBI_rgb_alpha (RGBA)
depth = 32;
pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32;
// Limpia
unloadTexture();
@@ -249,7 +242,7 @@ void Texture::unloadSurface()
// Crea una surface desde un fichero .gif
std::shared_ptr<Surface> Texture::loadSurface(const std::string &file_path)
{
// Desencadenar la superficie actual
// Libera la superficie actual
unloadSurface();
// Abrir el archivo usando std::ifstream para manejo automático del recurso
@@ -271,24 +264,26 @@ std::shared_ptr<Surface> Texture::loadSurface(const std::string &file_path)
std::cerr << "Error al leer el fichero " << file_path << std::endl;
throw std::runtime_error("Error al leer el fichero: " + file_path);
}
// Cerrar el archivo (automáticamente manejado por std::ifstream)
file.close();
// Crear un objeto Gif y llamar a la función LoadGif
Gif gif;
Uint16 w, h;
Uint8 *rawPixels = gif.LoadGif(buffer.data(), &w, &h);
if (!rawPixels)
// Crear un objeto Gif y llamar a la función loadGif
GIF::Gif gif;
Uint16 w = 0, h = 0;
std::vector<Uint8> rawPixels = gif.loadGif(buffer.data(), w, h);
if (rawPixels.empty())
{
return nullptr;
}
// Crear un std::shared_ptr con std::make_shared para pixels
auto pixels = std::shared_ptr<Uint8[]>(rawPixels, std::default_delete<Uint8[]>());
// Si el constructor de Surface espera un std::shared_ptr<Uint8[]>,
// reservamos un bloque dinámico y copiamos los datos del vector.
size_t pixelCount = rawPixels.size();
auto pixels = std::shared_ptr<Uint8[]>(new Uint8[pixelCount], std::default_delete<Uint8[]>());
std::memcpy(pixels.get(), rawPixels.data(), pixelCount);
auto surface = std::make_shared<Surface>(w, h, pixels);
// Actualizar la anchura y altura
// Actualizar las dimensiones
width_ = w;
height_ = h;
@@ -327,6 +322,7 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
{
std::vector<Uint32> palette;
// Abrir el archivo GIF
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
if (!file)
{
@@ -338,7 +334,8 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]");
}
auto size = file.tellg();
// Obtener el tamaño del archivo y leerlo en un buffer
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<Uint8> buffer(size);
@@ -347,17 +344,20 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
std::cerr << "Error: No se pudo leer completamente el fichero " << getFileName(file_path) << std::endl;
throw std::runtime_error("Error al leer el fichero: " + getFileName(file_path));
}
file.close();
Gif gif;
const auto *PAL = gif.LoadPalette(buffer.data());
if (!PAL)
// Usar la nueva función loadPalette, que devuelve un vector<uint32_t>
GIF::Gif gif;
std::vector<uint32_t> pal = gif.loadPalette(buffer.data());
if (pal.empty())
{
return palette;
return palette; // Devuelve un vector vacío si no hay paleta
}
for (int i = 0; i < 256; ++i)
// Modificar la conversión para obtener formato RGBA (0xRRGGBBAA)
for (const auto &color : pal)
{
palette.push_back((PAL[i] << 8) + 255);
palette.push_back((color << 8) | 0xFF); // Resultado: 0xRRGGBBAA
}
return palette;