/*
 *	@(#)TiledImage.java 1.4 00/02/10 13:14:03
 *
 * Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

import java.awt.*;
import java.awt.image.*;
import java.util.Vector;
import java.awt.color.ColorSpace;

public class TiledImage extends Object implements RenderedImage {
    
 
    WritableRaster[][] tile = new WritableRaster[2][2];
    
    WritableRaster bigTile;
    ComponentColorModel colorModel;
    BufferedImage checkBoard;

    TiledImage() {
	ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); 
	int[] nBits = {8, 8, 8, 8};
	int i, j, k, cc = 255;
	int[] bandOffset = new int[4];
	colorModel =
	    new ComponentColorModel(cs, nBits, true, false, Transparency.OPAQUE, 0);
	// Create four tiles
	bandOffset[0] = 3;
	bandOffset[1] = 2;
	bandOffset[2] = 1;
	bandOffset[3] = 0;
	for (i = 0; i < 2; i++) {
	    for (j = 0; j < 2; j++) {
		tile[i][j] = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 8, 8 , 32, 4, bandOffset, null);
	    }
	}

	byte[] byteData = ((DataBufferByte)tile[0][0].getDataBuffer()).getData();
	// tile {0, 0}
	for (i=0, k = 0;i < 8;i++){
	    for (j=0;j < 8;j++, k+=4){
		byteData[k] = (byte)0;
		byteData[k+1] = (byte)cc; 
		byteData[k+2] = (byte)0;
		byteData[k+3] = (byte)cc ;
	    }
	}

	// tile {0.1}
	byteData = ((DataBufferByte)tile[0][1].getDataBuffer()).getData();
	for (i=0, k = 0;i < 8;i++){
	    for (j=0;j < 8;j++, k+=4){
		byteData[k] = (byte)0;
		byteData[k+1] = (byte)0; 
		byteData[k+2] = (byte)0;
		byteData[k+3] = (byte)cc ;
	    }
	}

	// tile {1,0}
	byteData = ((DataBufferByte)tile[1][0].getDataBuffer()).getData();
	for (i=0, k = 0;i < 8;i++){
	    for (j=0;j < 8;j++, k+=4){
		byteData[k] = (byte)0;
		byteData[k+1] = (byte) 0; 
		byteData[k+2] = (byte)cc;
		byteData[k+3] = (byte)0 ;
	    }
	}

	// tile {1,1}
	byteData = ((DataBufferByte)tile[1][1].getDataBuffer()).getData();
	for (i=0, k = 0;i < 8;i++){
	    for (j=0;j < 8;j++, k+=4){
		byteData[k] = (byte)0;
		byteData[k+1] = (byte) 0; 
		byteData[k+2] = (byte)cc;
		byteData[k+3] = (byte)cc ;
	    }
	}

	bigTile =  Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 16, 16 , 64, 4, bandOffset, null);;
        byteData = ((DataBufferByte)bigTile.getDataBuffer()).getData(); 
        for (i=0, k = 0;i < 8;i++){
            for (j=0;j < 8;j++, k+=4){
		byteData[k] = (byte)0;
                byteData[k+1] = (byte)cc; 
                byteData[k+2] = (byte)0;
                byteData[k+3] = (byte)cc ;
            } 
            for (;j < 16;j++, k+=4){ 
		byteData[k] = (byte)0;
                byteData[k+1] = (byte)0; 
                byteData[k+2] = (byte)0;
                byteData[k+3] = (byte)cc ;
            } 
        } 
        for (;i < 16;i++){
            for (j=0;j < 8;j++, k+=4){ 
		byteData[k] = (byte)0;
                byteData[k+1] = (byte)0; 
                byteData[k+2] = (byte)cc;
                byteData[k+3] = (byte)0;
            } 
            for (;j < 16;j++, k+=4){ 
		byteData[k] = (byte)0;
                byteData[k+1] = (byte)0; 
                byteData[k+2] = (byte)cc;
                byteData[k+3] = (byte)cc ;
            } 
        }
	checkBoard = new BufferedImage(colorModel, bigTile, false, null);
    }


    
    // create  four tiles {r, g, b, y}
    public WritableRaster copyData(WritableRaster raster) {
	return checkBoard.copyData(raster);
    }

    public ColorModel getColorModel() {
	return checkBoard.getColorModel();
    }

    public Raster getData() {
	return checkBoard.getData();
    }

    public Raster getData(Rectangle rect) {
	return checkBoard.getData(rect);
    }

    public int getHeight() {
	return 16;
    }

    public int getMinTileX() {
	return 0;
    }

    public int getMinTileY() {
	return 0;
    }

    public int getMinX () {
	return 0;
    }

    public int getMinY () {
	return 0;
    }

    public int getNumXTiles() {
	return 2;
    }

    public int getNumYTiles() {
	return 2;
    }

    public Object getProperty(String name) {
	return checkBoard.getProperty(name);
    }

    public String[] getPropertyNames() {
	return checkBoard.getPropertyNames();
    }


    public SampleModel getSampleModel() {
	return checkBoard.getSampleModel();
    }

    public Vector getSources() {
	return null;
    }

    public Raster getTile(int tileX, int tileY) {
	return tile[tileX][tileY];
    }

    public int getTileGridXOffset() {
	return 0;
    }

    public int getTileGridYOffset() {
	return 0;
    }


    public int getTileHeight() {
	return 8;
    }


    public int getTileWidth() {
	return 8;
    }

    public int getWidth() {
	return checkBoard.getWidth();
    }
}
    
