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 PixelComponents", 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;
85 if(conv.get()=="SRGB")
87 else if(conv.get()=="SRGB_ALPHA")
93 fmt = make_pixelformat(comp, (comp==DEPTH_COMPONENT ? FLOAT : UNSIGNED_BYTE));
95 IO::print(IO::cerr, "Warning: deprecated conversion of '%s' to PixelFormat\n", conv.get());
99 PixelComponents pixelformat_from_graphics(Graphics::PixelFormat pf)
103 case Graphics::LUMINANCE: return LUMINANCE;
104 case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
105 case Graphics::RGB: return RGB;
107 case Graphics::RGBA: return RGBA;
108 case Graphics::BGR: return BGR;
110 case Graphics::BGRA: return BGRA;
111 default: throw invalid_argument("pixelformat_from_graphics");
115 PixelComponents storage_pixelformat_from_graphics(Graphics::PixelFormat pf)
117 #pragma GCC diagnostic push
118 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
123 case Graphics::BGRX: return RGB;
124 case Graphics::BGRA: return RGBA;
125 default: return pixelformat_from_graphics(pf);
127 #pragma GCC diagnostic pop
130 PixelFormat pixelformat_from_image(const Graphics::Image &image)
132 #pragma GCC diagnostic push
133 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
134 PixelComponents comp = pixelformat_from_graphics(image.get_format());
135 #pragma GCC diagnostic pop
136 return make_pixelformat(comp, UNSIGNED_BYTE);
139 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
141 if(srgb && type!=UNSIGNED_BYTE && comp!=RGB && comp!=RGBA && comp!=BGR && comp!=BGRA)
142 throw invalid_argument("make_pixelformat");
149 case UNSIGNED_BYTE: return R8;
150 case HALF_FLOAT: return R16F;
151 case FLOAT: return R32F;
152 default: throw invalid_argument("make_pixelformat");
157 case UNSIGNED_BYTE: return RG8;
158 case HALF_FLOAT: return RG16F;
159 case FLOAT: return RG32F;
160 default: throw invalid_argument("make_pixelformat");
165 case UNSIGNED_BYTE: return (srgb ? SRGB8 : RGB8);
166 case HALF_FLOAT: return RGB16F;
167 case FLOAT: return RGB32F;
168 default: throw invalid_argument("make_pixelformat");
173 case UNSIGNED_BYTE: return (srgb ? SRGB8_ALPHA8 : RGBA8);
174 case HALF_FLOAT: return RGBA16F;
175 case FLOAT: return RGBA32F;
176 default: throw invalid_argument("make_pixelformat");
179 if(type!=UNSIGNED_BYTE)
180 throw invalid_argument("make_pixelformat");
181 return (srgb ? BGR8 : SBGR8);
183 if(type!=UNSIGNED_BYTE)
184 throw invalid_argument("make_pixelformat");
185 return (srgb ? BGRA8 : SBGR8_ALPHA8);
187 if(type!=UNSIGNED_BYTE)
188 throw invalid_argument("make_pixelformat");
190 case LUMINANCE_ALPHA:
191 if(type!=UNSIGNED_BYTE)
192 throw invalid_argument("make_pixelformat");
195 if(type!=UNSIGNED_BYTE)
196 throw invalid_argument("make_pixelformat");
197 return STENCIL_INDEX8;
198 case DEPTH_COMPONENT:
201 case UNSIGNED_SHORT: return DEPTH_COMPONENT16;
202 case UNSIGNED_INT: return DEPTH_COMPONENT32;
203 case FLOAT: return DEPTH_COMPONENT32F;
204 default: throw invalid_argument("make_pixelformat");
207 throw invalid_argument("make_pixelformat");
211 PixelFormat get_base_pixelformat(PixelFormat pf)
215 case SRGB8: return RGB8;
216 case SRGB8_ALPHA8: return RGBA8;
221 PixelComponents get_components(PixelFormat pf)
227 case R32F: return RED;
230 case RG32F: return RG;
234 case SRGB8: return RGB;
238 case SRGB8_ALPHA8: return RGBA;
240 case SBGR8: return BGR;
242 case SBGR8_ALPHA8: return BGRA;
243 case LUMINANCE8: return LUMINANCE;
244 case LUMINANCE8_ALPHA8: return LUMINANCE_ALPHA;
245 case STENCIL_INDEX8: return STENCIL_INDEX;
246 case DEPTH_COMPONENT16:
247 case DEPTH_COMPONENT24:
248 case DEPTH_COMPONENT32:
249 case DEPTH_COMPONENT32F: return DEPTH_COMPONENT;
250 default: throw invalid_argument("get_components");
254 PixelFormat get_default_sized_pixelformat(PixelComponents comp)
256 DataType type = UNSIGNED_BYTE;
257 if(comp==DEPTH_COMPONENT)
259 if(get_gl_api()==OPENGL_ES2 && !ARB_depth_buffer_float)
260 type = UNSIGNED_SHORT;
264 return make_pixelformat(comp, type);
267 PixelFormat get_srgb_pixelformat(PixelFormat pf)
271 case RGB8: return SRGB8;
272 case RGBA8: return SRGB8_ALPHA8;
277 unsigned get_component_count(PixelComponents comp)
283 case DEPTH_COMPONENT:
287 case LUMINANCE_ALPHA:
296 throw invalid_argument("get_component_count");
300 DataType get_component_type(PixelFormat pf)
315 case LUMINANCE8_ALPHA8:
316 return UNSIGNED_BYTE;
322 case DEPTH_COMPONENT16:
323 return UNSIGNED_SHORT;
328 case DEPTH_COMPONENT32:
330 case DEPTH_COMPONENT32F:
332 case DEPTH_COMPONENT24:
333 // There's no DataType value with 24-bit size
335 throw invalid_argument("get_component_type");
339 bool is_srgb(PixelFormat pf)
341 return (pf==SRGB8 || pf==SRGB8_ALPHA8 || pf==SBGR8 || pf==SBGR8_ALPHA8);
344 unsigned get_pixel_size(PixelFormat pf)
346 return get_component_count(pf)*get_type_size(get_component_type(pf));
349 void require_pixelformat(PixelFormat pf)
351 /* TODO These checks are only accurate for textures. On OpenGL ES some
352 formats are allowed for render buffers earlier than textures. In particular
353 it's possible to create a 16-bit depth renderbuffer on OpenGL ES 2.0 but
354 depth textures are only available with 3.0 or the OES_depth_texture
360 { static Require _req(OES_required_internalformat); }
364 { static Require _req(ARB_texture_rg); }
370 { static Require _req(ARB_texture_rg); }
371 { static Require _req(ARB_texture_float); }
377 { static Require _req(ARB_texture_float); }
381 { static Require _req(EXT_texture_sRGB); }
383 case DEPTH_COMPONENT16:
384 case DEPTH_COMPONENT24:
385 case DEPTH_COMPONENT32:
386 { static Require _req(ARB_depth_texture); }
387 { static Require _req(OES_required_internalformat); }
389 case DEPTH_COMPONENT32F:
390 { static Require _req(ARB_depth_buffer_float); }
393 { static Require _req(OES_texture_stencil8); }
396 throw invalid_argument("require_pixelformat");