Blame IlmImfUtil/ImfImage.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_IMAGE_H
Packit Service 6754ca
#define INCLUDED_IMF_IMAGE_H
Packit Service 6754ca
Packit Service 6754ca
//----------------------------------------------------------------------------
Packit Service 6754ca
//
Packit Service 6754ca
// class Image -- an in-memory data structure that can hold an arbitrary
Packit Service 6754ca
// OpenEXR image, flat or deep, with one or multiple resolution levels,
Packit Service 6754ca
// and with an arbitrary set of channels.
Packit Service 6754ca
// 
Packit Service 6754ca
// An image is a container for a set of image levels, and an image level
Packit Service 6754ca
// is a container for a set of image channels.  An image channel contains
Packit Service 6754ca
// an array of pixel values of type half, float or unsigned int.
Packit Service 6754ca
// 
Packit Service 6754ca
// For example:
Packit Service 6754ca
// 
Packit Service 6754ca
//     image --+-- level 0 --+-- channel "R" --- pixel data
Packit Service 6754ca
//             |             |
Packit Service 6754ca
//             |             +-- channel "G" --- pixel data
Packit Service 6754ca
//             |             |
Packit Service 6754ca
//             |             +-- channel "B" --- pixel data
Packit Service 6754ca
//             |
Packit Service 6754ca
//             +-- level 1 --+-- channel "R" --- pixel data
Packit Service 6754ca
//             |             |
Packit Service 6754ca
//             |             +-- channel "G" --- pixel data
Packit Service 6754ca
//             |             |
Packit Service 6754ca
//             |             +-- channel "B" --- pixel data
Packit Service 6754ca
//             |
Packit Service 6754ca
//             +-- level 2 --+-- channel "R" --- pixel data
Packit Service 6754ca
//                           |
Packit Service 6754ca
//                           +-- channel "G" --- pixel data
Packit Service 6754ca
//                           |
Packit Service 6754ca
//                           +-- channel "B" --- pixel data
Packit Service 6754ca
// 
Packit Service 6754ca
// An image has a level mode, which can be ONE_LEVEL, MIPMAP_LEVELS or
Packit Service 6754ca
// RIPMAP_LEVELS, and a level rounding mode, which can be ROUND_UP or
Packit Service 6754ca
// ROUND_DOWN.  Together, the level mode and the level rounding mode
Packit Service 6754ca
// determine how many levels an image contains, and how large the data
Packit Service 6754ca
// window for each level is.  All levels in an image have the same set
Packit Service 6754ca
// of channels.
Packit Service 6754ca
// 
Packit Service 6754ca
// An image channel has a name (e.g. "R", "Z", or "xVelocity"), a type
Packit Service 6754ca
// (HALF, FLOAT or UINT) and x and y sampling rates.  A channel stores
Packit Service 6754ca
// samples for a pixel if the pixel is inside the data window of the
Packit Service 6754ca
// level to which the channel belongs, and the x and y coordinates of
Packit Service 6754ca
// the pixel are divisible by the x and y sampling rates of the channel.
Packit Service 6754ca
//
Packit Service 6754ca
// An image can be either flat or deep.  In a flat image each channel
Packit Service 6754ca
// in each level stores at most one value per pixel.  In a deep image
Packit Service 6754ca
// each channel in each level stores an arbitrary number of values per
Packit Service 6754ca
// pixel.  As an exception, each level of a deep image has a sample count
Packit Service 6754ca
// channel with a single value per pixel; this value determines how many
Packit Service 6754ca
// values each of the other channels in the same level has at the same
Packit Service 6754ca
// pixel location.
Packit Service 6754ca
//
Packit Service 6754ca
// The classes Image, ImageLevel and ImageChannel are abstract base
Packit Service 6754ca
// classes.  Two sets of concrete classes, one for flat and one for
Packit Service 6754ca
// deep images, are derived from the base classes.
Packit Service 6754ca
//
Packit Service 6754ca
//----------------------------------------------------------------------------
Packit Service 6754ca
Packit Service 6754ca
#include "ImfImageLevel.h"
Packit Service 6754ca
#include <ImfTileDescription.h>
Packit Service 6754ca
#include <ImfArray.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 Channel;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
class IMF_EXPORT Image
Packit Service 6754ca
{
Packit Service 6754ca
  public:
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Constructor and destructor
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    Image ();
Packit Service 6754ca
    virtual ~Image ();
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Access to the image's level mode and level rounding mode.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
	LevelMode               levelMode() const;
Packit Service 6754ca
	LevelRoundingMode       levelRoundingMode() const;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Number of levels:
Packit Service 6754ca
    //
Packit Service 6754ca
    // numXLevels() returns the image's number of levels in the x direction.
Packit Service 6754ca
    //
Packit Service 6754ca
    //	if levelMode() == ONE_LEVEL:
Packit Service 6754ca
    //      return value is: 1
Packit Service 6754ca
    //
Packit Service 6754ca
    //	if levelMode() == MIPMAP_LEVELS:
Packit Service 6754ca
    //      return value is: rfunc (log (max (w, h)) / log (2)) + 1
Packit Service 6754ca
    //
Packit Service 6754ca
    //	if levelMode() == RIPMAP_LEVELS:
Packit Service 6754ca
    //      return value is: rfunc (log (w) / log (2)) + 1
Packit Service 6754ca
    //
Packit Service 6754ca
    //	where
Packit Service 6754ca
    //	    w is the width of the image's data window,  max.x - min.x + 1,
Packit Service 6754ca
    //	    h is the height of the image's data window, max.y - min.y + 1,
Packit Service 6754ca
    //	    and rfunc(x) is either floor(x), or ceil(x), depending on
Packit Service 6754ca
    //	    whether levelRoundingMode() returns ROUND_DOWN or ROUND_UP.
Packit Service 6754ca
    //
Packit Service 6754ca
    // numYLevels() returns the image's number of levels in the y direction.
Packit Service 6754ca
    //
Packit Service 6754ca
    //	if levelMode() == ONE_LEVEL or levelMode() == MIPMAP_LEVELS:
Packit Service 6754ca
    //      return value is the same as for numXLevels()
Packit Service 6754ca
    //
Packit Service 6754ca
    //	if levelMode() == RIPMAP_LEVELS:
Packit Service 6754ca
    //      return value is: rfunc (log (h) / log (2)) + 1
Packit Service 6754ca
    //
Packit Service 6754ca
    //
Packit Service 6754ca
    // numLevels() is a convenience function for use with MIPMAP_LEVELS images.
Packit Service 6754ca
    //
Packit Service 6754ca
    //	if levelMode() == ONE_LEVEL or levelMode() == MIPMAP_LEVELS:
Packit Service 6754ca
    //      return value is the same as for numXLevels()
Packit Service 6754ca
    //
Packit Service 6754ca
    //	if levelMode() == RIPMAP_LEVELS:
Packit Service 6754ca
    //      a LogicExc exception is thrown
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
	int                     numLevels() const;
Packit Service 6754ca
	int                     numXLevels() const;
Packit Service 6754ca
	int                     numYLevels() const;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Per-level data windows
Packit Service 6754ca
    //
Packit Service 6754ca
    // dataWindow() returns the data window for the image; this is the
Packit Service 6754ca
    // same as the data window for the level with level number (0, 0).
Packit Service 6754ca
    //
Packit Service 6754ca
    // dataWindowForLevel(lx, ly) returns the data window for level x,
Packit Service 6754ca
    // that is, the window for which the image level with level number
Packit Service 6754ca
    // (lx, ly) has allocated pixel storage.
Packit Service 6754ca
    //
Packit Service 6754ca
    //	return value is a Box2i with min value:
Packit Service 6754ca
    //      (dataWindow().min.x,
Packit Service 6754ca
    //       dataWindow().min.y)
Packit Service 6754ca
    //
Packit Service 6754ca
    //	and max value:
Packit Service 6754ca
    //      (dataWindow().min.x + levelWidth(lx) - 1,
Packit Service 6754ca
    //       dataWindow().min.y + levelHeight(ly) - 1)
Packit Service 6754ca
    //
Packit Service 6754ca
    // dataWindowForLevel(l) is a convenience function used for ONE_LEVEL
Packit Service 6754ca
    // and MIPMAP_LEVELS files.  It returns dataWindowForLevel(l,l)).
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
	const IMATH_NAMESPACE::Box2i &  dataWindow() const;
Packit Service 6754ca
	const IMATH_NAMESPACE::Box2i &  dataWindowForLevel(int l) const;
Packit Service 6754ca
	const IMATH_NAMESPACE::Box2i &  dataWindowForLevel(int lx, int ly) const;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Size of a level:
Packit Service 6754ca
    //
Packit Service 6754ca
    // levelWidth(lx) returns the width of a level with level
Packit Service 6754ca
    // number (lx, *), where * is any number.
Packit Service 6754ca
    //
Packit Service 6754ca
    //	return value is:
Packit Service 6754ca
    //      max (1, rfunc (w / pow (2, lx)))
Packit Service 6754ca
    //
Packit Service 6754ca
    //
Packit Service 6754ca
    // levelHeight(ly) returns the height of a level with level
Packit Service 6754ca
    // number (*, ly), where * is any number.
Packit Service 6754ca
    //
Packit Service 6754ca
    //	return value is:
Packit Service 6754ca
    //      max (1, rfunc (h / pow (2, ly)))
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    int			    levelWidth  (int lx) const;
Packit Service 6754ca
    int			    levelHeight (int ly) const;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Resize the image:
Packit Service 6754ca
    //
Packit Service 6754ca
    // resize(dw,lm,lrm) sets the data window of the image to dw,
Packit Service 6754ca
    // sets the level mode to lm and the level rounding mode to lrm,
Packit Service 6754ca
    // and allocates new storage for image levels and image channels.
Packit Service 6754ca
    // The set of channels in the image does not change.
Packit Service 6754ca
    //
Packit Service 6754ca
    // The contents of the image are lost; pixel data are not preserved
Packit Service 6754ca
    // across the resize operation.  If resizing fails, then the image
Packit Service 6754ca
    // will be left with an empty data window and no image levels.
Packit Service 6754ca
    //
Packit Service 6754ca
    // resize(dw) is the same as resize(dw,levelMode(),levelRoundingMode())
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
	void                    resize(const IMATH_NAMESPACE::Box2i &dataWindow);
Packit Service 6754ca
Packit Service 6754ca
	virtual void            resize(const IMATH_NAMESPACE::Box2i &dataWindow,
Packit Service 6754ca
                                    LevelMode levelMode,
Packit Service 6754ca
                                    LevelRoundingMode levelRoundingMode);
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Shift the pixels and the data window of an image:
Packit Service 6754ca
    //
Packit Service 6754ca
    // shiftPixels(dx,dy) shifts the image by dx pixels horizontally and
Packit Service 6754ca
    // dy pixels vertically.  A pixel at location (x,y) moves to position
Packit Service 6754ca
    // (x+dx, y+dy).  The data window of the image is shifted along with
Packit Service 6754ca
    // the pixels.  No pixel data are lost.
Packit Service 6754ca
    //
Packit Service 6754ca
    // The horizontal and vertical shift distances must be multiples of
Packit Service 6754ca
    // the x and y sampling rates of all image channels.  If they are not,
Packit Service 6754ca
    // shiftPixels() throws an ArgExc exception.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
	void                    shiftPixels(int dx, int dy);
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Insert a new channel into the image.
Packit Service 6754ca
    //
Packit Service 6754ca
    // The arguments to this function are the same as for adding a
Packit Service 6754ca
    // a channel to an OpenEXR file: channel name, x and y sampling
Packit Service 6754ca
    // rates, and a "perceptually approximately linear" flag.
Packit Service 6754ca
    //
Packit Service 6754ca
    // If the image already contains a channel with the same name
Packit Service 6754ca
    // as the new name then the existing channel is deleted before
Packit Service 6754ca
    // the new channel is added.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    void                    insertChannel (const std::string &name,
Packit Service 6754ca
                                           PixelType type,
Packit Service 6754ca
                                           int xSampling = 1,
Packit Service 6754ca
                                           int ySampling = 1,
Packit Service 6754ca
                                           bool pLinear = false);
