1/*
2 * Copyright (c) 1999, 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_IconCursor.h"
27
28/* common code used by awt_Frame.cpp and awt_Cursor.cpp */
29
30HBITMAP create_BMP(HWND hW,int* imageData,int nSS, int nW, int nH)
31{
32    Bitmapheader    bmhHeader;
33    HDC             hDC;
34    char            *ptrImageData;
35    HBITMAP         hbmpBitmap;
36    HBITMAP         hBitmap;
37
38    int             nNumChannels    = 3;
39
40    if (!hW) {
41        hW = ::GetDesktopWindow();
42    }
43    hDC = ::GetDC(hW);
44    if (!hDC) {
45        return NULL;
46    }
47
48    memset(&bmhHeader, 0, sizeof(Bitmapheader));
49    bmhHeader.bmiHeader.biSize              = sizeof(BITMAPINFOHEADER);
50    bmhHeader.bmiHeader.biWidth             = nW;
51    bmhHeader.bmiHeader.biHeight            = -nH;
52    bmhHeader.bmiHeader.biPlanes            = 1;
53
54    bmhHeader.bmiHeader.biBitCount          = 24;
55    bmhHeader.bmiHeader.biCompression       = BI_RGB;
56
57    hbmpBitmap = ::CreateDIBSection(hDC, (BITMAPINFO*)&(bmhHeader),
58                                    DIB_RGB_COLORS,
59                                    (void**)&(ptrImageData),
60                                    NULL, 0);
61    int  *srcPtr = imageData;
62    char *dstPtr = ptrImageData;
63    if (!dstPtr) {
64        ReleaseDC(hW, hDC);
65        return NULL;
66    }
67    for (int nOutern = 0; nOutern < nH; nOutern++ ) {
68        for (int nInner = 0; nInner < nSS; nInner++ ) {
69            dstPtr[2] = (*srcPtr >> 0x10) & 0xFF;
70            dstPtr[1] = (*srcPtr >> 0x08) & 0xFF;
71            dstPtr[0] = *srcPtr & 0xFF;
72
73            srcPtr++;
74            dstPtr += nNumChannels;
75        }
76    }
77
78    // convert it into DDB to make CustomCursor work on WIN95
79    hBitmap = CreateDIBitmap(hDC,
80                             (BITMAPINFOHEADER*)&bmhHeader,
81                             CBM_INIT,
82                             (void *)ptrImageData,
83                             (BITMAPINFO*)&bmhHeader,
84                             DIB_RGB_COLORS);
85
86    ::DeleteObject(hbmpBitmap);
87    ::ReleaseDC(hW, hDC);
88    ::GdiFlush();
89    return hBitmap;
90}
91
92void destroy_BMP(HBITMAP hBMP)
93{
94    if (hBMP) {
95        ::DeleteObject(hBMP);
96    }
97}
98