]> git.tdb.fi Git - libs/gl.git/blob - source/pixelformat.cpp
Deal with nontrivial image configurations
[libs/gl.git] / source / pixelformat.cpp
1 #include <msp/gl/extensions/arb_texture_float.h>
2 #include <msp/gl/extensions/ext_bgra.h>
3 #include <msp/strings/format.h>
4 #include "pixelformat.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
12 {
13         if(conv.get()=="COLOR_INDEX")
14                 fmt = COLOR_INDEX;
15         else if(conv.get()=="STENCIL_INDEX")
16                 fmt = STENCIL_INDEX;
17         else if(conv.get()=="DEPTH_COMPONENT")
18                 fmt = DEPTH_COMPONENT;
19         else if(conv.get()=="RED")
20                 fmt = RED;
21         else if(conv.get()=="GREEN")
22                 fmt = GREEN;
23         else if(conv.get()=="BLUE")
24                 fmt = BLUE;
25         else if(conv.get()=="ALPHA")
26                 fmt = ALPHA;
27         else if(conv.get()=="RGB")
28                 fmt = RGB;
29         else if(conv.get()=="RGBA")
30                 fmt = RGBA;
31         else if(conv.get()=="BGR")
32                 fmt = BGR;
33         else if(conv.get()=="BGRA")
34                 fmt = BGRA;
35         else if(conv.get()=="LUMINANCE")
36                 fmt = LUMINANCE;
37         else if(conv.get()=="LUMINANCE_ALPHA")
38                 fmt = LUMINANCE_ALPHA;
39         else
40                 throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
41 }
42
43 PixelFormat pixelformat_from_graphics(Graphics::PixelFormat pf)
44 {
45         switch(pf)
46         {
47         case Graphics::COLOR_INDEX: return COLOR_INDEX;
48         case Graphics::LUMINANCE: return LUMINANCE;
49         case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
50         case Graphics::RGB: return RGB;
51         case Graphics::RGBX:
52         case Graphics::RGBA: return RGBA;
53         case Graphics::BGR: return BGR;
54         case Graphics::BGRX:
55         case Graphics::BGRA: return BGRA;
56         default: throw invalid_argument("pixelformat_from_graphics");
57         }
58 }
59
60 PixelFormat storage_pixelformat_from_graphics(Graphics::PixelFormat pf)
61 {
62         switch(pf)
63         {
64         case Graphics::RGBX:
65         case Graphics::BGR:
66         case Graphics::BGRX: return RGB;
67         case Graphics::BGRA: return RGBA;
68         default: return pixelformat_from_graphics(pf);
69         }
70 }
71
72 PixelFormat get_base_pixelformat(PixelFormat pf)
73 {
74         switch(pf)
75         {
76         case RGB8:
77         case RGB16F:
78         case RGB32F: return RGB;
79         case RGBA8:
80         case RGBA16F:
81         case RGBA32F: return RGBA;
82         case LUMINANCE8:
83         case LUMINANCE16F:
84         case LUMINANCE32F: return LUMINANCE;
85         case LUMINANCE_ALPHA8:
86         case LUMINANCE_ALPHA16F:
87         case LUMINANCE_ALPHA32F: return LUMINANCE_ALPHA;
88         default: return pf;
89         }
90 }
91
92 unsigned get_component_count(PixelFormat pf)
93 {
94         switch(get_base_pixelformat(pf))
95         {
96         case COLOR_INDEX:
97         case STENCIL_INDEX:
98         case DEPTH_COMPONENT:
99         case RED:
100         case GREEN:
101         case BLUE:
102         case LUMINANCE:
103                 return 1;
104         case LUMINANCE_ALPHA:
105                 return 2;
106         case RGB:
107         case BGR:
108                 return 3;
109         case RGBA:
110         case BGRA:
111                 return 4;
112         default:
113                 throw invalid_argument("get_pixelformat_component_count");
114         }
115 }
116
117 void require_pixelformat(PixelFormat pf)
118 {
119         switch(pf)
120         {
121         case RGB16F:
122         case RGB32F:
123         case RGBA16F:
124         case RGBA32F:
125         case LUMINANCE16F:
126         case LUMINANCE32F:
127         case LUMINANCE_ALPHA16F:
128         case LUMINANCE_ALPHA32F:
129                 { static Require _req(ARB_texture_float); }
130                 break;
131         case BGR:
132         case BGRA:
133                 { static Require _req(EXT_bgra); }
134                 break;
135         default:
136                 break;
137         }
138 }
139
140 } // namespace GL
141 } // namespace Msp