use of java.awt.image.Raster in project jna by java-native-access.
the class AlphaMaskDemo method updateX11.
private void updateX11(boolean a, boolean i) {
X11 x11 = X11.INSTANCE;
X11.Window win = X11.Window.None;
Display dpy = x11.XOpenDisplay(null);
try {
if (!alphaWindow.isDisplayable()) {
alphaWindow.pack();
if (System.getProperty("java.version").matches("^1\\.4\\..*"))
alphaWindow.setVisible(true);
win = new X11.Window((int) Native.getWindowID(alphaWindow));
Window parent = alphaWindow.getOwner();
Point where = parent.getLocationOnScreen();
where.translate(parent.getWidth(), 0);
alphaWindow.removeAll();
alphaWindow.setLocation(where);
alphaWindow.setBackground(new Color(0, 0, 0, 0));
} else {
win = new X11.Window((int) Native.getWindowID(alphaWindow));
}
if (i) {
int w = image.getWidth(null);
int h = image.getHeight(null);
alphaWindow.setSize(w, h);
if (buffer == null || buffer.size() != w * h * 4) {
buffer = new com.sun.jna.Memory(w * h * 4);
pixels = new int[w * h];
}
BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics g = buf.getGraphics();
g.drawImage(image, 0, 0, w, h, null);
long start = System.currentTimeMillis();
long blitTime, putImageTime, write;
GC gc = x11.XCreateGC(dpy, win, new NativeLong(0), null);
long gcTime = System.currentTimeMillis();
try {
Raster raster = buf.getData();
int[] pixel = new int[4];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
raster.getPixel(x, y, pixel);
int alpha = (pixel[3] & 0xFF) << 24;
int red = (pixel[2] & 0xFF);
int green = (pixel[1] & 0xFF) << 8;
int blue = (pixel[0] & 0xFF) << 16;
pixels[y * w + x] = alpha | red | green | blue;
}
}
blitTime = System.currentTimeMillis();
X11.XWindowAttributes xwa = new X11.XWindowAttributes();
x11.XGetWindowAttributes(dpy, win, xwa);
X11.XImage image = x11.XCreateImage(dpy, xwa.visual, 32, X11.ZPixmap, 0, buffer, w, h, 32, w * 4);
buffer.write(0, pixels, 0, pixels.length);
write = System.currentTimeMillis();
x11.XPutImage(dpy, win, gc, image, 0, 0, 0, 0, w, h);
x11.XFree(image.getPointer());
putImageTime = System.currentTimeMillis();
} finally {
if (gc != null)
x11.XFreeGC(dpy, gc);
}
long end = System.currentTimeMillis();
//System.out.println("gc: " + (gcTime-start) + "ms");
//System.out.println("blit: " + (blitTime-gcTime) + "ms");
//System.out.println("write: " + (write-blitTime) + "ms");
//System.out.println("put image: " + (putImageTime-write) + "ms");
//System.out.println("total: " + (end-start) + "ms");
}
} finally {
if (dpy != null)
x11.XCloseDisplay(dpy);
}
if (a)
WindowUtils.setWindowAlpha(alphaWindow, alpha);
if (!alphaWindow.isVisible()) {
alphaWindow.setVisible(true);
// hack for initial refresh (X11)
update(true, true);
}
}
use of java.awt.image.Raster in project nutz by nutzam.
the class Images method readJpeg.
/**
* 尝试读取JPEG文件的高级方法,可读取32位的jpeg文件
* <p/>
* 来自:
* http://stackoverflow.com/questions/2408613/problem-reading-jpeg-image-
* using-imageio-readfile-file
*
*/
private static BufferedImage readJpeg(InputStream in) throws IOException {
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
ImageReader reader = null;
while (readers.hasNext()) {
reader = readers.next();
if (reader.canReadRaster()) {
break;
}
}
if (reader == null)
return null;
ImageInputStream input = ImageIO.createImageInputStream(in);
reader.setInput(input);
// Read the image raster
Raster raster = reader.readRaster(0, null);
BufferedImage image = createJPEG4(raster);
ByteArrayOutputStream out = new ByteArrayOutputStream();
writeJpeg(image, out, 1);
out.flush();
return read(new ByteArrayInputStream(out.toByteArray()));
}
use of java.awt.image.Raster in project intellij-code-outline by sitano.
the class CodeOutlineImage method moveDataUp.
/**
* Copies data between the given images, starting at the top of the images
* and copying then moving downwards line by line. This method is
* suitable for when the two given images are subimages of the same image,
* and their data may overlap.
*
* @param from the source image
* @param to the destination image
*/
private static void moveDataUp(BufferedImage from, BufferedImage to) {
Raster raster = from.getRaster();
WritableRaster outRaster = to.getRaster();
int width = outRaster.getWidth();
int height = Math.min(raster.getHeight(), outRaster.getHeight());
int startX = outRaster.getMinX();
int startY = outRaster.getMinY();
Object tdata = null;
for (int i = startY; i < startY + height; i++) {
tdata = raster.getDataElements(raster.getMinX(), i, width, 1, tdata);
outRaster.setDataElements(startX, i, width, 1, tdata);
}
}
use of java.awt.image.Raster in project jdk8u_jdk by JetBrains.
the class WBMPPluginTest method compare.
private boolean compare(BufferedImage in, BufferedImage out) {
int width = in.getWidth();
int height = in.getHeight();
if (out.getWidth() != width || out.getHeight() != height) {
throw new RuntimeException("Dimensions changed!");
}
Raster oldras = in.getRaster();
ColorModel oldcm = in.getColorModel();
Raster newras = out.getRaster();
ColorModel newcm = out.getColorModel();
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
Object oldpixel = oldras.getDataElements(i, j, null);
int oldrgb = oldcm.getRGB(oldpixel);
int oldalpha = oldcm.getAlpha(oldpixel);
Object newpixel = newras.getDataElements(i, j, null);
int newrgb = newcm.getRGB(newpixel);
int newalpha = newcm.getAlpha(newpixel);
if (newrgb != oldrgb || newalpha != oldalpha) {
throw new RuntimeException("Pixels differ at " + i + ", " + j);
}
}
}
return true;
}
use of java.awt.image.Raster in project jdk8u_jdk by JetBrains.
the class BMPWriteParamTest method compare.
private static boolean compare(final BufferedImage in, final BufferedImage out) {
final int width = in.getWidth();
int height = in.getHeight();
if (out.getWidth() != width || out.getHeight() != height) {
throw new RuntimeException("Dimensions changed!");
}
Raster oldras = in.getRaster();
ColorModel oldcm = in.getColorModel();
Raster newras = out.getRaster();
ColorModel newcm = out.getColorModel();
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
Object oldpixel = oldras.getDataElements(i, j, null);
int oldrgb = oldcm.getRGB(oldpixel);
int oldalpha = oldcm.getAlpha(oldpixel);
Object newpixel = newras.getDataElements(i, j, null);
int newrgb = newcm.getRGB(newpixel);
int newalpha = newcm.getAlpha(newpixel);
if (newrgb != oldrgb || newalpha != oldalpha) {
// showDiff(in, out);
throw new RuntimeException("Pixels differ at " + i + ", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));
}
}
}
return true;
}
Aggregations