001: /*
002: * $RCSfile: RandomOpImage.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:44 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.test;
013:
014: import java.awt.Frame;
015: import java.awt.Point;
016: import java.awt.Rectangle;
017: import java.awt.image.DataBuffer;
018: import java.awt.image.Raster;
019: import java.awt.image.SampleModel;
020: import java.awt.image.WritableRaster;
021: import java.util.Map;
022: import javax.media.jai.ImageLayout;
023: import javax.media.jai.PlanarImage;
024: import javax.media.jai.RasterFactory;
025: import javax.media.jai.SourcelessOpImage;
026: import javax.media.jai.widget.ScrollingImagePanel;
027:
028: /** Defines an OpImage with random pixel values for testing purposes. */
029: final class RandomOpImage extends SourcelessOpImage {
030:
031: private int maxValue;
032: private int transtype;
033:
034: public RandomOpImage(int minX, int minY, int width, int height,
035: SampleModel sampleModel, Map configuration,
036: ImageLayout layout) {
037: super (layout, configuration, sampleModel, minX, minY, width,
038: height);
039:
040: switch (this .transtype = sampleModel.getTransferType()) {
041: case DataBuffer.TYPE_BYTE:
042: maxValue = 255;
043: break;
044: case DataBuffer.TYPE_USHORT:
045: maxValue = 65535;
046: break;
047: case DataBuffer.TYPE_SHORT:
048: maxValue = Short.MAX_VALUE;
049: break;
050: case DataBuffer.TYPE_INT:
051: maxValue = Integer.MAX_VALUE;
052: break;
053: }
054:
055: /*
056: * Fill in all pixel values so that when other OpImages uses this
057: * one for performance calculation it doesn't take away time.
058: */
059: for (int y = getMinTileY(); y <= getMaxTileY(); y++) {
060: for (int x = getMinTileX(); x <= getMaxTileX(); x++) {
061: getTile(x, y);
062: }
063: }
064: }
065:
066: public Raster computeTile(int tileX, int tileY) {
067: int orgX = tileXToX(tileX);
068: int orgY = tileYToY(tileY);
069:
070: WritableRaster dst = RasterFactory.createWritableRaster(
071: sampleModel, new Point(orgX, orgY));
072:
073: Rectangle rect = new Rectangle(orgX, orgY, sampleModel
074: .getWidth(), sampleModel.getHeight());
075: rect = rect.intersection(getBounds());
076:
077: int numBands = sampleModel.getNumBands();
078: int p[] = new int[numBands];
079:
080: for (int y = rect.y; y < (rect.y + rect.height); y++) {
081: for (int x = rect.x; x < (rect.x + rect.width); x++) {
082: for (int i = 0; i < numBands; i++) {
083: switch (transtype) {
084: case DataBuffer.TYPE_BYTE:
085: case DataBuffer.TYPE_USHORT:
086: // For unsigned data, limint the random number to [0, 1]
087: // and the result to [0, MAX_VALUE];
088: p[i] = (int) (maxValue * Math.random());
089: break;
090: default:
091: // For signed data, limint the random number to [-1, 1]
092: // and the result to [MIN_VALUE, MAX_VALUE];
093: p[i] = (int) ((maxValue + 1.0F)
094: * (Math.random() - 0.5F) * 2.0F);
095: }
096: }
097: dst.setPixel(x, y, p);
098: }
099: }
100: return dst;
101: }
102:
103: public static void main(String args[]) {
104: ImageLayout layout = new ImageLayout();
105: layout.setTileWidth(64);
106: layout.setTileHeight(64);
107: layout.setColorModel(OpImageTester.createComponentColorModel());
108:
109: PlanarImage image = new RandomOpImage(0, 0, 100, 100,
110: RasterFactory.createPixelInterleavedSampleModel(
111: DataBuffer.TYPE_BYTE, 64, 64, 3), null, layout);
112:
113: ScrollingImagePanel panel = new ScrollingImagePanel(image, 120,
114: 120);
115:
116: Frame window = new Frame("JAI RandomOpImage Test");
117: window.add(panel);
118: window.pack();
119: window.show();
120: }
121: }
|