gdi32: Use the default DIB color table to create system and halftone palettes.
[wine/multimedia.git] / dlls / gdi32 / tests / palette.c
blob4f14d7a1905ea6063fc37e6d50972d7d3b55874d
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 a 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 );
147 static void test_halftone_palette(void)
149 HDC hdc;
150 HPALETTE pal;
151 PALETTEENTRY entries[256];
152 PALETTEENTRY defpal[20];
153 int i, count;
155 hdc = GetDC(0);
157 count = GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, 20, defpal );
158 ok( count == 20, "wrong size %u\n", count );
160 pal = CreateHalftonePalette( hdc );
161 count = GetPaletteEntries( pal, 0, 256, entries );
162 ok( count == 256 || broken(count == 20), /* nt 4 */
163 "wrong size %u\n", count );
165 /* first and last 10 match the default palette */
166 for (i = 0; i < 10; i++)
167 ok( entries[i].peRed == defpal[i].peRed &&
168 entries[i].peGreen == defpal[i].peGreen &&
169 entries[i].peBlue == defpal[i].peBlue &&
170 !entries[i].peFlags,
171 "%u: wrong color %02x,%02x,%02x,%02x\n", i,
172 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags );
173 for (i = count - 10; i < count; i++)
174 ok( entries[i].peRed == defpal[i - count + 20].peRed &&
175 entries[i].peGreen == defpal[i - count + 20].peGreen &&
176 entries[i].peBlue == defpal[i - count + 20].peBlue &&
177 !entries[i].peFlags,
178 "%u: wrong color %02x,%02x,%02x,%02x\n", i,
179 entries[i].peRed, entries[i].peGreen, entries[i].peBlue, entries[i].peFlags );
181 DeleteObject( pal );
182 ReleaseDC( 0, hdc );
185 START_TEST(palette)
187 test_DIB_PAL_COLORS();
188 test_palette_entries();
189 test_halftone_palette();