1 /* 2 * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include "awt.h" 27 #include "awt_Palette.h" 28 #include "awt_Component.h" 29 #include "img_util_md.h" 30 #include "awt_CustomPaletteDef.h" 31 #include "Trace.h" 32 33 BOOL AwtPalette::m_useCustomPalette = TRUE; 34 35 #define ERROR_GRAY (-1) 36 #define NON_GRAY 0 37 #define LINEAR_STATIC_GRAY 1 38 #define NON_LINEAR_STATIC_GRAY 2 39 40 /** 41 * Select the palette into the given HDC. This will 42 * allow operations using this HDC to access the palette 43 * colors/indices. 44 */ 45 HPALETTE AwtPalette::Select(HDC hDC) 46 { 47 HPALETTE prevPalette = NULL; 48 if (logicalPalette) { 49 BOOL background = !(m_useCustomPalette); 50 prevPalette = ::SelectPalette(hDC, logicalPalette, background); 51 } 52 return prevPalette; 53 } 54 55 /** 56 * Realize the palette of the given HDC. This will attempt to 57 * install the palette of the HDC onto the device associated with 58 * that HDC. 59 */ 60 void AwtPalette::Realize(HDC hDC) 61 { 62 if (logicalPalette) { 63 if (!m_useCustomPalette || 64 AwtComponent::QueryNewPaletteCalled() || 65 AwtToolkit::GetInstance().HasDisplayChanged()) { 66 // Fix for bug 4178909, workaround for Windows bug. Shouldn't 67 // do a RealizePalette until the first QueryNewPalette message 68 // has been processed. 69 // But if we are switching the primary monitor from non-8bpp 70 // to 8bpp mode, we may not get any palette messages during 71 // the display change event. Go ahead and realize the palette 72 // now anyway in this situation. This was especially noticeable 73 // on win2k in multimon. Note that there still seems to be some 74 // problem with actually setting the palette on the primary 75 // screen until after QNP is called, but at least the 76 // secondary devices can correctly realize the palette. 77 ::RealizePalette(hDC); 78 } 79 } 80 } 81 82 /** 83 * Disable the use of our custom palette. This method is called 84 * during initialization if we detect that we are running inside 85 * the plugin; we do not want to clobber our parent application's 86 * palette with our own in that situation. 87 */ 88 void AwtPalette::DisableCustomPalette() 89 { 90 m_useCustomPalette = FALSE; 91 } 92 93 /** 94 * Returns whether we are currently using a custom palette. Used 95 * by AwtWin32GraphicsDevice when creating the colorModel of the 96 * device. 97 */ 98 BOOL AwtPalette::UseCustomPalette() 99 { 100 return m_useCustomPalette; 101 } 102 103 104 /** 105 * Constructor. Initialize the system and logical palettes. 106 * used by this object. 107 */ 108 AwtPalette::AwtPalette(AwtWin32GraphicsDevice *device) 109 { 110 this->device = device; 111 Update(); 112 UpdateLogical(); 113 } 114 115 /** 116 * Retrieves system palette entries. Includes a workaround for some 117 * video drivers which may not support the GSPE call but may return 118 * valid values from this procedure. 119 */ 120 int AwtPalette::FetchPaletteEntries(HDC hDC, PALETTEENTRY* pPalEntries) 121 { 122 LOGPALETTE* pLogPal = 0; 123 HPALETTE hPal = 0; 124 HPALETTE hPalOld = 0; 136 if (pLogPal == NULL) { 137 return 0; 138 } 139 140 pLogPal->palVersion = 0x300; 141 pLogPal->palNumEntries = 256; 142 int iEntry; 143 PALETTEENTRY* pEntry; 144 for (iEntry = 0; iEntry < 256; iEntry++) { 145 pEntry = pLogPal->palPalEntry + iEntry; 146 pEntry->peRed = iEntry; 147 pEntry->peGreen = pEntry->peBlue = 0; 148 pEntry->peFlags = PC_EXPLICIT; 149 } 150 hPal = ::CreatePalette(pLogPal); 151 delete[] pLogPal; 152 if ( hPal == 0 ) { 153 return 0; 154 } 155 156 hPalOld = ::SelectPalette(hDC, hPal, 1); 157 if (hPalOld == 0) { 158 ::DeleteObject(hPal); 159 return 0; 160 } 161 ::RealizePalette(hDC); 162 163 COLORREF rgb; 164 for (iEntry = 0; iEntry < 256; iEntry++) { 165 rgb = ::GetNearestColor(hDC, PALETTEINDEX(iEntry)); 166 pPalEntries[iEntry].peRed = GetRValue(rgb); 167 pPalEntries[iEntry].peGreen = GetGValue(rgb); 168 pPalEntries[iEntry].peBlue = GetBValue(rgb); 169 } 170 171 ::SelectPalette(hDC, hPalOld, 0 ); 172 ::DeleteObject(hPal); 173 ::RealizePalette(hDC); 174 175 return 256; 176 } 177 178 int AwtPalette::GetGSType(PALETTEENTRY* pPalEntries) 179 { 180 int isGray = 1; 181 int isLinearStaticGray = 1; 182 int isNonLinearStaticGray = 1; 183 int iEntry; 184 char bUsed[256]; 185 BYTE r, g, b; 186 187 memset(bUsed, 0, sizeof(bUsed)); 188 for (iEntry = 0; iEntry < 256; iEntry++) { 189 r = pPalEntries[iEntry].peRed; 190 g = pPalEntries[iEntry].peGreen; 191 b = pPalEntries[iEntry].peBlue; | 1 /* 2 * Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include "awt.h" 27 #include "awt_Palette.h" 28 #include "awt_Component.h" 29 #include "img_util_md.h" 30 #include "awt_CustomPaletteDef.h" 31 #include "Trace.h" 32 33 #define ERROR_GRAY (-1) 34 #define NON_GRAY 0 35 #define LINEAR_STATIC_GRAY 1 36 #define NON_LINEAR_STATIC_GRAY 2 37 38 /** 39 * Select the palette into the given HDC. This will 40 * allow operations using this HDC to access the palette 41 * colors/indices. 42 */ 43 HPALETTE AwtPalette::Select(HDC hDC) 44 { 45 HPALETTE prevPalette = NULL; 46 if (logicalPalette) { 47 prevPalette = ::SelectPalette(hDC, logicalPalette, FALSE); 48 } 49 return prevPalette; 50 } 51 52 /** 53 * Realize the palette of the given HDC. This will attempt to 54 * install the palette of the HDC onto the device associated with 55 * that HDC. 56 */ 57 void AwtPalette::Realize(HDC hDC) 58 { 59 if (logicalPalette) { 60 if (AwtComponent::QueryNewPaletteCalled() || 61 AwtToolkit::GetInstance().HasDisplayChanged()) { 62 // Fix for bug 4178909, workaround for Windows bug. Shouldn't 63 // do a RealizePalette until the first QueryNewPalette message 64 // has been processed. 65 // But if we are switching the primary monitor from non-8bpp 66 // to 8bpp mode, we may not get any palette messages during 67 // the display change event. Go ahead and realize the palette 68 // now anyway in this situation. This was especially noticeable 69 // on win2k in multimon. Note that there still seems to be some 70 // problem with actually setting the palette on the primary 71 // screen until after QNP is called, but at least the 72 // secondary devices can correctly realize the palette. 73 ::RealizePalette(hDC); 74 } 75 } 76 } 77 78 /** 79 * Constructor. Initialize the system and logical palettes. 80 * used by this object. 81 */ 82 AwtPalette::AwtPalette(AwtWin32GraphicsDevice *device) 83 { 84 this->device = device; 85 Update(); 86 UpdateLogical(); 87 } 88 89 /** 90 * Retrieves system palette entries. Includes a workaround for some 91 * video drivers which may not support the GSPE call but may return 92 * valid values from this procedure. 93 */ 94 int AwtPalette::FetchPaletteEntries(HDC hDC, PALETTEENTRY* pPalEntries) 95 { 96 LOGPALETTE* pLogPal = 0; 97 HPALETTE hPal = 0; 98 HPALETTE hPalOld = 0; 110 if (pLogPal == NULL) { 111 return 0; 112 } 113 114 pLogPal->palVersion = 0x300; 115 pLogPal->palNumEntries = 256; 116 int iEntry; 117 PALETTEENTRY* pEntry; 118 for (iEntry = 0; iEntry < 256; iEntry++) { 119 pEntry = pLogPal->palPalEntry + iEntry; 120 pEntry->peRed = iEntry; 121 pEntry->peGreen = pEntry->peBlue = 0; 122 pEntry->peFlags = PC_EXPLICIT; 123 } 124 hPal = ::CreatePalette(pLogPal); 125 delete[] pLogPal; 126 if ( hPal == 0 ) { 127 return 0; 128 } 129 130 hPalOld = ::SelectPalette(hDC, hPal, TRUE); 131 if (hPalOld == 0) { 132 ::DeleteObject(hPal); 133 return 0; 134 } 135 ::RealizePalette(hDC); 136 137 COLORREF rgb; 138 for (iEntry = 0; iEntry < 256; iEntry++) { 139 rgb = ::GetNearestColor(hDC, PALETTEINDEX(iEntry)); 140 pPalEntries[iEntry].peRed = GetRValue(rgb); 141 pPalEntries[iEntry].peGreen = GetGValue(rgb); 142 pPalEntries[iEntry].peBlue = GetBValue(rgb); 143 } 144 145 ::SelectPalette(hDC, hPalOld, FALSE); 146 ::DeleteObject(hPal); 147 ::RealizePalette(hDC); 148 149 return 256; 150 } 151 152 int AwtPalette::GetGSType(PALETTEENTRY* pPalEntries) 153 { 154 int isGray = 1; 155 int isLinearStaticGray = 1; 156 int isNonLinearStaticGray = 1; 157 int iEntry; 158 char bUsed[256]; 159 BYTE r, g, b; 160 161 memset(bUsed, 0, sizeof(bUsed)); 162 for (iEntry = 0; iEntry < 256; iEntry++) { 163 r = pPalEntries[iEntry].peRed; 164 g = pPalEntries[iEntry].peGreen; 165 b = pPalEntries[iEntry].peBlue; |