use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class ImageUtil method imageIsContiguous.
/**
* Returns whether the image has contiguous data across rows.
*/
public static final boolean imageIsContiguous(RenderedImage image) {
SampleModel sm;
if (image instanceof BufferedImage) {
WritableRaster ras = ((BufferedImage) image).getRaster();
sm = ras.getSampleModel();
} else {
sm = image.getSampleModel();
}
if (sm instanceof ComponentSampleModel) {
// Ensure image rows samples are stored contiguously
// in a single bank.
ComponentSampleModel csm = (ComponentSampleModel) sm;
if (csm.getPixelStride() != csm.getNumBands()) {
return false;
}
int[] bandOffsets = csm.getBandOffsets();
for (int i = 0; i < bandOffsets.length; i++) {
if (bandOffsets[i] != i) {
return false;
}
}
int[] bankIndices = csm.getBankIndices();
for (int i = 0; i < bandOffsets.length; i++) {
if (bankIndices[i] != 0) {
return false;
}
}
return true;
}
// pixel stride.
return ImageUtil.isBinary(sm);
}
use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class WBMPImageReader method read.
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
if (iis == null) {
throw new IllegalStateException(I18N.getString("WBMPImageReader1"));
}
checkIndex(imageIndex);
clearAbortRequest();
processImageStarted(imageIndex);
if (param == null)
param = getDefaultReadParam();
//read header
readHeader();
Rectangle sourceRegion = new Rectangle(0, 0, 0, 0);
Rectangle destinationRegion = new Rectangle(0, 0, 0, 0);
computeRegions(param, this.width, this.height, param.getDestination(), sourceRegion, destinationRegion);
int scaleX = param.getSourceXSubsampling();
int scaleY = param.getSourceYSubsampling();
int xOffset = param.getSubsamplingXOffset();
int yOffset = param.getSubsamplingYOffset();
// If the destination is provided, then use it. Otherwise, create new one
BufferedImage bi = param.getDestination();
if (bi == null)
bi = new BufferedImage(destinationRegion.x + destinationRegion.width, destinationRegion.y + destinationRegion.height, BufferedImage.TYPE_BYTE_BINARY);
boolean noTransform = destinationRegion.equals(new Rectangle(0, 0, width, height)) && destinationRegion.equals(new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
// Get the image data.
WritableRaster tile = bi.getWritableTile(0, 0);
// Get the SampleModel.
MultiPixelPackedSampleModel sm = (MultiPixelPackedSampleModel) bi.getSampleModel();
if (noTransform) {
if (abortRequested()) {
processReadAborted();
return bi;
}
// If noTransform is necessary, read the data.
iis.read(((DataBufferByte) tile.getDataBuffer()).getData(), 0, height * sm.getScanlineStride());
processImageUpdate(bi, 0, 0, width, height, 1, 1, new int[] { 0 });
processImageProgress(100.0F);
} else {
int len = (this.width + 7) / 8;
byte[] buf = new byte[len];
byte[] data = ((DataBufferByte) tile.getDataBuffer()).getData();
int lineStride = sm.getScanlineStride();
iis.skipBytes(len * sourceRegion.y);
int skipLength = len * (scaleY - 1);
// cache the values to avoid duplicated computation
int[] srcOff = new int[destinationRegion.width];
int[] destOff = new int[destinationRegion.width];
int[] srcPos = new int[destinationRegion.width];
int[] destPos = new int[destinationRegion.width];
for (int i = destinationRegion.x, x = sourceRegion.x, j = 0; i < destinationRegion.x + destinationRegion.width; i++, j++, x += scaleX) {
srcPos[j] = x >> 3;
srcOff[j] = 7 - (x & 7);
destPos[j] = i >> 3;
destOff[j] = 7 - (i & 7);
}
for (int j = 0, y = sourceRegion.y, k = destinationRegion.y * lineStride; j < destinationRegion.height; j++, y += scaleY) {
if (abortRequested())
break;
iis.read(buf, 0, len);
for (int i = 0; i < destinationRegion.width; i++) {
//get the bit and assign to the data buffer of the raster
int v = (buf[srcPos[i]] >> srcOff[i]) & 1;
data[k + destPos[i]] |= v << destOff[i];
}
k += lineStride;
iis.skipBytes(skipLength);
processImageUpdate(bi, 0, j, destinationRegion.width, 1, 1, 1, new int[] { 0 });
processImageProgress(100.0F * j / destinationRegion.height);
}
}
if (abortRequested())
processReadAborted();
else
processImageComplete();
return bi;
}
use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class WBMPImageWriter method write.
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
if (stream == null) {
throw new IllegalStateException(I18N.getString("WBMPImageWriter3"));
}
if (image == null) {
throw new IllegalArgumentException(I18N.getString("WBMPImageWriter4"));
}
clearAbortRequest();
processImageStarted(0);
if (param == null)
param = getDefaultWriteParam();
RenderedImage input = null;
Raster inputRaster = null;
boolean writeRaster = image.hasRaster();
Rectangle sourceRegion = param.getSourceRegion();
SampleModel sampleModel = null;
if (writeRaster) {
inputRaster = image.getRaster();
sampleModel = inputRaster.getSampleModel();
} else {
input = image.getRenderedImage();
sampleModel = input.getSampleModel();
inputRaster = input.getData();
}
checkSampleModel(sampleModel);
if (sourceRegion == null)
sourceRegion = inputRaster.getBounds();
else
sourceRegion = sourceRegion.intersection(inputRaster.getBounds());
if (sourceRegion.isEmpty())
throw new RuntimeException(I18N.getString("WBMPImageWriter1"));
int scaleX = param.getSourceXSubsampling();
int scaleY = param.getSourceYSubsampling();
int xOffset = param.getSubsamplingXOffset();
int yOffset = param.getSubsamplingYOffset();
sourceRegion.translate(xOffset, yOffset);
sourceRegion.width -= xOffset;
sourceRegion.height -= yOffset;
int minX = sourceRegion.x / scaleX;
int minY = sourceRegion.y / scaleY;
int w = (sourceRegion.width + scaleX - 1) / scaleX;
int h = (sourceRegion.height + scaleY - 1) / scaleY;
Rectangle destinationRegion = new Rectangle(minX, minY, w, h);
sampleModel = sampleModel.createCompatibleSampleModel(w, h);
SampleModel destSM = sampleModel;
// If the data are not formatted nominally then reformat.
if (sampleModel.getDataType() != DataBuffer.TYPE_BYTE || !(sampleModel instanceof MultiPixelPackedSampleModel) || ((MultiPixelPackedSampleModel) sampleModel).getDataBitOffset() != 0) {
destSM = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, w, h, 1, w + 7 >> 3, 0);
}
if (!destinationRegion.equals(sourceRegion)) {
if (scaleX == 1 && scaleY == 1)
inputRaster = inputRaster.createChild(inputRaster.getMinX(), inputRaster.getMinY(), w, h, minX, minY, null);
else {
WritableRaster ras = Raster.createWritableRaster(destSM, new Point(minX, minY));
byte[] data = ((DataBufferByte) ras.getDataBuffer()).getData();
for (int j = minY, y = sourceRegion.y, k = 0; j < minY + h; j++, y += scaleY) {
for (int i = 0, x = sourceRegion.x; i < w; i++, x += scaleX) {
int v = inputRaster.getSample(x, y, 0);
data[k + (i >> 3)] |= v << (7 - (i & 7));
}
k += w + 7 >> 3;
}
inputRaster = ras;
}
}
// If the data are not formatted nominally then reformat.
if (!destSM.equals(inputRaster.getSampleModel())) {
WritableRaster raster = Raster.createWritableRaster(destSM, new Point(inputRaster.getMinX(), inputRaster.getMinY()));
raster.setRect(inputRaster);
inputRaster = raster;
}
// Check whether the image is white-is-zero.
boolean isWhiteZero = false;
if (!writeRaster && input.getColorModel() instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel) input.getColorModel();
isWhiteZero = icm.getRed(0) > icm.getRed(1);
}
// Get the line stride, bytes per row, and data array.
int lineStride = ((MultiPixelPackedSampleModel) destSM).getScanlineStride();
int bytesPerRow = (w + 7) / 8;
byte[] bdata = ((DataBufferByte) inputRaster.getDataBuffer()).getData();
// Write WBMP header.
// TypeField
stream.write(0);
// FixHeaderField
stream.write(0);
// width
stream.write(intToMultiByte(w));
// height
stream.write(intToMultiByte(h));
// Write the data.
if (!isWhiteZero && lineStride == bytesPerRow) {
// Write the entire image.
stream.write(bdata, 0, h * bytesPerRow);
processImageProgress(100.0F);
} else {
// Write the image row-by-row.
int offset = 0;
if (!isWhiteZero) {
// Black-is-zero
for (int row = 0; row < h; row++) {
if (abortRequested())
break;
stream.write(bdata, offset, bytesPerRow);
offset += lineStride;
processImageProgress(100.0F * row / h);
}
} else {
// White-is-zero: need to invert data.
byte[] inverted = new byte[bytesPerRow];
for (int row = 0; row < h; row++) {
if (abortRequested())
break;
for (int col = 0; col < bytesPerRow; col++) {
inverted[col] = (byte) (~(bdata[col + offset]));
}
stream.write(inverted, 0, bytesPerRow);
offset += lineStride;
processImageProgress(100.0F * row / h);
}
}
}
if (abortRequested())
processWriteAborted();
else {
processImageComplete();
stream.flushBefore(stream.getStreamPosition());
}
}
use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class OffScreenImageSource method sendPixels.
private void sendPixels() {
ColorModel cm = image.getColorModel();
WritableRaster raster = image.getRaster();
int numDataElements = raster.getNumDataElements();
int dataType = raster.getDataBuffer().getDataType();
int[] scanline = new int[width * numDataElements];
boolean needToCvt = true;
if (cm instanceof IndexColorModel) {
byte[] pixels = new byte[width];
theConsumer.setColorModel(cm);
if (raster instanceof ByteComponentRaster) {
needToCvt = false;
for (int y = 0; y < height; y++) {
raster.getDataElements(0, y, width, 1, pixels);
theConsumer.setPixels(0, y, width, 1, cm, pixels, 0, width);
}
} else if (raster instanceof BytePackedRaster) {
needToCvt = false;
// Binary image. Need to unpack it
for (int y = 0; y < height; y++) {
raster.getPixels(0, y, width, 1, scanline);
for (int x = 0; x < width; x++) {
pixels[x] = (byte) scanline[x];
}
theConsumer.setPixels(0, y, width, 1, cm, pixels, 0, width);
}
} else if (dataType == DataBuffer.TYPE_SHORT || dataType == DataBuffer.TYPE_INT) {
// Probably a short or int "GRAY" image
needToCvt = false;
for (int y = 0; y < height; y++) {
raster.getPixels(0, y, width, 1, scanline);
theConsumer.setPixels(0, y, width, 1, cm, scanline, 0, width);
}
}
} else if (cm instanceof DirectColorModel) {
theConsumer.setColorModel(cm);
needToCvt = false;
switch(dataType) {
case DataBuffer.TYPE_INT:
for (int y = 0; y < height; y++) {
raster.getDataElements(0, y, width, 1, scanline);
theConsumer.setPixels(0, y, width, 1, cm, scanline, 0, width);
}
break;
case DataBuffer.TYPE_BYTE:
byte[] bscanline = new byte[width];
for (int y = 0; y < height; y++) {
raster.getDataElements(0, y, width, 1, bscanline);
for (int x = 0; x < width; x++) {
scanline[x] = bscanline[x] & 0xff;
}
theConsumer.setPixels(0, y, width, 1, cm, scanline, 0, width);
}
break;
case DataBuffer.TYPE_USHORT:
short[] sscanline = new short[width];
for (int y = 0; y < height; y++) {
raster.getDataElements(0, y, width, 1, sscanline);
for (int x = 0; x < width; x++) {
scanline[x] = sscanline[x] & 0xffff;
}
theConsumer.setPixels(0, y, width, 1, cm, scanline, 0, width);
}
break;
default:
needToCvt = true;
}
}
if (needToCvt) {
// REMIND: Need to add other types of CMs here
ColorModel newcm = ColorModel.getRGBdefault();
theConsumer.setColorModel(newcm);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
scanline[x] = image.getRGB(x, y);
}
theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0, width);
}
}
}
use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class ImageRepresentation method getOpaqueRGBImage.
public BufferedImage getOpaqueRGBImage() {
if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) {
int w = bimage.getWidth();
int h = bimage.getHeight();
int size = w * h;
// Note that we steal the data array here, but only for reading...
DataBufferInt db = (DataBufferInt) biRaster.getDataBuffer();
int[] pixels = SunWritableRaster.stealData(db, 0);
for (int i = 0; i < size; i++) {
if ((pixels[i] >>> 24) != 0xff) {
return bimage;
}
}
ColorModel opModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
int[] bandmasks = { 0x00ff0000, 0x0000ff00, 0x000000ff };
WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w, bandmasks, null);
try {
BufferedImage opImage = createImage(opModel, opRaster, false, null);
return opImage;
} catch (Exception e) {
return bimage;
}
}
return bimage;
}
Aggregations