From 8b3e51aeff0e22d29a34772c77b8647108f58677 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 30 Sep 2021 14:28:14 +0300 Subject: [PATCH] Add an srgb flag to pixelformat_from_image This simplifies the pixel format handling in the texture classes. --- source/core/pixelformat.cpp | 4 ++-- source/core/pixelformat.h | 2 +- source/core/texture1d.cpp | 5 +---- source/core/texture2d.cpp | 6 +----- source/core/texture3d.cpp | 4 +--- source/core/texturecube.cpp | 7 ++----- 6 files changed, 8 insertions(+), 20 deletions(-) diff --git a/source/core/pixelformat.cpp b/source/core/pixelformat.cpp index 270f27f7..1ee0cd09 100644 --- a/source/core/pixelformat.cpp +++ b/source/core/pixelformat.cpp @@ -106,10 +106,10 @@ PixelComponents components_from_graphics(Graphics::PixelFormat pf) } } -PixelFormat pixelformat_from_image(const Graphics::Image &image) +PixelFormat pixelformat_from_image(const Graphics::Image &image, bool srgb) { PixelComponents comp = components_from_graphics(image.get_format()); - return make_pixelformat(comp, UNSIGNED_BYTE); + return make_pixelformat(comp, UNSIGNED_BYTE, srgb); } PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb) diff --git a/source/core/pixelformat.h b/source/core/pixelformat.h index 2f603845..75651a8f 100644 --- a/source/core/pixelformat.h +++ b/source/core/pixelformat.h @@ -86,7 +86,7 @@ void operator>>(const LexicalConverter &, PixelComponents &); void operator>>(const LexicalConverter &, PixelFormat &); PixelComponents components_from_graphics(Graphics::PixelFormat); -PixelFormat pixelformat_from_image(const Graphics::Image &); +PixelFormat pixelformat_from_image(const Graphics::Image &, bool = false); PixelFormat make_pixelformat(PixelComponents, DataType, bool = false); inline PixelComponents get_components(PixelFormat f) { return static_cast(f&0xFF); } diff --git a/source/core/texture1d.cpp b/source/core/texture1d.cpp index 3fd1ae09..76ff946f 100644 --- a/source/core/texture1d.cpp +++ b/source/core/texture1d.cpp @@ -85,10 +85,7 @@ void Texture1D::image(const Graphics::Image &img, unsigned lv) if(img.get_height()!=1) throw incompatible_data("Texture1D::image"); - unsigned w = img.get_width(); - PixelFormat fmt = pixelformat_from_image(img); - storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv); - + storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), lv); image(0, img.get_pixels()); } diff --git a/source/core/texture2d.cpp b/source/core/texture2d.cpp index 31d70165..703b719a 100644 --- a/source/core/texture2d.cpp +++ b/source/core/texture2d.cpp @@ -120,11 +120,7 @@ void Texture2D::image(const Graphics::Image &img, unsigned lv) void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer) { - unsigned w = img.get_width(); - unsigned h = img.get_height(); - PixelFormat fmt = pixelformat_from_image(img); - storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv); - + storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), img.get_height(), lv); image(0, from_buffer ? 0 : img.get_pixels()); } diff --git a/source/core/texture3d.cpp b/source/core/texture3d.cpp index 03a64f1c..b8e728cf 100644 --- a/source/core/texture3d.cpp +++ b/source/core/texture3d.cpp @@ -108,9 +108,7 @@ void Texture3D::image(const Graphics::Image &img, unsigned lv) unsigned d = h/w; h = w; - PixelFormat fmt = pixelformat_from_image(img); - storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, d, lv); - + storage(pixelformat_from_image(img, use_srgb_format), w, h, d, lv); image(0, img.get_pixels()); } diff --git a/source/core/texturecube.cpp b/source/core/texturecube.cpp index 2a15eb1b..3f8eabb3 100644 --- a/source/core/texturecube.cpp +++ b/source/core/texturecube.cpp @@ -126,9 +126,7 @@ void TextureCube::image(TextureCubeFace face, const Graphics::Image &img) if(w!=h) throw incompatible_data("TextureCube::image"); - PixelFormat fmt = pixelformat_from_image(img); - storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w); - + storage(pixelformat_from_image(img, use_srgb_format), w); image(face, 0, img.get_pixels()); } @@ -141,9 +139,8 @@ void TextureCube::image(const Graphics::Image &img, unsigned lv) throw incompatible_data("TextureCube::image"); h /= 6; - PixelFormat fmt = pixelformat_from_image(img); if(size==0) - storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv); + storage(pixelformat_from_image(img, use_srgb_format), w, lv); else if(w!=size || h!=size) throw incompatible_data("TextureCube::image"); -- 2.45.2