001: /*
002: * $RCSfile: OpImageTester.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.2 $
009: * $Date: 2005/02/24 02:07:43 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.test;
013:
014: import java.awt.Rectangle;
015: import java.awt.Transparency;
016: import java.awt.color.ColorSpace;
017: import java.awt.image.ComponentColorModel;
018: import java.awt.image.DataBuffer;
019: import java.awt.image.Raster;
020: import java.awt.image.RenderedImage;
021: import java.awt.image.SampleModel;
022: import java.awt.image.WritableRaster;
023: import java.util.Map;
024: import java.lang.reflect.Method;
025: import javax.media.jai.ImageLayout;
026: import javax.media.jai.OpImage;
027: import javax.media.jai.PlanarImage;
028: import javax.media.jai.RasterFactory;
029: import com.sun.media.jai.opimage.PatternOpImage;
030:
031: public class OpImageTester {
032:
033: long start = 0, end = 0;
034: int width, height, bands;
035: RenderedImage source;
036: private static final int allDataTypes[] = { 0, 1, 2, 3, 4, 5 };
037: private static final String dataTypeNames[] = { "BYTE ", "USHORT",
038: "SHORT ", "INT ", "FLOAT ", "DOUBLE" };
039:
040: public OpImageTester(int width, int height, int bands, int dataType) {
041: this .width = width;
042: this .height = height;
043: this .bands = bands;
044:
045: int tileWidth = width, tileHeight = height;
046: Raster srcRaster = RasterFactory.createInterleavedRaster(
047: dataType, tileWidth, tileHeight, bands, null);
048: ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
049: int bits[] = { 8, 8, 8 };
050: ComponentColorModel ccm = new ComponentColorModel(cs, bits,
051: false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
052:
053: source = new PatternOpImage(srcRaster, ccm, 0, 0, width, height);
054: }
055:
056: public OpImageTester(int width, int height, int bands) {
057: this (width, height, bands, DataBuffer.TYPE_BYTE);
058: }
059:
060: public RenderedImage getSource() {
061: return source;
062: }
063:
064: public Raster[] getSrcRasters() {
065: Raster[] r = { source.getTile(0, 0) };
066: return r;
067: }
068:
069: public WritableRaster getDstRaster() {
070: return RasterFactory.createInterleavedRaster(
071: DataBuffer.TYPE_BYTE, width, height, bands, null);
072: }
073:
074: public Rectangle getDstRect() {
075: return new Rectangle(0, 0, width, height);
076: }
077:
078: public static OpImage createRandomOpImage(ImageLayout layout) {
079: return new RandomOpImage(0, 0, 800, 800, RasterFactory
080: .createPixelInterleavedSampleModel(
081: DataBuffer.TYPE_BYTE, 200, 200, 3), null,
082: layout);
083: }
084:
085: public static OpImage createRandomOpImage(Map configuration,
086: ImageLayout layout) {
087: return new RandomOpImage(0, 0, 800, 800, RasterFactory
088: .createPixelInterleavedSampleModel(
089: DataBuffer.TYPE_BYTE, 200, 200, 3),
090: configuration, layout);
091: }
092:
093: public static ImageLayout createImageLayout(int minX, int minY,
094: int width, int height, int tileX, int tileY, int tileWidth,
095: int tileHeight, int dataType, int numBands, boolean isBanded) {
096: SampleModel sampleModel;
097: if (isBanded) {
098: sampleModel = RasterFactory.createBandedSampleModel(
099: dataType, tileWidth, tileHeight, numBands);
100: } else {
101: sampleModel = RasterFactory
102: .createPixelInterleavedSampleModel(dataType,
103: tileWidth, tileHeight, numBands);
104: }
105: return new ImageLayout(minX, minY, width, height, tileX, tileY,
106: tileWidth, tileHeight, sampleModel,
107: createComponentColorModel());
108: }
109:
110: public static ComponentColorModel createComponentColorModel() {
111: ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
112: int bits[] = { 8, 8, 8 };
113: return new ComponentColorModel(cs, bits, false, false,
114: Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
115: }
116:
117: public static void printPixels(String message, PlanarImage image,
118: Rectangle rect) {
119: int[] pixels = image.getData(rect).getPixels(rect.x, rect.y,
120: rect.width, rect.height, (int[]) null);
121:
122: System.out.println(message + " x=" + rect.x + " y=" + rect.y
123: + " width=" + rect.width + " height=" + rect.height);
124: int j = 0;
125: for (int h = 0; h < rect.height; h++) {
126: System.out.print(" ");
127: for (int w = 0; w < rect.width; w++) {
128: System.out.print("( ");
129: for (int b = 0; b < image.getSampleModel()
130: .getNumBands(); b++) {
131: System.out.print(pixels[j] + " ");
132: j++;
133: }
134: System.out.print(")");
135: }
136: System.out.println();
137: }
138: }
139:
140: /**
141: * Print out the pixel values of the destination image within a
142: * specific rectangle, and all of its source image(s) pixel values
143: * within the cooresponding rectangle obtained by calling
144: * <code>OpImage.mapDestRect()</code>.
145: *
146: * @param dst The destination image to be tested.
147: * @param dstRect The rectangle of interest within dst.
148: */
149: public static void testOpImage(OpImage dst, Rectangle dstRect) {
150: for (int i = 0; i < dst.getNumSources(); i++) {
151: PlanarImage src = dst.getSourceImage(i);
152: Rectangle srcRect = dst.mapDestRect(dstRect, i);
153: String message = "Source " + (i + 1) + ":";
154: printPixels(message, src, srcRect);
155: }
156: printPixels("Dest:", dst, dstRect);
157: }
158:
159: private static long benchmarkOpImage(OpImage img, int loop) {
160: img.setTileCache(null);
161: int minX = img.getMinTileX();
162: int maxX = img.getMaxTileX();
163: int minY = img.getMinTileY();
164: int maxY = img.getMaxTileY();
165:
166: long total = 0;
167: for (int i = 0; i < loop; i++) {
168: for (int y = minY; y <= maxY; y++) {
169: for (int x = minX; x <= maxX; x++) {
170: // System.gc();
171: long start = System.currentTimeMillis();
172: img.getTile(x, y);
173: long end = System.currentTimeMillis();
174: // System.gc();
175: int diff = (int) (end - start);
176: total += diff;
177: }
178: }
179: }
180: return total;
181: }
182:
183: public static long timeOpImage(OpImage img, int loops) {
184:
185: long total = benchmarkOpImage(img, loops);
186:
187: int w = img.getWidth();
188: int h = img.getHeight();
189: SampleModel sm = img.getSampleModel();
190:
191: double time = (total) / 1000.0;
192: int width = img.getWidth();
193: int height = img.getHeight();
194: System.out.print("\tLoops : " + loops);
195: System.out.print("\tTime : " + time);
196: System.out.println("\tMpixels/sec : "
197: + ((double) loops / 1000000.0)
198: * ((double) width * (double) height / time));
199:
200: return total;
201: }
202:
203: public static void performDiagnostics(String classname,
204: String args[]) {
205: int dataTypes[] = allDataTypes;
206: boolean verbose = false;
207: int width = 512;
208: int height = 512;
209: int bands = 1;
210: for (int i = 0; i < args.length; i++) {
211: if (args[i].equals("-fast")) {
212: int dt[] = { 0 };
213: dataTypes = dt;
214: } else if (args[i].equals("-verbose")) {
215: verbose = true;
216: }
217: }
218: runDiagnostics(classname, dataTypes, width, height, bands,
219: verbose);
220: }
221:
222: public static void runDiagnostics(String classname,
223: int dataTypes[], int width, int height, int bands,
224: boolean verbose) {
225: System.out.println("Performing Diagnostics for " + classname);
226:
227: for (int i = 0; i < dataTypes.length; i++) {
228: OpImageTester oit = new OpImageTester(512, 512, 1,
229: dataTypes[i]);
230: Class clazz = null;
231: Method createTestMethod = null;
232: System.out.print(" Testing DataBuffer.TYPE_"
233: + dataTypeNames[dataTypes[i]] + " ");
234: System.out.println(" Size : " + width + "x" + height
235: + " by " + bands + " bands");
236: try {
237: clazz = Class.forName(classname);
238: Class params[] = { Class
239: .forName("com.sun.media.jai.opimage.OpImageTester") };
240: createTestMethod = clazz.getMethod("createTestImage",
241: params);
242: Object methodArgs[] = { oit };
243: OpImage o = (OpImage) createTestMethod.invoke(null,
244: methodArgs);
245:
246: long total = benchmarkOpImage(o, 10);
247: total = benchmarkOpImage(o, 10);
248:
249: int loops = (int) (15000 / ((double) total / 10.0));
250:
251: timeOpImage(o, loops);
252:
253: } catch (Exception e) {
254: if (verbose) {
255: e.printStackTrace();
256: }
257: System.err.println("\tException thrown");
258: }
259: }
260: System.out.println("Finished Diagnostics\n");
261: }
262: }
|