Example usage for java.awt.image BufferedImage getData

List of usage examples for java.awt.image BufferedImage getData

Introduction

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

Prototype

public Raster getData() 

Source Link

Document

Returns the image as one large tile.

Usage

From source file:nitf.imagej.NITF_Reader.java

private static Object getPixelsFromBufferedImage(BufferedImage bufImage) {
    DataBuffer dataBuffer = bufImage.getData().getDataBuffer();
    if (dataBuffer.getDataType() == DataBuffer.TYPE_BYTE)
        return ((DataBufferByte) dataBuffer).getData();
    else if (dataBuffer.getDataType() == DataBuffer.TYPE_USHORT)
        return ((DataBufferUShort) dataBuffer).getData();
    return null;/* w  w w . jav a  2  s . c  o m*/
}

From source file:org.apache.pdfbox.filter.JBIG2Filter.java

/**
 * Decode JBIG2 data using Java ImageIO library.
 *
 * {@inheritDoc}/* w  w w.ja  v  a  2 s  . c o  m*/
 *
 */
public void decode(InputStream compressedData, OutputStream result, COSDictionary options, int filterIndex)
        throws IOException {
    BufferedImage bi = ImageIO.read(compressedData);
    if (bi != null) {
        DataBuffer dBuf = bi.getData().getDataBuffer();
        if (dBuf.getDataType() == DataBuffer.TYPE_BYTE) {
            result.write(((DataBufferByte) dBuf).getData());
        } else {
            log.error("Image data buffer not of type byte but type " + dBuf.getDataType());
        }
    } else {
        Iterator<ImageReader> reader = ImageIO.getImageReadersByFormatName("JBIG2");
        if (!reader.hasNext()) {
            log.error("Can't find an ImageIO plugin to decode the JBIG2 encoded datastream.");
        } else {
            log.error("Something went wrong when decoding the JBIG2 encoded datastream.");
        }
    }
}

From source file:org.apache.pdfbox.filter.JPXFilter.java

/**
 * Decode JPEG2000 data using Java ImageIO library.
 *
 * {@inheritDoc}//  w ww . ja v a  2  s . co m
 *
 */
