001/* 002Copyright 2006 Jerry Huxtable 003 004Licensed under the Apache License, Version 2.0 (the "License"); 005you may not use this file except in compliance with the License. 006You may obtain a copy of the License at 007 008 http://www.apache.org/licenses/LICENSE-2.0 009 010Unless required by applicable law or agreed to in writing, software 011distributed under the License is distributed on an "AS IS" BASIS, 012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013See the License for the specific language governing permissions and 014limitations under the License. 015*/ 016 017package com.jhlabs.image; 018 019import java.awt.*; 020import java.awt.image.*; 021 022/** 023 * A filter which tiles an image into a lerger one. 024 */ 025public class TileImageFilter extends AbstractBufferedImageOp { 026 027 private int width; 028 private int height; 029 private int tileWidth; 030 private int tileHeight; 031 032 /** 033 * Construct a TileImageFilter. 034 */ 035 public TileImageFilter() { 036 this(32, 32); 037 } 038 039 /** 040 * Construct a TileImageFilter. 041 * @param width the output image width 042 * @param height the output image height 043 */ 044 public TileImageFilter(int width, int height) { 045 this.width = width; 046 this.height = height; 047 } 048 049 /** 050 * Set the output image width. 051 * @param width the width 052 * @see #getWidth 053 */ 054 public void setWidth(int width) { 055 this.width = width; 056 } 057 058 /** 059 * Get the output image width. 060 * @return the width 061 * @see #setWidth 062 */ 063 public int getWidth() { 064 return width; 065 } 066 067 /** 068 * Set the output image height. 069 * @param height the height 070 * @see #getHeight 071 */ 072 public void setHeight(int height) { 073 this.height = height; 074 } 075 076 /** 077 * Get the output image height. 078 * @return the height 079 * @see #setHeight 080 */ 081 public int getHeight() { 082 return height; 083 } 084 085 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 086 int tileWidth = src.getWidth(); 087 int tileHeight = src.getHeight(); 088 089 if ( dst == null ) { 090 ColorModel dstCM = src.getColorModel(); 091 dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(width, height), dstCM.isAlphaPremultiplied(), null); 092 } 093 094 Graphics2D g = dst.createGraphics(); 095 for ( int y = 0; y < height; y += tileHeight) { 096 for ( int x = 0; x < width; x += tileWidth ) { 097 g.drawImage( src, null, x, y ); 098 } 099 } 100 g.dispose(); 101 102 return dst; 103 } 104 105 public String toString() { 106 return "Tile"; 107 } 108}