| 1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
|---|
| 2 | // Some of the procedures below are imported from image4j.sourceforge.net, license LGPL.
|
|---|
| 3 | package cadastre_fr;
|
|---|
| 4 |
|
|---|
| 5 | import java.awt.Color;
|
|---|
| 6 | import java.awt.Transparency;
|
|---|
| 7 | import java.awt.image.BufferedImage;
|
|---|
| 8 | import java.awt.image.ColorConvertOp;
|
|---|
| 9 | import java.awt.image.DataBuffer;
|
|---|
| 10 | import java.awt.image.IndexColorModel;
|
|---|
| 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 | private static final long serialVersionUID = 1L;
|
|---|
| 19 |
|
|---|
| 20 | protected int parcelColor = Color.RED.getRGB();
|
|---|
| 21 |
|
|---|
| 22 | public BufferedImage bufferedImage;
|
|---|
| 23 |
|
|---|
| 24 | public static int[] cRoofColors = new int[] {-197380, -592138};
|
|---|
| 25 | public static int[] cBuilingFootColors = new int[] {-256};
|
|---|
| 26 |
|
|---|
| 27 | protected BufferedImage convert1(BufferedImage src) {
|
|---|
| 28 | IndexColorModel icm = new IndexColorModel(
|
|---|
| 29 | 1, 2, new byte[] { (byte) 0, (byte) 0xFF },
|
|---|
| 30 | new byte[] { (byte) 0, (byte) 0xFF },
|
|---|
| 31 | new byte[] { (byte) 0, (byte) 0xFF }
|
|---|
| 32 | );
|
|---|
| 33 |
|
|---|
| 34 | BufferedImage dest = new BufferedImage(
|
|---|
| 35 | src.getWidth(), src.getHeight(),
|
|---|
| 36 | BufferedImage.TYPE_BYTE_BINARY,
|
|---|
| 37 | icm
|
|---|
| 38 | );
|
|---|
| 39 |
|
|---|
| 40 | ColorConvertOp cco = new ColorConvertOp(
|
|---|
| 41 | src.getColorModel().getColorSpace(),
|
|---|
| 42 | dest.getColorModel().getColorSpace(),
|
|---|
| 43 | null
|
|---|
| 44 | );
|
|---|
| 45 |
|
|---|
| 46 | cco.filter(src, dest);
|
|---|
| 47 |
|
|---|
| 48 | return dest;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Converts the source image to 4-bit colour
|
|---|
| 53 | * using the default 16-colour palette:
|
|---|
| 54 | * <ul>
|
|---|
| 55 | * <li>black</li><li>dark red</li><li>dark green</li>
|
|---|
| 56 | * <li>dark yellow</li><li>dark blue</li><li>dark magenta</li>
|
|---|
| 57 | * <li>dark cyan</li><li>dark grey</li><li>light grey</li>
|
|---|
| 58 | * <li>red</li><li>green</li><li>yellow</li><li>blue</li>
|
|---|
| 59 | * <li>magenta</li><li>cyan</li><li>white</li>
|
|---|
| 60 | * </ul>
|
|---|
| 61 | * No transparency.
|
|---|
| 62 | * @param src the source image to convert
|
|---|
| 63 | * @return a copy of the source image with a 4-bit colour depth, with the default colour pallette
|
|---|
| 64 | */
|
|---|
| 65 | protected BufferedImage convert4(BufferedImage src) {
|
|---|
| 66 | int[] cmap = new int[] {
|
|---|
| 67 | 0x000000, 0x800000, 0x008000, 0x808000,
|
|---|
| 68 | 0x000080, 0x800080, 0x008080, 0x808080,
|
|---|
| 69 | 0xC0C0C0, 0xFF0000, 0x00FF00, 0xFFFF00,
|
|---|
| 70 | 0x0000FF, 0xFF00FF, 0x00FFFF, 0xFFFFFF
|
|---|
| 71 | };
|
|---|
| 72 | return convert4(src, cmap);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * Converts the source image to 4-bit colour
|
|---|
| 77 | * using the given colour map. No transparency.
|
|---|
| 78 | * @param src the source image to convert
|
|---|
| 79 | * @param cmap the colour map, which should contain no more than 16 entries
|
|---|
| 80 | * The entries are in the form RRGGBB (hex).
|
|---|
| 81 | * @return a copy of the source image with a 4-bit colour depth, with the custom colour pallette
|
|---|
| 82 | */
|
|---|
| 83 | protected BufferedImage convert4(BufferedImage src, int[] cmap) {
|
|---|
| 84 | IndexColorModel icm = new IndexColorModel(
|
|---|
| 85 | 4, cmap.length, cmap, 0, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE
|
|---|
| 86 | );
|
|---|
| 87 | BufferedImage dest = new BufferedImage(
|
|---|
| 88 | src.getWidth(), src.getHeight(),
|
|---|
| 89 | BufferedImage.TYPE_BYTE_BINARY,
|
|---|
| 90 | icm
|
|---|
| 91 | );
|
|---|
| 92 | ColorConvertOp cco = new ColorConvertOp(
|
|---|
| 93 | src.getColorModel().getColorSpace(),
|
|---|
| 94 | dest.getColorModel().getColorSpace(),
|
|---|
| 95 | null
|
|---|
| 96 | );
|
|---|
| 97 | cco.filter(src, dest);
|
|---|
| 98 |
|
|---|
| 99 | return dest;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | protected BufferedImage convert8(BufferedImage src) {
|
|---|
| 103 | BufferedImage dest = new BufferedImage(
|
|---|
| 104 | src.getWidth(), src.getHeight(),
|
|---|
| 105 | BufferedImage.TYPE_BYTE_INDEXED
|
|---|
| 106 | );
|
|---|
| 107 | ColorConvertOp cco = new ColorConvertOp(
|
|---|
| 108 | src.getColorModel().getColorSpace(),
|
|---|
| 109 | dest.getColorModel().getColorSpace(),
|
|---|
| 110 | null
|
|---|
| 111 | );
|
|---|
| 112 | cco.filter(src, dest);
|
|---|
| 113 | return dest;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | public boolean isBuildingColor(int rgb, boolean ignoreParcelColor) {
|
|---|
| 117 | for (int i = 0; i < cBuilingFootColors.length; i++)
|
|---|
| 118 | if (rgb == cBuilingFootColors[i])
|
|---|
| 119 | return true;
|
|---|
| 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 | if (ignoreParcelColor && (rgb == parcelColor))
|
|---|
| 130 | return true;
|
|---|
| 131 | return false;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | public boolean isParcelColor(BufferedImage img, int x, int y) {
|
|---|
| 135 | int rgb = img.getRGB(x, y);
|
|---|
| 136 | return (rgb == parcelColor);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | public boolean isBuildingOrRoofColor(BufferedImage img, int x, int y, boolean ignoreParcelColor) {
|
|---|
| 140 | int rgb = img.getRGB(x, y);
|
|---|
| 141 | boolean ret = isBuildingColor(rgb, ignoreParcelColor) || isRoofColor(rgb, ignoreParcelColor);
|
|---|
| 142 | return ret;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | public boolean isBuildingOrRoofColor(BufferedImage img, int x, int y, boolean colorType, boolean ignoreParcelColor) {
|
|---|
| 146 | int rgb = img.getRGB(x, y);
|
|---|
| 147 | boolean ret;
|
|---|
| 148 | if (colorType)
|
|---|
| 149 | ret = isBuildingColor(rgb, ignoreParcelColor);
|
|---|
| 150 | else
|
|---|
| 151 | ret = isRoofColor(rgb, ignoreParcelColor);
|
|---|
| 152 | return ret;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * Checks if the rgb value is the black background color
|
|---|
| 157 | * @param
|
|---|
| 158 | * @return
|
|---|
| 159 | */
|
|---|
| 160 | public boolean isBackgroundColor(BufferedImage img, int x, int y) {
|
|---|
| 161 | return (img.getRGB(x, y) == -1);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | }
|
|---|