| 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.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 |
|
|---|
| 11 | public abstract class ImageModifier {
|
|---|
| 12 | /**
|
|---|
| 13 | * Current background color used by cadastre.gouv.fr
|
|---|
| 14 | */
|
|---|
| 15 | //public static int cadastreBackgroundTransp = 1; // original white but transparent
|
|---|
| 16 |
|
|---|
| 17 | private static final long serialVersionUID = 1L;
|
|---|
| 18 |
|
|---|
| 19 | public BufferedImage bufferedImage;
|
|---|
| 20 |
|
|---|
| 21 | protected BufferedImage convert1(BufferedImage src) {
|
|---|
| 22 | IndexColorModel icm = new IndexColorModel(
|
|---|
| 23 | 1, 2, new byte[] { (byte) 0, (byte) 0xFF },
|
|---|
| 24 | new byte[] { (byte) 0, (byte) 0xFF },
|
|---|
| 25 | new byte[] { (byte) 0, (byte) 0xFF }
|
|---|
| 26 | );
|
|---|
| 27 |
|
|---|
| 28 | BufferedImage dest = new BufferedImage(
|
|---|
| 29 | src.getWidth(), src.getHeight(),
|
|---|
| 30 | BufferedImage.TYPE_BYTE_BINARY,
|
|---|
| 31 | icm
|
|---|
| 32 | );
|
|---|
| 33 |
|
|---|
| 34 | ColorConvertOp cco = new ColorConvertOp(
|
|---|
| 35 | src.getColorModel().getColorSpace(),
|
|---|
| 36 | dest.getColorModel().getColorSpace(),
|
|---|
| 37 | null
|
|---|
| 38 | );
|
|---|
| 39 |
|
|---|
| 40 | cco.filter(src, dest);
|
|---|
| 41 |
|
|---|
| 42 | return dest;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Converts the source image to 4-bit colour
|
|---|
| 47 | * using the default 16-colour palette:
|
|---|
| 48 | * <ul>
|
|---|
| 49 | * <li>black</li><li>dark red</li><li>dark green</li>
|
|---|
| 50 | * <li>dark yellow</li><li>dark blue</li><li>dark magenta</li>
|
|---|
| 51 | * <li>dark cyan</li><li>dark grey</li><li>light grey</li>
|
|---|
| 52 | * <li>red</li><li>green</li><li>yellow</li><li>blue</li>
|
|---|
| 53 | * <li>magenta</li><li>cyan</li><li>white</li>
|
|---|
| 54 | * </ul>
|
|---|
| 55 | * No transparency.
|
|---|
| 56 | * @param src the source image to convert
|
|---|
| 57 | * @return a copy of the source image with a 4-bit colour depth, with the default colour pallette
|
|---|
| 58 | */
|
|---|
| 59 | protected BufferedImage convert4(BufferedImage src) {
|
|---|
| 60 | int[] cmap = new int[] {
|
|---|
| 61 | 0x000000, 0x800000, 0x008000, 0x808000,
|
|---|
| 62 | 0x000080, 0x800080, 0x008080, 0x808080,
|
|---|
| 63 | 0xC0C0C0, 0xFF0000, 0x00FF00, 0xFFFF00,
|
|---|
| 64 | 0x0000FF, 0xFF00FF, 0x00FFFF, 0xFFFFFF
|
|---|
| 65 | };
|
|---|
| 66 | return convert4(src, cmap);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Converts the source image to 4-bit colour
|
|---|
| 71 | * using the given colour map. No transparency.
|
|---|
| 72 | * @param src the source image to convert
|
|---|
| 73 | * @param cmap the colour map, which should contain no more than 16 entries
|
|---|
| 74 | * The entries are in the form RRGGBB (hex).
|
|---|
| 75 | * @return a copy of the source image with a 4-bit colour depth, with the custom colour pallette
|
|---|
| 76 | */
|
|---|
| 77 | protected BufferedImage convert4(BufferedImage src, int[] cmap) {
|
|---|
| 78 | IndexColorModel icm = new IndexColorModel(
|
|---|
| 79 | 4, cmap.length, cmap, 0, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE
|
|---|
| 80 | );
|
|---|
| 81 | BufferedImage dest = new BufferedImage(
|
|---|
| 82 | src.getWidth(), src.getHeight(),
|
|---|
| 83 | BufferedImage.TYPE_BYTE_BINARY,
|
|---|
| 84 | icm
|
|---|
| 85 | );
|
|---|
| 86 | ColorConvertOp cco = new ColorConvertOp(
|
|---|
| 87 | src.getColorModel().getColorSpace(),
|
|---|
| 88 | dest.getColorModel().getColorSpace(),
|
|---|
| 89 | null
|
|---|
| 90 | );
|
|---|
| 91 | cco.filter(src, dest);
|
|---|
| 92 |
|
|---|
| 93 | return dest;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | protected BufferedImage convert8(BufferedImage src) {
|
|---|
| 97 | BufferedImage dest = new BufferedImage(
|
|---|
| 98 | src.getWidth(), src.getHeight(),
|
|---|
| 99 | BufferedImage.TYPE_BYTE_INDEXED
|
|---|
| 100 | );
|
|---|
| 101 | ColorConvertOp cco = new ColorConvertOp(
|
|---|
| 102 | src.getColorModel().getColorSpace(),
|
|---|
| 103 | dest.getColorModel().getColorSpace(),
|
|---|
| 104 | null
|
|---|
| 105 | );
|
|---|
| 106 | cco.filter(src, dest);
|
|---|
| 107 | return dest;
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|