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()=="LUMINANCE8")
69 else if(conv.get()=="LUMINANCE8_ALPHA8")
70 fmt = LUMINANCE8_ALPHA8;
71 else if(conv.get()=="DEPTH_COMPONENT16")
72 fmt = DEPTH_COMPONENT16;
73 else if(conv.get()=="DEPTH_COMPONENT24")
74 fmt = DEPTH_COMPONENT24;
75 else if(conv.get()=="DEPTH_COMPONENT32")
76 fmt = DEPTH_COMPONENT32;
77 else if(conv.get()=="DEPTH_COMPONENT32F")
78 fmt = DEPTH_COMPONENT32F;
83 fmt = make_pixelformat(comp, (comp==DEPTH_COMPONENT ? FLOAT : UNSIGNED_BYTE));
84 IO::print(IO::cerr, "Warning: deprecated conversion of '%s' to PixelFormat\n", conv.get());
88 PixelComponents pixelformat_from_graphics(Graphics::PixelFormat pf)
92 case Graphics::LUMINANCE: return LUMINANCE;
93 case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
94 case Graphics::RGB: return RGB;
96 case Graphics::RGBA: return RGBA;
97 case Graphics::BGR: return BGR;
99 case Graphics::BGRA: return BGRA;
100 default: throw invalid_argument("pixelformat_from_graphics");
104 PixelComponents storage_pixelformat_from_graphics(Graphics::PixelFormat pf)
106 #pragma GCC diagnostic push
107 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
112 case Graphics::BGRX: return RGB;
113 case Graphics::BGRA: return RGBA;
114 default: return pixelformat_from_graphics(pf);
116 #pragma GCC diagnostic pop
119 PixelFormat pixelformat_from_image(const Graphics::Image &image)
121 #pragma GCC diagnostic push
122 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
123 PixelComponents comp = pixelformat_from_graphics(image.get_format());
124 #pragma GCC diagnostic pop
125 return make_pixelformat(comp, UNSIGNED_BYTE);
128 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
130 if(srgb && type!=UNSIGNED_BYTE && comp!=RGB && comp!=RGBA)
131 throw invalid_argument("make_pixelformat");
138 case UNSIGNED_BYTE: return R8;
139 case HALF_FLOAT: return R16F;
140 case FLOAT: return R32F;
141 default: throw invalid_argument("make_pixelformat");
146 case UNSIGNED_BYTE: return RG8;
147 case HALF_FLOAT: return RG16F;
148 case FLOAT: return RG32F;
149 default: throw invalid_argument("make_pixelformat");
154 case UNSIGNED_BYTE: return (srgb ? SRGB8 : RGB8);
155 case HALF_FLOAT: return RGB16F;
156 case FLOAT: return RGB32F;
157 default: throw invalid_argument("make_pixelformat");
162 case UNSIGNED_BYTE: return (srgb ? SRGB8_ALPHA8 : RGBA8);
163 case HALF_FLOAT: return RGBA16F;
164 case FLOAT: return RGBA32F;
165 default: throw invalid_argument("make_pixelformat");
168 if(type!=UNSIGNED_BYTE)
169 throw invalid_argument("make_pixelformat");
171 case LUMINANCE_ALPHA:
172 if(type!=UNSIGNED_BYTE)
173 throw invalid_argument("make_pixelformat");
176 if(type!=UNSIGNED_BYTE)
177 throw invalid_argument("make_pixelformat");
178 return STENCIL_INDEX8;
179 case DEPTH_COMPONENT:
182 case UNSIGNED_SHORT: return DEPTH_COMPONENT16;
183 case UNSIGNED_INT: return DEPTH_COMPONENT32;
184 case FLOAT: return DEPTH_COMPONENT32F;
185 default: throw invalid_argument("make_pixelformat");
188 throw invalid_argument("make_pixelformat");
192 PixelFormat get_base_pixelformat(PixelFormat pf)
196 case SRGB8: return RGB8;
197 case SRGB8_ALPHA8: return RGBA8;
202 PixelComponents get_components(PixelFormat pf)
208 case R32F: return RED;
211 case RG32F: return RG;
215 case SRGB8: return RGB;
219 case SRGB8_ALPHA8: return RGBA;
220 case LUMINANCE8: return LUMINANCE;
221 case LUMINANCE8_ALPHA8: return LUMINANCE_ALPHA;
222 case STENCIL_INDEX8: return STENCIL_INDEX;
223 case DEPTH_COMPONENT16:
224 case DEPTH_COMPONENT24:
225 case DEPTH_COMPONENT32:
226 case DEPTH_COMPONENT32F: return DEPTH_COMPONENT;
227 default: throw invalid_argument("get_components");
231 PixelFormat get_default_sized_pixelformat(PixelComponents comp)
233 DataType type = UNSIGNED_BYTE;
234 if(comp==DEPTH_COMPONENT)
236 if(get_gl_api()==OPENGL_ES2 && !ARB_depth_buffer_float)
237 type = UNSIGNED_SHORT;
241 return make_pixelformat(comp, type);
244 PixelFormat get_srgb_pixelformat(PixelFormat pf)
248 case RGB8: return SRGB8;
249 case RGBA8: return SRGB8_ALPHA8;
254 unsigned get_component_count(PixelComponents comp)
259 case DEPTH_COMPONENT:
264 case LUMINANCE_ALPHA:
273 throw invalid_argument("get_component_count");
277 DataType get_component_type(PixelFormat pf)
288 case LUMINANCE8_ALPHA8:
289 return UNSIGNED_BYTE;
295 case DEPTH_COMPONENT16:
296 return UNSIGNED_SHORT;
301 case DEPTH_COMPONENT32:
303 case DEPTH_COMPONENT32F:
305 case DEPTH_COMPONENT24:
306 // There's no DataType value with 24-bit size
308 throw invalid_argument("get_component_type");
312 unsigned get_pixel_size(PixelFormat pf)
314 return get_component_count(pf)*get_type_size(get_component_type(pf));
317 void require_pixelformat(PixelFormat pf)
319 /* TODO These checks are only accurate for textures. On OpenGL ES some
320 formats are allowed for render buffers earlier than textures. In particular
321 it's possible to create a 16-bit depth renderbuffer on OpenGL ES 2.0 but
322 depth textures are only available with 3.0 or the OES_depth_texture
328 { static Require _req(OES_required_internalformat); }
332 { static Require _req(ARB_texture_rg); }
338 { static Require _req(ARB_texture_rg); }
339 { static Require _req(ARB_texture_float); }
345 { static Require _req(ARB_texture_float); }
349 { static Require _req(EXT_texture_sRGB); }
351 case DEPTH_COMPONENT16:
352 case DEPTH_COMPONENT24:
353 case DEPTH_COMPONENT32:
354 { static Require _req(ARB_depth_texture); }
355 { static Require _req(OES_required_internalformat); }
357 case DEPTH_COMPONENT32F:
358 { static Require _req(ARB_depth_buffer_float); }
361 { static Require _req(OES_texture_stencil8); }
364 throw invalid_argument("require_pixelformat");