Refactor: is_exr_file checks memory buffer

All places that used is_exr_file already had first four bytes
of the file read into memory. So make is_exr_file work just
like is_dds_file does and check those bytes directly, no need
to hit stdio again.
This commit is contained in:
Aras Pranckevicius
2024-11-04 13:30:22 +02:00
committed by Baldur Karlsson
parent 6a05176075
commit 02fd7ac0cd
5 changed files with 12 additions and 19 deletions
+1 -1
View File
@@ -952,7 +952,7 @@ RDResult write_dds_to_file(FILE *f, const write_tex_data &data)
return RDResult();
}
bool is_dds_file(byte *headerBuffer, size_t size)
bool is_dds_file(const byte *headerBuffer, size_t size)
{
if(size < 4)
{
+1 -1
View File
@@ -28,6 +28,6 @@
#include "serialise/streamio.h"
#include "tex_data.h"
extern bool is_dds_file(byte *headerBuffer, size_t size);
extern bool is_dds_file(const byte *headerBuffer, size_t size);
extern RDResult load_dds_from_file(StreamReader *reader, read_tex_data &data);
extern RDResult write_dds_to_file(FILE *f, const write_tex_data &data);
+1 -1
View File
@@ -42,7 +42,7 @@ struct ReplayOptions;
struct SDObject;
// not provided by tinyexr, just do by hand
bool is_exr_file(FILE *f);
bool is_exr_file(const byte *headerBuffer, size_t size);
void LogReplayOptions(const ReplayOptions &opts);
enum class RDCDriver : uint32_t;
+2 -2
View File
@@ -433,7 +433,7 @@ RDResult IMG_CreateReplayDevice(RDCFile *rdc, IReplayDriver **driver)
FileIO::fseek64(f, 0, SEEK_SET);
// make sure the file is a type we recognise before going further
if(is_exr_file(f))
if(is_exr_file(headerBuffer, headerSize))
{
FileIO::fseek64(f, 0, SEEK_END);
uint64_t size = FileIO::ftell64(f);
@@ -645,7 +645,7 @@ void ImageViewer::RefreshFile()
uint64_t fileSize = FileIO::ftell64(f);
FileIO::fseek64(f, 0, SEEK_SET);
if(is_exr_file(f))
if(is_exr_file(headerBuffer, headerSize))
{
texDetails.format = rgba32_float;
+7 -14
View File
@@ -33,18 +33,14 @@
#include "zstdio.h"
// not provided by tinyexr, just do by hand
bool is_exr_file(FILE *f)
bool is_exr_file(const byte *headerBuffer, size_t size)
{
FileIO::fseek64(f, 0, SEEK_SET);
if(size < 4)
{
return false;
}
const uint32_t openexr_magic = MAKE_FOURCC(0x76, 0x2f, 0x31, 0x01);
uint32_t magic = 0;
size_t bytesRead = FileIO::fread(&magic, 1, sizeof(magic), f);
FileIO::fseek64(f, 0, SEEK_SET);
return bytesRead == sizeof(magic) && magic == openexr_magic;
return memcmp(headerBuffer, &openexr_magic, 4) == 0;
}
/*
@@ -275,10 +271,7 @@ void RDCFile::Open(const rdcstr &path)
byte headerBuffer[4];
const size_t headerSize = FileIO::fread(headerBuffer, 1, 4, m_File);
if(is_dds_file(headerBuffer, headerSize))
ret = x = y = comp = 1;
if(is_exr_file(m_File))
if(is_dds_file(headerBuffer, headerSize) || is_exr_file(headerBuffer, headerSize))
ret = x = y = comp = 1;
FileIO::fseek64(m_File, 0, SEEK_SET);