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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
qt-bugs@ issue : 11790 (part of)
applied: no
author: Lubos Lunak <[email protected]>
See 0005-qpixmap_mitshm.patch for details.
--- src/kernel/qpixmap_x11.cpp
+++ src/kernel/qpixmap_x11.cpp
@@ -1123,9 +1123,6 @@ bool QPixmap::convertFromImage( const QI
return FALSE;
int bppc = xi->bits_per_pixel;
- if ( bppc > 8 && xi->byte_order == LSBFirst )
- bppc++;
-
bool contig_bits = n_bits(red_mask) == rbits &&
n_bits(green_mask) == gbits &&
n_bits(blue_mask) == bbits;
@@ -1174,32 +1171,70 @@ bool QPixmap::convertFromImage( const QI
}
init=TRUE;
}
+
+ enum { BPP8,
+ BPP16_8_3_M3, BPP16_7_2_M3, BPP16_MSB, BPP16_LSB,
+ BPP24_MSB, BPP24_LSB,
+ BPP32_16_8_0, BPP32_MSB, BPP32_LSB
+ } mode = BPP8;
- for ( uint y=0; y<h; y++ ) {
- uchar* src = image.scanLine( y );
- uchar* dst = newbits + xi->bytes_per_line*y;
- QRgb* p = (QRgb *)src;
+ if ( bppc > 8 && xi->byte_order == LSBFirst )
+ bppc++;
-#define GET_RGB \
- int r = qRed ( *p ); \
- int g = qGreen( *p ); \
- int b = qBlue ( *p++ ); \
- r = red_shift > 0 \
- ? r << red_shift : r >> -red_shift; \
- g = green_shift > 0 \
- ? g << green_shift : g >> -green_shift; \
- b = blue_shift > 0 \
- ? b << blue_shift : b >> -blue_shift;
+ int wordsize;
+ bool bigendian;
+ qSysInfo( &wordsize, &bigendian );
+ bool same_msb_lsb = ( xi->byte_order == MSBFirst ) == ( bigendian );
+
+ if( bppc == 8 ) // 8 bit
+ mode = BPP8;
+ else if( bppc == 16 || bppc == 17 ) { // 16 bit MSB/LSB
+ if( red_shift == 8 && green_shift == 3 && blue_shift == -3
+ && !d8 && same_msb_lsb )
+ mode = BPP16_8_3_M3;
+ else if( red_shift == 7 && green_shift == 2 && blue_shift == -3
+ && !d8 && same_msb_lsb )
+ mode = BPP16_7_2_M3;
+ else
+ mode = bppc == 17 ? BPP16_LSB : BPP16_MSB;
+ } else if( bppc == 24 || bppc == 25 ) { // 24 bit MSB/LSB
+ mode = bppc == 25 ? BPP24_LSB : BPP24_MSB;
+ } else if( bppc == 32 || bppc == 33 ) { // 32 bit MSB/LSB
+ if( red_shift == 16 && green_shift == 8 && blue_shift == 0
+ && !d8 && same_msb_lsb )
+ mode = BPP32_16_8_0;
+ else
+ mode = bppc == 33 ? BPP32_LSB : BPP32_MSB;
+ } else
+ qFatal("Logic error 3");
#define GET_PIXEL \
int pixel; \
if ( d8 ) pixel = pix[*src++]; \
else { \
- GET_RGB \
- pixel = (b & blue_mask)|(g & green_mask) | (r & red_mask) \
+ int r = qRed ( *p ); \
+ int g = qGreen( *p ); \
+ int b = qBlue ( *p++ ); \
+ r = red_shift > 0 \
+ ? r << red_shift : r >> -red_shift; \
+ g = green_shift > 0 \
+ ? g << green_shift : g >> -green_shift; \
+ b = blue_shift > 0 \
+ ? b << blue_shift : b >> -blue_shift; \
+ pixel = (r & red_mask)|(g & green_mask) | (b & blue_mask) \
| ~(blue_mask | green_mask | red_mask); \
}
+// optimized case - no d8 case, shift only once instead of twice, mask only once instead of twice,
+// use direct values instead of variables, and use only one statement
+// (*p >> 16), (*p >> 8 ) and (*p) are qRed(),qGreen() and qBlue() without masking
+// shifts have to be passed including the shift operator (e.g. '>>3'), because of the direction
+#define GET_PIXEL_OPT(red_shift,green_shift,blue_shift,red_mask,green_mask,blue_mask) \
+ int pixel = ((( *p >> 16 ) red_shift ) & red_mask ) \
+ | ((( *p >> 8 ) green_shift ) & green_mask ) \
+ | ((( *p ) blue_shift ) & blue_mask ); \
+ ++p;
+
#define GET_PIXEL_DITHER_TC \
int r = qRed ( *p ); \
int g = qGreen( *p ); \
@@ -1220,91 +1255,177 @@ bool QPixmap::convertFromImage( const QI
? g << green_shift : g >> -green_shift; \
b = blue_shift > 0 \
? b << blue_shift : b >> -blue_shift; \
- int pixel = (b & blue_mask)|(g & green_mask) | (r & red_mask);
+ int pixel = (r & red_mask)|(g & green_mask) | (b & blue_mask);
- if ( dither_tc ) {
- uint x;
- switch ( bppc ) {
- case 16: // 16 bit MSB
- for ( x=0; x<w; x++ ) {
- GET_PIXEL_DITHER_TC
- *dst++ = (pixel >> 8);
- *dst++ = pixel;
- }
+// again, optimized case
+// can't be optimized that much :(
+#define GET_PIXEL_DITHER_TC_OPT(red_shift,green_shift,blue_shift,red_mask,green_mask,blue_mask, \
+ rbits,gbits,bbits) \
+ const int thres = D[x%16][y%16]; \
+ int r = qRed ( *p ); \
+ if ( r <= (255-(1<<(8-rbits))) && ((r<<rbits) & 255) \
+ > thres) \
+ r += (1<<(8-rbits)); \
+ int g = qGreen( *p ); \
+ if ( g <= (255-(1<<(8-gbits))) && ((g<<gbits) & 255) \
+ > thres) \
+ g += (1<<(8-gbits)); \
+ int b = qBlue ( *p++ ); \
+ if ( b <= (255-(1<<(8-bbits))) && ((b<<bbits) & 255) \
+ > thres) \
+ b += (1<<(8-bbits)); \
+ int pixel = (( r red_shift ) & red_mask ) \
+ | (( g green_shift ) & green_mask ) \
+ | (( b blue_shift ) & blue_mask );
+
+#define CYCLE(body) \
+ for ( uint y=0; y<h; y++ ) { \
+ uchar* src = image.scanLine( y ); \
+ uchar* dst = newbits + xi->bytes_per_line*y; \
+ QRgb* p = (QRgb *)src; \
+ body \
+ }
+
+ if ( dither_tc ) {
+ switch ( mode ) {
+ case BPP16_8_3_M3:
+ CYCLE(
+ Q_INT16* dst16 = (Q_INT16*)dst;
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL_DITHER_TC_OPT(<<8,<<3,>>3,0xf800,0x7e0,0x1f,5,6,5)
+ *dst16++ = pixel;
+ }
+ )
break;
- case 17: // 16 bit LSB
- for ( x=0; x<w; x++ ) {
- GET_PIXEL_DITHER_TC
- *dst++ = pixel;
- *dst++ = pixel >> 8;
- }
+ case BPP16_7_2_M3:
+ CYCLE(
+ Q_INT16* dst16 = (Q_INT16*)dst;
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL_DITHER_TC_OPT(<<7,<<2,>>3,0x7c00,0x3e0,0x1f,5,5,5)
+ *dst16++ = pixel;
+ }
+ )
+ break;
+ case BPP16_MSB: // 16 bit MSB
+ CYCLE(
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL_DITHER_TC
+ *dst++ = (pixel >> 8);
+ *dst++ = pixel;
+ }
+ )
+ break;
+ case BPP16_LSB: // 16 bit LSB
+ CYCLE(
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL_DITHER_TC
+ *dst++ = pixel;
+ *dst++ = pixel >> 8;
+ }
+ )
break;
default:
qFatal("Logic error");
}
- } else {
- uint x;
- switch ( bppc ) {
- case 8: // 8 bit
- for ( x=0; x<w; x++ ) {
- int pixel = pix[*src++];
- *dst++ = pixel;
- }
+ } else {
+ switch ( mode ) {
+ case BPP8: // 8 bit
+ CYCLE(
+ Q_UNUSED(p);
+ for ( uint x=0; x<w; x++ ) {
+ int pixel = pix[*src++];
+ *dst++ = pixel;
+ }
+ )
break;
- case 16: // 16 bit MSB
- for ( x=0; x<w; x++ ) {
- GET_PIXEL
- *dst++ = (pixel >> 8);
- *dst++ = pixel;
- }
+ case BPP16_8_3_M3:
+ CYCLE(
+ Q_INT16* dst16 = (Q_INT16*)dst;
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL_OPT(<<8,<<3,>>3,0xf800,0x7e0,0x1f)
+ *dst16++ = pixel;
+ }
+ )
break;
- case 17: // 16 bit LSB
- for ( x=0; x<w; x++ ) {
- GET_PIXEL
- *dst++ = pixel;
- *dst++ = pixel >> 8;
- }
+ case BPP16_7_2_M3:
+ CYCLE(
+ Q_INT16* dst16 = (Q_INT16*)dst;
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL_OPT(<<7,<<2,>>3,0x7c00,0x3e0,0x1f)
+ *dst16++ = pixel;
+ }
+ )
break;
- case 24: // 24 bit MSB
- for ( x=0; x<w; x++ ) {
- GET_PIXEL
- *dst++ = pixel >> 16;
- *dst++ = pixel >> 8;
- *dst++ = pixel;
- }
+ case BPP16_MSB: // 16 bit MSB
+ CYCLE(
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL
+ *dst++ = (pixel >> 8);
+ *dst++ = pixel;
+ }
+ )
break;
- case 25: // 24 bit LSB
- for ( x=0; x<w; x++ ) {
- GET_PIXEL
- *dst++ = pixel;
- *dst++ = pixel >> 8;
- *dst++ = pixel >> 16;
- }
+ case BPP16_LSB: // 16 bit LSB
+ CYCLE(
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL
+ *dst++ = pixel;
+ *dst++ = pixel >> 8;
+ }
+ )
break;
- case 32: // 32 bit MSB
- for ( x=0; x<w; x++ ) {
- GET_PIXEL
- *dst++ = pixel >> 24;
- *dst++ = pixel >> 16;
- *dst++ = pixel >> 8;
- *dst++ = pixel;
- }
+ case BPP24_MSB: // 24 bit MSB
+ CYCLE(
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL
+ *dst++ = pixel >> 16;
+ *dst++ = pixel >> 8;
+ *dst++ = pixel;
+ }
+ )
break;
- case 33: // 32 bit LSB
- for ( x=0; x<w; x++ ) {
- GET_PIXEL
- *dst++ = pixel;
- *dst++ = pixel >> 8;
- *dst++ = pixel >> 16;
- *dst++ = pixel >> 24;
- }
+ case BPP24_LSB: // 24 bit LSB
+ CYCLE(
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL
+ *dst++ = pixel;
+ *dst++ = pixel >> 8;
+ *dst++ = pixel >> 16;
+ }
+ )
break;
- default:
- qFatal("Logic error 2");
- }
- }
- }
- xi->data = (char *)newbits;
+ case BPP32_16_8_0:
+ CYCLE(
+ memcpy( dst, p, w * 4 );
+ )
+ break;
+ case BPP32_MSB: // 32 bit MSB
+ CYCLE(
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL
+ *dst++ = pixel >> 24;
+ *dst++ = pixel >> 16;
+ *dst++ = pixel >> 8;
+ *dst++ = pixel;
+ }
+ )
+ break;
+ case BPP32_LSB: // 32 bit LSB
+ CYCLE(
+ for ( uint x=0; x<w; x++ ) {
+ GET_PIXEL
+ *dst++ = pixel;
+ *dst++ = pixel >> 8;
+ *dst++ = pixel >> 16;
+ *dst++ = pixel >> 24;
+ }
+ )
+ break;
+ default:
+ qFatal("Logic error 2");
+ }
+ }
+ xi->data = (char *)newbits;
}
if ( d == 8 && !trucol ) { // 8 bit pixmap
@@ -1554,15 +1675,24 @@ bool QPixmap::convertFromImage( const QI
if (image.depth() == 32) {
const int *iptr = (const int *) image.bits();
- int max = w * h;
- while (max--)
- *aptr++ = *iptr++ >> 24; // squirt
+ if( axi->bytes_per_line == (int)w ) {
+ int max = w * h;
+ while (max--)
+ *aptr++ = *iptr++ >> 24; // squirt
+ } else {
+ for (uint i = 0; i < h; ++i ) {
+ for (uint j = 0; j < w; ++j )
+ *aptr++ = *iptr++ >> 24; // squirt
+ aptr += ( axi->bytes_per_line - w );
+ }
+ }
} else if (image.depth() == 8) {
const QRgb * const rgb = image.colorTable();
for (uint y = 0; y < h; ++y) {
const uchar *iptr = image.scanLine(y);
for (uint x = 0; x < w; ++x)
*aptr++ = qAlpha(rgb[*iptr++]);
+ aptr += ( axi->bytes_per_line - w );
}
}
|