001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/model/gridprocessing/ImgResize.java $ 002 /* 003 * ImgResize.java 004 * 005 * Created on 23. Januar 2003, 12:22 006 */ 007 008 package org.deegree.model.gridprocessing; 009 010 /** 011 * 012 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 013 * @version 22.1.2003 014 */ 015 public class ImgResize { 016 017 private float[][] inData = null; 018 private float[][] outData = null; 019 020 public ImgResize(float[][] data, int newWidth, int newHeight) { 021 this.inData = data; 022 outData = new float[newWidth][newHeight]; 023 } 024 025 private int sign(int x) { 026 int k = ( (x) > 0 ? 1:-1); 027 return k; 028 } 029 030 /* 031 * Stretches a horizontal source line onto a horizontal 032 * destination line. Used by RectStretch. 033 * Entry: 034 * x1,x2 - x-coordinates of the destination line 035 * y1,y2 - x-coordinates of the source line 036 * yr - y-coordinate of source line 037 * yw - y-coordinate of destination line 038 */ 039 private void stretch(int x1,int x2,int y1,int y2,int yr,int yw) { 040 int dx,dy,e,d,dx2; 041 int sx,sy; 042 float value = 0; 043 dx = Math.abs(x2-x1); 044 dy = Math.abs(y2-y1); 045 sx = sign(x2-x1); 046 sy = sign(y2-y1); 047 e=( dy << 1 ) - dx; 048 dx2 = dx << 1; 049 dy = dy << 1; 050 for( d = 0; d <= dx; d++ ) { 051 value = inData[yr][y1]; 052 outData[yw][x1] = value; 053 while(e >= 0 ) { 054 y1 += sy; 055 e -= dx2; 056 } 057 x1 += sx; 058 e += dy; 059 } 060 } 061 062 /** 063 * RectStretch enlarges or diminishes a source rectangle of 064 * a bitmap to a destination rectangle. The source 065 * rectangle is selected by the two points (xs1,ys1) and 066 * (xs2,ys2), and the destination rectangle by (xd1,yd1) and 067 * (xd2,yd2). Since readability of source-code is wanted, 068 * some optimizations have been left out for the reader: 069 * It�s possible to read one line at a time, by first 070 * stretching in x-direction and then stretching that bitmap 071 * in y-direction. 072 * Entry: 073 * xs1,ys1 - first point of source rectangle 074 * xs2,ys2 - second point of source rectangle 075 * xd1,yd1 - first point of destination rectangle 076 * xd2,yd2 - second point of destination rectangle 077 */ 078 public float[][] rectStretch() { 079 080 int xs1 = 0; 081 int ys1 = 0; 082 int xs2 = inData[0].length-1; 083 int ys2 = inData.length-1; 084 int xd1 = 0; 085 int yd1 = 0; 086 int xd2 = outData[0].length-1; 087 int yd2 = outData.length-1; 088 089 int dx,dy,e,d,dx2; 090 int sx,sy; 091 dx = Math.abs(yd2-yd1); 092 dy = Math.abs(ys2-ys1); 093 sx = sign(yd2-yd1); 094 sy = sign(ys2-ys1); 095 e =(dy << 1) - dx; 096 dx2 = dx << 1; 097 dy = dy << 1; 098 for(d = 0; d <= dx; d++) { 099 stretch(xd1,xd2,xs1,xs2,ys1,yd1); 100 while( e >= 0) { 101 ys1 += sy; 102 e -= dx2; 103 } 104 yd1 += sx; 105 e += dy; 106 } 107 return outData; 108 } 109 110 public float[][] simpleStretch() { 111 double dx = inData[0].length / (double)outData[0].length; 112 double dy = inData.length / (double)outData.length; 113 114 double py = 0.0; 115 for (int y = 0; y < outData.length; y++) { 116 double px = 0.0; 117 for (int x = 0; x < outData[0].length; x++) { 118 float v = inData[ (int)py ][ (int)py]; 119 outData[ y ][ x ] = v; 120 px += dx; 121 } 122 py += dy; 123 } 124 return outData; 125 } 126 127 128 129 } 130 /* ******************************************************************** 131 Changes to this class. What the people have been up to: 132 $Log$ 133 Revision 1.2 2006/07/12 14:46:19 poth 134 comment footer added 135 136 ********************************************************************** */