Packit Service 6754ca
Packit Service 6754ca
    void                    insertChannel (const std::string &name,
Packit Service 6754ca
                                           const Channel &channel);
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Erase channels from an image:
Packit Service 6754ca
    //
Packit Service 6754ca
    // eraseChannel(n) erases the channel with name n.
Packit Service 6754ca
    // clearChannels() erases all channels.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
	void                    eraseChannel(const std::string &name);
Packit Service 6754ca
	void                    clearChannels();
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Rename an image channel:
Packit Service 6754ca
    //
Packit Service 6754ca
    // renameChannel(nOld,nNew) changes the name of the image channel
Packit Service 6754ca
    // with name nOld to nNew.
Packit Service 6754ca
    //
Packit Service 6754ca
    // If the image already contains a channel called nNew, or if the
Packit Service 6754ca
    // image does not contain a channel called nOld, then renameChannel()
Packit Service 6754ca
    // throws an ArgExc exception.
Packit Service 6754ca
    //
Packit Service 6754ca
    // In the (unlikely) event that renaming the image channel causes
Packit Service 6754ca
    // the program to run out of memory, renameChannel() erases the
Packit Service 6754ca
    // channel that is being renamed, and throws an exception.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
	void                    renameChannel(const std::string &oldName,
Packit Service 6754ca
                                           const std::string &newName);
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Rename multiple image channels at the same time:
Packit Service 6754ca
    //
