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 022public class OffsetFilter extends TransformFilter { 023 024 private int width, height; 025 private int xOffset, yOffset; 026 private boolean wrap; 027 028 public OffsetFilter() { 029 this(0, 0, true); 030 } 031 032 public OffsetFilter(int xOffset, int yOffset, boolean wrap) { 033 this.xOffset = xOffset; 034 this.yOffset = yOffset; 035 this.wrap = wrap; 036 setEdgeAction( ZERO ); 037 } 038 039 public void setXOffset(int xOffset) { 040 this.xOffset = xOffset; 041 } 042 043 public int getXOffset() { 044 return xOffset; 045 } 046 047 public void setYOffset(int yOffset) { 048 this.yOffset = yOffset; 049 } 050 051 public int getYOffset() { 052 return yOffset; 053 } 054 055 public void setWrap(boolean wrap) { 056 this.wrap = wrap; 057 } 058 059 public boolean getWrap() { 060 return wrap; 061 } 062 063 protected void transformInverse(int x, int y, float[] out) { 064 if ( wrap ) { 065 out[0] = (x+width-xOffset) % width; 066 out[1] = (y+height-yOffset) % height; 067 } else { 068 out[0] = x-xOffset; 069 out[1] = y-yOffset; 070 } 071 } 072 073 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 074 this.width = src.getWidth(); 075 this.height = src.getHeight(); 076 if ( wrap ) { 077 while (xOffset < 0) 078 xOffset += width; 079 while (yOffset < 0) 080 yOffset += height; 081 xOffset %= width; 082 yOffset %= height; 083 } 084 return super.filter( src, dst ); 085 } 086 087 public String toString() { 088 return "Distort/Offset..."; 089 } 090}