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