Example usage for java.awt.image IndexColorModel IndexColorModel

List of usage examples for java.awt.image IndexColorModel IndexColorModel

Introduction

In this page you can find the example usage for java.awt.image IndexColorModel IndexColorModel.

Prototype

public IndexColorModel(int bits, int size, byte[] cmap, int start, boolean hasalpha) 

Source Link

Document

Constructs an IndexColorModel from a single array of interleaved red, green, blue and optional alpha components.

Usage

From source file:Main.java

public static void main(String[] args) {
    byte ff = (byte) 0xff;
    byte[] r = { ff, 0, 0, ff, 0 };
    byte[] g = { 0, ff, 0, ff, 0 };
    byte[] b = { 0, 0, ff, ff, 0 };

    ColorModel cm = new IndexColorModel(3, 5, r, g, b);

    Color[] colors = { new Color(255, 0, 0), new Color(0, 255, 0), new Color(0, 0, 255), new Color(64, 255, 64),
            new Color(255, 255, 0), new Color(0, 255, 255) };

    for (int i = 0; i < colors.length; i++) {
        float[] normalizedComponents = colors[i].getComponents(null);
        int[] unnormalizedComponents = cm.getUnnormalizedComponents(normalizedComponents, 0, null, 0);
        int rgb = colors[i].getRGB();
        Object pixel = cm.getDataElements(rgb, null);
        System.out.println(colors[i] + " -> " + ((byte[]) pixel)[0]);
    }// w  w w  .j a  va 2 s.  c o m

    for (byte i = 0; i < 5; i++) {
        int[] unnormalizedComponents = cm.getComponents(i, null, 0);
        System.out.print(i + " -> unnormalized components");
        for (int j = 0; j < unnormalizedComponents.length; j++)
            System.out.print(" " + unnormalizedComponents[j]);
        System.out.println();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    byte ff = (byte) 0xff;
    byte[] r = { ff, 0, 0, ff, 0 };
    byte[] g = { 0, ff, 0, ff, 0 };
    byte[] b = { 0, 0, ff, ff, 0 };

    ColorModel cm = new IndexColorModel(3, 5, r, g, b);

    Color[] colors = { new Color(255, 0, 0), new Color(0, 255, 0), new Color(0, 0, 255) };

    for (int i = 0; i < colors.length; i++) {
        int rgb = colors[i].getRGB();
        Object pixel = cm.getDataElements(rgb, null);
        System.out.println(colors[i] + " -> " + ((byte[]) pixel)[0]);
    }/*from   ww  w  . j  a v a2s  .co  m*/

    for (byte i = 0; i < 5; i++) {
        int[] unnormalizedComponents = cm.getComponents(i, null, 0);
        System.out.print(i + " -> unnormalized components");
        for (int j = 0; j < unnormalizedComponents.length; j++)
            System.out.print(" " + unnormalizedComponents[j]);
        System.out.println();
    }
}

From source file:ConvertUtil.java

/**
 * Converts the source to 1-bit colour depth (monochrome). No transparency.
 * /*from www  . jav a2 s.  com*/
 * @param src
 *            the source image to convert
 * @return a copy of the source image with a 1-bit colour depth.
 */
public static BufferedImage convert1(BufferedImage src) {
    IndexColorModel icm = new IndexColorModel(1, 2, new byte[] { (byte) 0, (byte) 0xFF },
            new byte[] { (byte) 0, (byte) 0xFF }, new byte[] { (byte) 0, (byte) 0xFF });

    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_BINARY,
            icm);

    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);

    cco.filter(src, dest);

    return dest;
}

From source file:StaticGenerator.java

public void initialize() {
    int w = getSize().width, h = getSize().height;
    int length = ((w + 7) * h) / 8;
    data = new byte[length];
    DataBuffer db = new DataBufferByte(data, length);
    WritableRaster wr = Raster.createPackedRaster(db, w, h, 1, null);
    ColorModel cm = new IndexColorModel(1, 2, new byte[] { (byte) 0, (byte) 255 },
            new byte[] { (byte) 0, (byte) 255 }, new byte[] { (byte) 0, (byte) 255 });
    image = new BufferedImage(cm, wr, false, null);
    random = new Random();
    new Thread(this).start();
}

From source file:image.text.CreateTifAndAnnotate.java

public void createAnnotatedTif(String[] args, OutputStream out) throws Exception {

    byte[] byteArray = new byte[] { -1, 0 };
    ColorModel colorModel = new IndexColorModel(1, 2, byteArray, byteArray, byteArray);

    WritableRaster writeableRaster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1700, 2200, 1, 1, null);
    BufferedImage bufImg = new BufferedImage(colorModel, writeableRaster, false, null);

    // -------------------------------------------------------------------        
    Graphics2D g2d = bufImg.createGraphics();
    g2d.setColor(Color.black);/*ww  w  .ja  va 2 s.  co m*/

    Font font = new Font("Arial Bold", Font.PLAIN, 36);
    g2d.setFont(font);
    int vertPos = 200;
    for (int i = 0; i < args.length; i++) {
        g2d.drawString(args[i], 75, vertPos);
        vertPos += 48;
    }

    PlanarImage planarImage = PlanarImage.wrapRenderedImage(bufImg);

    TIFFEncodeParam encodeParam = new TIFFEncodeParam();
    encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
    encodeParam.setWriteTiled(false); // false means use strips.
    encodeParam.setTileSize(0, 0); // tiling will make the file size much larger. 
    encodeParam.setLittleEndian(true); // create an Intel (II) format image

    String SoftwareVersion[] = new String[] { "TIFF by Joe, " + this.getClass().getName() };
    String docName[] = new String[] { "JoesTifAnnotator" };

    // Create a new TIFF fields including a new TIFF ASCII TIFF tag.
    TIFFField tiffFields[] = new TIFFField[5];

    tiffFields[0] = new TIFFField(269, TIFFField.TIFF_ASCII, docName.length, docName);
    tiffFields[1] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } });
    tiffFields[2] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } });
    // resolution unit 
    tiffFields[3] = new TIFFField(296, TIFFField.TIFF_SHORT, 1, new char[] { 2 });
    tiffFields[4] = new TIFFField(305, TIFFField.TIFF_ASCII, SoftwareVersion.length, SoftwareVersion);

    encodeParam.setExtraFields(tiffFields);

    TIFFImageEncoder encoder = new TIFFImageEncoder(out, encodeParam);
    encoder.encode(planarImage);
}

