Blame IlmImfUtil/ImfSampleCountChannel.h

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