Skip to content

Commit

Permalink
Allow empty file name (#1898)
Browse files Browse the repository at this point in the history
When providing a custom stream, there is no reason a filename needs to
be provided beyond clearer error messages. Because std::string does not
allow null, continue to disallow a null filename. Add a test case

Signed-off-by: Kimball Thurston <[email protected]>
  • Loading branch information
kdt3rd authored Oct 26, 2024
1 parent 4332f5e commit b6fc6ec
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/lib/OpenEXRCore/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ exr_test_file_header (
exr_context_t ret = NULL;
exr_context_initializer_t inits = fill_context_data (ctxtdata);

if (filename && filename[0] != '\0')
if (filename)
{
rv = internal_exr_alloc_context (
&ret,
Expand Down Expand Up @@ -244,7 +244,7 @@ exr_start_read (
return EXR_ERR_INVALID_ARGUMENT;
}

if (filename && filename[0] != '\0')
if (filename)
{
rv = internal_exr_alloc_context (
&ret,
Expand Down Expand Up @@ -311,7 +311,7 @@ exr_start_write (
return EXR_ERR_INVALID_ARGUMENT;
}

if (filename && filename[0] != '\0')
if (filename)
{
rv = internal_exr_alloc_context (
&ret,
Expand Down
2 changes: 1 addition & 1 deletion src/test/OpenEXRCoreTest/read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ testReadBadArgs (const std::string& tempdir)
EXRCORE_TEST_RVAL_FAIL (
EXR_ERR_INVALID_ARGUMENT, exr_start_read (&f, NULL, &cinit));
EXRCORE_TEST_RVAL_FAIL (
EXR_ERR_INVALID_ARGUMENT, exr_start_read (&f, "", &cinit));
EXR_ERR_FILE_ACCESS, exr_start_read (&f, "", &cinit));
// windows fails on directory open, where under unix you can open
// the directory as a file handle but not read from it
#ifdef _WIN32
Expand Down
56 changes: 56 additions & 0 deletions src/test/OpenEXRTest/testExistingStreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,29 @@ MMIFStream::readMemoryMapped (int n)
return retVal;
}

class PassThruIFStream : public OPENEXR_IMF_NAMESPACE::IStream
{
public:
//-------------------------------------------------------
// A constructor that opens the file with the given name.
//-------------------------------------------------------

PassThruIFStream (MMIFStream &s) : IStream(""), _s (s) {}

virtual ~PassThruIFStream () {}

virtual bool isMemoryMapped () const { return _s.isMemoryMapped (); }

virtual bool read (char c[/*n*/], int n) { return _s.read (c, n); }
virtual char* readMemoryMapped (int n) { return _s.readMemoryMapped (n); }
virtual uint64_t tellg () { return _s.tellg (); }
virtual void seekg (uint64_t pos) { _s.seekg (pos); }
virtual void clear () { _s.clear (); }

private:
MMIFStream &_s;
};

void
writeReadScanLines (
const char fileName[],
Expand Down Expand Up @@ -345,6 +368,39 @@ writeReadScanLines (
}
}

{
cout << ", reading (memory-mapped, passthru)";
MMIFStream ifs (fileName);
PassThruIFStream pfs (ifs);

RgbaInputFile in (pfs);

const Box2i& dw = in.dataWindow ();
int w = dw.max.x - dw.min.x + 1;
int h = dw.max.y - dw.min.y + 1;
int dx = dw.min.x;
int dy = dw.min.y;

Array2D<Rgba> p2 (h, w);
in.setFrameBuffer (&p2[-dy][-dx], 1, w);
in.readPixels (dw.min.y, dw.max.y);

if (!isLossyCompression (compression))
{
cout << ", comparing";
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
assert (p2[y][x].r == p1[y][x].r);
assert (p2[y][x].g == p1[y][x].g);
assert (p2[y][x].b == p1[y][x].b);
assert (p2[y][x].a == p1[y][x].a);
}
}
}
}

cout << endl;

remove (fileName);
Expand Down

0 comments on commit b6fc6ec

Please sign in to comment.