summaryrefslogtreecommitdiffstats
path: root/src/plugins/graphicssystems/meego/dithering.cpp
blob: 2561c22ff6d5793d9e8b904c851e8dd6886e23d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

// Implements two dithering methods:
//
//  * convertRGBA32_to_RGB565
//
// This is implemented using Ordered Bayer Dithering. The code has been adapted
// from QX11PixmapData::fromImage. This method was originally implemented using
// Floyd-Steinberg dithering but was later changed to Ordered Dithering because
// of the better quality of the results.
//
//  * convertRGBA32_to_RGBA4444
//
// This is implemented using Floyd-Steinberg dithering.
//
// The alghorithm used here is not the fastest possible but it's prolly fast enough:
// uses look-up tables, integer-only arthmetics and works in one pass on two lines
// at a time. It's a high-quality dithering using 1/8 diffusion precission.
// Each channel (RGBA) is diffused independently and alpha is dithered too.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <QVarLengthArray>

// Gets a component (red = 1, green = 2...) from a RGBA data structure.
// data is unsigned char. stride is the number of bytes per line.
#define GET_RGBA_COMPONENT(data, x, y, stride, c) (data[(y * stride) + (x << 2) + c])

// Writes a new pixel with r, g, b to data in 565 16bit format. Data is a short.
#define PUT_565(data, x, y, width, r, g, b) (data[(y * width) + x] = (r << 11) | (g << 5) | b)

// Writes a new pixel with r, g, b, a to data in 4444 RGBA 16bit format. Data is a short.
#define PUT_4444(data, x, y, width, r, g, b, a) (data[(y * width) + x] = (r << 12) | (g << 8) | (b << 4) | a)

// 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

// Clamps a value to be in 0..255 range.
#define CLAMP_256(v) if (v > 255) v = 255; if (v < 0) v = 0;

// 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)
{
    static bool thresholdMapInitialized = false;
    static int thresholdMap[16][16];

    if (!thresholdMapInitialized) {
        int i;
        int j;
        int n;

        thresholdMap[0][0] = 0;
        thresholdMap[1][0] = 2;
        thresholdMap[0][1] = 3;
        thresholdMap[1][1] = 1;

        for (n=2; n<16; n*=2) {
            for (i=0; i<n; i++) {
                for (j=0; j<n; j++) {
                    thresholdMap[i][j]    *= 4;
                    thresholdMap[i+n][j]   = thresholdMap[i][j] + 2;
                    thresholdMap[i][j+n]   = thresholdMap[i][j] + 3;
                    thresholdMap[i+n][j+n] = thresholdMap[i][j] + 1;
                }
            }
        }

        thresholdMapInitialized = true;
    }

    // Output line stride. Aligned to 4 bytes.
    int alignedWidth = width;
    if (alignedWidth % 2 > 0)
        alignedWidth++;

    // Will store output
    unsigned short *out = (unsigned short *)malloc (alignedWidth * height * 2);

    int x;
    int y;
    int threshold;

    // For each line...
    for (y = 0; y < height; y++) {

        // For each column....
        for (x = 0; x < width; x++) {

            int r = GET_RGBA_COMPONENT(in, x, y, stride, 0);
            int g = GET_RGBA_COMPONENT(in, x, y, stride, 1);
            int b = GET_RGBA_COMPONENT(in, x, y, stride, 2);

            threshold = thresholdMap[x%16][y%16];

            if (r <= (255-(1<<3)) && ((r<<5) & 255) > threshold) r += (1<<3);
            if (g <= (255-(1<<2)) && ((g<<6) & 255) > threshold) g += (1<<2);
            if (b <= (255-(1<<3)) && ((b<<5) & 255) > threshold) b += (1<<3);

            // Write the newly produced pixel
            PUT_565(out, x, y, alignedWidth, ((b >> 3) & 0x1f), ((g >> 2) & 0x3f), ((r >> 3) & 0x1f));
        }
    }

    return out;
}

// 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(alignedWidth * 2 * height);

    // Lookup tables for the 8bit => 4bit conversion
    unsigned char lookup_8bit_to_4bit[256];
    short lookup_8bit_to_4bit_diff[256];

    // Macros for the conversion using the lookup table.
    #define CONVERT_8BIT_TO_4BIT(v) (lookup_8bit_to_4bit[v])
    #define DIFF_8BIT_TO_4BIT(v) (lookup_8bit_to_4bit_diff[v])

    int i;
    int x, y, c; // Pixel we're processing. c is component number (0, 1, 2, 3 for r, b, b, a)
    short component[4]; // Stores the new components (r, g, b, a) for pixel produced during conversion
    short diff; // The difference between the converted value and the original one. To be accumulated.
    QVarLengthArray <short> accumulatorData(4 * width * 2); // Data for three acumulators for r, g, b. Each accumulator is two lines.
    short *accumulator[4]; // Helper for accessing the accumulator on a per-channel basis more easily.
    accumulator[0] = accumulatorData.data();
    accumulator[1] = accumulatorData.data() + width;
    accumulator[2] = accumulatorData.data() + (width * 2);
    accumulator[3] = accumulatorData.data() + (width * 3);

    // Produce the conversion lookup tables.
    for (i = 0; i < 256; i++) {
        lookup_8bit_to_4bit[i] = round(i / 16.0);
        // 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
    memset(accumulator[0], 0, width * 4);
    memset(accumulator[1], 0, width * 4);
    memset(accumulator[2], 0, width * 4);
    memset(accumulator[3], 0, width * 4);

    // For each line...
    for (y = 0; y < height; y++) {

        // For each component (r, g, b, a)...
        memcpy(accumulator[0], accumulator[0] + width, width * 2);
        memset(accumulator[0] + width, 0, width * 2);

        memcpy(accumulator[1], accumulator[1] + width, width * 2);
        memset(accumulator[1] + width, 0, width * 2);

        memcpy(accumulator[2], accumulator[2] + width, width * 2);
        memset(accumulator[2] + width, 0, width * 2);

        memcpy(accumulator[3], accumulator[3] + width, width * 2);
        memset(accumulator[3] + width, 0, width * 2);

        // For each column....
        for (x = 0; x < width; x++) {

            // For each component (r, g, b, a)...
            for (c = 0; c < 4; c++) {

                // Get the 8bit value from the original image
                component[c] = GET_RGBA_COMPONENT(in, x, y, stride, c);

                // Add the diffusion for this pixel we stored in the accumulator.
                // >> 7 because the values in accumulator are stored * 128
                component[c] += accumulator[c][x] >> 7;

                // Make sure we're not over the boundaries.
                CLAMP_256(component[c]);

                // Store the difference from converting 8bit => 4bit and the orig pixel.
                // Convert 8bit => 4bit.
                diff = DIFF_8BIT_TO_4BIT(component[c]);
                component[c] = CONVERT_8BIT_TO_4BIT(component[c]);

                // Distribute the difference according to the matrix in the
                // accumulation bufffer.
                ACCUMULATE(accumulator[c], x + 1, 0, width, diff * 7);
                ACCUMULATE(accumulator[c], x - 1, 1, width, diff * 3);
                ACCUMULATE(accumulator[c], x, 1, width, diff * 5);
                ACCUMULATE(accumulator[c], x + 1, 1, width, diff * 1);
            }

            // Write the newly produced pixel
            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];
        }
    }

    return out;
}