1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the plugins of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | // This is an implementation of the 32bit => 16bit Floyd-Steinberg dithering.
|
---|
43 | // The alghorithm used here is not the fastest possible but it's prolly fast enough:
|
---|
44 | // uses look-up tables, integer-only arthmetics and works in one pass on two lines
|
---|
45 | // at a time. It's a high-quality dithering using 1/8 diffusion precission.
|
---|
46 | // Two functions here to look at:
|
---|
47 | //
|
---|
48 | // * convertRGBA32_to_RGB565
|
---|
49 | // * convertRGBA32_to_RGBA4444
|
---|
50 | //
|
---|
51 | // Each channel (RGBA) is diffused independently and alpha is dithered too.
|
---|
52 |
|
---|
53 | #include <string.h>
|
---|
54 | #include <stdio.h>
|
---|
55 | #include <stdlib.h>
|
---|
56 | #include <math.h>
|
---|
57 | #include <QVarLengthArray>
|
---|
58 |
|
---|
59 | // Gets a component (red = 1, green = 2...) from a RGBA data structure.
|
---|
60 | // data is unsigned char. stride is the number of bytes per line.
|
---|
61 | #define GET_RGBA_COMPONENT(data, x, y, stride, c) (data[(y * stride) + (x << 2) + c])
|
---|
62 |
|
---|
63 | // Writes a new pixel with r, g, b to data in 565 16bit format. Data is a short.
|
---|
64 | #define PUT_565(data, x, y, width, r, g, b) (data[(y * width) + x] = (r << 11) | (g << 5) | b)
|
---|
65 |
|
---|
66 | // Writes a new pixel with r, g, b, a to data in 4444 RGBA 16bit format. Data is a short.
|
---|
67 | #define PUT_4444(data, x, y, width, r, g, b, a) (data[(y * width) + x] = (r << 12) | (g << 8) | (b << 4) | a)
|
---|
68 |
|
---|
69 | // Writes(ads) a new value to the diffusion accumulator. accumulator is a short.
|
---|
70 | // x, y is a position in the accumulation buffer. y can be 0 or 1 -- we operate on two lines at time.
|
---|
71 | #define ACCUMULATE(accumulator, x, y, width, v) if (x < width && x >= 0) accumulator[(y * width) + x] += v
|
---|
72 |
|
---|
73 | // Clamps a value to be in 0..255 range.
|
---|
74 | #define CLAMP_256(v) if (v > 255) v = 255; if (v < 0) v = 0;
|
---|
75 |
|
---|
76 | // Converts incoming RGB32 (QImage::Format_RGB32) to RGB565. Returns the newly allocated data.
|
---|
77 | unsigned short* convertRGB32_to_RGB565(const unsigned char *in, int width, int height, int stride)
|
---|
78 | {
|
---|
79 | // Output line stride. Aligned to 4 bytes.
|
---|
80 | int alignedWidth = width;
|
---|
81 | if (alignedWidth % 2 > 0)
|
---|
82 | alignedWidth++;
|
---|
83 |
|
---|
84 | // Will store output
|
---|
85 | unsigned short *out = (unsigned short *) malloc(alignedWidth * height * 2);
|
---|
86 |
|
---|
87 | // Lookup tables for the 8bit => 6bit and 8bit => 5bit conversion
|
---|
88 | unsigned char lookup_8bit_to_5bit[256];
|
---|
89 | short lookup_8bit_to_5bit_diff[256];
|
---|
90 | unsigned char lookup_8bit_to_6bit[256];
|
---|
91 | short lookup_8bit_to_6bit_diff[256];
|
---|
92 |
|
---|
93 | // Macros for the conversion using the lookup table.
|
---|
94 | #define CONVERT_8BIT_TO_5BIT(v) (lookup_8bit_to_5bit[v])
|
---|
95 | #define DIFF_8BIT_TO_5BIT(v) (lookup_8bit_to_5bit_diff[v])
|
---|
96 |
|
---|
97 | #define CONVERT_8BIT_TO_6BIT(v) (lookup_8bit_to_6bit[v])
|
---|
98 | #define DIFF_8BIT_TO_6BIT(v) (lookup_8bit_to_6bit_diff[v])
|
---|
99 |
|
---|
100 | int i;
|
---|
101 | int x, y, c; // Pixel we're processing. c is component number (0, 1, 2 for r, b, b)
|
---|
102 | short component[3]; // Stores the new components (r, g, b) for pixel produced during conversion
|
---|
103 | short diff; // The difference between the converted value and the original one. To be accumulated.
|
---|
104 | QVarLengthArray <short> accumulatorData(3 * width * 2); // Data for three acumulators for r, g, b. Each accumulator is two lines.
|
---|
105 | short *accumulator[3]; // Helper for accessing the accumulator on a per-channel basis more easily.
|
---|
106 | accumulator[0] = accumulatorData.data();
|
---|
107 | accumulator[1] = accumulatorData.data() + width;
|
---|
108 | accumulator[2] = accumulatorData.data() + (width * 2);
|
---|
109 |
|
---|
110 | // Produce the conversion lookup tables.
|
---|
111 | for (i = 0; i < 256; i++) {
|
---|
112 | lookup_8bit_to_5bit[i] = round(i / 8.0);
|
---|
113 |
|
---|
114 | // Before bitshifts: (i * 8) - (... * 8 * 8)
|
---|
115 | lookup_8bit_to_5bit_diff[i] = (i << 3) - (lookup_8bit_to_5bit[i] << 6);
|
---|
116 | if (lookup_8bit_to_5bit[i] > 31)
|
---|
117 | lookup_8bit_to_5bit[i] -= 1;
|
---|
118 |
|
---|
119 | lookup_8bit_to_6bit[i] = round(i / 4.0);
|
---|
120 |
|
---|
121 | // Before bitshifts: (i * 8) - (... * 4 * 8)
|
---|
122 | lookup_8bit_to_6bit_diff[i] = (i << 3) - (lookup_8bit_to_6bit[i] << 5);
|
---|
123 | if (lookup_8bit_to_6bit[i] > 63)
|
---|
124 | lookup_8bit_to_6bit[i] -= 1;
|
---|
125 | }
|
---|
126 |
|
---|
127 | // Clear the accumulators
|
---|
128 | memset(accumulator[0], 0, width * 4);
|
---|
129 | memset(accumulator[1], 0, width * 4);
|
---|
130 | memset(accumulator[2], 0, width * 4);
|
---|
131 |
|
---|
132 | // For each line...
|
---|
133 | for (y = 0; y < height; y++) {
|
---|
134 |
|
---|
135 | // For each accumulator, move the second line (index 1) to replace the first line (index 0).
|
---|
136 | // Clear the second line (index 1)
|
---|
137 | memcpy(accumulator[0], accumulator[0] + width, width * 2);
|
---|
138 | memset(accumulator[0] + width, 0, width * 2);
|
---|
139 |
|
---|
140 | memcpy(accumulator[1], accumulator[1] + width, width * 2);
|
---|
141 | memset(accumulator[1] + width, 0, width * 2);
|
---|
142 |
|
---|
143 | memcpy(accumulator[2], accumulator[2] + width, width * 2);
|
---|
144 | memset(accumulator[2] + width, 0, width * 2);
|
---|
145 |
|
---|
146 | // For each column....
|
---|
147 | for (x = 0; x < width; x++) {
|
---|
148 |
|
---|
149 | // For each component (r, g, b)...
|
---|
150 | for (c = 0; c < 3; c++) {
|
---|
151 |
|
---|
152 | // Get the 8bit value from the original image
|
---|
153 | component[c] = GET_RGBA_COMPONENT(in, x, y, stride, c);
|
---|
154 |
|
---|
155 | // Add the diffusion for this pixel we stored in the accumulator.
|
---|
156 | // >> 7 because the values in accumulator are stored * 128
|
---|
157 | if (x != 0 && x != (width - 1)) {
|
---|
158 | if (accumulator[c][x] >> 7 != 0)
|
---|
159 | component[c] += rand() % accumulator[c][x] >> 7;
|
---|
160 | }
|
---|
161 |
|
---|
162 | // Make sure we're not over the boundaries.
|
---|
163 | CLAMP_256(component[c]);
|
---|
164 |
|
---|
165 | // For green component we use 6 bits. Otherwise 5 bits.
|
---|
166 | // Store the difference from converting 8bit => 6 bit and the orig pixel.
|
---|
167 | // Convert 8bit => 6(5) bit.
|
---|
168 | if (c == 1) {
|
---|
169 | diff = DIFF_8BIT_TO_6BIT(component[c]);
|
---|
170 | component[c] = CONVERT_8BIT_TO_6BIT(component[c]);
|
---|
171 | } else {
|
---|
172 | diff = DIFF_8BIT_TO_5BIT(component[c]);
|
---|
173 | component[c] = CONVERT_8BIT_TO_5BIT(component[c]);
|
---|
174 | }
|
---|
175 |
|
---|
176 | // Distribute the difference according to the matrix in the
|
---|
177 | // accumulation bufffer.
|
---|
178 | ACCUMULATE(accumulator[c], x + 1, 0, width, diff * 3);
|
---|
179 | ACCUMULATE(accumulator[c], x - 1, 1, width, diff * 5);
|
---|
180 | ACCUMULATE(accumulator[c], x, 1, width, diff * 5);
|
---|
181 | ACCUMULATE(accumulator[c], x + 1, 1, width, diff * 3);
|
---|
182 | }
|
---|
183 |
|
---|
184 | // Write the newly produced pixel
|
---|
185 | PUT_565(out, x, y, alignedWidth, component[2], component[1], component[0]);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | return out;
|
---|
190 | }
|
---|
191 |
|
---|
192 | // Converts incoming RGBA32 (QImage::Format_ARGB32_Premultiplied) to RGB565. Returns the newly allocated data.
|
---|
193 | // This function is similar (yet different) to the _565 variant but it makes sense to duplicate it here for simplicity.
|
---|
194 | // The output has each scan line aligned to 4 bytes (as expected by GL by default).
|
---|
195 | unsigned short* convertARGB32_to_RGBA4444(const unsigned char *in, int width, int height, int stride)
|
---|
196 | {
|
---|
197 | // Output line stride. Aligned to 4 bytes.
|
---|
198 | int alignedWidth = width;
|
---|
199 | if (alignedWidth % 2 > 0)
|
---|
200 | alignedWidth++;
|
---|
201 |
|
---|
202 | // Will store output
|
---|
203 | unsigned short *out = (unsigned short *) malloc(alignedWidth * 2 * height);
|
---|
204 |
|
---|
205 | // Lookup tables for the 8bit => 4bit conversion
|
---|
206 | unsigned char lookup_8bit_to_4bit[256];
|
---|
207 | short lookup_8bit_to_4bit_diff[256];
|
---|
208 |
|
---|
209 | // Macros for the conversion using the lookup table.
|
---|
210 | #define CONVERT_8BIT_TO_4BIT(v) (lookup_8bit_to_4bit[v])
|
---|
211 | #define DIFF_8BIT_TO_4BIT(v) (lookup_8bit_to_4bit_diff[v])
|
---|
212 |
|
---|
213 | int i;
|
---|
214 | int x, y, c; // Pixel we're processing. c is component number (0, 1, 2, 3 for r, b, b, a)
|
---|
215 | short component[4]; // Stores the new components (r, g, b, a) for pixel produced during conversion
|
---|
216 | short diff; // The difference between the converted value and the original one. To be accumulated.
|
---|
217 | QVarLengthArray <short> accumulatorData(4 * width * 2); // Data for three acumulators for r, g, b. Each accumulator is two lines.
|
---|
218 | short *accumulator[4]; // Helper for accessing the accumulator on a per-channel basis more easily.
|
---|
219 | accumulator[0] = accumulatorData.data();
|
---|
220 | accumulator[1] = accumulatorData.data() + width;
|
---|
221 | accumulator[2] = accumulatorData.data() + (width * 2);
|
---|
222 | accumulator[3] = accumulatorData.data() + (width * 3);
|
---|
223 |
|
---|
224 | // Produce the conversion lookup tables.
|
---|
225 | for (i = 0; i < 256; i++) {
|
---|
226 | lookup_8bit_to_4bit[i] = round(i / 16.0);
|
---|
227 | // Before bitshifts: (i * 8) - (... * 16 * 8)
|
---|
228 | lookup_8bit_to_4bit_diff[i] = (i << 3) - (lookup_8bit_to_4bit[i] << 7);
|
---|
229 |
|
---|
230 | if (lookup_8bit_to_4bit[i] > 15)
|
---|
231 | lookup_8bit_to_4bit[i] = 15;
|
---|
232 | }
|
---|
233 |
|
---|
234 | // Clear the accumulators
|
---|
235 | memset(accumulator[0], 0, width * 4);
|
---|
236 | memset(accumulator[1], 0, width * 4);
|
---|
237 | memset(accumulator[2], 0, width * 4);
|
---|
238 | memset(accumulator[3], 0, width * 4);
|
---|
239 |
|
---|
240 | // For each line...
|
---|
241 | for (y = 0; y < height; y++) {
|
---|
242 |
|
---|
243 | // For each component (r, g, b, a)...
|
---|
244 | memcpy(accumulator[0], accumulator[0] + width, width * 2);
|
---|
245 | memset(accumulator[0] + width, 0, width * 2);
|
---|
246 |
|
---|
247 | memcpy(accumulator[1], accumulator[1] + width, width * 2);
|
---|
248 | memset(accumulator[1] + width, 0, width * 2);
|
---|
249 |
|
---|
250 | memcpy(accumulator[2], accumulator[2] + width, width * 2);
|
---|
251 | memset(accumulator[2] + width, 0, width * 2);
|
---|
252 |
|
---|
253 | memcpy(accumulator[3], accumulator[3] + width, width * 2);
|
---|
254 | memset(accumulator[3] + width, 0, width * 2);
|
---|
255 |
|
---|
256 | // For each column....
|
---|
257 | for (x = 0; x < width; x++) {
|
---|
258 |
|
---|
259 | // For each component (r, g, b, a)...
|
---|
260 | for (c = 0; c < 4; c++) {
|
---|
261 |
|
---|
262 | // Get the 8bit value from the original image
|
---|
263 | component[c] = GET_RGBA_COMPONENT(in, x, y, stride, c);
|
---|
264 |
|
---|
265 | // Add the diffusion for this pixel we stored in the accumulator.
|
---|
266 | // >> 7 because the values in accumulator are stored * 128
|
---|
267 | component[c] += accumulator[c][x] >> 7;
|
---|
268 |
|
---|
269 | // Make sure we're not over the boundaries.
|
---|
270 | CLAMP_256(component[c]);
|
---|
271 |
|
---|
272 | // Store the difference from converting 8bit => 4bit and the orig pixel.
|
---|
273 | // Convert 8bit => 4bit.
|
---|
274 | diff = DIFF_8BIT_TO_4BIT(component[c]);
|
---|
275 | component[c] = CONVERT_8BIT_TO_4BIT(component[c]);
|
---|
276 |
|
---|
277 | // Distribute the difference according to the matrix in the
|
---|
278 | // accumulation bufffer.
|
---|
279 | ACCUMULATE(accumulator[c], x + 1, 0, width, diff * 7);
|
---|
280 | ACCUMULATE(accumulator[c], x - 1, 1, width, diff * 3);
|
---|
281 | ACCUMULATE(accumulator[c], x, 1, width, diff * 5);
|
---|
282 | ACCUMULATE(accumulator[c], x + 1, 1, width, diff * 1);
|
---|
283 | }
|
---|
284 |
|
---|
285 | // Write the newly produced pixel
|
---|
286 | PUT_4444(out, x, y, alignedWidth, component[0], component[1], component[2], component[3]);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | return out;
|
---|
291 | }
|
---|
292 |
|
---|
293 | unsigned char* convertBGRA32_to_RGBA32(const unsigned char *in, int width, int height, int stride)
|
---|
294 | {
|
---|
295 | unsigned char *out = (unsigned char *) malloc(stride * height);
|
---|
296 |
|
---|
297 | // For each line...
|
---|
298 | for (int y = 0; y < height; y++) {
|
---|
299 | // For each column
|
---|
300 | for (int x = 0; x < width; x++) {
|
---|
301 | out[(stride * y) + (x * 4) + 0] = in[(stride * y) + (x * 4) + 2];
|
---|
302 | out[(stride * y) + (x * 4) + 1] = in[(stride * y) + (x * 4) + 1];
|
---|
303 | out[(stride * y) + (x * 4) + 2] = in[(stride * y) + (x * 4) + 0];
|
---|
304 | out[(stride * y) + (x * 4) + 3] = in[(stride * y) + (x * 4) + 3];
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | return out;
|
---|
309 | }
|
---|