]> git.tdb.fi Git - libs/gl.git/blob - source/core/pixelformat.cpp
Mark the start of a SPIR-V function as reachable
[libs/gl.git] / source / core / pixelformat.cpp
1 #include <msp/strings/format.h>
2 #include "pixelformat.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8
9 void operator>>(const LexicalConverter &conv, PixelComponents &comp)
10 {
11         if(conv.get()=="STENCIL_INDEX")
12                 comp = STENCIL_INDEX;
13         else if(conv.get()=="DEPTH_COMPONENT")
14                 comp = DEPTH_COMPONENT;
15         else if(conv.get()=="RED")
16                 comp = RED;
17         else if(conv.get()=="RG")
18                 comp = RG;
19         else if(conv.get()=="RGB")
20                 comp = RGB;
21         else if(conv.get()=="RGBA")
22                 comp = RGBA;
23         else if(conv.get()=="BGR")
24                 comp = BGR;
25         else if(conv.get()=="BGRA")
26                 comp = BGRA;
27         else if(conv.get()=="LUMINANCE")
28                 comp = LUMINANCE;
29         else if(conv.get()=="LUMINANCE_ALPHA")
30                 comp = LUMINANCE_ALPHA;
31         else
32                 throw lexical_error(format("conversion of '%s' to PixelComponents", conv.get()));
33 }
34
35 void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
36 {
37         if(conv.get()=="R8")
38                 fmt = R8;
39         else if(conv.get()=="R16F")
40                 fmt = R16F;
41         else if(conv.get()=="R32F")
42                 fmt = R32F;
43         else if(conv.get()=="RG8")
44                 fmt = RG8;
45         else if(conv.get()=="RG16F")
46                 fmt = RG16F;
47         else if(conv.get()=="RG32F")
48                 fmt = RG32F;
49         else if(conv.get()=="RGB8")
50                 fmt = RGB8;
51         else if(conv.get()=="RGB16F")
52                 fmt = RGB16F;
53         else if(conv.get()=="RGB32F")
54                 fmt = RGB32F;
55         else if(conv.get()=="RGBA8")
56                 fmt = RGBA8;
57         else if(conv.get()=="RGBA16F")
58                 fmt = RGBA16F;
59         else if(conv.get()=="RGBA32F")
60                 fmt = RGBA32F;
61         else if(conv.get()=="SRGB8")
62                 fmt = SRGB8;
63         else if(conv.get()=="SRGB8_ALPHA8")
64                 fmt = SRGB8_ALPHA8;
65         else if(conv.get()=="BGR8")
66                 fmt = BGR8;
67         else if(conv.get()=="BGRA8")
68                 fmt = BGRA8;
69         else if(conv.get()=="LUMINANCE8")
70                 fmt = LUMINANCE8;
71         else if(conv.get()=="LUMINANCE8_ALPHA8")
72                 fmt = LUMINANCE8_ALPHA8;
73         else if(conv.get()=="DEPTH_COMPONENT16")
74                 fmt = DEPTH_COMPONENT16;
75         else if(conv.get()=="DEPTH_COMPONENT24")
76                 fmt = DEPTH_COMPONENT24;
77         else if(conv.get()=="DEPTH_COMPONENT32")
78                 fmt = DEPTH_COMPONENT32;
79         else if(conv.get()=="DEPTH_COMPONENT32F")
80                 fmt = DEPTH_COMPONENT32F;
81         else
82                 throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
83 }
84
85 PixelComponents components_from_graphics(Graphics::PixelFormat pf)
86 {
87         switch(pf)
88         {
89         case Graphics::LUMINANCE: return LUMINANCE;
90         case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
91         case Graphics::RGB: return RGB;
92         case Graphics::RGBX:
93         case Graphics::RGBA: return RGBA;
94         case Graphics::BGR: return BGR;
95         case Graphics::BGRX:
96         case Graphics::BGRA: return BGRA;
97         default: throw invalid_argument("pixelformat_from_graphics");
98         }
99 }
100
101 PixelFormat pixelformat_from_image(const Graphics::Image &image, bool srgb)
102 {
103         PixelComponents comp = components_from_graphics(image.get_format());
104         return make_pixelformat(comp, UNSIGNED_BYTE, srgb);
105 }
106
107 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
108 {
109         bool normalized = !is_float(type);
110         return static_cast<PixelFormat>(comp | get_type_size(type)<<8 | (type&0x300)<<4 | normalized*0x4000 | srgb*0x8000);
111 }
112
113 } // namespace GL
114 } // namespace Msp