64 #include <QVarLengthArray> 68 #define GET_RGBA_COMPONENT(data, x, y, stride, c) (data[(y * stride) + (x << 2) + c]) 71 #define PUT_565(data, x, y, width, r, g, b) (data[(y * width) + x] = (r << 11) | (g << 5) | b) 74 #define PUT_4444(data, x, y, width, r, g, b, a) (data[(y * width) + x] = (r << 12) | (g << 8) | (b << 4) | a) 78 #define ACCUMULATE(accumulator, x, y, width, v) if (x < width && x >= 0) accumulator[(y * width) + x] += v 81 #define CLAMP_256(v) if (v > 255) v = 255; if (v < 0) v = 0; 86 static bool thresholdMapInitialized =
false;
87 static int thresholdMap[16][16];
89 if (!thresholdMapInitialized) {
94 thresholdMap[0][0] = 0;
95 thresholdMap[1][0] = 2;
96 thresholdMap[0][1] = 3;
97 thresholdMap[1][1] = 1;
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;
110 thresholdMapInitialized =
true;
114 int alignedWidth = width;
115 if (alignedWidth % 2 > 0)
119 unsigned short *out = (
unsigned short *)malloc (alignedWidth * height * 2);
126 for (y = 0; y < height; y++) {
129 for (x = 0; x < width; x++) {
135 threshold = thresholdMap[x%16][y%16];
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);
142 PUT_565(out, x, y, alignedWidth, ((b >> 3) & 0x1f), ((g >> 2) & 0x3f), ((r >> 3) & 0x1f));
155 int alignedWidth = width;
156 if (alignedWidth % 2 > 0)
160 unsigned short *out = (
unsigned short *) malloc(alignedWidth * 2 * height);
163 unsigned char lookup_8bit_to_4bit[256];
164 short lookup_8bit_to_4bit_diff[256];
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]) 175 short *accumulator[4];
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);
182 for (i = 0; i < 256; i++) {
183 lookup_8bit_to_4bit[i] = round(i / 16.0);
185 lookup_8bit_to_4bit_diff[i] = (i << 3) - (lookup_8bit_to_4bit[i] << 7);
187 if (lookup_8bit_to_4bit[i] > 15)
188 lookup_8bit_to_4bit[i] = 15;
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);
198 for (y = 0; y < height; y++) {
201 memcpy(accumulator[0], accumulator[0] + width, width * 2);
202 memset(accumulator[0] + width, 0, width * 2);
204 memcpy(accumulator[1], accumulator[1] + width, width * 2);
205 memset(accumulator[1] + width, 0, width * 2);
207 memcpy(accumulator[2], accumulator[2] + width, width * 2);
208 memset(accumulator[2] + width, 0, width * 2);
210 memcpy(accumulator[3], accumulator[3] + width, width * 2);
211 memset(accumulator[3] + width, 0, width * 2);
214 for (x = 0; x < width; x++) {
217 for (c = 0; c < 4; c++) {
224 component[
c] += accumulator[
c][x] >> 7;
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);
243 PUT_4444(out, x, y, alignedWidth, component[0], component[1], component[2], component[3]);
252 unsigned char *out = (
unsigned char *) malloc(stride * height);
255 for (
int y = 0; y < height; y++) {
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];
#define PUT_565(data, x, y, width, r, g, b)
#define CONVERT_8BIT_TO_4BIT(v)
unsigned short * convertRGB32_to_RGB565(const unsigned char *in, int width, int height, int stride)
#define PUT_4444(data, x, y, width, r, g, b, a)
static qreal component(const QPointF &point, unsigned int i)
#define DIFF_8BIT_TO_4BIT(v)
#define ACCUMULATE(accumulator, x, y, width, v)
#define GET_RGBA_COMPONENT(data, x, y, stride, c)
unsigned char * convertBGRA32_to_RGBA32(const unsigned char *in, int width, int height, int stride)
unsigned short * convertARGB32_to_RGBA4444(const unsigned char *in, int width, int height, int stride)