Blame IlmImfTest/testCopyPixels.cpp

Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
//
Packit 0d464f
// Copyright (c) 2002-2012, 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
#include <ImfOutputFile.h>
Packit 0d464f
#include <ImfInputFile.h>
Packit 0d464f
#include <ImfChannelList.h>
Packit 0d464f
#include <ImfArray.h>
Packit 0d464f
#include <half.h>
Packit 0d464f
Packit 0d464f
#include <stdio.h>
Packit 0d464f
#include <assert.h>
Packit 0d464f
Packit 0d464f
Packit 0d464f
using namespace OPENEXR_IMF_NAMESPACE;
Packit 0d464f
using namespace std;
Packit 0d464f
using namespace IMATH_NAMESPACE;
Packit 0d464f
Packit 0d464f
Packit 0d464f
namespace {
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fillPixels (Array2D<half> &ph, int width, int height)
Packit 0d464f
{
Packit 0d464f
    for (int y = 0; y < height; ++y)
Packit 0d464f
	for (int x = 0; x < width; ++x)
Packit 0d464f
	    ph[y][x] = sin (double (x)) + sin (y * 0.5);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
writeCopyRead (const Array2D<half> &ph1,
Packit 0d464f
	       const char fileName1[],
Packit 0d464f
	       const char fileName2[],
Packit 0d464f
	       int width,
Packit 0d464f
	       int height,
Packit 0d464f
	       int xOffset,
Packit 0d464f
	       int yOffset,
Packit 0d464f
	       Compression comp)
Packit 0d464f
{
Packit 0d464f
    //
Packit 0d464f
    // Write the pixel data in ph1 to an image file using the
Packit 0d464f
    // specified compression type and subsampling rates.
Packit 0d464f
    // Then copy the image file using OutputFile::copyPixels()
Packit 0d464f
    // Read the pixel data back from the copied file verify
Packit 0d464f
    // that the data are the same as those in ph1.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    cout << "compression " << comp << ":" << flush;
Packit 0d464f
Packit 0d464f
    Header hdr ((Box2i (V2i (0, 0),			// display window
Packit 0d464f
		        V2i (width - 1, height -1))),
Packit 0d464f
		(Box2i (V2i (xOffset, yOffset),		// data window
Packit 0d464f
		        V2i (xOffset + width - 1, yOffset + height - 1))));
Packit 0d464f
Packit 0d464f
    hdr.compression() = comp;
Packit 0d464f
    hdr.channels().insert ("H", Channel (HALF, 1, 1));
Packit 0d464f
Packit 0d464f
    {
Packit 0d464f
	FrameBuffer fb; 
Packit 0d464f
Packit 0d464f
	fb.insert ("H",						// name
Packit 0d464f
		   Slice (HALF,					// type
Packit 0d464f
			  (char *) &ph1[-yOffset][-xOffset],	// base
Packit 0d464f
			  sizeof (ph1[0][0]), 			// xStride
Packit 0d464f
			  sizeof (ph1[0][0]) * width,		// yStride
Packit 0d464f
			  1,					// xSampling
Packit 0d464f
			  1)					// ySampling
Packit 0d464f
		  );
Packit 0d464f
	
Packit 0d464f
	cout << " writing" << flush;
Packit 0d464f
Packit 0d464f
	remove (fileName1);
Packit 0d464f
	OutputFile out (fileName1, hdr);
Packit 0d464f
	out.setFrameBuffer (fb);
Packit 0d464f
	out.writePixels (height);
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    {
Packit 0d464f
	cout << " copying" << flush;
Packit 0d464f
Packit 0d464f
	remove (fileName2);
Packit 0d464f
	InputFile in (fileName1);
Packit 0d464f
	OutputFile out (fileName2, in.header());
Packit 0d464f
	out.copyPixels (in);
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    {
Packit 0d464f
	cout << " reading" << flush;
Packit 0d464f
Packit 0d464f
	InputFile in1 (fileName1);
Packit 0d464f
	InputFile in2 (fileName2);
Packit 0d464f
	
Packit 0d464f
	const Box2i &dw = in2.header().dataWindow();
Packit 0d464f
	int w = dw.max.x - dw.min.x + 1;
Packit 0d464f
	int h = dw.max.y - dw.min.y + 1;
Packit 0d464f
	int dx = dw.min.x;
Packit 0d464f
	int dy = dw.min.y;
Packit 0d464f
Packit 0d464f
	Array2D<half> ph1 (h, w);
Packit 0d464f
	Array2D<half> ph2 (h, w);
Packit 0d464f
Packit 0d464f
	FrameBuffer fb1;
Packit 0d464f
	FrameBuffer fb2;
Packit 0d464f
Packit 0d464f
	fb1.insert ("H",				// name
Packit 0d464f
		    Slice (HALF,			// type
Packit 0d464f
			   (char *) &ph1[-dy][-dx],	// base
Packit 0d464f
			   sizeof (ph1[0][0]), 		// xStride
Packit 0d464f
			   sizeof (ph1[0][0]) * w,	// yStride
Packit 0d464f
			   1,				// xSampling
Packit 0d464f
			   1)				// ySampling
Packit 0d464f
		   );
Packit 0d464f
Packit 0d464f
	fb2.insert ("H",				// name
Packit 0d464f
		    Slice (HALF,			// type
Packit 0d464f
			   (char *) &ph2[-dy][-dx],	// base
Packit 0d464f
			   sizeof (ph2[0][0]), 		// xStride
Packit 0d464f
			   sizeof (ph2[0][0]) * w,	// yStride
Packit 0d464f
			   1,				// xSampling
Packit 0d464f
			   1)				// ySampling
Packit 0d464f
		   );
Packit 0d464f
	
Packit 0d464f
	in1.setFrameBuffer (fb1);
Packit 0d464f
	in1.readPixels (dw.min.y, dw.max.y);
Packit 0d464f
	in2.setFrameBuffer (fb2);
Packit 0d464f
	in2.readPixels (dw.min.y, dw.max.y);
Packit 0d464f
Packit 0d464f
	cout << " comparing" << flush;
Packit 0d464f
Packit 0d464f
	assert (in2.header().displayWindow() == hdr.displayWindow());
Packit 0d464f
	assert (in2.header().dataWindow() == hdr.dataWindow());
Packit 0d464f
	assert (in2.header().pixelAspectRatio() == hdr.pixelAspectRatio());
Packit 0d464f
	assert (in2.header().screenWindowCenter() == hdr.screenWindowCenter());
Packit 0d464f
	assert (in2.header().screenWindowWidth() == hdr.screenWindowWidth());
Packit 0d464f
	assert (in2.header().lineOrder() == hdr.lineOrder());
Packit 0d464f
	assert (in2.header().compression() == hdr.compression());
Packit 0d464f
	assert (in2.header().channels() == hdr.channels());
Packit 0d464f
Packit 0d464f
	for (int y = 0; y < h; ++y)
Packit 0d464f
	    for (int x = 0; x < w; ++x)
Packit 0d464f
		assert (ph1[y][x] == ph2[y][x]);
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    remove (fileName1);
Packit 0d464f
    remove (fileName2);
Packit 0d464f
    cout << endl;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
writeCopyRead (const std::string &tempDir, const Array2D<half> &ph, int w, int h, int dx, int dy)
Packit 0d464f
{
Packit 0d464f
    std::string filename1 = tempDir + "imf_test_copy1.exr";
Packit 0d464f
    std::string filename2 = tempDir + "imf_test_copy2.exr";
Packit 0d464f
Packit 0d464f
    for (int comp = 0; comp < NUM_COMPRESSION_METHODS; ++comp)
Packit 0d464f
    {
Packit 0d464f
	writeCopyRead (ph,
Packit 0d464f
		       filename1.c_str(),
Packit 0d464f
		       filename2.c_str(),
Packit 0d464f
		       w, h,
Packit 0d464f
		       dx, dy,
Packit 0d464f
		       Compression (comp));
Packit 0d464f
    }
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
} // namespace
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
testCopyPixels (const std::string &tempDir)
Packit 0d464f
{
Packit 0d464f
    try
Packit 0d464f
    {
Packit 0d464f
	cout << "Testing fast pixel copying" << endl;
Packit 0d464f
Packit 0d464f
	const int W = 371;
Packit 0d464f
	const int H = 559;
Packit 0d464f
	const int DX = 17;
Packit 0d464f
	const int DY = 29;
Packit 0d464f
Packit 0d464f
	Array2D<half> ph (H, W);
Packit 0d464f
Packit 0d464f
	fillPixels (ph, W, H);
Packit 0d464f
	writeCopyRead (tempDir, ph, W, H, 0,  0);
Packit 0d464f
	writeCopyRead (tempDir, ph, W, H, 0,  DY);
Packit 0d464f
	writeCopyRead (tempDir, ph, W, H, DX, 0);
Packit 0d464f
	writeCopyRead (tempDir, ph, W, H, DX, DY);
Packit 0d464f
Packit 0d464f
	cout << "ok\n" << endl;
Packit 0d464f
    }
Packit 0d464f
    catch (const std::exception &e)
Packit 0d464f
    {
Packit 0d464f
	cerr << "ERROR -- caught exception: " << e.what() << endl;
Packit 0d464f
	assert (false);
Packit 0d464f
    }
Packit 0d464f
}