]> git.tdb.fi Git - libs/gl.git/blob - source/pixelformat.cpp
Delay opening the resource file until loading actually starts
[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()=="SRGB")
36                 fmt = SRGB;
37         else if(conv.get()=="SRGB_ALPHA")
38                 fmt = SRGB_ALPHA;
39         else if(conv.get()=="LUMINANCE")
40                 fmt = LUMINANCE;
41         else if(conv.get()=="LUMINANCE_ALPHA")
42                 fmt = LUMINANCE_ALPHA;
43         else if(conv.get()=="SLUMINANCE")
44                 fmt = SLUMINANCE;
45         else if(conv.get()=="SLUMINANCE_ALPHA")
46                 fmt = SLUMINANCE_ALPHA;
47         else
48                 throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
49 }
50
51 PixelFormat pixelformat_from_graphics(Graphics::PixelFormat pf)
52 {
53         switch(pf)
54         {
55         case Graphics::COLOR_INDEX: return COLOR_INDEX;
56         case Graphics::LUMINANCE: return LUMINANCE;
57         case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
58         case Graphics::RGB: return RGB;
59         case Graphics::RGBX:
60         case Graphics::RGBA: return RGBA;
61         case Graphics::BGR: return BGR;
62         case Graphics::BGRX:
63         case Graphics::BGRA: return BGRA;
64         default: throw invalid_argument("pixelformat_from_graphics");
65         }
66 }
67
68 PixelFormat storage_pixelformat_from_graphics(Graphics::PixelFormat pf)
69 {
70         switch(pf)
71         {
72         case Graphics::RGBX:
73         case Graphics::BGR:
74         case Graphics::BGRX: return RGB;
75         case Graphics::BGRA: return RGBA;
76         default: return pixelformat_from_graphics(pf);
77         }
78 }
79
80 PixelFormat get_base_pixelformat(PixelFormat pf)
81 {
82         switch(pf)
83         {
84         case RGB8:
85         case RGB16F:
86         case RGB32F:
87         case SRGB:
88         case SRGB8: return RGB;
89         case RGBA8:
90         case RGBA16F:
91         case RGBA32F:
92         case SRGB_ALPHA:
93         case SRGB8_ALPHA8: return RGBA;
94         case LUMINANCE8:
95         case LUMINANCE16F:
96         case LUMINANCE32F:
97         case SLUMINANCE:
98         case SLUMINANCE8: return LUMINANCE;
99         case LUMINANCE_ALPHA8:
100         case LUMINANCE_ALPHA16F:
101         case LUMINANCE_ALPHA32F:
102         case SLUMINANCE_ALPHA:
103         case SLUMINANCE8_ALPHA8: return LUMINANCE_ALPHA;
104         default: return pf;
105         }
106 }
107
108 PixelFormat get_srgb_pixelformat(PixelFormat pf)
109 {
110         switch(pf)
111         {
112         case RGB: return SRGB;
113         case RGBA: return SRGB_ALPHA;
114         case RGB8: return SRGB8;
115         case RGBA8: return SRGB8_ALPHA8;
116         case LUMINANCE: return SLUMINANCE;
117         case LUMINANCE8: return SLUMINANCE8;
118         case LUMINANCE_ALPHA: return SLUMINANCE_ALPHA;
119         case LUMINANCE_ALPHA8: return SLUMINANCE8_ALPHA8;
120         default: return pf;
121         }
122 }
123
124 unsigned get_component_count(PixelFormat pf)
125 {
126         switch(get_base_pixelformat(pf))
127         {
128         case COLOR_INDEX:
129         case STENCIL_INDEX:
130         case DEPTH_COMPONENT:
131         case RED:
132         case GREEN:
133         case BLUE:
134         case LUMINANCE:
135         case SLUMINANCE:
136                 return 1;
137         case LUMINANCE_ALPHA:
138         case SLUMINANCE_ALPHA:
139                 return 2;
140         case RGB:
141         case BGR:
142                 return 3;
143         case RGBA:
144         case BGRA:
145                 return 4;
146         default:
147                 throw invalid_argument("get_component_count");
148         }
149 }
150
151 unsigned get_component_size(PixelFormat pf)
152 {
153         switch(pf)
154         {
155         case RGB16F:
156         case RGBA16F:
157         case LUMINANCE16F:
158         case LUMINANCE_ALPHA16F:
159                 return 2;
160         case RGB32F:
161         case RGBA32F:
162         case LUMINANCE32F:
163         case LUMINANCE_ALPHA32F:
164                 return 4;
165         default:
166                 return 1;
167         }
168 }
169
170 unsigned get_pixel_size(PixelFormat pf)
171 {
172         return get_component_count(pf)*get_component_size(pf);
173 }
174
175 void require_pixelformat(PixelFormat pf)
176 {
177         switch(pf)
178         {
179         case RGB16F:
180         case RGB32F:
181         case RGBA16F:
182         case RGBA32F:
183         case LUMINANCE16F:
184         case LUMINANCE32F:
185         case LUMINANCE_ALPHA16F:
186         case LUMINANCE_ALPHA32F:
187                 { static Require _req(ARB_texture_float); }
188                 break;
189         case SRGB:
190         case SRGB8:
191         case SRGB_ALPHA:
192         case SRGB8_ALPHA8:
193         case SLUMINANCE:
194         case SLUMINANCE8:
195         case SLUMINANCE_ALPHA:
196         case SLUMINANCE8_ALPHA8:
197                 { static Require _req(EXT_texture_sRGB); }
198                 break;
199         case BGR:
200         case BGRA:
201                 { static Require _req(EXT_bgra); }
202                 break;
203         default:
204                 break;
205         }
206 }
207
208 } // namespace GL
209 } // namespace Msp