Skip to content

Commit

Permalink
[feature] Read and write the exr data window
Browse files Browse the repository at this point in the history
Denoise only the data window (it was the case).

Check that the histogram data window is the same than
the input data window.

Write the exr with the correct data and display windows.
  • Loading branch information
hulud75 authored and Benjamin Legros committed Nov 25, 2015
1 parent 5b2def2 commit 49aee18
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 31 deletions.
64 changes: 37 additions & 27 deletions io_exr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct sRGB
};


float *ReadImageEXR(const char fileName[], int *nx, int *ny, float *&alpha)
float *ReadImageEXR(const char fileName[], Box2i &dataWindow, Box2i &displayWindow, float *&alpha)
{


Expand All @@ -54,14 +54,12 @@ float *ReadImageEXR(const char fileName[], int *nx, int *ny, float *&alpha)
InputFile file (fileName);

// Get image dimensions.
Box2i dw = file.header().dataWindow();
dataWindow = file.header().dataWindow();
displayWindow = file.header().displayWindow();

int width = dw.max.x - dw.min.x + 1;
int height = dw.max.y - dw.min.y + 1;
int width = dataWindow.max.x - dataWindow.min.x + 1;
int height = dataWindow.max.y - dataWindow.min.y + 1;

*nx = width;
*ny = height;

const bool hasAlpha = file.header().channels().findChannel("A") != NULL;

// Allocate memory to read image bits. We will only try to read R, G and B
Expand All @@ -77,26 +75,26 @@ float *ReadImageEXR(const char fileName[], int *nx, int *ny, float *&alpha)
FrameBuffer frameBuffer;

frameBuffer.insert("R", Slice(FLOAT, (char*)(&pixelsR[0] -
dw.min.x - dw.min.y*width),
dataWindow.min.x - dataWindow.min.y*width),
sizeof(float),
width * sizeof(float),
1, 1, // x/y sampling
0.0));
frameBuffer.insert("G", Slice(FLOAT, (char*)(&pixelsG[0] -
dw.min.x -dw.min.y*width),
dataWindow.min.x -dataWindow.min.y*width),
sizeof(float),
width * sizeof(float),
1, 1, // x/y sampling
0.0));
frameBuffer.insert("B", Slice(FLOAT, (char*)(&pixelsB[0] -
dw.min.x -dw.min.y*width),
dataWindow.min.x -dataWindow.min.y*width),
sizeof(float),
width * sizeof(float),
1, 1, // x/y sampling
0.0));
if (alpha)
frameBuffer.insert("A", Slice(FLOAT, (char*)(alpha -
dw.min.x -dw.min.y*width),
dataWindow.min.x -dataWindow.min.y*width),
sizeof(float),
width * sizeof(float),
1, 1, // x/y sampling
Expand All @@ -109,7 +107,7 @@ float *ReadImageEXR(const char fileName[], int *nx, int *ny, float *&alpha)


try {
file.readPixels (dw.min.y, dw.max.y);
file.readPixels (dataWindow.min.y, dataWindow.max.y);

}
catch (const std::exception &) {
Expand Down Expand Up @@ -139,17 +137,24 @@ float *ReadImageEXR(const char fileName[], int *nx, int *ny, float *&alpha)

}


float *ReadImageEXR(const char fileName[], int *nx, int *ny)
{
Box2i dataWindow, displayWindow;
float *alpha;
float *rgb = ReadImageEXR (fileName, dataWindow, displayWindow, alpha);
if (alpha)
delete[] alpha;
*nx = dataWindow.max.x - dataWindow.min.x + 1;
*ny = dataWindow.max.y - dataWindow.min.y + 1;
return rgb;
}

void WriteImageEXR(const char *name, float *pixels, float *alpha,
int xRes, int yRes, int channelStride)
const Imath::Box2i &dataWindow,
const Imath::Box2i &displayWindow, int channelStride)
{

//this can be a parameter to gerneralize the function
int xOffset = 0;
int yOffset = 0;
int totalXRes = xRes;
int totalYRes = yRes;
const int xRes = dataWindow.max.x - dataWindow.min.x + 1;
const int yRes = dataWindow.max.y - dataWindow.min.y + 1;

Rgba *hrgba = new Rgba[xRes * yRes];
for (int i = 0; i < xRes * yRes; ++i)
Expand All @@ -160,13 +165,9 @@ void WriteImageEXR(const char *name, float *pixels, float *alpha,

}

Box2i displayWindow(V2i(0,0), V2i(totalXRes-1, totalYRes-1));
Box2i dataWindow(V2i(xOffset, yOffset),
V2i(xOffset + xRes - 1, yOffset + yRes - 1));

try {
RgbaOutputFile file(name, displayWindow, dataWindow, WRITE_RGBA);
file.setFrameBuffer(hrgba - xOffset - yOffset * xRes, 1, xRes);
file.setFrameBuffer(hrgba - dataWindow.min.x - dataWindow.min.y * xRes, 1, xRes);
file.writePixels(yRes);
}
catch (const std::exception &e) {
Expand Down Expand Up @@ -215,6 +216,15 @@ void WriteImageEXR(const char *name, float **pixels,
delete[] hrgba;
}

void WriteImageEXR(const char *name, float *pixels, int nx, int ny)
{
Imath::Box2i dw;
dw.min.x = dw.min.y = 0;
dw.max.x = nx-1;
dw.max.y = ny-1;
WriteImageEXR(name, pixels, NULL, dw, dw, nx*ny);
}

void writeMultiImageEXR (const char *fileName,
float *zPixels,
int width,
Expand Down Expand Up @@ -299,7 +309,6 @@ void writeMultiImageEXR (const char *fileName,

}


float * readMultiImageEXR(const char fileName[],
int *width, int *height, int *nbins)
{
Expand Down Expand Up @@ -345,7 +354,8 @@ float * readMultiImageEXR(const char fileName[],

frameBuffer.insert(ch_name,
Slice(FLOAT,
(char*)(&data[i*nhnc]),
(char*)(&data[i*nhnc] -
dw.min.x -dw.min.y**width),
sizeof(float),
(*width) * sizeof(float)));

Expand Down
12 changes: 10 additions & 2 deletions io_exr.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@
#ifndef IO_EXR_H_
#define IO_EXR_H_

#include <ImathBox.h>

float *ReadImageEXR(const char fileName[], int *nx, int *ny, float *&alpha);
float *ReadImageEXR(const char fileName[], int *nx,
int *ny);

float *ReadImageEXR(const char fileName[], Imath::Box2i &dataWindow,
Imath::Box2i &displayWindow, float *&alpha);

void WriteImageEXR(const char *name, float *pixels, int nx, int ny);

void WriteImageEXR(const char *name, float *pixels, float *alpha,
int xRes, int yRes, int channelStride);
const Imath::Box2i &dataWindow,
const Imath::Box2i &displayWindow, int channelStride);

void WriteImageEXR(const char *name, float **pixels,
int xRes, int yRes);
Expand Down
13 changes: 11 additions & 2 deletions rhf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,10 @@ int main(int argc, char **argv) {
float *d_v = NULL;

float *alpha = NULL;
d_v = ReadImageEXR(param.input, &nx, &ny, alpha);
Imath::Box2i dataWindow, displayWindow;
d_v = ReadImageEXR(param.input, dataWindow, displayWindow, alpha);
nx = dataWindow.max.x - dataWindow.min.x + 1;
ny = dataWindow.max.y - dataWindow.min.y + 1;

// Divide colors by alpha, could be controlled by a new parameter
if (alpha)
Expand Down Expand Up @@ -363,6 +366,12 @@ int main(int argc, char **argv) {
fpH = readMultiImageEXR(param.hist_file,
&nx_h, &ny_h, &nc_h);

if (dataWindow.max.x - dataWindow.min.x + 1 != nx_h ||
dataWindow.max.y - dataWindow.min.y + 1 != ny_h)
{
error ("The histogram file and the input file don't have the same data window.");
exit (-1);
}

float **fpHisto = new float*[nc_h];
for (int ii=0; ii < nc_h; ii++)
Expand Down Expand Up @@ -392,7 +401,7 @@ int main(int argc, char **argv) {

// save EXR denoised image
const int channelStride = d_c == 1 ? 0 : d_w*d_h;
WriteImageEXR(param.output, denoised, alpha, d_w, d_h, channelStride);
WriteImageEXR(param.output, denoised, alpha, dataWindow, displayWindow, channelStride);

delete[] fpHisto;
delete[] fpH;
Expand Down

0 comments on commit 49aee18

Please sign in to comment.