TuttleOFX  1
BaseTypeConverter.h
Go to the documentation of this file.
00001 // -*- mode: C++; tab-width: 4 -*-
00002 // vi: ts=4
00003 
00004 /*
00005  * Copyright (c) 2009, Patrick A. Palmer.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  *   - Redistributions of source code must retain the above copyright notice,
00012  *     this list of conditions and the following disclaimer.
00013  *
00014  *   - Redistributions in binary form must reproduce the above copyright
00015  *     notice, this list of conditions and the following disclaimer in the
00016  *     documentation and/or other materials provided with the distribution.
00017  *
00018  *   - Neither the name of Patrick A. Palmer nor the names of its
00019  *     contributors may be used to endorse or promote products derived from
00020  *     this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
00026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 
00036 #ifndef _DPX_BASETYPECONVERTER_H
00037 #define _DPX_BASETYPECONVERTER_H 1
00038 
00039 
00040 namespace dpx
00041 {
00042         // convert between all of the DPX base types in a controllable way
00043         
00044         // Note: These bit depth promotions (low precision -> high) use
00045         // a combination of bitshift and the 'or' operator in order to
00046         // fully populate the output coding space
00047         // 
00048         // For example, when converting 8->16 bits, the 'simple' method
00049         // (shifting 8 bits) maps 255 to 65280. This result is not ideal
00050         // (uint16 'max' of 65535 is preferable). Overall, the best conversion
00051         // is one that approximates the 'true' floating-point scale factor.
00052         // 8->16 : 65535.0 / 255.0. 10->16 : 65535.0 / 1023.0.
00053         // For performance considerations, we choose to emulate this
00054         // floating-poing scaling with pure integer math, using a trick
00055         // where we duplicate portions of the MSB in the LSB.
00056         //
00057         // For bit depth demotions, simple truncation is used.
00058         //
00059         
00060         inline void BaseTypeConverter(U8 &src, U8 &dst)
00061         {
00062                 dst = src;
00063         }
00064         
00065         inline void BaseTypeConverter(U8 &src, U16 &dst)
00066         {
00067                 dst = (src << 8) | src;
00068         }
00069         
00070         inline void BaseTypeConverter(U8 &src, U32 &dst)
00071         {
00072                 dst = (src << 24) | (src << 16) | (src << 8) | src;
00073         }
00074         
00075         inline void BaseTypeConverter(U8 &src, R32 &dst)
00076         {
00077                 dst = src;
00078         }
00079         
00080         inline void BaseTypeConverter(U8 &src, R64 &dst)
00081         {
00082                 dst = src;
00083         }
00084         
00085         inline void BaseTypeConverter(U16 &src, U8 &dst)
00086         {
00087                 dst = src >> 8;
00088         }
00089         
00090         inline void BaseTypeConverter(U16 &src, U16 &dst)
00091         {
00092                 dst = src;
00093         }
00094         
00095         inline void BaseTypeConverter(U16 &src, U32 &dst)
00096         {
00097                 dst = (src << 16) | src;
00098         }
00099         
00100         inline void BaseTypeConverter(U16 &src, R32 &dst)
00101         {
00102                 dst = src;
00103         }
00104         
00105         inline void BaseTypeConverter(U16 &src, R64 &dst)
00106         {
00107                 dst = src;
00108         }
00109         
00110         inline void BaseTypeConverter(U32 &src, U8 &dst)
00111         {
00112                 dst = src >> 24;
00113         }
00114         
00115         inline void BaseTypeConverter(U32 &src, U16 &dst)
00116         {
00117                 dst = src >> 16;
00118         }
00119         
00120         inline void BaseTypeConverter(U32 &src, U32 &dst)
00121         {
00122                 dst = src;
00123         }
00124         
00125         inline void BaseTypeConverter(U32 &src, R32 &dst)
00126         {
00127                 dst = src;
00128         }
00129         
00130         inline void BaseTypeConverter(U32 &src, R64 &dst)
00131         {
00132                 dst = src;
00133         }
00134         
00135         inline void BaseTypeConverter(R32 &src, U8 &dst)
00136         {
00137                 dst = src;
00138         }
00139         
00140         inline void BaseTypeConverter(R32 &src, U16 &dst)
00141         {
00142                 dst = src;
00143         }
00144         
00145         inline void BaseTypeConverter(R32 &src, U32 &dst)
00146         {
00147                 dst = src;
00148         }
00149         
00150         inline void BaseTypeConverter(R32 &src, R32 &dst)
00151         {
00152                 dst = src;
00153         }
00154         
00155         inline void BaseTypeConverter(R32 &src, R64 &dst)
00156         {
00157                 dst = src;
00158         }
00159         
00160         inline void BaseTypeConverter(R64 &src, U8 &dst)
00161         {
00162                 dst = src;
00163         }
00164         
00165         inline void BaseTypeConverter(R64 &src, U16 &dst)
00166         {
00167                 dst = src;
00168         }
00169         
00170         inline void BaseTypeConverter(R64 &src, U32 &dst)
00171         {
00172                 dst = src;
00173         }
00174         
00175         inline void BaseTypeConverter(R64 &src, R32 &dst)
00176         {
00177                 dst = src;
00178         }
00179         
00180         inline void BaseTypeConverter(R64 &src, R64 &dst)
00181         {
00182                 dst = src;
00183         }
00184         
00185         inline void BaseTypeConvertU10ToU16(U16 &src, U16 &dst)
00186         {
00187                 dst = (src << 6) | (src >> 4);
00188         }
00189         
00190         inline void BaseTypeConvertU12ToU16(U16 &src, U16 &dst)
00191         {
00192                 dst = (src << 4) | (src >> 8);
00193         }
00194         
00195 }
00196 
00197 #endif
00198 
00199