makefiles: Output rules for building generated .rc files.
[wine.git] / dlls / gdi32 / tests / palette.c
blob0043f5560c417e5a8ca9590e3e8a67ed22e4ab75
1 /*
2 * Unit test suite for palettes
4 * Copyright 2005 Glenn Wurster
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "mmsystem.h"
29 #include "wine/test.h"
31 static const PALETTEENTRY logpalettedata[8] = {
32 { 0x10, 0x20, 0x30, PC_NOCOLLAPSE },
33 { 0x20, 0x30, 0x40, PC_NOCOLLAPSE },
34 { 0x30, 0x40, 0x50, PC_NOCOLLAPSE },
35 { 0x40, 0x50, 0x60, PC_NOCOLLAPSE },
36 { 0x50, 0x60, 0x70, PC_NOCOLLAPSE },
37 { 0x60, 0x70, 0x80, PC_NOCOLLAPSE },
38 { 0x70, 0x80, 0x90, PC_NOCOLLAPSE },
39 { 0x80, 0x90, 0xA0, PC_NOCOLLAPSE },
42 static void test_DIB_PAL_COLORS(void) {
43 HDC hdc = GetDC( NULL );
44 HDC memhdc = CreateCompatibleDC( hdc );
45 HBITMAP hbmp, hbmpOld;
46 char bmpbuf[sizeof(BITMAPINFO) + 10 * sizeof(WORD)];
47 PBITMAPINFO bmp = (PBITMAPINFO)bmpbuf;
48 WORD * bmpPalPtr;
49 char logpalettebuf[sizeof(LOGPALETTE) + sizeof(logpalettedata)];
50 PLOGPALETTE logpalette = (PLOGPALETTE)logpalettebuf;
51 HPALETTE hpal, hpalOld;
52 COLORREF setColor, chkColor, getColor;
53 int i;
55 /* Initialize the logical palette with a few colours */
56 logpalette->palVersion = 0x300;
57 logpalette->palNumEntries = 8;
58 memcpy( logpalette->palPalEntry, logpalettedata, sizeof(logpalettedata) );
59 hpal = CreatePalette( logpalette );
60 hpalOld = SelectPalette( memhdc, hpal, FALSE );
61 ok( hpalOld != NULL, "error=%d\n", GetLastError() );
63 /* Create a DIB BMP which references colours in the logical palette */
64 memset( bmp, 0x00, sizeof(BITMAPINFO) );
65 bmp->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
66 bmp->bmiHeader.biWidth = 1;
67 bmp->bmiHeader.biHeight = 1;
68 bmp->bmiHeader.biPlanes = 1;
69 bmp->bmiHeader.biBitCount = 8;
70 bmp->bmiHeader.biCompression = BI_RGB;
71 bmp->bmiHeader.biClrUsed = 10;
72 bmp->bmiHeader.biClrImportant = 0;
73 bmpPalPtr = (WORD *)&bmp->bmiColors;
74 for( i = 0; i < 8; i++ ) {
75 *bmpPalPtr++ = i;
77 *bmpPalPtr++ = 8; /* Pointer to logical palette index just outside range */
78 *bmpPalPtr++ = 19; /* Pointer to bad logical palette index */
80 hbmp = CreateDIBSection( memhdc, bmp, DIB_PAL_COLORS, 0, 0, 0 );
81 ok( hbmp != NULL, "error=%d\n", GetLastError() );
82 hbmpOld = SelectObject( memhdc, hbmp );
83 ok( hbmpOld != NULL, "error=%d\n", GetLastError() );
85 /* Test with a RGB to DIB_PAL_COLORS */
86 setColor = RGB( logpalettedata[1].peRed, logpalettedata[1].peGreen, logpalettedata[1].peBlue );
87 SetPixel( memhdc, 0, 0, setColor );
88 chkColor = RGB( logpalettedata[1].peRed, logpalettedata[1].peGreen, logpalettedata[1].peBlue );
89 getColor = GetPixel( memhdc, 0, 0 );
90 ok( getColor == chkColor, "getColor=%08X\n", (UINT)getColor );
92 /* Test with a valid DIBINDEX to DIB_PAL_COLORS */
93 setColor = DIBINDEX( 2 );
94 SetPixel( memhdc, 0, 0, setColor );
95 chkColor = RGB( logpalettedata[2].peRed, logpalettedata[2].peGreen, logpalettedata[2].peBlue );
96 getColor = GetPixel( memhdc, 0, 0 );
97 ok( getColor == chkColor, "getColor=%08X\n", (UINT)getColor );
99 /* Test with an invalid DIBINDEX to DIB_PAL_COLORS */
100 setColor = DIBINDEX( 12 );
101 SetPixel( memhdc, 0, 0, setColor );
102 chkColor = RGB( 0, 0, 0 );
103 getColor = GetPixel( memhdc, 0, 0 );
104 ok( getColor == chkColor, "getColor=%08X\n", (UINT)getColor );
106 /* Test for double wraparound on logical palette references from */
107 /* DIBINDEX by DIB_PAL_COLORS. */
108 setColor = DIBINDEX( 9 );
109 SetPixel( memhdc, 0, 0, setColor );
110 chkColor = RGB( logpalettedata[3].peRed, logpalettedata[3].peGreen, logpalettedata[3].peBlue );
111 getColor = GetPixel( memhdc, 0, 0 );
112 ok( getColor == chkColor, "getColor=%08X\n", (UINT)getColor );
114 SelectPalette( memhdc, hpalOld, FALSE );
115 DeleteObject( hpal );
116 SelectObject( memhdc, hbmpOld );
117 DeleteObject( hbmp );
118 DeleteDC( memhdc );
119 ReleaseDC( NULL, hdc );
122 static void test_palette_entries(void)
124 char logpalettebuf[sizeof(LOGPALETTE) + sizeof(logpalettedata)];
125 PLOGPALETTE logpalette = (PLOGPALETTE)logpalettebuf;
126 HPALETTE hpal;
127 UINT res=0;
128 PALETTEENTRY palEntry = { 0x1, 0x2, 0x3, 0xff };
129 PALETTEENTRY getEntryResult;
131 /* Initialize the logical palette with a few colours */
132 logpalette->palVersion = 0x300;
133 logpalette->palNumEntries = 8;
134 memcpy( logpalette->palPalEntry, logpalettedata, sizeof(logpalettedata) );
135 hpal = CreatePalette( logpalette );
137 /* Set a new entry with peFlags to 0xff */
138 SetPaletteEntries(hpal, 0, 1, &palEntry);
140 /* Retrieve the entry to see if GDI32 performs any filtering on peFlags */
141 res = GetPaletteEntries(hpal, 0, 1, &getEntryResult);
142 ok(res == 1, "GetPaletteEntries should have returned 1 but returned %d\n", res);
144 ok( palEntry.peFlags == getEntryResult.peFlags, "palEntry.peFlags (%#x) != getEntryResult.peFlags (%#x)\n", palEntry.peFlags, getEntryResult.peFlags );
146 /* Try setting the system palette */
147 hpal = GetStockObject(DEFAULT_PALETTE);
148 res = SetPaletteEntries(hpal, 0, 1, &palEntry);
149 ok(!res, "SetPaletteEntries() should have failed\n");
151 res = GetPaletteEntries(hpal, 0, 1, &getEntryResult);
152 ok(res == 1, "GetPaletteEntries should have returned 1 but returned %d\n", res);
153 ok(memcmp(&palEntry, &getEntryResult, sizeof(PALETTEENTRY)), "entries should not match\n");
156 static void test_halftone_palette(void)
158 HDC hdc;
159 HPALETTE pal;
160 PALETTEENTRY entries[256];
161 PALETTEENTRY defpal[20];
162 int i, count;
164 hdc = GetDC(0);
166 count = GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, 20, defpal );
167 ok( count == 20, "wrong size %u\n", count );
169 pal = CreateHalftonePalette( hdc );
170 count = GetPaletteEntries( pal, 0, 256, entries );
171 ok( count == 256 || broken(count <= 20), /* nt 4 */
172 "wrong size %u\n", count );
174 /* first and last 8 match the default palette */
175 if (count >= 20)
177 for (i = 0; i < 8; i++)
179 ok( entries[i].peRed == defpal[i].peRed &&
180 entries[i].peGreen == defpal[i].peGreen &&
181 entries[i].peBlue == defpal[i].peBlue &&
182 !entries[i].peFlags,
183 "%u: wrong color %02x,%02x,%02x,%02x instead of %02x,%02x,%02x\n", i,
184 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags,
185 defpal[i].peRed, defpal[i].peGreen, defpal[i].peBlue );
187 for (i = count - 8; i < count; i++)
189 int idx = i - count + 20;
190 ok( entries[i].peRed == defpal[idx].peRed &&
191 entries[i].peGreen == defpal[idx].peGreen &&
192 entries[i].peBlue == defpal[idx].peBlue &&
193 !entries[i].peFlags,
194 "%u: wrong color %02x,%02x,%02x,%02x instead of %02x,%02x,%02x\n", i,
195 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags,
196 defpal[idx].peRed, defpal[idx].peGreen, defpal[idx].peBlue );
199 DeleteObject( pal );
200 ReleaseDC( 0, hdc );
203 static void check_system_palette_entries(HDC hdc)
205 PALETTEENTRY entries[256];
206 PALETTEENTRY defpal[20];
207 int i, count;
209 memset( defpal, 0xaa, sizeof(defpal) );
210 count = GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, 20, defpal );
211 ok( count == 20, "wrong size %u\n", count );
213 memset( entries, 0x55, sizeof(entries) );
214 count = GetSystemPaletteEntries( hdc, 0, 256, entries );
215 ok( count == 0, "wrong size %u\n", count);
216 for (i = 0; i < 10; i++)
218 ok( entries[i].peRed == defpal[i].peRed &&
219 entries[i].peGreen == defpal[i].peGreen &&
220 entries[i].peBlue == defpal[i].peBlue &&
221 !entries[i].peFlags,
222 "%u: wrong color %02x,%02x,%02x,%02x instead of %02x,%02x,%02x\n", i,
223 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags,
224 defpal[i].peRed, defpal[i].peGreen, defpal[i].peBlue );
226 for (i = 10; i < 246; ++i)
228 ok( !entries[i].peRed &&
229 !entries[i].peGreen &&
230 !entries[i].peBlue &&
231 !entries[i].peFlags,
232 "%u: wrong color %02x,%02x,%02x,%02x instead of 0,0,0\n", i,
233 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags);
235 for (i = 246; i < 256; i++)
237 int idx = i - 246 + 10;
238 ok( entries[i].peRed == defpal[idx].peRed &&
239 entries[i].peGreen == defpal[idx].peGreen &&
240 entries[i].peBlue == defpal[idx].peBlue &&
241 !entries[i].peFlags,
242 "%u: wrong color %02x,%02x,%02x,%02x instead of %02x,%02x,%02x\n", i,
243 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags,
244 defpal[idx].peRed, defpal[idx].peGreen, defpal[idx].peBlue );
247 memset( entries, 0x55, sizeof(entries) );
248 count = GetSystemPaletteEntries( hdc, 0, 10, entries );
249 ok( count == 0, "wrong size %u\n", count);
250 for (i = 0; i < 10; i++)
252 ok( entries[i].peRed == defpal[i].peRed &&
253 entries[i].peGreen == defpal[i].peGreen &&
254 entries[i].peBlue == defpal[i].peBlue &&
255 !entries[i].peFlags,
256 "%u: wrong color %02x,%02x,%02x,%02x instead of %02x,%02x,%02x\n", i,
257 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags,
258 defpal[i].peRed, defpal[i].peGreen, defpal[i].peBlue );
261 memset( entries, 0x55, sizeof(entries) );
262 count = GetSystemPaletteEntries( hdc, 10, 246, entries );
263 ok( count == 0, "wrong size %u\n", count);
264 for (i = 0; i < 236; ++i)
266 ok( !entries[i].peRed &&
267 !entries[i].peGreen &&
268 !entries[i].peBlue &&
269 !entries[i].peFlags,
270 "%u: wrong color %02x,%02x,%02x,%02x instead of 0,0,0\n", i,
271 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags);
273 for (i = 236; i < 246; i++)
275 int idx = i - 236 + 10;
276 ok( entries[i].peRed == defpal[idx].peRed &&
277 entries[i].peGreen == defpal[idx].peGreen &&
278 entries[i].peBlue == defpal[idx].peBlue &&
279 !entries[i].peFlags,
280 "%u: wrong color %02x,%02x,%02x,%02x instead of %02x,%02x,%02x\n", i,
281 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags,
282 defpal[idx].peRed, defpal[idx].peGreen, defpal[idx].peBlue );
285 memset( entries, 0x55, sizeof(entries) );
286 count = GetSystemPaletteEntries( hdc, 246, 10, entries );
287 ok( count == 0, "wrong size %u\n", count);
288 for (i = 0; i < 10; i++)
290 int idx = i + 10;
291 ok( entries[i].peRed == defpal[idx].peRed &&
292 entries[i].peGreen == defpal[idx].peGreen &&
293 entries[i].peBlue == defpal[idx].peBlue &&
294 !entries[i].peFlags,
295 "%u: wrong color %02x,%02x,%02x,%02x instead of %02x,%02x,%02x\n", i,
296 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags,
297 defpal[idx].peRed, defpal[idx].peGreen, defpal[idx].peBlue );
301 static void test_system_palette_entries(void)
303 HDC hdc;
304 HDC metafile_dc;
305 HMETAFILE metafile;
307 hdc = GetDC(0);
309 if (!(GetDeviceCaps( hdc, RASTERCAPS ) & RC_PALETTE))
311 check_system_palette_entries(hdc);
313 else
315 skip( "device is palette-based, skipping test\n" );
318 ReleaseDC( 0, hdc );
320 metafile_dc = CreateMetaFileA(NULL);
322 check_system_palette_entries(metafile_dc);
324 metafile = CloseMetaFile(metafile_dc);
325 DeleteMetaFile(metafile);
328 START_TEST(palette)
330 test_DIB_PAL_COLORS();
331 test_palette_entries();
332 test_halftone_palette();
333 test_system_palette_entries();