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:ComponentTest.java

public static void main(String[] args) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new ComponentColorModel(cs, new int[] { 5, 6, 5 }, false, false, Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);

    Color fifty = new Color(cs, new float[] { 1.0f, 1.0f, 1.0f }, 0);
    float[] components = fifty.getComponents(null);
    System.out.print("Original normalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(components[i] + " ");
    System.out.println();/*from  ww  w.  ja  v a  2  s . co  m*/
    int[] unnormalized = cm.getUnnormalizedComponents(components, 0, null, 0);
    System.out.print("Original unnormalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(unnormalized[i] + " ");
    System.out.println();
    Object pixel = cm.getDataElements(unnormalized, 0, (Object) null);
    System.out.print("Pixel samples: ");
    byte[] pixelBytes = (byte[]) pixel;
    for (int i = 0; i < 3; i++)
        System.out.print(pixelBytes[i] + " ");
    System.out.println();

    unnormalized = cm.getComponents(pixel, null, 0);
    System.out.print("Derived unnormalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(unnormalized[i] + " ");
    System.out.println();
    components = cm.getNormalizedComponents(unnormalized, 0, null, 0);
    System.out.print("Derived normalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(components[i] + " ");
    System.out.println();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new ComponentColorModel(cs, new int[] { 5, 6, 5 }, false, false, Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);

    Color fifty = new Color(cs, new float[] { 1.0f, 1.0f, 1.0f }, 0);
    float[] components = fifty.getComponents(null);
    System.out.print("Original normalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(components[i] + " ");
    System.out.println();/*from  w w  w .  j a v  a2s  .  c  o m*/
    int[] unnormalized = cm.getUnnormalizedComponents(components, 0, null, 0);
    System.out.print("Original unnormalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(unnormalized[i] + " ");
    System.out.println();

    Object pixel = cm.getDataElements(unnormalized, 0, (Object) null);
    System.out.print("Pixel samples: ");
    byte[] pixelBytes = (byte[]) pixel;
    for (int i = 0; i < 3; i++)
        System.out.print(pixelBytes[i] + " ");
    System.out.println();

    unnormalized = cm.getComponents(pixel, null, 0);
    System.out.print("Derived unnormalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(unnormalized[i] + " ");
    System.out.println();
    components = cm.getNormalizedComponents(unnormalized, 0, null, 0);
    System.out.print("Derived normalized components: ");
    for (int i = 0; i < 3; i++)
        System.out.print(components[i] + " ");
}

From source file:Main.java

private static ByteBuffer convertImageData(BufferedImage bufferedImage) {
    ByteBuffer imageBuffer;/*  www  .  j  av  a 2  s  .  c o m*/
    WritableRaster raster;
    BufferedImage texImage;

    // for a texture
    if (bufferedImage.getColorModel().hasAlpha()) {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 4, null);
        texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>());
    } else {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 3, null);
        texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>());
    }

    // copy the source image into the produced image
    Graphics g = texImage.getGraphics();
    g.setColor(new Color(0f, 0f, 0f, 0f));
    g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
    g.drawImage(bufferedImage, 0, 0, null);

    // build a byte buffer from the temporary image
    // that be used by OpenGL to produce a texture.
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

    imageBuffer = ByteBuffer.allocateDirect(data.length);
    imageBuffer.order(ByteOrder.nativeOrder());
    imageBuffer.put(data, 0, data.length);
    imageBuffer.flip();

    return imageBuffer;
}

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

/**
 * Decode JPEG2000 data using Java ImageIO library.
 *
 * {@inheritDoc}/*  w  ww  .j  a  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());
        }
    }
}

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

/**
 * Decode JBIG2 data using Java ImageIO library.
 *
 * {@inheritDoc}/* w ww.jav  a2 s . c  om*/
 *
 */
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:ConvertUtil.java

/**
 * Converts the source image to 4-bit colour using the given colour map. No
 * transparency./*  w  ww. jav a 2 s  .co  m*/
 * 
 * @param src
 *            the source image to convert
 * @param cmap
 *            the colour map, which should contain no more than 16 entries
 *            The entries are in the form RRGGBB (hex).
 * @return a copy of the source image with a 4-bit colour depth, with the
 *         custom colour pallette
 */
public static BufferedImage convert4(BufferedImage src, int[] cmap) {
    IndexColorModel icm = new IndexColorModel(4, cmap.length, cmap, 0, false, Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_BINARY,
            icm);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);

    return dest;
}

From source file:org.mrgeo.cmd.mrsimageinfo.MrsImageInfo.java

private static void printNodata(final MrsPyramidMetadata metadata) {
    System.out.print("NoData: ");
    for (int band = 0; band < metadata.getBands(); band++) {
        if (band > 0) {
            System.out.print(", ");
        }/*w  w w.  j av  a  2  s  .c om*/
        switch (metadata.getTileType()) {
        case DataBuffer.TYPE_BYTE:
            System.out.print(metadata.getDefaultValueByte(band));
            break;
        case DataBuffer.TYPE_FLOAT:
            System.out.print(metadata.getDefaultValueFloat(band));
            break;
        case DataBuffer.TYPE_DOUBLE:
            System.out.print(metadata.getDefaultValueDouble(band));
            break;
        case DataBuffer.TYPE_INT:
            System.out.print(metadata.getDefaultValueInt(band));
            break;
        case DataBuffer.TYPE_SHORT:
        case DataBuffer.TYPE_USHORT:
            System.out.print(metadata.getDefaultValueShort(band));
            break;
        default:
            break;
        }
    }
    System.out.println("");
}

From source file:image.text.CreateTifAndAnnotate.java

public void createAnnotatedTif(String[] args, OutputStream out) throws Exception {

    byte[] byteArray = new byte[] { -1, 0 };
    ColorModel colorModel = new IndexColorModel(1, 2, byteArray, byteArray, byteArray);

    WritableRaster writeableRaster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1700, 2200, 1, 1, null);
    BufferedImage bufImg = new BufferedImage(colorModel, writeableRaster, false, null);

    // -------------------------------------------------------------------        
    Graphics2D g2d = bufImg.createGraphics();
    g2d.setColor(Color.black);/*from  ww  w  .j  av a2 s .  co  m*/

    Font font = new Font("Arial Bold", Font.PLAIN, 36);
    g2d.setFont(font);
    int vertPos = 200;
    for (int i = 0; i < args.length; i++) {
        g2d.drawString(args[i], 75, vertPos);
        vertPos += 48;
    }

    PlanarImage planarImage = PlanarImage.wrapRenderedImage(bufImg);

    TIFFEncodeParam encodeParam = new TIFFEncodeParam();
    encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
    encodeParam.setWriteTiled(false); // false means use strips.
    encodeParam.setTileSize(0, 0); // tiling will make the file size much larger. 
    encodeParam.setLittleEndian(true); // create an Intel (II) format image

    String SoftwareVersion[] = new String[] { "TIFF by Joe, " + this.getClass().getName() };
    String docName[] = new String[] { "JoesTifAnnotator" };

    // Create a new TIFF fields including a new TIFF ASCII TIFF tag.
    TIFFField tiffFields[] = new TIFFField[5];

    tiffFields[0] = new TIFFField(269, TIFFField.TIFF_ASCII, docName.length, docName);
    tiffFields[1] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } });
    tiffFields[2] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, new long[][] { { 200, 1 } });
    // resolution unit 
    tiffFields[3] = new TIFFField(296, TIFFField.TIFF_SHORT, 1, new char[] { 2 });
    tiffFields[4] = new TIFFField(305, TIFFField.TIFF_ASCII, SoftwareVersion.length, SoftwareVersion);

    encodeParam.setExtraFields(tiffFields);

    TIFFImageEncoder encoder = new TIFFImageEncoder(out, encodeParam);
    encoder.encode(planarImage);
}

