Blame IlmImfTest/testSharedFrameBuffer.cpp

Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
//
Packit Service 6754ca
// Copyright (c) 2004-2012, Industrial Light & Magic, a division of Lucas
Packit Service 6754ca
// Digital Ltd. LLC
Packit Service 6754ca
// 
Packit Service 6754ca
// All rights reserved.
Packit Service 6754ca
// 
Packit Service 6754ca
// Redistribution and use in source and binary forms, with or without
Packit Service 6754ca
// modification, are permitted provided that the following conditions are
Packit Service 6754ca
// met:
Packit Service 6754ca
// *       Redistributions of source code must retain the above copyright
Packit Service 6754ca
// notice, this list of conditions and the following disclaimer.
Packit Service 6754ca
// *       Redistributions in binary form must reproduce the above
Packit Service 6754ca
// copyright notice, this list of conditions and the following disclaimer
Packit Service 6754ca
// in the documentation and/or other materials provided with the
Packit Service 6754ca
// distribution.
Packit Service 6754ca
// *       Neither the name of Industrial Light & Magic nor the names of
Packit Service 6754ca
// its contributors may be used to endorse or promote products derived
Packit Service 6754ca
// from this software without specific prior written permission. 
Packit Service 6754ca
// 
Packit Service 6754ca
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 6754ca
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 6754ca
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit Service 6754ca
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit Service 6754ca
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit Service 6754ca
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit Service 6754ca
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 6754ca
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 6754ca
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 6754ca
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 6754ca
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 6754ca
//
Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
#include "compareB44.h"
Packit Service 6754ca
#include "compareDwa.h"
Packit Service 6754ca
Packit Service 6754ca
#include <ImfRgbaFile.h>
Packit Service 6754ca
#include <ImfArray.h>
Packit Service 6754ca
#include <string>
Packit Service 6754ca
#include <ImathRandom.h>
Packit Service 6754ca
#include <ImfThreading.h>
Packit Service 6754ca
#include <IlmThread.h>
Packit Service 6754ca
#include <IlmThreadMutex.h>
Packit Service 6754ca
#include <IlmThreadSemaphore.h>
Packit Service 6754ca
#include <ImfThreading.h>
Packit Service 6754ca
Packit Service 6754ca
#include <stdio.h>
Packit Service 6754ca
#include <assert.h>
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
using namespace OPENEXR_IMF_NAMESPACE;
Packit Service 6754ca
using namespace std;
Packit Service 6754ca
using namespace IMATH_NAMESPACE;
Packit Service 6754ca
using namespace ILMTHREAD_NAMESPACE;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
namespace {
Packit Service 6754ca
Packit Service 6754ca
Rand48 rand1 (27);
Packit Service 6754ca
Mutex scanlineMutex;
Packit Service 6754ca
Semaphore threadSemaphore;
Packit Service 6754ca
int remainingScanlines = 0;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
void
Packit Service 6754ca
fillPixels (Array2D<Rgba> &pixels, int w, int h)
Packit Service 6754ca
{
Packit Service 6754ca
    assert (sizeof (int) == sizeof (float));
Packit Service 6754ca
    
Packit Service 6754ca
    for (int y = 0; y < h; ++y)
Packit Service 6754ca
    {
Packit Service 6754ca
	for (int x = 0; x < w; ++x)
Packit Service 6754ca
	{
Packit Service 6754ca
	    Rgba &p = pixels[y][x];
Packit Service 6754ca
Packit Service 6754ca
	    p.r = x % 100 + 100 * (y % 100) +
Packit Service 6754ca
		  rand1.nextf (-0.01f, -0.01f);
Packit Service 6754ca
Packit Service 6754ca
	    p.g = 0.5 + 0.5 * sin (0.1 * x + 0.2 * y) +
Packit Service 6754ca
                  rand1.nextf (-0.01f, -0.01f);
Packit Service 6754ca
Packit Service 6754ca
	    p.b = 0.5 + 0.5 * sin (0.1 * x + 0.3 * y) +
Packit Service 6754ca
                  rand1.nextf (-0.01f, -0.01f);
Packit Service 6754ca
Packit Service 6754ca
	    p.a = rand1.nextf (0.0f, 1.0f);
Packit Service 6754ca
	}
Packit Service 6754ca
    }
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
class WriterThread: public Thread
Packit Service 6754ca
{
Packit Service 6754ca
  public:
Packit Service 6754ca
Packit Service 6754ca
    WriterThread (RgbaOutputFile *outfile);
Packit Service 6754ca
Packit Service 6754ca
    virtual void	run ();
Packit Service 6754ca
Packit Service 6754ca
  private:
Packit Service 6754ca
Packit Service 6754ca
    RgbaOutputFile *	_outfile;
Packit Service 6754ca
};
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
WriterThread::WriterThread (RgbaOutputFile *outfile): _outfile (outfile)
Packit Service 6754ca
{
Packit Service 6754ca
    start();
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
void
Packit Service 6754ca
WriterThread::run ()
Packit Service 6754ca
{
Packit Service 6754ca
    //
Packit Service 6754ca
    // Signal that the thread has started
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    threadSemaphore.post();
Packit Service 6754ca
Packit Service 6754ca
    while (true)
Packit Service 6754ca
    {
Packit Service 6754ca
	Lock lock (scanlineMutex);
Packit Service 6754ca
Packit Service 6754ca
	if (remainingScanlines)
Packit Service 6754ca
	{
Packit Service 6754ca
	    int n = int (rand1.nextf (0.0f, remainingScanlines) + 0.5f);
Packit Service 6754ca
	    _outfile->writePixels (n);
Packit Service 6754ca
	    remainingScanlines -= n;
Packit Service 6754ca
	}
Packit Service 6754ca
	else
Packit Service 6754ca
	{
Packit Service 6754ca
	    break;
Packit Service 6754ca
	}
Packit Service 6754ca
    }
Packit Service 6754ca
}
Packit Service 6754ca
    
Packit Service 6754ca
Packit Service 6754ca
class ReaderThread : public Thread
Packit Service 6754ca
{
Packit Service 6754ca
  public:
Packit Service 6754ca
Packit Service 6754ca
    ReaderThread (RgbaInputFile *infile, int start, int step);
Packit Service 6754ca
Packit Service 6754ca
    virtual void	run ();
Packit Service 6754ca
    
Packit Service 6754ca
  private:
Packit Service 6754ca
Packit Service 6754ca
    RgbaInputFile *	_infile;
Packit Service 6754ca
    int			_start;
Packit Service 6754ca
    int			_step;
Packit Service 6754ca
};
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
ReaderThread::ReaderThread (RgbaInputFile *infile, int start, int step):
Packit Service 6754ca
    _infile (infile), _start (start), _step (step)
Packit Service 6754ca
{
Packit Service 6754ca
    Thread::start();
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
void
Packit Service 6754ca
ReaderThread::run ()
Packit Service 6754ca
{
Packit Service 6754ca
    //
Packit Service 6754ca
    // Signal that the thread has started
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    threadSemaphore.post ();
Packit Service 6754ca
Packit Service 6754ca
    int num = _infile->header().dataWindow().max.y -
Packit Service 6754ca
	      _infile->header().dataWindow().min.y + 1;
Packit Service 6754ca
Packit Service 6754ca
    for (int i = _start; i < num; i += _step)
Packit Service 6754ca
	_infile->readPixels (i);
Packit Service 6754ca
}
Packit Service 6754ca
    
Packit Service 6754ca
Packit Service 6754ca
void
Packit Service 6754ca
writeReadRGBA (const char fileName[],
Packit Service 6754ca
	       int width,
Packit Service 6754ca
	       int height,
Packit Service 6754ca
	       const Array2D<Rgba> &p1,
Packit Service 6754ca
	       RgbaChannels channels,
Packit Service 6754ca
	       Compression comp)
Packit Service 6754ca
{
Packit Service 6754ca
    //
Packit Service 6754ca
    // Save the selected channels of RGBA image p1; save the
Packit Service 6754ca
    // scan lines in the specified order.  Read the image back
Packit Service 6754ca
    // from the file, and compare the data with the orignal.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    cout << "channels " <<
Packit Service 6754ca
	    ((channels & WRITE_R)? "R": "") <<
Packit Service 6754ca
	    ((channels & WRITE_G)? "G": "") <<
Packit Service 6754ca
	    ((channels & WRITE_B)? "B": "") <<
Packit Service 6754ca
	    ((channels & WRITE_A)? "A": "") <<
Packit Service 6754ca
	    ", compression " << comp << ", " << flush;
Packit Service 6754ca
Packit Service 6754ca
    Header header (width, height);
Packit Service 6754ca
    header.compression() = comp;
Packit Service 6754ca
Packit Service 6754ca
    {
Packit Service 6754ca
	remove (fileName);
Packit Service 6754ca
        cout << "writing " << flush;
Packit Service 6754ca
	RgbaOutputFile out (fileName, header, channels);
Packit Service 6754ca
        out.setFrameBuffer (&p1[0][0], 1, width);
Packit Service 6754ca
        
Packit Service 6754ca
        remainingScanlines = height;
Packit Service 6754ca
        WriterThread writer1 (&out;;
Packit Service 6754ca
        WriterThread writer2 (&out;;
Packit Service 6754ca
        threadSemaphore.wait();
Packit Service 6754ca
        threadSemaphore.wait();
Packit Service 6754ca
    }
Packit Service 6754ca
Packit Service 6754ca
    {
Packit Service 6754ca
        cout << "reading " << flush;
Packit Service 6754ca
	RgbaInputFile in (fileName);
Packit Service 6754ca
	const Box2i &dw = in.dataWindow();
Packit Service 6754ca
	
Packit Service 6754ca
	int w = dw.max.x - dw.min.x + 1;
Packit Service 6754ca
	int h = dw.max.y - dw.min.y + 1;
Packit Service 6754ca
	int dx = dw.min.x;
Packit Service 6754ca
	int dy = dw.min.y;
Packit Service 6754ca
Packit Service 6754ca
	Array2D<Rgba> p2 (h, w);
Packit Service 6754ca
	in.setFrameBuffer (&p2[-dy][-dx], 1, w);
Packit Service 6754ca
        
Packit Service 6754ca
        {
Packit Service 6754ca
            ReaderThread reader1 (&in, 0, 2);
Packit Service 6754ca
            ReaderThread reader2 (&in, 1, 2);
Packit Service 6754ca
            threadSemaphore.wait();
Packit Service 6754ca
            threadSemaphore.wait();
Packit Service 6754ca
        }
Packit Service 6754ca
Packit Service 6754ca
	assert (in.displayWindow() == header.displayWindow());
Packit Service 6754ca
	assert (in.dataWindow() == header.dataWindow());
Packit Service 6754ca
	assert (in.pixelAspectRatio() == header.pixelAspectRatio());
Packit Service 6754ca
	assert (in.screenWindowCenter() == header.screenWindowCenter());
Packit Service 6754ca
	assert (in.screenWindowWidth() == header.screenWindowWidth());
Packit Service 6754ca
	assert (in.lineOrder() == header.lineOrder());
Packit Service 6754ca
	assert (in.compression() == header.compression());
Packit Service 6754ca
	assert (in.channels() == channels);
Packit Service 6754ca
Packit Service 6754ca
        cout << "comparing " << endl;
Packit Service 6754ca
Packit Service 6754ca
	if (in.compression() == B44_COMPRESSION ||
Packit Service 6754ca
	    in.compression() == B44A_COMPRESSION)
Packit Service 6754ca
	{
Packit Service 6754ca
	    compareB44 (width, height, p1, p2, channels);
Packit Service 6754ca
	}
Packit Service 6754ca
	else if (in.compression() == DWAA_COMPRESSION ||
Packit Service 6754ca
	         in.compression() == DWAB_COMPRESSION)
Packit Service 6754ca
	{
Packit Service 6754ca
	    compareDwa (width, height, p1, p2, channels);
Packit Service 6754ca
	}
Packit Service 6754ca
	else
Packit Service 6754ca
	{
Packit Service 6754ca
	    for (int y = 0; y < h; ++y)
Packit Service 6754ca
	    {
Packit Service 6754ca
		for (int x = 0; x < w; ++x)
Packit Service 6754ca
		{
Packit Service 6754ca
		    if (channels & WRITE_R)
Packit Service 6754ca
			assert (p2[y][x].r == p1[y][x].r);
Packit Service 6754ca
		    else
Packit Service 6754ca
			assert (p2[y][x].r == 0);
Packit Service 6754ca
Packit Service 6754ca
		    if (channels & WRITE_G)
Packit Service 6754ca
			assert (p2[y][x].g == p1[y][x].g);
Packit Service 6754ca
		    else
Packit Service 6754ca
			assert (p2[y][x].g == 0);
Packit Service 6754ca
Packit Service 6754ca
		    if (channels & WRITE_B)
Packit Service 6754ca
			assert (p2[y][x].b == p1[y][x].b);
Packit Service 6754ca
		    else
Packit Service 6754ca
			assert (p2[y][x].b == 0);
Packit Service 6754ca
Packit Service 6754ca
		    if (channels & WRITE_A)
Packit Service 6754ca
			assert (p2[y][x].a == p1[y][x].a);
Packit Service 6754ca
		    else
Packit Service 6754ca
			assert (p2[y][x].a == 1);
Packit Service 6754ca
		}
Packit Service 6754ca
	    }
Packit Service 6754ca
	}
Packit Service 6754ca
    }
Packit Service 6754ca
Packit Service 6754ca
    remove (fileName);
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
} // namespace
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
void
Packit Service 6754ca
testSharedFrameBuffer (const std::string &tempDir)
Packit Service 6754ca
{
Packit Service 6754ca
    try
Packit Service 6754ca
    {
Packit Service 6754ca
	cout << "Testing reading from and writing to files using\n"
Packit Service 6754ca
		"multiple threads and a shared framebuffer" << endl;
Packit Service 6754ca
Packit Service 6754ca
        if (!ILMTHREAD_NAMESPACE::supportsThreads ())
Packit Service 6754ca
        {
Packit Service 6754ca
            cout << "   Threading not supported!" << endl << endl;
Packit Service 6754ca
            return;
Packit Service 6754ca
        }
Packit Service 6754ca
                
Packit Service 6754ca
	const int W = 1371;
Packit Service 6754ca
	const int H = 159;
Packit Service 6754ca
        
Packit Service 6754ca
	Array2D<Rgba> p1 (H, W);
Packit Service 6754ca
	fillPixels (p1, W, H);
Packit Service 6754ca
        
Packit Service 6754ca
        for (int n = 0; n <= 8; n++)
Packit Service 6754ca
        {
Packit Service 6754ca
            int numThreads = (n * 3) % 8;
Packit Service 6754ca
Packit Service 6754ca
            setGlobalThreadCount (numThreads);
Packit Service 6754ca
            cout << "number of threads: " << globalThreadCount () << endl;
Packit Service 6754ca
Packit Service 6754ca
            for (int comp = 0; comp < NUM_COMPRESSION_METHODS; ++comp)
Packit Service 6754ca
            {
Packit Service 6754ca
                writeReadRGBA ((tempDir + "imf_test_rgba.exr").c_str(),
Packit Service 6754ca
                                W, H, p1,
Packit Service 6754ca
                                WRITE_RGBA,
Packit Service 6754ca
                                Compression (comp));
Packit Service 6754ca
Packit Service 6754ca
                writeReadRGBA ((tempDir + "imf_test_rgba.exr").c_str(),
Packit Service 6754ca
                                W, H, p1,
Packit Service 6754ca
                                WRITE_RGB,
Packit Service 6754ca
                                Compression (comp));
Packit Service 6754ca
Packit Service 6754ca
                writeReadRGBA ((tempDir + "imf_test_rgba.exr").c_str(),
Packit Service 6754ca
                                W, H, p1,
Packit Service 6754ca
                                WRITE_A,
Packit Service 6754ca
                                Compression (comp));
Packit Service 6754ca
Packit Service 6754ca
                writeReadRGBA ((tempDir + "imf_test_rgba.exr").c_str(),
Packit Service 6754ca
                                W, H, p1,
Packit Service 6754ca
                                RgbaChannels (WRITE_R | WRITE_B),
Packit Service 6754ca
                                Compression (comp));
Packit Service 6754ca
            }
Packit Service 6754ca
        }
Packit Service 6754ca
Packit Service 6754ca
	cout << "ok\n" << endl;
Packit Service 6754ca
    }
Packit Service 6754ca
    catch (const std::exception &e)
Packit Service 6754ca
    {
Packit Service 6754ca
	cerr << "ERROR -- caught exception: " << e.what() << endl;
Packit Service 6754ca
	assert (false);
Packit Service 6754ca
    }
Packit Service 6754ca
}