Skip to content

Commit

Permalink
Using chromaticities if provided #5
Browse files Browse the repository at this point in the history
  • Loading branch information
afichet committed May 27, 2021
1 parent 18c0806 commit f287e01
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/openexr-thumbnailer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include <OpenEXR/ImfRgbaFile.h>
#include <OpenEXR/ImfArray.h>
#include <OpenEXR/ImfHeader.h>
#include <OpenEXR/ImfChromaticitiesAttribute.h>

#include <cmath>

double to_sRGB(double rgb_color)
Expand All @@ -31,27 +34,44 @@ unsigned char *load_exr(const char *path, int *width, int *height)
Imf::Array2D<Imf::Rgba> pixels;

Imf::RgbaInputFile f(path);
Imath::Box2i dw1 = f.dataWindow();
int w = dw1.max.x - dw1.min.x + 1;
int h = dw1.max.y - dw1.min.y + 1;
*width = w;
*height = h;
Imath::Box2i dw = f.dataWindow();
int w = dw.max.x - dw.min.x + 1;
int h = dw.max.y - dw.min.y + 1;
*width = w;
*height = h;

pixels.resizeErase(h, w);
f.setFrameBuffer(&pixels[0][0] - dw1.min.x - dw1.min.y * w, 1, w);
f.readPixels(dw1.min.y, dw1.max.y);
f.setFrameBuffer(&pixels[0][0] - dw.min.x - dw.min.y * w, 1, w);
f.readPixels(dw.min.y, dw.max.y);

// Check if there is specific chromaticities tied to the color representation in this part.
const Imf::ChromaticitiesAttribute *c = f.header().findTypedAttribute<Imf::ChromaticitiesAttribute>("chromaticities");

Imf::Chromaticities chromaticities;

if (c != nullptr) {
chromaticities = c->value();
}

// Handle custom chromaticities
Imath::M44f RGB_XYZ = Imf::RGBtoXYZ(chromaticities, 1.f);
Imath::M44f XYZ_RGB = Imf::XYZtoRGB(Imf::Chromaticities(), 1.f);

Imath::M44f conversionMatrix = RGB_XYZ * XYZ_RGB;

unsigned char *img = new unsigned char[3 * w * h];
//unsigned char* img = calloc(sizeof(unsigned char), 3*w*h);

for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
Imath::V3f rgb(pixels[y][x].r, pixels[y][x].g, pixels[y][x].b);
rgb *= conversionMatrix;

img[3*(y*w + x) + 0] =
(unsigned char)(255.0*std::min(1.0, std::max(0.0, to_sRGB(pixels[y][x].r))));
(unsigned char)(255.0*std::min(1.0, std::max(0.0, to_sRGB(rgb.x))));
img[3*(y*w + x) + 1] =
(unsigned char)(255.0*std::min(1.0, std::max(0.0, to_sRGB(pixels[y][x].g))));
(unsigned char)(255.0*std::min(1.0, std::max(0.0, to_sRGB(rgb.y))));
img[3*(y*w + x) + 2] =
(unsigned char)(255.0*std::min(1.0, std::max(0.0, to_sRGB(pixels[y][x].b))));
(unsigned char)(255.0*std::min(1.0, std::max(0.0, to_sRGB(rgb.z))));
}
}

Expand Down

0 comments on commit f287e01

Please sign in to comment.