Blame IlmImfUtil/ImfSampleCountChannel.h

Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
//
Packit Service 6754ca
// Copyright (c) 2014, 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
#ifndef INCLUDED_IMF_SAMPLE_COUNT_CHANNEL_H
Packit Service 6754ca
#define INCLUDED_IMF_SAMPLE_COUNT_CHANNEL_H
Packit Service 6754ca
Packit Service 6754ca
//----------------------------------------------------------------------------
Packit Service 6754ca
//
Packit Service 6754ca
//      class SampleCountChannel
Packit Service 6754ca
//
Packit Service 6754ca
//      For an explanation of images, levels and channels,
Packit Service 6754ca
//      see the comments in header file Image.h.
Packit Service 6754ca
//
Packit Service 6754ca
//----------------------------------------------------------------------------
Packit Service 6754ca
Packit Service 6754ca
#include "ImfImageChannel.h"
Packit Service 6754ca
#include "ImfExport.h"
Packit Service 6754ca
Packit Service 6754ca
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Packit Service 6754ca
Packit Service 6754ca
class DeepImageLevel;
Packit Service 6754ca
Packit Service 6754ca
//
Packit Service 6754ca
// Sample count channel for a deep image level:
Packit Service 6754ca
//
Packit Service 6754ca
// Each deep image level has a number of samples channel.  For each
Packit Service 6754ca
// pixel location (x,y) within the data window of the level, the sample
Packit Service 6754ca
// count channel stores a single integer, n(x,y).  A deep channel, c,
Packit Service 6754ca
// in the level as the sample count channel stores n(x,y) samples at
Packit Service 6754ca
// location (x,y) if
Packit Service 6754ca
//
Packit Service 6754ca
//          x % c.xSampling() == 0 and y % c.ySampling() == 0.
Packit Service 6754ca
//
Packit Service 6754ca
// The deep channel stores no samples at location (x,y) if
Packit Service 6754ca
//
Packit Service 6754ca
//          x % c.xSampling() != 0 or y % c.ySampling() != 0,
Packit Service 6754ca
//
Packit Service 6754ca
Packit Service 6754ca
class IMF_EXPORT SampleCountChannel : public ImageChannel
Packit Service 6754ca
{
Packit Service 6754ca
  public:
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // The OpenEXR pixel type of this channel (HALF, FLOAT or UINT).
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    virtual PixelType   pixelType () const;
Packit Service 6754ca
    
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Construct an OpenEXR frame buffer slice for this channel.
Packit Service 6754ca
    // This function is needed reading an image from an OpenEXR
Packit Service 6754ca
    // file and for saving an image in an OpenEXR file.
Packit Service 6754ca
    // 
Packit Service 6754ca
Packit Service 6754ca
    Slice                   slice () const;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Access to the image level to which this channel belongs.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    DeepImageLevel &        deepLevel ();
Packit Service 6754ca
    const DeepImageLevel &  deepLevel () const;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Access to n(x,y), without bounds checking.  Accessing a location
Packit Service 6754ca
    // outside the data window of the image level results in undefined
Packit Service 6754ca
    // behavior.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    const unsigned int &    operator () (int x, int y) const;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Access to n(x,y), with bounds checking.  Accessing a location outside
Packit Service 6754ca
    // the data window of the image level throws an Iex::ArgExc exception.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    const unsigned int &    at (int x, int y) const;
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Faster access to n(x,y) for all pixels in a single horizontal row of
Packit Service 6754ca
    // the channel.  Rows are numbered from 0 to pixelsPerColumn()-1, and
Packit Service 6754ca
    // each row contains pixelsPerRow() values.
Packit Service 6754ca
    // Access is not bounds checked; accessing out of bounds rows or pixels
Packit Service 6754ca
    // results in undefined behavior.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    const unsigned int *    row (int r) const;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Change the sample counts in one or more pixels:
Packit Service 6754ca
    //
Packit Service 6754ca
    // set(x,y,m)   sets n(x,y) to m.
Packit Service 6754ca
    //
Packit Service 6754ca
    // set(r,m)     sets n(x,y) for all pixels in row r according to the
Packit Service 6754ca
    //              values in array m.  The array  must contain pixelsPerRow()
Packit Service 6754ca
    //              entries, and the row number must be in the range from 0
Packit Service 6754ca
    //              to pixelsPerColumn()-1.
Packit Service 6754ca
    //
Packit Service 6754ca
    // clear()      sets n(x,y) to 0 for all pixels within the data window
Packit Service 6754ca
    //              of the level.
Packit Service 6754ca
    //
Packit Service 6754ca
    // If the sample count for a pixel is increased, then new samples are
Packit Service 6754ca
    // appended at the end of the sample list of each deep channel.  The
Packit Service 6754ca
    // new samples are initialized to zero.  If the sample count in a pixel
Packit Service 6754ca
    // is decreased, then sample list of each deep channel is truncated by
Packit Service 6754ca
    // discarding samples at the end of the list.
Packit Service 6754ca
    //
Packit Service 6754ca
    // Access is bounds-checked; attempting to set the number of samples of
Packit Service 6754ca
    // a pixel outside the data window throws an Iex::ArgExc exception.
Packit Service 6754ca
    //
Packit Service 6754ca
    // Memory allocation for the sample lists is not particularly clever;
Packit Service 6754ca
    // repeatedly increasing and decreasing the number of samples in the
Packit Service 6754ca
    // pixels of a level is likely to result in serious memory fragmentation.
Packit Service 6754ca
    //
Packit Service 6754ca
    // Setting the number of samples for one or more pixels may cause the
Packit Service 6754ca
    // program to run out of memory.  If this happens, the image is resized
Packit Service 6754ca
    // to zero by zero pixels and an exception is thrown.  Note that the
Packit Service 6754ca
    // resizing operation deletes this sample count channel and the image
Packit Service 6754ca
    // level to which it belongs.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
	void                set(int x, int y, unsigned int newNumSamples);
Packit Service 6754ca
	void                set(int r, unsigned int newNumSamples[]);
Packit Service 6754ca
	void                clear();
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // OpenEXR file reading support / make sample counts editable:
Packit Service 6754ca
    //
Packit Service 6754ca
    //  beginEdit()     frees all memory that has been allocated for samples
Packit Service 6754ca
    //                  in the deep channels, and returns a pointer to an
Packit Service 6754ca
    //                  array of pixelsPerRow() by pixelsPerColumn() sample
Packit Service 6754ca
    //                  counts in row-major order.
Packit Service 6754ca
    //
Packit Service 6754ca
    //                  After beginEdit() returns, application code is
Packit Service 6754ca
    //                  free to change the values in the sample count array.
Packit Service 6754ca
    //                  In particular, the application can fill the array by
Packit Service 6754ca
    //                  reading the sample counts from an OpenEXR file.
Packit Service 6754ca
    //
Packit Service 6754ca
    //                  However, since memory for the samples in the deep
Packit Service 6754ca
    //                  channels has been freed, attempting to access any
Packit Service 6754ca
    //                  sample in a deep channel results in undefined
Packit Service 6754ca
    //                  behavior, most likely a program crash.
Packit Service 6754ca
    //
Packit Service 6754ca
    //  endEdit()       allocates new memory for all samples in the deep
Packit Service 6754ca
    //                  channels of the layer, according to the current
Packit Service 6754ca
    //                  sample counts, and sets the samples to zero.
Packit Service 6754ca
    //
Packit Service 6754ca
    // Application code must take make sure that each call to beginEdit()
Packit Service 6754ca
    // is followed by a corresponding endEdit() call, even if an
Packit Service 6754ca
    // exception occurs while the sample counts are acessed.  In order to
Packit Service 6754ca
    // do that, application code may want to create a temporary Edit
Packit Service 6754ca
    //  object instead of calling beginEdit() and endEdit() directly.
Packit Service 6754ca
    //
Packit Service 6754ca
    // Setting the number of samples for all pixels in the image may
Packit Service 6754ca
    // cause the program to run out of memory.  If this happens, the image
Packit Service 6754ca
    // is resized to zero by zero pixels and an exception is thrown.
Packit Service 6754ca
    // Note that the resizing operation deletes this sample count channel
Packit Service 6754ca
    // and the image level to which it belongs.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
	unsigned int *      beginEdit();
Packit Service 6754ca
	void                endEdit();
Packit Service 6754ca
Packit Service 6754ca
    class Edit
Packit Service 6754ca
    {
Packit Service 6754ca
      public:
Packit Service 6754ca
Packit Service 6754ca
        //
Packit Service 6754ca
        // Constructor calls level->beginEdit(),
Packit Service 6754ca
        // destructor calls level->endEdit().
Packit Service 6754ca
        //
Packit Service 6754ca
Packit Service 6754ca
         Edit (SampleCountChannel& level);
Packit Service 6754ca
        ~Edit ();
Packit Service 6754ca
Packit Service 6754ca
        //
Packit Service 6754ca
        // Access to the writable sample count array.
Packit Service 6754ca
        //
Packit Service 6754ca
Packit Service 6754ca
        unsigned int *          sampleCounts () const;
Packit Service 6754ca
Packit Service 6754ca
      private:
Packit Service 6754ca
Packit Service 6754ca
        SampleCountChannel &    _channel;
Packit Service 6754ca
        unsigned int *          _sampleCounts;
Packit Service 6754ca
    };
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Functions that support the implementation of deep image channels.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    const unsigned int *    numSamples () const;
Packit Service 6754ca
    const unsigned int *    sampleListSizes () const;
Packit Service 6754ca
    const size_t *          sampleListPositions () const;
Packit Service 6754ca
    size_t                  sampleBufferSize () const;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
  private:
Packit Service 6754ca
Packit Service 6754ca
    friend class DeepImageLevel;
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // The constructor and destructor are not public because
Packit Service 6754ca
    // image channels exist only as parts of a deep image level.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    SampleCountChannel (DeepImageLevel &level);
Packit Service 6754ca
    virtual ~SampleCountChannel();
Packit Service 6754ca
Packit Service 6754ca
    virtual void        resize ();
Packit Service 6754ca
Packit Service 6754ca
    void                resetBasePointer ();
Packit Service 6754ca
Packit Service 6754ca
    unsigned int *  _numSamples;            // Array of per-pixel sample counts
Packit Service 6754ca
                                           
Packit Service 6754ca
    unsigned int *  _base;                  // Base pointer for faster access
Packit Service 6754ca
                                            // to entries in _numSamples
Packit Service 6754ca
Packit Service 6754ca
    unsigned int *  _sampleListSizes;       // Array of allocated sizes of
Packit Service 6754ca
                                            // per-pixel sample lists
Packit Service 6754ca
Packit Service 6754ca
    size_t *        _sampleListPositions;   // Array of positions of per-pixel
Packit Service 6754ca
                                            // sample lists within sample list
Packit Service 6754ca
                                            // buffer
Packit Service 6754ca
Packit Service 6754ca
    size_t          _totalNumSamples;       // Sum of all entries in the
Packit Service 6754ca
                                            // _numSamples array
Packit Service 6754ca
Packit Service 6754ca
    size_t          _totalSamplesOccupied;  // Total number of samples within
Packit Service 6754ca
                                            // sample list buffer that have
Packit Service 6754ca
                                            // either been allocated for sample
Packit Service 6754ca
                                            // lists or lost to fragmentation
Packit Service 6754ca
Packit Service 6754ca
    size_t          _sampleBufferSize;      // Size of the sample list buffer.
Packit Service 6754ca
};
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
//-----------------------------------------------------------------------------
Packit Service 6754ca
// Implementation of templates and inline functions
Packit Service 6754ca
//-----------------------------------------------------------------------------
Packit Service 6754ca
Packit Service 6754ca
inline
Packit Service 6754ca
SampleCountChannel::Edit::Edit (SampleCountChannel &channel):
Packit Service 6754ca
    _channel (channel),
