use of java.awt.image.IndexColorModel 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;
}
use of java.awt.image.IndexColorModel in project yamcs-studio by yamcs.
the class AWT2SWTImageConverter method convertToSWT.
static ImageData convertToSWT(BufferedImage bufferedImage) {
if (bufferedImage.getColorModel() instanceof DirectColorModel) {
DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask());
ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
int rgb = bufferedImage.getRGB(x, y);
int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
data.setPixel(x, y, pixel);
}
}
return data;
} else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
int size = colorModel.getMapSize();
byte[] reds = new byte[size];
byte[] greens = new byte[size];
byte[] blues = new byte[size];
colorModel.getReds(reds);
colorModel.getGreens(greens);
colorModel.getBlues(blues);
RGB[] rgbs = new RGB[size];
for (int i = 0; i < rgbs.length; i++) {
rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
}
PaletteData palette = new PaletteData(rgbs);
ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
data.transparentPixel = colorModel.getTransparentPixel();
WritableRaster raster = bufferedImage.getRaster();
int[] pixelArray = new int[1];
for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
raster.getPixel(x, y, pixelArray);
data.setPixel(x, y, pixelArray[0]);
}
}
return data;
}
return null;
}
use of java.awt.image.IndexColorModel in project yamcs-studio by yamcs.
the class AWT2SWTImageConverter method convertToAWT.
static BufferedImage convertToAWT(ImageData data) {
ColorModel colorModel = null;
PaletteData palette = data.palette;
if (palette.isDirect) {
colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
int pixel = data.getPixel(x, y);
RGB rgb = palette.getRGB(pixel);
bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue);
}
}
return bufferedImage;
} else {
RGB[] rgbs = palette.getRGBs();
byte[] red = new byte[rgbs.length];
byte[] green = new byte[rgbs.length];
byte[] blue = new byte[rgbs.length];
for (int i = 0; i < rgbs.length; i++) {
RGB rgb = rgbs[i];
red[i] = (byte) rgb.red;
green[i] = (byte) rgb.green;
blue[i] = (byte) rgb.blue;
}
if (data.transparentPixel != -1) {
colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
} else {
colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
}
BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
WritableRaster raster = bufferedImage.getRaster();
int[] pixelArray = new int[1];
for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
int pixel = data.getPixel(x, y);
pixelArray[0] = pixel;
raster.setPixel(x, y, pixelArray);
}
}
return bufferedImage;
}
}
use of java.awt.image.IndexColorModel in project jdk8u_jdk by JetBrains.
the class BufImgSurfaceData method createDataBP.
public static SurfaceData createDataBP(BufferedImage bImg, SurfaceType sType, double scaleX, double scaleY) {
BytePackedRaster bpRaster = (BytePackedRaster) bImg.getRaster();
BufImgSurfaceData bisd = new BufImgSurfaceData(bpRaster.getDataBuffer(), bImg, sType, scaleX, scaleY);
ColorModel cm = bImg.getColorModel();
IndexColorModel icm = ((cm instanceof IndexColorModel) ? (IndexColorModel) cm : null);
bisd.initRaster(bpRaster.getDataStorage(), bpRaster.getDataBitOffset() / 8, bpRaster.getDataBitOffset() & 7, bpRaster.getWidth(), bpRaster.getHeight(), 0, bpRaster.getScanlineStride(), icm);
return bisd;
}
use of java.awt.image.IndexColorModel 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);
}
}
}
Aggregations