1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package cadastre_fr;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Transparency;
|
---|
6 | import java.awt.image.BufferedImage;
|
---|
7 | import java.awt.image.ColorConvertOp;
|
---|
8 | import java.awt.image.DataBuffer;
|
---|
9 | import java.awt.image.IndexColorModel;
|
---|
10 | import java.util.Objects;
|
---|
11 |
|
---|
12 | public abstract class ImageModifier {
|
---|
13 | /**
|
---|
14 | * Current background color used by cadastre.gouv.fr
|
---|
15 | */
|
---|
16 | //public static int cadastreBackgroundTransp = 1; // original white but transparent
|
---|
17 |
|
---|
18 | protected int parcelColor = Color.RED.getRGB();
|
---|
19 |
|
---|
20 | protected BufferedImage bufferedImage;
|
---|
21 |
|
---|
22 | public static int[] cRoofColors = new int[] {-197380, -592138};
|
---|
23 | public static int[] cBuilingFootColors = new int[] {-256};
|
---|
24 |
|
---|
25 | protected BufferedImage convert1(BufferedImage src) {
|
---|
26 | IndexColorModel icm = new IndexColorModel(
|
---|
27 | 1, 2,
|
---|
28 | new byte[] {(byte) 0, (byte) 0xFF},
|
---|
29 | new byte[] {(byte) 0, (byte) 0xFF},
|
---|
30 | new byte[] {(byte) 0, (byte) 0xFF}
|
---|
31 | );
|
---|
32 |
|
---|
33 | BufferedImage dest = new BufferedImage(
|
---|
34 | src.getWidth(), src.getHeight(),
|
---|
35 | BufferedImage.TYPE_BYTE_BINARY,
|
---|
36 | icm
|
---|
37 | );
|
---|
38 |
|
---|
39 | ColorConvertOp cco = new ColorConvertOp(
|
---|
40 | src.getColorModel().getColorSpace(),
|
---|
41 | dest.getColorModel().getColorSpace(),
|
---|
42 | null
|
---|
43 | );
|
---|
44 |
|
---|
45 | cco.filter(src, dest);
|
---|
46 |
|
---|
47 | return dest;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Converts the source image to 4-bit colour
|
---|
52 | * using the default 16-colour palette:
|
---|
53 | * <ul>
|
---|
54 | * <li>black</li><li>dark red</li><li>dark green</li>
|
---|
55 | * <li>dark yellow</li><li>dark blue</li><li>dark magenta</li>
|
---|
56 | * <li>dark cyan</li><li>dark grey</li><li>light grey</li>
|
---|
57 | * <li>red</li><li>green</li><li>yellow</li><li>blue</li>
|
---|
58 | * <li>magenta</li><li>cyan</li><li>white</li>
|
---|
59 | * </ul>
|
---|
60 | * No transparency.
|
---|
61 | * @param src the source image to convert
|
---|
62 | * @return a copy of the source image with a 4-bit colour depth, with the default colour pallette
|
---|
63 | */
|
---|
64 | protected BufferedImage convert4(BufferedImage src) {
|
---|
65 | int[] cmap = new int[] {
|
---|
66 | 0x000000, 0x800000, 0x008000, 0x808000,
|
---|
67 | 0x000080, 0x800080, 0x008080, 0x808080,
|
---|
68 | 0xC0C0C0, 0xFF0000, 0x00FF00, 0xFFFF00,
|
---|
69 | 0x0000FF, 0xFF00FF, 0x00FFFF, 0xFFFFFF
|
---|
70 | };
|
---|
71 | return convert4(src, cmap);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Converts the source image to 4-bit colour
|
---|
76 | * using the given colour map. No transparency.
|
---|
77 | * @param src the source image to convert
|
---|
78 | * @param cmap the colour map, which should contain no more than 16 entries
|
---|
79 | * The entries are in the form RRGGBB (hex).
|
---|
80 | * @return a copy of the source image with a 4-bit colour depth, with the custom colour pallette
|
---|
81 | */
|
---|
82 | protected BufferedImage convert4(BufferedImage src, int[] cmap) {
|
---|
83 | IndexColorModel icm = new IndexColorModel(
|
---|
84 | 4, cmap.length, cmap, 0, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE
|
---|
85 | );
|
---|
86 | BufferedImage dest = new BufferedImage(
|
---|
87 | src.getWidth(), src.getHeight(),
|
---|
88 | BufferedImage.TYPE_BYTE_BINARY,
|
---|
89 | icm
|
---|
90 | );
|
---|
91 | ColorConvertOp cco = new ColorConvertOp(
|
---|
92 | src.getColorModel().getColorSpace(),
|
---|
93 | dest.getColorModel().getColorSpace(),
|
---|
94 | null
|
---|
95 | );
|
---|
96 | cco.filter(src, dest);
|
---|
97 |
|
---|
98 | return dest;
|
---|
99 | }
|
---|
100 |
|
---|
101 | protected BufferedImage convert8(BufferedImage src) {
|
---|
102 | BufferedImage dest = new BufferedImage(
|
---|
103 | src.getWidth(), src.getHeight(),
|
---|
104 | BufferedImage.TYPE_BYTE_INDEXED
|
---|
105 | );
|
---|
106 | ColorConvertOp cco = new ColorConvertOp(
|
---|
107 | src.getColorModel().getColorSpace(),
|
---|
108 | dest.getColorModel().getColorSpace(),
|
---|
109 | null
|
---|
110 | );
|
---|
111 | cco.filter(src, dest);
|
---|
112 | return dest;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public boolean isBuildingColor(int rgb, boolean ignoreParcelColor) {
|
---|
116 | for (int i = 0; i < cBuilingFootColors.length; i++) {
|
---|
117 | if (rgb == cBuilingFootColors[i])
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 | if (ignoreParcelColor && (rgb == parcelColor))
|
---|
121 | return true;
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public boolean isRoofColor(int rgb, boolean ignoreParcelColor) {
|
---|
126 | for (int i = 0; i < cRoofColors.length; i++) {
|
---|
127 | if (rgb == cRoofColors[i])
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 | if (ignoreParcelColor && (rgb == parcelColor))
|
---|
131 | return true;
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 |
|
---|
135 | public boolean isParcelColor(BufferedImage img, int x, int y) {
|
---|
136 | int rgb = img.getRGB(x, y);
|
---|
137 | return (rgb == parcelColor);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public boolean isBuildingOrRoofColor(BufferedImage img, int x, int y, boolean ignoreParcelColor) {
|
---|
141 | int rgb = img.getRGB(x, y);
|
---|
142 | boolean ret = isBuildingColor(rgb, ignoreParcelColor) || isRoofColor(rgb, ignoreParcelColor);
|
---|
143 | return ret;
|
---|
144 | }
|
---|
145 |
|
---|
146 | public boolean isBuildingOrRoofColor(BufferedImage img, int x, int y, boolean colorType, boolean ignoreParcelColor) {
|
---|
147 | int rgb = img.getRGB(x, y);
|
---|
148 | boolean ret;
|
---|
149 | if (colorType)
|
---|
150 | ret = isBuildingColor(rgb, ignoreParcelColor);
|
---|
151 | else
|
---|
152 | ret = isRoofColor(rgb, ignoreParcelColor);
|
---|
153 | return ret;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Checks if the rgb value is the black background color
|
---|
158 | */
|
---|
159 | public boolean isBackgroundColor(BufferedImage img, int x, int y) {
|
---|
160 | return (img.getRGB(x, y) == -1);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Returns the buffered image.
|
---|
165 | * @return the buffered image
|
---|
166 | */
|
---|
167 | public final BufferedImage getBufferedImage() {
|
---|
168 | return bufferedImage;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Sets the buffered image.
|
---|
173 | * @param bufferedImage the buffered image
|
---|
174 | */
|
---|
175 | protected final void setBufferedImage(BufferedImage bufferedImage) {
|
---|
176 | this.bufferedImage = Objects.requireNonNull(bufferedImage);
|
---|
177 | }
|
---|
178 | }
|
---|