Blame IlmImfExamples/previewImageExamples.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 to add preview images
Packit 0d464f
//	(also known as thumbnais) to OpenEXR image files.
Packit 0d464f
//
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
Packit 0d464f
#include <ImfRgbaFile.h>
Packit 0d464f
#include <ImfArray.h>
Packit 0d464f
#include <ImfPreviewImage.h>
Packit 0d464f
#include "ImathFun.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
unsigned char
Packit 0d464f
gamma (float x)
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Convert a floating-point pixel value to an 8-bit gamma-2.2
Packit 0d464f
    // preview pixel.  (This routine is a simplified version of
Packit 0d464f
    // how the exrdisplay program transforms floating-point pixel
Packit 0d464f
    // values in order to display them on the screen.)
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    x = pow (5.5555f * max (0.f, x), 0.4545f) * 84.66f;
Packit 0d464f
    return (unsigned char) clamp (x, 0.f, 255.f);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
makePreviewImage (const Array2D <Rgba> &pixels,
Packit 0d464f
		  int width,
Packit 0d464f
		  int height,
Packit 0d464f
		  Array2D <PreviewRgba> &previewPixels,
Packit 0d464f
		  int &previewWidth,
Packit 0d464f
		  int &previewHeight)
Packit 0d464f
{
Packit 0d464f
    const int N = 8;
Packit 0d464f
Packit 0d464f
    previewWidth  = width / N;
Packit 0d464f
    previewHeight = height / N;
Packit 0d464f
    previewPixels.resizeErase (previewHeight, previewWidth);
Packit 0d464f
Packit 0d464f
    for (int y = 0; y < previewHeight; ++y)
Packit 0d464f
    {
Packit 0d464f
	for (int x = 0; x < previewWidth; ++x)
Packit 0d464f
	{
Packit 0d464f
	    const Rgba  &inPixel = pixels[y * N][x * N];
Packit 0d464f
	    PreviewRgba &outPixel = previewPixels[y][x];
Packit 0d464f
Packit 0d464f
	    outPixel.r = gamma (inPixel.r);
Packit 0d464f
	    outPixel.g = gamma (inPixel.g);
Packit 0d464f
	    outPixel.b = gamma (inPixel.b);
Packit 0d464f
	    outPixel.a = int (clamp (inPixel.a * 255.f, 0.f, 255.f) + 0.5f);
Packit 0d464f
	}
Packit 0d464f
    }
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
writeRgbaWithPreview1 (const char fileName[],
Packit 0d464f
		       const Array2D <Rgba> &pixels,
Packit 0d464f
		       int width,
Packit 0d464f
		       int height)
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Write an image file with a preview image, version 1:
Packit 0d464f
    //
Packit 0d464f
    // - generate the preview image by subsampling the main image
Packit 0d464f
    // - generate a file header
Packit 0d464f
    // - add the preview image to the file header
Packit 0d464f
    // - open the file (this stores the header with the
Packit 0d464f
    //   preview image in the file)
Packit 0d464f
    // - describe the memory layout of the main image's pixels
Packit 0d464f
    // - store the main image's pixels in the file
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    Array2D <PreviewRgba> previewPixels;
Packit 0d464f
    int previewWidth;
Packit 0d464f
    int previewHeight;
Packit 0d464f
Packit 0d464f
    makePreviewImage (pixels, width, height,
Packit 0d464f
		      previewPixels, previewWidth, previewHeight);
Packit 0d464f
Packit 0d464f
    Header header (width, height);
Packit 0d464f
Packit 0d464f
    header.setPreviewImage
Packit 0d464f
	(PreviewImage (previewWidth, previewHeight, &previewPixels[0][0]));
Packit 0d464f
Packit 0d464f
    RgbaOutputFile file (fileName, header, WRITE_RGBA);
Packit 0d464f
    file.setFrameBuffer (&pixels[0][0], 1, width);
Packit 0d464f
    file.writePixels (height);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
writeRgbaWithPreview2 (const char fileName[],
Packit 0d464f
		       int width,
Packit 0d464f
		       int height)
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Write an image file with a preview image, version 2:
Packit 0d464f
    //
Packit 0d464f
    // - generate a file header
Packit 0d464f
    // - add a dummy preview image to the file header
Packit 0d464f
    // - open the file (this stores the header with the dummy
Packit 0d464f
    //   preview image in the file)
Packit 0d464f
    // - render the main image's pixels one scan line at a time,
Packit 0d464f
    //   and store each scan line in the file before rendering
Packit 0d464f
    //   the next scan line
Packit 0d464f
    // - generate the preview image on the fly, while the main
Packit 0d464f
    //   image is being rendered
Packit 0d464f
    // - once the main image has been rendered, store the preview
Packit 0d464f
    //   image in the file, overwriting the dummy preview
Packit 0d464f
    //   
Packit 0d464f
Packit 0d464f
    Array <Rgba> pixels (width);
Packit 0d464f
Packit 0d464f
    const int N = 8;
Packit 0d464f
Packit 0d464f
    int previewWidth = width / N;
Packit 0d464f
    int previewHeight = height / N;
Packit 0d464f
    Array2D <PreviewRgba> previewPixels (previewHeight, previewWidth);
Packit 0d464f
Packit 0d464f
    Header header (width, height);
Packit 0d464f
    header.setPreviewImage (PreviewImage (previewWidth, previewHeight));
Packit 0d464f
Packit 0d464f
    RgbaOutputFile file (fileName, header, WRITE_RGBA);
Packit 0d464f
    file.setFrameBuffer (pixels, 1, 0);
Packit 0d464f
Packit 0d464f
    for (int y = 0; y < height; ++y)
Packit 0d464f
    {
Packit 0d464f
	drawImage7 (pixels, width, height, y);
Packit 0d464f
	file.writePixels (1);
Packit 0d464f
Packit 0d464f
	if (y % N == 0)
Packit 0d464f
	{
Packit 0d464f
	    for (int x = 0; x < width; x += N)
Packit 0d464f
	    {
Packit 0d464f
		const Rgba  &inPixel = pixels[x];
Packit 0d464f
		PreviewRgba &outPixel = previewPixels[y / N][x / N];
Packit 0d464f
Packit 0d464f
		outPixel.r = gamma (inPixel.r);
Packit 0d464f
		outPixel.g = gamma (inPixel.g);
Packit 0d464f
		outPixel.b = gamma (inPixel.b);
Packit 0d464f
		outPixel.a = int (clamp (inPixel.a * 255.f, 0.f, 255.f) + 0.5f);
Packit 0d464f
	    }
Packit 0d464f
	}
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    file.updatePreviewImage (&previewPixels[0][0]);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
previewImageExamples ()
Packit 0d464f
{
Packit 0d464f
    cout << "\nfiles with preview images\n" << endl;
Packit 0d464f
Packit 0d464f
    cout << "drawing image then writing file" << 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
    writeRgbaWithPreview1 ("rgbaWithPreview1.exr", p, w, h);
Packit 0d464f
Packit 0d464f
    cout << "drawing image while writing file" << endl;
Packit 0d464f
Packit 0d464f
    writeRgbaWithPreview2 ("rgbaWithPreview2.exr", w, h);
Packit 0d464f
Packit 0d464f
    cout << endl;
Packit 0d464f
}