use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class RasterWithMinXTest method main.
public static void main(String[] args) {
String format = "jpeg";
// Set output file.
ImageOutputStream output = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());
// Create image.
BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_3BYTE_BGR);
// Populate image.
int[] rgbArray = new int[256];
for (int i = 0; i < 256; i++) {
Arrays.fill(rgbArray, i);
bi.setRGB(0, i, 256, 1, rgbArray, 0, 256);
}
// create translated raster in order to get non-zero minX and minY
WritableRaster r = (WritableRaster) bi.getRaster().createTranslatedChild(64, 64);
Iterator i = ImageIO.getImageWritersByFormatName(format);
ImageWriter iw = null;
while (i.hasNext() && iw == null) {
Object o = i.next();
if (o instanceof com.sun.imageio.plugins.jpeg.JPEGImageWriter) {
iw = (ImageWriter) o;
}
}
if (iw == null) {
throw new RuntimeException("No available image writer");
}
ImageWriteParam iwp = iw.getDefaultWriteParam();
IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
IIOImage img = new IIOImage(r, null, metadata);
iw.setOutput(output);
try {
iw.write(img);
} catch (RasterFormatException e) {
e.printStackTrace();
throw new RuntimeException("RasterException occurs. Test Failed!");
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Unexpected Exception");
}
// test case of theImageWriteParam with non-null sourceRegion
iwp.setSourceRegion(new Rectangle(32, 32, 192, 192));
metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
try {
iw.write(metadata, img, iwp);
} catch (RasterFormatException e) {
e.printStackTrace();
throw new RuntimeException("SetSourceRegion causes the RasterException. Test Failed!");
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Unexpected Exception");
}
}
use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class GrayPngTest method getTestImage.
private BufferedImage getTestImage(int trans_type, int trans_pixel) {
IndexColorModel icm = null;
switch(trans_type) {
case Transparency.OPAQUE:
icm = new IndexColorModel(bpp, numColors, r, g, b);
break;
case Transparency.BITMASK:
icm = new IndexColorModel(bpp, numColors, r, g, b, trans_pixel);
break;
case Transparency.TRANSLUCENT:
a = Arrays.copyOf(r, r.length);
icm = new IndexColorModel(bpp, numColors, r, g, b, a);
break;
default:
throw new RuntimeException("Invalid transparency: " + trans_type);
}
int w = 256 * 2;
int h = 200;
dx = w / (numColors);
WritableRaster wr = icm.createCompatibleWritableRaster(w, h);
for (int i = 0; i < numColors; i++) {
int rx = i * dx;
int[] samples = new int[h * dx];
Arrays.fill(samples, i);
wr.setPixels(rx, 0, dx, h, samples);
}
// horizontal line with transparent color
int[] samples = new int[w * 10];
Arrays.fill(samples, trans_pixel);
wr.setPixels(0, h / 2 - 5, w, 10, samples);
// create index color model
return new BufferedImage(icm, wr, false, null);
}
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 LUTCompareTest method createFrame.
private static BufferedImage createFrame(int[] palette) {
IndexColorModel icm = new IndexColorModel(getNumBits(palette.length), palette.length, palette, 0, false, -1, DataBuffer.TYPE_BYTE);
WritableRaster wr = icm.createCompatibleWritableRaster(w, h);
int[] samples = new int[w * h];
Arrays.fill(samples, 0);
wr.setSamples(0, 0, w, h, 0, samples);
BufferedImage img = new BufferedImage(icm, wr, false, null);
return img;
}
Aggregations