Blame IlmImfExamples/rgbaInterfaceExamples.cpp

Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
//
Packit 0d464f
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
Packit 0d464f
// Digital Ltd. LLC
Packit 0d464f
// 
Packit 0d464f
// All rights reserved.
Packit 0d464f
// 
Packit 0d464f
// Redistribution and use in source and binary forms, with or without
Packit 0d464f
// modification, are permitted provided that the following conditions are
Packit 0d464f
// met:
Packit 0d464f
// *       Redistributions of source code must retain the above copyright
Packit 0d464f
// notice, this list of conditions and the following disclaimer.
Packit 0d464f
// *       Redistributions in binary form must reproduce the above
Packit 0d464f
// copyright notice, this list of conditions and the following disclaimer
Packit 0d464f
// in the documentation and/or other materials provided with the
Packit 0d464f
// distribution.
Packit 0d464f
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 0d464f
// its contributors may be used to endorse or promote products derived
Packit 0d464f
// from this software without specific prior written permission. 
Packit 0d464f
// 
Packit 0d464f
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 0d464f
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 0d464f
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 0d464f
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 0d464f
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 0d464f
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 0d464f
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 0d464f
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 0d464f
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 0d464f
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 0d464f
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 0d464f
//
Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
Packit 0d464f
Packit 0d464f
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
//
Packit 0d464f
//	Code examples that show how class RgbaInputFile and
Packit 0d464f
//	class RgbaOutputFile can be used to read and write
Packit 0d464f
//	OpenEXR image files with 16-bit floating-point red,
Packit 0d464f
//	green, blue and alpha channels.
Packit 0d464f
//
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
Packit 0d464f
#include <ImfRgbaFile.h>
Packit 0d464f
#include <ImfStringAttribute.h>
Packit 0d464f
#include <ImfMatrixAttribute.h>
Packit 0d464f
#include <ImfArray.h>
Packit 0d464f
Packit 0d464f
#include "drawImage.h"
Packit 0d464f
Packit 0d464f
#include <iostream>
Packit 0d464f
#include <algorithm>
Packit 0d464f
Packit 0d464f
#include "namespaceAlias.h"
Packit 0d464f
using namespace IMF;
Packit 0d464f
using namespace std;
Packit 0d464f
using namespace IMATH_NAMESPACE;
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
writeRgba1 (const char fileName[],
Packit 0d464f
	    const Rgba *pixels,
Packit 0d464f
	    int width,
Packit 0d464f
	    int height)
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Write an RGBA image using class RgbaOutputFile.
Packit 0d464f
    //
Packit 0d464f
    //	- open the file
Packit 0d464f
    //	- describe the memory layout of the pixels
Packit 0d464f
    //	- store the pixels in the file
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
Packit 0d464f
    RgbaOutputFile file (fileName, width, height, WRITE_RGBA);
Packit 0d464f
    file.setFrameBuffer (pixels, 1, width);
Packit 0d464f
    file.writePixels (height);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
writeRgba2 (const char fileName[],
Packit 0d464f
	    const Rgba *pixels,
Packit 0d464f
	    int width,
Packit 0d464f
	    int height,
Packit 0d464f
	    const Box2i &dataWindow)
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Write an RGBA image using class RgbaOutputFile.
Packit 0d464f
    // Don't store the whole image in the file, but
Packit 0d464f
    // crop it according to the given data window.
Packit 0d464f
    //
Packit 0d464f
    //	- open the file
Packit 0d464f
    //	- describe the memory layout of the pixels
Packit 0d464f
    //	- store the pixels in the file
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    Box2i displayWindow (V2i (0, 0), V2i (width - 1, height - 1));
Packit 0d464f
    RgbaOutputFile file (fileName, displayWindow, dataWindow, WRITE_RGBA);
Packit 0d464f
    file.setFrameBuffer (pixels, 1, width);
Packit 0d464f
    file.writePixels (dataWindow.max.y - dataWindow.min.y + 1);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
writeRgba3 (const char fileName[],
Packit 0d464f
	    const Rgba *pixels,
Packit 0d464f
	    int width,
Packit 0d464f
	    int height,
Packit 0d464f
	    const char comments[],
Packit 0d464f
	    const M44f &cameraTransform)
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Write an RGBA image using class RgbaOutputFile.
Packit 0d464f
    // Store two extra attributes in the image header:
Packit 0d464f
    // a string and a 4x4 transformation matrix.
Packit 0d464f
    //
Packit 0d464f
    //	- open the file
Packit 0d464f
    //	- describe the memory layout of the pixels
Packit 0d464f
    //	- store the pixels in the file
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    Header header (width, height);
Packit 0d464f
    header.insert ("comments", StringAttribute (comments));
Packit 0d464f
    header.insert ("cameraTransform", M44fAttribute (cameraTransform));
Packit 0d464f
Packit 0d464f
    RgbaOutputFile file (fileName, header, WRITE_RGBA);
Packit 0d464f
    file.setFrameBuffer (pixels, 1, width);
Packit 0d464f
    file.writePixels (height);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
readRgba1 (const char fileName[],
Packit 0d464f
	   Array2D<Rgba> &pixels,
Packit 0d464f
	   int &width,
Packit 0d464f
	   int &height)
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Read an RGBA image using class RgbaInputFile:
Packit 0d464f
    //
Packit 0d464f
    //	- open the file
