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