Skip to content

Commit

Permalink
core: use C++ streams to load Jpeg and Webp (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat authored Nov 22, 2024
1 parent dbea6cd commit 0b5e350
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 39 deletions.
26 changes: 8 additions & 18 deletions src/helpers/Jpeg.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "Jpeg.hpp"

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <filesystem>
#include <fstream>

cairo_surface_t* JPEG::createSurfaceFromJPEG(const std::string& path) {

Expand All @@ -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<uint8_t> bytes(file.tellg());
file.seekg(0);
file.read(reinterpret_cast<char*>(bytes.data()), bytes.size());

// now the JPEG is in the memory

Expand All @@ -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__
Expand Down Expand Up @@ -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);

Expand Down
30 changes: 9 additions & 21 deletions src/helpers/Webp.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#include "Webp.hpp"

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <filesystem>
#include <fstream>
#include <webp/decode.h>

cairo_surface_t* WEBP::createSurfaceFromWEBP(const std::string& path) {
Expand All @@ -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<uint8_t> bytes(file.tellg());
file.seekg(0);
file.read(reinterpret_cast<char*>(bytes.data()), bytes.size());

// now the WebP is in the memory

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 0b5e350

Please sign in to comment.