]> git.tdb.fi Git - libs/gl.git/blob - source/core/pixelformat.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / pixelformat.cpp
1 #include <msp/strings/format.h>
2 #include "pixelformat.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8
9 void operator>>(const LexicalConverter &conv, PixelComponents &comp)
10 {
11         if(conv.get()=="STENCIL_INDEX")
12                 comp = STENCIL_INDEX;
13         else if(conv.get()=="DEPTH_COMPONENT")
14                 comp = DEPTH_COMPONENT;
15         else if(conv.get()=="RED")
16                 comp = RED;
17         else if(conv.get()=="RG")
18                 comp = RG;
19         else if(conv.get()=="RGB")
20                 comp = RGB;
21         else if(conv.get()=="RGBA")
22                 comp = RGBA;
23         else if(conv.get()=="BGR")
24                 comp = BGR;
25         else if(conv.get()=="BGRA")
26                 comp = BGRA;
27         else if(conv.get()=="LUMINANCE")
28                 comp = LUMINANCE;
29         else if(conv.get()=="LUMINANCE_ALPHA")
30                 comp = LUMINANCE_ALPHA;
31         else
32                 throw lexical_error(format("conversion of '%s' to PixelComponents", conv.get()));
33 }
34
35 void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
36 {
37         if(conv.get()=="R8")
38                 fmt = R8;
39         else if(conv.get()=="R16F")
40                 fmt = R16F;
41         else if(conv.get()=="R32F")
42                 fmt = R32F;
43         else if(conv.get()=="RG8")
44                 fmt = RG8;
45         else if(conv.get()=="RG16F")
46                 fmt = RG16F;
47         else if(conv.get()=="RG32F")
48                 fmt = RG32F;
49         else if(conv.get()=="RGB8")
50                 fmt = RGB8;
51         else if(conv.get()=="RGB16F")
52                 fmt = RGB16F;
53         else if(conv.get()=="RGB32F")
54                 fmt = RGB32F;
55         else if(conv.get()=="RGBA8")
56                 fmt = RGBA8;
57         else if(conv.get()=="RGBA16F")
58                 fmt = RGBA16F;
59         else if(conv.get()=="RGBA32F")
60                 fmt = RGBA32F;
61         else if(conv.get()=="SRGB8")
62                 fmt = SRGB8;
63         else if(conv.get()=="SRGB8_ALPHA8")
64                 fmt = SRGB8_ALPHA8;
65         else if(conv.get()=="BGR8")
66                 fmt = BGR8;
67         else if(conv.get()=="BGRA8")
68                 fmt = BGRA8;
69         else if(conv.get()=="LUMINANCE8")
70                 fmt = LUMINANCE8;
71         else if(conv.get()=="LUMINANCE8_ALPHA8")
72                 fmt = LUMINANCE8_ALPHA8;
73         else if(conv.get()=="DEPTH_COMPONENT16")
74                 fmt = DEPTH_COMPONENT16;
75         else if(conv.get()=="DEPTH_COMPONENT24")
76                 fmt = DEPTH_COMPONENT24;
77         else if(conv.get()=="DEPTH_COMPONENT32")
78                 fmt = DEPTH_COMPONENT32;
79         else if(conv.get()=="DEPTH_COMPONENT32F")
80                 fmt = DEPTH_COMPONENT32F;
81         else
82                 throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
83 }
84
85 PixelComponents components_from_graphics(Graphics::PixelFormat pf)
86 {
87         switch(pf)
88         {
89         case Graphics::LUMINANCE: return LUMINANCE;
90         case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
91         case Graphics::RGB: return RGB;
92         case Graphics::RGBX:
93         case Graphics::RGBA: return RGBA;
94         case Graphics::BGR: return BGR;
95         case Graphics::BGRX:
96         case Graphics::BGRA: return BGRA;
97         default: throw invalid_argument("pixelformat_from_graphics");
98         }
99 }
100
101 PixelComponents swizzle_components(PixelComponents comp, ComponentSwizzle swiz)
102 {
103         if(swiz==NO_SWIZZLE)
104                 return comp;
105         else if(comp==RED && swiz==R_TO_LUMINANCE)
106                 return LUMINANCE;
107         else if(comp==RG && swiz==RG_TO_LUMINANCE_ALPHA)
108                 return LUMINANCE_ALPHA;
109         else if(comp==RGB && swiz==RGB_TO_BGR)
110                 return BGR;
111         else if(comp==RGBA && swiz==RGB_TO_BGR)
112                 return BGRA;
113         else if(comp==RGBA && swiz==RGBA_TO_RGB)
114                 return RGB;
115         else if(comp==BGRA && swiz==RGBA_TO_RGB)
116                 return BGR;
117         else
118                 throw invalid_argument("swizzle_components");
119 }
120
121 PixelComponents unswizzle_components(PixelComponents comp, ComponentSwizzle swiz)
122 {
123         if(swiz==NO_SWIZZLE)
124                 return comp;
125         else if(comp==LUMINANCE && swiz==R_TO_LUMINANCE)
126                 return RED;
127         else if(comp==LUMINANCE_ALPHA && swiz==RG_TO_LUMINANCE_ALPHA)
128                 return RG;
129         else if(comp==BGR && swiz==RGB_TO_BGR)
130                 return RGB;
131         else if(comp==BGRA && swiz==RGB_TO_BGR)
132                 return RGBA;
133         else if(comp==RGB && swiz==RGBA_TO_RGB)
134                 return RGBA;
135         else if(comp==BGR && swiz==RGBA_TO_RGB)
136                 return BGRA;
137         else
138                 throw invalid_argument("swizzle_components");
139 }
140
141 PixelFormat pixelformat_from_image(const Graphics::Image &image, bool srgb)
142 {
143         PixelComponents comp = components_from_graphics(image.get_format());
144         return make_pixelformat(comp, UNSIGNED_BYTE, srgb);
145 }
146
147 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
148 {
149         bool normalized = !is_float(type);
150         return static_cast<PixelFormat>(comp | get_type_size(type)<<8 | (type&0x300)<<4 | normalized*0x4000 | srgb*0x8000);
151 }
152
153 } // namespace GL
154 } // namespace Msp