]> git.tdb.fi Git - libs/gl.git/blob - source/pixelformat.cpp
Remove the deprecated ProgramBuilder class
[libs/gl.git] / source / pixelformat.cpp
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"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 void operator>>(const LexicalConverter &conv, PixelComponents &comp)
12 {
13         if(conv.get()=="STENCIL_INDEX")
14                 comp = STENCIL_INDEX;
15         else if(conv.get()=="DEPTH_COMPONENT")
16                 comp = DEPTH_COMPONENT;
17         else if(conv.get()=="RED")
18                 comp = RED;
19         else if(conv.get()=="RG")
20                 comp = RG;
21         else if(conv.get()=="RGB")
22                 comp = RGB;
23         else if(conv.get()=="RGBA")
24                 comp = RGBA;
25         else if(conv.get()=="BGR")
26                 comp = BGR;
27         else if(conv.get()=="BGRA")
28                 comp = BGRA;
29         else if(conv.get()=="LUMINANCE")
30                 comp = LUMINANCE;
31         else if(conv.get()=="LUMINANCE_ALPHA")
32                 comp = LUMINANCE_ALPHA;
33         else
34                 throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
35 }
36
37 void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
38 {
39         if(conv.get()=="R8")
40                 fmt = R8;
41         else if(conv.get()=="R16F")
42                 fmt = R16F;
43         else if(conv.get()=="R32F")
44                 fmt = R32F;
45         else if(conv.get()=="RG8")
46                 fmt = RG8;
47         else if(conv.get()=="RG16F")
48                 fmt = RG16F;
49         else if(conv.get()=="RG32F")
50                 fmt = RG32F;
51         else if(conv.get()=="RGB8")
52                 fmt = RGB8;
53         else if(conv.get()=="RGB16F")
54                 fmt = RGB16F;
55         else if(conv.get()=="RGB32F")
56                 fmt = RGB32F;
57         else if(conv.get()=="RGBA8")
58                 fmt = RGBA8;
59         else if(conv.get()=="RGBA16F")
60                 fmt = RGBA16F;
61         else if(conv.get()=="RGBA32F")
62                 fmt = RGBA32F;
63         else if(conv.get()=="SRGB8")
64                 fmt = SRGB8;
65         else if(conv.get()=="SRGB8_ALPHA8")
66                 fmt = SRGB8_ALPHA8;
67         else if(conv.get()=="BGR8")
68                 fmt = BGR8;
69         else if(conv.get()=="BGRA8")
70                 fmt = BGRA8;
71         else if(conv.get()=="LUMINANCE8")
72                 fmt = 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;
83         else
84         {
85                 PixelComponents comp;
86                 conv >> comp;
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());
89         }
90 }
91
92 PixelComponents pixelformat_from_graphics(Graphics::PixelFormat pf)
93 {
94         switch(pf)
95         {
96         case Graphics::LUMINANCE: return LUMINANCE;
97         case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
98         case Graphics::RGB: return RGB;
99         case Graphics::RGBX:
100         case Graphics::RGBA: return RGBA;
101         case Graphics::BGR: return BGR;
102         case Graphics::BGRX:
103         case Graphics::BGRA: return BGRA;
104         default: throw invalid_argument("pixelformat_from_graphics");
105         }
106 }
107
108 PixelComponents storage_pixelformat_from_graphics(Graphics::PixelFormat pf)
109 {
110 #pragma GCC diagnostic push
111 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
112         switch(pf)
113         {
114         case Graphics::RGBX:
115         case Graphics::BGR:
116         case Graphics::BGRX: return RGB;
117         case Graphics::BGRA: return RGBA;
118         default: return pixelformat_from_graphics(pf);
119         }
120 #pragma GCC diagnostic pop
121 }
122
123 PixelFormat pixelformat_from_image(const Graphics::Image &image)
124 {
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);
130 }
131
132 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
133 {
134         if(srgb && type!=UNSIGNED_BYTE && comp!=RGB && comp!=RGBA && comp!=BGR && comp!=BGRA)
135                 throw invalid_argument("make_pixelformat");
136
137         switch(comp)
138         {
139         case RED:
140                 switch(type)
141                 {
142                 case UNSIGNED_BYTE: return R8;
143                 case HALF_FLOAT: return R16F;
144                 case FLOAT: return R32F;
145                 default: throw invalid_argument("make_pixelformat");
146                 }
147         case RG:
148                 switch(type)
149                 {
150                 case UNSIGNED_BYTE: return RG8;
151                 case HALF_FLOAT: return RG16F;
152                 case FLOAT: return RG32F;
153                 default: throw invalid_argument("make_pixelformat");
154                 }
155         case RGB:
156                 switch(type)
157                 {
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");
162                 }
163         case RGBA:
164                 switch(type)
165                 {
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");
170                 }
171         case BGR:
172                 if(type!=UNSIGNED_BYTE)
173                         throw invalid_argument("make_pixelformat");
174                 return (srgb ? BGR8 : SBGR8);
175         case BGRA:
176                 if(type!=UNSIGNED_BYTE)
177                         throw invalid_argument("make_pixelformat");
178                 return (srgb ? BGRA8 : SBGR8_ALPHA8);
179         case LUMINANCE:
180                 if(type!=UNSIGNED_BYTE)
181                         throw invalid_argument("make_pixelformat");
182                 return LUMINANCE8;
183         case LUMINANCE_ALPHA:
184                 if(type!=UNSIGNED_BYTE)
185                         throw invalid_argument("make_pixelformat");
186                 return LUMINANCE8;
187         case STENCIL_INDEX:
188                 if(type!=UNSIGNED_BYTE)
189                         throw invalid_argument("make_pixelformat");
190                 return STENCIL_INDEX8;
191         case DEPTH_COMPONENT:
192                 switch(type)
193                 {
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");
198                 }
199         default:
200                 throw invalid_argument("make_pixelformat");
201         }
202 }
203
204 PixelFormat get_base_pixelformat(PixelFormat pf)
205 {
206         switch(pf)
207         {
208         case SRGB8: return RGB8;
209         case SRGB8_ALPHA8: return RGBA8;
210         default: return pf;
211         }
212 }
213
214 PixelComponents get_components(PixelFormat pf)
215 {
216         switch(pf)
217         {
218         case R8:
219         case R16F:
220         case R32F: return RED;
221         case RG8:
222         case RG16F:
223         case RG32F: return RG;
224         case RGB8:
225         case RGB16F:
226         case RGB32F:
227         case SRGB8: return RGB;
228         case RGBA8:
229         case RGBA16F:
230         case RGBA32F:
231         case SRGB8_ALPHA8: return RGBA;
232         case BGR8:
233         case SBGR8: return BGR;
234         case BGRA8:
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");
244         }
245 }
246
247 PixelFormat get_default_sized_pixelformat(PixelComponents comp)
248 {
249         DataType type = UNSIGNED_BYTE;
250         if(comp==DEPTH_COMPONENT)
251         {
252                 if(get_gl_api()==OPENGL_ES2 && !ARB_depth_buffer_float)
253                         type = UNSIGNED_SHORT;
254                 else
255                         type = FLOAT;
256         }
257         return make_pixelformat(comp, type);
258 }
259
260 PixelFormat get_srgb_pixelformat(PixelFormat pf)
261 {
262         switch(pf)
263         {
264         case RGB8: return SRGB8;
265         case RGBA8: return SRGB8_ALPHA8;
266         default: return pf;
267         }
268 }
269
270 unsigned get_component_count(PixelComponents comp)
271 {
272         switch(comp)
273         {
274         case RED:
275         case LUMINANCE:
276         case DEPTH_COMPONENT:
277         case STENCIL_INDEX:
278                 return 1;
279         case RG:
280         case LUMINANCE_ALPHA:
281                 return 2;
282         case RGB:
283         case BGR:
284                 return 3;
285         case RGBA:
286         case BGRA:
287                 return 4;
288         default:
289                 throw invalid_argument("get_component_count");
290         }
291 }
292
293 DataType get_component_type(PixelFormat pf)
294 {
295         switch(pf)
296         {
297         case R8:
298         case RG8:
299         case RGB8:
300         case RGBA8:
301         case SRGB8:
302         case SRGB8_ALPHA8:
303         case BGR8:
304         case BGRA8:
305         case SBGR8:
306         case SBGR8_ALPHA8:
307         case LUMINANCE8:
308         case LUMINANCE8_ALPHA8:
309                 return UNSIGNED_BYTE;
310         case R16F:
311         case RG16F:
312         case RGB16F:
313         case RGBA16F:
314                 return HALF_FLOAT;
315         case DEPTH_COMPONENT16:
316                 return UNSIGNED_SHORT;
317         case R32F:
318         case RG32F:
319         case RGB32F:
320         case RGBA32F:
321         case DEPTH_COMPONENT32:
322                 return UNSIGNED_INT;
323         case DEPTH_COMPONENT32F:
324                 return FLOAT;
325         case DEPTH_COMPONENT24:
326                 // There's no DataType value with 24-bit size
327         default:
328                 throw invalid_argument("get_component_type");
329         }
330 }
331
332 unsigned get_pixel_size(PixelFormat pf)
333 {
334         return get_component_count(pf)*get_type_size(get_component_type(pf));
335 }
336
337 void require_pixelformat(PixelFormat pf)
338 {
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
343         extension.*/
344         switch(pf)
345         {
346         case RGB8:
347         case RGBA8:
348                 { static Require _req(OES_required_internalformat); }
349                 break;
350         case R8:
351         case RG8:
352                 { static Require _req(ARB_texture_rg); }
353                 break;
354         case R16F:
355         case R32F:
356         case RG16F:
357         case RG32F:
358                 { static Require _req(ARB_texture_rg); }
359                 { static Require _req(ARB_texture_float); }
360                 break;
361         case RGB16F:
362         case RGB32F:
363         case RGBA16F:
364         case RGBA32F:
365                 { static Require _req(ARB_texture_float); }
366                 break;
367         case SRGB8:
368         case SRGB8_ALPHA8:
369                 { static Require _req(EXT_texture_sRGB); }
370                 break;
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); }
376                 break;
377         case DEPTH_COMPONENT32F:
378                 { static Require _req(ARB_depth_buffer_float); }
379                 break;
380         case STENCIL_INDEX8:
381                 { static Require _req(OES_texture_stencil8); }
382                 break;
383         default:
384                 throw invalid_argument("require_pixelformat");
385         }
386 }
387
388 } // namespace GL
389 } // namespace Msp