JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
PixelRectangle.java
Go to the documentation of this file.
1/**
2 * Copyright (c) 2014 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.util;
29
30import java.nio.ByteBuffer;
31
32/**
33 * Pixel Rectangle identified by it's {@link #hashCode()}.
34 * <p>
35 * The {@link #getPixels()} are assumed to be immutable.
36 * </p>
37 */
38public interface PixelRectangle {
39 /**
40 * <p>
41 * Computes a hash code over:
42 * <ul>
43 * <li>pixelformat</li>
44 * <li>size</li>
45 * <li>stride</li>
46 * <li>isGLOriented</li>
47 * <li>pixels</li>
48 * </ul>
49 * </p>
50 * <p>
51 * The hashCode shall be computed only once with first call
52 * and stored for later retrieval to enhance performance.
53 * </p>
54 * <p>
55 * {@inheritDoc}
56 * </p>
57 */
58 @Override
59 int hashCode();
60
61 /** Returns the {@link PixelFormat}. */
63
64 /** Returns the size, i.e. width and height. */
66
67 /**
68 * Returns stride in byte-size, i.e. byte count from one line to the next.
69 * <p>
70 * Must be >= {@link #getPixelformat()}.{@link PixelFormat#bytesPerPixel() bytesPerPixel()} * {@link #getSize()}.{@link DimensionImmutable#getWidth() getWidth()}.
71 * </p>
72 */
73 int getStride();
74
75 /**
76 * Returns <code>true</code> if the memory is laid out in
77 * OpenGL's coordinate system, <i>origin at bottom left</i>.
78 * Otherwise returns <code>false</code>, i.e. <i>origin at top left</i>.
79 */
80 public boolean isGLOriented();
81
82 /** Returns the pixels. */
83 ByteBuffer getPixels();
84
85 @Override
86 String toString();
87
88 /**
89 * Generic PixelRectangle implementation
90 */
91 public static class GenericPixelRect implements PixelRectangle {
92 protected final PixelFormat pixelformat;
93 protected final DimensionImmutable size;
94 protected final int strideInBytes;
95 protected final boolean isGLOriented;
96 protected final ByteBuffer pixels;
97 private int hashCode = 0;
98 private volatile boolean hashCodeComputed = false;
99
100 /**
101 *
102 * @param pixelformat
103 * @param size
104 * @param strideInBytes stride in byte-size, i.e. byte count from one line to the next.
105 * If not zero, value must be >= <code>width * bytes-per-pixel</code>.
106 * If zero, stride is set to <code>width * bytes-per-pixel</code>.
107 * @param isGLOriented
108 * @param pixels
109 * @throws IllegalArgumentException if <code>strideInBytes</code> is invalid.
110 * @throws IndexOutOfBoundsException if <code>pixels</code> has insufficient bytes left
111 */
112 public GenericPixelRect(final PixelFormat pixelformat, final DimensionImmutable size, int strideInBytes, final boolean isGLOriented, final ByteBuffer pixels)
113 throws IllegalArgumentException, IndexOutOfBoundsException
114 {
115 if( 0 != strideInBytes ) {
117 throw new IllegalArgumentException("Invalid stride "+strideInBytes+", must be greater than bytesPerPixel "+pixelformat.comp.bytesPerPixel()+" * width "+size.getWidth());
118 }
119 } else {
121 }
122 final int reqBytes = strideInBytes * size.getHeight();
123 if( pixels.limit() < reqBytes ) {
124 throw new IndexOutOfBoundsException("Dest buffer has insufficient bytes left, needs "+reqBytes+": "+pixels);
125 }
126 this.pixelformat = pixelformat;
127 this.size = size;
128 this.strideInBytes = strideInBytes;
129 this.isGLOriented = isGLOriented;
130 this.pixels = pixels;
131 }
132
133 /**
134 * Copy ctor validating src.
135 * @param src
136 * @throws IllegalArgumentException if <code>strideInBytes</code> is invalid.
137 * @throws IndexOutOfBoundsException if <code>pixels</code> has insufficient bytes left
138 */
140 throws IllegalArgumentException, IndexOutOfBoundsException
141 {
142 this(src.getPixelformat(), src.getSize(), src.getStride(), src.isGLOriented(), src.getPixels());
143 }
144
145 @Override
146 public int hashCode() {
147 if( !hashCodeComputed ) { // DBL CHECKED OK VOLATILE
148 synchronized (this) {
149 if( !hashCodeComputed ) {
150 // 31 * x == (x << 5) - x
151 int hash = pixelformat.comp.hashCode();
152 hash = ((hash << 5) - hash) + size.hashCode();
153 hash = ((hash << 5) - hash) + strideInBytes;
154 hash = ((hash << 5) - hash) + ( isGLOriented ? 1 : 0);
155 hashCode = ((hash << 5) - hash) + pixels.hashCode();
156 hashCodeComputed = true;
157 }
158 }
159 }
160 return hashCode;
161 }
162
163 @Override
165 return pixelformat;
166 }
167
168 @Override
170 return size;
171 }
172
173 @Override
174 public int getStride() {
175 return strideInBytes;
176 }
177
178 @Override
179 public boolean isGLOriented() {
180 return isGLOriented;
181 }
182
183 @Override
184 public ByteBuffer getPixels() {
185 return pixels;
186 }
187
188 @Override
189 public final String toString() {
190 return "PixelRect[obj 0x"+Integer.toHexString(super.hashCode())+", "+pixelformat+", "+size+", stride "+strideInBytes+", isGLOrient "+isGLOriented+", pixels "+pixels+"]";
191 }
192 }
193}
194
DimensionImmutable getSize()
Returns the size, i.e.
GenericPixelRect(final PixelFormat pixelformat, final DimensionImmutable size, int strideInBytes, final boolean isGLOriented, final ByteBuffer pixels)
boolean isGLOriented()
Returns true if the memory is laid out in OpenGL's coordinate system, origin at bottom left.
GenericPixelRect(final PixelRectangle src)
Copy ctor validating src.
final Composition comp
Unique Pixel Composition, i.e.
Immutable Dimension Interface, consisting of it's read only components:
int bytesPerPixel()
Number of bytes per pixel, i.e.
int hashCode()
Returns cached immutable hash value, see Object#hashCode().
Pixel Rectangle identified by it's hashCode().
boolean isGLOriented()
Returns true if the memory is laid out in OpenGL's coordinate system, origin at bottom left.
DimensionImmutable getSize()
Returns the size, i.e.
ByteBuffer getPixels()
Returns the pixels.
int getStride()
Returns stride in byte-size, i.e.
PixelFormat getPixelformat()
Returns the PixelFormat.