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