public void decode(InputStream compressedData, OutputStream result, COSDictionary options, int filterIndex)
        throws IOException {
    BufferedImage bi = ImageIO.read(compressedData);
    if (bi != null) {
        DataBuffer dBuf = bi.getData().getDataBuffer();
        if (dBuf.getDataType() == DataBuffer.TYPE_BYTE) {
            result.write(((DataBufferByte) dBuf).getData());
        } else {
            log.error("Image data buffer not of type byte but type " + dBuf.getDataType());
        }
    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.java

private void createImageStream(PDDocument doc, BufferedImage bi) throws IOException {
    BufferedImage alphaImage = null;
    BufferedImage rgbImage = null;
    int width = bi.getWidth();
    int height = bi.getHeight();
    if (bi.getColorModel().hasAlpha()) {
        // extract the alpha information
        WritableRaster alphaRaster = bi.getAlphaRaster();
        ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false,
                Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        alphaImage = new BufferedImage(cm, alphaRaster, false, null);
        // create a RGB image without alpha
        rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = rgbImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.drawImage(bi, 0, 0, null);/*from w  w w  .  ja v  a 2  s  .c  o m*/
    } else {
        rgbImage = bi;
    }
    java.io.OutputStream os = null;
    try {
        int numberOfComponents = rgbImage.getColorModel().getNumComponents();
        if (numberOfComponents == 3) {
            setColorSpace(PDDeviceRGB.INSTANCE);
        } else {
            if (numberOfComponents == 1) {
                setColorSpace(new PDDeviceGray());
            } else {
                throw new IllegalStateException();
            }
        }
        byte[] outData = new byte[width * height * numberOfComponents];
        rgbImage.getData().getDataElements(0, 0, width, height, outData);
        // add FlateDecode compression
        getPDStream().addCompression();
        os = getCOSStream().createUnfilteredStream();
        os.write(outData);

        COSDictionary dic = getCOSStream();
        dic.setItem(COSName.FILTER, COSName.FLATE_DECODE);
        dic.setItem(COSName.SUBTYPE, COSName.IMAGE);
        dic.setItem(COSName.TYPE, COSName.XOBJECT);
        if (alphaImage != null) {
            PDPixelMap smask = new PDPixelMap(doc, alphaImage);
            dic.setItem(COSName.SMASK, smask);
        }
        setBitsPerComponent(8);
        setHeight(height);
        setWidth(width);
    } finally {
        os.close();
    }
}

From source file:org.dcm4che.tool.dcm2jpg.Dcm2Jpg.java

private BufferedImage convert(BufferedImage bi) {
    ColorModel cm = bi.getColorModel();
    if (cm instanceof PaletteColorModel)
        return ((PaletteColorModel) cm).convertToIntDiscrete(bi.getData());
    return bi;//from  w w w  . j  ava 2  s .c o  m
}

From source file:org.dcm4che2.tool.dcm2dcm.Dcm2Dcm.java

/**
 * Read the raw raster data from the input stream without applying a VOI LUT, if one
 * is present./*from w  w w . ja v  a 2 s. c o m*/
 */
private Raster readRaster(java.io.InputStream inputStream, int frame)
        throws IOException, NoSuchFieldException, IllegalAccessException {

    ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);

    // Mark the stream position, since we may need to rewind
    imageInputStream.mark();

    DicomImageReader regularReader = (DicomImageReader) new DicomImageReaderSpi().createReaderInstance();
    regularReader.setInput(imageInputStream);

    // Make sure we read the DICOM data, so that the 'compressed' flag is properly set.
    DicomStreamMetaData metadata = (DicomStreamMetaData) regularReader.getStreamMetadata();
    metadata.getDicomObject();

    DicomImageReadParam params = (DicomImageReadParam) regularReader.getDefaultReadParam();

    // If the reader has determined that this is compressed data, it will enter a conditional block and
    // potentially throw an exception in readRaster.
    // We check here and avoid that block if the compressed flag is true.

    if (isCompressed(regularReader)) {
        // Rewind the stream
        imageInputStream.reset();

        // Use the custom reader
        DicomImageReaderNoVoiLut reader = (DicomImageReaderNoVoiLut) new DicomImageReaderNoVoiLutSpi()
                .createReaderInstance();
        reader.setInput(imageInputStream);

        BufferedImage image16 = reader.read(frame, params);
        return image16.getData();
    } else {
        return regularReader.readRaster(frame, params);
    }
}

From source file:org.deegree.framework.util.ImageUtils.java

/**
 *
 *
 * @param out/*from   w  w w . j a va  2 s.com*/
 * @param img
 *
 * @throws IOException
 */
private static void encodePng(OutputStream out, BufferedImage img) throws IOException {
    PNGEncodeParam encodeParam = PNGEncodeParam.getDefaultEncodeParam(img);

    if (encodeParam instanceof PNGEncodeParam.Palette) {
        PNGEncodeParam.Palette p = (PNGEncodeParam.Palette) encodeParam;
        byte[] b = new byte[] { -127 };
        p.setPaletteTransparency(b);
    }

    com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder("PNG", out, encodeParam);
    encoder.encode(img.getData(), img.getColorModel());
}

From source file:org.deegree.services.wps.provider.jrxml.contentprovider.map.MapContentProvider.java

private File writeImage(BufferedImage bi) throws ProcessletException {
    FileOutputStream fos = null;/*  w w  w. jav  a 2  s.  c o m*/
    try {
        File f = File.createTempFile("image", ".png");
        fos = new FileOutputStream(f);

        PNGEncodeParam encodeParam = PNGEncodeParam.getDefaultEncodeParam(bi);

        if (encodeParam instanceof PNGEncodeParam.Palette) {
            PNGEncodeParam.Palette p = (PNGEncodeParam.Palette) encodeParam;
            byte[] b = new byte[] { -127 };
            p.setPaletteTransparency(b);
        }
        com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder("PNG", fos, encodeParam);
        encoder.encode(bi.getData(), bi.getColorModel());
        LOG.debug("Wrote image to file: {}", f.toString());
        return f;
    } catch (IOException e) {
        String msg = "Could not write image to file: " + e.getMessage();
        LOG.debug(msg, e);
        throw new ProcessletException(msg);
    } finally {
        IOUtils.closeQuietly(fos);
    }
}

From source file:org.deegree.tile.persistence.geotiff.GeoTIFFTile.java

@Override
public BufferedImage getAsImage() throws TileIOException {
    ImageReader reader = null;/* ww  w  . j  a v a  2s. c o  m*/
    try {
        reader = (ImageReader) readerPool.borrowObject();
        BufferedImage img = reader.readTile(imageIndex, x, y);
        if (img.getWidth() != sizeX || img.getHeight() != sizeY) {
            Hashtable<Object, Object> table = new Hashtable<Object, Object>();
            String[] props = img.getPropertyNames();
            if (props != null) {
                for (String p : props) {
                    table.put(p, img.getProperty(p));
                }
            }
            BufferedImage img2 = new BufferedImage(img.getColorModel(),
                    img.getData().createCompatibleWritableRaster(sizeX, sizeY), img.isAlphaPremultiplied(),
                    table);
            Graphics2D g = img2.createGraphics();
            g.drawImage(img, 0, 0, null);
            g.dispose();
            img = img2;
        }
        return img;
    } catch (Exception e) {
        throw new TileIOException("Error retrieving image: " + e.getMessage(), e);
    } finally {
        try {
            readerPool.returnObject(reader);
        } catch (Exception e) {
            // ignore closing error
        }
    }
}

From source file:org.mrgeo.resources.tms.TileMapServiceResource.java

protected static Response createEmptyTile(final ImageResponseWriter writer, final int width, final int height) {
    // return an empty image
    final int dataType;
    if (writer.getResponseMimeType().equals("image/jpeg")) {
        dataType = BufferedImage.TYPE_3BYTE_BGR;
    } else {/*  ww  w.j  a v a 2  s .co m*/
        // dataType = BufferedImage.TYPE_INT_ARGB;
        dataType = BufferedImage.TYPE_4BYTE_ABGR;
    }

    final BufferedImage bufImg = new BufferedImage(width, height, dataType);
    final Graphics2D g = bufImg.createGraphics();
    g.setColor(new Color(0, 0, 0, 0));
    g.fillRect(0, 0, width, height);
    g.dispose();

    return writer.write(bufImg.getData()).build();
}