Java Code Examples for java.awt.image.DataBuffer#getDataType()

The following examples show how to use java.awt.image.DataBuffer#getDataType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PartialImageEdit.java    From Pixelitor with GNU General Public License v3.0 6 votes vote down vote up
private static void debugRaster(String name, Raster raster) {
    if (raster == null) {
        System.err.printf("PartialImageEdit::debugRaster: NULL RASTER, name = '%s'%n", name);
        return;
    }
    Rectangle rasterBounds = raster.getBounds();
    String className = raster.getClass().getSimpleName();
    DataBuffer dataBuffer = raster.getDataBuffer();
    int dataType = dataBuffer.getDataType();
    String typeAsString = Debug.dateBufferTypeAsString(dataType);
    int numBanks = dataBuffer.getNumBanks();
    int numBands = raster.getNumBands();
    int numDataElements = raster.getNumDataElements();

    String msg = format("className = %s, rasterBounds = %s, dataType = %d, " +
            "typeAsString=%s, numBanks = %d, numBands = %d, numDataElements = %d",
        className, rasterBounds, dataType,
        typeAsString, numBanks, numBands, numDataElements);

    System.out.println("PartialImageEdit::debugRaster debugging raster: " + name + ": " + msg);
}
 
Example 2
Source File: RgbeImageParser.java    From commons-imaging with Apache License 2.0 6 votes vote down vote up
@Override
public BufferedImage getBufferedImage(final ByteSource byteSource, final Map<String, Object> params)
        throws ImageReadException, IOException {
    try (RgbeInfo info = new RgbeInfo(byteSource)) {
        // It is necessary to create our own BufferedImage here as the
        // org.apache.commons.imaging.common.IBufferedImageFactory interface does
        // not expose this complexity
        final DataBuffer buffer = new DataBufferFloat(info.getPixelData(),
                info.getWidth() * info.getHeight());

        final BufferedImage ret = new BufferedImage(new ComponentColorModel(
                ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
                Transparency.OPAQUE, buffer.getDataType()),
                Raster.createWritableRaster(
                        new BandedSampleModel(buffer.getDataType(),
                                info.getWidth(), info.getHeight(), 3),
                        buffer,
                        new Point()), false, null);
        return ret;
    }
}
 
Example 3
Source File: RawRect.java    From cosmic with Apache License 2.0 5 votes vote down vote up
@Override
public void paint(final BufferedImage image, final Graphics2D graphics) {

    final DataBuffer dataBuf = image.getRaster().getDataBuffer();

    switch (dataBuf.getDataType()) {

        case DataBuffer.TYPE_INT: {
            // We chose RGB888 model, so Raster will use DataBufferInt type
            final DataBufferInt dataBuffer = (DataBufferInt) dataBuf;

            final int imageWidth = image.getWidth();
            final int imageHeight = image.getHeight();

            // Paint rectangle directly on buffer, line by line
            final int[] imageBuffer = dataBuffer.getData();
            for (int srcLine = 0, dstLine = y; srcLine < height && dstLine < imageHeight; srcLine++, dstLine++) {
                try {
                    System.arraycopy(buf, srcLine * width, imageBuffer, x + dstLine * imageWidth, width);
                } catch (final IndexOutOfBoundsException e) {
                    s_logger.info("[ignored] buffer overflow!?!", e);
                }
            }
            break;
        }

        default:
            throw new RuntimeException("Unsupported data buffer in buffered image: expected data buffer of type int (DataBufferInt). Actual data buffer type: " +
                    dataBuf.getClass().getSimpleName());
    }
}
 
Example 4
Source File: RawRect.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public void paint(BufferedImage image, Graphics2D graphics) {

    DataBuffer dataBuf = image.getRaster().getDataBuffer();

    switch (dataBuf.getDataType()) {

    case DataBuffer.TYPE_INT: {
        // We chose RGB888 model, so Raster will use DataBufferInt type
        DataBufferInt dataBuffer = (DataBufferInt)dataBuf;

        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();

        // Paint rectangle directly on buffer, line by line
        int[] imageBuffer = dataBuffer.getData();
        for (int srcLine = 0, dstLine = y; srcLine < height && dstLine < imageHeight; srcLine++, dstLine++) {
            try {
                System.arraycopy(buf, srcLine * width, imageBuffer, x + dstLine * imageWidth, width);
            } catch (IndexOutOfBoundsException e) {
                s_logger.info("[ignored] buffer overflow!?!", e);
            }
        }
        break;
    }

    default:
        throw new RuntimeException("Unsupported data buffer in buffered image: expected data buffer of type int (DataBufferInt). Actual data buffer type: " +
                dataBuf.getClass().getSimpleName());
    }
}
 
