|
Packit |
0d464f |
///////////////////////////////////////////////////////////////////////////
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Copyright (c) 2004, 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_IMAGE_H
|
|
Packit |
0d464f |
#define INCLUDED_IMAGE_H
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
//----------------------------------------------------------------------------
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Classes for storing OpenEXR images in memory.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
//----------------------------------------------------------------------------
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
#include <ImfPixelType.h>
|
|
Packit |
0d464f |
#include <ImfFrameBuffer.h>
|
|
Packit |
0d464f |
#include <ImfArray.h>
|
|
Packit |
0d464f |
#include <ImathBox.h>
|
|
Packit |
0d464f |
#include <half.h>
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
#include <string>
|
|
Packit |
0d464f |
#include <map>
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
#include "namespaceAlias.h"
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
class Image;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
class ImageChannel
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
public:
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
friend class Image;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
ImageChannel (Image &image);
|
|
Packit |
0d464f |
virtual ~ImageChannel();
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
virtual IMF::Slice slice () const = 0;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
Image & image () {return _image;}
|
|
Packit |
0d464f |
const Image & image () const {return _image;}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
private:
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
virtual void resize (int width, int height) = 0;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
Image & _image;
|
|
Packit |
0d464f |
};
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
class TypedImageChannel: public ImageChannel
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
public:
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
TypedImageChannel (Image &image, int width, int height);
|
|
Packit |
0d464f |
virtual ~TypedImageChannel ();
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
IMF::PixelType pixelType () const;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
virtual IMF::Slice slice () const;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
T & operator () (int x, int y);
|
|
Packit |
0d464f |
const T & operator () (int x, int y) const;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
private:
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
virtual void resize (int width, int height);
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
IMF::Array2D<T> _pixels;
|
|
Packit |
0d464f |
};
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
typedef TypedImageChannel<half> HalfChannel;
|
|
Packit |
0d464f |
typedef TypedImageChannel<float> FloatChannel;
|
|
Packit |
0d464f |
typedef TypedImageChannel<unsigned int> UIntChannel;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
class Image
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
public:
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
Image ();
|
|
Packit |
0d464f |
Image (const IMATH_NAMESPACE::Box2i &dataWindow);
|
|
Packit |
0d464f |
~Image ();
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
const IMATH_NAMESPACE::Box2i & dataWindow () const;
|
|
Packit |
0d464f |
void resize (const IMATH_NAMESPACE::Box2i &dataWindow);
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
int width () const;
|
|
Packit |
0d464f |
int height () const;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
void addChannel (const std::string &name,
|
|
Packit |
0d464f |
IMF::PixelType type);
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
ImageChannel & channel (const std::string &name);
|
|
Packit |
0d464f |
const ImageChannel & channel (const std::string &name) const;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
TypedImageChannel<T> & typedChannel (const std::string &name);
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
const TypedImageChannel<T> & typedChannel (const std::string &name) const;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
private:
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
typedef std::map <std::string, ImageChannel *> ChannelMap;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
IMATH_NAMESPACE::Box2i _dataWindow;
|
|
Packit |
0d464f |
ChannelMap _channels;
|
|
Packit |
0d464f |
};
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Implementation of templates and inline functions.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
TypedImageChannel<T>::TypedImageChannel (Image &image, int width, int height):
|
|
Packit |
0d464f |
ImageChannel (image),
|
|
Packit |
0d464f |
_pixels (height, width)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
// empty
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
TypedImageChannel<T>::~TypedImageChannel ()
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
// empty
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <>
|
|
Packit |
0d464f |
inline OPENEXR_IMF_INTERNAL_NAMESPACE::PixelType
|
|
Packit |
0d464f |
HalfChannel::pixelType () const
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return OPENEXR_IMF_INTERNAL_NAMESPACE::HALF;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <>
|
|
Packit |
0d464f |
inline OPENEXR_IMF_INTERNAL_NAMESPACE::PixelType
|
|
Packit |
0d464f |
FloatChannel::pixelType () const
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <>
|
|
Packit |
0d464f |
inline OPENEXR_IMF_INTERNAL_NAMESPACE::PixelType
|
|
Packit |
0d464f |
UIntChannel::pixelType () const
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return OPENEXR_IMF_INTERNAL_NAMESPACE::UINT;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
OPENEXR_IMF_INTERNAL_NAMESPACE::Slice
|
|
Packit |
0d464f |
TypedImageChannel<T>::slice () const
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
const IMATH_NAMESPACE::Box2i &dw = image().dataWindow();
|
|
Packit |
0d464f |
int w = dw.max.x - dw.min.x + 1;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
return OPENEXR_IMF_INTERNAL_NAMESPACE::Slice (pixelType(),
|
|
Packit |
0d464f |
(char *) (&_pixels[0][0] - dw.min.y * w - dw.min.x),
|
|
Packit |
0d464f |
sizeof (T),
|
|
Packit |
0d464f |
w * sizeof (T));
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
inline const T &
|
|
Packit |
0d464f |
TypedImageChannel<T>::operator () (int x, int y) const
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return _pixels[y][x];
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
inline T &
|
|
Packit |
0d464f |
TypedImageChannel<T>::operator () (int x, int y)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return _pixels[y][x];
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
void
|
|
Packit |
0d464f |
TypedImageChannel<T>::resize (int width, int height)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
_pixels.resizeEraseUnsafe (height, width);
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
inline const IMATH_NAMESPACE::Box2i &
|
|
Packit |
0d464f |
Image::dataWindow () const
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return _dataWindow;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
inline int
|
|
Packit |
0d464f |
Image::width () const
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return _dataWindow.max.x - _dataWindow.min.x + 1;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
inline int
|
|
Packit |
0d464f |
Image::height () const
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return _dataWindow.max.y - _dataWindow.min.y + 1;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
TypedImageChannel<T> &
|
|
Packit |
0d464f |
Image::typedChannel (const std::string &name)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return dynamic_cast <TypedImageChannel<T>&> (channel (name));
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
template <class T>
|
|
Packit |
0d464f |
const TypedImageChannel<T> &
|
|
Packit |
0d464f |
Image::typedChannel (const std::string &name) const
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return dynamic_cast <const TypedImageChannel<T>&> (channel (name));
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
#endif
|