001: /*
002: * $RCSfile: TiledImage.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:40 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.geometry_by_ref;
046:
047: import java.awt.*;
048: import java.awt.image.*;
049: import java.util.Vector;
050: import java.awt.color.ColorSpace;
051:
052: public class TiledImage extends Object implements RenderedImage {
053:
054: WritableRaster[][] tile = new WritableRaster[3][3];
055:
056: WritableRaster bigTile;
057: ComponentColorModel colorModel;
058: BufferedImage checkBoard;
059: int minX = -2;
060: int minY = -1;
061:
062: TiledImage() {
063: ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
064: int[] nBits = { 8, 8, 8, 8 };
065: int i, j, k, cc = 255;
066: int[] bandOffset = new int[4];
067: colorModel = new ComponentColorModel(cs, nBits, true, false,
068: Transparency.OPAQUE, 0);
069: // Create 9 tiles
070: bandOffset[0] = 3;
071: bandOffset[1] = 2;
072: bandOffset[2] = 1;
073: bandOffset[3] = 0;
074: for (i = 0; i < 3; i++) {
075: for (j = 0; j < 3; j++) {
076: tile[i][j] = Raster.createInterleavedRaster(
077: DataBuffer.TYPE_BYTE, 8, 8, 32, 4, bandOffset,
078: null);
079: }
080: }
081:
082: // tile {-2, -1}
083: byte[] byteData = ((DataBufferByte) tile[0][0].getDataBuffer())
084: .getData();
085: for (i = 4, k = 8 * 4 * 4 + 4 * 4; i < 8; i++, k += 16) {
086: for (j = 4; j < 8; j++, k += 4) {
087: byteData[k] = (byte) 0;
088: byteData[k + 1] = (byte) cc;
089: byteData[k + 2] = (byte) 0;
090: byteData[k + 3] = (byte) cc;
091: }
092: }
093:
094: // tile {-1, -1}
095: byteData = ((DataBufferByte) tile[1][0].getDataBuffer())
096: .getData();
097: for (i = 4, k = 8 * 4 * 4; i < 8; i++) {
098: for (j = 4; j < 8; j++, k += 4) {
099: byteData[k] = (byte) 0;
100: byteData[k + 1] = (byte) cc;
101: byteData[k + 2] = (byte) 0;
102: byteData[k + 3] = (byte) cc;
103: }
104: for (j = 4; j < 8; j++, k += 4) {
105: byteData[k] = (byte) 0;
106: byteData[k + 1] = (byte) 0;
107: byteData[k + 2] = (byte) 0;
108: byteData[k + 3] = (byte) cc;
109: }
110: }
111:
112: // tile {1, -1}
113: byteData = ((DataBufferByte) tile[2][0].getDataBuffer())
114: .getData();
115: for (i = 4, k = 8 * 4 * 4; i < 8; i++, k += 16) {
116: for (j = 0; j < 4; j++, k += 4) {
117: byteData[k] = (byte) 0;
118: byteData[k + 1] = (byte) 0;
119: byteData[k + 2] = (byte) 0;
120: byteData[k + 3] = (byte) cc;
121: }
122: }
123:
124: // tile {-2, 0}
125: byteData = ((DataBufferByte) tile[0][1].getDataBuffer())
126: .getData();
127: for (i = 0, k = 16; i < 4; i++, k += 16) {
128: for (j = 4; j < 8; j++, k += 4) {
129: byteData[k] = (byte) 0;
130: byteData[k + 1] = (byte) cc;
131: byteData[k + 2] = (byte) 0;
132: byteData[k + 3] = (byte) cc;
133: }
134: }
135: for (i = 4, k = 8 * 4 * 4 + 16; i < 8; i++, k += 16) {
136: for (j = 4; j < 8; j++, k += 4) {
137: byteData[k] = (byte) 0;
138: byteData[k + 1] = (byte) 0;
139: byteData[k + 2] = (byte) cc;
140: byteData[k + 3] = (byte) 0;
141: }
142: }
143: // tile {-1, 0}
144: byteData = ((DataBufferByte) tile[1][1].getDataBuffer())
145: .getData();
146: for (i = 0, k = 0; i < 4; i++) {
147: for (j = 4; j < 8; j++, k += 4) {
148: byteData[k] = (byte) 0;
149: byteData[k + 1] = (byte) cc;
150: byteData[k + 2] = (byte) 0;
151: byteData[k + 3] = (byte) cc;
152: }
153: for (j = 4; j < 8; j++, k += 4) {
154: byteData[k] = (byte) 0;
155: byteData[k + 1] = (byte) 0;
156: byteData[k + 2] = (byte) 0;
157: byteData[k + 3] = (byte) cc;
158: }
159: }
160: for (i = 0, k = 8 * 4 * 4; i < 4; i++) {
161: for (j = 4; j < 8; j++, k += 4) {
162: byteData[k] = (byte) 0;
163: byteData[k + 1] = (byte) 0;
164: byteData[k + 2] = (byte) cc;
165: byteData[k + 3] = (byte) 0;
166: }
167:
168: for (j = 4; j < 8; j++, k += 4) {
169: byteData[k] = (byte) 0;
170: byteData[k + 1] = (byte) 0;
171: byteData[k + 2] = (byte) cc;
172: byteData[k + 3] = (byte) cc;
173: }
174:
175: }
176:
177: // tile {0, 0}
178: byteData = ((DataBufferByte) tile[2][1].getDataBuffer())
179: .getData();
180: for (i = 0, k = 0; i < 4; i++, k += 16) {
181: for (j = 4; j < 8; j++, k += 4) {
182: byteData[k] = (byte) 0;
183: byteData[k + 1] = (byte) 0;
184: byteData[k + 2] = (byte) 0;
185: byteData[k + 3] = (byte) cc;
186: }
187: }
188: for (i = 4, k = 8 * 4 * 4; i < 8; i++, k += 16) {
189: for (j = 4; j < 8; j++, k += 4) {
190: byteData[k] = (byte) 0;
191: byteData[k + 1] = (byte) 0;
192: byteData[k + 2] = (byte) cc;
193: byteData[k + 3] = (byte) cc;
194: }
195: }
196:
197: // tile {-2, 1}
198: byteData = ((DataBufferByte) tile[0][2].getDataBuffer())
199: .getData();
200: for (i = 4, k = 16; i < 8; i++, k += 16) {
201: for (j = 4; j < 8; j++, k += 4) {
202: byteData[k] = (byte) 0;
203: byteData[k + 1] = (byte) 0;
204: byteData[k + 2] = (byte) cc;
205: byteData[k + 3] = (byte) 0;
206: }
207: }
208:
209: // tile {-1, 1}
210: byteData = ((DataBufferByte) tile[1][2].getDataBuffer())
211: .getData();
212: for (i = 0, k = 0; i < 8; i++) {
213: for (j = 4; j < 8; j++, k += 4) {
214: byteData[k] = (byte) 0;
215: byteData[k + 1] = (byte) 0;
216: byteData[k + 2] = (byte) cc;
217: byteData[k + 3] = (byte) 0;
218: }
219: for (j = 4; j < 8; j++, k += 4) {
220: byteData[k] = (byte) 0;
221: byteData[k + 1] = (byte) 0;
222: byteData[k + 2] = (byte) cc;
223: byteData[k + 3] = (byte) cc;
224: }
225: }
226:
227: // tile {0, 1}
228: byteData = ((DataBufferByte) tile[2][2].getDataBuffer())
229: .getData();
230: for (i = 4, k = 0; i < 8; i++, k += 16) {
231: for (j = 4; j < 8; j++, k += 4) {
232: byteData[k] = (byte) 0;
233: byteData[k + 1] = (byte) 0;
234: byteData[k + 2] = (byte) cc;
235: byteData[k + 3] = (byte) cc;
236: }
237: }
238:
239: bigTile = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
240: 16, 16, 64, 4, bandOffset, null);
241: ;
242: byteData = ((DataBufferByte) bigTile.getDataBuffer()).getData();
243: for (i = 0, k = 0; i < 8; i++) {
244: for (j = 0; j < 8; j++, k += 4) {
245: byteData[k] = (byte) 0;
246: byteData[k + 1] = (byte) cc;
247: byteData[k + 2] = (byte) 0;
248: byteData[k + 3] = (byte) cc;
249: }
250: for (; j < 16; j++, k += 4) {
251: byteData[k] = (byte) 0;
252: byteData[k + 1] = (byte) 0;
253: byteData[k + 2] = (byte) 0;
254: byteData[k + 3] = (byte) cc;
255: }
256: }
257: for (; i < 16; i++) {
258: for (j = 0; j < 8; j++, k += 4) {
259: byteData[k] = (byte) 0;
260: byteData[k + 1] = (byte) 0;
261: byteData[k + 2] = (byte) cc;
262: byteData[k + 3] = (byte) 0;
263: }
264: for (; j < 16; j++, k += 4) {
265: byteData[k] = (byte) 0;
266: byteData[k + 1] = (byte) 0;
267: byteData[k + 2] = (byte) cc;
268: byteData[k + 3] = (byte) cc;
269: }
270: }
271: checkBoard = new BufferedImage(colorModel, bigTile, false, null);
272: }
273:
274: // create four tiles {r, g, b, y}
275: public WritableRaster copyData(WritableRaster raster) {
276: return checkBoard.copyData(raster);
277: }
278:
279: public ColorModel getColorModel() {
280: return checkBoard.getColorModel();
281: }
282:
283: public Raster getData() {
284: return checkBoard.getData();
285: }
286:
287: public Raster getData(Rectangle rect) {
288: return checkBoard.getData(rect);
289: }
290:
291: public int getHeight() {
292: return 16;
293: }
294:
295: public int getMinTileX() {
296: return minX;
297: }
298:
299: public int getMinTileY() {
300: return minY;
301: }
302:
303: public int getMinX() {
304: return -8;
305: }
306:
307: public int getMinY() {
308: return -8;
309: }
310:
311: public int getNumXTiles() {
312: return 3;
313: }
314:
315: public int getNumYTiles() {
316: return 3;
317: }
318:
319: public Object getProperty(String name) {
320: return checkBoard.getProperty(name);
321: }
322:
323: public String[] getPropertyNames() {
324: return checkBoard.getPropertyNames();
325: }
326:
327: public SampleModel getSampleModel() {
328: return checkBoard.getSampleModel();
329: }
330:
331: public Vector getSources() {
332: return null;
333: }
334:
335: public Raster getTile(int tileX, int tileY) {
336: return tile[tileX - minX][tileY - minY];
337: }
338:
339: public int getTileGridXOffset() {
340: return 4;
341: }
342:
343: public int getTileGridYOffset() {
344: return -4;
345: }
346:
347: public int getTileHeight() {
348: return 8;
349: }
350:
351: public int getTileWidth() {
352: return 8;
353: }
354:
355: public int getWidth() {
356: return 16;
357: }
358: }
|