Skip to content

Commit

Permalink
Fix for reading memory mapped files with DWA compression (#1333)
Browse files Browse the repository at this point in the history
* Test memory mapping

Signed-off-by: Darby Johnston <darbyjohnston@yahoo.com>

* Add Windows memory mapping

Signed-off-by: Darby Johnston <darbyjohnston@yahoo.com>

* Fix for DWAA conpression and memory mapping

Signed-off-by: Darby Johnston <darbyjohnston@yahoo.com>

* Remove duplicate code

Signed-off-by: Darby Johnston <darbyjohnston@yahoo.com>

---------

Signed-off-by: Darby Johnston <darbyjohnston@yahoo.com>
  • Loading branch information
darbyjohnston authored and cary-ilm committed Mar 5, 2023
1 parent 1132648 commit a2efebf
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 170 deletions.
14 changes: 11 additions & 3 deletions src/lib/OpenEXR/ImfDwaCompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
#include "ImathVec.h"
#include "half.h"

#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <limits>
#include <string>
#include <vector>
#include <string>
#include <cctype>
Expand Down Expand Up @@ -2347,10 +2353,12 @@ DwaCompressor::uncompress
// Flip the counters from XDR to NATIVE
//

std::array<uint64_t, NUM_SIZES_SINGLE> counterBuf;
memcpy (counterBuf.data (), inPtr, counterBuf.size() * sizeof (uint64_t));
for (int i = 0; i < NUM_SIZES_SINGLE; ++i)
{
uint64_t *dst = (((uint64_t *)inPtr) + i);
const char *src = (char *)(((uint64_t *)inPtr) + i);
uint64_t* dst = counterBuf.data() + i;
const char* src = (char*) (counterBuf.data() + i);

Xdr::read<CharPtrIO> (src, *dst);
}
Expand All @@ -2359,7 +2367,7 @@ DwaCompressor::uncompress
// Unwind all the counter info
//

const uint64_t *inPtr64 = (const uint64_t*) inPtr;
const uint64_t* inPtr64 = counterBuf.data();

uint64_t version = *(inPtr64 + VERSION);
uint64_t unknownUncompressedSize = *(inPtr64 + UNKNOWN_UNCOMPRESSED_SIZE);
Expand Down
9 changes: 9 additions & 0 deletions src/lib/OpenEXR/ImfMisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <ImfTileDescription.h>
#include "ImfNamespace.h"

#include <codecvt>
#include <locale>

OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER

using IMATH_NAMESPACE::Box2i;
Expand Down Expand Up @@ -1893,5 +1896,11 @@ getChunkOffsetTableSize(const Header& header)

}

std::wstring
WidenFilename (const char* filename)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
return converter.from_bytes (filename);
}

OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT
8 changes: 8 additions & 0 deletions src/lib/OpenEXR/ImfMisc.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,14 @@ IMF_EXPORT
int getChunkOffsetTableSize(const Header& header);


//
// Convert a filename to a wide string. This is useful for working with
// filenames on Windows.
//

IMF_EXPORT
std::wstring WidenFilename (const char* filename);

OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT


Expand Down
22 changes: 6 additions & 16 deletions src/lib/OpenEXR/ImfStdIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//
//-----------------------------------------------------------------------------

#include "Iex.h"
#include <ImfMisc.h>
#include <ImfStdIO.h>
#include "Iex.h"
#include <errno.h>
Expand All @@ -34,22 +36,10 @@ OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
namespace {

#ifdef _WIN32
wstring WidenFilename (const char *filename)
{
wstring ret;
int fnlen = static_cast<int>( strlen(filename) );
int len = MultiByteToWideChar(CP_UTF8, 0, filename, fnlen, NULL, 0 );
if (len > 0)
{
ret.resize(len);
MultiByteToWideChar(CP_UTF8, 0, filename, fnlen, &ret[0], len);
}
return ret;
}

# if defined(__GLIBCXX__) && !(defined(_GLIBCXX_HAVE_WFOPEN) && defined(_GLIBCXX_USE_WCHAR_T))
# define USE_CUSTOM_WIDE_OPEN 1
# endif
# if defined(__GLIBCXX__) && \
!(defined(_GLIBCXX_HAVE_WFOPEN) && defined(_GLIBCXX_USE_WCHAR_T))
# define USE_CUSTOM_WIDE_OPEN 1
# endif

# ifdef USE_CUSTOM_WIDE_OPEN
template <typename CharT, typename TraitsT>
Expand Down
22 changes: 5 additions & 17 deletions src/test/OpenEXRTest/TestUtilFStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#ifndef INCLUDE_TestUtilFStream_h_
#define INCLUDE_TestUtilFStream_h_ 1

#include <fstream>
#include <string>
# include <ImfMisc.h>

# include <fstream>
# include <string>

#ifdef _WIN32
# ifndef NOMINMAX
Expand All @@ -25,21 +27,7 @@

namespace testutil
{
#ifdef _WIN32
inline std::wstring
WidenFilename (const char* filename)
{
std::wstring ret;
int fnlen = static_cast<int> (strlen (filename));
int len = MultiByteToWideChar (CP_UTF8, 0, filename, fnlen, NULL, 0);
if (len > 0)
{
ret.resize (len);
MultiByteToWideChar (CP_UTF8, 0, filename, fnlen, &ret[0], len);
}
return ret;
}

# ifdef _WIN32
// This is a big work around mechanism for compiling using mingw / gcc under windows
// until mingw 9 where they add the wide filename version of open
# if (defined(__GLIBCXX__) && !(defined(_GLIBCXX_HAVE_WFOPEN) && defined(_GLIBCXX_USE_WCHAR_T)))
Expand Down
Loading

0 comments on commit a2efebf

Please sign in to comment.