Example usage for java.awt.image DataBuffer TYPE_BYTE

List of usage examples for java.awt.image DataBuffer TYPE_BYTE

Introduction

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

Prototype

int TYPE_BYTE

To view the source code for java.awt.image DataBuffer TYPE_BYTE.

Click Source Link

Document

Tag for unsigned byte data.

Usage

From source file:org.apache.pdfbox.rendering.TilingPaint.java

/**
 * Returns the pattern image in parent stream coordinates.
 *///from ww w.j  a va  2  s .  com
private BufferedImage getImage(PageDrawer drawer, PDTilingPattern pattern, PDColorSpace colorSpace,
        PDColor color, AffineTransform xform, Rectangle2D anchorRect) throws IOException {
    ColorSpace outputCS = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new ComponentColorModel(outputCS, true, false, Transparency.TRANSLUCENT,
            DataBuffer.TYPE_BYTE);

    float width = (float) Math.abs(anchorRect.getWidth());
    float height = (float) Math.abs(anchorRect.getHeight());

    // device scale transform (i.e. DPI) (see PDFBOX-1466.pdf)
    Matrix xformMatrix = new Matrix(xform);
    float xScale = Math.abs(xformMatrix.getScalingFactorX());
    float yScale = Math.abs(xformMatrix.getScalingFactorY());
    width *= xScale;
    height *= yScale;

    int rasterWidth = Math.max(1, ceiling(width));
    int rasterHeight = Math.max(1, ceiling(height));

    // create raster
    WritableRaster raster = cm.createCompatibleWritableRaster(rasterWidth, rasterHeight);
    BufferedImage image = new BufferedImage(cm, raster, false, null);

    Graphics2D graphics = image.createGraphics();

    // flip a -ve YStep around its own axis (see gs-bugzilla694385.pdf)
    if (pattern.getYStep() < 0) {
        graphics.translate(0, rasterHeight);
        graphics.scale(1, -1);
    }

    // flip a -ve XStep around its own axis
    if (pattern.getXStep() < 0) {
        graphics.translate(rasterWidth, 0);
        graphics.scale(-1, 1);
    }

    // device scale transform (i.e. DPI)
    graphics.scale(xScale, yScale);

    // apply only the scaling from the pattern transform, doing scaling here improves the
    // image quality and prevents large scale-down factors from creating huge tiling cells.
    Matrix newPatternMatrix;
    newPatternMatrix = Matrix.getScaleInstance(Math.abs(patternMatrix.getScalingFactorX()),
            Math.abs(patternMatrix.getScalingFactorY()));

    // move origin to (0,0)
    newPatternMatrix.concatenate(Matrix.getTranslateInstance(-pattern.getBBox().getLowerLeftX(),
            -pattern.getBBox().getLowerLeftY()));

    // render using PageDrawer
    drawer.drawTilingPattern(graphics, pattern, colorSpace, color, newPatternMatrix);
    graphics.dispose();

    return image;
}

From source file:nl.ru.ai.projects.parrot.tools.TwitterAccess.java

/**
 * Returns a RenderedImage object from a byte array
 * @param cameraOutput The camera output to transform into a RenderedImage
 * @return The RenderedImage that resulted from the camera output
 *///ww w  .  j a  v a2 s  .  com
private RenderedImage getImageFromCamera(byte[] cameraOutput) {
    BufferedImage image = new BufferedImage(VideoPollInterface.FRONT_VIDEO_FRAME_WIDTH,
            VideoPollInterface.FRONT_VIDEO_FRAME_HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
    if (cameraOutput != null) {
        WritableRaster raster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE,
                VideoPollInterface.FRONT_VIDEO_FRAME_WIDTH, VideoPollInterface.FRONT_VIDEO_FRAME_HEIGHT, 3,
                new Point(0, 0));
        raster.setDataElements(0, 0, VideoPollInterface.FRONT_VIDEO_FRAME_WIDTH,
                VideoPollInterface.FRONT_VIDEO_FRAME_HEIGHT, cameraOutput);
        image.setData(raster);
    }

    return image;
}

From source file:org.apache.xmlgraphics.image.loader.impl.PNGFile.java

