From 5f58bfbadbd8acff6f32fef8ae2bbfe61627f283 Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 17 May 2021 22:57:09 +0100 Subject: [PATCH] Improve backwards compatibility of OpenGLImage Signed-off-by: falkTX --- dgl/ImageBase.hpp | 1 + dgl/OpenGL.hpp | 57 +++++++++++++++++++++++++++++++++++++++------- dgl/src/Cairo.cpp | 6 +++++ dgl/src/OpenGL.cpp | 18 +++++++++++++++ 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/dgl/ImageBase.hpp b/dgl/ImageBase.hpp index 1e41bd63..36c2ab28 100644 --- a/dgl/ImageBase.hpp +++ b/dgl/ImageBase.hpp @@ -25,6 +25,7 @@ START_NAMESPACE_DGL enum ImageFormat { kImageFormatNull, + kImageFormatGrayscale, kImageFormatBGR, kImageFormatBGRA, kImageFormatRGB, diff --git a/dgl/OpenGL.hpp b/dgl/OpenGL.hpp index 29c9018f..a9769355 100644 --- a/dgl/OpenGL.hpp +++ b/dgl/OpenGL.hpp @@ -117,6 +117,26 @@ struct OpenGLGraphicsContext : GraphicsContext // ----------------------------------------------------------------------- +static inline +ImageFormat asDISTRHOImageFormat(const GLenum format) +{ + switch (format) + { + case GL_LUMINANCE: + return kImageFormatGrayscale; + case GL_BGR: + return kImageFormatBGR; + case GL_BGRA: + return kImageFormatBGRA; + case GL_RGB: + return kImageFormatRGB; + case GL_RGBA: + return kImageFormatRGBA; + } + + return kImageFormatNull; +} + static inline GLenum asOpenGLImageFormat(const ImageFormat format) { @@ -124,6 +144,8 @@ GLenum asOpenGLImageFormat(const ImageFormat format) { case kImageFormatNull: break; + case kImageFormatGrayscale: + return GL_LUMINANCE; case kImageFormatBGR: return GL_BGR; case kImageFormatBGRA: @@ -199,32 +221,51 @@ public: */ OpenGLImage& operator=(const OpenGLImage& image) noexcept; + // FIXME this should not be needed + inline void loadFromMemory(const char* rawData, uint w, uint h, ImageFormat format = kImageFormatBGRA) + { loadFromMemory(rawData, Size(w, h), format); }; + inline void draw(const GraphicsContext& context) + { drawAt(context, Point(0, 0)); }; + inline void drawAt(const GraphicsContext& context, int x, int y) + { drawAt(context, Point(x, y)); }; + + /** + Constructor using raw image data, specifying an OpenGL image format. + @note @a rawData must remain valid for the lifetime of this Image. + DEPRECATED This constructor uses OpenGL image format instead of DISTRHO one. + */ + DISTRHO_DEPRECATED_BY("OpenGLImage(const char*,uint,uint,ImageFormat") + explicit OpenGLImage(const char* rawData, uint width, uint height, GLenum format); + + /** + Constructor using raw image data, specifying an OpenGL image format. + @note @a rawData must remain valid for the lifetime of this Image. + DEPRECATED This constructor uses OpenGL image format instead of DISTRHO one. + */ + DISTRHO_DEPRECATED_BY("OpenGLImage(const char*,const Size&,ImageFormat") + explicit OpenGLImage(const char* rawData, const Size& size, GLenum format); + /** Draw this image at (0, 0) point using the current OpenGL context. + DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL. */ DISTRHO_DEPRECATED_BY("draw(const GraphicsContext&)") void draw(); /** Draw this image at (x, y) point using the current OpenGL context. + DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL. */ DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&,int,int)") void drawAt(const int x, const int y); /** Draw this image at position @a pos using the current OpenGL context. + DEPRECATED This function does not take into consideration the current graphics context and only works in OpenGL. */ DISTRHO_DEPRECATED_BY("drawAt(const GraphicsContext&,const Point&)") void drawAt(const Point& pos); - // FIXME this should not be needed - inline void loadFromMemory(const char* rawData, uint w, uint h, ImageFormat format) - { loadFromMemory(rawData, Size(w, h), format); }; - inline void draw(const GraphicsContext& context) - { drawAt(context, Point(0, 0)); }; - inline void drawAt(const GraphicsContext& context, int x, int y) - { drawAt(context, Point(x, y)); }; - /** Get the image type. DEPRECATED Type is always assumed to be GL_UNSIGNED_BYTE. diff --git a/dgl/src/Cairo.cpp b/dgl/src/Cairo.cpp index e51cda98..46fb232e 100644 --- a/dgl/src/Cairo.cpp +++ b/dgl/src/Cairo.cpp @@ -280,6 +280,8 @@ static cairo_format_t asCairoImageFormat(const ImageFormat format) { case kImageFormatNull: break; + case kImageFormatGrayscale: + return CAIRO_FORMAT_A8; case kImageFormatBGR: case kImageFormatRGB: return CAIRO_FORMAT_RGB24; @@ -366,6 +368,10 @@ void CairoImage::loadFromMemory(const char* const rdata, const Size& s, co { case kImageFormatNull: break; + case kImageFormatGrayscale: + // Grayscale to A8 + // TODO + break; case kImageFormatBGR: // BGR8 to CAIRO_FORMAT_RGB24 for (uint h = 0; h < height; ++h) diff --git a/dgl/src/OpenGL.cpp b/dgl/src/OpenGL.cpp index f96a6ae6..43a0ded3 100644 --- a/dgl/src/OpenGL.cpp +++ b/dgl/src/OpenGL.cpp @@ -401,6 +401,24 @@ OpenGLImage& OpenGLImage::operator=(const OpenGLImage& image) noexcept } // deprecated calls +OpenGLImage::OpenGLImage(const char* const rawData, const uint width, const uint height, const GLenum format) + : ImageBase(rawData, width, height, asDISTRHOImageFormat(format)), + textureId(0), + setupCalled(false) +{ + glGenTextures(1, &textureId); + DISTRHO_SAFE_ASSERT(textureId != 0); +} + +OpenGLImage::OpenGLImage(const char* const rawData, const Size& size, const GLenum format) + : ImageBase(rawData, size, asDISTRHOImageFormat(format)), + textureId(0), + setupCalled(false) +{ + glGenTextures(1, &textureId); + DISTRHO_SAFE_ASSERT(textureId != 0); +} + void OpenGLImage::draw() { drawOpenGLImage(*this, Point(0, 0), textureId, setupCalled);