JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
ImageUtil.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistribution of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * Neither the name of Sun Microsystems, Inc. or the names of
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * This software is provided "AS IS," without a warranty of any kind. ALL
20 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
23 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
24 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
25 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
26 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
27 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
28 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
29 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
30 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31 *
32 * You acknowledge that this software is not designed or intended for use
33 * in the design, construction, operation or maintenance of any nuclear
34 * facility.
35 *
36 * Sun gratefully acknowledges that this software was originally authored
37 * and developed by Kenneth Bradley Russell and Christopher John Kline.
38 */
39
40package com.jogamp.opengl.util.awt;
41
42import java.awt.*;
43import java.awt.image.*;
44
45/** Utilities for dealing with images. */
46
47public class ImageUtil {
48 private ImageUtil() {}
49
50 /** Flips the supplied BufferedImage vertically. This is often a
51 necessary conversion step to display a Java2D image correctly
52 with OpenGL and vice versa. */
53 public static void flipImageVertically(final BufferedImage image) {
54 final WritableRaster raster = image.getRaster();
55 Object scanline1 = null;
56 Object scanline2 = null;
57
58 for (int i = 0; i < image.getHeight() / 2; i++) {
59 scanline1 = raster.getDataElements(0, i, image.getWidth(), 1, scanline1);
60 scanline2 = raster.getDataElements(0, image.getHeight() - i - 1, image.getWidth(), 1, scanline2);
61 raster.setDataElements(0, i, image.getWidth(), 1, scanline2);
62 raster.setDataElements(0, image.getHeight() - i - 1, image.getWidth(), 1, scanline1);
63 }
64 }
65
66 /**
67 * Creates a <code>BufferedImage</code> with a pixel format compatible with the graphics
68 * environment. The returned image can thus benefit from hardware accelerated operations
69 * in Java2D API.
70 *
71 * @param width The width of the image to be created
72 * @param height The height of the image to be created
73 *
74 * @return A instance of <code>BufferedImage</code> with a type compatible with the graphics card.
75 */
76 public static BufferedImage createCompatibleImage(final int width, final int height) {
77 final GraphicsConfiguration configuration =
78 GraphicsEnvironment.getLocalGraphicsEnvironment().
79 getDefaultScreenDevice().getDefaultConfiguration();
80 return configuration.createCompatibleImage(width, height);
81 }
82
83 /**
84 * Creates a thumbnail from an image. A thumbnail is a scaled down version of the original picture.
85 * This method will retain the width to height ratio of the original picture and return a new
86 * instance of <code>BufferedImage</code>. The original picture is not modified.
87 *
88 * @param image The original image to sample down
89 * @param thumbWidth The width of the thumbnail to be created
90 *
91 * @throws IllegalArgumentException If thumbWidth is greater than image.getWidth()
92 *
93 * @return A thumbnail with the requested width or the original picture if thumbWidth = image.getWidth()
94 */
95 public static BufferedImage createThumbnail(final BufferedImage image, final int thumbWidth) {
96 // Thanks to Romain Guy for this utility
97 if (thumbWidth > image.getWidth()) {
98 throw new IllegalArgumentException("Thumbnail width must be greater than image width");
99 }
100
101 if (thumbWidth == image.getWidth()) {
102 return image;
103 }
104
105 final float ratio = (float) image.getWidth() / (float) image.getHeight();
106 int width = image.getWidth();
107 BufferedImage thumb = image;
108
109 do {
110 width /= 2;
111 if (width < thumbWidth) {
112 width = thumbWidth;
113 }
114
115 final BufferedImage temp = createCompatibleImage(width, (int) (width / ratio));
116 final Graphics2D g2 = temp.createGraphics();
117 g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
118 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
119 g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
120 g2.dispose();
121 thumb = temp;
122 } while (width != thumbWidth);
123
124 return thumb;
125 }
126
127}
Utilities for dealing with images.
Definition: ImageUtil.java:47
static BufferedImage createCompatibleImage(final int width, final int height)
Creates a BufferedImage with a pixel format compatible with the graphics environment.
Definition: ImageUtil.java:76
static void flipImageVertically(final BufferedImage image)
Flips the supplied BufferedImage vertically.
Definition: ImageUtil.java:53
static BufferedImage createThumbnail(final BufferedImage image, final int thumbWidth)
Creates a thumbnail from an image.
Definition: ImageUtil.java:95