Blame IlmImfUtil/ImfFlatImageChannel.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_FLAT_IMAGE_CHANNEL_H
Packit 0d464f
#define INCLUDED_IMF_FLAT_IMAGE_CHANNEL_H
Packit 0d464f
Packit 0d464f
//----------------------------------------------------------------------------
Packit 0d464f
//
Packit 0d464f
//      class FlatImageChannel,
Packit 0d464f
//      template class TypedFlatImageChannel<T>
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 <ImfPixelType.h>
Packit 0d464f
#include <ImfFrameBuffer.h>
Packit 0d464f
#include <ImathBox.h>
Packit 0d464f
#include <half.h>
Packit 0d464f
Packit 0d464f
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Packit 0d464f
Packit 0d464f
class FlatImageLevel;
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// Image channels:
Packit 0d464f
//
Packit 0d464f
// A TypedFlatImageChannel<T> holds the pixel data for a single channel
Packit 0d464f
// of one level of a flat image.  The pixels in the channel are of type T,
Packit 0d464f
// where T is either half, float or unsigned int.  Storage is allocated
Packit 0d464f
// only for pixels within the data window of the level.
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
class FlatImageChannel: public ImageChannel
Packit 0d464f
{
Packit 0d464f
  public:
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
    virtual Slice           slice () const = 0;
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //
Packit 0d464f
    // Access to the flat image level to which this channel belongs.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    FlatImageLevel &        flatLevel ();
Packit 0d464f
    const FlatImageLevel &  flatLevel () const;
Packit 0d464f
Packit 0d464f
  protected:
Packit 0d464f
Packit 0d464f
    friend class FlatImageLevel;
Packit 0d464f
Packit 0d464f
    FlatImageChannel (FlatImageLevel &level,
Packit 0d464f
                      int xSampling,
Packit 0d464f
                      int ySampling,
Packit 0d464f
                      bool pLinear);
Packit 0d464f
Packit 0d464f
    virtual ~FlatImageChannel();
Packit 0d464f
Packit 0d464f
    virtual void            resize ();
Packit 0d464f
Packit 0d464f
    virtual void            resetBasePointer () = 0;
Packit 0d464f
};
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
class TypedFlatImageChannel: public FlatImageChannel
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
    // 
Packit 0d464f
Packit 0d464f
    virtual Slice       slice () const;
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //
Packit 0d464f
    // Access to the pixel at pixel space location (x, y), without
Packit 0d464f
    // bounds checking.  Accessing a location outside the data window
Packit 0d464f
    // of the image level results in undefined behavior.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    T &                 operator () (int x, int y);
Packit 0d464f
    const T &           operator () (int x, int y) const;
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //
Packit 0d464f
    // Access to the pixel at pixel space location (x, y), with bounds
Packit 0d464f
    // checking.  Accessing a location outside the data window of the
Packit 0d464f
    // image level throws an Iex::ArgExc exception.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    T &                 at (int x, int y);
Packit 0d464f
    const T &           at (int x, int y) const;
Packit 0d464f
Packit 0d464f
    //
Packit 0d464f
    // Faster access to all pixels in a single horizontal row of the
Packit 0d464f
    // 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
Packit 0d464f
    // pixels results in undefined behavior.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    T *                 row (int r);
Packit 0d464f
    const T *           row (int r) const;
Packit 0d464f
Packit 0d464f
  private:
Packit 0d464f
    
Packit 0d464f
    friend class FlatImageLevel;
Packit 0d464f
Packit 0d464f
    //
Packit 0d464f
    // The constructor and destructor are not public because flat
Packit 0d464f
    // image channels exist only as parts of a flat image level.
Packit 0d464f
    //
Packit 0d464f
Packit 0d464f
    TypedFlatImageChannel (FlatImageLevel &level,
Packit 0d464f
                           int xSampling,
Packit 0d464f
                           int ySampling,
Packit 0d464f
                           bool pLinear);
Packit 0d464f
Packit 0d464f
    virtual ~TypedFlatImageChannel ();
Packit 0d464f
Packit 0d464f
    virtual void        resize ();
Packit 0d464f
Packit 0d464f
    virtual void        resetBasePointer ();
Packit 0d464f
Packit 0d464f
    T *                 _pixels;        // Pointer to allocated storage
Packit 0d464f
    T *                 _base;          // Base pointer for faster pixel access
