001: /*
002: * $RCSfile: ImageOps.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:21:54 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.texture_by_ref;
046:
047: import java.awt.*;
048: import java.awt.image.*;
049: import java.awt.color.*;
050:
051: // some useful, static image operations
052:
053: public class ImageOps {
054:
055: // flip the image
056: public static void flipImage(BufferedImage bImage) {
057: int width = bImage.getWidth();
058: int height = bImage.getHeight();
059: int[] rgbArray = new int[width * height];
060: bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
061: int[] tempArray = new int[width * height];
062: int y2 = 0;
063: for (int y = height - 1; y >= 0; y--) {
064: for (int x = 0; x < width; x++) {
065: tempArray[y2 * width + x] = rgbArray[y * width + x];
066: }
067: y2++;
068: }
069: bImage.setRGB(0, 0, width, height, tempArray, 0, width);
070: }
071:
072: // convert the image to a specified BufferedImage type and return it
073: public static BufferedImage convertImage(BufferedImage bImage,
074: int type) {
075: int width = bImage.getWidth();
076: int height = bImage.getHeight();
077: BufferedImage newImage = new BufferedImage(width, height, type);
078: int[] rgbArray = new int[width * height];
079: bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
080: newImage.setRGB(0, 0, width, height, rgbArray, 0, width);
081: return newImage;
082: }
083:
084: // print out some of the types of BufferedImages
085: static void printType(BufferedImage bImage) {
086: int type = bImage.getType();
087: if (type == BufferedImage.TYPE_4BYTE_ABGR) {
088: System.out.println("TYPE_4BYTE_ABGR");
089: } else if (type == BufferedImage.TYPE_INT_ARGB) {
090: System.out.println("TYPE_INT_ARGB");
091: } else if (type == BufferedImage.TYPE_3BYTE_BGR) {
092: System.out.println("TYPE_3BYTE_BGR");
093: } else if (type == BufferedImage.TYPE_CUSTOM) {
094: System.out.println("TYPE_CUSTOM");
095: } else
096: System.out.println(type);
097: }
098:
099: public static BufferedImage convertToCustomRGBA(BufferedImage bImage) {
100: if (bImage.getType() != BufferedImage.TYPE_INT_ARGB) {
101: ImageOps.convertImage(bImage, BufferedImage.TYPE_INT_ARGB);
102: }
103:
104: int width = bImage.getWidth();
105: int height = bImage.getHeight();
106:
107: ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
108: int[] nBits = { 8, 8, 8, 8 };
109: ColorModel cm = new ComponentColorModel(cs, nBits, true, false,
110: Transparency.OPAQUE, 0);
111: int[] bandOffset = { 0, 1, 2, 3 };
112:
113: WritableRaster newRaster = Raster.createInterleavedRaster(
114: DataBuffer.TYPE_BYTE, width, height, width * 4, 4,
115: bandOffset, null);
116: byte[] byteData = ((DataBufferByte) newRaster.getDataBuffer())
117: .getData();
118: Raster origRaster = bImage.getData();
119: int[] pixel = new int[4];
120: int k = 0;
121: for (int j = 0; j < height; j++) {
122: for (int i = 0; i < width; i++) {
123: pixel = origRaster.getPixel(i, j, pixel);
124: byteData[k++] = (byte) (pixel[0]);
125: byteData[k++] = (byte) (pixel[1]);
126: byteData[k++] = (byte) (pixel[2]);
127: byteData[k++] = (byte) (pixel[3]);
128: }
129: }
130: BufferedImage newImage = new BufferedImage(cm, newRaster,
131: false, null);
132: // if (newImage.getType() == BufferedImage.TYPE_CUSTOM) {
133: // System.out.println("Type is custom");
134: // }
135: return newImage;
136: }
137:
138: public static BufferedImage convertToCustomRGB(BufferedImage bImage) {
139: if (bImage.getType() != BufferedImage.TYPE_INT_ARGB) {
140: ImageOps.convertImage(bImage, BufferedImage.TYPE_INT_ARGB);
141: }
142:
143: int width = bImage.getWidth();
144: int height = bImage.getHeight();
145:
146: ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
147: int[] nBits = { 8, 8, 8 };
148: ColorModel cm = new ComponentColorModel(cs, nBits, false,
149: false, Transparency.OPAQUE, 0);
150: int[] bandOffset = { 0, 1, 2 };
151:
152: WritableRaster newRaster = Raster.createInterleavedRaster(
153: DataBuffer.TYPE_BYTE, width, height, width * 3, 3,
154: bandOffset, null);
155: byte[] byteData = ((DataBufferByte) newRaster.getDataBuffer())
156: .getData();
157: Raster origRaster = bImage.getData();
158: int[] pixel = new int[4];
159: int k = 0;
160: for (int j = 0; j < height; j++) {
161: for (int i = 0; i < width; i++) {
162: pixel = origRaster.getPixel(i, j, pixel);
163: byteData[k++] = (byte) (pixel[0]);
164: byteData[k++] = (byte) (pixel[1]);
165: byteData[k++] = (byte) (pixel[2]);
166: }
167: }
168: BufferedImage newImage = new BufferedImage(cm, newRaster,
169: false, null);
170: // if (newImage.getType() == BufferedImage.TYPE_CUSTOM) {
171: // System.out.println("Type is custom");
172: // }
173: return newImage;
174: }
175: }
|