Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / src / gui / painting / qcolormap_win.cpp
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 #include "qcolor.h"
43 #include "qcolormap.h"
44 #include "qvector.h"
45 #include "qt_windows.h"
46
47 #if defined(Q_WS_WINCE)
48 #include "qguifunctions_wince.h"
49 #endif
50
51 QT_BEGIN_NAMESPACE
52
53 class QColormapPrivate
54 {
55 public:
56     inline QColormapPrivate()
57         : ref(1), mode(QColormap::Direct), depth(0), hpal(0)
58     { }
59
60     QAtomicInt ref;
61
62     QColormap::Mode mode;
63     int depth;
64     int numcolors;
65
66     HPALETTE hpal;
67     QVector<QColor> palette;
68 };
69
70 static QColormapPrivate *screenMap = 0;
71
72 void QColormap::initialize()
73 {
74     HDC dc = qt_win_display_dc();
75
76     screenMap = new QColormapPrivate;
77     screenMap->depth = GetDeviceCaps(dc, BITSPIXEL);
78
79     screenMap->numcolors = -1;
80     if (GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE)
81         screenMap->numcolors = GetDeviceCaps(dc, SIZEPALETTE);
82
83     if (screenMap->numcolors <= 16 || screenMap->numcolors > 256)        // no need to create palette
84         return;
85
86     LOGPALETTE* pal = 0;
87     int numPalEntries = 6*6*6; // color cube
88
89     pal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) + numPalEntries * sizeof(PALETTEENTRY));
90     // Make 6x6x6 color cube
91     int idx = 0;
92     for(int ir = 0x0; ir <= 0xff; ir+=0x33) {
93         for(int ig = 0x0; ig <= 0xff; ig+=0x33) {
94             for(int ib = 0x0; ib <= 0xff; ib+=0x33) {
95                 pal->palPalEntry[idx].peRed = ir;
96                 pal->palPalEntry[idx].peGreen = ig;
97                 pal->palPalEntry[idx].peBlue = ib;
98                 pal->palPalEntry[idx].peFlags = 0;
99                 idx++;
100             }
101         }
102     }
103
104     pal->palVersion = 0x300;
105     pal->palNumEntries = numPalEntries;
106
107     screenMap->hpal = CreatePalette(pal);
108     if (!screenMap->hpal)
109         qErrnoWarning("QColor::initialize: Failed to create logical palette");
110     free (pal);
111
112     SelectPalette(dc, screenMap->hpal, FALSE);
113     RealizePalette(dc);
114
115     PALETTEENTRY paletteEntries[256];
116     screenMap->numcolors = GetPaletteEntries(screenMap->hpal, 0, 255, paletteEntries);
117
118     screenMap->palette.resize(screenMap->numcolors);
119     for (int i = 0; i < screenMap->numcolors; i++) {
120         screenMap->palette[i] = qRgb(paletteEntries[i].peRed,
121                                      paletteEntries[i].peGreen,
122                                      paletteEntries[i].peBlue);
123     }
124 }
125
126 void QColormap::cleanup()
127 {
128     if (!screenMap)
129         return;
130
131     if (screenMap->hpal) {                                // delete application global
132         DeleteObject(screenMap->hpal);                        // palette
133         screenMap->hpal = 0;
134     }
135
136     delete screenMap;
137     screenMap = 0;
138 }
139
140 QColormap QColormap::instance(int)
141 {
142     Q_ASSERT_X(screenMap, "QColormap",
143                "A QApplication object needs to be constructed before QColormap is used.");
144     return QColormap();
145 }
146
147 QColormap::QColormap()
148     : d(screenMap)
149 { d->ref.ref(); }
150
151 QColormap::QColormap(const QColormap &colormap)
152     :d (colormap.d)
153 { d->ref.ref(); }
154
155 QColormap::~QColormap()
156 {
157     if (!d->ref.deref())
158         delete d;
159 }
160
161 QColormap::Mode QColormap::mode() const
162 { return d->mode; }
163
164 int QColormap::depth() const
165 { return d->depth; }
166
167 int QColormap::size() const
168 { return d->numcolors; }
169
170 uint QColormap::pixel(const QColor &color) const
171 {
172     const QColor c = color.toRgb();
173     COLORREF rgb = RGB(c.red(), c.green(), c.blue());
174     if (d->hpal)
175         return PALETTEINDEX(GetNearestPaletteIndex(d->hpal, rgb));
176     return rgb;
177 }
178
179 const QColor QColormap::colorAt(uint pixel) const
180 {
181     if (d->hpal) {
182         if (pixel < uint(d->numcolors))
183             return d->palette.at(pixel);
184         return QColor();
185     }
186     return QColor(GetRValue(pixel), GetGValue(pixel), GetBValue(pixel));
187 }
188
189
190 HPALETTE QColormap::hPal()
191 { return screenMap ? screenMap->hpal : 0; }
192
193
194 const QVector<QColor> QColormap::colormap() const
195 { return d->palette; }
196
197 QColormap &QColormap::operator=(const QColormap &colormap)
198 { qAtomicAssign(d, colormap.d); return *this; }
199
200
201 QT_END_NAMESPACE