mirror of
https://github.com/bkaradzic/bimg.git
synced 2026-06-08 10:53:46 +00:00
Updated tinyexr.
This commit is contained in:
159
3rdparty/tinyexr/tinyexr.h
vendored
159
3rdparty/tinyexr/tinyexr.h
vendored
@@ -207,7 +207,8 @@ typedef struct _EXRHeader {
|
||||
// Custom attributes(exludes required attributes(e.g. `channels`,
|
||||
// `compression`, etc)
|
||||
int num_custom_attributes;
|
||||
EXRAttribute *custom_attributes; // array of EXRAttribute. size = `num_custom_attributes`.
|
||||
EXRAttribute *custom_attributes; // array of EXRAttribute. size =
|
||||
// `num_custom_attributes`.
|
||||
|
||||
EXRChannelInfo *channels; // [num_channels]
|
||||
|
||||
@@ -6940,6 +6941,14 @@ void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename,
|
||||
|
||||
static const int kEXRVersionSize = 8;
|
||||
|
||||
static void cpy2(unsigned short *dst_val, const unsigned short *src_val) {
|
||||
unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
|
||||
const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
|
||||
|
||||
dst[0] = src[0];
|
||||
dst[1] = src[1];
|
||||
}
|
||||
|
||||
static void swap2(unsigned short *val) {
|
||||
#ifdef MINIZ_LITTLE_ENDIAN
|
||||
(void)val;
|
||||
@@ -6953,6 +6962,36 @@ static void swap2(unsigned short *val) {
|
||||
#endif
|
||||
}
|
||||
|
||||
static void cpy4(int *dst_val, const int *src_val) {
|
||||
unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
|
||||
const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
|
||||
|
||||
dst[0] = src[0];
|
||||
dst[1] = src[1];
|
||||
dst[2] = src[2];
|
||||
dst[3] = src[3];
|
||||
}
|
||||
|
||||
static void cpy4(unsigned int *dst_val, const unsigned int *src_val) {
|
||||
unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
|
||||
const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
|
||||
|
||||
dst[0] = src[0];
|
||||
dst[1] = src[1];
|
||||
dst[2] = src[2];
|
||||
dst[3] = src[3];
|
||||
}
|
||||
|
||||
static void cpy4(float *dst_val, const float *src_val) {
|
||||
unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
|
||||
const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
|
||||
|
||||
dst[0] = src[0];
|
||||
dst[1] = src[1];
|
||||
dst[2] = src[2];
|
||||
dst[3] = src[3];
|
||||
}
|
||||
|
||||
static void swap4(unsigned int *val) {
|
||||
#ifdef MINIZ_LITTLE_ENDIAN
|
||||
(void)val;
|
||||
@@ -6968,6 +7007,22 @@ static void swap4(unsigned int *val) {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if 0
|
||||
static void cpy8(tinyexr::tinyexr_uint64 *dst_val, const tinyexr::tinyexr_uint64 *src_val) {
|
||||
unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
|
||||
const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
|
||||
|
||||
dst[0] = src[0];
|
||||
dst[1] = src[1];
|
||||
dst[2] = src[2];
|
||||
dst[3] = src[3];
|
||||
dst[4] = src[4];
|
||||
dst[5] = src[5];
|
||||
dst[6] = src[6];
|
||||
dst[7] = src[7];
|
||||
}
|
||||
#endif
|
||||
|
||||
static void swap8(tinyexr::tinyexr_uint64 *val) {
|
||||
#ifdef MINIZ_LITTLE_ENDIAN
|
||||
(void)val;
|
||||
@@ -9055,8 +9110,8 @@ static bool CompressPiz(unsigned char *outPtr, unsigned int *outSize,
|
||||
}
|
||||
}
|
||||
|
||||
bitmapFromData(&tmpBuffer.at(0), static_cast<int>(tmpBuffer.size()), bitmap.data(),
|
||||
minNonZero, maxNonZero);
|
||||
bitmapFromData(&tmpBuffer.at(0), static_cast<int>(tmpBuffer.size()),
|
||||
bitmap.data(), minNonZero, maxNonZero);
|
||||
|
||||
std::vector<unsigned short> lut(USHORT_RANGE);
|
||||
unsigned short maxValue = forwardLutFromBitmap(bitmap.data(), lut.data());
|
||||
@@ -9143,8 +9198,10 @@ static bool DecompressPiz(unsigned char *outPtr, const unsigned char *inPtr,
|
||||
memset(bitmap.data(), 0, BITMAP_SIZE);
|
||||
|
||||
const unsigned char *ptr = inPtr;
|
||||
minNonZero = *(reinterpret_cast<const unsigned short *>(ptr));
|
||||
maxNonZero = *(reinterpret_cast<const unsigned short *>(ptr + 2));
|
||||
//minNonZero = *(reinterpret_cast<const unsigned short *>(ptr));
|
||||
tinyexr::cpy2(&minNonZero, reinterpret_cast<const unsigned short *>(ptr));
|
||||
//maxNonZero = *(reinterpret_cast<const unsigned short *>(ptr + 2));
|
||||
tinyexr::cpy2(&maxNonZero, reinterpret_cast<const unsigned short *>(ptr + 2));
|
||||
ptr += 4;
|
||||
|
||||
if (maxNonZero >= BITMAP_SIZE) {
|
||||
@@ -9167,7 +9224,8 @@ static bool DecompressPiz(unsigned char *outPtr, const unsigned char *inPtr,
|
||||
|
||||
int length;
|
||||
|
||||
length = *(reinterpret_cast<const int *>(ptr));
|
||||
//length = *(reinterpret_cast<const int *>(ptr));
|
||||
tinyexr::cpy4(&length, reinterpret_cast<const int *>(ptr));
|
||||
ptr += sizeof(int);
|
||||
|
||||
std::vector<unsigned short> tmpBuffer(tmpBufSize);
|
||||
@@ -9481,7 +9539,8 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
for (size_t u = 0; u < static_cast<size_t>(width); u++) {
|
||||
FP16 hf;
|
||||
|
||||
hf.u = line_ptr[u];
|
||||
// hf.u = line_ptr[u];
|
||||
tinyexr::cpy2(&(hf.u), line_ptr + u);
|
||||
|
||||
tinyexr::swap2(reinterpret_cast<unsigned short *>(&hf.u));
|
||||
|
||||
@@ -9524,7 +9583,9 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
&outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
|
||||
channel_offset_list[c] * static_cast<size_t>(width)));
|
||||
for (size_t u = 0; u < static_cast<size_t>(width); u++) {
|
||||
unsigned int val = line_ptr[u];
|
||||
unsigned int val;
|
||||
// val = line_ptr[u];
|
||||
tinyexr::cpy4(&val, line_ptr + u);
|
||||
|
||||
tinyexr::swap4(&val);
|
||||
|
||||
@@ -9550,7 +9611,9 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
v * pixel_data_size * static_cast<size_t>(x_stride) +
|
||||
channel_offset_list[c] * static_cast<size_t>(x_stride)));
|
||||
for (size_t u = 0; u < static_cast<size_t>(width); u++) {
|
||||
float val = line_ptr[u];
|
||||
float val;
|
||||
// val = line_ptr[u];
|
||||
tinyexr::cpy4(&val, line_ptr + u);
|
||||
|
||||
tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
|
||||
|
||||
@@ -9612,7 +9675,8 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
for (size_t u = 0; u < static_cast<size_t>(width); u++) {
|
||||
tinyexr::FP16 hf;
|
||||
|
||||
hf.u = line_ptr[u];
|
||||
// hf.u = line_ptr[u];
|
||||
tinyexr::cpy2(&(hf.u), line_ptr + u);
|
||||
|
||||
tinyexr::swap2(reinterpret_cast<unsigned short *>(&hf.u));
|
||||
|
||||
@@ -9655,7 +9719,9 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
&outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
|
||||
channel_offset_list[c] * static_cast<size_t>(width)));
|
||||
for (size_t u = 0; u < static_cast<size_t>(width); u++) {
|
||||
unsigned int val = line_ptr[u];
|
||||
unsigned int val;
|
||||
// val = line_ptr[u];
|
||||
tinyexr::cpy4(&val, line_ptr + u);
|
||||
|
||||
tinyexr::swap4(&val);
|
||||
|
||||
@@ -9681,7 +9747,9 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
&outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
|
||||
channel_offset_list[c] * static_cast<size_t>(width)));
|
||||
for (size_t u = 0; u < static_cast<size_t>(width); u++) {
|
||||
float val = line_ptr[u];
|
||||
float val;
|
||||
// val = line_ptr[u];
|
||||
tinyexr::cpy4(&val, line_ptr + u);
|
||||
|
||||
tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
|
||||
|
||||
@@ -9736,7 +9804,8 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
for (size_t u = 0; u < static_cast<size_t>(width); u++) {
|
||||
tinyexr::FP16 hf;
|
||||
|
||||
hf.u = line_ptr[u];
|
||||
// hf.u = line_ptr[u];
|
||||
tinyexr::cpy2(&(hf.u), line_ptr + u);
|
||||
|
||||
tinyexr::swap2(reinterpret_cast<unsigned short *>(&hf.u));
|
||||
|
||||
@@ -9779,7 +9848,9 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
&outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
|
||||
channel_offset_list[c] * static_cast<size_t>(width)));
|
||||
for (size_t u = 0; u < static_cast<size_t>(width); u++) {
|
||||
unsigned int val = line_ptr[u];
|
||||
unsigned int val;
|
||||
// val = line_ptr[u];
|
||||
tinyexr::cpy4(&val, line_ptr + u);
|
||||
|
||||
tinyexr::swap4(&val);
|
||||
|
||||
@@ -9805,7 +9876,9 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
&outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
|
||||
channel_offset_list[c] * static_cast<size_t>(width)));
|
||||
for (size_t u = 0; u < static_cast<size_t>(width); u++) {
|
||||
float val = line_ptr[u];
|
||||
float val;
|
||||
// val = line_ptr[u];
|
||||
tinyexr::cpy4(&val, line_ptr + u);
|
||||
|
||||
tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
|
||||
|
||||
@@ -9868,7 +9941,8 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
&outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
|
||||
channel_offset_list[c] * static_cast<size_t>(width)));
|
||||
for (size_t u = 0; u < static_cast<size_t>(width); u++) {
|
||||
float val = line_ptr[u];
|
||||
float val;
|
||||
tinyexr::cpy4(&val, line_ptr + u);
|
||||
|
||||
tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
|
||||
|
||||
@@ -9918,7 +9992,8 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
for (int u = 0; u < width; u++) {
|
||||
tinyexr::FP16 hf;
|
||||
|
||||
hf.u = line_ptr[u];
|
||||
// hf.u = line_ptr[u];
|
||||
tinyexr::cpy2(&(hf.u), line_ptr + u);
|
||||
|
||||
tinyexr::swap2(reinterpret_cast<unsigned short *>(&hf.u));
|
||||
|
||||
@@ -9935,7 +10010,9 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
for (int u = 0; u < width; u++) {
|
||||
tinyexr::FP16 hf;
|
||||
|
||||
hf.u = line_ptr[u];
|
||||
// address may not be aliged. use byte-wise copy for safety.#76
|
||||
// hf.u = line_ptr[u];
|
||||
tinyexr::cpy2(&(hf.u), line_ptr + u);
|
||||
|
||||
tinyexr::swap2(reinterpret_cast<unsigned short *>(&hf.u));
|
||||
|
||||
@@ -9959,7 +10036,8 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
}
|
||||
|
||||
for (int u = 0; u < width; u++) {
|
||||
float val = line_ptr[u];
|
||||
float val;
|
||||
tinyexr::cpy4(&val, line_ptr + u);
|
||||
|
||||
tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
|
||||
|
||||
@@ -9977,7 +10055,8 @@ static bool DecodePixelData(/* out */ unsigned char **out_images,
|
||||
}
|
||||
|
||||
for (int u = 0; u < width; u++) {
|
||||
unsigned int val = line_ptr[u];
|
||||
unsigned int val;
|
||||
tinyexr::cpy4(&val, line_ptr + u);
|
||||
|
||||
tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
|
||||
|
||||
@@ -10102,7 +10181,6 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header,
|
||||
}
|
||||
|
||||
if (version->multipart) {
|
||||
|
||||
if (size > 0 && marker[0] == '\0') {
|
||||
// End of header list.
|
||||
if (empty_header) {
|
||||
@@ -10452,17 +10530,20 @@ static void ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info) {
|
||||
exr_header->num_custom_attributes = static_cast<int>(info.attributes.size());
|
||||
|
||||
if (exr_header->num_custom_attributes > 0) {
|
||||
// TODO(syoyo): Report warning when # of attributes exceeds `TINYEXR_MAX_CUSTOM_ATTRIBUTES`
|
||||
// TODO(syoyo): Report warning when # of attributes exceeds
|
||||
// `TINYEXR_MAX_CUSTOM_ATTRIBUTES`
|
||||
if (exr_header->num_custom_attributes > TINYEXR_MAX_CUSTOM_ATTRIBUTES) {
|
||||
exr_header->num_custom_attributes = TINYEXR_MAX_CUSTOM_ATTRIBUTES;
|
||||
}
|
||||
|
||||
exr_header->custom_attributes = static_cast<EXRAttribute *>(
|
||||
malloc(sizeof(EXRAttribute) * size_t(exr_header->num_custom_attributes)));
|
||||
exr_header->custom_attributes = static_cast<EXRAttribute *>(malloc(
|
||||
sizeof(EXRAttribute) * size_t(exr_header->num_custom_attributes)));
|
||||
|
||||
for (size_t i = 0; i < info.attributes.size(); i++) {
|
||||
memcpy(exr_header->custom_attributes[i].name, info.attributes[i].name, 256);
|
||||
memcpy(exr_header->custom_attributes[i].type, info.attributes[i].type, 256);
|
||||
memcpy(exr_header->custom_attributes[i].name, info.attributes[i].name,
|
||||
256);
|
||||
memcpy(exr_header->custom_attributes[i].type, info.attributes[i].type,
|
||||
256);
|
||||
exr_header->custom_attributes[i].size = info.attributes[i].size;
|
||||
// Just copy poiner
|
||||
exr_header->custom_attributes[i].value = info.attributes[i].value;
|
||||
@@ -11472,7 +11553,8 @@ size_t SaveEXRImageToMemory(const EXRImage *exr_image,
|
||||
static_cast<size_t>(pixel_data_size * y * exr_image->width) +
|
||||
channel_offset_list[c] *
|
||||
static_cast<size_t>(exr_image->width)));
|
||||
line_ptr[x] = f32.f;
|
||||
// line_ptr[x] = f32.f;
|
||||
tinyexr::cpy4(line_ptr + x, &(f32.f));
|
||||
}
|
||||
}
|
||||
} else if (exr_header->requested_pixel_types[c] ==
|
||||
@@ -11490,7 +11572,8 @@ size_t SaveEXRImageToMemory(const EXRImage *exr_image,
|
||||
exr_image->width) +
|
||||
channel_offset_list[c] *
|
||||
static_cast<size_t>(exr_image->width)));
|
||||
line_ptr[x] = val;
|
||||
// line_ptr[x] = val;
|
||||
tinyexr::cpy2(line_ptr + x, &val);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -11516,7 +11599,8 @@ size_t SaveEXRImageToMemory(const EXRImage *exr_image,
|
||||
exr_image->width) +
|
||||
channel_offset_list[c] *
|
||||
static_cast<size_t>(exr_image->width)));
|
||||
line_ptr[x] = h16.u;
|
||||
// line_ptr[x] = h16.u;
|
||||
tinyexr::cpy2(line_ptr + x, &(h16.u));
|
||||
}
|
||||
}
|
||||
} else if (exr_header->requested_pixel_types[c] ==
|
||||
@@ -11533,7 +11617,8 @@ size_t SaveEXRImageToMemory(const EXRImage *exr_image,
|
||||
static_cast<size_t>(pixel_data_size * y * exr_image->width) +
|
||||
channel_offset_list[c] *
|
||||
static_cast<size_t>(exr_image->width)));
|
||||
line_ptr[x] = val;
|
||||
// line_ptr[x] = val;
|
||||
tinyexr::cpy4(line_ptr + x, &val);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -11552,7 +11637,8 @@ size_t SaveEXRImageToMemory(const EXRImage *exr_image,
|
||||
static_cast<size_t>(pixel_data_size * y * exr_image->width) +
|
||||
channel_offset_list[c] *
|
||||
static_cast<size_t>(exr_image->width)));
|
||||
line_ptr[x] = val;
|
||||
// line_ptr[x] = val;
|
||||
tinyexr::cpy4(line_ptr + x, &val);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12117,8 +12203,10 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) {
|
||||
|
||||
if (channels[c].pixel_type == 0) { // UINT
|
||||
for (size_t x = 0; x < static_cast<size_t>(samples_per_line); x++) {
|
||||
unsigned int ui = *reinterpret_cast<unsigned int *>(
|
||||
unsigned int ui;
|
||||
unsigned int *src_ptr = reinterpret_cast<unsigned int *>(
|
||||
&sample_data.at(size_t(data_offset) + x * sizeof(int)));
|
||||
tinyexr::cpy4(&ui, src_ptr);
|
||||
deep_image->image[c][y][x] = static_cast<float>(ui); // @fixme
|
||||
}
|
||||
data_offset +=
|
||||
@@ -12126,16 +12214,19 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) {
|
||||
} else if (channels[c].pixel_type == 1) { // half
|
||||
for (size_t x = 0; x < static_cast<size_t>(samples_per_line); x++) {
|
||||
tinyexr::FP16 f16;
|
||||
f16.u = *reinterpret_cast<unsigned short *>(
|
||||
const unsigned short *src_ptr = reinterpret_cast<unsigned short *>(
|
||||
&sample_data.at(size_t(data_offset) + x * sizeof(short)));
|
||||
tinyexr::cpy2(&(f16.u), src_ptr);
|
||||
tinyexr::FP32 f32 = half_to_float(f16);
|
||||
deep_image->image[c][y][x] = f32.f;
|
||||
}
|
||||
data_offset += sizeof(short) * static_cast<size_t>(samples_per_line);
|
||||
} else { // float
|
||||
for (size_t x = 0; x < static_cast<size_t>(samples_per_line); x++) {
|
||||
float f = *reinterpret_cast<float *>(
|
||||
float f;
|
||||
const float *src_ptr = reinterpret_cast<float *>(
|
||||
&sample_data.at(size_t(data_offset) + x * sizeof(float)));
|
||||
tinyexr::cpy4(&f, src_ptr);
|
||||
deep_image->image[c][y][x] = f;
|
||||
}
|
||||
data_offset += sizeof(float) * static_cast<size_t>(samples_per_line);
|
||||
|
||||
Reference in New Issue
Block a user