From 27942ce1947d3fcd18b67702f4565bb399441bb8 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 17 Feb 2017 10:35:00 +0100 Subject: Optimize PixelBuffer and PixelBufferAlpha setPixel method when no alpha is used. --- plugingui/pixelbuffer.cc | 56 +++++++++++++++++++++++++++++++++++------------- plugingui/pixelbuffer.h | 14 ++++++------ 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/plugingui/pixelbuffer.cc b/plugingui/pixelbuffer.cc index 15270af..bbef84c 100644 --- a/plugingui/pixelbuffer.cc +++ b/plugingui/pixelbuffer.cc @@ -28,7 +28,10 @@ #include -namespace GUI { +#include + +namespace GUI +{ PixelBuffer::PixelBuffer(size_t width, size_t height) : buf(nullptr) @@ -59,13 +62,26 @@ void PixelBuffer::setPixel(size_t x, size_t y, assert(x < width); assert(y < height); - unsigned int a = alpha; - unsigned int b = 255 - alpha; + if(alpha == 0) + { + return; + } - buf[PX(0)] = (unsigned char)(((int)red * a + (int)buf[PX(0)] * b) / 255); - buf[PX(1)] = (unsigned char)(((int)green * a + (int)buf[PX(1)] * b) / 255); - buf[PX(2)] = (unsigned char)(((int)blue * a + (int)buf[PX(2)] * b) / 255); + if(alpha < 255) + { + unsigned int a = alpha; + unsigned int b = 255 - alpha; + buf[PX(0)] = (unsigned char)(((int)red * a + (int)buf[PX(0)] * b) / 255); + buf[PX(1)] = (unsigned char)(((int)green * a + (int)buf[PX(1)] * b) / 255); + buf[PX(2)] = (unsigned char)(((int)blue * a + (int)buf[PX(2)] * b) / 255); + } + else + { + buf[PX(0)] = red; + buf[PX(1)] = green; + buf[PX(2)] = blue; + } } PixelBufferAlpha::PixelBufferAlpha(size_t width, size_t height) @@ -133,17 +149,27 @@ void PixelBufferAlpha::addPixel(size_t x, size_t y, return; } - float a, b; - getAlpha(alpha, buf[PX(3)], a, b); + if(alpha < 255) + { + float a, b; + getAlpha(alpha, buf[PX(3)], a, b); - buf[PX(0)] = (unsigned char)((float)red * a + (float)buf[PX(0)] * b); - buf[PX(0)] /= (a + b); - buf[PX(1)] = (unsigned char)((float)green * a + (float)buf[PX(1)] * b); - buf[PX(1)] /= (a + b); - buf[PX(2)] = (unsigned char)((float)blue * a + (float)buf[PX(2)] * b); - buf[PX(2)] /= (a + b); + buf[PX(0)] = (unsigned char)((float)red * a + (float)buf[PX(0)] * b); + buf[PX(0)] /= (a + b); + buf[PX(1)] = (unsigned char)((float)green * a + (float)buf[PX(1)] * b); + buf[PX(1)] /= (a + b); + buf[PX(2)] = (unsigned char)((float)blue * a + (float)buf[PX(2)] * b); + buf[PX(2)] /= (a + b); - buf[PX(3)] = (a + b) * 255; + buf[PX(3)] = (a + b) * 255; + } + else + { + buf[PX(0)] = red; + buf[PX(1)] = green; + buf[PX(2)] = blue; + buf[PX(3)] = alpha; + } } void PixelBufferAlpha::addPixel(size_t x, size_t y, const Colour& c) diff --git a/plugingui/pixelbuffer.h b/plugingui/pixelbuffer.h index b9096c9..94c5496 100644 --- a/plugingui/pixelbuffer.h +++ b/plugingui/pixelbuffer.h @@ -28,9 +28,10 @@ #include "colour.h" -#include +#include -namespace GUI { +namespace GUI +{ class PixelBuffer { public: @@ -80,10 +81,11 @@ public: bool managed{false}; unsigned char* buf{nullptr}; - size_t width{0}; - size_t height{0}; - size_t x{0}; - size_t y{0}; + std::size_t width{0}; + std::size_t height{0}; + int x{0}; + int y{0}; + bool dirty{true}; }; } // GUI:: -- cgit v1.2.3