palette.c revision 1.2 1 1.2 takemura /* $NetBSD: palette.c,v 1.2 2000/03/20 10:47:35 takemura Exp $ */
2 1.1 takemura
3 1.1 takemura /*-
4 1.1 takemura * Copyright (c) 1999 Shin Takemura.
5 1.1 takemura * All rights reserved.
6 1.1 takemura *
7 1.1 takemura * This software is part of the PocketBSD.
8 1.1 takemura *
9 1.1 takemura * Redistribution and use in source and binary forms, with or without
10 1.1 takemura * modification, are permitted provided that the following conditions
11 1.1 takemura * are met:
12 1.1 takemura * 1. Redistributions of source code must retain the above copyright
13 1.1 takemura * notice, this list of conditions and the following disclaimer.
14 1.1 takemura * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 takemura * notice, this list of conditions and the following disclaimer in the
16 1.1 takemura * documentation and/or other materials provided with the distribution.
17 1.1 takemura * 3. All advertising materials mentioning features or use of this software
18 1.1 takemura * must display the following acknowledgement:
19 1.1 takemura * This product includes software developed by the PocketBSD project
20 1.1 takemura * and its contributors.
21 1.1 takemura * 4. Neither the name of the project nor the names of its contributors
22 1.1 takemura * may be used to endorse or promote products derived from this software
23 1.1 takemura * without specific prior written permission.
24 1.1 takemura *
25 1.1 takemura * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 takemura * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 takemura * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 takemura * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 takemura * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 takemura * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 takemura * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 takemura * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 takemura * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 takemura * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 takemura * SUCH DAMAGE.
36 1.1 takemura *
37 1.1 takemura */
38 1.1 takemura #include <pbsdboot.h>
39 1.1 takemura
40 1.2 takemura #include <arch/hpcmips/dev/hpccmapvar.h>
41 1.2 takemura
42 1.1 takemura /*
43 1.1 takemura * If Windows have no color palette, we have nothing to do here.
44 1.1 takemura */
45 1.1 takemura #if ( _WIN32_WCE >= 200 ) // WIN CE Ver 2.00 or greater
46 1.1 takemura #define HAVE_COLOR_PALETTE
47 1.1 takemura #endif
48 1.1 takemura
49 1.1 takemura #define ASSERT(a) \
50 1.1 takemura if (!(a)) { \
51 1.1 takemura msg_printf(MSG_ERROR, TEXT("assertion failure"), \
52 1.1 takemura TEXT(#a)); \
53 1.1 takemura }
54 1.1 takemura
55 1.1 takemura #ifdef HAVE_COLOR_PALETTE
56 1.1 takemura enum palette_status palette_succeeded = PAL_ERROR;
57 1.1 takemura static void palette_setup(PALETTEENTRY *pal);
58 1.1 takemura #else
59 1.1 takemura enum palette_status palette_succeeded = PAL_NOERROR;
60 1.1 takemura #endif
61 1.1 takemura
62 1.1 takemura static HPALETTE pal = NULL;
63 1.1 takemura
64 1.1 takemura void
65 1.1 takemura palette_init(HWND hWnd)
66 1.1 takemura {
67 1.1 takemura #ifdef HAVE_COLOR_PALETTE
68 1.1 takemura HDC hdc;
69 1.1 takemura PAINTSTRUCT ps;
70 1.1 takemura LOGPALETTE* logpal;
71 1.1 takemura
72 1.1 takemura debug_printf(TEXT("*** palette init ***\n"));
73 1.1 takemura
74 1.1 takemura hdc = BeginPaint(hWnd, &ps);
75 1.1 takemura if (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASDISPLAY &&
76 1.1 takemura (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) &&
77 1.1 takemura GetDeviceCaps(hdc, COLORRES) == 8) {
78 1.1 takemura ASSERT(GetDeviceCaps(hdc, BITSPIXEL) == 8);
79 1.1 takemura ASSERT(GetDeviceCaps(hdc, PLANES) == 1);
80 1.1 takemura /*
81 1.1 takemura * create logical palette
82 1.1 takemura */
83 1.1 takemura logpal = LocalAlloc (LPTR,
84 1.1 takemura (sizeof(LOGPALETTE) +
85 1.1 takemura sizeof(PALETTEENTRY) * 256));
86 1.1 takemura logpal->palVersion = 0x300;
87 1.1 takemura logpal->palNumEntries = 256;
88 1.1 takemura palette_setup(logpal->palPalEntry);
89 1.1 takemura
90 1.1 takemura pal = CreatePalette(logpal);
91 1.1 takemura if (pal == NULL) {
92 1.1 takemura debug_printf(TEXT("CreatePalette() failed"));
93 1.1 takemura msg_printf(MSG_ERROR, TEXT("Error"),
94 1.1 takemura TEXT("CreatePalette() failed: %d"),
95 1.1 takemura GetLastError());
96 1.1 takemura }
97 1.1 takemura LocalFree((HLOCAL)logpal);
98 1.1 takemura } else {
99 1.1 takemura /*
100 1.1 takemura * The screen is not 8 bit depth nor indexed color.
101 1.1 takemura */
102 1.1 takemura palette_succeeded = PAL_NOERROR;
103 1.1 takemura }
104 1.1 takemura EndPaint(hWnd, &ps);
105 1.1 takemura #endif /* HAVE_COLOR_PALETTE */
106 1.1 takemura }
107 1.1 takemura
108 1.1 takemura #ifdef HAVE_COLOR_PALETTE
109 1.1 takemura static void
110 1.1 takemura palette_setup(PALETTEENTRY *pal)
111 1.1 takemura {
112 1.2 takemura int i;
113 1.1 takemura
114 1.2 takemura for (i = 0; i < 256; i++) {
115 1.1 takemura pal[i].peFlags = PC_EXPLICIT;
116 1.2 takemura pal[i].peRed = bivideo_cmap_r[i];
117 1.2 takemura pal[i].peGreen = bivideo_cmap_g[i];
118 1.2 takemura pal[i].peBlue = bivideo_cmap_b[i];
119 1.1 takemura }
120 1.1 takemura }
121 1.1 takemura #endif /* HAVE_COLOR_PALETTE */
122 1.1 takemura
123 1.1 takemura void
124 1.1 takemura palette_set(HWND hWnd)
125 1.1 takemura {
126 1.1 takemura #ifdef HAVE_COLOR_PALETTE
127 1.1 takemura int n;
128 1.1 takemura HDC hdc;
129 1.1 takemura PAINTSTRUCT ps;
130 1.1 takemura HPALETTE prev_pal;
131 1.1 takemura
132 1.1 takemura debug_printf(TEXT("*** palette set ***\n"));
133 1.1 takemura
134 1.1 takemura if (pal != NULL) {
135 1.1 takemura hdc = BeginPaint(hWnd, &ps);
136 1.1 takemura prev_pal = SelectPalette(hdc, pal, FALSE);
137 1.1 takemura if (prev_pal == NULL) {
138 1.1 takemura debug_printf(TEXT("SelectPalette() failed"));
139 1.1 takemura msg_printf(MSG_ERROR, TEXT("Error"),
140 1.1 takemura TEXT("SelectPalette() failed: %d"),
141 1.1 takemura GetLastError());
142 1.1 takemura } else {
143 1.1 takemura n = RealizePalette(hdc);
144 1.1 takemura debug_printf(TEXT("RealizePalette() = %d\n"), n);
145 1.1 takemura if (n == GDI_ERROR) {
146 1.1 takemura debug_printf(TEXT("RealizePalette() failed"));
147 1.1 takemura msg_printf(MSG_ERROR, TEXT("Error"),
148 1.1 takemura TEXT("RealizePalette() failed: %d"),
149 1.1 takemura GetLastError());
150 1.1 takemura }
151 1.1 takemura }
152 1.1 takemura EndPaint(hWnd, &ps);
153 1.1 takemura }
154 1.1 takemura #endif /* HAVE_COLOR_PALETTE */
155 1.1 takemura }
156 1.1 takemura
157 1.1 takemura void
158 1.1 takemura palette_check(HWND hWnd)
159 1.1 takemura {
160 1.1 takemura #ifdef HAVE_COLOR_PALETTE
161 1.1 takemura HDC hdc;
162 1.1 takemura PAINTSTRUCT ps;
163 1.1 takemura PALETTEENTRY syspal[256];
164 1.1 takemura PALETTEENTRY canonpal[256];
165 1.1 takemura int i, n, error;
166 1.1 takemura
167 1.1 takemura debug_printf(TEXT("*** palette check ***\n"));
168 1.1 takemura
169 1.1 takemura error = 0;
170 1.1 takemura if (pal != NULL) {
171 1.1 takemura hdc = BeginPaint(hWnd, &ps);
172 1.1 takemura
173 1.1 takemura palette_setup(canonpal);
174 1.1 takemura
175 1.1 takemura n = GetSystemPaletteEntries(hdc, 0, 256, syspal);
176 1.1 takemura if (n == 0) {
177 1.1 takemura msg_printf(MSG_ERROR, TEXT("Error"),
178 1.1 takemura TEXT("GetSystemPaletteEntries() failed: %d"),
179 1.1 takemura GetLastError());
180 1.1 takemura error++;
181 1.1 takemura } else {
182 1.1 takemura for (i = 0; i < n; i++) {
183 1.1 takemura debug_printf(TEXT("%3d: %02x %02x %02x"),
184 1.1 takemura i,
185 1.1 takemura syspal[i].peRed,
186 1.1 takemura syspal[i].peGreen,
187 1.1 takemura syspal[i].peBlue);
188 1.1 takemura if (syspal[i].peRed != canonpal[i].peRed ||
189 1.1 takemura syspal[i].peGreen != canonpal[i].peGreen ||
190 1.1 takemura syspal[i].peBlue != canonpal[i].peBlue ) {
191 1.1 takemura debug_printf(TEXT(" *"));
192 1.1 takemura error++;
193 1.1 takemura }
194 1.1 takemura debug_printf(TEXT("\n"));
195 1.1 takemura }
196 1.1 takemura }
197 1.1 takemura EndPaint(hWnd, &ps);
198 1.1 takemura if (error) {
199 1.1 takemura palette_succeeded = PAL_ERROR;
200 1.1 takemura } else {
201 1.1 takemura palette_succeeded = PAL_SUCCEEDED;
202 1.1 takemura }
203 1.1 takemura }
204 1.1 takemura #endif /* HAVE_COLOR_PALETTE */
205 1.1 takemura }
206