Packit 0d464f
    //	- allocate memory for the pixels
Packit 0d464f
    //	- describe the memory layout of the pixels
Packit 0d464f
    //	- read the pixels from the file
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    RgbaInputFile file (fileName);
Packit 0d464f
    Box2i dw = file.dataWindow();
Packit 0d464f
Packit 0d464f
    width  = dw.max.x - dw.min.x + 1;
Packit 0d464f
    height = dw.max.y - dw.min.y + 1;
Packit 0d464f
    pixels.resizeErase (height, width);
Packit 0d464f
Packit 0d464f
    file.setFrameBuffer (&pixels[0][0] - dw.min.x - dw.min.y * width, 1, width);
Packit 0d464f
    file.readPixels (dw.min.y, dw.max.y);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
readRgba2 (const char fileName[])
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Read an RGBA image using class RgbaInputFile.
Packit 0d464f
    // Read the pixels, 10 scan lines at a time, and
Packit 0d464f
    // store the pixel data in a buffer that is just
Packit 0d464f
    // large enough to hold 10 scan lines worth of data.
Packit 0d464f
    //
Packit 0d464f
    //	- open the file
Packit 0d464f
    //	- allocate memory for the pixels
Packit 0d464f
    //	- for each block of 10 scan lines,
Packit 0d464f
    //	  describe the memory layout of the pixels,
Packit 0d464f
    //	  read the pixels from the file,
Packit 0d464f
    //	  process the pixels and discard them
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    RgbaInputFile file (fileName);
Packit 0d464f
    Box2i dw = file.dataWindow();
Packit 0d464f
Packit 0d464f
    int width  = dw.max.x - dw.min.x + 1;
Packit 0d464f
    int height = dw.max.y - dw.min.y + 1;
Packit 0d464f
    Array2D<Rgba> pixels (10, width);
Packit 0d464f
Packit 0d464f
    while (dw.min.y <= dw.max.y)
Packit 0d464f
    {
Packit 0d464f
	file.setFrameBuffer (&pixels[0][0] - dw.min.x - dw.min.y * width,
Packit 0d464f
			     1, width);
Packit 0d464f
Packit 0d464f
	file.readPixels (dw.min.y, min (dw.min.y + 9, dw.max.y));
Packit 0d464f
	// processPixels (pixels)
Packit 0d464f
	
Packit 0d464f
	dw.min.y += 10;
Packit 0d464f
    }
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
readHeader (const char fileName[])
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Read an image's header from a file, and if the header
Packit 0d464f
    // contains comments and camera transformation attributes,
Packit 0d464f
    // print the values of those attributes.
Packit 0d464f
    //
Packit 0d464f
    //	- open the file
Packit 0d464f
    //	- get the file header
Packit 0d464f
    //	- look for the attributes
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    RgbaInputFile file (fileName);
Packit 0d464f
Packit 0d464f
    const StringAttribute *comments =
Packit 0d464f
	file.header().findTypedAttribute <StringAttribute> ("comments");
Packit 0d464f
Packit 0d464f
    const M44fAttribute *cameraTransform = 
Packit 0d464f
	file.header().findTypedAttribute <M44fAttribute> ("cameraTransform");
Packit 0d464f
Packit 0d464f
    if (comments)
Packit 0d464f
	cout << "comments\n   " << comments->value() << endl;
Packit 0d464f
Packit 0d464f
    if (cameraTransform)
Packit 0d464f
	cout << "cameraTransform\n" << cameraTransform->value() << flush;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
rgbaInterfaceExamples ()
Packit 0d464f
{
Packit 0d464f
    cout << "\nRGBA images\n" << endl;
Packit 0d464f
    cout << "drawing image" << endl;
Packit 0d464f
Packit 0d464f
    int w = 800;
Packit 0d464f
    int h = 600;
Packit 0d464f
Packit 0d464f
    Array2D<Rgba> p (h, w);
Packit 0d464f
    drawImage1 (p, w, h);
Packit 0d464f
Packit 0d464f
    cout << "writing entire image" << endl;
Packit 0d464f
Packit 0d464f
    writeRgba1 ("rgba1.exr", &p[0][0], w, h);
Packit 0d464f
Packit 0d464f
    cout << "writing cropped image" << endl;
Packit 0d464f
Packit 0d464f
    writeRgba2 ("rgba2.exr", &p[0][0], w, h,
Packit 0d464f
	        Box2i (V2i (w/6, h/6), V2i (w/2, h/2)));
Packit 0d464f
Packit 0d464f
    cout << "writing image with extra header attributes" << endl;
Packit 0d464f
Packit 0d464f
    writeRgba3 ("rgba3.exr", &p[0][0], w, h,
Packit 0d464f
	        "may contain peanuts", M44f());
Packit 0d464f
Packit 0d464f
    cout << "reading rgba file" << endl;
Packit 0d464f
Packit 0d464f
    readRgba1 ("rgba2.exr", p, w, h);
Packit 0d464f
Packit 0d464f
    cout << "reading rgba file into 10-scanline buffer" << endl;
Packit 0d464f
Packit 0d464f
    readRgba2 ("rgba2.exr");
Packit 0d464f
Packit 0d464f
    cout << "reading extra file header attributes" << endl;
Packit 0d464f
Packit 0d464f
    readHeader ("rgba3.exr");
Packit 0d464f
}