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

@@ -29,6 +29,7 @@ done
case $opcion in case $opcion in
w) w)
cppcheck --force --enable=warning,style,performance --std=c++20 \ cppcheck --force --enable=warning,style,performance --std=c++20 \
--check-level=exhaustive \
--suppressions-list=./cppcheck_suppressions \ --suppressions-list=./cppcheck_suppressions \
../source/ \ ../source/ \
2>./cppcheck-result-warning-style-performance.txt 2>./cppcheck-result-warning-style-performance.txt

View File

@@ -55,7 +55,7 @@ Director::Director(int argc, const char *argv[])
section::name = section::Name::GAME; section::name = section::Name::GAME;
section::options = section::Options::GAME_PLAY_1P; section::options = section::Options::GAME_PLAY_1P;
#elif DEBUG #elif DEBUG
section::name = section::Name::CREDITS; section::name = section::Name::LOGO;
#else // NORMAL GAME #else // NORMAL GAME
section::name = section::Name::LOGO; section::name = section::Name::LOGO;
section::options = section::Options::NONE; section::options = section::Options::NONE;

View File

@@ -1,61 +1,78 @@
#include "gif.h" #include "gif.h"
#include <cstdio> // Para fprintf, stderr #include <cstdio> // for fprintf, stderr
#include <cstdlib> // Para exit, malloc, calloc, free, realloc #include <cstring> // for memcpy, size_t
#include <vector> // Para vector #include <stdexcept> // for runtime_error
#include <string> // for allocator, char_traits, operator==, basic_string
void Gif::uncompress(int code_length, const uint8_t *input, int input_length, uint8_t *out) namespace GIF
{ {
// Función inline para reemplazar el macro READ.
// Actualiza el puntero 'buffer' tras copiar 'size' bytes a 'dst'.
inline void readBytes(const uint8_t *&buffer, void *dst, size_t size)
{
std::memcpy(dst, buffer, size);
buffer += size;
}
void Gif::decompress(int code_length, const uint8_t *input, int input_length, uint8_t *out)
{
// Verifica que el code_length tenga un rango razonable.
if (code_length < 2 || code_length > 12)
{
throw std::runtime_error("Invalid LZW code length");
}
int i, bit; int i, bit;
int code, prev = -1; int prev = -1;
std::vector<DictionaryEntry> dictionary; std::vector<DictionaryEntry> dictionary;
int dictionary_ind; int dictionary_ind;
unsigned int mask = 0x01; unsigned int mask = 0x01;
int reset_code_length; int reset_code_length = code_length;
int clear_code; int clear_code = 1 << code_length;
int stop_code; int stop_code = clear_code + 1;
int match_len; int match_len = 0;
clear_code = 1 << (code_length);
stop_code = clear_code + 1;
reset_code_length = code_length;
// Inicializamos el diccionario con el tamaño correspondiente.
dictionary.resize(1 << (code_length + 1)); dictionary.resize(1 << (code_length + 1));
for (dictionary_ind = 0; dictionary_ind < (1 << code_length); dictionary_ind++) for (dictionary_ind = 0; dictionary_ind < (1 << code_length); dictionary_ind++)
{ {
dictionary[dictionary_ind].byte = dictionary_ind; dictionary[dictionary_ind].byte = static_cast<uint8_t>(dictionary_ind);
dictionary[dictionary_ind].prev = -1; dictionary[dictionary_ind].prev = -1;
dictionary[dictionary_ind].len = 1; dictionary[dictionary_ind].len = 1;
} }
dictionary_ind += 2; // Reservamos espacio para clear y stop codes
dictionary_ind += 2; // Bucle principal: procesar el stream comprimido.
while (input_length > 0)
while (input_length)
{ {
code = 0x0; int code = 0;
// Lee (code_length + 1) bits para formar el código.
for (i = 0; i < (code_length + 1); i++) for (i = 0; i < (code_length + 1); i++)
{ {
bit = (*input & mask) ? 1 : 0; if (input_length <= 0)
{
throw std::runtime_error("Unexpected end of input in uncompress");
}
bit = ((*input & mask) != 0) ? 1 : 0;
mask <<= 1; mask <<= 1;
if (mask == 0x100) if (mask == 0x100)
{ {
mask = 0x01; mask = 0x01;
input++; input++;
input_length--; input_length--;
} }
code |= (bit << i);
code = code | (bit << i);
} }
if (code == clear_code) if (code == clear_code)
{ {
// Reinicia el diccionario.
code_length = reset_code_length; code_length = reset_code_length;
dictionary.resize(1 << (code_length + 1)); dictionary.resize(1 << (code_length + 1));
for (dictionary_ind = 0; dictionary_ind < (1 << code_length); dictionary_ind++) for (dictionary_ind = 0; dictionary_ind < (1 << code_length); dictionary_ind++)
{ {
dictionary[dictionary_ind].byte = dictionary_ind; dictionary[dictionary_ind].byte = static_cast<uint8_t>(dictionary_ind);
dictionary[dictionary_ind].prev = -1; dictionary[dictionary_ind].prev = -1;
dictionary[dictionary_ind].len = 1; dictionary[dictionary_ind].len = 1;
} }
@@ -68,33 +85,29 @@ void Gif::uncompress(int code_length, const uint8_t *input, int input_length, ui
break; break;
} }
if ((prev > -1) && (code_length < 12)) if (prev > -1 && code_length < 12)
{ {
if (code > dictionary_ind) if (code > dictionary_ind)
{ {
fprintf(stderr, "code = %.02x, but dictionary_ind = %.02x\n", code, dictionary_ind); std::fprintf(stderr, "code = %.02x, but dictionary_ind = %d\n", code, dictionary_ind);
exit(0); throw std::runtime_error("LZW error: code exceeds dictionary_ind.");
} }
int ptr;
if (code == dictionary_ind) if (code == dictionary_ind)
{ {
int ptr = prev; ptr = prev;
while (dictionary[ptr].prev != -1) while (dictionary[ptr].prev != -1)
{
ptr = dictionary[ptr].prev; ptr = dictionary[ptr].prev;
}
dictionary[dictionary_ind].byte = dictionary[ptr].byte; dictionary[dictionary_ind].byte = dictionary[ptr].byte;
} }
else else
{ {
int ptr = code; ptr = code;
while (dictionary[ptr].prev != -1) while (dictionary[ptr].prev != -1)
{
ptr = dictionary[ptr].prev; ptr = dictionary[ptr].prev;
}
dictionary[dictionary_ind].byte = dictionary[ptr].byte; dictionary[dictionary_ind].byte = dictionary[ptr].byte;
} }
dictionary[dictionary_ind].prev = prev; dictionary[dictionary_ind].prev = prev;
dictionary[dictionary_ind].len = dictionary[prev].len + 1; dictionary[dictionary_ind].len = dictionary[prev].len + 1;
dictionary_ind++; dictionary_ind++;
@@ -108,155 +121,197 @@ void Gif::uncompress(int code_length, const uint8_t *input, int input_length, ui
prev = code; prev = code;
match_len = dictionary[code].len; // Verifica que 'code' sea un índice válido antes de usarlo.
while (code != -1) if (code < 0 || static_cast<size_t>(code) >= dictionary.size())
{ {
out[dictionary[code].len - 1] = dictionary[code].byte; std::fprintf(stderr, "Invalid LZW code %d, dictionary size %zu\n", code, dictionary.size());
if (dictionary[code].prev == code) throw std::runtime_error("LZW error: invalid code encountered");
{
fprintf(stderr, "Internal error; self-reference.");
exit(0);
}
code = dictionary[code].prev;
} }
int curCode = code; // Variable temporal para recorrer la cadena.
match_len = dictionary[curCode].len;
while (curCode != -1)
{
// Se asume que dictionary[curCode].len > 0.
out[dictionary[curCode].len - 1] = dictionary[curCode].byte;
if (dictionary[curCode].prev == curCode)
{
std::fprintf(stderr, "Internal error; self-reference detected.\n");
throw std::runtime_error("Internal error in uncompress: self-reference");
}
curCode = dictionary[curCode].prev;
}
out += match_len; out += match_len;
} }
} }
int Gif::read_sub_blocks(uint8_t *buffer, uint8_t **data) std::vector<uint8_t> Gif::readSubBlocks(const uint8_t *&buffer)
{ {
int data_length = 0; std::vector<uint8_t> data;
int index = 0; uint8_t block_size = *buffer;
uint8_t block_size; buffer++;
while (block_size != 0)
*data = nullptr;
while (true)
{ {
READ(&block_size, 1); data.insert(data.end(), buffer, buffer + block_size);
buffer += block_size;
if (block_size == 0) block_size = *buffer;
{ buffer++;
break; }
return data;
} }
data_length += block_size; std::vector<uint8_t> Gif::processImageDescriptor(const uint8_t *&buffer, const std::vector<RGB> &gct, int resolution_bits)
*data = (uint8_t *)realloc(*data, data_length);
READ(*data + index, block_size);
index += block_size;
}
return data_length;
}
uint8_t *Gif::process_image_descriptor(uint8_t *buffer, RGB *gct, int gct_size, int resolution_bits)
{ {
ImageDescriptor image_descriptor; ImageDescriptor image_descriptor;
int compressed_data_length; // Lee 9 bytes para el image descriptor.
uint8_t *compressed_data = nullptr; readBytes(buffer, &image_descriptor, sizeof(ImageDescriptor));
uint8_t lzw_code_size; uint8_t lzw_code_size;
int uncompressed_data_length = 0; readBytes(buffer, &lzw_code_size, sizeof(uint8_t));
uint8_t *uncompressed_data = nullptr;
READ(&image_descriptor, 9); std::vector<uint8_t> compressed_data = readSubBlocks(buffer);
READ(&lzw_code_size, 1); int uncompressed_data_length = image_descriptor.image_width * image_descriptor.image_height;
std::vector<uint8_t> uncompressed_data(uncompressed_data_length);
compressed_data_length = read_sub_blocks(buffer, &compressed_data);
uncompressed_data_length = image_descriptor.image_width * image_descriptor.image_height;
uncompressed_data = (uint8_t *)malloc(uncompressed_data_length);
uncompress(lzw_code_size, compressed_data, compressed_data_length, uncompressed_data);
if (compressed_data)
{
free(compressed_data);
}
decompress(lzw_code_size, compressed_data.data(), static_cast<int>(compressed_data.size()), uncompressed_data.data());
return uncompressed_data; return uncompressed_data;
} }
uint32_t *Gif::LoadPalette(uint8_t *buffer) std::vector<uint32_t> Gif::loadPalette(const uint8_t *buffer)
{ {
uint8_t header[7]; uint8_t header[6];
std::memcpy(header, buffer, 6);
buffer += 6;
ScreenDescriptor screen_descriptor; ScreenDescriptor screen_descriptor;
int global_color_table_size = 0; std::memcpy(&screen_descriptor, buffer, sizeof(ScreenDescriptor));
uint32_t *global_color_table = nullptr; buffer += sizeof(ScreenDescriptor);
READ(header, 6);
READ(&screen_descriptor, 7);
global_color_table = (uint32_t *)calloc(1, 1024);
std::vector<uint32_t> global_color_table;
if (screen_descriptor.fields & 0x80) if (screen_descriptor.fields & 0x80)
{ {
global_color_table_size = 1 << (((screen_descriptor.fields & 0x07) + 1)); int global_color_table_size = 1 << (((screen_descriptor.fields & 0x07) + 1));
global_color_table.resize(global_color_table_size);
for (int i = 0; i < global_color_table_size; ++i) for (int i = 0; i < global_color_table_size; ++i)
{ {
global_color_table[i] = (buffer[0] << 16) + (buffer[1] << 8) + buffer[2]; uint8_t r = buffer[0];
uint8_t g = buffer[1];
uint8_t b = buffer[2];
global_color_table[i] = (r << 16) | (g << 8) | b;
buffer += 3; buffer += 3;
} }
} }
return global_color_table; return global_color_table;
} }
uint8_t *Gif::process_gif_stream(uint8_t *buffer, uint16_t *w, uint16_t *h) std::vector<uint8_t> Gif::processGifStream(const uint8_t *buffer, uint16_t &w, uint16_t &h)
{ {
uint8_t header[7]; // Leer la cabecera de 6 bytes ("GIF87a" o "GIF89a")
uint8_t header[6];
std::memcpy(header, buffer, 6);
buffer += 6;
// Opcional: Validar header
std::string headerStr(reinterpret_cast<char *>(header), 6);
if (headerStr != "GIF87a" && headerStr != "GIF89a")
{
throw std::runtime_error("Formato de archivo GIF inválido.");
}
// Leer el Screen Descriptor (7 bytes, empaquetado sin padding)
ScreenDescriptor screen_descriptor; ScreenDescriptor screen_descriptor;
int color_resolution_bits; readBytes(buffer, &screen_descriptor, sizeof(ScreenDescriptor));
int global_color_table_size = 0;
RGB *global_color_table = nullptr;
uint8_t block_type = 0x0;
READ(header, 6); // Asigna ancho y alto
header[6] = 0x0; w = screen_descriptor.width;
h = screen_descriptor.height;
READ(&screen_descriptor, 7); // Imprime para depuración
*w = screen_descriptor.width; std::fprintf(stderr, "Screen Descriptor - Width: %d, Height: %d\n", w, h);
*h = screen_descriptor.height;
color_resolution_bits = ((screen_descriptor.fields & 0x70) >> 4) + 1;
int color_resolution_bits = ((screen_descriptor.fields & 0x70) >> 4) + 1;
std::vector<RGB> global_color_table;
if (screen_descriptor.fields & 0x80) if (screen_descriptor.fields & 0x80)
{ {
global_color_table_size = 1 << (((screen_descriptor.fields & 0x07) + 1)); int global_color_table_size = 1 << (((screen_descriptor.fields & 0x07) + 1));
global_color_table = (RGB *)malloc(3 * global_color_table_size); global_color_table.resize(global_color_table_size);
READ(global_color_table, 3 * global_color_table_size); std::memcpy(global_color_table.data(), buffer, 3 * global_color_table_size);
buffer += 3 * global_color_table_size;
} }
// Supongamos que 'buffer' es el puntero actual y TRAILER es 0x3B
uint8_t block_type = *buffer++;
while (block_type != TRAILER) while (block_type != TRAILER)
{ {
READ(&block_type, 1); if (block_type == EXTENSION_INTRODUCER) // 0x21
uint8_t size;
switch (block_type)
{ {
case IMAGE_DESCRIPTOR: // Se lee la etiqueta de extensión, la cual indica el tipo de extensión.
return process_image_descriptor(buffer, global_color_table, global_color_table_size, color_resolution_bits); uint8_t extension_label = *buffer++;
case EXTENSION_INTRODUCER: switch (extension_label)
buffer++;
size = *(buffer++);
buffer += size;
do
{ {
size = *(buffer++); case GRAPHIC_CONTROL: // 0xF9
buffer += size; {
} while (size != 0); // Procesar Graphic Control Extension:
uint8_t blockSize = *buffer++; // Normalmente, blockSize == 4
buffer += blockSize; // Saltamos los 4 bytes del bloque fijo
// Saltar los sub-bloques
uint8_t subBlockSize = *buffer++;
while (subBlockSize != 0)
{
buffer += subBlockSize;
subBlockSize = *buffer++;
}
break; break;
case TRAILER: }
case APPLICATION_EXTENSION: // 0xFF
case COMMENT_EXTENSION: // 0xFE
case PLAINTEXT_EXTENSION: // 0x01
{
// Para estas extensiones, saltamos el bloque fijo y los sub-bloques.
uint8_t blockSize = *buffer++;
buffer += blockSize;
uint8_t subBlockSize = *buffer++;
while (subBlockSize != 0)
{
buffer += subBlockSize;
subBlockSize = *buffer++;
}
break; break;
}
default: default:
fprintf(stderr, "Bailing on unrecognized block type %.02x\n", block_type); {
return nullptr; // Si la etiqueta de extensión es desconocida, saltarla también:
uint8_t blockSize = *buffer++;
buffer += blockSize;
uint8_t subBlockSize = *buffer++;
while (subBlockSize != 0)
{
buffer += subBlockSize;
subBlockSize = *buffer++;
}
break;
} }
} }
return nullptr; }
else if (block_type == IMAGE_DESCRIPTOR)
{
// Procesar el Image Descriptor y retornar los datos de imagen
return processImageDescriptor(buffer, global_color_table, color_resolution_bits);
}
else
{
std::fprintf(stderr, "Unrecognized block type %.02x\n", block_type);
return std::vector<uint8_t>{};
}
block_type = *buffer++;
} }
uint8_t *Gif::LoadGif(uint8_t *buffer, uint16_t *w, uint16_t *h) return std::vector<uint8_t>{};
{
return process_gif_stream(buffer, w, h);
} }
std::vector<uint8_t> Gif::loadGif(const uint8_t *buffer, uint16_t &w, uint16_t &h)
{
return processGifStream(buffer, w, h);
}
} // namespace GIF

View File

@@ -1,20 +1,21 @@
#pragma once #pragma once
#include <cstdint> // Para uint8_t, uint16_t, uint32_t #include <cstdint> // for uint8_t, uint16_t, uint32_t
#include <cstring> // Para memcpy #include <vector> // for vector
#define EXTENSION_INTRODUCER 0x21 namespace GIF
#define IMAGE_DESCRIPTOR 0x2C {
#define TRAILER 0x3B
#define GRAPHIC_CONTROL 0xF9
#define APPLICATION_EXTENSION 0xFF
#define COMMENT_EXTENSION 0xFE
#define PLAINTEXT_EXTENSION 0x01
#define READ(dst, size) \ // Constantes definidas con constexpr, en lugar de macros
memcpy(dst, buffer, size); \ constexpr uint8_t EXTENSION_INTRODUCER = 0x21;
buffer += size constexpr uint8_t IMAGE_DESCRIPTOR = 0x2C;
constexpr uint8_t TRAILER = 0x3B;
constexpr uint8_t GRAPHIC_CONTROL = 0xF9;
constexpr uint8_t APPLICATION_EXTENSION = 0xFF;
constexpr uint8_t COMMENT_EXTENSION = 0xFE;
constexpr uint8_t PLAINTEXT_EXTENSION = 0x01;
#pragma pack(push, 1)
struct ScreenDescriptor struct ScreenDescriptor
{ {
uint16_t width; uint16_t width;
@@ -37,6 +38,7 @@ struct ImageDescriptor
uint16_t image_height; uint16_t image_height;
uint8_t fields; uint8_t fields;
}; };
#pragma pack(pop)
struct DictionaryEntry struct DictionaryEntry
{ {
@@ -74,12 +76,27 @@ struct PlaintextExtension
class Gif class Gif
{ {
public: public:
void uncompress(int code_length, const uint8_t *input, int input_length, uint8_t *out); // Descompone (uncompress) el bloque comprimido usando LZW.
uint32_t *LoadPalette(uint8_t *buffer); // Este método puede lanzar std::runtime_error en caso de error.
uint8_t *LoadGif(uint8_t *buffer, uint16_t *w, uint16_t *h); void decompress(int code_length, const uint8_t *input, int input_length, uint8_t *out);
// Carga la paleta (global color table) a partir de un buffer,
// retornándola en un vector de uint32_t (cada color se compone de R, G, B).
std::vector<uint32_t> loadPalette(const uint8_t *buffer);
// Carga el stream GIF; devuelve un vector con los datos de imagen sin comprimir y
// asigna el ancho y alto mediante referencias.
std::vector<uint8_t> loadGif(const uint8_t *buffer, uint16_t &w, uint16_t &h);
private: private:
int read_sub_blocks(uint8_t *buffer, uint8_t **data); // Lee los sub-bloques de datos y los acumula en un std::vector<uint8_t>.
uint8_t *process_image_descriptor(uint8_t *buffer, RGB *gct, int gct_size, int resolution_bits); std::vector<uint8_t> readSubBlocks(const uint8_t *&buffer);
uint8_t *process_gif_stream(uint8_t *buffer, uint16_t *w, uint16_t *h);
// Procesa el Image Descriptor y retorna el vector de datos sin comprimir.
std::vector<uint8_t> processImageDescriptor(const uint8_t *&buffer, const std::vector<RGB> &gct, int resolution_bits);
// Procesa el stream completo del GIF y devuelve los datos sin comprimir.
std::vector<uint8_t> processGifStream(const uint8_t *buffer, uint16_t &w, uint16_t &h);
}; };
} // namespace GIF

View File

@@ -1,5 +1,4 @@
#include "jail_shader.h" #include "jail_shader.h"
#include <SDL2/SDL_opengl.h> // Para GLuint, glTexCoord2f, glVertex2f, GLfloat
#include <SDL2/SDL_rect.h> // Para SDL_Point #include <SDL2/SDL_rect.h> // Para SDL_Point
#include <SDL2/SDL_stdinc.h> // Para SDL_bool #include <SDL2/SDL_stdinc.h> // Para SDL_bool
#include <cstring> // Para strncmp #include <cstring> // Para strncmp
@@ -8,17 +7,15 @@
#include <vector> // Para vector #include <vector> // Para vector
#ifdef __APPLE__ #ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h" #include "CoreFoundation/CoreFoundation.h" // Para Core Foundation en macOS
#include <OpenGL/OpenGL.h> #include <OpenGL/OpenGL.h> // Para OpenGL en macOS
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 #if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#include <OpenGL/gl3.h> #include <OpenGL/gl3.h> // Para OpenGL 3 en macOS
#else // NO ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 #else // NO ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#include <OpenGL/gl.h> #include <OpenGL/gl.h> // Para OpenGL (compatibilidad) en macOS
#endif // ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 #endif // ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#else // SI NO ES __APPLE__ #else // SI NO ES __APPLE__
#include <SDL2/SDL_opengl.h> #include <SDL2/SDL_opengl.h> // Para GLuint, glTexCoord2f, glVertex2f, GLfloat
#endif // __APPLE__ #endif // __APPLE__
namespace shader namespace shader

View File

@@ -1,14 +1,15 @@
#include "texture.h" #include "texture.h"
#include <SDL2/SDL_error.h> // Para SDL_GetError #include <SDL2/SDL_error.h> // for SDL_GetError
#include <SDL2/SDL_surface.h> // Para SDL_CreateRGBSurfaceWithFormatFrom #include <SDL2/SDL_surface.h> // for SDL_CreateRGBSurfaceWithFormatFrom
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream #include <stdint.h> // for uint32_t
#include <iostream> // Para cerr, cout #include <cstring> // for memcpy, size_t
#include <stdexcept> // Para runtime_error #include <fstream> // for basic_ostream, operator<<, basic_ifstream
#include <string> // Para char_traits, operator<<, operator+ #include <iostream> // for cerr, cout
#include <vector> // Para vector #include <stdexcept> // for runtime_error
#include "gif.h" // Para LoadGif, LoadPalette #include <string> // for char_traits, operator<<, operator+
#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a... #include <vector> // for vector
#include "utils.h" // Para getFileName, printWithDots #include "gif.h" // for Gif
#include "utils.h" // for getFileName, Color, printWithDots
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" // Para stbi_failure_reason, stbi_image_free #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 // Añade la propia paleta del fichero a la lista
addPaletteFromFile(path_); addPaletteFromFile(path_);
// setPaletteColor(0, 0, 0x00000000);
// Crea la textura, establece el BlendMode y copia la surface a la textura // Crea la textura, establece el BlendMode y copia la surface a la textura
createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING); createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
@@ -77,18 +77,11 @@ bool Texture::loadFromFile(const std::string &file_path)
int depth, pitch; int depth, pitch;
Uint32 pixel_format; Uint32 pixel_format;
/*if (req_format == STBI_rgb)
{ // STBI_rgb_alpha (RGBA)
depth = 24;
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
pixel_format = SDL_PIXELFORMAT_RGB24;
}
else*/
{ // STBI_rgb_alpha (RGBA)
depth = 32; depth = 32;
pitch = 4 * width; pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32; pixel_format = SDL_PIXELFORMAT_RGBA32;
}
// Limpia // Limpia
unloadTexture(); unloadTexture();
@@ -249,7 +242,7 @@ void Texture::unloadSurface()
// Crea una surface desde un fichero .gif // Crea una surface desde un fichero .gif
std::shared_ptr<Surface> Texture::loadSurface(const std::string &file_path) std::shared_ptr<Surface> Texture::loadSurface(const std::string &file_path)
{ {
// Desencadenar la superficie actual // Libera la superficie actual
unloadSurface(); unloadSurface();
// Abrir el archivo usando std::ifstream para manejo automático del recurso // 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; std::cerr << "Error al leer el fichero " << file_path << std::endl;
throw std::runtime_error("Error al leer el fichero: " + file_path); throw std::runtime_error("Error al leer el fichero: " + file_path);
} }
// Cerrar el archivo (automáticamente manejado por std::ifstream)
file.close(); file.close();
// Crear un objeto Gif y llamar a la función LoadGif // Crear un objeto Gif y llamar a la función loadGif
Gif gif; GIF::Gif gif;
Uint16 w, h; Uint16 w = 0, h = 0;
Uint8 *rawPixels = gif.LoadGif(buffer.data(), &w, &h); std::vector<Uint8> rawPixels = gif.loadGif(buffer.data(), w, h);
if (!rawPixels) if (rawPixels.empty())
{ {
return nullptr; return nullptr;
} }
// Crear un std::shared_ptr con std::make_shared para pixels // Si el constructor de Surface espera un std::shared_ptr<Uint8[]>,
auto pixels = std::shared_ptr<Uint8[]>(rawPixels, std::default_delete<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); auto surface = std::make_shared<Surface>(w, h, pixels);
// Actualizar la anchura y altura // Actualizar las dimensiones
width_ = w; width_ = w;
height_ = h; height_ = h;
@@ -327,6 +322,7 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
{ {
std::vector<Uint32> palette; std::vector<Uint32> palette;
// Abrir el archivo GIF
std::ifstream file(file_path, std::ios::binary | std::ios::ate); std::ifstream file(file_path, std::ios::binary | std::ios::ate);
if (!file) if (!file)
{ {
@@ -338,7 +334,8 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]"); 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); file.seekg(0, std::ios::beg);
std::vector<Uint8> buffer(size); 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; 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)); throw std::runtime_error("Error al leer el fichero: " + getFileName(file_path));
} }
file.close();
Gif gif; // Usar la nueva función loadPalette, que devuelve un vector<uint32_t>
const auto *PAL = gif.LoadPalette(buffer.data()); GIF::Gif gif;
if (!PAL) 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; return palette;