JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
DirectDataBufferInt.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.nativewindow.awt;
29
30import java.awt.Point;
31import java.awt.color.ColorSpace;
32import java.awt.image.BufferedImage;
33import java.awt.image.ColorModel;
34import java.awt.image.DataBuffer;
35import java.awt.image.DirectColorModel;
36import java.awt.image.SampleModel;
37import java.awt.image.SinglePixelPackedSampleModel;
38import java.awt.image.WritableRaster;
39import java.nio.ByteBuffer;
40import java.nio.IntBuffer;
41import java.util.Hashtable;
42
43import com.jogamp.common.nio.Buffers;
44
45/**
46 * {@link DataBuffer} specialization using NIO direct buffer of type {@link DataBuffer#TYPE_INT} as storage.
47 */
48public final class DirectDataBufferInt extends DataBuffer {
49
50 public static class DirectWritableRaster extends WritableRaster {
51 protected DirectWritableRaster(final SampleModel sampleModel, final DirectDataBufferInt dataBuffer, final Point origin) {
52 super(sampleModel, dataBuffer, origin);
53 }
54 }
55
56 public static class BufferedImageInt extends BufferedImage {
57 final int customImageType;
58 final DirectDataBufferInt dataBuffer;
59 public BufferedImageInt (final int customImageType, final ColorModel cm,
60 final DirectDataBufferInt dataBuffer, final WritableRaster raster, final Hashtable<?,?> properties) {
61 super(cm, raster, false /* isRasterPremultiplied */, properties);
62 this.customImageType = customImageType;
63 this.dataBuffer = dataBuffer;
64 }
65
66 /**
67 * @return one of the custom image-type values {@link BufferedImage#TYPE_INT_ARGB TYPE_INT_ARGB},
68 * {@link BufferedImage#TYPE_INT_ARGB_PRE TYPE_INT_ARGB_PRE},
69 * {@link BufferedImage#TYPE_INT_RGB TYPE_INT_RGB} or {@link BufferedImage#TYPE_INT_BGR TYPE_INT_BGR}.
70 */
71 public int getCustomType() {
72 return customImageType;
73 }
74
75 /**
76 * Returns the underlying {@link DirectDataBufferInt} associated with this instance via {@link Raster} {@link #getRaster() instance}.
77 */
78 public DirectDataBufferInt getDataBuffer() { return dataBuffer; }
79
80 @Override
81 public String toString() {
82 return "BufferedImageInt@"+Integer.toHexString(hashCode())
83 +": custom/internal type = "+customImageType+"/"+getType()
84 +" "+getColorModel()+" "+getRaster();
85 }
86 }
87
88 /**
89 * Creates a {@link BufferedImageInt} using a {@link DirectColorModel direct color model} in {@link ColorSpace#CS_sRGB sRGB color space}.<br>
90 * It uses a {@link DirectWritableRaster} utilizing {@link DirectDataBufferInt} storage.
91 * <p>
92 * Note that due to using the custom storage type {@link DirectDataBufferInt}, the resulting
93 * {@link BufferedImage}'s {@link BufferedImage#getType() image-type} is of {@link BufferedImage#TYPE_CUSTOM TYPE_CUSTOM}.
94 * We are not able to change this detail, since the AWT image implementation associates the {@link BufferedImage#getType() image-type}
95 * with a build-in storage-type.
96 * Use {@link BufferedImageInt#getCustomType()} to retrieve the custom image-type, which will return the <code>imageType</code>
97 * value passed here.
98 * </p>
99 *
100 * @param width
101 * @param height
102 * @param imageType one of {@link BufferedImage#TYPE_INT_ARGB TYPE_INT_ARGB}, {@link BufferedImage#TYPE_INT_ARGB_PRE TYPE_INT_ARGB_PRE},
103 * {@link BufferedImage#TYPE_INT_RGB TYPE_INT_RGB} or {@link BufferedImage#TYPE_INT_BGR TYPE_INT_BGR}.
104 * @param location origin, if <code>null</code> 0/0 is assumed.
105 * @param properties <code>Hashtable</code> of
106 * <code>String</code>/<code>Object</code> pairs. Used for {@link BufferedImage#getProperty(String)} etc.
107 * @return
108 */
109 public static BufferedImageInt createBufferedImage(final int width, final int height, final int imageType, Point location, final Hashtable<?,?> properties) {
110 final ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
111 final int transferType = DataBuffer.TYPE_INT;
112 final int bpp, rmask, gmask, bmask, amask;
113 final boolean alphaPreMul;
114 switch( imageType ) {
115 case BufferedImage.TYPE_INT_ARGB:
116 bpp = 32;
117 rmask = 0x00ff0000;
118 gmask = 0x0000ff00;
119 bmask = 0x000000ff;
120 amask = 0xff000000;
121 alphaPreMul = false;
122 break;
123 case BufferedImage.TYPE_INT_ARGB_PRE:
124 bpp = 32;
125 rmask = 0x00ff0000;
126 gmask = 0x0000ff00;
127 bmask = 0x000000ff;
128 amask = 0xff000000;
129 alphaPreMul = true;
130 break;
131 case BufferedImage.TYPE_INT_RGB:
132 bpp = 24;
133 rmask = 0x00ff0000;
134 gmask = 0x0000ff00;
135 bmask = 0x000000ff;
136 amask = 0x0;
137 alphaPreMul = false;
138 break;
139 case BufferedImage.TYPE_INT_BGR:
140 bpp = 24;
141 rmask = 0x000000ff;
142 gmask = 0x0000ff00;
143 bmask = 0x00ff0000;
144 amask = 0x0;
145 alphaPreMul = false;
146 break;
147 default:
148 throw new IllegalArgumentException("Unsupported imageType, must be [INT_ARGB, INT_ARGB_PRE, INT_RGB, INT_BGR], has "+imageType);
149 }
150 final ColorModel colorModel = new DirectColorModel(colorSpace, bpp, rmask, gmask, bmask, amask, alphaPreMul, transferType);
151 final int[] bandMasks;
152 if ( 0 != amask ) {
153 bandMasks = new int[4];
154 bandMasks[3] = amask;
155 }
156 else {
157 bandMasks = new int[3];
158 }
159 bandMasks[0] = rmask;
160 bandMasks[1] = gmask;
161 bandMasks[2] = bmask;
162
163 final DirectDataBufferInt dataBuffer = new DirectDataBufferInt(width*height);
164 if( null == location ) {
165 location = new Point(0,0);
166 }
167 final SinglePixelPackedSampleModel sppsm = new SinglePixelPackedSampleModel(dataBuffer.getDataType(),
168 width, height, width /* scanLineStride */, bandMasks);
169 // IntegerComponentRasters must haveinteger DataBuffers:
170 // final WritableRaster raster = new IntegerInterleavedRaster(sppsm, dataBuffer, location);
171 // Not public:
172 // final WritableRaster raster = new SunWritableRaster(sppsm, dataBuffer, location);
173 final WritableRaster raster = new DirectWritableRaster(sppsm, dataBuffer, location);
174
175 return new BufferedImageInt(imageType, colorModel, dataBuffer, raster, properties);
176 }
177
178 /** Default NIO data bank storage, {@link ByteBuffer} representation. */
179 private final ByteBuffer dataBytes;
180 /** Default NIO data bank storage, {@link IntBuffer} representation. */
181 private final IntBuffer dataInts;
182
183 /** All NIO data banks, {@link ByteBuffer} representation. */
184 private final ByteBuffer bankdataBytes[];
185 /** All NIO data banks, {@link IntBuffer} representation. */
186 private final IntBuffer bankdataInts[];
187
188 /**
189 * Constructs an nio integer-based {@link DataBuffer} with a single bank
190 * and the specified size.
191 *
192 * @param size The size of the {@link DataBuffer}.
193 */
194 public DirectDataBufferInt(final int size) {
195 super(TYPE_INT, size);
196 dataBytes = Buffers.newDirectByteBuffer(size * Buffers.SIZEOF_INT);
197 dataInts = dataBytes.asIntBuffer();
198 bankdataBytes = new ByteBuffer[1];
199 bankdataInts = new IntBuffer[1];
200 bankdataBytes[0] = dataBytes;
201 bankdataInts[0] = dataInts;
202 }
203
204 /**
205 * Constructs an nio integer-based {@link DataBuffer} with the specified number of
206 * banks, all of which are the specified size.
207 *
208 * @param size The size of the banks in the {@link DataBuffer}.
209 * @param numBanks The number of banks in the a{@link DataBuffer}.
210 */
211 public DirectDataBufferInt(final int size, final int numBanks) {
212 super(TYPE_INT,size,numBanks);
213 bankdataBytes = new ByteBuffer[numBanks];
214 bankdataInts = new IntBuffer[numBanks];
215 for (int i= 0; i < numBanks; i++) {
216 bankdataBytes[i] = Buffers.newDirectByteBuffer(size * Buffers.SIZEOF_INT);
217 bankdataInts[i] = bankdataBytes[i].asIntBuffer();
218 }
219 dataBytes = bankdataBytes[0];
220 dataInts = bankdataInts[0];
221 }
222
223 /**
224 * Constructs an nio integer-based {@link DataBuffer} with a single bank using the
225 * specified array.
226 * <p>
227 * Only the first <code>size</code> elements should be used by accessors of
228 * this {@link DataBuffer}. <code>dataArray</code> must be large enough to
229 * hold <code>size</code> elements.
230 * </p>
231 *
232 * @param dataArray The NIO {@link ByteBuffer} array, holding the integer data for the {@link DataBuffer}.
233 * @param size The size of the {@link DataBuffer} bank.
234 */
235 public DirectDataBufferInt(final ByteBuffer dataArray, final int size) {
236 super(TYPE_INT,size);
237 dataBytes = Buffers.nativeOrder(dataArray);
238 dataInts = dataBytes.asIntBuffer();
239 bankdataBytes = new ByteBuffer[1];
240 bankdataInts = new IntBuffer[1];
241 bankdataBytes[0] = dataBytes;
242 bankdataInts[0] = dataInts;
243 }
244
245 /**
246 * Returns the default (first) int data array in {@link DataBuffer} as an {@link IntBuffer} representation.
247 *
248 * @return The first integer data array.
249 * @see #getDataBytes()
250 */
251 public IntBuffer getData() {
252 return dataInts;
253 }
254 /**
255 * Returns the default (first) int data array in {@link DataBuffer} as a {@link ByteBuffer} representation.
256 *
257 * @return The first integer data array.
258 * @see #getData()
259 */
260 public ByteBuffer getDataBytes() {
261 return dataBytes;
262 }
263
264 /**
265 * Returns the data array for the specified bank as an {@link IntBuffer} representation.
266 *
267 * @param bank The bank whose data array you want to get.
268 * @return The data array for the specified bank.
269 * @see #getDataBytes(int)
270 */
271 public IntBuffer getData(final int bank) {
272 return bankdataInts[bank];
273 }
274 /**
275 * Returns the data array for the specified bank as a {@link ByteBuffer} representation.
276 *
277 * @param bank The bank whose data array you want to get.
278 * @return The data array for the specified bank.
279 * @see #getData(int)
280 */
281 public ByteBuffer getDataBytes(final int bank) {
282 return bankdataBytes[bank];
283 }
284
285 /**
286 * Returns the requested data array element from the first (default) bank.
287 *
288 * @param i The data array element you want to get.
289 * @return The requested data array element as an integer.
290 * @see #setElem(int, int)
291 * @see #setElem(int, int, int)
292 */
293 @Override
294 public int getElem(final int i) {
295 return dataInts.get(i+offset);
296 }
297
298 /**
299 * Returns the requested data array element from the specified bank.
300 *
301 * @param bank The bank from which you want to get a data array element.
302 * @param i The data array element you want to get.
303 * @return The requested data array element as an integer.
304 * @see #setElem(int, int)
305 * @see #setElem(int, int, int)
306 */
307 @Override
308 public int getElem(final int bank, final int i) {
309 return bankdataInts[bank].get(i+offsets[bank]);
310 }
311
312 /**
313 * Sets the requested data array element in the first (default) bank
314 * to the specified value.
315 *
316 * @param i The data array element you want to set.
317 * @param val The integer value to which you want to set the data array element.
318 * @see #getElem(int)
319 * @see #getElem(int, int)
320 */
321 @Override
322 public void setElem(final int i, final int val) {
323 dataInts.put(i+offset, val);
324 }
325
326 /**
327 * Sets the requested data array element in the specified bank
328 * to the integer value <code>i</code>.
329 * @param bank The bank in which you want to set the data array element.
330 * @param i The data array element you want to set.
331 * @param val The integer value to which you want to set the specified data array element.
332 * @see #getElem(int)
333 * @see #getElem(int, int)
334 */
335 @Override
336 public void setElem(final int bank, final int i, final int val) {
337 bankdataInts[bank].put(i+offsets[bank], val);
338 }
339}
340
BufferedImageInt(final int customImageType, final ColorModel cm, final DirectDataBufferInt dataBuffer, final WritableRaster raster, final Hashtable<?,?> properties)
DirectDataBufferInt getDataBuffer()
Returns the underlying DirectDataBufferInt associated with this instance via Raster instance.
DirectWritableRaster(final SampleModel sampleModel, final DirectDataBufferInt dataBuffer, final Point origin)
DataBuffer specialization using NIO direct buffer of type DataBuffer#TYPE_INT as storage.
DirectDataBufferInt(final int size)
Constructs an nio integer-based DataBuffer with a single bank and the specified size.
int getElem(final int bank, final int i)
Returns the requested data array element from the specified bank.
int getElem(final int i)
Returns the requested data array element from the first (default) bank.
IntBuffer getData()
Returns the default (first) int data array in DataBuffer as an IntBuffer representation.
IntBuffer getData(final int bank)
Returns the data array for the specified bank as an IntBuffer representation.
static BufferedImageInt createBufferedImage(final int width, final int height, final int imageType, Point location, final Hashtable<?,?> properties)
Creates a BufferedImageInt using a direct color model in sRGB color space.
ByteBuffer getDataBytes()
Returns the default (first) int data array in DataBuffer as a ByteBuffer representation.
DirectDataBufferInt(final ByteBuffer dataArray, final int size)
Constructs an nio integer-based DataBuffer with a single bank using the specified array.
void setElem(final int bank, final int i, final int val)
Sets the requested data array element in the specified bank to the integer value i.
DirectDataBufferInt(final int size, final int numBanks)
Constructs an nio integer-based DataBuffer with the specified number of banks, all of which are the s...
ByteBuffer getDataBytes(final int bank)
Returns the data array for the specified bank as a ByteBuffer representation.
void setElem(final int i, final int val)
Sets the requested data array element in the first (default) bank to the specified value.