Hello, World¶
A simple program to write a simple .exr
file of an image of 10x10
pixels with values that are a ramp in green and blue:
#include <ImfRgbaFile.h>
#include <ImfArray.h>
#include <iostream>
int
main()
{
int width = 10;
int height = 10;
Imf::Array2D<Imf::Rgba> pixels(width, height);
for (int y=0; y<height; y++)
for (int x=0; x<width; x++)
pixels[y][x] = Imf::Rgba(0, x / (width-1.0f), y / (height-1.0f));
try {
Imf::RgbaOutputFile file ("hello.exr", width, height, Imf::WRITE_RGBA);
file.setFrameBuffer (&pixels[0][0], 1, width);
file.writePixels (height);
} catch (const std::exception &e) {
std::cerr << "error writing image file hello.exr:" << e.what() << std::endl;
return 1;
}
return 0;
}
And the CMakeLists.txt
file to build:
cmake_minimum_required(VERSION 3.12)
project(exrwriter)
find_package(OpenEXR REQUIRED)
add_executable(${PROJECT_NAME} writer.cpp)
target_link_libraries(${PROJECT_NAME} OpenEXR::OpenEXR)
To build:
$ mkdir _build
$ cmake -S . -B _build
$ cmake --build _build
For more details, see The OpenEXR API.
And a simple program to read an .exr
file:
#include <ImfRgbaFile.h>
#include <ImfArray.h>
#include <iostream>
int
main()
{
try {
Imf::RgbaInputFile file("hello.exr");
Imath::Box2i dw = file.dataWindow();
int width = dw.max.x - dw.min.x + 1;
int height = dw.max.y - dw.min.y + 1;
Imf::Array2D<Imf::Rgba> pixels(width, height);
file.setFrameBuffer(&pixels[0][0], 1, width);
file.readPixels(dw.min.y, dw.max.y);
} catch (const std::exception &e) {
std::cerr << "error reading image file hello.exr:" << e.what() << std::endl;
return 1;
}
return 0;
}
And the CMakeLists.txt
file to build:
cmake_minimum_required(VERSION 3.12)
project(exrreader)
find_package(OpenEXR REQUIRED)
add_executable(${PROJECT_NAME} reader.cpp)
target_link_libraries(${PROJECT_NAME} OpenEXR::OpenEXR)
To build:
$ mkdir _build
$ cmake -S . -B _build
$ cmake --build _build