public ImageRawPNG getImageRawPNG(final ImageInfo info) throws ImageException, IOException {
    try (final InputStream seqStream = new SequenceInputStream(Collections.enumeration(this.streamVec))) {
        switch (this.colorType) {
        case PNG_COLOR_GRAY:
            if (this.hasPalette) {
                throw new ImageException("Corrupt PNG: color palette is not allowed!");
            }//from www  . j a  va  2 s .co  m
            this.colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
            break;
        case PNG_COLOR_RGB:
            // actually a check of the sRGB chunk would be necessary to
            // confirm
            // if it's really sRGB
            this.colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
            break;
        case PNG_COLOR_PALETTE:
            if (this.hasAlphaPalette) {
                this.colorModel = new IndexColorModel(this.bitDepth, this.paletteEntries, this.redPalette,
                        this.greenPalette, this.bluePalette, this.alphaPalette);
            } else {
                this.colorModel = new IndexColorModel(this.bitDepth, this.paletteEntries, this.redPalette,
                        this.greenPalette, this.bluePalette);
            }
            break;
        case PNG_COLOR_GRAY_ALPHA:
            if (this.hasPalette) {
                throw new ImageException("Corrupt PNG: color palette is not allowed!");
            }
            this.colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), true, false,
                    Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
            break;
        case PNG_COLOR_RGB_ALPHA:
            // actually a check of the sRGB chunk would be necessary to
            // confirm
            // if it's really sRGB
            this.colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false,
                    Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
            break;
        default:
            throw new ImageException("Unsupported color type: " + this.colorType);
        }
        // the iccProfile is still null for now
        final ImageRawPNG rawImage = new ImageRawPNG(info, seqStream, this.colorModel, this.bitDepth,
                this.iccProfile);
        if (this.isTransparent) {
            if (this.colorType == PNG_COLOR_GRAY) {
                rawImage.setGrayTransparentAlpha(this.grayTransparentAlpha);
            } else if (this.colorType == PNG_COLOR_RGB) {
                rawImage.setRGBTransparentAlpha(this.redTransparentAlpha, this.greenTransparentAlpha,
                        this.blueTransparentAlpha);
            } else if (this.colorType == PNG_COLOR_PALETTE) {
                rawImage.setTransparent();
            } else {
                //
            }
        }
        return rawImage;
    }
}

From source file:omr.jai.TestImage3.java

    public static PlanarImage decodeImage (String[] rows)
    {/* w w w  .  ja  v a2 s  . c o  m*/
        // Create the DataBuffer to hold the pixel samples
        final int width = rows[0].length();
        final int height = rows.length;

        // Create Raster
        Raster raster;
        if (true) {
            raster = Raster.createPackedRaster
            (DataBuffer.TYPE_INT, width, height,
             new int[] {0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000},// bandMasks RGBA
             null);
        } else {
            raster = Raster.createInterleavedRaster
                (DataBuffer.TYPE_BYTE, width, height,
                 4,// num of bands
                 null);
        }

        // Populate the data buffer
        DataBuffer dataBuffer = raster.getDataBuffer();
        int index = 0;
        for (String row : rows) {
            for (int x = 0; x < width; x++) {
                int argb = toARGB(row.charAt(x));
                dataBuffer.setElem(index, argb);
                index++;
            }
        }

        // Dump
//         final int size = width * height;
//         System.out.println("DataBuffer :");
//         for (int i = 0; i < size; i++) {
//             if (i % width == 0) {
//                 System.out.println();
//             }
//             System.out.print(String.format("%8x ", dataBuffer.getElem(i)));
//         }
//         System.out.println();

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

        // Dump
//         System.out.println("BufferedImage :");
//         for (int y = 0; y < height; y++) {
//             System.out.println();
//             for (int x = 0; x < width; x++) {
//                 System.out.print(String.format("%8x ", bufferedImage.getRGB(x, y)));
//             }
//         }
//         System.out.println();

        return PlanarImage.wrapRenderedImage(bufferedImage);
    }

From source file:org.mrgeo.image.MrsImagePyramidMetadata.java

public static int toTileType(final String tiletype) {
    if (tiletype == "Byte") {
        return DataBuffer.TYPE_BYTE;
    }/* w w  w . j a  v a  2s. c om*/
    if (tiletype == "Float") {
        return DataBuffer.TYPE_FLOAT;
    }
    if (tiletype == "Double") {
        return DataBuffer.TYPE_DOUBLE;
    }
    if (tiletype == "Int") {
        return DataBuffer.TYPE_INT;
    }
    if (tiletype == "Short") {
        return DataBuffer.TYPE_SHORT;
    }
    if (tiletype == "UShort") {
        return DataBuffer.TYPE_USHORT;
    }

    return DataBuffer.TYPE_UNDEFINED;
}

From source file:org.apache.pdfbox.pdmodel.graphics.color.PDICCBased.java

/**
 * Create a Java color model for this colorspace.
 *
 * @param bpc The number of bits per component.
 *
 * @return A color model that can be used for Java AWT operations.
 *
 * @throws IOException If there is an error creating the color model.
 *///from w  w w.  j a  v  a 2 s.c o  m