Example 5
Source File: AWTLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Object extractImageData(BufferedImage img){
    DataBuffer buf = img.getRaster().getDataBuffer();
    switch (buf.getDataType()){
        case DataBuffer.TYPE_BYTE:
            DataBufferByte byteBuf = (DataBufferByte) buf;
            return byteBuf.getData();
        case DataBuffer.TYPE_USHORT:
            DataBufferUShort shortBuf = (DataBufferUShort) buf;
            return shortBuf.getData();
    }
    return null;
}
 
Example 6
Source File: JBIG2Filter.java    From sambox with Apache License 2.0 4 votes vote down vote up
@Override
public DecodeResult decode(InputStream encoded, OutputStream decoded, COSDictionary
        parameters, int index) throws IOException
{
    ImageReader reader = findImageReader("JBIG2", "jbig2-imageio is not installed");
    if (reader.getClass().getName().contains("levigo"))
    {
        logLevigoDonated();
    }

    DecodeResult result = new DecodeResult(new COSDictionary());
    result.getParameters().addAll(parameters);

    int bits = parameters.getInt(COSName.BITS_PER_COMPONENT, 1);
    COSDictionary params = getDecodeParams(parameters, index);

    COSStream globals = null;
    if (params != null)
    {
        globals = (COSStream) params.getDictionaryObject(COSName.JBIG2_GLOBALS);
    }

    ImageInputStream iis = null;
    try
    {
        if (globals != null)
        {
            iis = ImageIO.createImageInputStream(
                    new SequenceInputStream(globals.getUnfilteredStream(), encoded));
            reader.setInput(iis);
        }
        else
        {
            iis = ImageIO.createImageInputStream(encoded);
            reader.setInput(iis);
        }

        BufferedImage image;
        try
        {
            image = reader.read(0, reader.getDefaultReadParam());
        }
        catch (Exception e)
        {
            // wrap and rethrow any exceptions
            throw new IOException("Could not read JBIG2 image", e);
        }

        // I am assuming since JBIG2 is always black and white
        // depending on your renderer this might or might be needed
        if (image.getColorModel().getPixelSize() != bits)
        {
            if (bits != 1)
            {
                LOG.warn("Attempting to handle a JBIG2 with more than 1-bit depth");
            }
            BufferedImage packedImage = new BufferedImage(image.getWidth(), image.getHeight(),
                    BufferedImage.TYPE_BYTE_BINARY);
            Graphics graphics = packedImage.getGraphics();
            graphics.drawImage(image, 0, 0, null);
            graphics.dispose();
            image = packedImage;
        }

        DataBuffer dBuf = image.getData().getDataBuffer();
        if (dBuf.getDataType() == DataBuffer.TYPE_BYTE)
        {
            decoded.write(((DataBufferByte) dBuf).getData());
        }
        else
        {
            throw new IOException("Unexpected image buffer type");
        }
    }
    finally
    {
        if (iis != null)
        {
            iis.close();
        }
        reader.dispose();
    }

    return new DecodeResult(parameters);
}
 
