1
0
Fork 0

Graphics/ImageLoader: move decode() to loadFromMemory()

This commit is contained in:
Henrik Hautakoski 2020-01-19 22:00:56 +01:00
parent 29749e945a
commit 8a3b5a17d8
No known key found for this signature in database
GPG key ID: 96765B12FEAC4745
2 changed files with 18 additions and 31 deletions

View file

@ -35,8 +35,7 @@ bool ImageLoader::loadFromFile(const char *filename, Image& img)
fread(&buf[0], 1, buf.size(), fd);
fclose(fd);
// loaded into memory. now decode.
if (decode((const char*)&buf[0], buf.size(), img) == false) {
if (loadFromMemory(&buf[0], buf.size(), img) == false) {
Log::warn("ImageLoader: could not load file '%s'. Reason: %s",
filename, m_error);
return false;
@ -51,11 +50,25 @@ bool ImageLoader::loadFromFile(const char *filename, Image& img)
bool ImageLoader::loadFromMemory(const void *data, unsigned size, Image& img)
{
//std::vector<unsigned char> buf;
// width, height, num components.
int w, h, n_comp;
const stbi_uc *ptr = (const stbi_uc *) data;
//buf.assign(((unsigned char*) data), ((unsigned char*) data) + size);
unsigned char *pixels = stbi_load_from_memory(ptr, size, &w, &h, &n_comp, 4);
return decode((const char*) data, size, img);
if (pixels) {
unsigned int len = w * h * 4;
img.create(w, h, pixels);
stbi_image_free(pixels);
return true;
}
m_error = stbi_failure_reason();
return false;
}
// TODO: Support more formats.
@ -89,30 +102,6 @@ bool ImageLoader::saveToFile(const Image& img, const char *filename)
return false;
}
bool ImageLoader::decode(const char *data, unsigned int size, Image& img)
{
// width, height, num components.
int w, h, n_comp;
const stbi_uc *ptr = (const stbi_uc *) data;
unsigned char *pixels = stbi_load_from_memory(ptr, size, &w, &h, &n_comp, 4);
if (pixels) {
unsigned int len = w * h * 4;
img.create(w, h, pixels);
stbi_image_free(pixels);
return true;
}
m_error = stbi_failure_reason();
return false;
}
bool ImageLoader::encodePNG(const Image& img, std::vector<unsigned char>& data)
{
int buf_len;