001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/model/gridprocessing/ImgResize.java $
002    /*
003     * ImgResize.java
004     *
005     * Created on 23. Januar 2003, 12:22
006     */
007    /*----------------    FILE HEADER  ------------------------------------------
008    
009     This file is part of deegree.
010     Copyright (C) 2001-2008 by:
011     EXSE, Department of Geography, University of Bonn
012     http://www.giub.uni-bonn.de/deegree/
013     lat/lon GmbH
014     http://www.lat-lon.de
015    
016     This library is free software; you can redistribute it and/or
017     modify it under the terms of the GNU Lesser General Public
018     License as published by the Free Software Foundation; either
019     version 2.1 of the License, or (at your option) any later version.
020    
021     This library is distributed in the hope that it will be useful,
022     but WITHOUT ANY WARRANTY; without even the implied warranty of
023     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024     Lesser General Public License for more details.
025    
026     You should have received a copy of the GNU Lesser General Public
027     License along with this library; if not, write to the Free Software
028     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029    
030     Contact:
031    
032     Andreas Poth
033     lat/lon GmbH
034     Aennchenstr. 19
035     53115 Bonn
036     Germany
037     E-Mail: poth@lat-lon.de
038    
039     Prof. Dr. Klaus Greve
040     Department of Geography
041     University of Bonn
042     Meckenheimer Allee 166
043     53115 Bonn
044     Germany
045     E-Mail: greve@giub.uni-bonn.de
046    
047     
048     ---------------------------------------------------------------------------*/
049    package org.deegree.model.gridprocessing;
050    
051    /**
052     * 
053     * 
054     * 
055     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
056     * @author last edited by: $Author: apoth $
057     * 
058     * @version. $Revision: 9343 $, $Date: 2007-12-27 14:30:32 +0100 (Do, 27 Dez 2007) $
059     */
060    public class ImgResize {
061    
062        private float[][] inData = null;
063    
064        private float[][] outData = null;
065    
066        public ImgResize( float[][] data, int newWidth, int newHeight ) {
067            this.inData = data;
068            outData = new float[newWidth][newHeight];
069        }
070    
071        private int sign( int x ) {
072            int k = ( ( x ) > 0 ? 1 : -1 );
073            return k;
074        }
075    
076        /*
077         * Stretches a horizontal source line onto a horizontal destination line. Used by RectStretch.
078         * Entry: x1,x2 - x-coordinates of the destination line y1,y2 - x-coordinates of the source line
079         * yr - y-coordinate of source line yw - y-coordinate of destination line
080         */
081        private void stretch( int x1, int x2, int y1, int y2, int yr, int yw ) {
082            int dx, dy, e, d, dx2;
083            int sx, sy;
084            float value = 0;
085            dx = Math.abs( x2 - x1 );
086            dy = Math.abs( y2 - y1 );
087            sx = sign( x2 - x1 );
088            sy = sign( y2 - y1 );
089            e = ( dy << 1 ) - dx;
090            dx2 = dx << 1;
091            dy = dy << 1;
092            for ( d = 0; d <= dx; d++ ) {
093                value = inData[yr][y1];
094                outData[yw][x1] = value;
095                while ( e >= 0 ) {
096                    y1 += sy;
097                    e -= dx2;
098                }
099                x1 += sx;
100                e += dy;
101            }
102        }
103    
104        /**
105         * RectStretch enlarges or diminishes a source rectangle of a bitmap to a destination rectangle.
106         * The source rectangle is selected by the two points (xs1,ys1) and (xs2,ys2), and the
107         * destination rectangle by (xd1,yd1) and (xd2,yd2). Since readability of source-code is wanted,
108         * some optimizations have been left out for the reader: It�s possible to read one line at a
109         * time, by first stretching in x-direction and then stretching that bitmap in y-direction.
110         * Entry: xs1,ys1 - first point of source rectangle xs2,ys2 - second point of source rectangle
111         * xd1,yd1 - first point of destination rectangle xd2,yd2 - second point of destination
112         * rectangle
113         */
114        public float[][] rectStretch() {
115    
116            int xs1 = 0;
117            int ys1 = 0;
118            int xs2 = inData[0].length - 1;
119            int ys2 = inData.length - 1;
120            int xd1 = 0;
121            int yd1 = 0;
122            int xd2 = outData[0].length - 1;
123            int yd2 = outData.length - 1;
124    
125            int dx, dy, e, d, dx2;
126            int sx, sy;
127            dx = Math.abs( yd2 - yd1 );
128            dy = Math.abs( ys2 - ys1 );
129            sx = sign( yd2 - yd1 );
130            sy = sign( ys2 - ys1 );
131            e = ( dy << 1 ) - dx;
132            dx2 = dx << 1;
133            dy = dy << 1;
134            for ( d = 0; d <= dx; d++ ) {
135                stretch( xd1, xd2, xs1, xs2, ys1, yd1 );
136                while ( e >= 0 ) {
137                    ys1 += sy;
138                    e -= dx2;
139                }
140                yd1 += sx;
141                e += dy;
142            }
143            return outData;
144        }
145    
146        public float[][] simpleStretch() {
147            double dx = inData[0].length / (double) outData[0].length;
148            double dy = inData.length / (double) outData.length;
149    
150            double py = 0.0;
151            for ( int y = 0; y < outData.length; y++ ) {
152                double px = 0.0;
153                for ( int x = 0; x < outData[0].length; x++ ) {
154                    float v = inData[(int) py][(int) py];
155                    outData[y][x] = v;
156                    px += dx;
157                }
158                py += dy;
159            }
160            return outData;
161        }
162    
163    }