Changeset 11895 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2017-04-13T13:07:03+02:00 (7 years ago)
Author:
bastiK
Message:

see #7427 - avoid code duplication (loop over colors)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageWarp.java

    r11891 r11895  
    22package org.openstreetmap.josm.tools;
    33
    4 import java.awt.Color;
    54import java.awt.Dimension;
    65import java.awt.geom.Point2D;
     
    7271                Point2D srcCoord = invTransform.transform(new Point2D.Double(i, j));
    7372                if (isInside(srcCoord, srcRect, interpolation.getMargin())) {
    74                         int rgb;
    75                         switch (interpolation) {
    76                             case NEAREST_NEIGHBOR:
    77                                 rgb = getColor((int) Math.round(srcCoord.getX()), (int) Math.round(srcCoord.getY()), srcImg).getRGB();
    78                                 break;
    79                             case BILINEAR:
    80                                 int x0 = (int) Math.floor(srcCoord.getX());
    81                                 double dx = srcCoord.getX() - x0;
    82                                 int y0 = (int) Math.floor(srcCoord.getY());
    83                                 double dy = srcCoord.getY() - y0;
    84                                 Color c00 = getColor(x0, y0, srcImg);
    85                                 Color c01 = getColor(x0, y0 + 1, srcImg);
    86                                 Color c10 = getColor(x0 + 1, y0, srcImg);
    87                                 Color c11 = getColor(x0 + 1, y0 + 1, srcImg);
    88                                 int red = (int) Math.round(
    89                                         (c00.getRed() * (1-dx) + c10.getRed() * dx) * (1-dy) +
    90                                         (c01.getRed() * (1-dx) + c11.getRed() * dx) * dy);
    91                                 int green = (int) Math.round(
    92                                         (c00.getGreen()* (1-dx) + c10.getGreen() * dx) * (1-dy) +
    93                                         (c01.getGreen() * (1-dx) + c11.getGreen() * dx) * dy);
    94                                 int blue = (int) Math.round(
    95                                         (c00.getBlue()* (1-dx) + c10.getBlue() * dx) * (1-dy) +
    96                                         (c01.getBlue() * (1-dx) + c11.getBlue() * dx) * dy);
    97                                 int alpha = (int) Math.round(
    98                                         (c00.getAlpha()* (1-dx) + c10.getAlpha() * dx) * (1-dy) +
    99                                         (c01.getAlpha() * (1-dx) + c11.getAlpha() * dx) * dy);
    100                                 rgb = new Color(red, green, blue, alpha).getRGB();
    101                                 break;
    102                             default:
    103                                 throw new AssertionError();
    104                         }
    105                         imgTarget.setRGB(i, j, rgb);
     73                    int rgba;
     74                    switch (interpolation) {
     75                        case NEAREST_NEIGHBOR:
     76                            rgba = getColor((int) Math.round(srcCoord.getX()), (int) Math.round(srcCoord.getY()), srcImg);
     77                            break;
     78                        case BILINEAR:
     79                            int x0 = (int) Math.floor(srcCoord.getX());
     80                            double dx = srcCoord.getX() - x0;
     81                            int y0 = (int) Math.floor(srcCoord.getY());
     82                            double dy = srcCoord.getY() - y0;
     83                            int c00 = getColor(x0, y0, srcImg);
     84                            int c01 = getColor(x0, y0 + 1, srcImg);
     85                            int c10 = getColor(x0 + 1, y0, srcImg);
     86                            int c11 = getColor(x0 + 1, y0 + 1, srcImg);
     87                            rgba = 0;
     88                            // loop over color components: blue, green, red, alpha
     89                            for (int ch = 0; ch <= 3; ch++) {
     90                                int shift = 8 * ch;
     91                                int chVal = (int) Math.round(
     92                                    (((c00 >> shift) & 0xff) * (1-dx) + ((c10 >> shift) & 0xff) * dx) * (1-dy) +
     93                                    (((c01 >> shift) & 0xff) * (1-dx) + ((c11 >> shift) & 0xff) * dx) * dy);
     94                                rgba |= chVal << shift;
     95                            }
     96                            break;
     97                        default:
     98                            throw new AssertionError();
     99                    }
     100                    imgTarget.setRGB(i, j, rgba);
    106101                }
    107102            }
     
    119114    }
    120115
    121     private static int clamp(int i, int max) {
    122         if (i < 0) {
    123             return 0;
    124         } else if (i >= max) {
    125             return max - 1;
    126         } else {
    127             return i;
    128         }
    129     }
    130 
    131     private static Color getColor(int x, int y, BufferedImage img) {
     116    private static int getColor(int x, int y, BufferedImage img) {
    132117        // border strategy: continue with the color of the outermost pixel,
    133118        // but change alpha component to fully translucent
    134         int a = clamp(x, img.getWidth());
    135         int b = clamp(y, img.getHeight());
    136         Color clr = new Color(img.getRGB(a, b));
     119        int a = Utils.clamp(x, 0, img.getWidth() - 1);// clamp(x, img.getWidth());
     120        int b = Utils.clamp(y, 0, img.getHeight() - 1); // clamp(y, img.getHeight());
     121        int clr = img.getRGB(a, b);
    137122        if (a == x && b == y)
    138123            return clr;
    139124        // keep color components, but set transparency to 0
    140125        // (the idea is that border fades out and mixes with next tile)
    141         return new Color(clr.getRed(), clr.getGreen(), clr.getBlue(), 0);
     126        return clr & 0x00ffffff;
    142127    }
    143128}
Note: See TracChangeset for help on using the changeset viewer.