Example usage for java.awt.image DataBufferByte DataBufferByte

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

Introduction

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

Prototype

public DataBufferByte(byte[][] dataArray, int size) 

Source Link

Document

Constructs a byte-based DataBuffer with the specified arrays.

Usage

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:com.alvermont.terraj.planet.io.ImageBuilder.java

/**
 * Create and return a <code>BufferedImage</code> object from the
 * result of the projection. This image can then be further processed
 * or written out to a file using <code>ImageIO</code>.
 *
 * @param proj The projection that will provide the image
 * @return A <code>BufferedImage</code> object containing the results of
 * the projection.//from w  w w . j av  a 2  s .c  o  m
 */
public BufferedImage getImage(Projector proj) {
    // get the pixel data and store it into a data buffer
    final byte[] pixels = getPixels(proj);

    final DataBuffer db = new DataBufferByte(pixels, pixels.length);

    // set up offsets for the R,G,B elements
    final int[] offsets = new int[3];

    offsets[0] = 0;
    offsets[1] = 1;
    offsets[2] = 2;

    // create the raster from the pixel data
    final int height = proj.getParameters().getProjectionParameters().getHeight();
    final int width = proj.getParameters().getProjectionParameters().getWidth();

    final WritableRaster raster = Raster.createInterleavedRaster(db, width, height, width * 3, 3, offsets,
            null);

    // create a colour model that matches the raster we have
    final ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
            Transparency.OPAQUE, DataBuffer.TYPE_BYTE);

    // combine raster and colour model into a buffered image
    final BufferedImage img = new BufferedImage(cm, raster, false, null);

    return img;
}

From source file:PNGDecoder.java

public WritableRaster getRaster() {
    int width = (int) getWidth();
    int height = (int) getHeight();
    int bitsPerPixel = getBitsPerPixel();
    short colorType = getColorType();

    if (colorType == 3) {
        byte[] imageData = getImageData();
        DataBuffer db = new DataBufferByte(imageData, imageData.length);
        WritableRaster raster = Raster.createPackedRaster(db, width, height, bitsPerPixel, null);
        return raster;
    } else/*from w  w w .  j  a  v a 2  s . c  o m*/
        System.out.println("Unsupported color type!");
    return null;
}

From source file:TestImage2.java

public static RenderedImage toRenderedImage(String[] rows) {
    // Create the DataBuffer to hold the pixel samples
    final int width = rows[0].length();
    final int height = rows.length;
    final int size = width * height;
    byte[] pixels = new byte[size];
    int index = 0;
    for (String row : rows) {
        for (int x = 0; x < width; x++) {
            pixels[index++] = (byte) decodeGray(row.charAt(x));
        }//from www. j a va  2s.c om
    }
    DataBuffer dataBuffer = new DataBufferByte(pixels, size);

    // Create Raster
    WritableRaster writableRaster = Raster.createBandedRaster(dataBuffer, width, height, width, // scanlineStride
            new int[] { 0 }, // bankIndices,
            new int[] { 0 }, // bandOffsets,
            null); // location

    // Create the image
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    bufferedImage.setData(writableRaster);

    return bufferedImage;
}

From source file:org.geoserver.jai.ConcurrentTileFactory.java

/**
 * Builds a new tile, eventually recycling the data array backing it
 *//*from  ww w .  ja  va 2s.c o  m*/
public WritableRaster createTile(SampleModel sampleModel, Point location) {
    // sanity checks
    if (sampleModel == null) {
        throw new NullPointerException("sampleModel cannot be null");
    }
    if (location == null) {
        location = new Point(0, 0);
    }

    DataBuffer db = null;

    // get the three elements making the key into the recycled array map
    int type = sampleModel.getTransferType();
    long numBanks = 0;
    long size = 0;
    if (sampleModel instanceof ComponentSampleModel) {
        ComponentSampleModel csm = (ComponentSampleModel) sampleModel;
        numBanks = getNumBanksCSM(csm);
        size = getBufferSizeCSM(csm);
    } else if (sampleModel instanceof MultiPixelPackedSampleModel) {
        MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sampleModel;
        numBanks = 1;
        int dataTypeSize = DataBuffer.getDataTypeSize(type);
        size = mppsm.getScanlineStride() * mppsm.getHeight()
                + (mppsm.getDataBitOffset() + dataTypeSize - 1) / dataTypeSize;
    } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
        SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
        numBanks = 1;
        size = sppsm.getScanlineStride() * (sppsm.getHeight() - 1) + sppsm.getWidth();
    }

    if (size > 0) {
        // try to build a new data buffer starting from 
        Object array = recycledArrays.getRecycledArray(type, numBanks, size);
        if (array != null) {
            switch (type) {
            case DataBuffer.TYPE_BYTE: {
                byte[][] bankData = (byte[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], (byte) 0);
                }
                db = new DataBufferByte(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_USHORT: {
                short[][] bankData = (short[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], (short) 0);
                }
                db = new DataBufferUShort(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_SHORT: {
                short[][] bankData = (short[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], (short) 0);
                }
                db = new DataBufferShort(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_INT: {
                int[][] bankData = (int[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], 0);
                }
                db = new DataBufferInt(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_FLOAT: {
                float[][] bankData = (float[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], 0.0F);
                }
                db = DataBufferUtils.createDataBufferFloat(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_DOUBLE: {
                double[][] bankData = (double[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], 0.0);
                }
                db = DataBufferUtils.createDataBufferDouble(bankData, (int) size);
            }
                break;
            default:
                throw new IllegalArgumentException("Unknown array type");
            }
        }
    }

    if (db == null) {
        db = sampleModel.createDataBuffer();
    }

    return Raster.createWritableRaster(sampleModel, db, location);
}

From source file:c.depthchart.ViewerPanel.java

public void paintComponent(Graphics g)
// Draw the depth image and statistics info
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    // convert image pixel array into an image
    DataBufferByte dataBuffer = new DataBufferByte(imgbytes, imWidth * imHeight);
    Raster raster = Raster.createPackedRaster(dataBuffer, imWidth, imHeight, 8, null);
    image.setData(raster);/* ww  w.j  av a  2  s.  c  o m*/
    if (image != null)
        g2.drawImage(image, 0, 0, this);

    writeStats(g2);
}

From source file:TrackerPanel.java

private void drawUserDepths(Graphics2D g2d) {
    // define an 8-bit RGB channel color model
    ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
            new int[] { 8, 8, 8 }, false, false, ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE);

    // fill the raster with the depth image bytes
    DataBufferByte dataBuffer = new DataBufferByte(imgbytes, imWidth * imHeight * 3);

    WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, imWidth, imHeight, imWidth * 3, 3,
            new int[] { 0, 1, 2 }, null);

    // combine color model and raster to create a BufferedImage
    BufferedImage image = new BufferedImage(colorModel, raster, false, null);

    g2d.drawImage(image, 0, 0, null);//from w  w w  . ja v a  2 s. c o m
}

