01: /*
02: * $RCSfile: CheckerboardOpImage.java,v $
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
05: *
06: * Use is subject to license terms.
07: *
08: * $Revision: 1.2 $
09: * $Date: 2005/02/24 02:07:43 $
10: * $State: Exp $
11: */
12: package com.sun.media.jai.test;
13:
14: import java.awt.Point;
15: import java.awt.Rectangle;
16: import java.awt.image.DataBuffer;
17: import java.awt.image.Raster;
18: import java.awt.image.SampleModel;
19: import java.awt.image.WritableRaster;
20: import java.util.Map;
21: import javax.media.jai.ImageLayout;
22: import javax.media.jai.SourcelessOpImage;
23: import javax.media.jai.RasterFactory;
24:
25: /** Defines a checkerboard image for testing purpose. */
26: final class CheckerboardOpImage extends SourcelessOpImage {
27:
28: private int increment;
29:
30: private int checkerSize;
31:
32: private int numColors;
33:
34: /** Defines a checkerboard image of several grey shades. */
35: public CheckerboardOpImage(int minX, int minY, int width,
36: int height, SampleModel sampleModel, Map configuration,
37: ImageLayout layout, int checkerSize, int numColors) {
38: super (layout, configuration, sampleModel, minX, minY, width,
39: height);
40:
41: if (numColors < 2) {
42: numColors = 2;
43: }
44: this .checkerSize = checkerSize;
45: this .numColors = numColors;
46:
47: switch (sampleModel.getTransferType()) {
48: case DataBuffer.TYPE_BYTE:
49: increment = 255 / (numColors - 1);
50: break;
51: case DataBuffer.TYPE_USHORT:
52: increment = 65535 / (numColors - 1);
53: break;
54: case DataBuffer.TYPE_SHORT:
55: increment = Short.MAX_VALUE / (numColors - 1);
56: break;
57: case DataBuffer.TYPE_INT:
58: increment = Integer.MAX_VALUE / (numColors - 1);
59: break;
60: }
61: }
62:
63: public Raster computeTile(int tileX, int tileY) {
64: int orgX = tileXToX(tileX);
65: int orgY = tileYToY(tileY);
66:
67: WritableRaster dst = RasterFactory.createWritableRaster(
68: sampleModel, new Point(orgX, orgY));
69:
70: Rectangle rect = new Rectangle(orgX, orgY, sampleModel
71: .getWidth(), sampleModel.getHeight());
72: rect = rect.intersection(getBounds());
73:
74: int numBands = sampleModel.getNumBands();
75: int p[] = new int[numBands];
76:
77: for (int y = rect.y; y < (rect.y + rect.height); y++) {
78: for (int x = rect.x; x < (rect.x + rect.width); x++) {
79: int value = getPixelValue(x, y);
80: for (int i = 0; i < numBands; i++) {
81: p[i] = value;
82: }
83: dst.setPixel(x, y, p);
84: }
85: }
86: return dst;
87: }
88:
89: private int getPixelValue(int x, int y) {
90: int squareX = x / checkerSize;
91: int squareY = y / checkerSize;
92: return (increment * ((squareX + squareY) % numColors));
93: }
94: }
|