summaryrefslogtreecommitdiffstats
path: root/src/plugins/graphicssystems/meego/dithering.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/graphicssystems/meego/dithering.cpp')
-rw-r--r--src/plugins/graphicssystems/meego/dithering.cpp53
1 files changed, 41 insertions, 12 deletions
diff --git a/src/plugins/graphicssystems/meego/dithering.cpp b/src/plugins/graphicssystems/meego/dithering.cpp
index b50826c..1a2e3fa 100644
--- a/src/plugins/graphicssystems/meego/dithering.cpp
+++ b/src/plugins/graphicssystems/meego/dithering.cpp
@@ -68,7 +68,7 @@
// Writes(ads) a new value to the diffusion accumulator. accumulator is a short.
// x, y is a position in the accumulation buffer. y can be 0 or 1 -- we operate on two lines at time.
-#define ACCUMULATE(accumulator, x, y, width, v) if (x < width && x > 0) accumulator[(y * width) + x] += v
+#define ACCUMULATE(accumulator, x, y, width, v) if (x < width && x >= 0) accumulator[(y * width) + x] += v
// Clamps a value to be in 0..255 range.
#define CLAMP_256(v) if (v > 255) v = 255; if (v < 0) v = 0;
@@ -76,8 +76,13 @@
// Converts incoming RGB32 (QImage::Format_RGB32) to RGB565. Returns the newly allocated data.
unsigned short* convertRGB32_to_RGB565(const unsigned char *in, int width, int height, int stride)
{
+ // Output line stride. Aligned to 4 bytes.
+ int alignedWidth = width;
+ if (alignedWidth % 2 > 0)
+ alignedWidth++;
+
// Will store output
- unsigned short *out = (unsigned short *) malloc(width * height * 2);
+ unsigned short *out = (unsigned short *) malloc(alignedWidth * height * 2);
// Lookup tables for the 8bit => 6bit and 8bit => 5bit conversion
unsigned char lookup_8bit_to_5bit[256];
@@ -105,18 +110,18 @@ unsigned short* convertRGB32_to_RGB565(const unsigned char *in, int width, int h
// Produce the conversion lookup tables.
for (i = 0; i < 256; i++) {
lookup_8bit_to_5bit[i] = round(i / 8.0);
- if (lookup_8bit_to_5bit[i] > 31)
- lookup_8bit_to_5bit[i] -= 1;
// Before bitshifts: (i * 8) - (... * 8 * 8)
lookup_8bit_to_5bit_diff[i] = (i << 3) - (lookup_8bit_to_5bit[i] << 6);
+ if (lookup_8bit_to_5bit[i] > 31)
+ lookup_8bit_to_5bit[i] -= 1;
lookup_8bit_to_6bit[i] = round(i / 4.0);
- if (lookup_8bit_to_6bit[i] > 63)
- lookup_8bit_to_6bit[i] -= 1;
// Before bitshifts: (i * 8) - (... * 4 * 8)
lookup_8bit_to_6bit_diff[i] = (i << 3) - (lookup_8bit_to_6bit[i] << 5);
+ if (lookup_8bit_to_6bit[i] > 63)
+ lookup_8bit_to_6bit[i] -= 1;
}
// Clear the accumulators
@@ -174,7 +179,7 @@ unsigned short* convertRGB32_to_RGB565(const unsigned char *in, int width, int h
}
// Write the newly produced pixel
- PUT_565(out, x, y, width, component[2], component[1], component[0]);
+ PUT_565(out, x, y, alignedWidth, component[2], component[1], component[0]);
}
}
@@ -183,10 +188,16 @@ unsigned short* convertRGB32_to_RGB565(const unsigned char *in, int width, int h
// Converts incoming RGBA32 (QImage::Format_ARGB32_Premultiplied) to RGB565. Returns the newly allocated data.
// This function is similar (yet different) to the _565 variant but it makes sense to duplicate it here for simplicity.
+// The output has each scan line aligned to 4 bytes (as expected by GL by default).
unsigned short* convertARGB32_to_RGBA4444(const unsigned char *in, int width, int height, int stride)
{
+ // Output line stride. Aligned to 4 bytes.
+ int alignedWidth = width;
+ if (alignedWidth % 2 > 0)
+ alignedWidth++;
+
// Will store output
- unsigned short *out = (unsigned short *) malloc(width * height * 2);
+ unsigned short *out = (unsigned short *) malloc(alignedWidth * 2 * height);
// Lookup tables for the 8bit => 4bit conversion
unsigned char lookup_8bit_to_4bit[256];
@@ -210,11 +221,11 @@ unsigned short* convertARGB32_to_RGBA4444(const unsigned char *in, int width, in
// Produce the conversion lookup tables.
for (i = 0; i < 256; i++) {
lookup_8bit_to_4bit[i] = round(i / 16.0);
- if (lookup_8bit_to_4bit[i] > 15)
- lookup_8bit_to_4bit[i] -= 1;
-
// Before bitshifts: (i * 8) - (... * 16 * 8)
lookup_8bit_to_4bit_diff[i] = (i << 3) - (lookup_8bit_to_4bit[i] << 7);
+
+ if (lookup_8bit_to_4bit[i] > 15)
+ lookup_8bit_to_4bit[i] = 15;
}
// Clear the accumulators
@@ -269,7 +280,25 @@ unsigned short* convertARGB32_to_RGBA4444(const unsigned char *in, int width, in
}
// Write the newly produced pixel
- PUT_4444(out, x, y, width, component[0], component[1], component[2], component[3]);
+ PUT_4444(out, x, y, alignedWidth, component[0], component[1], component[2], component[3]);
+ }
+ }
+
+ return out;
+}
+
+unsigned char* convertBGRA32_to_RGBA32(const unsigned char *in, int width, int height, int stride)
+{
+ unsigned char *out = (unsigned char *) malloc(stride * height);
+
+ // For each line...
+ for (int y = 0; y < height; y++) {
+ // For each column
+ for (int x = 0; x < width; x++) {
+ out[(stride * y) + (x * 4) + 0] = in[(stride * y) + (x * 4) + 2];
+ out[(stride * y) + (x * 4) + 1] = in[(stride * y) + (x * 4) + 1];
+ out[(stride * y) + (x * 4) + 2] = in[(stride * y) + (x * 4) + 0];
+ out[(stride * y) + (x * 4) + 3] = in[(stride * y) + (x * 4) + 3];
}
}