]> git.tdb.fi Git - libs/gl.git/blobdiff - source/pixelformat.cpp
Separate abstract pixel compositions from concrete pixel formats
[libs/gl.git] / source / pixelformat.cpp
index dcc6b4eea70d7c38eb07020d53b9f4aa107a9242..cc6a6d007067a671dd8e849ba8e9ad4951787e74 100644 (file)
@@ -1,5 +1,5 @@
 #include <msp/gl/extensions/arb_texture_float.h>
-#include <msp/gl/extensions/ext_bgra.h>
+#include <msp/io/print.h>
 #include <msp/strings/format.h>
 #include "pixelformat.h"
 
@@ -8,37 +8,84 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
+void operator>>(const LexicalConverter &conv, PixelComponents &comp)
 {
        if(conv.get()=="STENCIL_INDEX")
-               fmt = STENCIL_INDEX;
+               comp = STENCIL_INDEX;
        else if(conv.get()=="DEPTH_COMPONENT")
-               fmt = DEPTH_COMPONENT;
+               comp = DEPTH_COMPONENT;
        else if(conv.get()=="RED")
-               fmt = RED;
+               comp = RED;
        else if(conv.get()=="RG")
-               fmt = RG;
+               comp = RG;
        else if(conv.get()=="RGB")
-               fmt = RGB;
+               comp = RGB;
        else if(conv.get()=="RGBA")
-               fmt = RGBA;
+               comp = RGBA;
        else if(conv.get()=="BGR")
-               fmt = BGR;
+               comp = BGR;
        else if(conv.get()=="BGRA")
-               fmt = BGRA;
-       else if(conv.get()=="SRGB")
-               fmt = SRGB;
-       else if(conv.get()=="SRGB_ALPHA")
-               fmt = SRGB_ALPHA;
+               comp = BGRA;
        else if(conv.get()=="LUMINANCE")
-               fmt = LUMINANCE;
+               comp = LUMINANCE;
        else if(conv.get()=="LUMINANCE_ALPHA")
-               fmt = LUMINANCE_ALPHA;
+               comp = LUMINANCE_ALPHA;
        else
                throw lexical_error(format("conversion of '%s' to PixelFormat", conv.get()));
 }
 
-PixelFormat pixelformat_from_graphics(Graphics::PixelFormat pf)
+void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
+{
+       if(conv.get()=="R8")
+               fmt = R8;
+       else if(conv.get()=="R16F")
+               fmt = R16F;
+       else if(conv.get()=="R32F")
+               fmt = R32F;
+       else if(conv.get()=="RG8")
+               fmt = RG8;
+       else if(conv.get()=="RG16F")
+               fmt = RG16F;
+       else if(conv.get()=="RG32F")
+               fmt = RG32F;
+       else if(conv.get()=="RGB8")
+               fmt = RGB8;
+       else if(conv.get()=="RGB16F")
+               fmt = RGB16F;
+       else if(conv.get()=="RGB32F")
+               fmt = RGB32F;
+       else if(conv.get()=="RGBA8")
+               fmt = RGBA8;
+       else if(conv.get()=="RGBA16F")
+               fmt = RGBA16F;
+       else if(conv.get()=="RGBA32F")
+               fmt = RGBA32F;
+       else if(conv.get()=="SRGB8")
+               fmt = SRGB8;
+       else if(conv.get()=="SRGB8_ALPHA8")
+               fmt = SRGB8_ALPHA8;
+       else if(conv.get()=="LUMINANCE8")
+               fmt = LUMINANCE8;
+       else if(conv.get()=="LUMINANCE8_ALPHA8")
+               fmt = LUMINANCE8_ALPHA8;
+       else if(conv.get()=="DEPTH_COMPONENT16")
+               fmt = DEPTH_COMPONENT16;
+       else if(conv.get()=="DEPTH_COMPONENT24")
+               fmt = DEPTH_COMPONENT24;
+       else if(conv.get()=="DEPTH_COMPONENT32")
+               fmt = DEPTH_COMPONENT32;
+       else if(conv.get()=="DEPTH_COMPONENT32F")
+               fmt = DEPTH_COMPONENT32F;
+       else
+       {
+               PixelComponents comp;
+               conv >> comp;
+               fmt = make_pixelformat(comp, (comp==DEPTH_COMPONENT ? FLOAT : UNSIGNED_BYTE));
+               IO::print(IO::cerr, "Warning: deprecated conversion of '%s' to PixelFormat\n", conv.get());
+       }
+}
+
+PixelComponents pixelformat_from_graphics(Graphics::PixelFormat pf)
 {
        switch(pf)
        {
@@ -54,36 +101,105 @@ PixelFormat pixelformat_from_graphics(Graphics::PixelFormat pf)
        }
 }
 