Example 7
Source File: DataBufferPersistenceUtils.java    From geowave with Apache License 2.0 4 votes vote down vote up
public static byte[] getDataBufferBinary(final DataBuffer dataBuffer) {
  final DataBufferProtos.DataBuffer.Builder bldr = DataBufferProtos.DataBuffer.newBuilder();
  bldr.setType(dataBuffer.getDataType());
  bldr.addAllOffsets(Ints.asList(dataBuffer.getOffsets()));
  bldr.setSize(dataBuffer.getSize());
  switch (dataBuffer.getDataType()) {
    case DataBuffer.TYPE_BYTE:
      final ByteDataBuffer.Builder byteBldr = ByteDataBuffer.newBuilder();
      final byte[][] byteBank = ((DataBufferByte) dataBuffer).getBankData();
      final Iterable<ByteString> byteIt = () -> new Iterator<ByteString>() {
        private int index = 0;

        @Override
        public boolean hasNext() {
          return byteBank.length > index;
        }

        @Override
        public ByteString next() {
          if (!hasNext()) {
            throw new NoSuchElementException();
          }
          return ByteString.copyFrom(byteBank[index++]);
        }
      };
      byteBldr.addAllBanks(byteIt);
      bldr.setByteDb(byteBldr.build());
      break;
    case DataBuffer.TYPE_SHORT:
      setBuilder(shortToInt(((DataBufferShort) dataBuffer).getBankData()), bldr);
      break;
    case DataBuffer.TYPE_USHORT:
      setBuilder(shortToInt(((DataBufferUShort) dataBuffer).getBankData()), bldr);
      break;
    case DataBuffer.TYPE_INT:
      setBuilder(((DataBufferInt) dataBuffer).getBankData(), bldr);
      break;
    case DataBuffer.TYPE_FLOAT:
      final FloatDataBuffer.Builder fltBldr = FloatDataBuffer.newBuilder();
      final float[][] fltBank = ((DataBufferFloat) dataBuffer).getBankData();
      final Iterable<FloatArray> floatIt = () -> new Iterator<FloatArray>() {
        private int index = 0;

        @Override
        public boolean hasNext() {
          return fltBank.length > index;
        }

        @Override
        public FloatArray next() {
          return FloatArray.newBuilder().addAllSamples(Floats.asList(fltBank[index++])).build();
        }
      };
      fltBldr.addAllBanks(floatIt);
      bldr.setFlt(fltBldr);
      break;
    case DataBuffer.TYPE_DOUBLE:
      final DoubleDataBuffer.Builder dblBldr = DoubleDataBuffer.newBuilder();
      final double[][] dblBank = ((DataBufferDouble) dataBuffer).getBankData();
      final Iterable<DoubleArray> dblIt = () -> new Iterator<DoubleArray>() {
        private int index = 0;

        @Override
        public boolean hasNext() {
          return dblBank.length > index;
        }

        @Override
        public DoubleArray next() {
          return DoubleArray.newBuilder().addAllSamples(Doubles.asList(dblBank[index++])).build();
        }
      };
      dblBldr.addAllBanks(dblIt);
      bldr.setDbl(dblBldr);
      break;
    default:
      throw new RuntimeException(
          "Unsupported DataBuffer type for serialization " + dataBuffer.getDataType());
  }
  return bldr.build().toByteArray();
}
 
Example 8
Source File: BufferedImagePixelsAdapter.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public void handleData(ByteBuffer buf, Link link) {
    if (verbose)
        s_logger.debug("[" + this + "] INFO: Data received: " + buf + ".");

    int x = (Integer)buf.getMetadata(TARGET_X);
    int y = (Integer)buf.getMetadata(TARGET_Y);
    int rectWidth = (Integer)buf.getMetadata(WIDTH);
    int rectHeight = (Integer)buf.getMetadata(HEIGHT);
    String format = (String)buf.getMetadata(PIXEL_FORMAT);

    int bpp;
    // Support RGB888/32 little endian only
    if (format != null && RGB888LE32.equals(format)) {
        bpp = 4;
        // TODO: support more formats
    } else
        throw new RuntimeException("Unsupported format: " + format + ". Supported formats: " + RGB888LE32 + ".");

    int dataLength = rectWidth * rectHeight * bpp;
    if (!cap(buf, dataLength, dataLength, link, false))
        return;

    // Draw rectangle on offline buffer
    BufferedImage image = canvas.getOfflineImage();

    DataBuffer dataBuf = image.getRaster().getDataBuffer();

    switch (dataBuf.getDataType()) {

    case DataBuffer.TYPE_INT: {

        // Convert array of bytes to array of int's
        int[] intArray = buf.toIntLEArray();

        // We chose RGB888 model, so Raster will use DataBufferInt type
        DataBufferInt dataBuffer = (DataBufferInt)dataBuf;

        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();

        // Paint rectangle directly on buffer, line by line
        int[] imageBuffer = dataBuffer.getData();

        for (int srcLine = 0, dstLine = y; srcLine < rectHeight && dstLine < imageHeight; srcLine++, dstLine++) {
            try {
                System.arraycopy(intArray, srcLine * rectWidth, imageBuffer, x + dstLine * imageWidth, rectWidth);
            } catch (IndexOutOfBoundsException e) {
                s_logger.info("[ignored] copy error",e);
            }
        }
        break;
    }

    default:
        throw new RuntimeException("Unsupported data buffer in buffered image: expected data buffer of type int (DataBufferInt). Actual data buffer type: "
                + dataBuf.getClass().getSimpleName());
    }

    // Request update of repainted area
    canvas.repaint(x, y, rectWidth, rectHeight);

    buf.unref();
}
 