Packit 0d464f
};
Packit 0d464f
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// Channel typedefs for the pixel data types supported by OpenEXR.
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
typedef TypedFlatImageChannel<half>         FlatHalfChannel;
Packit 0d464f
typedef TypedFlatImageChannel<float>        FlatFloatChannel;
Packit 0d464f
typedef TypedFlatImageChannel<unsigned int> FlatUIntChannel;
Packit 0d464f
Packit 0d464f
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
// Implementation of templates and inline functions
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
TypedFlatImageChannel<T>::TypedFlatImageChannel
Packit 0d464f
    (FlatImageLevel &level,
Packit 0d464f
     int xSampling,
Packit 0d464f
     int ySampling,
Packit 0d464f
     bool pLinear)
Packit 0d464f
:
Packit 0d464f
    FlatImageChannel (level, xSampling, ySampling, pLinear),
Packit 0d464f
    _pixels (0),
Packit 0d464f
    _base (0)
Packit 0d464f
{
Packit 0d464f
    resize();
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
TypedFlatImageChannel<T>::~TypedFlatImageChannel ()
Packit 0d464f
{
Packit 0d464f
    delete [] _pixels;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <>
Packit 0d464f
inline PixelType
Packit 0d464f
FlatHalfChannel::pixelType () const
Packit 0d464f
{
Packit 0d464f
    return HALF;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <>
Packit 0d464f
inline PixelType
Packit 0d464f
FlatFloatChannel::pixelType () const
Packit 0d464f
{
Packit 0d464f
    return FLOAT;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <>
Packit 0d464f
inline PixelType
Packit 0d464f
FlatUIntChannel::pixelType () const
Packit 0d464f
{
Packit 0d464f
    return UINT;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
Slice
Packit 0d464f
TypedFlatImageChannel<T>::slice () const
Packit 0d464f
{
Packit 0d464f
    return Slice (pixelType(),                 // type
Packit 0d464f
                  (char *) _base,              // base
Packit 0d464f
                  sizeof (T),                  // xStride
Packit 0d464f
                  pixelsPerRow() * sizeof (T), // yStride
Packit 0d464f
                  xSampling(),
Packit 0d464f
                  ySampling());
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline T &
Packit 0d464f
TypedFlatImageChannel<T>::operator () (int x, int y)
Packit 0d464f
{
Packit 0d464f
    return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline const T &
Packit 0d464f
TypedFlatImageChannel<T>::operator () (int x, int y) const
Packit 0d464f
{
Packit 0d464f
    return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline T &
Packit 0d464f
TypedFlatImageChannel<T>::at (int x, int y)
Packit 0d464f
{
Packit 0d464f
    boundsCheck (x, y);
Packit 0d464f
    return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline const T &
Packit 0d464f
TypedFlatImageChannel<T>::at (int x, int y) const
Packit 0d464f
{
Packit 0d464f
    boundsCheck (x, y);
Packit 0d464f
    return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline T *
Packit 0d464f
TypedFlatImageChannel<T>::row (int r)
Packit 0d464f
{
Packit 0d464f
    return _base + r * pixelsPerRow();
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
inline const T *
Packit 0d464f
TypedFlatImageChannel<T>::row (int n) const
Packit 0d464f
{
Packit 0d464f
    return _base + n * pixelsPerRow();
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
void
Packit 0d464f
TypedFlatImageChannel<T>::resize ()
Packit 0d464f
{
Packit 0d464f
    delete [] _pixels;
Packit 0d464f
    _pixels = 0;
Packit 0d464f
Packit 0d464f
    FlatImageChannel::resize();  // may throw an exception
Packit 0d464f
Packit 0d464f
    _pixels = new T [numPixels()];
Packit 0d464f
Packit 0d464f
    for (size_t i = 0; i < numPixels(); ++i)
Packit 0d464f
        _pixels[i] = T (0);
Packit 0d464f
Packit 0d464f
    resetBasePointer ();
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
template <class T>
Packit 0d464f
void
Packit 0d464f
TypedFlatImageChannel<T>::resetBasePointer ()
Packit 0d464f
{
Packit 0d464f
    _base = _pixels -
Packit 0d464f
            (level().dataWindow().min.y / ySampling()) * pixelsPerRow() -
Packit 0d464f
            (level().dataWindow().min.x / xSampling());
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Packit 0d464f
Packit 0d464f
#endif