-PixelFormat storage_pixelformat_from_graphics(Graphics::PixelFormat pf, bool srgb)
+PixelComponents storage_pixelformat_from_graphics(Graphics::PixelFormat pf)
 {
-       PixelFormat result;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
        switch(pf)
        {
        case Graphics::RGBX:
        case Graphics::BGR:
-       case Graphics::BGRX: result = RGB; break;
-       case Graphics::BGRA: result = RGBA; break;
-       default: result = pixelformat_from_graphics(pf);
+       case Graphics::BGRX: return RGB;
+       case Graphics::BGRA: return RGBA;
+       default: return pixelformat_from_graphics(pf);
        }
+#pragma GCC diagnostic pop
+}
 
-       if(srgb)
-               return get_srgb_pixelformat(result);
-       else
-               return result;
+PixelFormat pixelformat_from_image(const Graphics::Image &image)
+{
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+       PixelComponents comp = pixelformat_from_graphics(image.get_format());
+#pragma GCC diagnostic pop
+       return make_pixelformat(comp, UNSIGNED_BYTE);
+}
+
+PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
+{
+       if(srgb && type!=UNSIGNED_BYTE && comp!=RGB && comp!=RGBA)
+               throw invalid_argument("make_pixelformat");
+
+       switch(comp)
+       {
+       case RED:
+               switch(type)
+               {
+               case UNSIGNED_BYTE: return R8;
+               case HALF_FLOAT: return R16F;
+               case FLOAT: return R32F;
+               default: throw invalid_argument("make_pixelformat");
+               }
+       case RG:
+               switch(type)
+               {
+               case UNSIGNED_BYTE: return RG8;
+               case HALF_FLOAT: return RG16F;
+               case FLOAT: return RG32F;
+               default: throw invalid_argument("make_pixelformat");
+               }
+       case RGB:
+               switch(type)
+               {
+               case UNSIGNED_BYTE: return (srgb ? SRGB8 : RGB8);
+               case HALF_FLOAT: return RGB16F;
+               case FLOAT: return RGB32F;
+               default: throw invalid_argument("make_pixelformat");
+               }
+       case RGBA:
+               switch(type)
+               {
+               case UNSIGNED_BYTE: return (srgb ? SRGB8_ALPHA8 : RGBA8);
+               case HALF_FLOAT: return RGBA16F;
+               case FLOAT: return RGBA32F;
+               default: throw invalid_argument("make_pixelformat");
+               }
+       case LUMINANCE:
+               if(type!=UNSIGNED_BYTE)
+                       throw invalid_argument("make_pixelformat");
+               return LUMINANCE8;
+       case LUMINANCE_ALPHA:
+               if(type!=UNSIGNED_BYTE)
+                       throw invalid_argument("make_pixelformat");
+               return LUMINANCE8;
+       case STENCIL_INDEX:
+               if(type!=UNSIGNED_BYTE)
+                       throw invalid_argument("make_pixelformat");
+               return STENCIL_INDEX8;
+       case DEPTH_COMPONENT:
+               switch(type)
+               {
+               case UNSIGNED_SHORT: return DEPTH_COMPONENT16;
+               case UNSIGNED_INT: return DEPTH_COMPONENT32;
+               case FLOAT: return DEPTH_COMPONENT32F;
+               default: throw invalid_argument("make_pixelformat");
+               }
+       default:
+               throw invalid_argument("make_pixelformat");
+       }
 }
 
 PixelFormat get_base_pixelformat(PixelFormat pf)
 {
-       PixelFormat unsized = get_unsized_pixelformat(pf);
-       switch(unsized)
+       switch(pf)
        {
-       case SRGB: return RGB;
-       case SRGB_ALPHA: return RGBA;
-       default: return unsized;
+       case SRGB8: return RGB8;
+       case SRGB8_ALPHA8: return RGBA8;
+       default: return pf;
        }
 }
 
