Example usage for java.awt.image MultiPixelPackedSampleModel MultiPixelPackedSampleModel

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

Introduction

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

Prototype

public MultiPixelPackedSampleModel(int dataType, int w, int h, int numberOfBits) 

Source Link

Document

Constructs a MultiPixelPackedSampleModel with the specified data type, width, height and number of bits per pixel.

Usage

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);// www. j  a  v a  2  s  .c om
    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:org.apache.fop.render.pcl.PCLGenerator.java

/**
 * Paint a bitmap at the current cursor position. The bitmap must be a monochrome
 * (1-bit) bitmap image./*from   ww  w.j av  a  2 s .  co  m*/
 * @param img the bitmap image (must be 1-bit b/w)
 * @param resolution the resolution of the image (must be a PCL resolution)
 * @throws IOException In case of an I/O error
 */
public void paintMonochromeBitmap(RenderedImage img, int resolution) throws IOException {
    if (!isValidPCLResolution(resolution)) {
        throw new IllegalArgumentException("Invalid PCL resolution: " + resolution);
    }
    boolean monochrome = isMonochromeImage(img);
    if (!monochrome) {
        throw new IllegalArgumentException("img must be a monochrome image");
    }

    setRasterGraphicsResolution(resolution);
    writeCommand("*r0f" + img.getHeight() + "t" + img.getWidth() + "s1A");
    Raster raster = img.getData();

    Encoder encoder = new Encoder(img);
    // Transfer graphics data
    int imgw = img.getWidth();
    IndexColorModel cm = (IndexColorModel) img.getColorModel();
    if (cm.getTransferType() == DataBuffer.TYPE_BYTE) {
        DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
        MultiPixelPackedSampleModel packedSampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
                img.getWidth(), img.getHeight(), 1);
        if (img.getSampleModel().equals(packedSampleModel) && dataBuffer.getNumBanks() == 1) {
            //Optimized packed encoding
            byte[] buf = dataBuffer.getData();
            int scanlineStride = packedSampleModel.getScanlineStride();
            int idx = 0;
            int c0 = toGray(cm.getRGB(0));
            int c1 = toGray(cm.getRGB(1));
            boolean zeroIsWhite = c0 > c1;
            for (int y = 0, maxy = img.getHeight(); y < maxy; y++) {
                for (int x = 0, maxx = scanlineStride; x < maxx; x++) {
                    if (zeroIsWhite) {
                        encoder.add8Bits(buf[idx]);
                    } else {
                        encoder.add8Bits((byte) ~buf[idx]);
                    }
                    idx++;
                }
                encoder.endLine();
            }
        } else {
            //Optimized non-packed encoding
            for (int y = 0, maxy = img.getHeight(); y < maxy; y++) {
                byte[] line = (byte[]) raster.getDataElements(0, y, imgw, 1, null);
                for (int x = 0, maxx = imgw; x < maxx; x++) {
                    encoder.addBit(line[x] == 0);
                }
                encoder.endLine();
            }
        }
    } else {
        //Safe but slow fallback
        for (int y = 0, maxy = img.getHeight(); y < maxy; y++) {
            for (int x = 0, maxx = imgw; x < maxx; x++) {
                int sample = raster.getSample(x, y, 0);
                encoder.addBit(sample == 0);
            }
            encoder.endLine();
        }
    }

    // End raster graphics
    writeCommand("*rB");
}