diff --git a/src/helpers/Jpeg.cpp b/src/helpers/Jpeg.cpp index 9e6f8d6..e26e693 100644 --- a/src/helpers/Jpeg.cpp +++ b/src/helpers/Jpeg.cpp @@ -1,9 +1,7 @@ #include "Jpeg.hpp" -#include -#include -#include #include +#include cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) { @@ -17,19 +15,11 @@ cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) { exit(1); } - void* imageRawData; - - struct stat fileInfo = {}; - - const auto FD = open(path.c_str(), O_RDONLY); - - fstat(FD, &fileInfo); - - imageRawData = malloc(fileInfo.st_size); - - read(FD, imageRawData, fileInfo.st_size); - - close(FD); + std::ifstream file(path, std::ios::binary | std::ios::ate); + file.exceptions(std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit); + std::vector bytes(file.tellg()); + file.seekg(0); + file.read(reinterpret_cast(bytes.data()), bytes.size()); // now the JPEG is in the memory @@ -38,7 +28,7 @@ cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) { decompressStruct.err = jpeg_std_error(&errorManager); jpeg_create_decompress(&decompressStruct); - jpeg_mem_src(&decompressStruct, (const unsigned char*)imageRawData, fileInfo.st_size); + jpeg_mem_src(&decompressStruct, bytes.data(), bytes.size()); jpeg_read_header(&decompressStruct, true); #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ @@ -68,7 +58,7 @@ cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) { } cairo_surface_mark_dirty(cairoSurface); - cairo_surface_set_mime_data(cairoSurface, CAIRO_MIME_TYPE_JPEG, (const unsigned char*)imageRawData, fileInfo.st_size, free, imageRawData); + cairo_surface_set_mime_data(cairoSurface, CAIRO_MIME_TYPE_JPEG, bytes.data(), bytes.size(), nullptr, nullptr); jpeg_finish_decompress(&decompressStruct); jpeg_destroy_decompress(&decompressStruct); diff --git a/src/helpers/Webp.cpp b/src/helpers/Webp.cpp index 8eb997b..812d5cd 100644 --- a/src/helpers/Webp.cpp +++ b/src/helpers/Webp.cpp @@ -1,10 +1,7 @@ #include "Webp.hpp" -#include -#include -#include - #include +#include #include cairo_surface_t* WEBP::createSurfaceFromWEBP(const std::string& path) { @@ -14,19 +11,11 @@ cairo_surface_t* WEBP::createSurfaceFromWEBP(const std::string& path) { exit(1); } - void* imageRawData; - - struct stat fileInfo = {}; - - const auto FD = open(path.c_str(), O_RDONLY); - - fstat(FD, &fileInfo); - - imageRawData = malloc(fileInfo.st_size); - - read(FD, imageRawData, fileInfo.st_size); - - close(FD); + std::ifstream file(path, std::ios::binary | std::ios::ate); + file.exceptions(std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit); + std::vector bytes(file.tellg()); + file.seekg(0); + file.read(reinterpret_cast(bytes.data()), bytes.size()); // now the WebP is in the memory @@ -36,9 +25,8 @@ cairo_surface_t* WEBP::createSurfaceFromWEBP(const std::string& path) { exit(1); } - if (WebPGetFeatures((const unsigned char*)imageRawData, fileInfo.st_size, &config.input) != VP8_STATUS_OK) { + if (WebPGetFeatures(bytes.data(), bytes.size(), &config.input) != VP8_STATUS_OK) { Debug::log(ERR, "createSurfaceFromWEBP: file is not webp format"); - free(imageRawData); exit(1); } @@ -69,13 +57,13 @@ cairo_surface_t* WEBP::createSurfaceFromWEBP(const std::string& path) { config.output.width = WIDTH; config.output.height = HEIGHT; - if (WebPDecode((const unsigned char*)imageRawData, fileInfo.st_size, &config) != VP8_STATUS_OK) { + if (WebPDecode(bytes.data(), bytes.size(), &config) != VP8_STATUS_OK) { Debug::log(CRIT, "createSurfaceFromWEBP: WebP Decode Failed (?)"); exit(1); } cairo_surface_mark_dirty(cairoSurface); - cairo_surface_set_mime_data(cairoSurface, CAIRO_MIME_TYPE_PNG, (const unsigned char*)imageRawData, fileInfo.st_size, free, imageRawData); + cairo_surface_set_mime_data(cairoSurface, "image/webp", bytes.data(), bytes.size(), nullptr, nullptr); WebPFreeDecBuffer(&config.output);