jdk/src/java.desktop/windows/native/libawt/windows/awt_Palette.cpp
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 26751 70bac69b37c9
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1954
diff changeset
     2
 * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1954
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1954
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1954
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1954
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1954
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
1954
b93b85df3211 6585765: RFE: Remove Unicows-related code from AWT
art
parents: 2
diff changeset
    26
#include "awt.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "awt_Palette.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "awt_Component.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "img_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "awt_CustomPaletteDef.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "Trace.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
BOOL AwtPalette::m_useCustomPalette = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#define ERROR_GRAY (-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#define NON_GRAY 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#define LINEAR_STATIC_GRAY 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#define NON_LINEAR_STATIC_GRAY 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Select the palette into the given HDC.  This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * allow operations using this HDC to access the palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * colors/indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
HPALETTE AwtPalette::Select(HDC hDC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    HPALETTE prevPalette = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    if (logicalPalette) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        BOOL background = !(m_useCustomPalette);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        prevPalette = ::SelectPalette(hDC, logicalPalette, background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    return prevPalette;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Realize the palette of the given HDC.  This will attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * install the palette of the HDC onto the device associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * that HDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
void AwtPalette::Realize(HDC hDC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    if (logicalPalette) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (!m_useCustomPalette ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            AwtComponent::QueryNewPaletteCalled() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            AwtToolkit::GetInstance().HasDisplayChanged()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            // Fix for bug 4178909, workaround for Windows bug.  Shouldn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            // do a RealizePalette until the first QueryNewPalette message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            // has been processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            // But if we are switching the primary monitor from non-8bpp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            // to 8bpp mode, we may not get any palette messages during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            // the display change event.  Go ahead and realize the palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            // now anyway in this situation.  This was especially noticeable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            // on win2k in multimon.  Note that there still seems to be some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            // problem with actually setting the palette on the primary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            // screen until after QNP is called, but at least the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            // secondary devices can correctly realize the palette.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            ::RealizePalette(hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Disable the use of our custom palette.  This method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * during initialization if we detect that we are running inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * the plugin; we do not want to clobber our parent application's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * palette with our own in that situation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
void AwtPalette::DisableCustomPalette()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    m_useCustomPalette = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Returns whether we are currently using a custom palette.  Used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * by AwtWin32GraphicsDevice when creating the colorModel of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
BOOL AwtPalette::UseCustomPalette()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    return m_useCustomPalette;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * Constructor.  Initialize the system and logical palettes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * used by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
AwtPalette::AwtPalette(AwtWin32GraphicsDevice *device)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    this->device = device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    Update();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    UpdateLogical();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
/**
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26751
diff changeset
   116
 * Retrieves system palette entries. Includes a workaround for some
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * video drivers which may not support the GSPE call but may return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * valid values from this procedure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
int AwtPalette::FetchPaletteEntries(HDC hDC, PALETTEENTRY* pPalEntries)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    LOGPALETTE* pLogPal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    HPALETTE hPal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    HPALETTE hPalOld = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    int numEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    numEntries = ::GetSystemPaletteEntries(hDC, 0, 256, pPalEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    if (numEntries > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return numEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // Workaround: some drivers do not support GetSysPalEntries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    pLogPal = (LOGPALETTE*) new char[sizeof(LOGPALETTE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                    + 256*sizeof(PALETTEENTRY)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    if (pLogPal == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    pLogPal->palVersion = 0x300;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    pLogPal->palNumEntries = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    int iEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    PALETTEENTRY* pEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    for (iEntry = 0; iEntry < 256; iEntry++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        pEntry = pLogPal->palPalEntry + iEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        pEntry->peRed = iEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        pEntry->peGreen = pEntry->peBlue = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        pEntry->peFlags = PC_EXPLICIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    hPal = ::CreatePalette(pLogPal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    delete pLogPal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    if ( hPal == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    hPalOld = ::SelectPalette(hDC, hPal, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    if (hPalOld == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        ::DeleteObject(hPal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    ::RealizePalette(hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    COLORREF rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    for (iEntry = 0; iEntry < 256; iEntry++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        rgb = ::GetNearestColor(hDC, PALETTEINDEX(iEntry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        pPalEntries[iEntry].peRed = GetRValue(rgb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        pPalEntries[iEntry].peGreen = GetGValue(rgb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        pPalEntries[iEntry].peBlue = GetBValue(rgb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    ::SelectPalette(hDC, hPalOld, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    ::DeleteObject(hPal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    ::RealizePalette(hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    return 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
int AwtPalette::GetGSType(PALETTEENTRY* pPalEntries)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    int isGray = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    int isLinearStaticGray = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    int isNonLinearStaticGray = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    int iEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    char bUsed[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    BYTE r, g, b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    memset(bUsed, 0, sizeof(bUsed));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    for (iEntry = 0; iEntry < 256; iEntry++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        r = pPalEntries[iEntry].peRed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        g = pPalEntries[iEntry].peGreen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        b = pPalEntries[iEntry].peBlue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (r != g || r != b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            isGray = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // the values are gray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (r != iEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                // it's not linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                // but it could be non-linear static gray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                isLinearStaticGray = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            bUsed[r] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    if (isGray && !isLinearStaticGray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // check if all 256 grays are there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // if that's the case, it's non-linear static gray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        for (iEntry = 0; iEntry < 256; iEntry++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (!bUsed[iEntry]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                // not non-linear (not all 256 colors are used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                isNonLinearStaticGray = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    if (!isGray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        J2dTraceLn(J2D_TRACE_INFO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                   "Detected palette: NON_GRAY/USER-MODIFIABLE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return NON_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    if (isLinearStaticGray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        J2dTraceLn(J2D_TRACE_INFO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                   "Detected palette: LINEAR_STATIC_GRAY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return LINEAR_STATIC_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    if (isNonLinearStaticGray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        J2dTraceLn(J2D_TRACE_INFO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                   "Detected palette: NON_LINEAR_STATIC_GRAY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return NON_LINEAR_STATIC_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    J2dTraceLn(J2D_TRACE_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
               "Unable to detect palette type, non-gray is assumed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // not supposed to be here, error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    return ERROR_GRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * Updates our system palette variables to make sure they match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * the current state of the actual system palette.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * is called during AwtPalette creation and after palette changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * Return whether there were any palette changes from the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * system palette.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
BOOL AwtPalette::Update()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    PALETTEENTRY pe[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    int numEntries = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    int bitsPerPixel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    HDC hDC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    hDC = device->GetDC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    if (!hDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    bitsPerPixel = ::GetDeviceCaps(hDC, BITSPIXEL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    device->ReleaseDC(hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    if (8 != bitsPerPixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    hDC = device->GetDC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    numEntries = FetchPaletteEntries(hDC, pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    device->ReleaseDC(hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    if ((numEntries == numSystemEntries) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        (0 == memcmp(pe, systemEntriesWin32, numEntries * sizeof(PALETTEENTRY))))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    // make this system palette the new cached win32 palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    numEntries = (numEntries > 256)? 256: numEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    memcpy(systemEntriesWin32, pe, numEntries * sizeof(PALETTEENTRY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    numSystemEntries = numEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    // Create jdk-style system palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    int startIndex = 0, endIndex = numEntries-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    int staticGrayType = GetGSType(systemEntriesWin32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    if (staticGrayType == LINEAR_STATIC_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        device->SetGrayness(GS_STATICGRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    } else if (staticGrayType == NON_LINEAR_STATIC_GRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        device->SetGrayness(GS_NONLINGRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    } else if (getenv("FORCEGRAY")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        J2dTraceLn(J2D_TRACE_INFO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    "Gray Palette Forced via FORCEGRAY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // Need to zero first and last ten
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // palette entries. Otherwise in UpdateDynamicColorModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // we could set non-gray values to the palette.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        for (i = 0; i < 10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            systemEntries[i] = 0x00000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            systemEntries[i+246] = 0x00000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        numEntries -= 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        startIndex = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        endIndex -= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        device->SetGrayness(GS_INDEXGRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        device->SetGrayness(GS_NOTGRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    for (i = startIndex; i <= endIndex; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        systemEntries[i] =  0xff000000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        | (pe[i].peRed << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        | (pe[i].peGreen << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        | (pe[i].peBlue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    systemInverseLUT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        initCubemap((int *)systemEntries, numEntries, 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    ColorData *cData = device->GetColorData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    if ((device->GetGrayness() == GS_NONLINGRAY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         device->GetGrayness() == GS_INDEXGRAY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        cData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (cData->pGrayInverseLutData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            free(cData->pGrayInverseLutData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            cData->pGrayInverseLutData = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        initInverseGrayLut((int*)systemEntries, 256, device->GetColorData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 * Creates our custom palette based on: the current system palette,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 * the grayscale-ness of the system palette, and the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
 * primary device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
void AwtPalette::UpdateLogical()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    // Create and initialize a palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    int nEntries = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    char *buf = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    buf = new char[sizeof(LOGPALETTE) + nEntries *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        sizeof(PALETTEENTRY)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    LOGPALETTE *pLogPal = (LOGPALETTE*)buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    PALETTEENTRY *pPalEntries = (PALETTEENTRY *)(&(pLogPal->palPalEntry[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    memcpy(pPalEntries, systemEntriesWin32, 256 * sizeof(PALETTEENTRY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    PALETTEENTRY *pPal = pPalEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    int staticGrayType = device->GetGrayness();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    if (staticGrayType == GS_INDEXGRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        float m = 255.0f / 235.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        float g = 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        pPal = &pPalEntries[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        for (i = 10; i < 246; i++, pPal++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            pPal->peRed = pPal->peGreen = pPal->peBlue =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                (int)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            g += m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            pPal->peFlags = PC_NOCOLLAPSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    } else if (staticGrayType == GS_NOTGRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        for (i = 10; i < 246; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            pPalEntries[i] = customPalette[i-10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    pLogPal->palNumEntries = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    pLogPal->palVersion = 0x300;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    logicalPalette = ::CreatePalette(pLogPal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    for (i = 0; i < nEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        logicalEntries[i] =  0xff000000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        | (pPalEntries[i].peRed << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        | (pPalEntries[i].peGreen << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        | (pPalEntries[i].peBlue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    delete [] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}