-PixelFormat get_unsized_pixelformat(PixelFormat pf)
+PixelComponents get_components(PixelFormat pf)
 {
        switch(pf)
        {
@@ -95,99 +211,49 @@ PixelFormat get_unsized_pixelformat(PixelFormat pf)
        case RG32F: return RG;
        case RGB8:
        case RGB16F:
-       case RGB32F: return RGB;
-       case SRGB8: return SRGB;
+       case RGB32F:
+       case SRGB8: return RGB;
        case RGBA8:
        case RGBA16F:
-       case RGBA32F: return RGBA;
-       case SRGB8_ALPHA8: return SRGB_ALPHA;
+       case RGBA32F:
+       case SRGB8_ALPHA8: return RGBA;
+       case LUMINANCE8: return LUMINANCE;
+       case LUMINANCE8_ALPHA8: return LUMINANCE_ALPHA;
+       case STENCIL_INDEX8: return STENCIL_INDEX;
        case DEPTH_COMPONENT16:
        case DEPTH_COMPONENT24:
        case DEPTH_COMPONENT32:
        case DEPTH_COMPONENT32F: return DEPTH_COMPONENT;
-       default: return pf;
-       }
-}
-
-PixelFormat get_sized_pixelformat(PixelFormat pf, unsigned size)
-{
-       if(!size || size>4)
-               throw invalid_argument("get_sized_pixelformat");
-
-       pf = get_unsized_pixelformat(pf);
-
-       switch(size)
-       {
-       case 1:
-               switch(pf)
-               {
-               case RED: return R8;
-               case RG: return RG8;
-               case RGB: return RGB8;
-               case RGBA: return RGBA8;
-               case SRGB: return SRGB8;
-               case SRGB_ALPHA: return SRGB8_ALPHA8;
-               default: throw invalid_argument("get_sized_pixelformat");
-               }
-       case 2:
-               switch(pf)
-               {
-               case RED: return R16F;
-               case RG: return RG16F;
-               case RGB: return RGB16F;
-               case RGBA: return RGBA16F;
-               case DEPTH_COMPONENT: return DEPTH_COMPONENT16;
-               default: throw invalid_argument("get_sized_pixelformat");
-               }
-       case 3:
-               if(pf==DEPTH_COMPONENT)
-                       return DEPTH_COMPONENT24;
-               else
-                       throw invalid_argument("get_sized_pixelformat");
-       case 4:
-               switch(pf)
-               {
-               case RED: return R32F;
-               case RG: return RG32F;
-               case RGB: return RGB32F;
-               case RGBA: return RGBA32F;
-               case DEPTH_COMPONENT: return ARB_depth_buffer_float ? DEPTH_COMPONENT32F : DEPTH_COMPONENT32;
-               default: throw invalid_argument("get_sized_pixelformat");
-               }
-       default:
-               throw invalid_argument("get_sized_pixelformat");
+       default: throw invalid_argument("get_components");
        }
 }
 
