Hello, World¶
Write an Image¶
This example exrwriter.cpp
program writes a simple image stripes.exr
:
#include <ImfRgbaFile.h> #include <ImfArray.h> #include <iostream> using namespace OPENEXR_IMF_NAMESPACE; int main() { int width = 100; int height = 50; Array2D<Rgba> pixels(height, width); for (int y=0; y<height; y++) { float c = (y / 5 % 2 == 0) ? (y / (float) height) : 0.0; for (int x=0; x<width; x++) pixels[y][x] = Rgba(c, c, c); } try { RgbaOutputFile file ("stripes.exr", width, height, WRITE_RGBA); file.setFrameBuffer (&pixels[0][0], 1, width); file.writePixels (height); } catch (const std::exception &e) { std::cerr << "error writing image file stripes.exr:" << e.what() << std::endl; return 1; } return 0; }
This creates an image 100 pixels wide and 50 pixels high with
horizontal stripes 5 pixels high of graduated intensity, bright on the
bottom of the image and dark towards the top. Note that pixel[0][0]
is in the upper left:
The CMakeLists.txt
file to
build is:
cmake_minimum_required(VERSION 3.12) project(exrwriter) find_package(OpenEXR REQUIRED) add_executable(${PROJECT_NAME} exrwriter.cpp) target_link_libraries(${PROJECT_NAME} OpenEXR::OpenEXR)
To build:
$ mkdir _build $ cmake -S . -B _build -DCMAKE_PREFIX_PATH=<path to OpenEXR libraries/includes> $ cmake --build _build
For more details, see The OpenEXR API.
Read an Image¶
This companion example exrreader.cpp
program reads the stripes.exr
file written by the writer program above:
#include <ImfRgbaFile.h> #include <ImfArray.h> #include <iostream> int main() { try { Imf::RgbaInputFile file("stripes.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 stripes.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} exrreader.cpp) target_link_libraries(${PROJECT_NAME} OpenEXR::OpenEXR)
To build:
$ mkdir _build $ cmake -S . -B _build -DCMAKE_PREFIX_PATH=<path to OpenEXR libraries/includes> $ cmake --build _build