include/cinder/ChanTraits.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, The Cinder Project
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00006  the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00009     the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
00012 
00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00020  POSSIBILITY OF SUCH DAMAGE.
00021 */
00022 
00023 #pragma once
00024 
00025 #include "cinder/Cinder.h"
00026 
00027 #include <boost/preprocessor/seq.hpp>
00028 
00029 namespace cinder {
00030 
00031 template<typename T>
00032 struct CHANTRAIT 
00033 {
00034 };
00035 
00036 template<>
00037 struct CHANTRAIT<uint8_t>
00038 {
00039     typedef uint32_t Sum;
00040     typedef uint32_t Accum;
00041     typedef int32_t SignedSum;
00042     static uint8_t max() { return 255; }
00043     static uint8_t convert( uint8_t v ) { return v; }
00044     static uint8_t convert( uint16_t v ) { return v / 257; }    
00045     static uint8_t convert( float v ) { return static_cast<uint8_t>( v * 255 ); }
00046     static uint8_t grayscale( uint8_t r, uint8_t g, uint8_t b ) { return ( r * 54 + g * 183 + b * 19 ) >> 8; } // luma coefficients from Rec. 709
00047     static uint8_t premultiply( uint8_t c, uint8_t a ) { return a * c / 255; }
00048     //static uint8_t premultiply( uint8_t c, uint8_t a ) { uint16_t t = c * a + 0x80f; return ( ( t >> 8 ) + t ) >> 8; }
00049     static uint8_t inverse( uint8_t c ) { return ~c; }
00050 };
00051 
00052 template<>
00053 struct CHANTRAIT<uint16_t>
00054 {
00055     typedef uint32_t Sum;
00056     typedef uint32_t Accum;
00057     typedef int32_t SignedSum;
00058     static uint16_t max() { return 65535; }
00059     static uint16_t convert( uint8_t v ) { return ( v << 8 ) | v; }
00060     static uint16_t convert( uint16_t v ) { return v; } 
00061     static uint16_t convert( float v ) { return static_cast<uint16_t>( v * 65535 ); }
00062     static uint16_t grayscale( uint16_t r, uint16_t g, uint16_t b ) { return ( r * 6966 + g * 23436 + b * 2366 ) >> 15; } // luma coefficients from Rec. 709
00063 };
00064 
00065 template<>
00066 struct CHANTRAIT<float>
00067 {
00068     typedef float Sum;
00069     typedef float Accum;
00070     typedef float SignedSum;
00071     static float max() { return 1.0f; }
00072     static float convert( uint8_t v ) { return v / 255.0f; }
00073     static float convert( uint16_t v ) { return v / 65535.0f; }
00074     static float convert( float v ) { return v; }
00075     static float grayscale( float r, float g, float b ) { return r * 0.2126f + g * 0.7152f + b * 0.0722f; } // luma coefficients from Rec. 709
00077     static float premultiply( float c, float a ) { return c * a; }
00078     static float inverse( float c ) { return 1.0f - c; }    
00079 };
00080 
00081 #define CHANNEL_TYPES (uint8_t)(float)
00082 
00083 } // namespace cinder