Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ChanTraits.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Cinder Project
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 
27 namespace cinder {
28 
29 template<typename T>
30 struct CHANTRAIT
31 {
32 };
33 
34 template<>
35 struct CHANTRAIT<uint8_t>
36 {
37  typedef uint32_t Sum;
38  typedef uint32_t Accum;
39  typedef int32_t SignedSum;
40  static uint8_t max() { return 255; }
41  static uint8_t convert( uint8_t v ) { return v; }
42  static uint8_t convert( uint16_t v ) { return v / 257; }
43  static uint8_t convert( float v ) { return static_cast<uint8_t>( v * 255 ); }
44  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
45  static uint8_t premultiply( uint8_t c, uint8_t a ) { return a * c / 255; }
46  //static uint8_t premultiply( uint8_t c, uint8_t a ) { uint16_t t = c * a + 0x80f; return ( ( t >> 8 ) + t ) >> 8; }
47  static uint8_t inverse( uint8_t c ) { return ~c; }
48 };
49 
50 template<>
51 struct CHANTRAIT<uint16_t>
52 {
53  typedef uint32_t Sum;
54  typedef uint32_t Accum;
55  typedef int32_t SignedSum;
56  static uint16_t max() { return 65535; }
57  static uint16_t convert( uint8_t v ) { return ( v << 8 ) | v; }
58  static uint16_t convert( uint16_t v ) { return v; }
59  static uint16_t convert( float v ) { return static_cast<uint16_t>( v * 65535 ); }
60  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
61 };
62 
63 template<>
64 struct CHANTRAIT<float>
65 {
66  typedef float Sum;
67  typedef float Accum;
68  typedef float SignedSum;
69  static float max() { return 1.0f; }
70  static float convert( uint8_t v ) { return v / 255.0f; }
71  static float convert( uint16_t v ) { return v / 65535.0f; }
72  static float convert( float v ) { return v; }
73  static float grayscale( float r, float g, float b ) { return r * 0.2126f + g * 0.7152f + b * 0.0722f; } // luma coefficients from Rec. 709
75  static float premultiply( float c, float a ) { return c * a; }
76  static float inverse( float c ) { return 1.0f - c; }
77 };
78 
79 #define CHANNEL_TYPES (uint8_t)(float)
80 
81 } // namespace cinder
static uint16_t convert(float v)
Definition: ChanTraits.h:59
GLdouble GLdouble GLdouble r
Definition: GLee.h:1474
static uint16_t convert(uint16_t v)
Definition: ChanTraits.h:58
static uint8_t grayscale(uint8_t r, uint8_t g, uint8_t b)
Definition: ChanTraits.h:44
static uint16_t max()
Definition: ChanTraits.h:56
static float grayscale(float r, float g, float b)
Definition: ChanTraits.h:73
static uint8_t premultiply(uint8_t c, uint8_t a)
Definition: ChanTraits.h:45
float SignedSum
Definition: ChanTraits.h:68
static float convert(float v)
Definition: ChanTraits.h:72
float Accum
Definition: ChanTraits.h:67
static float convert(uint8_t v)
Definition: ChanTraits.h:70
static uint16_t grayscale(uint16_t r, uint16_t g, uint16_t b)
Definition: ChanTraits.h:60
static float inverse(float c)
Definition: ChanTraits.h:76
uint32_t Accum
Definition: ChanTraits.h:38
static uint16_t convert(uint8_t v)
Definition: ChanTraits.h:57
GLboolean GLboolean g
Definition: GLee.h:2964
Definition: ChanTraits.h:30
float Sum
Definition: ChanTraits.h:66
int32_t SignedSum
Definition: ChanTraits.h:55
static float premultiply(float c, float a)
Calculates the multiplied version of a color component c by alpha a.
Definition: ChanTraits.h:75
uint32_t Sum
Definition: ChanTraits.h:53
int32_t SignedSum
Definition: ChanTraits.h:39
const GLdouble * v
Definition: GLee.h:1384
uint32_t Sum
Definition: ChanTraits.h:37
GLboolean GLboolean GLboolean b
Definition: GLee.h:2964
uint32_t Accum
Definition: ChanTraits.h:54
const GLubyte * c
Definition: GLee.h:8491
static uint8_t convert(float v)
Definition: ChanTraits.h:43
static uint8_t convert(uint16_t v)
Definition: ChanTraits.h:42
static float max()
Definition: ChanTraits.h:69
static uint8_t inverse(uint8_t c)
Definition: ChanTraits.h:47
GLboolean GLboolean GLboolean GLboolean a
Definition: GLee.h:2964
static uint8_t max()
Definition: ChanTraits.h:40
static float convert(uint16_t v)
Definition: ChanTraits.h:71
static uint8_t convert(uint8_t v)
Definition: ChanTraits.h:41