Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / src / gui / painting / qdrawhelper_p.h
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 QtGui module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDRAWHELPER_P_H
43 #define QDRAWHELPER_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "QtCore/qglobal.h"
57 #include "QtGui/qcolor.h"
58 #include "QtGui/qpainter.h"
59 #include "QtGui/qimage.h"
60 #ifndef QT_FT_BEGIN_HEADER
61 #define QT_FT_BEGIN_HEADER
62 #define QT_FT_END_HEADER
63 #endif
64 #include "private/qrasterdefs_p.h"
65 #include <private/qsimd_p.h>
66 #include <private/qmath_p.h>
67
68 #ifdef Q_WS_QWS
69 #include "QtGui/qscreen_qws.h"
70 #endif
71
72 QT_BEGIN_NAMESPACE
73
74 #if defined(Q_CC_MSVC) && _MSCVER <= 1300 && !defined(Q_CC_INTEL)
75 #define Q_STATIC_TEMPLATE_SPECIALIZATION static
76 #else
77 #define Q_STATIC_TEMPLATE_SPECIALIZATION
78 #endif
79
80 #if defined(Q_CC_RVCT)
81 // RVCT doesn't like static template functions
82 #  define Q_STATIC_TEMPLATE_FUNCTION
83 #  define Q_STATIC_INLINE_FUNCTION static __forceinline
84 #else
85 #  define Q_STATIC_TEMPLATE_FUNCTION static
86 #  define Q_STATIC_INLINE_FUNCTION static inline
87 #endif
88
89 static const uint AMASK = 0xff000000;
90 static const uint RMASK = 0x00ff0000;
91 static const uint GMASK = 0x0000ff00;
92 static const uint BMASK = 0x000000ff;
93
94 /*******************************************************************************
95  * QSpan
96  *
97  * duplicate definition of FT_Span
98  */
99 typedef QT_FT_Span QSpan;
100
101 struct QSolidData;
102 struct QTextureData;
103 struct QGradientData;
104 struct QLinearGradientData;
105 struct QRadialGradientData;
106 struct QConicalGradientData;
107 struct QSpanData;
108 class QGradient;
109 class QRasterBuffer;
110 class QClipData;
111 class QRasterPaintEngineState;
112
113 typedef QT_FT_SpanFunc ProcessSpans;
114 typedef void (*BitmapBlitFunc)(QRasterBuffer *rasterBuffer,
115                                int x, int y, quint32 color,
116                                const uchar *bitmap,
117                                int mapWidth, int mapHeight, int mapStride);
118
119 typedef void (*AlphamapBlitFunc)(QRasterBuffer *rasterBuffer,
120                                  int x, int y, quint32 color,
121                                  const uchar *bitmap,
122                                  int mapWidth, int mapHeight, int mapStride,
123                                  const QClipData *clip);
124
125 typedef void (*AlphaRGBBlitFunc)(QRasterBuffer *rasterBuffer,
126                                  int x, int y, quint32 color,
127                                  const uint *rgbmask,
128                                  int mapWidth, int mapHeight, int mapStride,
129                                  const QClipData *clip);
130
131 typedef void (*RectFillFunc)(QRasterBuffer *rasterBuffer,
132                              int x, int y, int width, int height,
133                              quint32 color);
134
135 typedef void (*SrcOverBlendFunc)(uchar *destPixels, int dbpl,
136                                  const uchar *src, int spbl,
137                                  int w, int h,
138                                  int const_alpha);
139
140 typedef void (*SrcOverScaleFunc)(uchar *destPixels, int dbpl,
141                                  const uchar *src, int spbl,
142                                  const QRectF &targetRect,
143                                  const QRectF &sourceRect,
144                                  const QRect &clipRect,
145                                  int const_alpha);
146
147 typedef void (*SrcOverTransformFunc)(uchar *destPixels, int dbpl,
148                                      const uchar *src, int spbl,
149                                      const QRectF &targetRect,
150                                      const QRectF &sourceRect,
151                                      const QRect &clipRect,
152                                      const QTransform &targetRectTransform,
153                                      int const_alpha);
154
155 typedef void (*MemRotateFunc)(const uchar *srcPixels, int w, int h, int sbpl, uchar *destPixels, int dbpl);
156
157 struct DrawHelper {
158     ProcessSpans blendColor;
159     ProcessSpans blendGradient;
160     BitmapBlitFunc bitmapBlit;
161     AlphamapBlitFunc alphamapBlit;
162     AlphaRGBBlitFunc alphaRGBBlit;
163     RectFillFunc fillRect;
164 };
165
166 extern SrcOverBlendFunc qBlendFunctions[QImage::NImageFormats][QImage::NImageFormats];
167 extern SrcOverScaleFunc qScaleFunctions[QImage::NImageFormats][QImage::NImageFormats];
168 extern SrcOverTransformFunc qTransformFunctions[QImage::NImageFormats][QImage::NImageFormats];
169 extern MemRotateFunc qMemRotateFunctions[QImage::NImageFormats][3];
170
171 extern DrawHelper qDrawHelper[QImage::NImageFormats];
172
173 void qBlendTexture(int count, const QSpan *spans, void *userData);
174 #if defined(Q_WS_QWS) && !defined(QT_NO_RASTERCALLBACKS)
175 extern DrawHelper qDrawHelperCallback[QImage::NImageFormats];
176 void qBlendTextureCallback(int count, const QSpan *spans, void *userData);
177 #endif
178
179 typedef void (QT_FASTCALL *CompositionFunction)(uint *dest, const uint *src, int length, uint const_alpha);
180 typedef void (QT_FASTCALL *CompositionFunctionSolid)(uint *dest, int length, uint color, uint const_alpha);
181
182 struct LinearGradientValues
183 {
184     qreal dx;
185     qreal dy;
186     qreal l;
187     qreal off;
188 };
189
190 struct RadialGradientValues
191 {
192     qreal dx;
193     qreal dy;
194     qreal dr;
195     qreal sqrfr;
196     qreal a;
197     qreal inv2a;
198     bool extended;
199 };
200
201 struct Operator;
202 typedef uint* (QT_FASTCALL *DestFetchProc)(uint *buffer, QRasterBuffer *rasterBuffer, int x, int y, int length);
203 typedef void (QT_FASTCALL *DestStoreProc)(QRasterBuffer *rasterBuffer, int x, int y, const uint *buffer, int length);
204 typedef const uint* (QT_FASTCALL *SourceFetchProc)(uint *buffer, const Operator *o, const QSpanData *data, int y, int x, int length);
205
206 struct Operator
207 {
208     QPainter::CompositionMode mode;
209     DestFetchProc dest_fetch;
210     DestStoreProc dest_store;
211     SourceFetchProc src_fetch;
212     CompositionFunctionSolid funcSolid;
213     CompositionFunction func;
214     union {
215         LinearGradientValues linear;
216         RadialGradientValues radial;
217     };
218 };
219
220 void qInitDrawhelperAsm();
221
222 class QRasterPaintEngine;
223
224 struct QSolidData
225 {
226     uint color;
227 };
228
229 struct QLinearGradientData
230 {
231     struct {
232         qreal x;
233         qreal y;
234     } origin;
235     struct {
236         qreal x;
237         qreal y;
238     } end;
239 };
240
241 struct QRadialGradientData
242 {
243     struct {
244         qreal x;
245         qreal y;
246         qreal radius;
247     } center;
248     struct {
249         qreal x;
250         qreal y;
251         qreal radius;
252     } focal;
253 };
254
255 struct QConicalGradientData
256 {
257     struct {
258         qreal x;
259         qreal y;
260     } center;
261     qreal angle;
262 };
263
264 struct QGradientData
265 {
266     QGradient::Spread spread;
267
268     union {
269         QLinearGradientData linear;
270         QRadialGradientData radial;
271         QConicalGradientData conical;
272     };
273
274 #ifdef Q_WS_QWS
275 #define GRADIENT_STOPTABLE_SIZE 256
276 #define GRADIENT_STOPTABLE_SIZE_SHIFT 8
277 #else
278 #define GRADIENT_STOPTABLE_SIZE 1024
279 #define GRADIENT_STOPTABLE_SIZE_SHIFT 10
280 #endif
281
282     uint* colorTable; //[GRADIENT_STOPTABLE_SIZE];
283
284     uint alphaColor : 1;
285 };
286
287 struct QTextureData
288 {
289     const uchar *imageData;
290     const uchar *scanLine(int y) const { return imageData + y*bytesPerLine; }
291
292     int width;
293     int height;
294     // clip rect
295     int x1;
296     int y1;
297     int x2;
298     int y2;
299     int bytesPerLine;
300     QImage::Format format;
301     const QVector<QRgb> *colorTable;
302     bool hasAlpha;
303     enum Type {
304         Plain,
305         Tiled
306     };
307     Type type;
308     int const_alpha;
309 };
310
311 struct QSpanData
312 {
313     QSpanData() : tempImage(0) {}
314     ~QSpanData() { delete tempImage; }
315
316     QRasterBuffer *rasterBuffer;
317 #ifdef Q_WS_QWS
318     QRasterPaintEngine *rasterEngine;
319 #endif
320     ProcessSpans blend;
321     ProcessSpans unclipped_blend;
322     BitmapBlitFunc bitmapBlit;
323     AlphamapBlitFunc alphamapBlit;
324     AlphaRGBBlitFunc alphaRGBBlit;
325     RectFillFunc fillRect;
326     qreal m11, m12, m13, m21, m22, m23, m33, dx, dy;   // inverse xform matrix
327     const QClipData *clip;
328     enum Type {
329         None,
330         Solid,
331         LinearGradient,
332         RadialGradient,
333         ConicalGradient,
334         Texture
335     } type : 8;
336     int txop : 8;
337     int fast_matrix : 1;
338     bool bilinear;
339     QImage *tempImage;
340     union {
341         QSolidData solid;
342         QGradientData gradient;
343         QTextureData texture;
344     };
345
346     void init(QRasterBuffer *rb, const QRasterPaintEngine *pe);
347     void setup(const QBrush &brush, int alpha, QPainter::CompositionMode compositionMode);
348     void setupMatrix(const QTransform &matrix, int bilinear);
349     void initTexture(const QImage *image, int alpha, QTextureData::Type = QTextureData::Plain, const QRect &sourceRect = QRect());
350     void adjustSpanMethods();
351 };
352
353 static inline uint qt_gradient_clamp(const QGradientData *data, int ipos)
354 {
355     if (ipos < 0 || ipos >= GRADIENT_STOPTABLE_SIZE) {
356         if (data->spread == QGradient::RepeatSpread) {
357             ipos = ipos % GRADIENT_STOPTABLE_SIZE;
358             ipos = ipos < 0 ? GRADIENT_STOPTABLE_SIZE + ipos : ipos;
359         } else if (data->spread == QGradient::ReflectSpread) {
360             const int limit = GRADIENT_STOPTABLE_SIZE * 2;
361             ipos = ipos % limit;
362             ipos = ipos < 0 ? limit + ipos : ipos;
363             ipos = ipos >= GRADIENT_STOPTABLE_SIZE ? limit - 1 - ipos : ipos;
364         } else {
365             if (ipos < 0)
366                 ipos = 0;
367             else if (ipos >= GRADIENT_STOPTABLE_SIZE)
368                 ipos = GRADIENT_STOPTABLE_SIZE-1;
369         }
370     }
371
372     Q_ASSERT(ipos >= 0);
373     Q_ASSERT(ipos < GRADIENT_STOPTABLE_SIZE);
374
375     return ipos;
376 }
377
378 static inline uint qt_gradient_pixel(const QGradientData *data, qreal pos)
379 {
380     int ipos = int(pos * (GRADIENT_STOPTABLE_SIZE - 1) + qreal(0.5));
381     return data->colorTable[qt_gradient_clamp(data, ipos)];
382 }
383
384 static inline qreal qRadialDeterminant(qreal a, qreal b, qreal c)
385 {
386     return (b * b) - (4 * a * c);
387 }
388
389 template <class RadialFetchFunc>
390 const uint * QT_FASTCALL qt_fetch_radial_gradient_template(uint *buffer, const Operator *op, const QSpanData *data,
391                                                            int y, int x, int length)
392 {
393     // avoid division by zero
394     if (qFuzzyIsNull(op->radial.a)) {
395         extern void (*qt_memfill32)(quint32 *dest, quint32 value, int count);
396         qt_memfill32(buffer, 0, length);
397         return buffer;
398     }
399
400     const uint *b = buffer;
401     qreal rx = data->m21 * (y + qreal(0.5))
402                + data->dx + data->m11 * (x + qreal(0.5));
403     qreal ry = data->m22 * (y + qreal(0.5))
404                + data->dy + data->m12 * (x + qreal(0.5));
405     bool affine = !data->m13 && !data->m23;
406
407     uint *end = buffer + length;
408     if (affine) {
409         rx -= data->gradient.radial.focal.x;
410         ry -= data->gradient.radial.focal.y;
411
412         qreal inv_a = 1 / qreal(2 * op->radial.a);
413
414         const qreal delta_rx = data->m11;
415         const qreal delta_ry = data->m12;
416
417         qreal b = 2*(op->radial.dr*data->gradient.radial.focal.radius + rx * op->radial.dx + ry * op->radial.dy);
418         qreal delta_b = 2*(delta_rx * op->radial.dx + delta_ry * op->radial.dy);
419         const qreal b_delta_b = 2 * b * delta_b;
420         const qreal delta_b_delta_b = 2 * delta_b * delta_b;
421
422         const qreal bb = b * b;
423         const qreal delta_bb = delta_b * delta_b;
424
425         b *= inv_a;
426         delta_b *= inv_a;
427
428         const qreal rxrxryry = rx * rx + ry * ry;
429         const qreal delta_rxrxryry = delta_rx * delta_rx + delta_ry * delta_ry;
430         const qreal rx_plus_ry = 2*(rx * delta_rx + ry * delta_ry);
431         const qreal delta_rx_plus_ry = 2 * delta_rxrxryry;
432
433         inv_a *= inv_a;
434
435         qreal det = (bb - 4 * op->radial.a * (op->radial.sqrfr - rxrxryry)) * inv_a;
436         qreal delta_det = (b_delta_b + delta_bb + 4 * op->radial.a * (rx_plus_ry + delta_rxrxryry)) * inv_a;
437         const qreal delta_delta_det = (delta_b_delta_b + 4 * op->radial.a * delta_rx_plus_ry) * inv_a;
438
439         RadialFetchFunc::fetch(buffer, end, op, data, det, delta_det, delta_delta_det, b, delta_b);
440     } else {
441         qreal rw = data->m23 * (y + qreal(0.5))
442                    + data->m33 + data->m13 * (x + qreal(0.5));
443
444         while (buffer < end) {
445             if (rw == 0) {
446                 *buffer = 0;
447             } else {
448                 qreal invRw = 1 / rw;
449                 qreal gx = rx * invRw - data->gradient.radial.focal.x;
450                 qreal gy = ry * invRw - data->gradient.radial.focal.y;
451                 qreal b  = 2*(op->radial.dr*data->gradient.radial.focal.radius + gx*op->radial.dx + gy*op->radial.dy);
452                 qreal det = qRadialDeterminant(op->radial.a, b, op->radial.sqrfr - (gx*gx + gy*gy));
453
454                 quint32 result = 0;
455                 if (det >= 0) {
456                     qreal detSqrt = qSqrt(det);
457
458                     qreal s0 = (-b - detSqrt) * op->radial.inv2a;
459                     qreal s1 = (-b + detSqrt) * op->radial.inv2a;
460
461                     qreal s = qMax(s0, s1);
462
463                     if (data->gradient.radial.focal.radius + op->radial.dr * s >= 0)
464                         result = qt_gradient_pixel(&data->gradient, s);
465                 }
466
467                 *buffer = result;
468             }
469
470             rx += data->m11;
471             ry += data->m12;
472             rw += data->m13;
473
474             ++buffer;
475         }
476     }
477
478     return b;
479 }
480
481 template <class Simd>
482 class QRadialFetchSimd
483 {
484 public:
485     static void fetch(uint *buffer, uint *end, const Operator *op, const QSpanData *data, qreal det,
486                       qreal delta_det, qreal delta_delta_det, qreal b, qreal delta_b)
487     {
488         typename Simd::Vect_buffer_f det_vec;
489         typename Simd::Vect_buffer_f delta_det4_vec;
490         typename Simd::Vect_buffer_f b_vec;
491
492         for (int i = 0; i < 4; ++i) {
493             det_vec.f[i] = det;
494             delta_det4_vec.f[i] = 4 * delta_det;
495             b_vec.f[i] = b;
496
497             det += delta_det;
498             delta_det += delta_delta_det;
499             b += delta_b;
500         }
501
502         const typename Simd::Float32x4 v_delta_delta_det16 = Simd::v_dup(16 * delta_delta_det);
503         const typename Simd::Float32x4 v_delta_delta_det6 = Simd::v_dup(6 * delta_delta_det);
504         const typename Simd::Float32x4 v_delta_b4 = Simd::v_dup(4 * delta_b);
505
506         const typename Simd::Float32x4 v_r0 = Simd::v_dup(data->gradient.radial.focal.radius);
507         const typename Simd::Float32x4 v_dr = Simd::v_dup(op->radial.dr);
508
509         const typename Simd::Float32x4 v_min = Simd::v_dup(0.0f);
510         const typename Simd::Float32x4 v_max = Simd::v_dup(float(GRADIENT_STOPTABLE_SIZE-1));
511         const typename Simd::Float32x4 v_half = Simd::v_dup(0.5f);
512
513         const typename Simd::Int32x4 v_repeat_mask = Simd::v_dup(~(uint(0xffffff) << GRADIENT_STOPTABLE_SIZE_SHIFT));
514         const typename Simd::Int32x4 v_reflect_mask = Simd::v_dup(~(uint(0xffffff) << (GRADIENT_STOPTABLE_SIZE_SHIFT+1)));
515
516         const typename Simd::Int32x4 v_reflect_limit = Simd::v_dup(2 * GRADIENT_STOPTABLE_SIZE - 1);
517
518         const int extended_mask = op->radial.extended ? 0x0 : ~0x0;
519
520 #define FETCH_RADIAL_LOOP_PROLOGUE \
521         while (buffer < end) { \
522             typename Simd::Vect_buffer_i v_buffer_mask; \
523             v_buffer_mask.v = Simd::v_greaterOrEqual(det_vec.v, v_min); \
524             const typename Simd::Float32x4 v_index_local = Simd::v_sub(Simd::v_sqrt(Simd::v_max(v_min, det_vec.v)), b_vec.v); \
525             const typename Simd::Float32x4 v_index = Simd::v_add(Simd::v_mul(v_index_local, v_max), v_half); \
526             v_buffer_mask.v = Simd::v_and(v_buffer_mask.v, Simd::v_greaterOrEqual(Simd::v_add(v_r0, Simd::v_mul(v_dr, v_index_local)), v_min)); \
527             typename Simd::Vect_buffer_i index_vec;
528 #define FETCH_RADIAL_LOOP_CLAMP_REPEAT \
529             index_vec.v = Simd::v_and(v_repeat_mask, Simd::v_toInt(v_index));
530 #define FETCH_RADIAL_LOOP_CLAMP_REFLECT \
531             const typename Simd::Int32x4 v_index_i = Simd::v_and(v_reflect_mask, Simd::v_toInt(v_index)); \
532             const typename Simd::Int32x4 v_index_i_inv = Simd::v_sub(v_reflect_limit, v_index_i); \
533             index_vec.v = Simd::v_min_16(v_index_i, v_index_i_inv);
534 #define FETCH_RADIAL_LOOP_CLAMP_PAD \
535             index_vec.v = Simd::v_toInt(Simd::v_min(v_max, Simd::v_max(v_min, v_index)));
536 #define FETCH_RADIAL_LOOP_EPILOGUE \
537             det_vec.v = Simd::v_add(Simd::v_add(det_vec.v, delta_det4_vec.v), v_delta_delta_det6); \
538             delta_det4_vec.v = Simd::v_add(delta_det4_vec.v, v_delta_delta_det16); \
539             b_vec.v = Simd::v_add(b_vec.v, v_delta_b4); \
540             for (int i = 0; i < 4; ++i) \
541                 *buffer++ = (extended_mask | v_buffer_mask.i[i]) & data->gradient.colorTable[index_vec.i[i]]; \
542         }
543
544 #define FETCH_RADIAL_LOOP(FETCH_RADIAL_LOOP_CLAMP) \
545         FETCH_RADIAL_LOOP_PROLOGUE \
546         FETCH_RADIAL_LOOP_CLAMP \
547         FETCH_RADIAL_LOOP_EPILOGUE
548
549         switch (data->gradient.spread) {
550         case QGradient::RepeatSpread:
551             FETCH_RADIAL_LOOP(FETCH_RADIAL_LOOP_CLAMP_REPEAT)
552             break;
553         case QGradient::ReflectSpread:
554             FETCH_RADIAL_LOOP(FETCH_RADIAL_LOOP_CLAMP_REFLECT)
555             break;
556         case QGradient::PadSpread:
557             FETCH_RADIAL_LOOP(FETCH_RADIAL_LOOP_CLAMP_PAD)
558             break;
559         default:
560             Q_ASSERT(false);
561         }
562     }
563 };
564
565 #if defined(Q_CC_RVCT)
566 #  pragma push
567 #  pragma arm
568 #endif
569 Q_STATIC_INLINE_FUNCTION uint INTERPOLATE_PIXEL_255(uint x, uint a, uint y, uint b) {
570     uint t = (x & 0xff00ff) * a + (y & 0xff00ff) * b;
571     t = (t + ((t >> 8) & 0xff00ff) + 0x800080) >> 8;
572     t &= 0xff00ff;
573
574     x = ((x >> 8) & 0xff00ff) * a + ((y >> 8) & 0xff00ff) * b;
575     x = (x + ((x >> 8) & 0xff00ff) + 0x800080);
576     x &= 0xff00ff00;
577     x |= t;
578     return x;
579 }
580 #if defined(Q_CC_RVCT)
581 #  pragma pop
582 #endif
583
584 #if QT_POINTER_SIZE == 8 // 64-bit versions
585
586 Q_STATIC_INLINE_FUNCTION uint INTERPOLATE_PIXEL_256(uint x, uint a, uint y, uint b) {
587     quint64 t = (((quint64(x)) | ((quint64(x)) << 24)) & 0x00ff00ff00ff00ff) * a;
588     t += (((quint64(y)) | ((quint64(y)) << 24)) & 0x00ff00ff00ff00ff) * b;
589     t >>= 8;
590     t &= 0x00ff00ff00ff00ff;
591     return (uint(t)) | (uint(t >> 24));
592 }
593
594 Q_STATIC_INLINE_FUNCTION uint BYTE_MUL(uint x, uint a) {
595     quint64 t = (((quint64(x)) | ((quint64(x)) << 24)) & 0x00ff00ff00ff00ff) * a;
596     t = (t + ((t >> 8) & 0xff00ff00ff00ff) + 0x80008000800080) >> 8;
597     t &= 0x00ff00ff00ff00ff;
598     return (uint(t)) | (uint(t >> 24));
599 }
600
601 Q_STATIC_INLINE_FUNCTION uint PREMUL(uint x) {
602     uint a = x >> 24;
603     quint64 t = (((quint64(x)) | ((quint64(x)) << 24)) & 0x00ff00ff00ff00ff) * a;
604     t = (t + ((t >> 8) & 0xff00ff00ff00ff) + 0x80008000800080) >> 8;
605     t &= 0x000000ff00ff00ff;
606     return (uint(t)) | (uint(t >> 24)) | (a << 24);
607 }
608
609 #else // 32-bit versions
610
611 Q_STATIC_INLINE_FUNCTION uint INTERPOLATE_PIXEL_256(uint x, uint a, uint y, uint b) {
612     uint t = (x & 0xff00ff) * a + (y & 0xff00ff) * b;
613     t >>= 8;
614     t &= 0xff00ff;
615
616     x = ((x >> 8) & 0xff00ff) * a + ((y >> 8) & 0xff00ff) * b;
617     x &= 0xff00ff00;
618     x |= t;
619     return x;
620 }
621
622 #if defined(Q_CC_RVCT)
623 #  pragma push
624 #  pragma arm
625 #endif
626 Q_STATIC_INLINE_FUNCTION uint BYTE_MUL(uint x, uint a) {
627     uint t = (x & 0xff00ff) * a;
628     t = (t + ((t >> 8) & 0xff00ff) + 0x800080) >> 8;
629     t &= 0xff00ff;
630
631     x = ((x >> 8) & 0xff00ff) * a;
632     x = (x + ((x >> 8) & 0xff00ff) + 0x800080);
633     x &= 0xff00ff00;
634     x |= t;
635     return x;
636 }
637 #if defined(Q_CC_RVCT)
638 #  pragma pop
639 #endif
640
641 Q_STATIC_INLINE_FUNCTION uint PREMUL(uint x) {
642     uint a = x >> 24;
643     uint t = (x & 0xff00ff) * a;
644     t = (t + ((t >> 8) & 0xff00ff) + 0x800080) >> 8;
645     t &= 0xff00ff;
646
647     x = ((x >> 8) & 0xff) * a;
648     x = (x + ((x >> 8) & 0xff) + 0x80);
649     x &= 0xff00;
650     x |= t | (a << 24);
651     return x;
652 }
653 #endif
654
655
656 Q_STATIC_INLINE_FUNCTION uint BYTE_MUL_RGB16(uint x, uint a) {
657     a += 1;
658     uint t = (((x & 0x07e0)*a) >> 8) & 0x07e0;
659     t |= (((x & 0xf81f)*(a>>2)) >> 6) & 0xf81f;
660     return t;
661 }
662
663 Q_STATIC_INLINE_FUNCTION uint BYTE_MUL_RGB16_32(uint x, uint a) {
664     uint t = (((x & 0xf81f07e0) >> 5)*a) & 0xf81f07e0;
665     t |= (((x & 0x07e0f81f)*a) >> 5) & 0x07e0f81f;
666     return t;
667 }
668
669 #define INV_PREMUL(p)                                   \
670     (qAlpha(p) == 0 ? 0 :                               \
671     ((qAlpha(p) << 24)                                  \
672      | (((255*qRed(p))/ qAlpha(p)) << 16)               \
673      | (((255*qGreen(p)) / qAlpha(p))  << 8)            \
674      | ((255*qBlue(p)) / qAlpha(p))))
675
676 template <class DST, class SRC>
677 inline DST qt_colorConvert(SRC color, DST dummy)
678 {
679     Q_UNUSED(dummy);
680     return DST(color);
681 }
682
683
684 template <>
685 inline quint32 qt_colorConvert(quint16 color, quint32 dummy)
686 {
687     Q_UNUSED(dummy);
688     const int r = (color & 0xf800);
689     const int g = (color & 0x07e0);
690     const int b = (color & 0x001f);
691     const int tr = (r >> 8) | (r >> 13);
692     const int tg = (g >> 3) | (g >> 9);
693     const int tb = (b << 3) | (b >> 2);
694
695     return qRgb(tr, tg, tb);
696 }
697
698 template <>
699 inline quint16 qt_colorConvert(quint32 color, quint16 dummy)
700 {
701     Q_UNUSED(dummy);
702     const int r = qRed(color) << 8;
703     const int g = qGreen(color) << 3;
704     const int b = qBlue(color) >> 3;
705
706     return (r & 0xf800) | (g & 0x07e0)| (b & 0x001f);
707 }
708
709 class quint32p
710 {
711 public:
712     inline quint32p(quint32 v) : data(PREMUL(v)) {}
713
714     inline operator quint32() const { return data; }
715
716     inline operator quint16() const
717     {
718         return qt_colorConvert<quint16, quint32>(data, 0);
719     }
720
721     Q_STATIC_INLINE_FUNCTION quint32p fromRawData(quint32 v)
722     {
723         quint32p p;
724         p.data = v;
725         return p;
726     }
727
728 private:
729     quint32p() {}
730     quint32 data;
731 } Q_PACKED;
732
733 class qabgr8888
734 {
735 public:
736     inline qabgr8888(quint32 v)
737     {
738         data = qRgba(qBlue(v), qGreen(v), qRed(v), qAlpha(v));
739     }
740
741     inline bool operator==(const qabgr8888 &v) const { return data == v.data; }
742
743 private:
744     quint32 data;
745 } Q_PACKED;
746
747 class qrgb565;
748
749 class qargb8565
750 {
751 public:
752     Q_STATIC_INLINE_FUNCTION bool hasAlpha() { return true; }
753
754     inline qargb8565() {}
755     inline qargb8565(quint32 v);
756     inline explicit qargb8565(quint32p v);
757     inline qargb8565(const qargb8565 &v);
758     inline qargb8565(const qrgb565 &v);
759
760     inline operator quint32() const;
761     inline operator quint16() const;
762
763     inline quint8 alpha() const { return data[0]; }
764     inline qargb8565 truncedAlpha() {
765         data[0] &= 0xf8;
766         data[1] &= 0xdf;
767         return *this;
768     }
769     Q_STATIC_INLINE_FUNCTION quint8 alpha(quint8 a) { return (a + 1) >> 3; }
770     Q_STATIC_INLINE_FUNCTION quint8 ialpha(quint8 a) { return 0x20 - alpha(a); }
771
772     inline qargb8565 byte_mul(quint8 a) const;
773     inline qargb8565 operator+(qargb8565 v) const;
774     inline bool operator==(const qargb8565 &v) const;
775
776     inline quint32 rawValue() const;
777     inline quint16 rawValue16() const;
778
779 private:
780     friend class qrgb565;
781
782     quint8 data[3];
783 } Q_PACKED;
784
785 class qrgb565
786 {
787 public:
788     Q_STATIC_INLINE_FUNCTION bool hasAlpha() { return false; }
789
790     qrgb565(int v = 0) : data(v) {}
791
792     inline explicit qrgb565(quint32p v);
793     inline explicit qrgb565(quint32 v);
794     inline explicit qrgb565(const qargb8565 &v);
795
796     inline operator quint32() const;
797     inline operator quint16() const;
798
799     inline qrgb565 operator+(qrgb565 v) const;
800
801     inline quint8 alpha() const { return 0xff; }
802     inline qrgb565 truncedAlpha() { return *this; }
803     Q_STATIC_INLINE_FUNCTION quint8 alpha(quint8 a) { return (a + 1) >> 3; }
804     Q_STATIC_INLINE_FUNCTION quint8 ialpha(quint8 a) { return 0x20 - alpha(a); }
805
806     inline qrgb565 byte_mul(quint8 a) const;
807
808     inline bool operator==(const qrgb565 &v) const;
809     inline quint16 rawValue() const { return data; }
810
811 private:
812     friend class qargb8565;
813
814     quint16 data;
815 } Q_PACKED;
816
817 qargb8565::qargb8565(quint32 v)
818 {
819     *this = qargb8565(quint32p(v));
820 }
821
822 qargb8565::qargb8565(quint32p v)
823 {
824     data[0] = qAlpha(v);
825     const int r = qRed(v);
826     const int g = qGreen(v);
827     const int b = qBlue(v);
828     data[1] = ((g << 3) & 0xe0) | (b >> 3);
829     data[2] = (r & 0xf8) | (g >> 5);
830 }
831
832 qargb8565::qargb8565(const qargb8565 &v)
833 {
834     data[0] = v.data[0];
835     data[1] = v.data[1];
836     data[2] = v.data[2];
837 }
838
839 qargb8565::qargb8565(const qrgb565 &v)
840 {
841     data[0] = 0xff;
842     data[1] = v.data & 0xff;
843     data[2] = v.data >> 8;
844 }
845
846 qargb8565::operator quint32() const
847 {
848     const quint16 rgb = (data[2] << 8) | data[1];
849     const int a = data[0];
850     const int r = (rgb & 0xf800);
851     const int g = (rgb & 0x07e0);
852     const int b = (rgb & 0x001f);
853     const int tr = qMin(a, (r >> 8) | (r >> 13));
854     const int tg = qMin(a, (g >> 3) | (g >> 9));
855     const int tb = qMin(a, (b << 3) | (b >> 2));
856     return qRgba(tr, tg, tb, data[0]);
857 }
858
859 qargb8565::operator quint16() const
860 {
861     return (data[2] << 8) | data[1];
862 }
863
864 qargb8565 qargb8565::operator+(qargb8565 v) const
865 {
866     qargb8565 t;
867     t.data[0] = data[0] + v.data[0];
868     const quint16 rgb =  ((data[2] + v.data[2]) << 8)
869                          + (data[1] + v.data[1]);
870     t.data[1] = rgb & 0xff;
871     t.data[2] = rgb >> 8;
872     return t;
873 }
874
875 qargb8565 qargb8565::byte_mul(quint8 a) const
876 {
877     qargb8565 result;
878     result.data[0] = (data[0] * a) >> 5;
879
880     const quint16 x = (data[2] << 8) | data[1];
881     const quint16 t = ((((x & 0x07e0) >> 5) * a) & 0x07e0) |
882                       ((((x & 0xf81f) * a) >> 5) & 0xf81f);
883     result.data[1] = t & 0xff;
884     result.data[2] = t >> 8;
885     return result;
886 }
887
888 bool qargb8565::operator==(const qargb8565 &v) const
889 {
890     return data[0] == v.data[0]
891         && data[1] == v.data[1]
892         && data[2] == v.data[2];
893 }
894
895 quint32 qargb8565::rawValue() const
896 {
897     return (data[2] << 16) | (data[1] << 8) | data[0];
898 }
899
900 quint16 qargb8565::rawValue16() const
901 {
902     return (data[2] << 8) | data[1];
903 }
904
905 qrgb565::qrgb565(quint32p v)
906 {
907     *this = qrgb565(quint32(v));
908 }
909
910 qrgb565::qrgb565(quint32 v)
911 {
912     const int r = qRed(v) << 8;
913     const int g = qGreen(v) << 3;
914     const int b = qBlue(v) >> 3;
915
916     data = (r & 0xf800) | (g & 0x07e0)| (b & 0x001f);
917 }
918
919 qrgb565::qrgb565(const qargb8565 &v)
920 {
921     data = (v.data[2] << 8) | v.data[1];
922 }
923
924 qrgb565::operator quint32() const
925 {
926     const int r = (data & 0xf800);
927     const int g = (data & 0x07e0);
928     const int b = (data & 0x001f);
929     const int tr = (r >> 8) | (r >> 13);
930     const int tg = (g >> 3) | (g >> 9);
931     const int tb = (b << 3) | (b >> 2);
932     return qRgb(tr, tg, tb);
933 }
934
935 qrgb565::operator quint16() const
936 {
937     return data;
938 }
939
940 qrgb565 qrgb565::operator+(qrgb565 v) const
941 {
942     qrgb565 t;
943     t.data = data + v.data;
944     return t;
945 }
946
947 qrgb565 qrgb565::byte_mul(quint8 a) const
948 {
949     qrgb565 result;
950     result.data = ((((data & 0x07e0) >> 5) * a) & 0x07e0) |
951                   ((((data & 0xf81f) * a) >> 5) & 0xf81f);
952     return result;
953 }
954
955 bool qrgb565::operator==(const qrgb565 &v) const
956 {
957     return data == v.data;
958 }
959
960 class qbgr565
961 {
962 public:
963     inline qbgr565(quint16 v)
964     {
965         data = ((v & 0x001f) << 11) |
966                (v & 0x07e0) |
967                ((v & 0xf800) >> 11);
968     }
969
970     inline bool operator==(const qbgr565 &v) const
971     {
972         return data == v.data;
973     }
974
975 private:
976     quint16 data;
977 } Q_PACKED;
978
979 class qrgb555;
980
981 class qargb8555
982 {
983 public:
984     Q_STATIC_INLINE_FUNCTION bool hasAlpha() { return true; }
985
986     qargb8555() {}
987     inline qargb8555(quint32 v);
988     inline explicit qargb8555(quint32p v);
989     inline qargb8555(const qargb8555 &v);
990     inline qargb8555(const qrgb555 &v);
991
992     inline operator quint32() const;
993
994     inline quint8 alpha() const { return data[0]; }
995     inline qargb8555 truncedAlpha() { data[0] &= 0xf8; return *this; }
996     Q_STATIC_INLINE_FUNCTION quint8 alpha(quint8 a) { return (a + 1) >> 3; }
997     Q_STATIC_INLINE_FUNCTION quint8 ialpha(quint8 a) { return 0x20 - alpha(a); }
998
999     inline qargb8555 operator+(qargb8555 v) const;
1000     inline qargb8555 byte_mul(quint8 a) const;
1001
1002     inline bool operator==(const qargb8555 &v) const;
1003
1004     inline quint32 rawValue() const;
1005
1006 private:
1007     friend class qrgb555;
1008     quint8 data[3];
1009 } Q_PACKED;
1010
1011 class qrgb555
1012 {
1013 public:
1014     Q_STATIC_INLINE_FUNCTION bool hasAlpha() { return false; }
1015
1016     inline qrgb555(int v = 0) : data(v) {}
1017
1018     inline explicit qrgb555(quint32p v) { *this = qrgb555(quint32(v)); }
1019
1020     inline explicit qrgb555(quint32 v)
1021     {
1022         const int r = qRed(v) << 7;
1023         const int g = qGreen(v) << 2;
1024         const int b = qBlue(v) >> 3;
1025
1026         data = (r & 0x7c00) | (g & 0x03e0) | (b & 0x001f);
1027     }
1028
1029     inline explicit qrgb555(quint16 v)
1030     {
1031         data = ((v >> 1) & (0x7c00 | 0x03e0)) |
1032                (v & 0x001f);
1033     }
1034
1035     inline explicit qrgb555(const qargb8555 &v);
1036
1037     inline operator quint32() const
1038     {
1039         const int r = (data & 0x7c00);
1040         const int g = (data & 0x03e0);
1041         const int b = (data & 0x001f);
1042         const int tr = (r >> 7) | (r >> 12);
1043         const int tg = (g >> 2) | (g >> 7);
1044         const int tb = (b << 3) | (b >> 2);
1045
1046         return qRgb(tr, tg, tb);
1047     }
1048
1049     inline operator quint16() const
1050     {
1051         const int r = ((data & 0x7c00) << 1) & 0xf800;
1052         const int g = (((data & 0x03e0) << 1) | ((data >> 4) & 0x0020)) & 0x07e0;
1053         const int b = (data & 0x001f);
1054
1055         return r | g | b;
1056     }
1057
1058     inline qrgb555 operator+(qrgb555 v) const;
1059     inline qrgb555 byte_mul(quint8 a) const;
1060
1061     inline quint8 alpha() const { return 0xff; }
1062     inline qrgb555 truncedAlpha() { return *this; }
1063     Q_STATIC_INLINE_FUNCTION quint8 alpha(quint8 a) { return (a + 1) >> 3; }
1064     Q_STATIC_INLINE_FUNCTION quint8 ialpha(quint8 a) { return 0x20 - alpha(a); }
1065
1066     inline bool operator==(const qrgb555 &v) const { return v.data == data; }
1067     inline bool operator!=(const qrgb555 &v) const { return v.data != data; }
1068
1069     inline quint16 rawValue() const { return data; }
1070
1071 private:
1072     friend class qargb8555;
1073     friend class qbgr555;
1074     quint16 data;
1075
1076 } Q_PACKED;
1077
1078 qrgb555::qrgb555(const qargb8555 &v)
1079 {
1080     data = (v.data[2] << 8) | v.data[1];
1081 }
1082
1083 qrgb555 qrgb555::operator+(qrgb555 v) const
1084 {
1085     qrgb555 t;
1086     t.data = data + v.data;
1087     return t;
1088 }
1089
1090 qrgb555 qrgb555::byte_mul(quint8 a) const
1091 {
1092     quint16 t = (((data & 0x3e0) * a) >> 5) & 0x03e0;
1093     t |= (((data & 0x7c1f) * a) >> 5) & 0x7c1f;
1094
1095     qrgb555 result;
1096     result.data = t;
1097     return result;
1098 }
1099
1100 class qbgr555
1101 {
1102 public:
1103     inline qbgr555(quint32 v) { *this = qbgr555(qrgb555(v)); }
1104
1105     inline qbgr555(qrgb555 v)
1106     {
1107         data = ((v.data & 0x001f) << 10) |
1108                (v.data & 0x03e0) |
1109                ((v.data & 0x7c00) >> 10);
1110     }
1111
1112     inline bool operator==(const qbgr555 &v) const
1113     {
1114         return data == v.data;
1115     }
1116
1117 private:
1118     quint16 data;
1119 } Q_PACKED;
1120
1121 qargb8555::qargb8555(quint32 v)
1122 {
1123     v = quint32p(v);
1124     data[0] = qAlpha(v);
1125     const int r = qRed(v);
1126     const int g = qGreen(v);
1127     const int b = qBlue(v);
1128     data[1] = ((g << 2) & 0xe0) | (b >> 3);
1129     data[2] = ((r >> 1) & 0x7c) | (g >> 6);
1130
1131 }
1132
1133 qargb8555::qargb8555(quint32p v)
1134 {
1135     data[0] = qAlpha(v);
1136     const int r = qRed(v);
1137     const int g = qGreen(v);
1138     const int b = qBlue(v);
1139     data[1] = ((g << 2) & 0xe0) | (b >> 3);
1140     data[2] = ((r >> 1) & 0x7c) | (g >> 6);
1141 }
1142
1143 qargb8555::qargb8555(const qargb8555 &v)
1144 {
1145     data[0] = v.data[0];
1146     data[1] = v.data[1];
1147     data[2] = v.data[2];
1148 }
1149
1150 qargb8555::qargb8555(const qrgb555 &v)
1151 {
1152     data[0] = 0xff;
1153     data[1] = v.data & 0xff;
1154     data[2] = v.data >> 8;
1155 }
1156
1157 qargb8555::operator quint32() const
1158 {
1159     const quint16 rgb = (data[2] << 8) | data[1];
1160     const int r = (rgb & 0x7c00);
1161     const int g = (rgb & 0x03e0);
1162     const int b = (rgb & 0x001f);
1163     const int tr = (r >> 7) | (r >> 12);
1164     const int tg = (g >> 2) | (g >> 7);
1165     const int tb = (b << 3) | (b >> 2);
1166
1167     return qRgba(tr, tg, tb, data[0]);
1168 }
1169
1170 bool qargb8555::operator==(const qargb8555 &v) const
1171 {
1172     return data[0] == v.data[0]
1173         && data[1] == v.data[1]
1174         && data[2] == v.data[2];
1175 }
1176
1177 quint32 qargb8555::rawValue() const
1178 {
1179     return (data[2] << 16) | (data[1] << 8) | data[0];
1180 }
1181
1182 qargb8555 qargb8555::operator+(qargb8555 v) const
1183 {
1184     qargb8555 t;
1185     t.data[0] = data[0] + v.data[0];
1186     const quint16 rgb =  ((data[2] + v.data[2]) << 8)
1187                          + (data[1] + v.data[1]);
1188     t.data[1] = rgb & 0xff;
1189     t.data[2] = rgb >> 8;
1190     return t;
1191 }
1192
1193 qargb8555 qargb8555::byte_mul(quint8 a) const
1194 {
1195     qargb8555 result;
1196     result.data[0] = (data[0] * a) >> 5;
1197
1198     const quint16 x = (data[2] << 8) | data[1];
1199     quint16 t = (((x & 0x3e0) * a) >> 5) & 0x03e0;
1200     t |= (((x & 0x7c1f) * a) >> 5) & 0x7c1f;
1201     result.data[1] = t & 0xff;
1202     result.data[2] = t >> 8;
1203     return result;
1204
1205 }
1206
1207 class qrgb666;
1208
1209 class qargb6666
1210 {
1211 public:
1212     Q_STATIC_INLINE_FUNCTION bool hasAlpha() { return true; }
1213
1214     inline qargb6666() {}
1215     inline qargb6666(quint32 v) { *this = qargb6666(quint32p(v)); }
1216     inline explicit qargb6666(quint32p v);
1217     inline qargb6666(const qargb6666 &v);
1218     inline qargb6666(const qrgb666 &v);
1219
1220     inline operator quint32 () const;
1221
1222     inline quint8 alpha() const;
1223     inline qargb6666 truncedAlpha() { return *this; }
1224     Q_STATIC_INLINE_FUNCTION quint8 alpha(quint8 a) { return (a + 1) >> 2; }
1225     Q_STATIC_INLINE_FUNCTION quint8 ialpha(quint8 a) { return (255 - a + 1) >> 2; }
1226
1227     inline qargb6666 byte_mul(quint8 a) const;
1228     inline qargb6666 operator+(qargb6666 v) const;
1229     inline bool operator==(const qargb6666 &v) const;
1230
1231     inline quint32 rawValue() const;
1232
1233 private:
1234     friend class qrgb666;
1235     quint8 data[3];
1236
1237 } Q_PACKED;
1238
1239 class qrgb666
1240 {
1241 public:
1242     Q_STATIC_INLINE_FUNCTION bool hasAlpha() { return false; }
1243
1244     inline qrgb666() {}
1245     inline qrgb666(quint32 v);
1246     inline qrgb666(const qargb6666 &v);
1247
1248     inline operator quint32 () const;
1249
1250     inline quint8 alpha() const { return 0xff; }
1251     inline qrgb666 truncedAlpha() { return *this; }
1252     Q_STATIC_INLINE_FUNCTION quint8 alpha(quint8 a) { return (a + 1) >> 2; }
1253     Q_STATIC_INLINE_FUNCTION quint8 ialpha(quint8 a) { return (255 - a + 1) >> 2; }
1254
1255     inline qrgb666 operator+(qrgb666 v) const;
1256     inline qrgb666 byte_mul(quint8 a) const;
1257
1258     inline bool operator==(const qrgb666 &v) const;
1259     inline bool operator!=(const qrgb666 &v) const { return !(*this == v); }
1260
1261     inline quint32 rawValue() const
1262     {
1263         return (data[2] << 16) | (data[1] << 8) | data[0];
1264     }
1265
1266 private:
1267     friend class qargb6666;
1268
1269     quint8 data[3];
1270 } Q_PACKED;
1271
1272 qrgb666::qrgb666(quint32 v)
1273 {
1274     const uchar b = qBlue(v);
1275     const uchar g = qGreen(v);
1276     const uchar r = qRed(v);
1277     const uint p = (b >> 2) | ((g >> 2) << 6) | ((r >> 2) << 12);
1278     data[0] = qBlue(p);
1279     data[1] = qGreen(p);
1280     data[2] = qRed(p);
1281 }
1282
1283 qrgb666::qrgb666(const qargb6666 &v)
1284 {
1285     data[0] = v.data[0];
1286     data[1] = v.data[1];
1287     data[2] = v.data[2] & 0x03;
1288 }
1289
1290 qrgb666::operator quint32 () const
1291 {
1292     const uchar r = (data[2] << 6) | ((data[1] & 0xf0) >> 2) | (data[2] & 0x3);
1293     const uchar g = (data[1] << 4) | ((data[0] & 0xc0) >> 4) | ((data[1] & 0x0f) >> 2);
1294     const uchar b = (data[0] << 2) | ((data[0] & 0x3f) >> 4);
1295     return qRgb(r, g, b);
1296 }
1297
1298 qrgb666 qrgb666::operator+(qrgb666 v) const
1299 {
1300     const quint32 x1 = (data[2] << 16) | (data[1] << 8) | data[0];
1301     const quint32 x2 = (v.data[2] << 16) | (v.data[1] << 8) | v.data[0];
1302     const quint32 t = x1 + x2;
1303     qrgb666 r;
1304     r.data[0] = t & 0xff;
1305     r.data[1] = (t >> 8) & 0xff;
1306     r.data[2] = (t >> 16) & 0xff;
1307     return r;
1308 }
1309
1310 qrgb666 qrgb666::byte_mul(quint8 a) const
1311 {
1312     const quint32 x = (data[2] << 16) | (data[1] << 8) | data[0];
1313     const quint32 t = ((((x & 0x03f03f) * a) >> 6) & 0x03f03f) |
1314                       ((((x & 0x000fc0) * a) >> 6) & 0x000fc0);
1315
1316     qrgb666 r;
1317     r.data[0] = t & 0xff;
1318     r.data[1] = (t >> 8) & 0xff;
1319     r.data[2] = (t >> 16) & 0xff;
1320     return r;
1321 }
1322
1323 bool qrgb666::operator==(const qrgb666 &v) const
1324 {
1325     return (data[0] == v.data[0] &&
1326             data[1] == v.data[1] &&
1327             data[2] == v.data[2]);
1328 }
1329
1330 qargb6666::qargb6666(quint32p v)
1331 {
1332     const quint8 b = qBlue(v) >> 2;
1333     const quint8 g = qGreen(v) >> 2;
1334     const quint8 r = qRed(v) >> 2;
1335     const quint8 a = qAlpha(v) >> 2;
1336     const uint p = (a << 18) | (r << 12) | (g << 6) | b;
1337     data[0] = qBlue(p);
1338     data[1] = qGreen(p);
1339     data[2] = qRed(p);
1340 }
1341
1342 qargb6666::qargb6666(const qargb6666 &v)
1343 {
1344     data[0] = v.data[0];
1345     data[1] = v.data[1];
1346     data[2] = v.data[2];
1347 }
1348
1349 qargb6666::qargb6666(const qrgb666 &v)
1350 {
1351     data[0] = v.data[0];
1352     data[1] = v.data[1];
1353     data[2] = (v.data[2] | 0xfc);
1354 }
1355
1356 qargb6666::operator quint32 () const
1357 {
1358     const quint8 r = (data[2] << 6) | ((data[1] & 0xf0) >> 2) | (data[2] & 0x3);
1359     const quint8 g = (data[1] << 4) | ((data[0] & 0xc0) >> 4) | ((data[1] & 0x0f) >> 2);
1360     const quint8 b = (data[0] << 2) | ((data[0] & 0x3f) >> 4);
1361     const quint8 a = (data[2] & 0xfc) | (data[2] >> 6);
1362     return qRgba(r, g, b, a);
1363 }
1364
1365 qargb6666 qargb6666::operator+(qargb6666 v) const
1366 {
1367     const quint32 x1 = (data[2] << 16) | (data[1] << 8) | data[0];
1368     const quint32 x2 = (v.data[2] << 16) | (v.data[1] << 8) | v.data[0];
1369     const quint32 t = x1 + x2;
1370     qargb6666 r;
1371     r.data[0] = t & 0xff;
1372     r.data[1] = (t >> 8) & 0xff;
1373     r.data[2] = (t >> 16) & 0xff;
1374     return r;
1375 }
1376
1377 quint8 qargb6666::alpha() const
1378 {
1379     return (data[2] & 0xfc) | (data[2] >> 6);
1380 }
1381
1382 inline qargb6666 qargb6666::byte_mul(quint8 a) const
1383 {
1384     const quint32 x = (data[2] << 16) | (data[1] << 8) | data[0];
1385     const quint32 t = ((((x & 0x03f03f) * a) >> 6) & 0x03f03f) |
1386                       ((((x & 0xfc0fc0) * a) >> 6) & 0xfc0fc0);
1387
1388     qargb6666 r;
1389     r.data[0] = t & 0xff;
1390     r.data[1] = (t >> 8) & 0xff;
1391     r.data[2] = (t >> 16) & 0xff;
1392     return r;
1393 }
1394
1395 bool qargb6666::operator==(const qargb6666 &v) const
1396 {
1397     return data[0] == v.data[0]
1398         && data[1] == v.data[1]
1399         && data[2] == v.data[2];
1400 }
1401
1402 quint32 qargb6666::rawValue() const
1403 {
1404     return (data[2] << 16) | (data[1] << 8) | data[0];
1405 }
1406
1407 class qrgb888
1408 {
1409 public:
1410     Q_STATIC_INLINE_FUNCTION bool hasAlpha() { return false; }
1411
1412     inline qrgb888() {}
1413     inline qrgb888(quint32 v);
1414
1415     inline operator quint32() const;
1416
1417     inline quint8 alpha() const { return 0xff; }
1418     inline qrgb888 truncedAlpha() { return *this; }
1419     Q_STATIC_INLINE_FUNCTION quint8 alpha(quint8 a) { return a; }
1420     Q_STATIC_INLINE_FUNCTION quint8 ialpha(quint8 a) { return 255 - a; }
1421
1422     inline qrgb888 byte_mul(quint8 a) const;
1423     inline qrgb888 operator+(qrgb888 v) const;
1424     inline bool operator==(qrgb888 v) const;
1425
1426     inline quint32 rawValue() const;
1427
1428 private:
1429     uchar data[3];
1430
1431 } Q_PACKED;
1432
1433 qrgb888::qrgb888(quint32 v)
1434 {
1435     data[0] = qRed(v);
1436     data[1] = qGreen(v);
1437     data[2] = qBlue(v);
1438 }
1439
1440 qrgb888::operator quint32() const
1441 {
1442     return qRgb(data[0], data[1], data[2]);
1443 }
1444
1445 qrgb888 qrgb888::operator+(qrgb888 v) const
1446 {
1447     qrgb888 t = *this;
1448     t.data[0] += v.data[0];
1449     t.data[1] += v.data[1];
1450     t.data[2] += v.data[2];
1451     return t;
1452 }
1453
1454 qrgb888 qrgb888::byte_mul(quint8 a) const
1455 {
1456     quint32 x(*this);
1457
1458     quint32 t = (x & 0xff00ff) * a;
1459     t = (t + ((t >> 8) & 0xff00ff) + 0x800080) >> 8;
1460     t &= 0xff00ff;
1461
1462     x = ((x >> 8) & 0xff00ff) * a;
1463     x = (x + ((x >> 8) & 0xff00ff) + 0x800080);
1464     x &= 0xff00ff00;
1465     x |= t;
1466     return qrgb888(x);
1467 }
1468
1469 bool qrgb888::operator==(qrgb888 v) const
1470 {
1471     return (data[0] == v.data[0] &&
1472             data[1] == v.data[1] &&
1473             data[2] == v.data[2]);
1474 }
1475
1476 quint32 qrgb888::rawValue() const
1477 {
1478     return (data[2] << 16) | (data[1] << 8) | data[0];
1479 }
1480
1481 template <>
1482 inline qrgb888 qt_colorConvert(quint32 color, qrgb888 dummy)
1483 {
1484     Q_UNUSED(dummy);
1485     return qrgb888(color);
1486 }
1487
1488 template <>
1489 inline quint32 qt_colorConvert(qrgb888 color, quint32 dummy)
1490 {
1491     Q_UNUSED(dummy);
1492     return quint32(color);
1493 }
1494
1495 #ifdef QT_QWS_DEPTH_8
1496 template <>
1497 inline quint8 qt_colorConvert(quint32 color, quint8 dummy)
1498 {
1499     Q_UNUSED(dummy);
1500
1501     uchar r = ((qRed(color) & 0xf8) + 0x19) / 0x33;
1502     uchar g = ((qGreen(color) &0xf8) + 0x19) / 0x33;
1503     uchar b = ((qBlue(color) &0xf8) + 0x19) / 0x33;
1504
1505     return r*6*6 + g*6 + b;
1506 }
1507
1508 template <>
1509 inline quint8 qt_colorConvert(quint16 color, quint8 dummy)
1510 {
1511     Q_UNUSED(dummy);
1512
1513     uchar r = (color & 0xf800) >> (11-3);
1514     uchar g = (color & 0x07c0) >> (6-3);
1515     uchar b = (color & 0x001f) << 3;
1516
1517     uchar tr = (r + 0x19) / 0x33;
1518     uchar tg = (g + 0x19) / 0x33;
1519     uchar tb = (b + 0x19) / 0x33;
1520
1521     return tr*6*6 + tg*6 + tb;
1522 }
1523
1524 #endif // QT_QWS_DEPTH_8
1525
1526 // hw: endianess??
1527 class quint24
1528 {
1529 public:
1530     inline quint24(quint32 v)
1531     {
1532         data[0] = qBlue(v);
1533         data[1] = qGreen(v);
1534         data[2] = qRed(v);
1535     }
1536
1537     inline operator quint32 ()
1538     {
1539         return qRgb(data[2], data[1], data[0]);
1540     }
1541
1542     inline bool operator==(const quint24 &v) const
1543     {
1544         return data[0] == v.data[0]
1545             && data[1] == v.data[1]
1546             && data[2] == v.data[2];
1547     }
1548
1549 private:
1550     uchar data[3];
1551 } Q_PACKED;
1552
1553 template <>
1554 inline quint24 qt_colorConvert(quint32 color, quint24 dummy)
1555 {
1556     Q_UNUSED(dummy);
1557     return quint24(color);
1558 }
1559
1560 // hw: endianess??
1561 class quint18
1562 {
1563 public:
1564     inline quint18(quint32 v)
1565     {
1566         uchar b = qBlue(v);
1567         uchar g = qGreen(v);
1568         uchar r = qRed(v);
1569         uint p = (b >> 2) | ((g >> 2) << 6) | ((r >> 2) << 12);
1570         data[0] = qBlue(p);
1571         data[1] = qGreen(p);
1572         data[2] = qRed(p);
1573     }
1574
1575     inline operator quint32 ()
1576     {
1577         const uchar r = (data[2] << 6) | ((data[1] & 0xf0) >> 2) | (data[2] & 0x3);
1578         const uchar g = (data[1] << 4) | ((data[0] & 0xc0) >> 4) | ((data[1] & 0x0f) >> 2);
1579         const uchar b = (data[0] << 2) | ((data[0] & 0x3f) >> 4);
1580         return qRgb(r, g, b);
1581     }
1582
1583 private:
1584     uchar data[3];
1585 } Q_PACKED;
1586
1587 template <>
1588 inline quint18 qt_colorConvert(quint32 color, quint18 dummy)
1589 {
1590     Q_UNUSED(dummy);
1591     return quint18(color);
1592 }
1593
1594 class qrgb444;
1595
1596 class qargb4444
1597 {
1598 public:
1599     Q_STATIC_INLINE_FUNCTION bool hasAlpha() { return true; }
1600
1601     inline qargb4444() {}
1602     inline qargb4444(quint32 v) { *this = qargb4444(quint32p(v)); }
1603     inline explicit qargb4444(quint32p v);
1604     inline qargb4444(const qrgb444 &v);
1605
1606     inline operator quint32() const;
1607     inline operator quint8() const;
1608
1609     inline qargb4444 operator+(qargb4444 v) const;
1610
1611     inline quint8 alpha() const { return ((data & 0xf000) >> 8) | ((data & 0xf000) >> 12); }
1612     inline qargb4444 truncedAlpha() { return *this; }
1613     Q_STATIC_INLINE_FUNCTION quint8 alpha(quint8 a) { return (a + 1) >> 4; }
1614     Q_STATIC_INLINE_FUNCTION quint8 ialpha(quint8 a) { return 0x10 - alpha(a); }
1615     inline qargb4444 byte_mul(quint8 a) const;
1616
1617     inline bool operator==(const qargb4444 &v) const { return data == v.data; }
1618
1619     inline quint16 rawValue() const { return data; }
1620
1621 private:
1622     friend class qrgb444;
1623     quint16 data;
1624
1625 } Q_PACKED;
1626
1627 class qrgb444
1628 {
1629 public:
1630     Q_STATIC_INLINE_FUNCTION bool hasAlpha() { return false; }
1631
1632     inline qrgb444() {}
1633     inline qrgb444(quint32 v);
1634     inline explicit qrgb444(qargb4444 v);
1635
1636     inline operator quint32() const;
1637     inline operator quint8() const;
1638
1639     inline qrgb444 operator+(qrgb444 v) const;
1640     inline quint8 alpha() const { return 0xff; }
1641     inline qrgb444 truncedAlpha() { return *this; }
1642     Q_STATIC_INLINE_FUNCTION quint8 alpha(quint8 a) { return (a + 1) >> 4; }
1643     Q_STATIC_INLINE_FUNCTION quint8 ialpha(quint8 a) { return 0x10 - alpha(a); }
1644     inline qrgb444 byte_mul(quint8 a) const;
1645
1646     inline bool operator==(const qrgb444 &v) const { return data == v.data; }
1647     inline bool operator!=(const qrgb444 &v) const { return data != v.data; }
1648
1649     inline quint16 rawValue() const { return data; }
1650
1651 private:
1652     friend class qargb4444;
1653     quint16 data;
1654
1655 } Q_PACKED;
1656
1657
1658 qargb4444::qargb4444(quint32p color)
1659 {
1660     quint32 v = color;
1661     v &= 0xf0f0f0f0;
1662     const int a = qAlpha(v) << 8;
1663     const int r = qRed(v) << 4;
1664     const int g = qGreen(v);
1665     const int b = qBlue(v) >> 4;
1666
1667     data = a | r | g | b;
1668 }
1669
1670 qargb4444::qargb4444(const qrgb444 &v)
1671 {
1672     data = v.data | 0xf000;
1673 }
1674
1675 qargb4444::operator quint32() const
1676 {
1677     const int a = (data & 0xf000);
1678     const int r = (data & 0x0f00);
1679     const int g = (data & 0x00f0);
1680     const int b = (data & 0x000f);
1681     const int ta = (a >> 8) | (a >> 12);
1682     const int tr = (r >> 4) | (r >> 8);
1683     const int tg = g | (g >> 4);
1684     const int tb = (b << 4) | b;
1685
1686     return qRgba(tr, tg, tb, ta);
1687 }
1688
1689 qargb4444::operator quint8() const
1690 {
1691     // hw: optimize!
1692     return qt_colorConvert<quint8, quint32>(operator quint32(), 0);
1693 }
1694
1695 qargb4444 qargb4444::operator+(qargb4444 v) const
1696 {
1697     qargb4444 t;
1698     t.data = data + v.data;
1699     return t;
1700 }
1701
1702 qargb4444 qargb4444::byte_mul(quint8 a) const
1703 {
1704     quint16 t = (((data & 0xf0f0) * a) >> 4) & 0xf0f0;
1705     t |= (((data & 0x0f0f) * a) >> 4) & 0x0f0f;
1706
1707     qargb4444 result;
1708     result.data = t;
1709     return result;
1710 }
1711
1712 qrgb444::qrgb444(quint32 v)
1713 {
1714     v &= 0xf0f0f0f0;
1715     const int r = qRed(v) << 4;
1716     const int g = qGreen(v);
1717     const int b = qBlue(v) >> 4;
1718
1719     data = r | g | b;
1720 }
1721
1722 qrgb444::qrgb444(qargb4444 v)
1723 {
1724     data = v.data & 0x0fff;
1725 }
1726
1727 qrgb444::operator quint32() const
1728 {
1729     const int r = (data & 0x0f00);
1730     const int g = (data & 0x00f0);
1731     const int b = (data & 0x000f);
1732     const int tr = (r >> 4) | (r >> 8);
1733     const int tg = g | (g >> 4);
1734     const int tb = (b << 4) | b;
1735
1736     return qRgb(tr, tg, tb);
1737 }
1738
1739 qrgb444::operator quint8() const
1740 {
1741     // hw: optimize!
1742     return qt_colorConvert<quint8, quint32>(operator quint32(), 0);
1743 }
1744
1745 qrgb444 qrgb444::operator+(qrgb444 v) const
1746 {
1747     qrgb444 t;
1748     t.data = data + v.data;
1749     return t;
1750 }
1751
1752 qrgb444 qrgb444::byte_mul(quint8 a) const
1753 {
1754     quint16 t = (((data & 0xf0f0) * a) >> 4) & 0xf0f0;
1755     t |= (((data & 0x0f0f) * a) >> 4) & 0x0f0f;
1756
1757     qrgb444 result;
1758     result.data = t;
1759     return result;
1760 }
1761
1762 #ifdef QT_QWS_DEPTH_GENERIC
1763
1764 struct qrgb
1765 {
1766 public:
1767     static int bpp;
1768     static int len_red;
1769     static int len_green;
1770     static int len_blue;
1771     static int len_alpha;
1772     static int off_red;
1773     static int off_green;
1774     static int off_blue;
1775     static int off_alpha;
1776 } Q_PACKED;
1777
1778 template <typename SRC>
1779 Q_STATIC_TEMPLATE_FUNCTION inline quint32 qt_convertToRgb(SRC color);
1780
1781 template <>
1782 inline quint32 qt_convertToRgb(quint32 color)
1783 {
1784     const int r = qRed(color) >> (8 - qrgb::len_red);
1785     const int g = qGreen(color) >> (8 - qrgb::len_green);
1786     const int b = qBlue(color) >> (8 - qrgb::len_blue);
1787     const int a = qAlpha(color) >> (8 - qrgb::len_alpha);
1788     const quint32 v = (r << qrgb::off_red)
1789                       | (g << qrgb::off_green)
1790                       | (b << qrgb::off_blue)
1791                       | (a << qrgb::off_alpha);
1792
1793     return v;
1794 }
1795
1796 template <>
1797 inline quint32 qt_convertToRgb(quint16 color)
1798 {
1799     return qt_convertToRgb(qt_colorConvert<quint32, quint16>(color, 0));
1800 }
1801
1802 class qrgb_generic16
1803 {
1804 public:
1805     inline qrgb_generic16(quint32 color)
1806     {
1807         const int r = qRed(color) >> (8 - qrgb::len_red);
1808         const int g = qGreen(color) >> (8 - qrgb::len_green);
1809         const int b = qBlue(color) >> (8 - qrgb::len_blue);
1810         const int a = qAlpha(color) >> (8 - qrgb::len_alpha);
1811         data = (r << qrgb::off_red)
1812                | (g << qrgb::off_green)
1813                | (b << qrgb::off_blue)
1814                | (a << qrgb::off_alpha);
1815     }
1816
1817     inline operator quint16 () { return data; }
1818     inline quint32 operator<<(int shift) const { return data << shift; }
1819
1820 private:
1821     quint16 data;
1822 } Q_PACKED;
1823
1824 template <>
1825 inline qrgb_generic16 qt_colorConvert(quint32 color, qrgb_generic16 dummy)
1826 {
1827     Q_UNUSED(dummy);
1828     return qrgb_generic16(color);
1829 }
1830
1831 template <>
1832 inline qrgb_generic16 qt_colorConvert(quint16 color, qrgb_generic16 dummy)
1833 {
1834     Q_UNUSED(dummy);
1835     return qrgb_generic16(qt_colorConvert<quint32, quint16>(color, 0));
1836 }
1837
1838 #endif // QT_QWS_DEPTH_GENERIC
1839
1840 template <class T>
1841 void qt_memfill(T *dest, T value, int count);
1842
1843 template<> inline void qt_memfill(quint32 *dest, quint32 color, int count)
1844 {
1845     extern void (*qt_memfill32)(quint32 *dest, quint32 value, int count);
1846     qt_memfill32(dest, color, count);
1847 }
1848
1849 template<> inline void qt_memfill(quint16 *dest, quint16 color, int count)
1850 {
1851     extern void (*qt_memfill16)(quint16 *dest, quint16 value, int count);
1852     qt_memfill16(dest, color, count);
1853 }
1854
1855 template<> inline void qt_memfill(quint8 *dest, quint8 color, int count)
1856 {
1857     memset(dest, color, count);
1858 }
1859
1860 template <class T>
1861 inline void qt_memfill(T *dest, T value, int count)
1862 {
1863     if (!count)
1864         return;
1865
1866     int n = (count + 7) / 8;
1867     switch (count & 0x07)
1868     {
1869     case 0: do { *dest++ = value;
1870     case 7:      *dest++ = value;
1871     case 6:      *dest++ = value;
1872     case 5:      *dest++ = value;
1873     case 4:      *dest++ = value;
1874     case 3:      *dest++ = value;
1875     case 2:      *dest++ = value;
1876     case 1:      *dest++ = value;
1877     } while (--n > 0);
1878     }
1879 }
1880
1881 template <class T>
1882 inline void qt_rectfill(T *dest, T value,
1883                         int x, int y, int width, int height, int stride)
1884 {
1885     char *d = reinterpret_cast<char*>(dest + x) + y * stride;
1886     if (uint(stride) == (width * sizeof(T))) {
1887         qt_memfill(reinterpret_cast<T*>(d), value, width * height);
1888     } else {
1889         for (int j = 0; j < height; ++j) {
1890             dest = reinterpret_cast<T*>(d);
1891             qt_memfill(dest, value, width);
1892             d += stride;
1893         }
1894     }
1895 }
1896
1897 template <class DST, class SRC>
1898 inline void qt_memconvert(DST *dest, const SRC *src, int count)
1899 {
1900     if (sizeof(DST) == 1) {
1901         while (count) {
1902             int n = 1;
1903             const SRC color = *src++;
1904             const DST dstColor = qt_colorConvert<DST, SRC>(color, 0);
1905             while (--count && (*src == color || dstColor == qt_colorConvert<DST, SRC>(*src, 0))) {
1906                 ++n;
1907                 ++src;
1908             }
1909             qt_memfill(dest, dstColor, n);
1910             dest += n;
1911         }
1912     } else {
1913         /* Duff's device */
1914         int n = (count + 7) / 8;
1915         switch (count & 0x07)
1916         {
1917         case 0: do { *dest++ = qt_colorConvert<DST, SRC>(*src++, 0);
1918             case 7:      *dest++ = qt_colorConvert<DST, SRC>(*src++, 0);
1919             case 6:      *dest++ = qt_colorConvert<DST, SRC>(*src++, 0);
1920             case 5:      *dest++ = qt_colorConvert<DST, SRC>(*src++, 0);
1921             case 4:      *dest++ = qt_colorConvert<DST, SRC>(*src++, 0);
1922             case 3:      *dest++ = qt_colorConvert<DST, SRC>(*src++, 0);
1923             case 2:      *dest++ = qt_colorConvert<DST, SRC>(*src++, 0);
1924             case 1:      *dest++ = qt_colorConvert<DST, SRC>(*src++, 0);
1925             } while (--n > 0);
1926         }
1927     }
1928 }
1929
1930 #define QT_TRIVIAL_MEMCONVERT_IMPL(T) \
1931     template <> \
1932     inline void qt_memconvert(T *dest, const T *src, int count) \
1933     { \
1934         memcpy(dest, src, count * sizeof(T)); \
1935     }
1936 QT_TRIVIAL_MEMCONVERT_IMPL(quint32)
1937 QT_TRIVIAL_MEMCONVERT_IMPL(qrgb888)
1938 QT_TRIVIAL_MEMCONVERT_IMPL(qargb6666)
1939 QT_TRIVIAL_MEMCONVERT_IMPL(qrgb666)
1940 QT_TRIVIAL_MEMCONVERT_IMPL(quint16)
1941 QT_TRIVIAL_MEMCONVERT_IMPL(qrgb565)
1942 QT_TRIVIAL_MEMCONVERT_IMPL(qargb8565)
1943 QT_TRIVIAL_MEMCONVERT_IMPL(qargb8555)
1944 QT_TRIVIAL_MEMCONVERT_IMPL(qrgb555)
1945 QT_TRIVIAL_MEMCONVERT_IMPL(qargb4444)
1946 QT_TRIVIAL_MEMCONVERT_IMPL(qrgb444)
1947 #undef QT_TRIVIAL_MEMCONVERT_IMPL
1948
1949 #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
1950 template <>
1951 inline void qt_memconvert(qrgb666 *dest, const quint32 *src, int count)
1952 {
1953     if (count < 3) {
1954         switch (count) {
1955         case 2: *dest++ = qrgb666(*src++);
1956         case 1: *dest = qrgb666(*src);
1957         }
1958         return;
1959     }
1960
1961     const int align = (quintptr(dest) & 3);
1962     switch (align) {
1963     case 1: *dest++ = qrgb666(*src++); --count;
1964     case 2: *dest++ = qrgb666(*src++); --count;
1965     case 3: *dest++ = qrgb666(*src++); --count;
1966     }
1967
1968     quint32 *dest32 = reinterpret_cast<quint32*>(dest);
1969     int sourceCount = count >> 2;
1970     while (sourceCount--) {
1971         dest32[0] = ((src[1] & 0x00000c00) << 20)
1972                     | ((src[1] & 0x000000fc) << 22)
1973                     | ((src[0] & 0x00fc0000) >> 6)
1974                     | ((src[0] & 0x0000fc00) >> 4)
1975                     |  ((src[0] & 0x000000fc) >> 2);
1976         dest32[1] = ((src[2] & 0x003c0000) << 10)
1977                     | ((src[2] & 0x0000fc00) << 12)
1978                     | ((src[2] & 0x000000fc) << 14)
1979                     | ((src[1] & 0x00fc0000) >> 14)
1980                     | ((src[1] & 0x0000f000) >> 12);
1981         dest32[2] = ((src[3] & 0x00fc0000) << 2)
1982                     | ((src[3] & 0x0000fc00) << 4)
1983                     | ((src[3] & 0x000000fc) << 6)
1984                     | ((src[2] & 0x00c00000) >> 22);
1985         dest32 += 3;
1986         src += 4;
1987     }
1988
1989     dest = reinterpret_cast<qrgb666*>(dest32);
1990     switch (count & 3) {
1991     case 3: *dest++ = qrgb666(*src++);
1992     case 2: *dest++ = qrgb666(*src++);
1993     case 1: *dest = qrgb666(*src);
1994     }
1995 }
1996 #endif // Q_BYTE_ORDER
1997
1998 template <class T>
1999 inline void qt_rectcopy(T *dest, const T *src,
2000                         int x, int y, int width, int height,
2001                         int dstStride, int srcStride)
2002 {
2003     char *d = (char*)(dest + x) + y * dstStride;
2004     const char *s = (char*)(src);
2005     for (int i = 0; i < height; ++i) {
2006         ::memcpy(d, s, width * sizeof(T));
2007         d += dstStride;
2008         s += srcStride;
2009     }
2010 }
2011
2012 template <class DST, class SRC>
2013 inline void qt_rectconvert(DST *dest, const SRC *src,
2014                            int x, int y, int width, int height,
2015                            int dstStride, int srcStride)
2016 {
2017     char *d = (char*)(dest + x) + y * dstStride;
2018     const char *s = (char*)(src);
2019     for (int i = 0; i < height; ++i) {
2020         qt_memconvert<DST,SRC>((DST*)d, (const SRC*)s, width);
2021         d += dstStride;
2022         s += srcStride;
2023     }
2024 }
2025
2026 #define QT_RECTCONVERT_TRIVIAL_IMPL(T)                                  \
2027     template <>                                                         \
2028     inline void qt_rectconvert(T *dest, const T *src,                   \
2029                                int x, int y, int width, int height,     \
2030                                int dstStride, int srcStride)            \
2031     {                                                                   \
2032         qt_rectcopy(dest, src, x, y, width, height, dstStride, srcStride); \
2033     }
2034 QT_RECTCONVERT_TRIVIAL_IMPL(quint32)
2035 QT_RECTCONVERT_TRIVIAL_IMPL(qrgb888)
2036 QT_RECTCONVERT_TRIVIAL_IMPL(qargb6666)
2037 QT_RECTCONVERT_TRIVIAL_IMPL(qrgb666)
2038 QT_RECTCONVERT_TRIVIAL_IMPL(qrgb565)
2039 QT_RECTCONVERT_TRIVIAL_IMPL(qargb8565)
2040 QT_RECTCONVERT_TRIVIAL_IMPL(quint16)
2041 QT_RECTCONVERT_TRIVIAL_IMPL(qargb8555)
2042 QT_RECTCONVERT_TRIVIAL_IMPL(qrgb555)
2043 QT_RECTCONVERT_TRIVIAL_IMPL(qargb4444)
2044 QT_RECTCONVERT_TRIVIAL_IMPL(qrgb444)
2045 #undef QT_RECTCONVERT_TRIVIAL_IMPL
2046
2047 #ifdef QT_QWS_DEPTH_GENERIC
2048 template <> void qt_rectconvert(qrgb *dest, const quint32 *src,
2049                                 int x, int y, int width, int height,
2050                                 int dstStride, int srcStride);
2051
2052 template <> void qt_rectconvert(qrgb *dest, const quint16 *src,
2053                                 int x, int y, int width, int height,
2054                                 int dstStride, int srcStride);
2055 #endif // QT_QWS_DEPTH_GENERIC
2056
2057 #define QT_MEMFILL_UINT(dest, length, color)            \
2058     qt_memfill<quint32>(dest, color, length);
2059
2060 #define QT_MEMFILL_USHORT(dest, length, color) \
2061     qt_memfill<quint16>(dest, color, length);
2062
2063 #define QT_MEMCPY_REV_UINT(dest, src, length) \
2064 do {                                          \
2065     /* Duff's device */                       \
2066     uint *_d = (uint*)(dest) + length;         \
2067     const uint *_s = (uint*)(src) + length;    \
2068     register int n = ((length) + 7) / 8;      \
2069     switch ((length) & 0x07)                  \
2070     {                                         \
2071     case 0: do { *--_d = *--_s;                 \
2072     case 7:      *--_d = *--_s;                 \
2073     case 6:      *--_d = *--_s;                 \
2074     case 5:      *--_d = *--_s;                 \
2075     case 4:      *--_d = *--_s;                 \
2076     case 3:      *--_d = *--_s;                 \
2077     case 2:      *--_d = *--_s;                 \
2078     case 1:      *--_d = *--_s;                 \
2079     } while (--n > 0);                        \
2080     }                                         \
2081 } while (0)
2082
2083 #define QT_MEMCPY_USHORT(dest, src, length) \
2084 do {                                          \
2085     /* Duff's device */                       \
2086     ushort *_d = (ushort*)(dest);         \
2087     const ushort *_s = (ushort*)(src);    \
2088     register int n = ((length) + 7) / 8;      \
2089     switch ((length) & 0x07)                  \
2090     {                                         \
2091     case 0: do { *_d++ = *_s++;                 \
2092     case 7:      *_d++ = *_s++;                 \
2093     case 6:      *_d++ = *_s++;                 \
2094     case 5:      *_d++ = *_s++;                 \
2095     case 4:      *_d++ = *_s++;                 \
2096     case 3:      *_d++ = *_s++;                 \
2097     case 2:      *_d++ = *_s++;                 \
2098     case 1:      *_d++ = *_s++;                 \
2099     } while (--n > 0);                        \
2100     }                                         \
2101 } while (0)
2102
2103 #if defined(Q_CC_RVCT)
2104 #  pragma push
2105 #  pragma arm
2106 #endif
2107 Q_STATIC_INLINE_FUNCTION int qt_div_255(int x) { return (x + (x>>8) + 0x80) >> 8; }
2108 #if defined(Q_CC_RVCT)
2109 #  pragma pop
2110 #endif
2111
2112 inline ushort qConvertRgb32To16(uint c)
2113 {
2114    return (((c) >> 3) & 0x001f)
2115        | (((c) >> 5) & 0x07e0)
2116        | (((c) >> 8) & 0xf800);
2117 }
2118
2119 #if defined(Q_WS_QWS) || (QT_VERSION >= 0x040400)
2120 inline quint32 qConvertRgb32To16x2(quint64 c)
2121 {
2122     c = (((c) >> 3) & Q_UINT64_C(0x001f0000001f))
2123         | (((c) >> 5) & Q_UINT64_C(0x07e0000007e0))
2124         | (((c) >> 8) & Q_UINT64_C(0xf8000000f800));
2125     return c | (c >> 16);
2126 }
2127 #endif
2128
2129 inline QRgb qConvertRgb16To32(uint c)
2130 {
2131     return 0xff000000
2132         | ((((c) << 3) & 0xf8) | (((c) >> 2) & 0x7))
2133         | ((((c) << 5) & 0xfc00) | (((c) >> 1) & 0x300))
2134         | ((((c) << 8) & 0xf80000) | (((c) << 3) & 0x70000));
2135 }
2136
2137 inline int qRed565(quint16 rgb) {
2138     const int r = (rgb & 0xf800);
2139     return (r >> 8) | (r >> 13);
2140 }
2141
2142 inline int qGreen565(quint16 rgb) {
2143     const int g = (rgb & 0x07e0);
2144     return (g >> 3) | (g >> 9);
2145 }
2146
2147 inline int qBlue565(quint16 rgb) {
2148     const int b = (rgb & 0x001f);
2149     return (b << 3) | (b >> 2);
2150 }
2151
2152 const uint qt_bayer_matrix[16][16] = {
2153     { 0x1, 0xc0, 0x30, 0xf0, 0xc, 0xcc, 0x3c, 0xfc,
2154       0x3, 0xc3, 0x33, 0xf3, 0xf, 0xcf, 0x3f, 0xff},
2155     { 0x80, 0x40, 0xb0, 0x70, 0x8c, 0x4c, 0xbc, 0x7c,
2156       0x83, 0x43, 0xb3, 0x73, 0x8f, 0x4f, 0xbf, 0x7f},
2157     { 0x20, 0xe0, 0x10, 0xd0, 0x2c, 0xec, 0x1c, 0xdc,
2158       0x23, 0xe3, 0x13, 0xd3, 0x2f, 0xef, 0x1f, 0xdf},
2159     { 0xa0, 0x60, 0x90, 0x50, 0xac, 0x6c, 0x9c, 0x5c,
2160       0xa3, 0x63, 0x93, 0x53, 0xaf, 0x6f, 0x9f, 0x5f},
2161     { 0x8, 0xc8, 0x38, 0xf8, 0x4, 0xc4, 0x34, 0xf4,
2162       0xb, 0xcb, 0x3b, 0xfb, 0x7, 0xc7, 0x37, 0xf7},
2163     { 0x88, 0x48, 0xb8, 0x78, 0x84, 0x44, 0xb4, 0x74,
2164       0x8b, 0x4b, 0xbb, 0x7b, 0x87, 0x47, 0xb7, 0x77},
2165     { 0x28, 0xe8, 0x18, 0xd8, 0x24, 0xe4, 0x14, 0xd4,
2166       0x2b, 0xeb, 0x1b, 0xdb, 0x27, 0xe7, 0x17, 0xd7},
2167     { 0xa8, 0x68, 0x98, 0x58, 0xa4, 0x64, 0x94, 0x54,
2168       0xab, 0x6b, 0x9b, 0x5b, 0xa7, 0x67, 0x97, 0x57},
2169     { 0x2, 0xc2, 0x32, 0xf2, 0xe, 0xce, 0x3e, 0xfe,
2170       0x1, 0xc1, 0x31, 0xf1, 0xd, 0xcd, 0x3d, 0xfd},
2171     { 0x82, 0x42, 0xb2, 0x72, 0x8e, 0x4e, 0xbe, 0x7e,
2172       0x81, 0x41, 0xb1, 0x71, 0x8d, 0x4d, 0xbd, 0x7d},
2173     { 0x22, 0xe2, 0x12, 0xd2, 0x2e, 0xee, 0x1e, 0xde,
2174       0x21, 0xe1, 0x11, 0xd1, 0x2d, 0xed, 0x1d, 0xdd},
2175     { 0xa2, 0x62, 0x92, 0x52, 0xae, 0x6e, 0x9e, 0x5e,
2176       0xa1, 0x61, 0x91, 0x51, 0xad, 0x6d, 0x9d, 0x5d},
2177     { 0xa, 0xca, 0x3a, 0xfa, 0x6, 0xc6, 0x36, 0xf6,
2178       0x9, 0xc9, 0x39, 0xf9, 0x5, 0xc5, 0x35, 0xf5},
2179     { 0x8a, 0x4a, 0xba, 0x7a, 0x86, 0x46, 0xb6, 0x76,
2180       0x89, 0x49, 0xb9, 0x79, 0x85, 0x45, 0xb5, 0x75},
2181     { 0x2a, 0xea, 0x1a, 0xda, 0x26, 0xe6, 0x16, 0xd6,
2182       0x29, 0xe9, 0x19, 0xd9, 0x25, 0xe5, 0x15, 0xd5},
2183     { 0xaa, 0x6a, 0x9a, 0x5a, 0xa6, 0x66, 0x96, 0x56,
2184       0xa9, 0x69, 0x99, 0x59, 0xa5, 0x65, 0x95, 0x55}
2185 };
2186
2187 #define ARGB_COMBINE_ALPHA(argb, alpha) \
2188     ((((argb >> 24) * alpha) >> 8) << 24) | (argb & 0x00ffffff)
2189
2190
2191 #if QT_POINTER_SIZE == 8 // 64-bit versions
2192 #define AMIX(mask) (qMin(((qint64(s)&mask) + (qint64(d)&mask)), qint64(mask)))
2193 #define MIX(mask) (qMin(((qint64(s)&mask) + (qint64(d)&mask)), qint64(mask)))
2194 #else // 32 bits
2195 // The mask for alpha can overflow over 32 bits
2196 #define AMIX(mask) quint32(qMin(((qint64(s)&mask) + (qint64(d)&mask)), qint64(mask)))
2197 #define MIX(mask) (qMin(((quint32(s)&mask) + (quint32(d)&mask)), quint32(mask)))
2198 #endif
2199
2200 inline int comp_func_Plus_one_pixel_const_alpha(uint d, const uint s, const uint const_alpha, const uint one_minus_const_alpha)
2201 {
2202     const int result = (AMIX(AMASK) | MIX(RMASK) | MIX(GMASK) | MIX(BMASK));
2203     return INTERPOLATE_PIXEL_255(result, const_alpha, d, one_minus_const_alpha);
2204 }
2205
2206 inline int comp_func_Plus_one_pixel(uint d, const uint s)
2207 {
2208     const int result = (AMIX(AMASK) | MIX(RMASK) | MIX(GMASK) | MIX(BMASK));
2209     return result;
2210 }
2211
2212 #undef MIX
2213 #undef AMIX
2214
2215 // prototypes of all the composition functions
2216 void QT_FASTCALL comp_func_SourceOver(uint *dest, const uint *src, int length, uint const_alpha);
2217 void QT_FASTCALL comp_func_DestinationOver(uint *dest, const uint *src, int length, uint const_alpha);
2218 void QT_FASTCALL comp_func_Clear(uint *dest, const uint *, int length, uint const_alpha);
2219 void QT_FASTCALL comp_func_Source(uint *dest, const uint *src, int length, uint const_alpha);
2220 void QT_FASTCALL comp_func_Destination(uint *, const uint *, int, uint);
2221 void QT_FASTCALL comp_func_SourceIn(uint *dest, const uint *src, int length, uint const_alpha);
2222 void QT_FASTCALL comp_func_DestinationIn(uint *dest, const uint *src, int length, uint const_alpha);
2223 void QT_FASTCALL comp_func_SourceOut(uint *dest, const uint *src, int length, uint const_alpha);
2224 void QT_FASTCALL comp_func_DestinationOut(uint *dest, const uint *src, int length, uint const_alpha);
2225 void QT_FASTCALL comp_func_SourceAtop(uint *dest, const uint *src, int length, uint const_alpha);
2226 void QT_FASTCALL comp_func_DestinationAtop(uint *dest, const uint *src, int length, uint const_alpha);
2227 void QT_FASTCALL comp_func_XOR(uint *dest, const uint *src, int length, uint const_alpha);
2228 void QT_FASTCALL comp_func_Plus(uint *dest, const uint *src, int length, uint const_alpha);
2229 void QT_FASTCALL comp_func_Multiply(uint *dest, const uint *src, int length, uint const_alpha);
2230 void QT_FASTCALL comp_func_Screen(uint *dest, const uint *src, int length, uint const_alpha);
2231 void QT_FASTCALL comp_func_Overlay(uint *dest, const uint *src, int length, uint const_alpha);
2232 void QT_FASTCALL comp_func_Darken(uint *dest, const uint *src, int length, uint const_alpha);
2233 void QT_FASTCALL comp_func_Lighten(uint *dest, const uint *src, int length, uint const_alpha);
2234 void QT_FASTCALL comp_func_ColorDodge(uint *dest, const uint *src, int length, uint const_alpha);
2235 void QT_FASTCALL comp_func_ColorBurn(uint *dest, const uint *src, int length, uint const_alpha);
2236 void QT_FASTCALL comp_func_HardLight(uint *dest, const uint *src, int length, uint const_alpha);
2237 void QT_FASTCALL comp_func_SoftLight(uint *dest, const uint *src, int length, uint const_alpha);
2238 void QT_FASTCALL comp_func_Difference(uint *dest, const uint *src, int length, uint const_alpha);
2239 void QT_FASTCALL comp_func_Exclusion(uint *dest, const uint *src, int length, uint const_alpha);
2240 void QT_FASTCALL rasterop_SourceOrDestination(uint *dest, const uint *src, int length, uint const_alpha);
2241 void QT_FASTCALL rasterop_SourceAndDestination(uint *dest, const uint *src, int length, uint const_alpha);
2242 void QT_FASTCALL rasterop_SourceXorDestination(uint *dest, const uint *src, int length, uint const_alpha);
2243 void QT_FASTCALL rasterop_NotSourceAndNotDestination(uint *dest, const uint *src, int length, uint const_alpha);
2244 void QT_FASTCALL rasterop_NotSourceOrNotDestination(uint *dest, const uint *src, int length, uint const_alpha);
2245 void QT_FASTCALL rasterop_NotSourceXorDestination(uint *dest, const uint *src, int length, uint const_alpha);
2246 void QT_FASTCALL rasterop_NotSource(uint *dest, const uint *src, int length, uint const_alpha);
2247 void QT_FASTCALL rasterop_NotSourceAndDestination(uint *dest, const uint *src, int length, uint const_alpha);
2248 void QT_FASTCALL rasterop_SourceAndNotDestination(uint *dest, const uint *src, int length, uint const_alpha);
2249
2250 // prototypes of all the solid composition functions
2251 void QT_FASTCALL comp_func_solid_SourceOver(uint *dest, int length, uint color, uint const_alpha);
2252 void QT_FASTCALL comp_func_solid_DestinationOver(uint *dest, int length, uint color, uint const_alpha);
2253 void QT_FASTCALL comp_func_solid_Clear(uint *dest, int length, uint color, uint const_alpha);
2254 void QT_FASTCALL comp_func_solid_Source(uint *dest, int length, uint color, uint const_alpha);
2255 void QT_FASTCALL comp_func_solid_Destination(uint *dest, int length, uint color, uint const_alpha);
2256 void QT_FASTCALL comp_func_solid_SourceIn(uint *dest, int length, uint color, uint const_alpha);
2257 void QT_FASTCALL comp_func_solid_DestinationIn(uint *dest, int length, uint color, uint const_alpha);
2258 void QT_FASTCALL comp_func_solid_SourceOut(uint *dest, int length, uint color, uint const_alpha);
2259 void QT_FASTCALL comp_func_solid_DestinationOut(uint *dest, int length, uint color, uint const_alpha);
2260 void QT_FASTCALL comp_func_solid_SourceAtop(uint *dest, int length, uint color, uint const_alpha);
2261 void QT_FASTCALL comp_func_solid_DestinationAtop(uint *dest, int length, uint color, uint const_alpha);
2262 void QT_FASTCALL comp_func_solid_XOR(uint *dest, int length, uint color, uint const_alpha);
2263 void QT_FASTCALL comp_func_solid_Plus(uint *dest, int length, uint color, uint const_alpha);
2264 void QT_FASTCALL comp_func_solid_Multiply(uint *dest, int length, uint color, uint const_alpha);
2265 void QT_FASTCALL comp_func_solid_Screen(uint *dest, int length, uint color, uint const_alpha);
2266 void QT_FASTCALL comp_func_solid_Overlay(uint *dest, int length, uint color, uint const_alpha);
2267 void QT_FASTCALL comp_func_solid_Darken(uint *dest, int length, uint color, uint const_alpha);
2268 void QT_FASTCALL comp_func_solid_Lighten(uint *dest, int length, uint color, uint const_alpha);
2269 void QT_FASTCALL comp_func_solid_ColorDodge(uint *dest, int length, uint color, uint const_alpha);
2270 void QT_FASTCALL comp_func_solid_ColorBurn(uint *dest, int length, uint color, uint const_alpha);
2271 void QT_FASTCALL comp_func_solid_HardLight(uint *dest, int length, uint color, uint const_alpha);
2272 void QT_FASTCALL comp_func_solid_SoftLight(uint *dest, int length, uint color, uint const_alpha);
2273 void QT_FASTCALL comp_func_solid_Difference(uint *dest, int length, uint color, uint const_alpha);
2274 void QT_FASTCALL comp_func_solid_Exclusion(uint *dest, int length, uint color, uint const_alpha);
2275 void QT_FASTCALL rasterop_solid_SourceOrDestination(uint *dest, int length, uint color, uint const_alpha);
2276 void QT_FASTCALL rasterop_solid_SourceAndDestination(uint *dest, int length, uint color, uint const_alpha);
2277 void QT_FASTCALL rasterop_solid_SourceXorDestination(uint *dest, int length, uint color, uint const_alpha);
2278 void QT_FASTCALL rasterop_solid_NotSourceAndNotDestination(uint *dest, int length, uint color, uint const_alpha);
2279 void QT_FASTCALL rasterop_solid_NotSourceOrNotDestination(uint *dest, int length, uint color, uint const_alpha);
2280 void QT_FASTCALL rasterop_solid_NotSourceXorDestination(uint *dest, int length, uint color, uint const_alpha);
2281 void QT_FASTCALL rasterop_solid_NotSource(uint *dest, int length, uint color, uint const_alpha);
2282 void QT_FASTCALL rasterop_solid_NotSourceAndDestination(uint *dest, int length, uint color, uint const_alpha);
2283 void QT_FASTCALL rasterop_solid_SourceAndNotDestination(uint *dest, int length, uint color, uint const_alpha);
2284
2285 QT_END_NAMESPACE
2286
2287 #endif // QDRAWHELPER_P_H