Skip to content

Commit

Permalink
Image: Don't use libjpeg stdio functions
Browse files Browse the repository at this point in the history
Fixes I/O in debug builds.
  • Loading branch information
stenzek committed Apr 3, 2024
1 parent cb6be52 commit c36506a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/common/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,6 @@ static constexpr u32 HOST_PAGE_SHIFT = 12;
static_cast<std::underlying_type<type_>::type>(rhs)); \
return lhs; \
}

// Compute the address of a base type given a field offset.
#define BASE_FROM_RECORD_FIELD(ptr, base_type, field) ((base_type*)(((char*)ptr) - offsetof(base_type, field)))
101 changes: 99 additions & 2 deletions src/util/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,62 @@ bool JPEGBufferLoader(RGBA8Image* image, const void* buffer, size_t buffer_size)

bool JPEGFileLoader(RGBA8Image* image, const char* filename, std::FILE* fp)
{
return WrapJPEGDecompress(image, [fp](jpeg_decompress_struct& info) { jpeg_stdio_src(&info, fp); });
static constexpr u32 BUFFER_SIZE = 16384;

struct FileCallback
{
jpeg_source_mgr mgr;

std::FILE* fp;
std::unique_ptr<u8[]> buffer;
bool end_of_file;
};

FileCallback cb = {
.mgr = {
.init_source = [](j_decompress_ptr cinfo) {},
.fill_input_buffer = [](j_decompress_ptr cinfo) -> boolean {
FileCallback* cb = BASE_FROM_RECORD_FIELD(cinfo->src, FileCallback, mgr);
cb->mgr.next_input_byte = cb->buffer.get();
if (cb->end_of_file)
{
cb->buffer[0] = 0xFF;
cb->buffer[1] = JPEG_EOI;
cb->mgr.bytes_in_buffer = 2;
return true;
}

const size_t r = std::fread(cb->buffer.get(), 1, BUFFER_SIZE, cb->fp);
cb->end_of_file |= (std::feof(cb->fp) != 0);
cb->mgr.bytes_in_buffer = r;
return true;
},
.skip_input_data =
[](j_decompress_ptr cinfo, long num_bytes) {
FileCallback* cb = BASE_FROM_RECORD_FIELD(cinfo->src, FileCallback, mgr);
const size_t skip_in_buffer = std::min<size_t>(cb->mgr.bytes_in_buffer, static_cast<size_t>(num_bytes));
cb->mgr.next_input_byte += skip_in_buffer;
cb->mgr.bytes_in_buffer -= skip_in_buffer;

const size_t seek_cur = static_cast<size_t>(num_bytes) - skip_in_buffer;
if (seek_cur > 0)
{
if (FileSystem::FSeek64(cb->fp, static_cast<size_t>(seek_cur), SEEK_CUR) != 0)
{
cb->end_of_file = true;
return;
}
}
},
.resync_to_restart = jpeg_resync_to_restart,
.term_source = [](j_decompress_ptr cinfo) {},
},
.fp = fp,
.buffer = std::make_unique<u8[]>(BUFFER_SIZE),
.end_of_file = false,
};

return WrapJPEGDecompress(image, [&cb](jpeg_decompress_struct& info) { info.src = &cb.mgr; });
}

template<typename T>
Expand Down Expand Up @@ -613,7 +668,49 @@ bool JPEGBufferSaver(const RGBA8Image& image, std::vector<u8>* buffer, u8 qualit

bool JPEGFileSaver(const RGBA8Image& image, const char* filename, std::FILE* fp, u8 quality)
{
return WrapJPEGCompress(image, quality, [fp](jpeg_compress_struct& info) { jpeg_stdio_dest(&info, fp); });
static constexpr u32 BUFFER_SIZE = 16384;

struct FileCallback
{
jpeg_destination_mgr mgr;

std::FILE* fp;
std::unique_ptr<u8[]> buffer;
bool write_error;
};

FileCallback cb = {
.mgr = {
.init_destination =
[](j_compress_ptr cinfo) {
FileCallback* cb = BASE_FROM_RECORD_FIELD(cinfo->dest, FileCallback, mgr);
cb->mgr.next_output_byte = cb->buffer.get();
cb->mgr.free_in_buffer = BUFFER_SIZE;
},
.empty_output_buffer = [](j_compress_ptr cinfo) -> boolean {
FileCallback* cb = BASE_FROM_RECORD_FIELD(cinfo->dest, FileCallback, mgr);
if (!cb->write_error)
cb->write_error |= (std::fwrite(cb->buffer.get(), 1, BUFFER_SIZE, cb->fp) != BUFFER_SIZE);

cb->mgr.next_output_byte = cb->buffer.get();
cb->mgr.free_in_buffer = BUFFER_SIZE;
return true;
},
.term_destination =
[](j_compress_ptr cinfo) {
FileCallback* cb = BASE_FROM_RECORD_FIELD(cinfo->dest, FileCallback, mgr);
const size_t left = BUFFER_SIZE - cb->mgr.free_in_buffer;
if (left > 0 && !cb->write_error)
cb->write_error |= (std::fwrite(cb->buffer.get(), 1, left, cb->fp) != left);
},
},
.fp = fp,
.buffer = std::make_unique<u8[]>(BUFFER_SIZE),
.write_error = false,
};

return (WrapJPEGCompress(image, quality, [&cb](jpeg_compress_struct& info) { info.dest = &cb.mgr; }) &&
!cb.write_error);
}

bool WebPBufferLoader(RGBA8Image* image, const void* buffer, size_t buffer_size)
Expand Down
38 changes: 26 additions & 12 deletions src/util/imgui_fullscreen.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <[email protected]>
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)

#define IMGUI_DEFINE_MATH_OPERATORS
Expand All @@ -10,6 +10,7 @@

#include "common/assert.h"
#include "common/easing.h"
#include "common/error.h"
#include "common/file_system.h"
#include "common/log.h"
#include "common/lru_cache.h"
Expand Down Expand Up @@ -271,24 +272,37 @@ const std::shared_ptr<GPUTexture>& ImGuiFullscreen::GetPlaceholderTexture()
std::optional<RGBA8Image> ImGuiFullscreen::LoadTextureImage(const char* path)
{
std::optional<RGBA8Image> image;

std::optional<std::vector<u8>> data;
if (Path::IsAbsolute(path))
data = FileSystem::ReadBinaryFile(path);
else
data = Host::ReadResourceFile(path, true);
if (data.has_value())
{
image = RGBA8Image();
if (!image->LoadFromBuffer(path, data->data(), data->size()))
Error error;
auto fp = FileSystem::OpenManagedCFile(path, "rb", &error);
if (fp)
{
image = RGBA8Image();
if (!image->LoadFromFile(path, fp.get()))
Log_ErrorFmt("Failed to read texture file '{}'", path);
}
else
{
Log_ErrorPrintf("Failed to read texture resource '%s'", path);
image.reset();
Log_ErrorFmt("Failed to open texture file '{}': {}", path, error.GetDescription());
}
}
else
{
Log_ErrorPrintf("Failed to open texture resource '%s'", path);
std::optional<std::vector<u8>> data = Host::ReadResourceFile(path, true);
if (data.has_value())
{
image = RGBA8Image();
if (!image->LoadFromBuffer(path, data->data(), data->size()))
{
Log_ErrorFmt("Failed to read texture resource '{}'", path);
image.reset();
}
}
else
{
Log_ErrorFmt("Failed to open texture resource '{}'", path);
}
}

return image;
Expand Down

0 comments on commit c36506a

Please sign in to comment.