core: use C++ streams to load Jpeg and Webp (#214)

This commit is contained in:
Adrià 2024-11-22 22:18:04 +09:00 committed by GitHub
parent dbea6cdf0c
commit 0b5e350011
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 39 deletions

View file

@ -1,9 +1,7 @@
#include "Jpeg.hpp" #include "Jpeg.hpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <filesystem> #include <filesystem>
#include <fstream>
cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) { cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) {
@ -17,19 +15,11 @@ cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) {
exit(1); exit(1);
} }
void* imageRawData; std::ifstream file(path, std::ios::binary | std::ios::ate);
file.exceptions(std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit);
struct stat fileInfo = {}; std::vector<uint8_t> bytes(file.tellg());
file.seekg(0);
const auto FD = open(path.c_str(), O_RDONLY); file.read(reinterpret_cast<char*>(bytes.data()), bytes.size());
fstat(FD, &fileInfo);
imageRawData = malloc(fileInfo.st_size);
read(FD, imageRawData, fileInfo.st_size);
close(FD);
// now the JPEG is in the memory // 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); decompressStruct.err = jpeg_std_error(&errorManager);
jpeg_create_decompress(&decompressStruct); 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); jpeg_read_header(&decompressStruct, true);
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ #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_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_finish_decompress(&decompressStruct);
jpeg_destroy_decompress(&decompressStruct); jpeg_destroy_decompress(&decompressStruct);

View file

@ -1,10 +1,7 @@
#include "Webp.hpp" #include "Webp.hpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <filesystem> #include <filesystem>
#include <fstream>
#include <webp/decode.h> #include <webp/decode.h>
cairo_surface_t* WEBP::createSurfaceFromWEBP(const std::string& path) { cairo_surface_t* WEBP::createSurfaceFromWEBP(const std::string& path) {
@ -14,19 +11,11 @@ cairo_surface_t* WEBP::createSurfaceFromWEBP(const std::string& path) {
exit(1); exit(1);
} }
void* imageRawData; std::ifstream file(path, std::ios::binary | std::ios::ate);
file.exceptions(std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit);
struct stat fileInfo = {}; std::vector<uint8_t> bytes(file.tellg());
file.seekg(0);
const auto FD = open(path.c_str(), O_RDONLY); file.read(reinterpret_cast<char*>(bytes.data()), bytes.size());
fstat(FD, &fileInfo);
imageRawData = malloc(fileInfo.st_size);
read(FD, imageRawData, fileInfo.st_size);
close(FD);
// now the WebP is in the memory // now the WebP is in the memory
@ -36,9 +25,8 @@ cairo_surface_t* WEBP::createSurfaceFromWEBP(const std::string& path) {
exit(1); 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"); Debug::log(ERR, "createSurfaceFromWEBP: file is not webp format");
free(imageRawData);
exit(1); exit(1);
} }
@ -69,13 +57,13 @@ cairo_surface_t* WEBP::createSurfaceFromWEBP(const std::string& path) {
config.output.width = WIDTH; config.output.width = WIDTH;
config.output.height = HEIGHT; 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 (?)"); Debug::log(CRIT, "createSurfaceFromWEBP: WebP Decode Failed (?)");
exit(1); exit(1);
} }
cairo_surface_mark_dirty(cairoSurface); 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); WebPFreeDecBuffer(&config.output);