Qt 4.8
qcolormap_win.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
52 
53 class QColormapPrivate
54 {
55 public:
57  : ref(1), mode(QColormap::Direct), depth(0), hpal(0)
58  { }
59 
61 
63  int depth;
64  int numcolors;
65 
66  HPALETTE hpal;
68 };
69 
71 
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 
141 {
142  Q_ASSERT_X(screenMap, "QColormap",
143  "A QApplication object needs to be constructed before QColormap is used.");
144  return QColormap();
145 }
146 
148  : d(screenMap)
149 { d->ref.ref(); }
150 
152  :d (colormap.d)
153 { d->ref.ref(); }
154 
156 {
157  if (!d->ref.deref())
158  delete d;
159 }
160 
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 
195 { return d->palette; }
196 
197 QColormap &QColormap::operator=(const QColormap &colormap)
198 { qAtomicAssign(d, colormap.d); return *this; }
199 
200 
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
static QColormap instance(int screen=-1)
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
int size() const
static HPALETTE hPal()
ushort red
Returns the red color component of this color.
Definition: qcolor.h:243
QVector< QColor > palette
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
QColormap::Mode mode
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const QColor colorAt(uint pixel) const
int depth() const
QColormapPrivate * d
Definition: qcolormap.h:90
unsigned int uint
Definition: qglobal.h:996
QColormap & operator=(const QColormap &colormap)
#define FALSE
Synonym for false.
Definition: qglobal.h:1019
Q_GUI_EXPORT HDC qt_win_display_dc()
uint pixel(const QColor &color) const
#define rgb(r, g, b)
Definition: qcolor_p.cpp:130
QColor toRgb() const
Create and returns an RGB QColor based on this color.
Definition: qcolor.cpp:1636
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
ushort blue
Returns the blue color component of this color.
Definition: qcolor.h:245
const QVector< QColor > colormap() const
static void initialize()
Q_GUI_EXPORT_INLINE QRgb qRgb(int r, int g, int b)
Definition: qrgb.h:69
#define PALETTEINDEX(a)
void qAtomicAssign(T *&d, T *x)
This is a helper for the assignment operators of implicitly shared classes.
Definition: qatomic.h:195
Mode mode() const
QColormap()
Constructs a new colormap.
static QColormapPrivate * screenMap
ushort green
Returns the green color component of this color.
Definition: qcolor.h:244
static void cleanup()
void qErrnoWarning(const char *msg,...)
Definition: qglobal.cpp:2954