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:gdsc.utils.Cell_Outliner.java

private void applyColorModel(ImagePlus imp) {
    // Load the spectrum LUT 
    WindowManager.setTempCurrentImage(imp);
    LutLoader lut = new LutLoader();
    lut.run("spectrum");
    WindowManager.setTempCurrentImage(null);

    // Set zero to black
    ImageProcessor ip = imp.getProcessor();
    IndexColorModel cm = (IndexColorModel) ip.getColorModel();
    byte[] r = new byte[256];
    byte[] b = new byte[256];
    byte[] g = new byte[256];
    cm.getReds(r);//  www  . j  a  v  a2 s .  co m
    cm.getBlues(b);
    cm.getGreens(g);
    r[0] = 0;
    b[0] = 0;
    g[0] = 0;
    cm = new IndexColorModel(8, 256, r, g, b);
    ip.setColorModel(cm);
}

From source file:MyJava3D.java

private BufferedImage createBinaryImage(int w, int h, int pixelBits) {

    int[] pixels = new int[w * h];
    int bytesPerRow = w * pixelBits / 8;
    if (w * pixelBits % 8 != 0) {
        bytesPerRow++;//from www .j  av a2  s  .  c om
    }
    byte[] imageData = new byte[h * bytesPerRow];
    IndexColorModel cm = null;
    switch (pixelBits) {
    case 1:
        cm = new IndexColorModel(pixelBits, lut1Arr.length, lut1Arr, lut1Arr, lut1Arr);
        break;
    case 2:
        cm = new IndexColorModel(pixelBits, lut2Arr.length, lut2Arr, lut2Arr, lut2Arr);
        break;
    case 4:
        cm = new IndexColorModel(pixelBits, lut4Arr.length, lut4Arr, lut4Arr, lut4Arr);
        break;
    default: {
        new Exception("Invalid # of bit per pixel").printStackTrace();
    }
    }

    DataBuffer db = new DataBufferByte(imageData, imageData.length);
    WritableRaster r = Raster.createPackedRaster(db, w, h, pixelBits, null);
    return new BufferedImage(cm, r, false, null);
}

From source file:com.jcraft.weirdx.XColormap.java

void mkIcm() {
    if ((visual.clss & DynamicClass) != 0 || (flags & BeingCreated) != 0) {
        if (visual.depth.depth == 16)
            return;
        cm = new IndexColorModel((r.length == 256 ? 8 : 1), r.length, r, g, b);
    }/*from w w w  . jav  a2 s  . c  o m*/
    icmtime = System.currentTimeMillis();
}

From source file:odcplot.OdcPlot.java

/**
 * Generate a Index Color Model with our color coding and establish text for color legend (if enabled)
 * /*from   w  w  w .j  ava2 s.  c  om*/
 * @return 
 */
private IndexColorModel getColorModel() {
    byte[] r = new byte[256];
    byte[] g = new byte[256];
    byte[] b = new byte[256];
    int rd, gr, bl;

    for (int i = 0; i < 256; i++) {
        r[i] = g[i] = b[i] = 0;

        if (i < keyColors.length) {
            rd = keyColors[i].getRed();
            gr = keyColors[i].getGreen();
            bl = keyColors[i].getBlue();
        } else {
            int intensity = Math.round((i - 8) / 255.f * 255);
            rd = gr = bl = intensity;
        }

        // deal with signed bytes and unsigned 8-bit index colors
        r[i] |= rd & 0xff;
        g[i] |= gr & 0xff;
        b[i] |= bl & 0xff;
    }
    IndexColorModel ret = new IndexColorModel(8, 256, r, g, b);
    return ret;
}

From source file:org.apache.fop.render.pcl.PCLGenerator.java

private RenderedImage getMask(RenderedImage img, Dimension targetDim) {
    ColorModel cm = img.getColorModel();
    if (cm.hasAlpha()) {
        BufferedImage alpha = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Raster raster = img.getData();
        GraphicsUtil.copyBand(raster, cm.getNumColorComponents(), alpha.getRaster(), 0);

        BufferedImageOp op1 = new LookupOp(new ByteLookupTable(0, THRESHOLD_TABLE), null);
        BufferedImage alphat = op1.filter(alpha, null);

        BufferedImage mask;/*w w  w  . ja v  a 2  s. c o  m*/
        if (true) {
            mask = new BufferedImage(targetDim.width, targetDim.height, BufferedImage.TYPE_BYTE_BINARY);
        } else {
            byte[] arr = { (byte) 0, (byte) 0xff };
            ColorModel colorModel = new IndexColorModel(1, 2, arr, arr, arr);
            WritableRaster wraster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, targetDim.width,
                    targetDim.height, 1, 1, null);
            mask = new BufferedImage(colorModel, wraster, false, null);
        }

        Graphics2D g2d = mask.createGraphics();
        try {
            AffineTransform at = new AffineTransform();
            double sx = targetDim.getWidth() / img.getWidth();
            double sy = targetDim.getHeight() / img.getHeight();
            at.scale(sx, sy);
            g2d.drawRenderedImage(alphat, at);
        } finally {
            g2d.dispose();
        }
        /*
        try {
        BatchDiffer.saveAsPNG(alpha, new java.io.File("D:/out-alpha.png"));
        BatchDiffer.saveAsPNG(mask, new java.io.File("D:/out-mask.png"));
        } catch (IOException e) {
        e.printStackTrace();
        }*/
        return mask;
    } else {
        return null;
    }
}