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