use of java.awt.image.WritableRaster in project jdk8u_jdk by JetBrains.
the class RenderToCustomBufferTest method createCustomBuffer.
private static BufferedImage createCustomBuffer() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
WritableRaster wr = cm.createCompatibleWritableRaster(width, height);
return new BufferedImage(cm, wr, false, null);
}
use of java.awt.image.WritableRaster in project MantaroBot by Mantaro.
the class BadgeUtils method applyBadge.
public static byte[] applyBadge(byte[] avatarBytes, byte[] badgeBytes, int startX, int startY, boolean allWhite) {
BufferedImage avatar;
BufferedImage badge;
try {
avatar = ImageIO.read(new ByteArrayInputStream(avatarBytes));
badge = ImageIO.read(new ByteArrayInputStream(badgeBytes));
} catch (IOException impossible) {
throw new AssertionError(impossible);
}
WritableRaster raster = badge.getRaster();
if (allWhite) {
for (int xx = 0, width = badge.getWidth(); xx < width; xx++) {
for (int yy = 0, height = badge.getHeight(); yy < height; yy++) {
int[] pixels = raster.getPixel(xx, yy, (int[]) null);
pixels[0] = 255;
pixels[1] = 255;
pixels[2] = 255;
pixels[3] = pixels[3] == 255 ? 165 : 0;
raster.setPixel(xx, yy, pixels);
}
}
}
BufferedImage res = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
int circleCenterX = 88, circleCenterY = 88;
int width = 32, height = 32;
int circleRadius = 40;
Graphics2D g2d = res.createGraphics();
g2d.drawImage(avatar, 0, 0, 128, 128, null);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(new Color(0, 0, 165, 165));
g2d.fillOval(circleCenterX, circleCenterY, circleRadius, circleRadius);
g2d.drawImage(badge, startX, startY, width, height, null);
g2d.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(res, "png", baos);
} catch (IOException e) {
throw new AssertionError(e);
}
return baos.toByteArray();
}
use of java.awt.image.WritableRaster in project Lucee by lucee.
the class PointFilter method filter.
@Override
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
int width = src.getWidth();
int height = src.getHeight();
int type = src.getType();
WritableRaster srcRaster = src.getRaster();
if (dst == null)
dst = createCompatibleDestImage(src, null);
WritableRaster dstRaster = dst.getRaster();
setDimensions(width, height);
int[] inPixels = new int[width];
for (int y = 0; y < height; y++) {
// We try to avoid calling getRGB on images as it causes them to become unmanaged, causing horrible performance problems.
if (type == BufferedImage.TYPE_INT_ARGB) {
srcRaster.getDataElements(0, y, width, 1, inPixels);
for (int x = 0; x < width; x++) inPixels[x] = filterRGB(x, y, inPixels[x]);
dstRaster.setDataElements(0, y, width, 1, inPixels);
} else {
src.getRGB(0, y, width, 1, inPixels, 0, width);
for (int x = 0; x < width; x++) inPixels[x] = filterRGB(x, y, inPixels[x]);
dst.setRGB(0, y, width, 1, inPixels, 0, width);
}
}
return dst;
}
use of java.awt.image.WritableRaster in project vcell by virtualcell.
the class BrowseImage method gifFromVCImage.
/**
* This method was created in VisualAge.
* @return java.lang.Integer
* @param vci VCImage
*/
private static byte[] gifFromVCImage(VCImage vci) throws ImageException, IOException {
byte[] grey = new byte[256];
for (int c = 0; c < 256; c += 1) {
grey[c] = (byte) c;
}
IndexColorModel icm = new IndexColorModel(8, 256, grey, grey, grey);
BufferedImage bufferedImage = new BufferedImage(vci.getNumX(), vci.getNumY() * vci.getNumZ(), BufferedImage.TYPE_BYTE_INDEXED, icm);
WritableRaster raster = bufferedImage.getRaster();
DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
System.arraycopy(vci.getPixels(), 0, dataBuffer.getData(), 0, vci.getPixels().length);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "gif", byteArrayOutputStream);
byteArrayOutputStream.close();
return byteArrayOutputStream.toByteArray();
}
use of java.awt.image.WritableRaster in project vcell by virtualcell.
the class ITextWriter method generateGeometryImage.
// pretty similar to its static counterpart
/*
protected ByteArrayOutputStream generateDocStructureImage(Model model, String resolution) throws Exception {
if (model == null || !isValidResolutionSetting(resolution)) {
throw new IllegalArgumentException("Invalid parameters for generating structure image for model:" + model.getName());
}
ByteArrayOutputStream bos;
// Create a new model and clone the structures only
// Getting rid of species so that the image created will not have a problem being added to the document
// when there are more than 15 species in the model.
Model sparseModel = new Model(model.getName());
Structure[] oldStructures = (Structure[])BeanUtils.cloneSerializable(model.getStructures());
sparseModel.setStructures(oldStructures);
StructureCartoon scartoon = new StructureCartoon();
scartoon.setModel(sparseModel);
scartoon.refreshAll();
//scartoon.setZoomPercent(scartoon.getZoomPercent()*3);
BufferedImage dummyBufferedImage = new BufferedImage(DEF_IMAGE_WIDTH, DEF_IMAGE_HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D dummyGraphics = (Graphics2D)dummyBufferedImage.getGraphics();
Dimension prefDim = scartoon.getPreferedCanvasSize(dummyGraphics);
int width = (int)prefDim.getWidth()*110/100;
int height = (int)prefDim.getHeight()*110/100;
if (width < ITextWriter.DEF_IMAGE_WIDTH) {
width = ITextWriter.DEF_IMAGE_WIDTH;
}
if (height < ITextWriter.DEF_IMAGE_HEIGHT) {
height = ITextWriter.DEF_IMAGE_HEIGHT;
}
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = (Graphics2D)bufferedImage.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
while (true) {
GraphContainerLayout containerLayout = new GraphContainerLayoutVCellClassical();
containerLayout.layout(scartoon, g, new Dimension(width,height));
break;
}
scartoon.paint(g, null);
bos = encodeJPEG(bufferedImage);
return bos;
}
*/
protected ByteArrayOutputStream generateGeometryImage(Geometry geom) throws Exception {
GeometrySpec geomSpec = geom.getGeometrySpec();
IndexColorModel icm = DisplayAdapterService.getHandleColorMap();
geom.precomputeAll(new GeometryThumbnailImageFactoryAWT());
VCImage geomImage = geomSpec.getSampledImage().getCurrentValue();
if (geomImage == null) {
throw new Exception("generateGeometryImage error : No Image");
}
int x = geomImage.getNumX();
int y = geomImage.getNumY();
int z = geomImage.getNumZ();
BufferedImage bufferedImage = null;
WritableRaster pixelWR = null;
Image adjImage = null;
BufferedImage newBufferedImage = null;
if (geom.getDimension() > 0 && geom.getDimension() < 3) {
bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_BYTE_INDEXED, icm);
pixelWR = bufferedImage.getRaster();
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
pixelWR.setSample(i, j, 0, geomImage.getPixel(i, j, 0));
}
}
// Adjust the image width and height
// retaining the aspect ratio. Start by adjusting the height, then adjust width to maintain aspect ratio.
double scaleFactor = 1.0;
if (x * scaleFactor > DEF_GEOM_WIDTH) {
scaleFactor = ((double) DEF_GEOM_WIDTH) / x;
}
if (y * scaleFactor > DEF_GEOM_HEIGHT) {
scaleFactor = ((double) DEF_GEOM_HEIGHT) / y;
}
int adjX = (int) Math.ceil(x * scaleFactor);
int adjY = (int) Math.ceil(y * scaleFactor);
adjImage = bufferedImage.getScaledInstance(adjX, adjY, BufferedImage.SCALE_REPLICATE);
newBufferedImage = new BufferedImage(adjX, adjY, BufferedImage.TYPE_BYTE_INDEXED, icm);
newBufferedImage.getGraphics().drawImage(adjImage, 0, 0, null);
} else if (geom.getDimension() == 3) {
WritableRaster smallPixelWR = null;
int[] cmap = new int[256];
final int DISPLAY_DIM_MAX = 256;
try {
// Reset colormap (grayscale)
for (int i = 0; i < cmap.length; i += 1) {
int iv = (int) (0x000000FF & i);
cmap[i] = 0xFF << 24 | iv << 16 | iv << 8 | i;
}
// stretch cmap grays
if (geomImage != null && geomImage.getPixelClasses().length < 32) {
for (int i = 0; i < geomImage.getPixelClasses().length; i += 1) {
int stretchIndex = (int) (0xFF & geomImage.getPixelClasses()[i].getPixel());
int newI = 32 + (i * ((256 - 32) / geomImage.getPixelClasses().length));
cmap[stretchIndex] = 0xFF << 24 | newI << 16 | newI << 8 | newI;
}
}
// Set grid color
// white
cmap[cmap.length - 1] = 0xFFFFFFFF;
// Initialize image data
int xSide = 0;
int ySide = 0;
if (pixelWR == null) {
VCImage sampledImage = geomImage;
double side = Math.sqrt(x * y * z);
xSide = (int) Math.round(side / (double) x);
if (xSide == 0) {
xSide = 1;
}
if (xSide > z) {
xSide = z;
}
ySide = (int) Math.ceil((double) z / (double) xSide);
if (ySide == 0) {
ySide = 1;
}
if (ySide > z) {
ySide = z;
}
pixelWR = icm.createCompatibleWritableRaster(xSide * x, ySide * y);
byte[] sib = sampledImage.getPixels();
// write the image to buffer
int ystride = x;
int zstride = x * y;
for (int row = 0; row < ySide; row += 1) {
for (int col = 0; col < xSide; col += 1) {
int xoffset = col * x;
int yoffset = (row * y);
int zoffset = (col + (row * xSide)) * zstride;
if (zoffset >= sib.length) {
for (int xi = 0; xi < x; xi += 1) {
for (int yi = 0; yi < y; yi += 1) {
pixelWR.setSample(xi + xoffset, yi + yoffset, 0, cmap.length - 1);
}
}
} else {
for (int xi = 0; xi < x; xi += 1) {
for (int yi = 0; yi < y; yi += 1) {
pixelWR.setSample(xi + xoffset, yi + yoffset, 0, (int) (0xFF & sib[xi + (ystride * yi) + zoffset]));
}
}
}
}
}
// scale if necessary
double displayScale = 1.0;
if (pixelWR.getWidth() < DISPLAY_DIM_MAX || pixelWR.getHeight() < DISPLAY_DIM_MAX) {
displayScale = (int) Math.min((DISPLAY_DIM_MAX / pixelWR.getWidth()), (DISPLAY_DIM_MAX / pixelWR.getHeight()));
if (displayScale == 0) {
displayScale = 1;
}
}
if ((displayScale == 1) && (pixelWR.getWidth() > DISPLAY_DIM_MAX || pixelWR.getHeight() > DISPLAY_DIM_MAX)) {
displayScale = Math.max((pixelWR.getWidth() / DISPLAY_DIM_MAX), (pixelWR.getHeight() / DISPLAY_DIM_MAX));
// displayScale = Math.min(((double)DISPLAY_DIM_MAX/(double)pixelWR.getWidth()),((double)DISPLAY_DIM_MAX/(double)pixelWR.getHeight()));
if (displayScale == 0) {
displayScale = 1;
}
displayScale = 1.0 / displayScale;
}
if (displayScale != 1) {
java.awt.geom.AffineTransform at = new java.awt.geom.AffineTransform();
at.setToScale(displayScale, displayScale);
java.awt.image.AffineTransformOp ato = new java.awt.image.AffineTransformOp(at, java.awt.image.AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
smallPixelWR = ato.createCompatibleDestRaster(pixelWR);
ato.filter(pixelWR, smallPixelWR);
}
}
// draw labels and grid
if (pixelWR != null) {
bufferedImage = new java.awt.image.BufferedImage(icm, smallPixelWR, false, null);
if (xSide > 0 || ySide > 0) {
float gridXBlockLen = ((float) (bufferedImage.getWidth()) / xSide);
float gridYBlockLen = ((float) (bufferedImage.getHeight()) / ySide);
java.awt.Graphics g = bufferedImage.getGraphics();
g.setColor(java.awt.Color.white);
// horiz lines
for (int row = 0; row < ySide; row += 1) {
if (row > 0) {
g.drawLine(0, (int) (row * gridYBlockLen), bufferedImage.getWidth(), (int) (row * gridYBlockLen));
}
}
// vert lines
for (int col = 0; col < xSide; col += 1) {
if (col > 0) {
g.drawLine((int) (col * gridXBlockLen), 0, (int) (col * gridXBlockLen), bufferedImage.getHeight());
}
}
// z markers
if (xSide > 1 || ySide > 1) {
for (int row = 0; row < xSide; row += 1) {
for (int col = 0; col < ySide; col += 1) {
g.drawString("" + (1 + row + (col * xSide)), (int) (row * gridXBlockLen) + 3, (int) (col * gridYBlockLen) + 12);
}
}
}
}
}
} catch (Throwable e) {
throw new Exception("CreateGeometryImageIcon error\n" + (e.getMessage() != null ? e.getMessage() : e.getClass().getName()));
}
// Adjust the image width and height
adjImage = bufferedImage.getScaledInstance(smallPixelWR.getWidth(), smallPixelWR.getHeight(), BufferedImage.SCALE_REPLICATE);
newBufferedImage = new BufferedImage(smallPixelWR.getWidth(), smallPixelWR.getHeight(), BufferedImage.TYPE_BYTE_INDEXED, icm);
newBufferedImage.getGraphics().drawImage(adjImage, 0, 0, null);
}
ByteArrayOutputStream bos = null;
bos = encodeJPEG(newBufferedImage);
return bos;
}
Aggregations