Home | History | Annotate | Line # | Download | only in pbsdboot
palette.c revision 1.1
      1  1.1  takemura /*	$NetBSD: palette.c,v 1.1 2000/03/19 11:10:58 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.1  takemura /*
     41  1.1  takemura  * If Windows have no color palette, we have nothing to do here.
     42  1.1  takemura  */
     43  1.1  takemura #if ( _WIN32_WCE >= 200 ) // WIN CE Ver 2.00 or greater
     44  1.1  takemura #define HAVE_COLOR_PALETTE
     45  1.1  takemura #endif
     46  1.1  takemura 
     47  1.1  takemura #define ASSERT(a) \
     48  1.1  takemura 	if (!(a)) { \
     49  1.1  takemura 		msg_printf(MSG_ERROR, TEXT("assertion failure"), \
     50  1.1  takemura 			TEXT(#a)); \
     51  1.1  takemura 	}
     52  1.1  takemura 
     53  1.1  takemura #ifdef HAVE_COLOR_PALETTE
     54  1.1  takemura enum palette_status palette_succeeded = PAL_ERROR;
     55  1.1  takemura static void palette_setup(PALETTEENTRY *pal);
     56  1.1  takemura #else
     57  1.1  takemura enum palette_status palette_succeeded = PAL_NOERROR;
     58  1.1  takemura #endif
     59  1.1  takemura 
     60  1.1  takemura static u_char compo6[6] = {   0,  51, 102, 153, 204, 255 };
     61  1.1  takemura static u_char compo7[7] = {   0,  42,  85, 127, 170, 212, 255 };
     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.1  takemura 	int i, r, g, b;
    113  1.1  takemura 
    114  1.1  takemura 	/*
    115  1.1  takemura 	 * 0 - 31, gray scale
    116  1.1  takemura 	 */
    117  1.1  takemura 	for (i = 0; i < 32; i++) {
    118  1.1  takemura 		pal[i].peFlags = PC_EXPLICIT;
    119  1.1  takemura 		pal[i].peRed = i * 8;
    120  1.1  takemura 		pal[i].peGreen = i * 8;
    121  1.1  takemura 		pal[i].peBlue = i * 8;
    122  1.1  takemura 	}
    123  1.1  takemura 	/*
    124  1.1  takemura 	 * 32 - 247, RGB color
    125  1.1  takemura 	 */
    126  1.1  takemura 	for (r = 0; r < 6; r++) {
    127  1.1  takemura 		for (g = 0; g < 6; g++) {
    128  1.1  takemura 			for (b = 0; b < 6; b++) {
    129  1.1  takemura 				pal[i].peFlags = PC_EXPLICIT;
    130  1.1  takemura 				pal[i].peRed = compo6[r];
    131  1.1  takemura 				pal[i].peGreen = compo6[g];
    132  1.1  takemura 				pal[i].peBlue = compo6[b];
    133  1.1  takemura 				i++;
    134  1.1  takemura 			}
    135  1.1  takemura 		}
    136  1.1  takemura 	}
    137  1.1  takemura 	/*
    138  1.1  takemura 	 * 248 - 255, just white
    139  1.1  takemura 	 */
    140  1.1  takemura 	for ( ; i < 256; i++) {
    141  1.1  takemura 		pal[i].peFlags = PC_EXPLICIT;
    142  1.1  takemura 		pal[i].peRed = 255;
    143  1.1  takemura 		pal[i].peGreen = 255;
    144  1.1  takemura 		pal[i].peBlue = 255;
    145  1.1  takemura 	}
    146  1.1  takemura }
    147  1.1  takemura #endif /* HAVE_COLOR_PALETTE */
    148  1.1  takemura 
    149  1.1  takemura void
    150  1.1  takemura palette_set(HWND hWnd)
    151  1.1  takemura {
    152  1.1  takemura #ifdef HAVE_COLOR_PALETTE
    153  1.1  takemura 	int n;
    154  1.1  takemura 	HDC hdc;
    155  1.1  takemura 	PAINTSTRUCT ps;
    156  1.1  takemura 	HPALETTE prev_pal;
    157  1.1  takemura 
    158  1.1  takemura 	debug_printf(TEXT("*** palette set ***\n"));
    159  1.1  takemura 
    160  1.1  takemura 	if (pal != NULL) {
    161  1.1  takemura 		hdc = BeginPaint(hWnd, &ps);
    162  1.1  takemura 		prev_pal = SelectPalette(hdc, pal, FALSE);
    163  1.1  takemura 		if (prev_pal == NULL) {
    164  1.1  takemura 			debug_printf(TEXT("SelectPalette() failed"));
    165  1.1  takemura 			msg_printf(MSG_ERROR, TEXT("Error"),
    166  1.1  takemura 			    TEXT("SelectPalette() failed: %d"),
    167  1.1  takemura 			    GetLastError());
    168  1.1  takemura 		} else {
    169  1.1  takemura 			n = RealizePalette(hdc);
    170  1.1  takemura 			debug_printf(TEXT("RealizePalette() = %d\n"), n);
    171  1.1  takemura 			if (n == GDI_ERROR) {
    172  1.1  takemura 				debug_printf(TEXT("RealizePalette() failed"));
    173  1.1  takemura 				msg_printf(MSG_ERROR, TEXT("Error"),
    174  1.1  takemura 				    TEXT("RealizePalette() failed: %d"),
    175  1.1  takemura 				    GetLastError());
    176  1.1  takemura 			}
    177  1.1  takemura 		}
    178  1.1  takemura 		EndPaint(hWnd, &ps);
    179  1.1  takemura 	}
    180  1.1  takemura #endif /* HAVE_COLOR_PALETTE */
    181  1.1  takemura }
    182  1.1  takemura 
    183  1.1  takemura void
    184  1.1  takemura palette_check(HWND hWnd)
    185  1.1  takemura {
    186  1.1  takemura #ifdef HAVE_COLOR_PALETTE
    187  1.1  takemura 	HDC hdc;
    188  1.1  takemura 	PAINTSTRUCT ps;
    189  1.1  takemura 	PALETTEENTRY syspal[256];
    190  1.1  takemura 	PALETTEENTRY canonpal[256];
    191  1.1  takemura 	int i, n, error;
    192  1.1  takemura 
    193  1.1  takemura 	debug_printf(TEXT("*** palette check ***\n"));
    194  1.1  takemura 
    195  1.1  takemura 	error = 0;
    196  1.1  takemura 	if (pal != NULL) {
    197  1.1  takemura 		hdc = BeginPaint(hWnd, &ps);
    198  1.1  takemura 
    199  1.1  takemura 		palette_setup(canonpal);
    200  1.1  takemura 
    201  1.1  takemura 		n = GetSystemPaletteEntries(hdc, 0, 256, syspal);
    202  1.1  takemura 		if (n == 0) {
    203  1.1  takemura 			msg_printf(MSG_ERROR, TEXT("Error"),
    204  1.1  takemura 			    TEXT("GetSystemPaletteEntries() failed: %d"),
    205  1.1  takemura 			    GetLastError());
    206  1.1  takemura 			error++;
    207  1.1  takemura 		} else {
    208  1.1  takemura 			for (i = 0; i < n; i++) {
    209  1.1  takemura 				debug_printf(TEXT("%3d: %02x %02x %02x"),
    210  1.1  takemura 				    i,
    211  1.1  takemura 				    syspal[i].peRed,
    212  1.1  takemura 				    syspal[i].peGreen,
    213  1.1  takemura 				    syspal[i].peBlue);
    214  1.1  takemura 				if (syspal[i].peRed != canonpal[i].peRed ||
    215  1.1  takemura 				    syspal[i].peGreen != canonpal[i].peGreen ||
    216  1.1  takemura 				    syspal[i].peBlue != canonpal[i].peBlue ) {
    217  1.1  takemura 					debug_printf(TEXT(" *"));
    218  1.1  takemura 					error++;
    219  1.1  takemura 				}
    220  1.1  takemura 				debug_printf(TEXT("\n"));
    221  1.1  takemura 			}
    222  1.1  takemura 		}
    223  1.1  takemura 		EndPaint(hWnd, &ps);
    224  1.1  takemura 		if (error) {
    225  1.1  takemura 			palette_succeeded = PAL_ERROR;
    226  1.1  takemura 		} else {
    227  1.1  takemura 			palette_succeeded = PAL_SUCCEEDED;
    228  1.1  takemura 		}
    229  1.1  takemura 	}
    230  1.1  takemura #endif /* HAVE_COLOR_PALETTE */
    231  1.1  takemura }
    232