| 1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
|---|
| 2 | package cadastre_fr;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.Color;
|
|---|
| 5 | import java.awt.image.BufferedImage;
|
|---|
| 6 | import java.awt.image.ComponentColorModel;
|
|---|
| 7 |
|
|---|
| 8 | import org.openstreetmap.josm.Main;
|
|---|
| 9 |
|
|---|
| 10 | public class RasterImageModifier extends ImageModifier {
|
|---|
| 11 |
|
|---|
| 12 | private int cadastreBackground = -1; // white
|
|---|
| 13 |
|
|---|
| 14 | public static int cadastreBackgroundTransp = 16777215; // original white but transparent
|
|---|
| 15 |
|
|---|
| 16 | private boolean transparencyEnabled = false;
|
|---|
| 17 |
|
|---|
| 18 | public RasterImageModifier(BufferedImage bi) {
|
|---|
| 19 | bufferedImage = bi;
|
|---|
| 20 | transparencyEnabled = Main.pref.getBoolean("cadastrewms.backgroundTransparent");
|
|---|
| 21 | if (transparencyEnabled)
|
|---|
| 22 | makeTransparent();
|
|---|
| 23 | if (Main.pref.getBoolean("cadastrewms.invertGrey"))
|
|---|
| 24 | invertGrey();
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Invert black/white/grey pixels (to change original black characters to white).
|
|---|
| 29 | */
|
|---|
| 30 | private void invertGrey() {
|
|---|
| 31 | int w = bufferedImage.getWidth();
|
|---|
| 32 | int h = bufferedImage.getHeight();
|
|---|
| 33 | for (int x = 0; x < w; x++) {
|
|---|
| 34 | for (int y = 0; y < h; y++) {
|
|---|
| 35 | int pixel = bufferedImage.getRGB(x, y);
|
|---|
| 36 | if ((!transparencyEnabled && pixel != cadastreBackground)
|
|---|
| 37 | || (transparencyEnabled && pixel != cadastreBackgroundTransp)) {
|
|---|
| 38 | bufferedImage.setRGB(x, y, reverseIfGrey(pixel));
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Reverse the grey value if the pixel is grey (light grey becomes dark grey)
|
|---|
| 46 | * Used for texts.
|
|---|
| 47 | * @param pixel
|
|---|
| 48 | * @return
|
|---|
| 49 | */
|
|---|
| 50 | private int reverseIfGrey(int pixel) {
|
|---|
| 51 | Color col = new Color(pixel);
|
|---|
| 52 | int r = col.getRed();
|
|---|
| 53 | int g = col.getGreen();
|
|---|
| 54 | int b = col.getBlue();
|
|---|
| 55 | if ((b == r) && (b == g)) {
|
|---|
| 56 | pixel = (0x00 << 32) + ((byte) (255 - r) << 16) + ((byte) (255 - r) << 8) + ((byte) (255 - r));
|
|---|
| 57 | }
|
|---|
| 58 | return pixel;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | private void makeTransparent() {
|
|---|
| 62 | if (bufferedImage.getColorModel() instanceof ComponentColorModel) {
|
|---|
| 63 | int width = bufferedImage.getWidth();
|
|---|
| 64 | int height = bufferedImage.getHeight();
|
|---|
| 65 | BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
|---|
| 66 | for (int y = 0; y < height; y++) {
|
|---|
| 67 | for (int x = 0; x < width; x++) {
|
|---|
| 68 | int rgb = bufferedImage.getRGB(x, y);
|
|---|
| 69 | Color c = new Color(rgb);
|
|---|
| 70 | int r = c.getRed();
|
|---|
| 71 | int g = c.getGreen();
|
|---|
| 72 | int b = c.getBlue();
|
|---|
| 73 | Color maskedColor;
|
|---|
| 74 | if (rgb == cadastreBackground) {
|
|---|
| 75 | maskedColor = new Color(r, g, b, 0x00); // transparent
|
|---|
| 76 | } else {
|
|---|
| 77 | maskedColor = new Color(r, g, b, 0xFF); // opaque
|
|---|
| 78 | }
|
|---|
| 79 | //maskedColor = new Color(r, g, b, alpha);
|
|---|
| 80 | bi.setRGB(x, y, maskedColor.getRGB());
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | bufferedImage = bi;
|
|---|
| 84 | }
|
|---|
| 85 | return;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Temporary fix for Java6 which doesn't de-serialize correctly cached image on disk.
|
|---|
| 90 | * Recreate a new raster image based on what is loaded/serialized from disk cache.
|
|---|
| 91 | * @param img
|
|---|
| 92 | * @return new image
|
|---|
| 93 | */
|
|---|
| 94 | public static BufferedImage fixRasterImage(BufferedImage img) {
|
|---|
| 95 | int width = img.getWidth();
|
|---|
| 96 | int height = img.getHeight();
|
|---|
| 97 | BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
|---|
| 98 | int rgbArray[] = new int[width * height];
|
|---|
| 99 | img.getRGB(0, 0, width, height, rgbArray, 0, width);
|
|---|
| 100 | bi.setRGB(0, 0, width, height, rgbArray, 0, width);
|
|---|
| 101 | return bi;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | }
|
|---|