1 #include <msp/gl/extensions/arb_texture_float.h>
2 #include <msp/io/print.h>
3 #include <msp/strings/format.h>
4 #include "pixelformat.h"
11 void operator>>(const LexicalConverter &conv, PixelComponents &comp)
13 if(conv.get()=="STENCIL_INDEX")
15 else if(conv.get()=="DEPTH_COMPONENT")
16 comp = DEPTH_COMPONENT;
17 else if(conv.get()=="RED")
19 else if(conv.get()=="RG")
21 else if(conv.get()=="RGB")
23 else if(conv.get()=="RGBA")
25 else if(conv.get()=="BGR")
27 else if(conv.get()=="BGRA")
29 else if(conv.get()=="LUMINANCE")
31 else if(conv.get()=="LUMINANCE_ALPHA")
32 comp = LUMINANCE_ALPHA;
34 throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
37 void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
41 else if(conv.get()=="R16F")
43 else if(conv.get()=="R32F")
45 else if(conv.get()=="RG8")
47 else if(conv.get()=="RG16F")
49 else if(conv.get()=="RG32F")
51 else if(conv.get()=="RGB8")
53 else if(conv.get()=="RGB16F")
55 else if(conv.get()=="RGB32F")
57 else if(conv.get()=="RGBA8")
59 else if(conv.get()=="RGBA16F")
61 else if(conv.get()=="RGBA32F")
63 else if(conv.get()=="SRGB8")
65 else if(conv.get()=="SRGB8_ALPHA8")
67 else if(conv.get()=="BGR8")
69 else if(conv.get()=="BGRA8")
71 else if(conv.get()=="LUMINANCE8")
73 else if(conv.get()=="LUMINANCE8_ALPHA8")
74 fmt = LUMINANCE8_ALPHA8;
75 else if(conv.get()=="DEPTH_COMPONENT16")
76 fmt = DEPTH_COMPONENT16;
77 else if(conv.get()=="DEPTH_COMPONENT24")
78 fmt = DEPTH_COMPONENT24;
79 else if(conv.get()=="DEPTH_COMPONENT32")
80 fmt = DEPTH_COMPONENT32;
81 else if(conv.get()=="DEPTH_COMPONENT32F")
82 fmt = DEPTH_COMPONENT32F;
87 fmt = make_pixelformat(comp, (comp==DEPTH_COMPONENT ? FLOAT : UNSIGNED_BYTE));
88 IO::print(IO::cerr, "Warning: deprecated conversion of '%s' to PixelFormat\n", conv.get());
92 PixelComponents pixelformat_from_graphics(Graphics::PixelFormat pf)
96 case Graphics::LUMINANCE: return LUMINANCE;
97 case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
98 case Graphics::RGB: return RGB;
100 case Graphics::RGBA: return RGBA;
101 case Graphics::BGR: return BGR;
103 case Graphics::BGRA: return BGRA;
104 default: throw invalid_argument("pixelformat_from_graphics");
108 PixelComponents storage_pixelformat_from_graphics(Graphics::PixelFormat pf)
110 #pragma GCC diagnostic push
111 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
116 case Graphics::BGRX: return RGB;
117 case Graphics::BGRA: return RGBA;
118 default: return pixelformat_from_graphics(pf);
120 #pragma GCC diagnostic pop
123 PixelFormat pixelformat_from_image(const Graphics::Image &image)
125 #pragma GCC diagnostic push
126 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
127 PixelComponents comp = pixelformat_from_graphics(image.get_format());
128 #pragma GCC diagnostic pop
129 return make_pixelformat(comp, UNSIGNED_BYTE);
132 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
134 if(srgb && type!=UNSIGNED_BYTE && comp!=RGB && comp!=RGBA && comp!=BGR && comp!=BGRA)
135 throw invalid_argument("make_pixelformat");
142 case UNSIGNED_BYTE: return R8;
143 case HALF_FLOAT: return R16F;
144 case FLOAT: return R32F;
145 default: throw invalid_argument("make_pixelformat");
150 case UNSIGNED_BYTE: return RG8;
151 case HALF_FLOAT: return RG16F;
152 case FLOAT: return RG32F;
153 default: throw invalid_argument("make_pixelformat");
158 case UNSIGNED_BYTE: return (srgb ? SRGB8 : RGB8);
159 case HALF_FLOAT: return RGB16F;
160 case FLOAT: return RGB32F;
161 default: throw invalid_argument("make_pixelformat");
166 case UNSIGNED_BYTE: return (srgb ? SRGB8_ALPHA8 : RGBA8);
167 case HALF_FLOAT: return RGBA16F;
168 case FLOAT: return RGBA32F;
169 default: throw invalid_argument("make_pixelformat");
172 if(type!=UNSIGNED_BYTE)
173 throw invalid_argument("make_pixelformat");
174 return (srgb ? BGR8 : SBGR8);
176 if(type!=UNSIGNED_BYTE)
177 throw invalid_argument("make_pixelformat");
178 return (srgb ? BGRA8 : SBGR8_ALPHA8);
180 if(type!=UNSIGNED_BYTE)
181 throw invalid_argument("make_pixelformat");
183 case LUMINANCE_ALPHA:
184 if(type!=UNSIGNED_BYTE)
185 throw invalid_argument("make_pixelformat");
188 if(type!=UNSIGNED_BYTE)
189 throw invalid_argument("make_pixelformat");
190 return STENCIL_INDEX8;
191 case DEPTH_COMPONENT:
194 case UNSIGNED_SHORT: return DEPTH_COMPONENT16;
195 case UNSIGNED_INT: return DEPTH_COMPONENT32;
196 case FLOAT: return DEPTH_COMPONENT32F;
197 default: throw invalid_argument("make_pixelformat");
200 throw invalid_argument("make_pixelformat");
204 PixelFormat get_base_pixelformat(PixelFormat pf)
208 case SRGB8: return RGB8;
209 case SRGB8_ALPHA8: return RGBA8;
214 PixelComponents get_components(PixelFormat pf)
220 case R32F: return RED;
223 case RG32F: return RG;
227 case SRGB8: return RGB;
231 case SRGB8_ALPHA8: return RGBA;
233 case SBGR8: return BGR;
235 case SBGR8_ALPHA8: return BGRA;
236 case LUMINANCE8: return LUMINANCE;
237 case LUMINANCE8_ALPHA8: return LUMINANCE_ALPHA;
238 case STENCIL_INDEX8: return STENCIL_INDEX;
239 case DEPTH_COMPONENT16:
240 case DEPTH_COMPONENT24:
241 case DEPTH_COMPONENT32:
242 case DEPTH_COMPONENT32F: return DEPTH_COMPONENT;
243 default: throw invalid_argument("get_components");
247 PixelFormat get_default_sized_pixelformat(PixelComponents comp)
249 DataType type = UNSIGNED_BYTE;
250 if(comp==DEPTH_COMPONENT)
252 if(get_gl_api()==OPENGL_ES2 && !ARB_depth_buffer_float)
253 type = UNSIGNED_SHORT;
257 return make_pixelformat(comp, type);
260 PixelFormat get_srgb_pixelformat(PixelFormat pf)
264 case RGB8: return SRGB8;
265 case RGBA8: return SRGB8_ALPHA8;
270 unsigned get_component_count(PixelComponents comp)
276 case DEPTH_COMPONENT:
280 case LUMINANCE_ALPHA:
289 throw invalid_argument("get_component_count");
293 DataType get_component_type(PixelFormat pf)
308 case LUMINANCE8_ALPHA8:
309 return UNSIGNED_BYTE;
315 case DEPTH_COMPONENT16:
316 return UNSIGNED_SHORT;
321 case DEPTH_COMPONENT32:
323 case DEPTH_COMPONENT32F:
325 case DEPTH_COMPONENT24:
326 // There's no DataType value with 24-bit size
328 throw invalid_argument("get_component_type");
332 unsigned get_pixel_size(PixelFormat pf)
334 return get_component_count(pf)*get_type_size(get_component_type(pf));
337 void require_pixelformat(PixelFormat pf)
339 /* TODO These checks are only accurate for textures. On OpenGL ES some
340 formats are allowed for render buffers earlier than textures. In particular
341 it's possible to create a 16-bit depth renderbuffer on OpenGL ES 2.0 but
342 depth textures are only available with 3.0 or the OES_depth_texture
348 { static Require _req(OES_required_internalformat); }
352 { static Require _req(ARB_texture_rg); }
358 { static Require _req(ARB_texture_rg); }
359 { static Require _req(ARB_texture_float); }
365 { static Require _req(ARB_texture_float); }
369 { static Require _req(EXT_texture_sRGB); }
371 case DEPTH_COMPONENT16:
372 case DEPTH_COMPONENT24:
373 case DEPTH_COMPONENT32:
374 { static Require _req(ARB_depth_texture); }
375 { static Require _req(OES_required_internalformat); }
377 case DEPTH_COMPONENT32F:
378 { static Require _req(ARB_depth_buffer_float); }
381 { static Require _req(OES_texture_stencil8); }
384 throw invalid_argument("require_pixelformat");