]> git.tdb.fi Git - libs/gl.git/blob - source/core/pixelformat.h
Rework PixelComponents and PixelFormat to use custom values
[libs/gl.git] / source / core / pixelformat.h
1 #ifndef MSP_GL_PIXELFORMAT_H_
2 #define MSP_GL_PIXELFORMAT_H_
3
4 #include <msp/core/attributes.h>
5 #include <msp/graphics/image.h>
6 #include <msp/strings/lexicalcast.h>
7 #include "gl.h"
8 #include <msp/gl/extensions/arb_depth_buffer_float.h>
9 #include <msp/gl/extensions/arb_depth_texture.h>
10 #include <msp/gl/extensions/arb_texture_float.h>
11 #include <msp/gl/extensions/arb_texture_rg.h>
12 #include <msp/gl/extensions/ext_bgra.h>
13 #include <msp/gl/extensions/ext_texture_srgb.h>
14 #include <msp/gl/extensions/oes_required_internalformat.h>
15 #include <msp/gl/extensions/oes_texture_stencil8.h>
16 #include <msp/gl/extensions/msp_luminance_formats.h>
17 #include "datatype.h"
18
19 namespace Msp {
20 namespace GL {
21
22 /**
23 Identifies the components of a pixel, without type information.  The values
24 are bitfields laid as follows:
25
26 _grs dccc
27  │││ │  └╴Number of components
28  │││ └───╴Depth flag
29  ││└─────╴Stencil flag
30  │└──────╴Reverse order flag
31  └───────╴Grayscale flag
32
33 This information is presented for internal documentation purposes only; it is
34 inadvisable for programs to rely on it.
35 */
36 enum PixelComponents
37 {
38         RED = 0x01,
39         RG = 0x02,
40         RGB = 0x03,
41         RGBA = 0x04,
42         DEPTH_COMPONENT = 0x09,
43         STENCIL_INDEX = 0x11,
44         BGR = 0x23,
45         BGRA = 0x24,
46         LUMINANCE = 0x41,
47         LUMINANCE_ALPHA = 0x42
48 };
49
50 /**
51 Identifies a pixel format, with components and type.  The values are bitfields
52 laid as follows:
53
54 tnfg ssss cccc cccc
55 ││││    │         └╴Components (see PixelComponents)
56 ││││    └──────────╴Size of one component (bytes)
57 │││└───────────────╴Signed flag
58 ││└────────────────╴Floating-point flag
59 │└─────────────────╴Normalized flag
60 └──────────────────╴sRGB flag
61
62 This information is presented for internal documentation purposes only; it is
63 inadvisable for programs to rely on it.
64 */
65 enum PixelFormat
66 {
67         R8 = 0x4100|RED,
68         R16F = 0x3200|RED,
69         R32F = 0x3400|RED,
70         RG8 = 0x4100|RG,
71         RG16F = 0x3200|RG,
72         RG32F = 0x3400|RG,
73         RGB8 = 0x4100|RGB,
74         RGB16F = 0x3200|RGB,
75         RGB32F = 0x3400|RGB,
76         RGBA8 = 0x4100|RGBA,
77         RGBA16F = 0x3200|RGBA,
78         RGBA32F = 0x3400|RGBA,
79         SRGB8 = 0xC100|RGB,
80         SRGB8_ALPHA8 = 0xC100|RGBA,
81         BGR8 = 0x4100|BGR,
82         BGRA8 = 0x4100|BGRA,
83         SBGR8 = 0xC100|BGR,
84         SBGR8_ALPHA8 = 0xC100|BGRA,
85         LUMINANCE8 = 0x4100|LUMINANCE,
86         LUMINANCE8_ALPHA8 = 0x4100|LUMINANCE_ALPHA,
87         DEPTH_COMPONENT16 = 0x4200|DEPTH_COMPONENT,
88         DEPTH_COMPONENT24 = 0x4300|DEPTH_COMPONENT,
89         DEPTH_COMPONENT32 = 0x4400|DEPTH_COMPONENT,
90         DEPTH_COMPONENT32F = 0x3400|DEPTH_COMPONENT,
91         STENCIL_INDEX8 = 0x0100|STENCIL_INDEX
92 };
93
94 void operator>>(const LexicalConverter &, PixelComponents &);
95 void operator>>(const LexicalConverter &, PixelFormat &);
96
97 PixelComponents components_from_graphics(Graphics::PixelFormat);
98 PixelFormat pixelformat_from_image(const Graphics::Image &);
99
100 PixelFormat make_pixelformat(PixelComponents, DataType, bool = false);
101 inline PixelComponents get_components(PixelFormat f) { return static_cast<PixelComponents>(f&0xFF); }
102 inline unsigned get_component_count(PixelComponents c) { return c&7; }
103 inline unsigned get_component_count(PixelFormat f) { return get_component_count(get_components(f)); }
104 inline DataType get_component_type(PixelFormat f) { return static_cast<DataType>((f&0xF00)>>8 | (f&0x3000)>>4); }
105 inline unsigned get_component_size(PixelFormat f) { return get_type_size(get_component_type(f)); }
106 inline bool is_srgb(PixelFormat f) { return f&0x8000; }
107 inline unsigned get_pixel_size(PixelFormat f) { return get_component_count(f)*get_type_size(get_component_type(f)); }
108
109 void require_pixelformat(PixelFormat);
110
111 GLenum get_gl_components(PixelComponents);
112 GLenum get_gl_pixelformat(PixelFormat);
113
114 } // namespace GL
115 } // namespace Msp
116
117 #endif