-PixelFormat get_default_sized_pixelformat(PixelFormat pf)
+PixelFormat get_default_sized_pixelformat(PixelComponents comp)
 {
-       pf = get_unsized_pixelformat(pf);
-       unsigned size = 1;
-       if(pf==DEPTH_COMPONENT)
+       DataType type = UNSIGNED_BYTE;
+       if(comp==DEPTH_COMPONENT)
        {
                if(get_gl_api()==OPENGL_ES2 && !ARB_depth_buffer_float)
-                       size = 2;
+                       type = UNSIGNED_SHORT;
                else
-                       size = 4;
+                       type = FLOAT;
        }
-       return get_sized_pixelformat(pf, size);
+       return make_pixelformat(comp, type);
 }
 
 PixelFormat get_srgb_pixelformat(PixelFormat pf)
 {
        switch(pf)
        {
-       case RGB: return SRGB;
-       case RGBA: return SRGB_ALPHA;
        case RGB8: return SRGB8;
        case RGBA8: return SRGB8_ALPHA8;
        default: return pf;
        }
 }
 
-unsigned get_component_count(PixelFormat pf)
+unsigned get_component_count(PixelComponents comp)
 {
-       switch(get_base_pixelformat(pf))
+       switch(comp)
        {
        case STENCIL_INDEX:
        case DEPTH_COMPONENT:
@@ -208,7 +274,7 @@ unsigned get_component_count(PixelFormat pf)
        }
 }
 
-unsigned get_component_size(PixelFormat pf)
+DataType get_component_type(PixelFormat pf)
 {
        switch(pf)
        {
@@ -218,30 +284,34 @@ unsigned get_component_size(PixelFormat pf)
        case RGBA8:
        case SRGB8:
        case SRGB8_ALPHA8:
-               return 1;
+       case LUMINANCE8:
+       case LUMINANCE8_ALPHA8:
+               return UNSIGNED_BYTE;
        case R16F:
        case RG16F:
        case RGB16F:
        case RGBA16F:
+               return HALF_FLOAT;
        case DEPTH_COMPONENT16:
-               return 2;
-       case DEPTH_COMPONENT24:
-               return 3;
+               return UNSIGNED_SHORT;
        case R32F:
        case RG32F:
        case RGB32F:
        case RGBA32F:
        case DEPTH_COMPONENT32:
+               return UNSIGNED_INT;
        case DEPTH_COMPONENT32F:
-               return 4;
+               return FLOAT;
+       case DEPTH_COMPONENT24:
+               // There's no DataType value with 24-bit size
        default:
-               return 0;
+               throw invalid_argument("get_component_type");
        }
 }
 
 unsigned get_pixel_size(PixelFormat pf)
 {
-       return get_component_count(pf)*max(get_component_size(pf), 1U);
+       return get_component_count(pf)*get_type_size(get_component_type(pf));
 }
 
 void require_pixelformat(PixelFormat pf)
@@ -257,9 +327,7 @@ void require_pixelformat(PixelFormat pf)
        case RGBA8:
                { static Require _req(OES_required_internalformat); }
                break;
-       case RED:
        case R8:
-       case RG:
        case RG8:
                { static Require _req(ARB_texture_rg); }
                break;
@@ -276,19 +344,10 @@ void require_pixelformat(PixelFormat pf)
        case RGBA32F:
                { static Require _req(ARB_texture_float); }
                break;
-       case SRGB:
        case SRGB8:
-       case SRGB_ALPHA:
        case SRGB8_ALPHA8:
                { static Require _req(EXT_texture_sRGB); }
                break;
-       case BGR:
-       case BGRA:
-               { static Require _req(EXT_bgra); }
-               break;
-       case DEPTH_COMPONENT:
-               { static Require _req(ARB_depth_texture); }
-               break;
        case DEPTH_COMPONENT16:
        case DEPTH_COMPONENT24:
        case DEPTH_COMPONENT32:
@@ -298,11 +357,11 @@ void require_pixelformat(PixelFormat pf)
        case DEPTH_COMPONENT32F:
                { static Require _req(ARB_depth_buffer_float); }
                break;
-       case STENCIL_INDEX:
+       case STENCIL_INDEX8:
                { static Require _req(OES_texture_stencil8); }
                break;
        default:
-               break;
+               throw invalid_argument("require_pixelformat");
        }
 }