From source file:org.apache.pdfbox.pdmodel.graphics.shading.ShadingContext.java

/**
 * Constructor./*from  ww  w  . j  a  v  a2  s .  c  om*/
 *
 * @param shading the shading type to be used
 * @param cm the color model to be used
 * @param xform transformation for user to device space
 * @param matrix the pattern matrix concatenated with that of the parent content stream
 * @throws java.io.IOException if there is an error getting the color space
 * or doing background color conversion.
 */
public ShadingContext(PDShading shading, ColorModel cm, AffineTransform xform, Matrix matrix)
        throws IOException {
    this.shading = shading;
    shadingColorSpace = shading.getColorSpace();

    // create the output color model using RGB+alpha as color space
    ColorSpace outputCS = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    outputColorModel = new ComponentColorModel(outputCS, true, false, Transparency.TRANSLUCENT,
            DataBuffer.TYPE_BYTE);

    bboxRect = shading.getBBox();
    if (bboxRect != null) {
        transformBBox(matrix, xform);
    }

    // get background values if available
    COSArray bg = shading.getBackground();
    if (bg != null) {
        background = bg.toFloatArray();
        rgbBackground = convertToRGB(background);
    }
}

From source file:com.embedler.moon.jtxt2img.mmap.MappedFileBuffer.java

public static DataBuffer create(final int type, final int size, final int numBanks) {
    switch (type) {
    case DataBuffer.TYPE_BYTE:
        return new DataBufferByte(size, numBanks);
    case DataBuffer.TYPE_USHORT:
        return new DataBufferUShort(size, numBanks);
    case DataBuffer.TYPE_INT:
        return new DataBufferInt(size, numBanks);
    default://from  w  w w.  j av a 2 s . c  o m
        throw new JTxt2ImgIoRuntimeException("Unsupported data type: " + type);
    }
}