TuttleOFX  1
OfxhImage.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License :
00003  *
00004  * Copyright (c) 2007-2009, The Open Effects Association Ltd. All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * Redistributions of source code must retain the above copyright notice,
00010  *    this list of conditions and the following disclaimer.
00011  * Redistributions in binary form must reproduce the above copyright notice,
00012  *    this list of conditions and the following disclaimer in the documentation
00013  *    and/or other materials provided with the distribution.
00014  * Neither the name The Open Effects Association Ltd, nor the names of its
00015  *    contributors may be used to endorse or promote products derived from this
00016  *    software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00022  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00025  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00027  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 // ofx host
00031 #include "OfxhBinary.hpp"
00032 #include "OfxhImageEffectNode.hpp"
00033 #include "property/OfxhSet.hpp"
00034 #include "attribute/OfxhClip.hpp"
00035 
00036 // ofx
00037 #include <ofxCore.h>
00038 
00039 namespace tuttle {
00040 namespace host {
00041 namespace ofx {
00042 namespace imageEffect {
00043 
00044 std::ptrdiff_t OfxhImage::_count = 0;
00045 
00046 static property::OfxhPropSpec imageStuffs[] = {
00047         { kOfxPropType, property::ePropTypeString, 1, false, kOfxTypeImage },
00048         { kOfxImageEffectPropPixelDepth, property::ePropTypeString, 1, true, kOfxBitDepthNone },
00049         { kOfxImageEffectPropComponents, property::ePropTypeString, 1, true, kOfxImageComponentNone },
00050         { kOfxImageEffectPropPreMultiplication, property::ePropTypeString, 1, true, kOfxImageOpaque },
00051         { kOfxImageEffectPropRenderScale, property::ePropTypeDouble, 2, true, "1.0" },
00052         { kOfxImagePropPixelAspectRatio, property::ePropTypeDouble, 1, true, "1.0" },
00053         { kOfxImagePropData, property::ePropTypePointer, 1, true, NULL },
00054         { kOfxImagePropBounds, property::ePropTypeInt, 4, true, "0" },
00055         { kOfxImagePropRegionOfDefinition, property::ePropTypeInt, 4, true, "0", },
00056         { kOfxImagePropRowBytes, property::ePropTypeInt, 1, true, "0", },
00057         { kOfxImagePropField, property::ePropTypeString, 1, true, "", },
00058         { kOfxImagePropUniqueIdentifier, property::ePropTypeString, 1, true, "" },
00059         { 0 }
00060 };
00061 
00062 OfxhImage::OfxhImage()
00063         : property::OfxhSet( imageStuffs )
00064         , _id( _count++ )
00065         , _clipName( "No clip !" )
00066         , _time( 0 )
00067 {
00068         TUTTLE_TLOG( TUTTLE_INFO, "[Ofxh Image] create clip:" << getClipName()
00069                 << ", time:" << getTime()
00070                 << ", id:" << getId()
00071                 << ", ref host:" << getReferenceCount( ofx::imageEffect::OfxhImage::eReferenceOwnerHost )
00072                 << ", ref plugins:" << getReferenceCount( ofx::imageEffect::OfxhImage::eReferenceOwnerPlugin ) );
00073 }
00074 
00075 /**
00076  * make an image from a clip instance
00077  */
00078 OfxhImage::OfxhImage( attribute::OfxhClip& instance, const OfxTime time )
00079         : property::OfxhSet( imageStuffs )
00080         , _id( _count++ )
00081         , _clipName( instance.getName() )
00082         , _time( time )
00083 {
00084         TUTTLE_TLOG( TUTTLE_INFO, "[Ofxh Image] create clip:" << getClipName()
00085                 << ", time:" << getTime()
00086                 << ", id:" << getId()
00087                 << ", ref host:" << getReferenceCount( ofx::imageEffect::OfxhImage::eReferenceOwnerHost )
00088                 << ", ref plugins:" << getReferenceCount( ofx::imageEffect::OfxhImage::eReferenceOwnerPlugin ) );
00089         initClipBits( instance );
00090 }
00091 
00092 /**
00093  * construction based on clip instance
00094  *
00095 OfxhImage::OfxhImage( attribute::OfxhClip& instance,
00096                                           OfxTime              time,
00097                       double               renderScaleX,
00098                       double               renderScaleY,
00099                       void*                data,
00100                       const OfxRectI&      bounds,
00101                       const OfxRectI&      rod,
00102                       int                  rowBytes,
00103                       const std::string&   field,
00104                       const std::string&   uniqueIdentifier )
00105         : property::OfxhSet( imageStuffs )
00106         , _id( _count++ )
00107         , _clipName( instance.getName() )
00108         , _time( time )
00109 {
00110         TUTTLE_TLOG( TUTTLE_INFO, "++ OfxhImage, clipName:" << getClipName()
00111                 << ", time:" << getTime()
00112                 << ", id:" << getId()
00113                 << ", ref host:" << getReferenceCount( ofx::imageEffect::OfxhImage::eReferenceOwnerHost )
00114                 << ", ref plugins:" << getReferenceCount( ofx::imageEffect::OfxhImage::eReferenceOwnerPlugin ) );
00115         initClipBits( instance );
00116 
00117         // set other data
00118         setDoubleProperty( kOfxImageEffectPropRenderScale, renderScaleX, 0 );
00119         setDoubleProperty( kOfxImageEffectPropRenderScale, renderScaleY, 1 );
00120         setPointerProperty( kOfxImagePropData, data );
00121         setIntProperty( kOfxImagePropBounds, bounds.x1, 0 );
00122         setIntProperty( kOfxImagePropBounds, bounds.y1, 1 );
00123         setIntProperty( kOfxImagePropBounds, bounds.x2, 2 );
00124         setIntProperty( kOfxImagePropBounds, bounds.y2, 3 );
00125         setIntProperty( kOfxImagePropRegionOfDefinition, rod.x1, 0 );
00126         setIntProperty( kOfxImagePropRegionOfDefinition, rod.y1, 1 );
00127         setIntProperty( kOfxImagePropRegionOfDefinition, rod.x2, 2 );
00128         setIntProperty( kOfxImagePropRegionOfDefinition, rod.y2, 3 );
00129         setIntProperty( kOfxImagePropRowBytes, rowBytes );
00130 
00131         setStringProperty( kOfxImagePropField, field );
00132         setStringProperty( kOfxImageClipPropFieldOrder, field );
00133         setStringProperty( kOfxImagePropUniqueIdentifier, uniqueIdentifier );
00134 }
00135 */
00136 
00137 OfxhImage::~OfxhImage()
00138 {
00139         TUTTLE_TLOG( TUTTLE_INFO, "[Ofxh Image] delete clip:" << getClipName()
00140                 << ", time:" << getTime()
00141                 << ", id:" << getId()
00142                 << ", ref host:" << getReferenceCount( eReferenceOwnerHost )
00143                 << ", ref plugins:" << getReferenceCount( eReferenceOwnerPlugin ) );
00144         BOOST_ASSERT( getReferenceCount(eReferenceOwnerHost) == 0 );
00145 }
00146 
00147 /// called during ctor to get bits from the clip props into ours
00148 
00149 void OfxhImage::initClipBits( attribute::OfxhClip& instance )
00150 {
00151         const property::OfxhSet& clipProperties = instance.getProperties();
00152 
00153         // get and set the clip instance pixel depth
00154         const std::string& depth = clipProperties.getStringProperty( kOfxImageEffectPropPixelDepth );
00155 
00156         setStringProperty( kOfxImageEffectPropPixelDepth, depth );
00157 
00158         // get and set the clip instance components
00159         const std::string& comps = clipProperties.getStringProperty( kOfxImageEffectPropComponents );
00160         setStringProperty( kOfxImageEffectPropComponents, comps );
00161 
00162         // get and set the clip instance premultiplication
00163         setStringProperty( kOfxImageEffectPropPreMultiplication, clipProperties.getStringProperty( kOfxImageEffectPropPreMultiplication ) );
00164 
00165         // get and set the clip instance pixel aspect ratio
00166         setDoubleProperty( kOfxImagePropPixelAspectRatio, clipProperties.getDoubleProperty( kOfxImagePropPixelAspectRatio ) );
00167 }
00168 
00169 OfxRectI OfxhImage::getBounds() const
00170 {
00171         OfxRectI bounds;
00172         getIntPropertyN( kOfxImagePropBounds, &bounds.x1, 4 );
00173         return bounds;
00174 }
00175 
00176 OfxRectI OfxhImage::getROD() const
00177 {
00178         OfxRectI rod;
00179         getIntPropertyN( kOfxImagePropRegionOfDefinition, &rod.x1, 4 );
00180         return rod;
00181 }
00182 
00183 EBitDepth OfxhImage::getBitDepth() const
00184 {
00185         std::string depth  = getStringProperty( kOfxImageEffectPropPixelDepth );
00186         EBitDepth bitDepth = eBitDepthNone;
00187 
00188         if( depth == kOfxBitDepthByte )
00189         {
00190                 bitDepth = eBitDepthUByte;
00191         }
00192         else if( depth == kOfxBitDepthShort )
00193         {
00194                 bitDepth = eBitDepthUShort;
00195         }
00196         else if( depth == kOfxBitDepthFloat )
00197         {
00198                 bitDepth = eBitDepthFloat;
00199         }
00200         return bitDepth;
00201 }
00202 
00203 EPixelComponent OfxhImage::getComponentsType() const
00204 {
00205         std::string sType        = getStringProperty( kOfxImageEffectPropComponents );
00206         EPixelComponent compType = ePixelComponentNone;
00207 
00208         if( sType == kOfxImageComponentRGBA )
00209         {
00210                 compType = ePixelComponentRGBA;
00211         }
00212         else if( sType == kOfxImageComponentRGB )
00213         {
00214                 compType = ePixelComponentRGB;
00215         }
00216         else if( sType == kOfxImageComponentAlpha )
00217         {
00218                 compType = ePixelComponentAlpha;
00219         }
00220         return compType;
00221 }
00222 
00223 int OfxhImage::getRowBytes() const
00224 {
00225         return getIntProperty( kOfxImagePropRowBytes );
00226 }
00227 
00228 int OfxhImage::getReferenceCount( const EReferenceOwner from ) const
00229 {
00230         RefMap::const_iterator it = _referenceCount.find(from);
00231         if( it == _referenceCount.end() )
00232                 return 0;
00233         return it->second;
00234 }
00235 
00236 void OfxhImage::addReference( const EReferenceOwner from, const std::size_t n )
00237 {
00238         const std::ptrdiff_t refC = _referenceCount[from] = getReferenceCount(from) + n;
00239         TUTTLE_TLOG( TUTTLE_INFO, "[Ofxh Image] add reference with degree " << n << ", clipName:" << getClipName() << ", time:" << getTime() << ", id:" << getId() << ", ref:" << refC );
00240 }
00241 
00242 bool OfxhImage::releaseReference( const EReferenceOwner from )
00243 {
00244         const std::ptrdiff_t refC = _referenceCount[from] = getReferenceCount(from) - 1;
00245         TUTTLE_TLOG( TUTTLE_INFO, "[Ofxh Image] release reference, clipName:" << getClipName() << ", time:" << getTime() << ", id:" << getId() << ", ref:" << refC );
00246         if( refC < 0 )
00247                 BOOST_THROW_EXCEPTION( std::logic_error( "Try to release an undeclared reference to an Image." ) );
00248         return refC <= 0;
00249 }
00250 
00251 }
00252 }
00253 }
00254 }