use of java.awt.image.PixelGrabber in project LoboEvolution by LoboEvolution.
the class InputImage method hasAlpha.
private static boolean hasAlpha(Image image) {
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage) image;
return bimage.getColorModel().hasAlpha();
}
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
ColorModel cm = pg.getColorModel();
return cm.hasAlpha();
}
use of java.awt.image.PixelGrabber in project vcell by virtualcell.
the class NativeImage method createPixels.
/**
* This method was created in VisualAge.
* @return byte[]
*/
private void createPixels() throws Exception {
if (bPixelsExist()) {
return;
}
PixelGrabber pg = new PixelGrabber(getJavaImage(), 0, 0, getSize().getX(), getSize().getX(), false);
if (!pg.grabPixels()) {
throw new Exception("Error getting pixels, status = " + pg.getStatus());
}
Object pixels = pg.getPixels();
ColorModel colorModel = null;
while (colorModel == null) {
colorModel = pg.getColorModel();
}
if ((pixels instanceof int[]) && (colorModel instanceof DirectColorModel)) {
newRGBAPixels = (int[]) pixels;
} else if ((pixels instanceof byte[]) && (colorModel instanceof IndexColorModel)) {
newIndexPixels = (byte[]) pixels;
IndexColorModel indexColorModel = (IndexColorModel) colorModel;
int colorMapSize = indexColorModel.getMapSize();
byte[] temp_colorMap = new byte[colorMapSize * IndexColorMap.RGB_PACK_SIZE];
byte[] reds = new byte[colorMapSize];
indexColorModel.getReds(reds);
byte[] greens = new byte[colorMapSize];
indexColorModel.getGreens(greens);
byte[] blues = new byte[colorMapSize];
indexColorModel.getBlues(blues);
for (int c = 0; c < colorMapSize; c += 1) {
// make packed R,G,B array for ColorIndexModel
int packCount = c * IndexColorMap.RGB_PACK_SIZE;
temp_colorMap[packCount] = reds[c];
temp_colorMap[packCount + 1] = greens[c];
temp_colorMap[packCount + 2] = blues[c];
}
colorMap = new IndexColorMap(temp_colorMap);
} else {
throw new Exception("Unknown combination of data type=" + pixels.getClass().toString() + " and ColorModel=" + colorModel.getClass().toString());
}
}
use of java.awt.image.PixelGrabber in project hale by halestudio.
the class ImageMarker method hasAlpha.
/**
* Determines if an image has transparent pixels
*
* @param image the {@link Image}
* @return if the given image has transparent pixels
*/
public static boolean hasAlpha(Image image) {
// If buffered image, the color model is readily available
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage) image;
return bimage.getColorModel().hasAlpha();
}
// Use a pixel grabber to retrieve the image's color model;
// grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
// ignore
}
// Get the image's color model
ColorModel cm = pg.getColorModel();
return cm.hasAlpha();
}
use of java.awt.image.PixelGrabber in project intellij-plugins by JetBrains.
the class ImageUtil method write.
public static void write(VirtualFile file, String mimeType, FileOutputStream out) throws IOException {
Image image = Toolkit.getDefaultToolkit().createImage(file.contentsToByteArray());
PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, -1, -1, true);
try {
pixelGrabber.grabPixels();
} catch (InterruptedException ignored) {
throw new IOException("Failed to grab pixels for image " + file.getPresentableUrl());
}
if (((pixelGrabber.getStatus() & ImageObserver.WIDTH) == 0) || ((pixelGrabber.getStatus() & ImageObserver.HEIGHT) == 0)) {
throw new IOException("Failed to grab pixels for image " + file.getPresentableUrl());
}
final byte[] byteBuffer = BUFFER.get();
IOUtil.writeShort(pixelGrabber.getWidth(), byteBuffer, 0);
IOUtil.writeShort(pixelGrabber.getHeight(), byteBuffer, 2);
byteBuffer[4] = (byte) ((mimeType == null ? file.getName().endsWith(".jpg") : mimeType.equals("image/jpeg")) ? 0 : 1);
out.write(byteBuffer, 0, 5);
int bufferLength = 0;
for (int pixel : (int[]) pixelGrabber.getPixels()) {
byteBuffer[bufferLength++] = (byte) ((pixel >> 24) & 0xff);
byteBuffer[bufferLength++] = (byte) ((pixel >> 16) & 0xff);
byteBuffer[bufferLength++] = (byte) ((pixel >> 8) & 0xff);
byteBuffer[bufferLength++] = (byte) (pixel & 0xff);
if (bufferLength == MAX_BUFFER_LENGTH) {
//noinspection ConstantConditions
out.write(byteBuffer, 0, bufferLength);
bufferLength = 0;
}
}
if (bufferLength > 0) {
out.write(byteBuffer, 0, bufferLength);
}
}
use of java.awt.image.PixelGrabber in project intellij-community by JetBrains.
the class UIUtil method getColorAt.
@Nullable
public static Color getColorAt(final Icon icon, final int x, final int y) {
if (0 <= x && x < icon.getIconWidth() && 0 <= y && y < icon.getIconHeight()) {
final BufferedImage image = createImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
icon.paintIcon(null, image.getGraphics(), 0, 0);
final int[] pixels = new int[1];
final PixelGrabber pixelGrabber = new PixelGrabber(image, x, y, 1, 1, pixels, 0, 1);
try {
pixelGrabber.grabPixels();
return new Color(pixels[0]);
} catch (InterruptedException ignored) {
}
}
return null;
}
Aggregations