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