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