Skip to content

Instantly share code, notes, and snippets.

@anderslanglands
Created June 28, 2021 04:58
Show Gist options
  • Save anderslanglands/48998f275e01f594a57be0c2bacaa65d to your computer and use it in GitHub Desktop.
Save anderslanglands/48998f275e01f594a57be0c2bacaa65d to your computer and use it in GitHub Desktop.
write_deep_tiled
cmake_minimum_required(VERSION 3.5)
project(write_deep_tiled)
find_package(Imath 3.0.1)
find_package(OpenEXR 3.0.1)
add_executable(write_deep_tiled main.cpp)
target_link_libraries(write_deep_tiled OpenEXR::OpenEXR Imath::Imath)
#include <Imath/ImathBox.h>
#include <OpenEXR/ImfChannelList.h>
#include <OpenEXR/ImfDeepFrameBuffer.h>
#include <OpenEXR/ImfDeepTiledOutputFile.h>
#include <OpenEXR/ImfHeader.h>
#include <cstdint>
#include <vector>
using Imath::Box2i;
using Imath::half;
using Imath::V2i;
int main() {
int width = 256;
int height = 256;
int num_pixels = width * height;
Box2i display_window(V2i(0, 0), V2i(width - 1, height - 1));
Box2i data_window = display_window;
Imf::Header header(display_window, data_window);
header.setTileDescription(Imf::TileDescription(64, 64, Imf::ONE_LEVEL));
header.compression() = Imf::NO_COMPRESSION;
header.channels().insert("Z", Imf::Channel(Imf::FLOAT));
header.channels().insert("A", Imf::Channel(Imf::HALF));
Imf::DeepTiledOutputFile file("wdtof_cpp.exr", header);
std::vector<float*> z_data;
std::vector<half*> a_data;
for (int y = 0; y < height; ++y) {
float v = float(y) / float(height) * 8.0f;
for (int x = 0; x < width; ++x) {
float u = float(x) / float(width) * 8.0f;
z_data.push_back(new float((sin(u) * sin(v)) + 1));
a_data.push_back(new half(1.0f));
}
}
std::vector<uint32_t> sample_counts;
sample_counts.reserve(num_pixels);
for (int i = 0; i < num_pixels; ++i) {
sample_counts.push_back(1);
}
Imf::DeepFrameBuffer frame_buffer;
frame_buffer.insertSampleCountSlice(
Imf::Slice(Imf::UINT, (char*)sample_counts.data(), sizeof(uint32_t),
sizeof(uint32_t) * width));
frame_buffer.insert(
"Z", Imf::DeepSlice(Imf::FLOAT, (char*)z_data.data(), sizeof(float*),
sizeof(float*) * width, sizeof(float)));
frame_buffer.insert(
"A", Imf::DeepSlice(Imf::HALF, (char*)a_data.data(), sizeof(half*),
sizeof(half*) * width, sizeof(half)));
file.setFrameBuffer(frame_buffer);
file.writeTiles(0, file.numXTiles() - 1, 0, file.numYTiles() - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment