jdk/src/windows/native/sun/windows/awt_BitmapUtil.cpp
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6825 795e9fe949d3
child 18232 b538b71fb429
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6825
diff changeset
     2
 * Copyright (c) 2006, 2010, 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: 2451
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: 2451
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: 2451
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2451
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2451
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "stdhdrs.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "windows.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <windowsx.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <zmouse.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
    31
#include "GraphicsPrimitiveMgr.h"
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "awt.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "awt_BitmapUtil.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
    36
// Platform-dependent RECT_[EQ | SET | INC_HEIGHT] macros
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
    37
#include "utility/rect.h"
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
    38
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
HBITMAP BitmapUtil::CreateTransparencyMaskFromARGB(int width, int height, int* imageData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    //Scan lines should be aligned to word boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    int bufLength = ((width + 15) / 16 * 2) * height;//buf length (bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    int* srcPos = imageData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    char* buf = new char[bufLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    char* bufPos = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    int tmp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int cbit = 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    if (buf == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    for (int i = 0; i < height; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (int j = 0; j < width; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            //cbit is shifted right for every pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            //next byte is stored when cbit is zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            if ((cbit & 0xFF) == 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                *bufPos = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                bufPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                tmp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                cbit = 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            unsigned char alpha = (*srcPos >> 0x18) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (alpha == 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                tmp |= cbit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            cbit >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            srcPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        //save last word at the end of scan line even if it's incomplete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        *bufPos = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        bufPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        tmp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        cbit = 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        //add word-padding byte if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (((bufPos - buf) & 0x01) == 0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            *bufPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            bufPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    HBITMAP bmp = CreateBitmap(width, height, 1, 1, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    delete[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    return bmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
//BITMAPINFO extended with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
typedef struct tagBITMAPINFOEX  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    BITMAPINFOHEADER bmiHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    DWORD            dwMasks[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
}   BITMAPINFOEX, *LPBITMAPINFOEX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Creates 32-bit ARGB bitmap from specified RAW data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * This function may not work on OS prior to Win95.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * See MSDN articles for CreateDIBitmap, BITMAPINFOHEADER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * BITMAPV4HEADER, BITMAPV5HEADER for additional info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
HBITMAP BitmapUtil::CreateV4BitmapFromARGB(int width, int height, int* imageData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    BITMAPINFOEX    bitmapInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    HDC             hDC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    char            *bitmapData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    HBITMAP         hTempBitmap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    HBITMAP         hBitmap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    hDC = ::GetDC(::GetDesktopWindow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    if (!hDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    memset(&bitmapInfo, 0, sizeof(BITMAPINFOEX));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    bitmapInfo.bmiHeader.biWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    bitmapInfo.bmiHeader.biHeight = -height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    bitmapInfo.bmiHeader.biPlanes = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    bitmapInfo.bmiHeader.biBitCount = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    bitmapInfo.bmiHeader.biCompression = BI_RGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    hTempBitmap = ::CreateDIBSection(hDC, (BITMAPINFO*)&(bitmapInfo),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                    DIB_RGB_COLORS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                    (void**)&(bitmapData),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                    NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (!bitmapData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        ReleaseDC(::GetDesktopWindow(), hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    int* src = imageData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    char* dest = bitmapData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    for (int i = 0; i < height; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (int j = 0; j < width; j++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            unsigned char alpha = (*src >> 0x18) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (alpha == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                dest[3] = dest[2] = dest[1] = dest[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                dest[3] = alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                dest[2] = (*src >> 0x10) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                dest[1] = (*src >> 0x08) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                dest[0] = *src & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            dest += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    hBitmap = CreateDIBitmap(hDC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                             (BITMAPINFOHEADER*)&bitmapInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                             CBM_INIT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                             (void *)bitmapData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                             (BITMAPINFO*)&bitmapInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                             DIB_RGB_COLORS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    ::DeleteObject(hTempBitmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    ::ReleaseDC(::GetDesktopWindow(), hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    ::GdiFlush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    return hBitmap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   156
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   157
/*
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   158
 * Creates 32-bit premultiplied ARGB bitmap from specified ARGBPre data.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   159
 * This function may not work on OS prior to Win95.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   160
 * See MSDN articles for CreateDIBitmap, BITMAPINFOHEADER,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   161
 * BITMAPV4HEADER, BITMAPV5HEADER for additional info.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   162
 */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   163
HBITMAP BitmapUtil::CreateBitmapFromARGBPre(int width, int height,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   164
                                            int srcStride,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   165
                                            int* imageData)
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   166
{
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   167
    BITMAPINFOHEADER bmi;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   168
    void *bitmapBits = NULL;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   169
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   170
    ZeroMemory(&bmi, sizeof(bmi));
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   171
    bmi.biSize = sizeof(bmi);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   172
    bmi.biWidth = width;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   173
    bmi.biHeight = -height;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   174
    bmi.biPlanes = 1;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   175
    bmi.biBitCount = 32;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   176
    bmi.biCompression = BI_RGB;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   177
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   178
    HBITMAP hBitmap =
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   179
        ::CreateDIBSection(NULL, (BITMAPINFO *) & bmi, DIB_RGB_COLORS,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   180
                           &bitmapBits, NULL, 0);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   181
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   182
    if (!bitmapBits) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   183
        return NULL;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   184
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   185
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   186
    int dstStride = width * 4;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   187
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   188
    if (srcStride == dstStride) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   189
        memcpy(bitmapBits, (void*)imageData, srcStride * height);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   190
    } else if (height > 0) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   191
        void *pSrcPixels = (void*)imageData;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   192
        void *pDstPixels = bitmapBits;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   193
        do {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   194
            memcpy(pDstPixels, pSrcPixels, dstStride);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   195
            pSrcPixels = PtrAddBytes(pSrcPixels, srcStride);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   196
            pDstPixels = PtrAddBytes(pDstPixels, dstStride);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   197
        } while (--height > 0);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   198
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   199
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   200
    return hBitmap;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   201
}
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   202
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   203
extern "C" {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   204
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   205
/**
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   206
 * This method is called from the WGL pipeline when it needs to create a bitmap
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   207
 * needed to update the layered window.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   208
 */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   209
HBITMAP BitmapUtil_CreateBitmapFromARGBPre(int width, int height,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   210
                                           int srcStride,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   211
                                           int* imageData)
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   212
{
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   213
    return BitmapUtil::CreateBitmapFromARGBPre(width, height,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   214
                                               srcStride, imageData);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   215
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   216
}
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   217
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   218
}  /* extern "C" */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   219
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   220
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   221
/**
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   222
 * Transforms the given bitmap into an HRGN representing the transparency
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   223
 * of the bitmap. The bitmap MUST BE 32bpp. Alpha value == 0 is considered
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   224
 * transparent, alpha > 0 - opaque.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   225
 */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   226
HRGN BitmapUtil::BitmapToRgn(HBITMAP hBitmap)
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   227
{
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   228
    HDC hdc = ::CreateCompatibleDC(NULL);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   229
    ::SelectObject(hdc, hBitmap);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   230
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   231
    BITMAPINFOEX bi;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   232
    ::ZeroMemory(&bi, sizeof(bi));
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   233
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   234
    bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   235
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   236
    BOOL r = ::GetDIBits(hdc, hBitmap, 0, 0, NULL,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   237
            reinterpret_cast<BITMAPINFO*>(&bi), DIB_RGB_COLORS);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   238
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   239
    if (!r || bi.bmiHeader.biBitCount != 32)
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   240
    {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   241
        ::DeleteDC(hdc);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   242
        return NULL;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   243
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   244
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   245
    UINT width = bi.bmiHeader.biWidth;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   246
    UINT height = abs(bi.bmiHeader.biHeight);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   247
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   248
    BYTE * buf = (BYTE*)safe_Malloc(bi.bmiHeader.biSizeImage);
6825
795e9fe949d3 6989721: awt native code compiler warnings
art
parents: 5506
diff changeset
   249
    bi.bmiHeader.biHeight = -(INT)height;
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   250
    ::GetDIBits(hdc, hBitmap, 0, height, buf,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   251
            reinterpret_cast<BITMAPINFO*>(&bi), DIB_RGB_COLORS);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   252
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   253
    /* reserving memory for the worst case */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   254
    RGNDATA * pRgnData = (RGNDATA *) safe_Malloc(sizeof(RGNDATAHEADER) +
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   255
            sizeof(RECT) * (width / 2 + 1) * height);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   256
    RGNDATAHEADER * pRgnHdr = (RGNDATAHEADER *) pRgnData;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   257
    pRgnHdr->dwSize = sizeof(RGNDATAHEADER);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   258
    pRgnHdr->iType = RDH_RECTANGLES;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   259
    pRgnHdr->nRgnSize = 0;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   260
    pRgnHdr->rcBound.top = 0;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   261
    pRgnHdr->rcBound.left = 0;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   262
    pRgnHdr->rcBound.bottom = height;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   263
    pRgnHdr->rcBound.right = width;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   264
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   265
    pRgnHdr->nCount = BitmapToYXBandedRectangles(32, width, height, buf,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   266
            (RECT_T *) (((BYTE *) pRgnData) + sizeof(RGNDATAHEADER)));
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   267
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   268
    HRGN rgn = ::ExtCreateRegion(NULL,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   269
            sizeof(RGNDATAHEADER) + sizeof(RECT_T) * pRgnHdr->nCount,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   270
            pRgnData);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   271
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   272
    free(pRgnData);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   273
    ::DeleteDC(hdc);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   274
    free(buf);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   275
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   276
    return rgn;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   277
}
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   278
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   279
/**
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   280
 * Makes a copy of the given bitmap. Blends every pixel of the source
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   281
 * with the given blendColor and alpha. If alpha == 0, the function
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   282
 * simply makes a plain copy of the source without any blending.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   283
 */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   284
HBITMAP BitmapUtil::BlendCopy(HBITMAP hSrcBitmap, COLORREF blendColor,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   285
        BYTE alpha)
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   286
{
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   287
    HDC hdc = ::CreateCompatibleDC(NULL);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   288
    HBITMAP oldBitmap = (HBITMAP)::SelectObject(hdc, hSrcBitmap);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   289
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   290
    BITMAPINFOEX bi;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   291
    ::ZeroMemory(&bi, sizeof(bi));
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   292
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   293
    bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   294
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   295
    BOOL r = ::GetDIBits(hdc, hSrcBitmap, 0, 0, NULL,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   296
            reinterpret_cast<BITMAPINFO*>(&bi), DIB_RGB_COLORS);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   297
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   298
    if (!r || bi.bmiHeader.biBitCount != 32)
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   299
    {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   300
        ::DeleteDC(hdc);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   301
        return NULL;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   302
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   303
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   304
    UINT width = bi.bmiHeader.biWidth;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   305
    UINT height = abs(bi.bmiHeader.biHeight);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   306
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   307
    BYTE * buf = (BYTE*)safe_Malloc(bi.bmiHeader.biSizeImage);
6825
795e9fe949d3 6989721: awt native code compiler warnings
art
parents: 5506
diff changeset
   308
    bi.bmiHeader.biHeight = -(INT)height;
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   309
    ::GetDIBits(hdc, hSrcBitmap, 0, height, buf,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   310
            reinterpret_cast<BITMAPINFO*>(&bi), DIB_RGB_COLORS);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   311
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   312
    UINT widthBytes = width * bi.bmiHeader.biBitCount / 8;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   313
    UINT alignedWidth = (((widthBytes - 1) / 4) + 1) * 4;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   314
    UINT i, j;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   315
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   316
    for (j = 0; j < height; j++) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   317
        BYTE *pSrc = (BYTE *) buf + j * alignedWidth;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   318
        for (i = 0; i < width; i++, pSrc += 4) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   319
            // Note: if the current alpha is zero, the other three color
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   320
            // components may (theoretically) contain some uninitialized
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   321
            // data. The developer does not expect to display them,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   322
            // hence we handle this situation differently.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   323
            if (pSrc[3] == 0) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   324
                pSrc[0] = GetBValue(blendColor) * alpha / 255;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   325
                pSrc[1] = GetGValue(blendColor) * alpha / 255;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   326
                pSrc[2] = GetRValue(blendColor) * alpha / 255;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   327
                pSrc[3] = alpha;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   328
            } else {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   329
                pSrc[0] = (GetBValue(blendColor) * alpha / 255) +
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   330
                    (pSrc[0] * (255 - alpha) / 255);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   331
                pSrc[1] = (GetGValue(blendColor) * alpha / 255) +
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   332
                    (pSrc[1] * (255 - alpha) / 255);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   333
                pSrc[2] = (GetRValue(blendColor) * alpha / 255) +
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   334
                    (pSrc[2] * (255 - alpha) / 255);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   335
                pSrc[3] = (alpha * alpha / 255) +
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   336
                    (pSrc[3] * (255 - alpha) / 255);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   337
            }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   338
        }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   339
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   340
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   341
    HBITMAP hDstBitmap = ::CreateDIBitmap(hdc,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   342
            reinterpret_cast<BITMAPINFOHEADER*>(&bi),
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   343
            CBM_INIT,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   344
            buf,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   345
            reinterpret_cast<BITMAPINFO*>(&bi),
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   346
            DIB_RGB_COLORS
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   347
            );
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   348
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   349
    ::SelectObject(hdc, oldBitmap);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   350
    ::DeleteDC(hdc);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   351
    free(buf);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   352
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   353
    return hDstBitmap;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   354
}
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   355
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   356
/**
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   357
 * Creates a 32 bit ARGB bitmap. Returns the bitmap handle. The *bitmapBits
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   358
 * contains the pointer to the bitmap data or NULL if an error occured.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   359
 */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   360
HBITMAP BitmapUtil::CreateARGBBitmap(int width, int height, void ** bitmapBitsPtr)
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   361
{
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   362
    BITMAPINFOHEADER bmi;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   363
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   364
    ::ZeroMemory(&bmi, sizeof(bmi));
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   365
    bmi.biSize = sizeof(BITMAPINFOHEADER);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   366
    bmi.biWidth = width;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   367
    bmi.biHeight = -height;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   368
    bmi.biPlanes = 1;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   369
    bmi.biBitCount = 32;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   370
    bmi.biCompression = BI_RGB;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   371
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   372
    return ::CreateDIBSection(NULL, (BITMAPINFO *) & bmi, DIB_RGB_COLORS,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   373
                bitmapBitsPtr, NULL, 0);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 2
diff changeset
   374
}