public ColorModel createColorModel(int bpc) throws IOException {

    int[] nbBits;
    int numOfComponents = getNumberOfComponents();
    switch (numOfComponents) {
    case 1:
        // DeviceGray
        nbBits = new int[] { bpc };
        break;
    case 3:
        // DeviceRGB
        nbBits = new int[] { bpc, bpc, bpc };
        break;
    case 4:
        // DeviceCMYK
        nbBits = new int[] { bpc, bpc, bpc, bpc };
        break;
    default:
        throw new IOException("Unknown colorspace number of components:" + numOfComponents);
    }
    ComponentColorModel componentColorModel = new ComponentColorModel(getJavaColorSpace(), nbBits, false, false,
            Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    return componentColorModel;

}

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.//w  ww . 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:org.mrgeo.data.raster.RasterWritable.java

private static byte[] rasterToBytes(final Raster raster) {
    final int datatype = raster.getTransferType();

    byte[] pixels;

    final Object elements = raster.getDataElements(raster.getMinX(), raster.getMinY(), raster.getWidth(),
            raster.getHeight(), null);/*from   www .  j  a  va  2 s  . c  o  m*/

    switch (datatype) {
    case DataBuffer.TYPE_BYTE: {
        pixels = (byte[]) elements;
        break;
    }
    case DataBuffer.TYPE_FLOAT: {
        final float[] floatElements = (float[]) elements;

        pixels = new byte[floatElements.length * RasterUtils.FLOAT_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final FloatBuffer floatbuff = bytebuff.asFloatBuffer();
        floatbuff.put(floatElements);

        break;
    }
    case DataBuffer.TYPE_DOUBLE: {
        final double[] doubleElements = (double[]) elements;

        pixels = new byte[doubleElements.length * RasterUtils.DOUBLE_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final DoubleBuffer doubleBuff = bytebuff.asDoubleBuffer();
        doubleBuff.put(doubleElements);

        break;
    }
    case DataBuffer.TYPE_INT: {
        final int[] intElements = (int[]) elements;

        pixels = new byte[intElements.length * RasterUtils.INT_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final IntBuffer intBuff = bytebuff.asIntBuffer();
        intBuff.put(intElements);

        break;
    }
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT: {
        final short[] shortElements = (short[]) elements;

        pixels = new byte[shortElements.length * RasterUtils.SHORT_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final ShortBuffer shortbuff = bytebuff.asShortBuffer();
        shortbuff.put(shortElements);

        break;
    }
    default:
        throw new RasterWritableException("Error trying to append raster.  Bad raster data type");
    }

    return pixels;
}

From source file:org.mrgeo.image.MrsImagePyramidMetadata.java

public static String toTileTypeText(final int tiletype) {
    switch (tiletype) {
    case DataBuffer.TYPE_BYTE: {
        return "Byte";
    }/*from www  .j  a  v a 2s. co m*/
    case DataBuffer.TYPE_FLOAT: {
        return "Float";
    }
    case DataBuffer.TYPE_DOUBLE: {
        return "Double";
    }
    case DataBuffer.TYPE_INT: {
        return "Int";
    }
    case DataBuffer.TYPE_SHORT: {
        return "Short";
    }
    case DataBuffer.TYPE_USHORT: {
        return "UShort";
    }
    }

    return "Undefined";
}

From source file:GraphicsUtil.java

/**
 * Creates a new raster that has a <b>copy</b> of the data in
 * <tt>ras</tt>.  This is highly optimized for speed.  There is
 * no provision for changing any aspect of the SampleModel.
 * However you can specify a new location for the returned raster.
 *
 * This method should be used when you need to change the contents
 * of a Raster that you do not "own" (ie the result of a
 * <tt>getData</tt> call)./*from w  w w .  j  av a 2  s.  com*/
 *
 * @param ras The Raster to copy.
 *
 * @param minX The x location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @param minY The y location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @return    A writable copy of <tt>ras</tt>
 */
public static WritableRaster copyRaster(Raster ras, int minX, int minY) {
    WritableRaster ret = Raster.createWritableRaster(ras.getSampleModel(), new Point(0, 0));
    ret = ret.createWritableChild(ras.getMinX() - ras.getSampleModelTranslateX(),
            ras.getMinY() - ras.getSampleModelTranslateY(), ras.getWidth(), ras.getHeight(), minX, minY, null);

    // Use System.arraycopy to copy the data between the two...
    DataBuffer srcDB = ras.getDataBuffer();
    DataBuffer retDB = ret.getDataBuffer();
    if (srcDB.getDataType() != retDB.getDataType()) {
        throw new IllegalArgumentException("New DataBuffer doesn't match original");
    }
    int len = srcDB.getSize();
    int banks = srcDB.getNumBanks();
    int[] offsets = srcDB.getOffsets();
    for (int b = 0; b < banks; b++) {
        switch (srcDB.getDataType()) {
        case DataBuffer.TYPE_BYTE: {
            DataBufferByte srcDBT = (DataBufferByte) srcDB;
            DataBufferByte retDBT = (DataBufferByte) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        case DataBuffer.TYPE_INT: {
            DataBufferInt srcDBT = (DataBufferInt) srcDB;
            DataBufferInt retDBT = (DataBufferInt) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        case DataBuffer.TYPE_SHORT: {
            DataBufferShort srcDBT = (DataBufferShort) srcDB;
            DataBufferShort retDBT = (DataBufferShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        case DataBuffer.TYPE_USHORT: {
            DataBufferUShort srcDBT = (DataBufferUShort) srcDB;
            DataBufferUShort retDBT = (DataBufferUShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        }
    }

    return ret;
}