[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Openexr-devel] writing an exr
From: |
Florian Kainz |
Subject: |
Re: [Openexr-devel] writing an exr |
Date: |
Thu, 23 Jan 2003 17:35:54 -0800 |
Kevin Smith wrote:
>
> Can anyone share some boilerplater code for writing an exr image? Something
> as simple as writing out a 512x512, middle grey (0.18) .exr would be great.
>
Hi Kevin,
below are two versions of a function that creates an image
and stores it in an OpenEXR file. One version first creates
the whole image in a frame buffer and then saves it. The
other version creates the image one scanline at a time, and
saves each scanline as soon as possible.
The OpenEXR interface allows many other variations: instead
of containing the whole image, or only a single scanline, the
frame buffer might contain a small number of scanlines, its
layout in memory may be non-contiguous, etc.
hope this helps,
Florian
#include <ImfRgbaFile.h>
#include <ImfArray.h>
using namespace Imf;
using namespace std;
void
writeImageFullFrameBuffer (const char fileName[], int width, int height)
{
// Create a frame buffer, and fill it with some pattern
Array2D<Rgba> pixels (height, width);
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x)
{
Rgba &p = pixels[y][x];
p.r = (y % 5) * 0.2;
p.g = (x % 5) * 0.2;
p.b = 0.18;
}
// Store the frame buffer's contents in an OpenEXR file
Header header (width, height);
RgbaOutputFile out (fileName, header, WRITE_RGB);
out.setFrameBuffer (&pixels[0][0], 1, width);
out.writePixels (height);
}
void
writeImageSingleScanLine (const char fileName[], int width, int height)
{
// Create a frame buffer for a single scanline
Array<Rgba> pixels (width);
// Generate an image, one scanline at a time,
// and store it in the OpenEXR file.
Header header (width, height);
RgbaOutputFile out (fileName, header, WRITE_RGB);
out.setFrameBuffer (&pixels[0], 1, 0);
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
Rgba &p = pixels[x];
p.r = (y % 5) * 0.2;
p.g = (x % 5) * 0.2;
p.b = 0.18;
}
out.writePixels (1);
}
}
int
main ()
{
try
{
writeImageFullFrameBuffer ("test1.exr", 512, 512);
writeImageSingleScanLine ("test2.exr", 512, 512);
}
catch (const std::exception &e)
{
cerr << e.what() << endl;
}
return 0;
}