Packit Service 6754ca
    // Given a map, m, from old to new channel names, renameChannels(m)
Packit Service 6754ca
    // assigns new names to the channels in the image.  If m has an entry
Packit Service 6754ca
    // for a channel named c, then the channel will be renamed to m[c].
Packit Service 6754ca
    // If m has no entry for c, then the channel keeps its old name.
Packit Service 6754ca
    //
Packit Service 6754ca
    // If the same name would be assigned to more than one channel, then
Packit Service 6754ca
    // renameChannels() does not rename any channels but throws an ArgExc
Packit Service 6754ca
    // exception instead.
Packit Service 6754ca
    // 
Packit Service 6754ca
    // In the (unlikely) event that renaming the image channel causes the
Packit Service 6754ca
    // program to run out of memory, renameChannels() erases all channels
Packit Service 6754ca
    // in the image and throws an exception.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
	void                    renameChannels(const RenamingMap &oldToNewNames);
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Accessing image levels by level number.
Packit Service 6754ca
    //
Packit Service 6754ca
    // level(lx,ly) returns a reference to the image level
Packit Service 6754ca
    // with level number (lx,ly).
Packit Service 6754ca
    //
Packit Service 6754ca
    // level(l) returns level(l,l).
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    virtual ImageLevel &            level (int l = 0);
Packit Service 6754ca
    virtual const ImageLevel &      level (int l = 0) const;
