summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2017-02-17 10:35:00 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2017-02-17 10:35:00 +0100
commit27942ce1947d3fcd18b67702f4565bb399441bb8 (patch)
treec694568db9605ec6b7a7ff6c1db178b81a9c4629
parent57f5e6102deaf8903cff99ab30546e536c4d264d (diff)
Optimize PixelBuffer and PixelBufferAlpha setPixel method when no alpha is used.
-rw-r--r--plugingui/pixelbuffer.cc56
-rw-r--r--plugingui/pixelbuffer.h14
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 <cassert>
-namespace GUI {
+#include <cstdlib>
+
+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 <stdlib.h>
+#include <cstddef>
-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::