1 #include <msp/strings/format.h>
2 #include "pixelformat.h"
9 void operator>>(const LexicalConverter &conv, PixelComponents &comp)
11 if(conv.get()=="STENCIL_INDEX")
13 else if(conv.get()=="DEPTH_COMPONENT")
14 comp = DEPTH_COMPONENT;
15 else if(conv.get()=="RED")
17 else if(conv.get()=="RG")
19 else if(conv.get()=="RGB")
21 else if(conv.get()=="RGBA")
23 else if(conv.get()=="BGR")
25 else if(conv.get()=="BGRA")
27 else if(conv.get()=="LUMINANCE")
29 else if(conv.get()=="LUMINANCE_ALPHA")
30 comp = LUMINANCE_ALPHA;
32 throw lexical_error(format("conversion of '%s' to PixelComponents", conv.get()));
35 void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
39 else if(conv.get()=="R16F")
41 else if(conv.get()=="R32F")
43 else if(conv.get()=="RG8")
45 else if(conv.get()=="RG16F")
47 else if(conv.get()=="RG32F")
49 else if(conv.get()=="RGB8")
51 else if(conv.get()=="RGB16F")
53 else if(conv.get()=="RGB32F")
55 else if(conv.get()=="RGBA8")
57 else if(conv.get()=="RGBA16F")
59 else if(conv.get()=="RGBA32F")
61 else if(conv.get()=="SRGB8")
63 else if(conv.get()=="SRGB8_ALPHA8")
65 else if(conv.get()=="BGR8")
67 else if(conv.get()=="BGRA8")
69 else if(conv.get()=="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;
82 throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
85 PixelComponents components_from_graphics(Graphics::PixelFormat pf)
89 case Graphics::LUMINANCE: return LUMINANCE;
90 case Graphics::LUMINANCE_ALPHA: return LUMINANCE_ALPHA;
91 case Graphics::RGB: return RGB;
93 case Graphics::RGBA: return RGBA;
94 case Graphics::BGR: return BGR;
96 case Graphics::BGRA: return BGRA;
97 default: throw invalid_argument("pixelformat_from_graphics");
101 PixelFormat pixelformat_from_image(const Graphics::Image &image, bool srgb)
103 PixelComponents comp = components_from_graphics(image.get_format());
104 return make_pixelformat(comp, UNSIGNED_BYTE, srgb);
107 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
109 bool normalized = !is_float(type);
110 return static_cast<PixelFormat>(comp | get_type_size(type)<<8 | (type&0x300)<<4 | normalized*0x4000 | srgb*0x8000);