Ignore:
Timestamp:
2009-08-16T23:36:16+02:00 (16 years ago)
Author:
pieren
Message:

raster image feature implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/GeorefImage.java

    r15961 r17089  
    1515import java.io.ObjectOutputStream;
    1616import java.io.Serializable;
     17
    1718import javax.imageio.ImageIO;
    1819
     
    2324    private static final long serialVersionUID = 1L;
    2425
    25     public EastNorth min, max;
    26 
    27     public EastNorth org_min, org_max;
    28 
     26    public EastNorth min;
     27    public EastNorth max;
    2928    public BufferedImage image;
    3029
    31     private double angle = 0; // in radian
    32 
    33     private BufferedImage rotated_image; // only if angle <> 0
    34 
    35     double pixelPerEast;
    36     double pixelPerNorth;
     30    private double pixelPerEast;
     31    private double pixelPerNorth;
    3732
    3833    public GeorefImage(BufferedImage img, EastNorth min, EastNorth max) {
     
    4338    }
    4439
    45     public void displace(double dx, double dy) {
    46         min = new EastNorth(min.east() + dx, min.north() + dy);
    47         max = new EastNorth(max.east() + dx, max.north() + dy);
    48     }
    49 
    50     public void resize(EastNorth rasterCenter, double proportion) {
    51         min = min.interpolate(rasterCenter, proportion);
    52         max = max.interpolate(rasterCenter, proportion);
    53         updatePixelPer();
    54     }
    55 
    56     public void rotate(EastNorth pivot, double delta) {
    57         if (angle == 0) {
    58             org_min = min;
    59             org_max = max;
    60         }
    61         this.angle += delta;
    62 
    63         EastNorth imageCenter = org_min.interpolate(org_max, 0.5);
    64         EastNorth newimageCenter = imageCenter.rotate(pivot, angle);
    65         min.setLocation(org_min.east() + newimageCenter.east()-imageCenter.east(),
    66                 org_min.north() + newimageCenter.north()-imageCenter.north());
    67         max.setLocation(org_max.east() + newimageCenter.east()-imageCenter.east(),
    68                 org_max.north() + newimageCenter.north()-imageCenter.north());
    69         EastNorth min2 = new EastNorth(min.east(), max.north());
    70         EastNorth max2 = new EastNorth(max.east(), min.north());
    71         min = org_min.rotate(newimageCenter, angle);
    72         max = org_max.rotate(newimageCenter, angle);
    73         min2 = min2.rotate(newimageCenter, angle);
    74         max2 = max2.rotate(newimageCenter, angle);
    75         getNewBounding(min, max, min2, max2);
    76 
    77         rotated_image = tilt(image, angle);
    78     }
    79 
    80     public static BufferedImage tilt(BufferedImage image, double angle) {
    81         double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    82         int w = image.getWidth(), h = image.getHeight();
    83         int neww = (int)Math.floor(w*cos+h*sin), newh = (int)Math.floor(h*cos+w*sin);
    84         GraphicsConfiguration gc = getDefaultConfiguration();
    85         BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
    86         Graphics2D g = result.createGraphics();
    87         g.translate((neww-w)/2, (newh-h)/2);
    88         g.rotate(angle, w/2, h/2);
    89         g.drawRenderedImage(image, null);
    90         g.dispose();
    91         return result;
    92     }
    9340    public static GraphicsConfiguration getDefaultConfiguration() {
    9441        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     
    12774            return;
    12875
    129         BufferedImage toDisplay;
    130         if (angle != 0)
    131             toDisplay = rotated_image;
    132         else
    133             toDisplay = image;
    134 
    13576        Point minPt = nc.getPoint(min), maxPt = nc.getPoint(max);
    13677
     
    14485            g.drawRect(minPt.x, maxPt.y, maxPt.x - minPt.x, minPt.y - maxPt.y);
    14586        }
    146         g.drawImage(toDisplay, minPt.x, maxPt.y, maxPt.x, minPt.y, // dest
    147                 0, 0, toDisplay.getWidth(), toDisplay.getHeight(), // src
     87        g.drawImage(image, minPt.x, maxPt.y, maxPt.x, minPt.y, // dest
     88                0, 0, image.getWidth(), image.getHeight(), // src
    14889                null);
    14990        if (backgroundTransparent && transparency < 1.0f)
     
    183124            for (int x = minXMaskPixel; x < minXMaskPixel + widthXMaskPixel; x++)
    184125                for (int y = minYMaskPixel; y < minYMaskPixel + heightYMaskPixel; y++)
    185                     image.setRGB(x, y, ImageModifier.cadastreBackgroundTransp);
     126                    image.setRGB(x, y, VectorImageModifier.cadastreBackgroundTransp);
    186127            g.dispose();
    187128        }
    188129    }
    189130
     131    /*
     132     * Method required by BufferedImage serialization
     133     */
    190134    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    191135        max = (EastNorth) in.readObject();
     
    195139    }
    196140
    197 
    198     private void updatePixelPer() {
    199         pixelPerEast = image.getWidth()/(max.east()-min.east());
    200         pixelPerNorth = image.getHeight()/(max.north()-min.north());
    201     }
    202 
     141    /*
     142     * Method required by BufferedImage serialization
     143     */
    203144    private void writeObject(ObjectOutputStream out) throws IOException {
    204145        out.writeObject(max);
     
    207148    }
    208149
     150    private void updatePixelPer() {
     151        pixelPerEast = image.getWidth()/(max.east()-min.east());
     152        pixelPerNorth = image.getHeight()/(max.north()-min.north());
     153    }
     154
     155    public double getPixelPerEast() {
     156        return pixelPerEast;
     157    }
     158
     159    public double getPixelPerNorth() {
     160        return pixelPerNorth;
     161    }
     162
    209163    @Override
    210164    public String toString() {
     
    212166    }
    213167
     168    /*
     169     * Following methods are used for affine transformation of two points p1 and p2
     170     */
     171    /**
     172     * Add a translation (dx, dy) to this image min,max coordinates
     173     * @param dx delta added to X image coordinate
     174     * @param dy delta added to Y image coordinate
     175     */
     176    public void shear(double dx, double dy) {
     177        min = new EastNorth(min.east() + dx, min.north() + dy);
     178        max = new EastNorth(max.east() + dx, max.north() + dy);
     179    }
     180   
     181    /**
     182     * Change this image scale by moving the min,max coordinates around an anchor
     183     * @param anchor
     184     * @param proportion
     185     */
     186    public void scale(EastNorth anchor, double proportion) {
     187        min = anchor.interpolate(min, proportion);
     188        max = anchor.interpolate(max, proportion);
     189        updatePixelPer();
     190    }
     191
     192    /**
     193     * Rotate this image and its min/max coordinates around anchor point
     194     * @param anchor anchor of rotation
     195     * @param angle angle of rotation (in radians)
     196     */
     197    public void rotate(EastNorth anchor, double angle) {
     198        EastNorth min2 = new EastNorth(min.east(), max.north());
     199        EastNorth max2 = new EastNorth(max.east(), min.north());
     200        min = min.rotate(anchor, angle);
     201        max = max.rotate(anchor, angle);
     202        min2 = min2.rotate(anchor, angle);
     203        max2 = max2.rotate(anchor, angle);
     204        getNewBounding(min, max, min2, max2);
     205        image = tilt(image, angle);
     206    }
     207
     208    /**
     209     * Rotate by copying original buffered image into a new one with new dimensions
     210     * @param image
     211     * @param angle
     212     * @return
     213     */
     214    public static BufferedImage tilt(BufferedImage image, double angle) {
     215        double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
     216        int w = image.getWidth(), h = image.getHeight();
     217        int neww = (int)Math.floor(w*cos+h*sin), newh = (int)Math.floor(h*cos+w*sin);
     218        GraphicsConfiguration gc = getDefaultConfiguration();
     219        BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
     220        Graphics2D g = result.createGraphics();
     221        g.translate((neww-w)/2, (newh-h)/2);
     222        g.rotate(angle, w/2, h/2);
     223        g.drawRenderedImage(image, null);
     224        g.dispose();
     225        return result;
     226    }
     227
    214228}
Note: See TracChangeset for help on using the changeset viewer.