From source file:org.kuali.kra.printing.service.impl.PersonSignatureServiceImpl.java

/**
 * This method is to get buffered image based on predefined parameters
 * @param params/*  w w  w  .  java 2s  . c o  m*/
 * @param imageData
 * @return
 */
protected BufferedImage getBufferedImage(ImageParameters params, byte[] imageData) {
    byte[] transparentColors = new byte[] { (byte) 0xFF, (byte) 0xFF };
    byte[] colors = new byte[] { 0, (byte) 0xFF };
    IndexColorModel colorModel = new IndexColorModel(1, 2, colors, colors, colors, transparentColors);
    BufferedImage image = new BufferedImage(params.getWidth(), params.getHeight(),
            BufferedImage.TYPE_BYTE_BINARY, colorModel);
    DataBufferByte buffer = new DataBufferByte(imageData, 1);
    WritableRaster raster = Raster.createPackedRaster(buffer, params.getWidth(), params.getHeight(),
            params.getBitsPerComponent(), new Point(0, 0));
    image.setData(raster);
    return image;
}

From source file:org.hippoecm.frontend.plugins.gallery.imageutil.ImageUtils.java

/**
 * Converts image raster data to a JPEG with RGB color space. Only images with color space CMYK and YCCK are
 * converted, other images are left untouched.
 *
 * Rationale: Java's ImageIO can't process 4-component images and Java2D can't apply AffineTransformOp either,
 * so we have to convert raster data to a JPG with RGB color space.
 *
 * The technique used in this method is due to Mark Stephens, and free for any use. See
 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903 or
 * http://www.mail-archive.com/java2d-interest@capra.eng.sun.com/msg03247.html
 *
 * @param is the image data//from  w ww . ja v a 2  s  .co m
 * @param colorModel the color model of the image
 * @return the RGB version of the supplied image
 */
public static InputStream convertToRGB(InputStream is, ColorModel colorModel)
        throws IOException, UnsupportedImageException {
    if (colorModel != ColorModel.CMYK && colorModel != ColorModel.YCCK) {
        return is;
    }

    // Get an ImageReader.
    ImageInputStream input = ImageIO.createImageInputStream(is);

    try {
        Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
        if (readers == null || !readers.hasNext()) {
            throw new UnsupportedImageException("No ImageReaders found");
        }

        ImageReader reader = readers.next();
        reader.setInput(input);
        Raster raster = reader.readRaster(0, reader.getDefaultReadParam());

        int w = raster.getWidth();
        int h = raster.getHeight();
        byte[] rgb = new byte[w * h * 3];

        switch (colorModel) {
        case YCCK: {
            float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
            float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
            float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
            float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);

            for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
                float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];

                double val = y + 1.402 * (cr - 128) - k;
                val = (val - 128) * .65f + 128;
                rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);

                val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
                val = (val - 128) * .65f + 128;
                rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);

                val = y + 1.772 * (cb - 128) - k;
                val = (val - 128) * .65f + 128;
                rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
            }
            break;
        }
        case CMYK: {
            int[] C = raster.getSamples(0, 0, w, h, 0, (int[]) null);
            int[] M = raster.getSamples(0, 0, w, h, 1, (int[]) null);
            int[] Y = raster.getSamples(0, 0, w, h, 2, (int[]) null);
            int[] K = raster.getSamples(0, 0, w, h, 3, (int[]) null);

            for (int i = 0, imax = C.length, base = 0; i < imax; i++, base += 3) {
                int c = 255 - C[i];
                int m = 255 - M[i];
                int y = 255 - Y[i];
                int k = 255 - K[i];
                float kk = k / 255f;

                rgb[base] = (byte) (255 - Math.min(255f, c * kk + k));
                rgb[base + 1] = (byte) (255 - Math.min(255f, m * kk + k));
                rgb[base + 2] = (byte) (255 - Math.min(255f, y * kk + k));
            }
            break;
        }
        }

        // from other image types we know InterleavedRaster's can be
        // manipulated by AffineTransformOp, so create one of those.
        raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3,
                new int[] { 0, 1, 2 }, null);

        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        java.awt.image.ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE,
                DataBuffer.TYPE_BYTE);
        BufferedImage convertedImage = new BufferedImage(cm, (WritableRaster) raster, true, null);

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(convertedImage, "jpg", os);

        return new ByteArrayInputStream(os.toByteArray());
    } finally {
        IOUtils.closeQuietly(is);
        if (input != null) {
            input.close();
        }
    }
}

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  ww  w. j a v  a  2 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);
}