1 | /* |
2 | * $Id: HBITMAP.java,v 1.3 2005/10/28 18:53:16 hastings Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | |
13 | package com.moesol.bindings.platform_sdk.windows_api; |
14 | |
15 | import java.awt.image.BufferedImage; |
16 | import java.awt.image.SinglePixelPackedSampleModel; |
17 | |
18 | /** |
19 | * Holds a Win32 HBITMAP |
20 | */ |
21 | public class HBITMAP extends HANDLE { |
22 | HBITMAP(long bitmap, boolean temporary) { |
23 | super(bitmap, temporary); |
24 | } |
25 | HBITMAP(long bitmap) { |
26 | super(bitmap); |
27 | } |
28 | /** |
29 | * Create a depth 1 bitmap from an array of bytes, one bit per pixel |
30 | * eight bits per byte. |
31 | * |
32 | * @param width - number of bits in a scanline |
33 | * @param height - number of scanlines |
34 | * @param bits - array of bytes with length = width * height / 8 or null. |
35 | * @return handle to a native bitmap |
36 | */ |
37 | public static HBITMAP createBitmap(int width, int height, byte[] bits) { |
38 | checkBits(width, height, bits); |
39 | long r = jni_createBitmap(width, height, bits); |
40 | if (r == 0) { throw new RuntimeException("createBitmap failed"); } |
41 | |
42 | HBITMAP rbm = new HBITMAP(r); |
43 | rbm.m_width = width; |
44 | rbm.m_height = height; |
45 | |
46 | return rbm; |
47 | } |
48 | public static HBITMAP createCompatibleBitmap(HDC hdc, int width, int height) { |
49 | long r = jni_createCompatibleBitmap(hdc._getHandle(), width, height); |
50 | if (r == 0) { throw new RuntimeException("createCompatibleBitmap failed"); } |
51 | |
52 | HBITMAP rbm = new HBITMAP(r); |
53 | rbm.m_width = width; |
54 | rbm.m_height = height; |
55 | return rbm; |
56 | } |
57 | public static HBITMAP createDIBitmap( |
58 | HDC hdc, BITMAPINFOHEADER bmih, |
59 | int fdwInit, |
60 | int[] lpbInit, |
61 | BITMAPINFO lpbmi, |
62 | int fuUsage |
63 | ) { |
64 | long r = jni_createDIBBitmap(hdc._getHandle(), bmih._getStructureBytes(), |
65 | fdwInit, |
66 | lpbInit, |
67 | lpbmi.get_bmiHeader()._getStructureBytes(), |
68 | fuUsage); |
69 | if (r == 0) { |
70 | throw new RuntimeException("createDIBitmap failed"); |
71 | } |
72 | HBITMAP rbm = new HBITMAP(r); |
73 | rbm.m_width = bmih.get_biWidth(); |
74 | rbm.m_height = bmih.get_biHeight(); |
75 | return rbm; |
76 | } |
77 | /** |
78 | * Create a Win32 Device Dependent Bitmap from a java.awt.image.BufferedImage. |
79 | * Currently only images of type TYPE_INT_ARGB are supported. |
80 | * |
81 | * @param hdc |
82 | * @param bi - BufferedImage of type TYPE_INT_ARGB. |
83 | * @param clear_alpha - if true resets the alpha byte to zero |
84 | * @return handle to a Device Dependent Bitmap |
85 | */ |
86 | public static HBITMAP createDIBitmap(HDC hdc, BufferedImage bi, boolean clear_alpha) { |
87 | BITMAPINFO bmi = new BITMAPINFO(0); |
88 | BITMAPINFOHEADER bmih = bmi.get_bmiHeader(); |
89 | bmih.set_biSize(BITMAPINFOHEADER.sizeof()); |
90 | bmih.set_biWidth(bi.getWidth()); |
91 | bmih.set_biHeight(-bi.getHeight()); |
92 | bmih.set_biPlanes((short)1); |
93 | bmih.set_biBitCount((short)32); |
94 | bmih.set_biCompression(BITMAPINFOHEADER.BI_RGB); |
95 | |
96 | SinglePixelPackedSampleModel sm = (SinglePixelPackedSampleModel)bi.getSampleModel(); |
97 | int[] bits = bi.getRGB(0, 0, bi.getWidth(), bi.getHeight(), null, 0, sm.getScanlineStride()); |
98 | if (clear_alpha) { |
99 | int n = bits.length; |
100 | for (int i = 0; i < n; i++) { |
101 | bits[i] &= (0x00FFFFFF & bits[i]); |
102 | } |
103 | } |
104 | return HBITMAP.createDIBitmap(hdc, bmi.get_bmiHeader(), |
105 | HBITMAP.CBM_INIT, bits, bmi, HBITMAP.DIB_RGB_COLORS); |
106 | } |
107 | public static HBITMAP createDIBitmap(HDC hdc, BufferedImage bi) { |
108 | return createDIBitmap(hdc, bi, false); |
109 | } |
110 | |
111 | public int getHeight() { |
112 | return m_height; |
113 | } |
114 | public int getWidth() { |
115 | return m_width; |
116 | } |
117 | |
118 | public void setBits(byte[] bits) { |
119 | checkBitsForHandle(bits); |
120 | boolean r = jni_setBits(_getHandle(), bits, bits.length); |
121 | if (!r) { throw new RuntimeException("setBits failed"); } |
122 | } |
123 | public void getBits(byte[] bits) { |
124 | checkBitsForHandle(bits); |
125 | boolean r = jni_getBits(_getHandle(), bits, bits.length); |
126 | if (!r) { throw new RuntimeException("getBits failed"); } |
127 | } |
128 | public void destroyBitmap(){ |
129 | m_width = 0; |
130 | m_height = 0; |
131 | boolean r = jni_destroyBitmap(_getHandle()); |
132 | if (!r) { throw new RuntimeException("destroyBitmap failed"); } |
133 | detach(); |
134 | } |
135 | |
136 | public int getObject(BITMAP [] out_bitmap) { |
137 | BITMAP bm = new BITMAP(); |
138 | int bytes = jni_getObject(_getHandle(), bm._getStructureBytes()); |
139 | assert(bm.get_bmBitsPixel() != 0); |
140 | out_bitmap[0] = bm; |
141 | return bytes; |
142 | } |
143 | |
144 | private native int jni_getObject(long handle, byte [] bm_bytes); |
145 | |
146 | public static final int CBM_INIT = 0x04; |
147 | public static final int DIB_RGB_COLORS = 0; |
148 | public static final int DIB_PAL_COLORS = 1; |
149 | |
150 | private static void checkBits(int width, int height, byte[] bits) { |
151 | if (bits == null) { |
152 | // create an uninitialized bitmap of width, height |
153 | return; |
154 | } |
155 | if (bits.length != width * Math.abs(height) / 8) { |
156 | throw new IllegalArgumentException("bits is wrong size"); |
157 | } |
158 | } |
159 | private void checkBitsForHandle(byte[] bits) { |
160 | BITMAP[] bm = { null }; |
161 | getObject(bm); |
162 | int expect = bm[0].get_bmWidthBytes() * bm[0].get_bmWidth(); |
163 | if (expect != bits.length) { |
164 | throw new IllegalArgumentException("bits is wrong size"); |
165 | } |
166 | } |
167 | |
168 | private static native long jni_createBitmap(int width, int height, byte[] bits); |
169 | private static native long jni_createCompatibleBitmap(long handle, int width, int height); |
170 | private static native long jni_createDIBBitmap(long handle, byte[] bmih, int fdwInit, int[] lpbInit, byte[] lpbmi, int fuUsage); |
171 | |
172 | private native boolean jni_destroyBitmap(long handle); |
173 | private native boolean jni_setBits(long handle, byte[] bits, int length); |
174 | private native boolean jni_getBits(long handle, byte[] bits, int length); |
175 | |
176 | private int m_width; |
177 | private int m_height; |
178 | } |