Packit Service 6754ca
Packit Service 6754ca
    virtual ImageLevel &            level (int lx, int ly);
Packit Service 6754ca
    virtual const ImageLevel &      level (int lx, int ly) const;
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
  protected:
Packit Service 6754ca
Packit Service 6754ca
    virtual ImageLevel *
Packit Service 6754ca
        newLevel (int lx, int ly, const IMATH_NAMESPACE::Box2i &dataWindow) = 0;
Packit Service 6754ca
Packit Service 6754ca
  private:
Packit Service 6754ca
Packit Service 6754ca
    bool        levelNumberIsValid (int lx, int ly) const;
Packit Service 6754ca
    void        clearLevels ();
Packit Service 6754ca
Packit Service 6754ca
    struct ChannelInfo
Packit Service 6754ca
    {
Packit Service 6754ca
        ChannelInfo (PixelType type = HALF,
Packit Service 6754ca
                     int xSampling = 1,
Packit Service 6754ca
                     int ySampling = 1,
Packit Service 6754ca
                     bool pLinear = false);
Packit Service 6754ca
Packit Service 6754ca
        PixelType   type;
Packit Service 6754ca
        int         xSampling;
Packit Service 6754ca
        int         ySampling;
Packit Service 6754ca
        bool        pLinear;
Packit Service 6754ca
    };
Packit Service 6754ca
Packit Service 6754ca
    typedef std::map <std::string, ChannelInfo> ChannelMap;
Packit Service 6754ca
Packit Service 6754ca
    IMATH_NAMESPACE::Box2i  _dataWindow;
Packit Service 6754ca
    LevelMode               _levelMode;
Packit Service 6754ca
    LevelRoundingMode       _levelRoundingMode;
Packit Service 6754ca
    ChannelMap              _channels;
Packit Service 6754ca
    Array2D<ImageLevel *>   _levels;
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