Example 9
Source File: BufferedImageTools.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Gets the pixel type of the given image.
 *
 * @return One of the following types:
 *         <ul>
 *         <li>FormatReader.INT8</li>
 *         <li>FormatReader.UINT8</li>
 *         <li>FormatReader.INT16</li>
 *         <li>FormatReader.UINT16</li>
 *         <li>FormatReader.INT32</li>
 *         <li>FormatReader.UINT32</li>
 *         <li>FormatReader.FLOAT</li>
 *         <li>FormatReader.DOUBLE</li>
 *         <li>-1 (unknown type)</li>
 *         </ul>
 */
public static int getPixelType(final BufferedImage image) {
	final Raster raster = image.getRaster();
	if (raster == null) return -1;
	final DataBuffer buffer = raster.getDataBuffer();
	if (buffer == null) return -1;

	if (buffer instanceof SignedByteBuffer) {
		return FormatTools.INT8;
	}
	else if (buffer instanceof SignedShortBuffer) {
		return FormatTools.INT16;
	}
	else if (buffer instanceof UnsignedIntBuffer) {
		return FormatTools.UINT32;
	}

	final int type = buffer.getDataType();
	final int imageType = image.getType();
	switch (type) {
		case DataBuffer.TYPE_BYTE:
			return FormatTools.UINT8;
		case DataBuffer.TYPE_DOUBLE:
			return FormatTools.DOUBLE;
		case DataBuffer.TYPE_FLOAT:
			return FormatTools.FLOAT;
		case DataBuffer.TYPE_INT:
			if (imageType == BufferedImage.TYPE_INT_RGB ||
				imageType == BufferedImage.TYPE_INT_BGR ||
				imageType == BufferedImage.TYPE_INT_ARGB)
			{
				return FormatTools.UINT8;
			}
			if (buffer instanceof UnsignedIntBuffer) {
				return FormatTools.UINT32;
			}
			return FormatTools.INT32;
		case DataBuffer.TYPE_SHORT:
			return FormatTools.INT16;
		case DataBuffer.TYPE_USHORT:
			if (imageType == BufferedImage.TYPE_USHORT_555_RGB ||
				imageType == BufferedImage.TYPE_USHORT_565_RGB)
			{
				return FormatTools.UINT8;
			}
			return FormatTools.UINT16;
		default:
			return -1;
	}
}
 
Example 10
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Gets the pixel type of the given image.
 *
 * @return One of the following types:
 *         <ul>
 *         <li>FormatReader.INT8</li>
 *         <li>FormatReader.UINT8</li>
 *         <li>FormatReader.INT16</li>
 *         <li>FormatReader.UINT16</li>
 *         <li>FormatReader.INT32</li>
 *         <li>FormatReader.UINT32</li>
 *         <li>FormatReader.FLOAT</li>
 *         <li>FormatReader.DOUBLE</li>
 *         <li>-1 (unknown type)</li>
 *         </ul>
 */
public static int getPixelType(final BufferedImage image) {
	final Raster raster = image.getRaster();
	if (raster == null) return -1;
	final DataBuffer buffer = raster.getDataBuffer();
	if (buffer == null) return -1;

	if (buffer instanceof SignedByteBuffer) {
		return FormatTools.INT8;
	}
	else if (buffer instanceof SignedShortBuffer) {
		return FormatTools.INT16;
	}
	else if (buffer instanceof UnsignedIntBuffer) {
		return FormatTools.UINT32;
	}

	final int type = buffer.getDataType();
	final int imageType = image.getType();
	switch (type) {
		case DataBuffer.TYPE_BYTE:
			return FormatTools.UINT8;
		case DataBuffer.TYPE_DOUBLE:
			return FormatTools.DOUBLE;
		case DataBuffer.TYPE_FLOAT:
			return FormatTools.FLOAT;
		case DataBuffer.TYPE_INT:
			if (imageType == BufferedImage.TYPE_INT_RGB ||
				imageType == BufferedImage.TYPE_INT_BGR ||
				imageType == BufferedImage.TYPE_INT_ARGB)
			{
				return FormatTools.UINT8;
			}
			if (buffer instanceof UnsignedIntBuffer) {
				return FormatTools.UINT32;
			}
			return FormatTools.INT32;
		case DataBuffer.TYPE_SHORT:
			return FormatTools.INT16;
		case DataBuffer.TYPE_USHORT:
			if (imageType == BufferedImage.TYPE_USHORT_555_RGB ||
				imageType == BufferedImage.TYPE_USHORT_565_RGB)
			{
				return FormatTools.UINT8;
			}
			return FormatTools.UINT16;
		default:
			return -1;
	}
}