JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
AWTGLReadBufferUtil.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.opengl.util.awt;
29
30import java.awt.image.BufferedImage;
31
32import com.jogamp.opengl.GL;
33import com.jogamp.opengl.GLDrawable;
34import com.jogamp.opengl.GLProfile;
35import com.jogamp.opengl.util.GLPixelBuffer;
36import com.jogamp.opengl.util.GLReadBufferUtil;
37import com.jogamp.opengl.util.GLPixelBuffer.GLPixelBufferProvider;
38
39/**
40 * {@link GLReadBufferUtil} specialization allowing to
41 * read out a frambuffer to an AWT BufferedImage
42 * utilizing {@link AWTPixelBufferProviderInt} for further AWT processing.
43 */
45 /**
46 * {@inheritDoc}
47 *
48 * Using the AWT {@link GLPixelBuffer}: {@link AWTGLPixelBuffer.AWTGLPixelBufferProvider}, always using alpha on OpenGL operations.
49 * <p>
50 * The host {@link PixelFormat} will be a 32bit INT compatible to AWT, capable to store the GL RGBA read data, regardless whether AWT utilizes the alpha component.
51 * </p>
52 *
53 * @param requestAlpha true for RGBA readPixels, otherwise RGB readPixels. Disclaimer: {@link #hasAlpha()}==true is forced due to the used {@link AWTGLPixelBuffer.AWTGLPixelBufferProvider} when calling {@link #readPixels(GL, int, int, int, int, boolean) readPixels}.
54 */
55 public AWTGLReadBufferUtil(final GLProfile glp, final boolean requestAlpha) {
56 super(new AWTGLPixelBuffer.AWTGLPixelBufferProvider( glp.isGL2ES3() /* allowRowStride */ ), requestAlpha /* See Bug 1381 */, false);
57 }
58
59 /**
60 * Returns the {@link AWTGLPixelBuffer}, as filled by previous call to {@link #readPixels(GL, int, int, int, int, boolean)}.
61 */
63
64 /**
65 * Read the drawable's pixels to TextureData and Texture, if requested at construction,
66 * and returns an aligned {@link BufferedImage}.
67 *
68 * @param gl the current GL context object. It's read drawable is being used as the pixel source.
69 * @param awtOrientation flips the data vertically if <code>true</code>.
70 * The context's drawable {@link GLDrawable#isGLOriented()} state
71 * is taken into account.
72 * Vertical flipping is propagated to TextureData
73 * and handled in a efficient manner there (TextureCoordinates and TextureIO writer).
74 * @see #AWTGLReadBufferUtil(GLProfile, boolean)
75 */
76 public BufferedImage readPixelsToBufferedImage(final GL gl, final boolean awtOrientation) {
77 return readPixelsToBufferedImage(gl, 0, 0, 0, 0, awtOrientation);
78 }
79
80 /**
81 * Read the drawable's pixels to TextureData and Texture, if requested at construction,
82 * and returns an aligned {@link BufferedImage}.
83 *
84 * @param gl the current GL context object. It's read drawable is being used as the pixel source.
85 * @param inX readPixel x offset
86 * @param inY readPixel y offset
87 * @param inWidth optional readPixel width value, used if [1 .. drawable.width], otherwise using drawable.width
88 * @param inHeight optional readPixel height, used if [1 .. drawable.height], otherwise using drawable.height
89 * @param awtOrientation flips the data vertically if <code>true</code>.
90 * The context's drawable {@link GLDrawable#isGLOriented()} state
91 * is taken into account.
92 * Vertical flipping is propagated to TextureData
93 * and handled in a efficient manner there (TextureCoordinates and TextureIO writer).
94 * @see #AWTGLReadBufferUtil(GLProfile, boolean)
95 */
96 public BufferedImage readPixelsToBufferedImage(final GL gl, final int inX, final int inY, final int inWidth, final int inHeight, final boolean awtOrientation) {
97 final GLDrawable drawable = gl.getContext().getGLReadDrawable();
98 final int width, height;
99 if( 0 >= inWidth || drawable.getSurfaceWidth() < inWidth ) {
100 width = drawable.getSurfaceWidth();
101 } else {
102 width = inWidth;
103 }
104 if( 0 >= inHeight || drawable.getSurfaceHeight() < inHeight ) {
105 height = drawable.getSurfaceHeight();
106 } else {
107 height= inHeight;
108 }
109 if( readPixelsImpl(drawable, gl, inX, inY, width, height, awtOrientation) ) {
110 final BufferedImage image = getAWTGLPixelBuffer().getAlignedImage(width, height);
111 if( getTextureData().getMustFlipVertically() ) {
113 }
114 return image;
115 }
116 return null;
117 }
118}
abstract GLDrawable getGLReadDrawable()
Returns the read-Drawable this context uses for read framebuffer operations.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
final boolean isGL2ES3()
Indicates whether this profile is capable of GL2ES3.
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
boolean readPixelsImpl(final GLDrawable drawable, final GL gl, final int inX, final int inY, final int width, final int height, final boolean mustFlipVertically)
GLPixelBuffer getPixelBuffer()
Returns the GLPixelBuffer, created and filled by readPixels(GLAutoDrawable, boolean).
AWT GLPixelBuffer backed by an BufferedImage of type BufferedImage#TYPE_INT_ARGB or BufferedImage#TYP...
BufferedImage getAlignedImage(final int width, final int height)
Returns a width- and height-aligned image representation sharing data w/ image.
GLReadBufferUtil specialization allowing to read out a frambuffer to an AWT BufferedImage utilizing A...
AWTGLReadBufferUtil(final GLProfile glp, final boolean requestAlpha)
BufferedImage readPixelsToBufferedImage(final GL gl, final int inX, final int inY, final int inWidth, final int inHeight, final boolean awtOrientation)
Read the drawable's pixels to TextureData and Texture, if requested at construction,...
AWTGLPixelBuffer getAWTGLPixelBuffer()
Returns the AWTGLPixelBuffer, as filled by previous call to readPixels(GL, int, int,...
BufferedImage readPixelsToBufferedImage(final GL gl, final boolean awtOrientation)
Read the drawable's pixels to TextureData and Texture, if requested at construction,...
Utilities for dealing with images.
Definition: ImageUtil.java:47
static void flipImageVertically(final BufferedImage image)
Flips the supplied BufferedImage vertically.
Definition: ImageUtil.java:53
GLContext getContext()
Returns the GLContext associated which this GL object.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.