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.geom.*; 021import java.awt.image.*; 022 023/** 024 * A filter which crops an image to a given rectangle. 025 */ 026public class CropFilter extends AbstractBufferedImageOp { 027 028 private int x; 029 private int y; 030 private int width; 031 private int height; 032 033 /** 034 * Construct a CropFilter. 035 */ 036 public CropFilter() { 037 this(0, 0, 32, 32); 038 } 039 040 /** 041 * Construct a CropFilter. 042 * @param x the left edge of the crop rectangle 043 * @param y the top edge of the crop rectangle 044 * @param width the width of the crop rectangle 045 * @param height the height of the crop rectangle 046 */ 047 public CropFilter(int x, int y, int width, int height) { 048 this.x = x; 049 this.y = y; 050 this.width = width; 051 this.height = height; 052 } 053 054 /** 055 * Set the left edge of the crop rectangle. 056 * @param x the left edge of the crop rectangle 057 * @see #getX 058 */ 059 public void setX(int x) { 060 this.x = x; 061 } 062 063 /** 064 * Get the left edge of the crop rectangle. 065 * @return the left edge of the crop rectangle 066 * @see #setX 067 */ 068 public int getX() { 069 return x; 070 } 071 072 /** 073 * Set the top edge of the crop rectangle. 074 * @param y the top edge of the crop rectangle 075 * @see #getY 076 */ 077 public void setY(int y) { 078 this.y = y; 079 } 080 081 /** 082 * Get the top edge of the crop rectangle. 083 * @return the top edge of the crop rectangle 084 * @see #setY 085 */ 086 public int getY() { 087 return y; 088 } 089 090 /** 091 * Set the width of the crop rectangle. 092 * @param width the width of the crop rectangle 093 * @see #getWidth 094 */ 095 public void setWidth(int width) { 096 this.width = width; 097 } 098 099 /** 100 * Get the width of the crop rectangle. 101 * @return the width of the crop rectangle 102 * @see #setWidth 103 */ 104 public int getWidth() { 105 return width; 106 } 107 108 /** 109 * Set the height of the crop rectangle. 110 * @param height the height of the crop rectangle 111 * @see #getHeight 112 */ 113 public void setHeight(int height) { 114 this.height = height; 115 } 116 117 /** 118 * Get the height of the crop rectangle. 119 * @return the height of the crop rectangle 120 * @see #setHeight 121 */ 122 public int getHeight() { 123 return height; 124 } 125 126 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 127 int w = src.getWidth(); 128 int h = src.getHeight(); 129 130 if ( dst == null ) { 131 ColorModel dstCM = src.getColorModel(); 132 dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(width, height), dstCM.isAlphaPremultiplied(), null); 133 } 134 135 Graphics2D g = dst.createGraphics(); 136 g.drawRenderedImage( src, AffineTransform.getTranslateInstance(-x, -y) ); 137 g.dispose(); 138 139 return dst; 140 } 141 142 public String toString() { 143 return "Distort/Crop"; 144 } 145}