Qt 4.8
Macros | Functions
dithering.cpp File Reference
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <QVarLengthArray>

Go to the source code of this file.

Macros

#define ACCUMULATE(accumulator, x, y, width, v)   if (x < width && x >= 0) accumulator[(y * width) + x] += v
 
#define CLAMP_256(v)   if (v > 255) v = 255; if (v < 0) v = 0;
 
#define CONVERT_8BIT_TO_4BIT(v)   (lookup_8bit_to_4bit[v])
 
#define DIFF_8BIT_TO_4BIT(v)   (lookup_8bit_to_4bit_diff[v])
 
#define GET_RGBA_COMPONENT(data, x, y, stride, c)   (data[(y * stride) + (x << 2) + c])
 
#define PUT_4444(data, x, y, width, r, g, b, a)   (data[(y * width) + x] = (r << 12) | (g << 8) | (b << 4) | a)
 
#define PUT_565(data, x, y, width, r, g, b)   (data[(y * width) + x] = (r << 11) | (g << 5) | b)
 

Functions

unsigned short * convertARGB32_to_RGBA4444 (const unsigned char *in, int width, int height, int stride)
 
unsigned char * convertBGRA32_to_RGBA32 (const unsigned char *in, int width, int height, int stride)
 
unsigned short * convertRGB32_to_RGB565 (const unsigned char *in, int width, int height, int stride)
 

Macro Definition Documentation

◆ ACCUMULATE

#define ACCUMULATE (   accumulator,
  x,
  y,
  width,
 
)    if (x < width && x >= 0) accumulator[(y * width) + x] += v

Definition at line 78 of file dithering.cpp.

Referenced by convertARGB32_to_RGBA4444().

◆ CLAMP_256

#define CLAMP_256 (   v)    if (v > 255) v = 255; if (v < 0) v = 0;

Definition at line 81 of file dithering.cpp.

Referenced by convertARGB32_to_RGBA4444().

◆ CONVERT_8BIT_TO_4BIT

#define CONVERT_8BIT_TO_4BIT (   v)    (lookup_8bit_to_4bit[v])

◆ DIFF_8BIT_TO_4BIT

#define DIFF_8BIT_TO_4BIT (   v)    (lookup_8bit_to_4bit_diff[v])

◆ GET_RGBA_COMPONENT

#define GET_RGBA_COMPONENT (   data,
  x,
  y,
  stride,
  c 
)    (data[(y * stride) + (x << 2) + c])

Definition at line 68 of file dithering.cpp.

Referenced by convertARGB32_to_RGBA4444(), and convertRGB32_to_RGB565().

◆ PUT_4444

#define PUT_4444 (   data,
  x,
  y,
  width,
  r,
  g,
  b,
  a 
)    (data[(y * width) + x] = (r << 12) | (g << 8) | (b << 4) | a)

Definition at line 74 of file dithering.cpp.

Referenced by convertARGB32_to_RGBA4444().

◆ PUT_565

#define PUT_565 (   data,
  x,
  y,
  width,
  r,
  g,
 
)    (data[(y * width) + x] = (r << 11) | (g << 5) | b)

Definition at line 71 of file dithering.cpp.

Referenced by convertRGB32_to_RGB565().

Function Documentation

◆ convertARGB32_to_RGBA4444()

unsigned short* convertARGB32_to_RGBA4444 ( const unsigned char *  in,
int  width,
int  height,
int  stride 
)

Definition at line 152 of file dithering.cpp.

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 }
unsigned char c[8]
Definition: qnumeric_p.h:62
#define CONVERT_8BIT_TO_4BIT(v)
#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
#define CLAMP_256(v)
Definition: dithering.cpp:81

◆ convertBGRA32_to_RGBA32()

unsigned char* convertBGRA32_to_RGBA32 ( const unsigned char *  in,
int  width,
int  height,
int  stride 
)

Definition at line 250 of file dithering.cpp.

Referenced by QMeeGoPixmapData::imageToEGLSharedImage().

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 }

◆ convertRGB32_to_RGB565()

unsigned short* convertRGB32_to_RGB565 ( const unsigned char *  in,
int  width,
int  height,
int  stride 
)

Definition at line 84 of file dithering.cpp.

Referenced by QMeeGoPixmapData::imageToEGLSharedImage().

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 }
#define PUT_565(data, x, y, width, r, g, b)
Definition: dithering.cpp:71
#define GET_RGBA_COMPONENT(data, x, y, stride, c)
Definition: dithering.cpp:68