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/ImageModifier.java

    r15961 r17089  
    11package cadastre_fr;
    22
    3 import java.awt.Color;
    43import java.awt.image.BufferedImage;
    5 import java.awt.image.ColorModel;
    6 import java.awt.image.IndexColorModel;
    7 import java.awt.image.WritableRaster;
    8 import org.openstreetmap.josm.Main;
    9 import org.openstreetmap.josm.tools.ColorHelper;
    104
    11 public class ImageModifier {
    12 
     5public abstract class ImageModifier {
    136    /**
    147     * Current background color used by cadastre.gouv.fr
    158     */
     9    //public static int cadastreBackgroundTransp = 1; // original white but transparent
     10
    1611    private static final long serialVersionUID = 1L;
    17 
    18     public static final int cadastreBackground = -1; // white
    19 
    20     public static final int cadastreBackgroundTransp = 1; // original white but transparent
    2112
    2213    public BufferedImage bufferedImage;
    2314
    24     private boolean withBackground = false;
    25 
    26     private int backgroundPixel = 0;
    27 
    28     private int backgroundSampleX, backgroundSampleY;
    29 
    30     public ImageModifier(BufferedImage bi) {
    31         bufferedImage = bi;
    32         if (Main.pref.getBoolean("cadastrewms.backgroundTransparent"))
    33             makeTransparent();
    34         else if (Main.pref.getBoolean("cadastrewms.alterColors"))
    35             replaceBackground();
    36 
    37         if (Main.pref.getBoolean("cadastrewms.invertGrey"))
    38             invertGrey();
    39     }
    40 
    41     /**
    42      * Replace the background color by the josm color.background color.
    43      */
    44     private void replaceBackground() {
    45         int w = bufferedImage.getWidth();
    46         int h = bufferedImage.getHeight();
    47         int josmBackgroundColor = ColorHelper.html2color(Main.pref.get("color.background", "#000000")).getRGB();
    48         for (int x = 0; x < w; x++) {
    49             for (int y = 0; y < h; y++) {
    50                 int pixel = bufferedImage.getRGB(x, y);
    51                 if (pixel == cadastreBackground) {
    52                     bufferedImage.setRGB(x, y, josmBackgroundColor);
    53                     if (!withBackground)
    54                         withBackground = true;
    55                     backgroundSampleX = x;
    56                     backgroundSampleY = y;
    57                 }
    58             }
    59         }
    60     }
    61 
    62     /**
    63      * Invert black/white/grey pixels (to change original black characters to white).
    64      */
    65     private void invertGrey() {
    66         int w = bufferedImage.getWidth();
    67         int h = bufferedImage.getHeight();
    68         for (int x = 0; x < w; x++) {
    69             for (int y = 0; y < h; y++) {
    70                 int pixel = bufferedImage.getRGB(x, y);
    71                 if (pixel != cadastreBackground) {
    72                     bufferedImage.setRGB(x, y, reverseIfGrey(pixel));
    73                 }
    74             }
    75         }
    76     }
    77 
    78     /**
    79      * Reverse the grey value if the pixel is grey (light grey becomes dark grey)
    80      * Used for texts.
    81      * @param pixel
    82      * @return
    83      */
    84     private int reverseIfGrey(int pixel) {
    85         Color col = new Color(pixel);
    86         int r = col.getRed();
    87         int g = col.getGreen();
    88         int b = col.getBlue();
    89         if ((b == r) && (b == g)) {
    90             pixel = (0x00 << 32) + ((byte) (255 - r) << 16) + ((byte) (255 - r) << 8) + ((byte) (255 - r));
    91         }
    92         return pixel;
    93     }
    94 
    95     private void makeTransparent() {
    96         ColorModel colorModel = bufferedImage.getColorModel();
    97         if (bufferedImage.getColorModel() instanceof IndexColorModel) {
    98             // vector image (IndexColorModel)
    99             IndexColorModel icm = (IndexColorModel) colorModel;
    100             WritableRaster raster = bufferedImage.getRaster();
    101             // pixel is offset in ICM's palette
    102             if (withBackground)
    103                 backgroundPixel = raster.getSample(backgroundSampleX, backgroundSampleY, 0);
    104             else
    105                 backgroundPixel = 1; // default Cadastre background sample
    106             int size = icm.getMapSize();
    107             byte[] reds = new byte[size];
    108             byte[] greens = new byte[size];
    109             byte[] blues = new byte[size];
    110             icm.getReds(reds);
    111             icm.getGreens(greens);
    112             icm.getBlues(blues);
    113             IndexColorModel icm2 = new IndexColorModel(colorModel.getPixelSize(), size, reds, greens, blues,
    114                     backgroundPixel);
    115             bufferedImage = new BufferedImage(icm2, raster, bufferedImage.isAlphaPremultiplied(), null);
    116         } else {
    117             int width = bufferedImage.getWidth();
    118             int height = bufferedImage.getHeight();
    119             BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    120             for (int y = 0; y < height; y++) {
    121                 for (int x = 0; x < width; x++) {
    122                     Color c = new Color(bufferedImage.getRGB(x, y));
    123                     int r = c.getRed();
    124                     int g = c.getGreen();
    125                     int b = c.getBlue();
    126                     Color maskedColor;
    127                     if (r==0 && g==0 && b==0) {
    128                         maskedColor = new Color(r, g, b, 0x00);
    129                     } else {
    130                         maskedColor = new Color(r, g, b, 0xFF);
    131                     }
    132                     bi.setRGB(x, y, maskedColor.getRGB());
    133                 }
    134             }
    135             bufferedImage = bi;
    136         }
    137         return;
    138     }
    13915}
Note: See TracChangeset for help on using the changeset viewer.