Example usage for javax.imageio ImageTypeSpecifier createBufferedImage

List of usage examples for javax.imageio ImageTypeSpecifier createBufferedImage

Introduction

In this page you can find the example usage for javax.imageio ImageTypeSpecifier createBufferedImage.

Prototype

public BufferedImage createBufferedImage(int width, int height) 

Source Link

Document

Creates a BufferedImage with a given width and height according to the specification embodied in this object.

Usage

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

/**
 * Utility method for creating a BufferedImage from a source raster Currently only Float->Byte and Byte->Byte are supported. Will throw an
 * {@link UnsupportedOperationException} if the conversion is not supported.
 * /*from w  w w.  ja  v a 2 s .  c om*/
 * @param raster
 * @param imageType
 * @return
 */
public static BufferedImage rasterToBufferedImage(Raster raster, ImageTypeSpecifier imageType) {
    if (imageType == null) {
        if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE)
            imageType = ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false);
        else
            throw new IllegalArgumentException("unable to dynamically determine the imageType");
    }
    // create a new buffered image, for display
    BufferedImage bufImage = imageType.createBufferedImage(raster.getWidth(), raster.getHeight());

    if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT
            && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) {
        // convert short pixels to bytes
        short[] shortData = ((DataBufferUShort) raster.getDataBuffer()).getData();
        byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData();
        ImageIOUtils.shortToByteBuffer(shortData, byteData, 1, raster.getNumBands());
    } else if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_FLOAT
            && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) {
        // convert float pixels to bytes
        float[] floatData = ((DataBufferFloat) raster.getDataBuffer()).getData();
        byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData();
        ImageIOUtils.floatToByteBuffer(floatData, byteData, 1, raster.getNumBands());
    } else if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_DOUBLE
            && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) {
        // convert double pixels to bytes
        double[] doubleData = ((DataBufferDouble) raster.getDataBuffer()).getData();
        byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData();
        ImageIOUtils.doubleToByteBuffer(doubleData, byteData, 1, raster.getNumBands());
    } else if ((raster.getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE
            && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE)
            || (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT
                    && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT)
            || (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_SHORT
                    && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_SHORT)) {
        bufImage.setData(raster);
    } else {
        throw new UnsupportedOperationException(
                "Unable to convert raster type to bufferedImage type: " + raster.getDataBuffer().getDataType()
                        + " ==> " + bufImage.getRaster().getDataBuffer().getDataType());
    }
    return bufImage;
}

From source file:brut.androlib.res.decoder.Res9patchStreamDecoder.java

@Override
public void decode(InputStream in, OutputStream out) throws AndrolibException {
    try {/*ww  w  .  j a  va  2s. c o  m*/
        byte[] data = IOUtils.toByteArray(in);

        BufferedImage im = ImageIO.read(new ByteArrayInputStream(data));
        int w = im.getWidth(), h = im.getHeight();

        ImageTypeSpecifier its = ImageTypeSpecifier.createFromRenderedImage(im);
        BufferedImage im2 = its.createBufferedImage(w + 2, h + 2);

        im2.getRaster().setRect(1, 1, im.getRaster());
        NinePatch np = getNinePatch(data);
        drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight);
        drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom);

        int[] xDivs = np.xDivs;
        for (int i = 0; i < xDivs.length; i += 2) {
            drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]);
        }

        int[] yDivs = np.yDivs;
        for (int i = 0; i < yDivs.length; i += 2) {
            drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]);
        }

        ImageIO.write(im2, "png", out);
    } catch (IOException ex) {
        throw new AndrolibException(ex);
    } catch (NullPointerException ex) {
        // In my case this was triggered because a .png file was
        // containing a html document instead of an image.
        // This could be more verbose and try to MIME ?
        throw new AndrolibException(ex);
    }
}

From source file:org.jimcat.services.imagemanager.ImageUtil.java

/**
 * this methode will load given image using tiles (saving memory)
 * //from   w w w. java 2s. c  om
 * this strategie is spliting the original image up into smaller parts
 * called tiles. Those tiles are downscaled one by one using given quality.
 * This results int probably best possible quality but may cost a lot of
 * time.
 * 
 * @param reader -
 *            the reader to load image from
 * @param size -
 *            the resulting image size
 * @param quality -
 *            the quality used for necessary rendering
 * @return the image as buffered image
 * @throws IOException
 */
@SuppressWarnings("unused")
private static BufferedImage loadImageWithTiles(ImageReader reader, Dimension size, ImageQuality quality)
        throws IOException {

    // the image buffer used to load tiles
    ImageTypeSpecifier imageSpec = reader.getImageTypes(0).next();
    BufferedImage tile = imageSpec.createBufferedImage(IMAGE_TILE_SIZE.width, IMAGE_TILE_SIZE.height);

    // the image the result is rendered into
    BufferedImage result = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = result.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, quality.getHint());

    // prepaire image reader parameter
    ImageReadParam param = reader.getDefaultReadParam();
    param.setDestination(tile);

    // count tiles
    int width = reader.getWidth(0);
    int height = reader.getHeight(0);
    int numX = (int) Math.ceil(width / (float) IMAGE_TILE_SIZE.width);
    int numY = (int) Math.ceil(height / (float) IMAGE_TILE_SIZE.height);

    float aspectX = (float) IMAGE_TILE_SIZE.width / width;
    float aspectY = (float) IMAGE_TILE_SIZE.height / height;

    // target tile dimension
    int targetTileWidth = (int) (size.width * aspectX);
    int targetTileHeight = (int) (size.height * aspectY);

    // load tiles
    Rectangle sourceRegion = new Rectangle();
    Rectangle targetRegion = new Rectangle();
    for (int i = 0; i < numX; i++) {
        // line increment
        sourceRegion.x = i * IMAGE_TILE_SIZE.width;
        sourceRegion.width = Math.min(IMAGE_TILE_SIZE.width, width - sourceRegion.x);
        targetRegion.x = i * targetTileWidth;
        targetRegion.width = Math.min(targetTileWidth, size.width - targetRegion.x);
        for (int j = 0; j < numY; j++) {
            // row increment
            sourceRegion.y = j * IMAGE_TILE_SIZE.height;
            sourceRegion.height = Math.min(IMAGE_TILE_SIZE.height, height - sourceRegion.y);
            targetRegion.y = j * targetTileHeight;
            targetRegion.height = Math.min(targetTileHeight, size.height - targetRegion.y);

            // performe read
            param.setSourceRegion(sourceRegion);
            reader.read(0, param);

            // insert into resulting image
            int dx1 = targetRegion.x;
            int dx2 = targetRegion.x + targetRegion.width;
            int dy1 = targetRegion.y;
            int dy2 = targetRegion.y + targetRegion.height;

            g.drawImage(tile, dx1, dy1, dx2, dy2, 0, 0, sourceRegion.width, sourceRegion.height, null);
        }
    }

    // finish drawing
    g.dispose();

    // return result
    return result;
}