Packit Service 6754ca
    _sampleCounts (channel.beginEdit())
Packit Service 6754ca
{
Packit Service 6754ca
    // empty
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
inline
Packit Service 6754ca
SampleCountChannel::Edit::~Edit ()
Packit Service 6754ca
{
Packit Service 6754ca
    _channel.endEdit();
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
inline unsigned int *
Packit Service 6754ca
SampleCountChannel::Edit::sampleCounts () const
Packit Service 6754ca
{
Packit Service 6754ca
    return _sampleCounts;
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
inline const unsigned int *
Packit Service 6754ca
SampleCountChannel::numSamples () const
Packit Service 6754ca
{
Packit Service 6754ca
    return _numSamples;
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
inline const unsigned int *
Packit Service 6754ca
SampleCountChannel::sampleListSizes () const
Packit Service 6754ca
{
Packit Service 6754ca
    return _sampleListSizes;
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
inline const size_t *
Packit Service 6754ca
SampleCountChannel::sampleListPositions () const
Packit Service 6754ca
{
Packit Service 6754ca
    return _sampleListPositions;
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
inline size_t
Packit Service 6754ca
SampleCountChannel::sampleBufferSize () const
Packit Service 6754ca
{
Packit Service 6754ca
    return _sampleBufferSize;
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
inline const unsigned int &
Packit Service 6754ca
SampleCountChannel::operator () (int x, int y) const
Packit Service 6754ca
{
Packit Service 6754ca
    return _base[y * pixelsPerRow() + x];
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
inline const unsigned int &
Packit Service 6754ca
SampleCountChannel::at (int x, int y) const
Packit Service 6754ca
{
Packit Service 6754ca
    boundsCheck (x, y);
Packit Service 6754ca
    return _base[y * pixelsPerRow() + x];
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
inline const unsigned int *
Packit Service 6754ca
SampleCountChannel::row (int n) const
Packit Service 6754ca
{
Packit Service 6754ca
    return _base + n * pixelsPerRow();
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Packit Service 6754ca
Packit Service 6754ca
#endif