From source file:omr.jai.RGBToBilevel.java

RGBToBilevel(final String fileName, boolean isErrorDiffusion) {

    // Load the file.
    PlanarImage src = JAI.create("fileload", fileName);

    // Load the ParameterBlock for the dithering operation
    // and set the operation name.
    ParameterBlock pb = new ParameterBlock();
    pb.addSource(src);//  w  w w  .ja  v a 2s .com
    String opName = null;
    if (isErrorDiffusion) {
        opName = "errordiffusion";
        LookupTableJAI lut = new LookupTableJAI(new byte[][] { { (byte) 0x00, (byte) 0xff },
                { (byte) 0x00, (byte) 0xff }, { (byte) 0x00, (byte) 0xff } });
        pb.add(lut);
        pb.add(KernelJAI.ERROR_FILTER_FLOYD_STEINBERG);
    } else {
        opName = "ordereddither";
        ColorCube cube = ColorCube.createColorCube(DataBuffer.TYPE_BYTE, 0, new int[] { 2, 2, 2 });
        pb.add(cube);
        pb.add(KernelJAI.DITHER_MASK_443);
    }

    // Create a layout containing an IndexColorModel which maps
    // zero to zero and unity to 255; force SampleModel to be bilevel.
    ImageLayout layout = new ImageLayout();
    byte[] map = new byte[] { (byte) 0x00, (byte) 0xff };
    ColorModel cm = new IndexColorModel(1, 2, map, map, map);
    layout.setColorModel(cm);
    SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, src.getWidth(), src.getHeight(), 1);
    layout.setSampleModel(sm);

    // Create a hint containing the layout.
    RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);

    // Dither the image.
    final PlanarImage dst = JAI.create(opName, pb, hints);

    // Exit on window closing.
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent we) {
            JAI.create("filestore", dst, fileName + ".out", "PNG", null);
            System.exit(0);
        }
    });

    // Display the result.
    //// ATTENTION A REMPLACER : add(new ScrollingImagePanel(dst, dst.getWidth(), dst.getHeight()));
    pack();
    setVisible(true);
}

From source file:PNGDecoder.java

public ColorModel getColorModel() {
    short colorType = getColorType();
    int bitsPerPixel = getBitsPerPixel();

    if (colorType == 3) {
        byte[] paletteData = getChunk("PLTE").getData();
        int paletteLength = paletteData.length / 3;
        return new IndexColorModel(bitsPerPixel, paletteLength, paletteData, 0, false);
    }/*w  w  w.j a  v  a2s.  c o m*/
    System.out.println("Unsupported color type: " + colorType);
    return null;
}

From source file:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java

public static ColorModel createGrayscaleColorModel(boolean invert) {
    byte[] rLUT = new byte[256];
    byte[] gLUT = new byte[256];
    byte[] bLUT = new byte[256];
    if (invert)//from   www  .j  a v  a  2s . c o m
        for (int i = 0; i < 256; i++) {
            rLUT[255 - i] = (byte) i;
            gLUT[255 - i] = (byte) i;
            bLUT[255 - i] = (byte) i;
        }
    else {
        for (int i = 0; i < 256; i++) {
            rLUT[i] = (byte) i;
            gLUT[i] = (byte) i;
            bLUT[i] = (byte) i;
        }
    }
    return (new IndexColorModel(8, 256, rLUT, gLUT, bLUT));
}

From source file:BMPLoader.java

public MemoryImageSource getImageSource() {
    ColorModel cm;//from   w ww .j a v a 2s . c  o m
    MemoryImageSource mis;
    if (noOfEntries > 0) {
        // There is a color palette; create an IndexColorModel
        cm = new IndexColorModel(bitsPerPixel, noOfEntries, r, g, b);
    } else {
        // There is no palette; use the default RGB color model
        cm = ColorModel.getRGBdefault();
    }
    // Create MemoryImageSource
    if (bitsPerPixel > 8) {
        // use one int per pixel
        mis = new MemoryImageSource(width, height, cm, intData, 0, width);
    } else {
        // use one byte per pixel
        mis = new MemoryImageSource(width, height, cm, byteData, 0, width);
    }
    return mis; // this can be used by JComponent.createImage()
}

From source file:com.sketchy.utils.image.SketchyImage.java

public static BufferedImage createByteImage(int width, int height) {
    IndexColorModel model = new IndexColorModel(8, 2, new byte[] { 0, (byte) 255 },
            new byte[] { 0, (byte) 255 }, new byte[] { 0, (byte) 255 });
    return new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, model);
}