Retocs en Texture::loadPaletteFromFile

This commit is contained in:
2024-11-03 20:43:52 +01:00
parent 371c477d0d
commit 4d3e862f6a

View File

@@ -2,13 +2,13 @@
#include "texture.h"
#include <SDL2/SDL_error.h> // Para SDL_GetError
#include <SDL2/SDL_surface.h> // Para SDL_CreateRGBSurfaceWithFormatFrom
#include <fcntl.h> // Para SEEK_END, SEEK_SET
#include <stdio.h> // Para fseek, fclose, fopen, fread, ftell, FILE
#include <stdlib.h> // Para free, malloc
#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.c" // Para LoadGif, LoadPalette
#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a...
#include "utils.h" // Para getFileName, printWithDots
#define STB_IMAGE_IMPLEMENTATION
@@ -315,8 +315,8 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
{
std::vector<Uint32> palette;
FILE *f = fopen(file_path.c_str(), "rb");
if (!f)
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
if (!file)
{
std::cerr << "Error: Fichero no encontrado " << getFileName(file_path) << std::endl;
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
@@ -326,21 +326,22 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]");
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
Uint8 *buffer = static_cast<Uint8 *>(malloc(size));
fread(buffer, size, 1, f);
fclose(f);
auto size = file.tellg();
file.seekg(0, std::ios::beg);
const auto *pal = LoadPalette(buffer);
std::vector<Uint8> buffer(size);
if (!file.read(reinterpret_cast<char *>(buffer.data()), size))
{
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));
}
const auto pal = LoadPalette(buffer.data());
if (!pal)
{
return palette;
}
free(buffer);
for (int i = 0; i < 256; ++i)
{
palette.push_back((pal[i] << 8) + 255);