Search in sources :

Example 86 with PixelGrabber

use of java.awt.image.PixelGrabber in project logisim-evolution by reds-heig.

the class ImageXmlFactory method createStream.

public void createStream(Image BoardPicture) {
    BufferedImage result = new BufferedImage(740, 400, BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g2 = result.createGraphics();
    int width = BoardPicture.getWidth(null);
    int hight = BoardPicture.getHeight(null);
    PixelGrabber pixelGrabber = new PixelGrabber(BoardPicture, 0, 0, width, hight, false);
    try {
        pixelGrabber.grabPixels();
    } catch (Exception e) {
        /* TODO: handle exceptions */
        logger.error("PixelGrabber exception: {}", e.getMessage());
    }
    ColorModel color_model = pixelGrabber.getColorModel();
    if (pixelGrabber.getPixels() instanceof byte[]) {
        byte[] the_pixels = (byte[]) pixelGrabber.getPixels();
        int index = 0;
        for (int y = 0; y < hight; y++) {
            for (int x = 0; x < width; x++) {
                Color PixCol = new Color(color_model.getRed(the_pixels[index]), color_model.getGreen(the_pixels[index]), color_model.getBlue(the_pixels[index++]));
                g2.setColor(PixCol);
                g2.fillRect(x, y, 1, 1);
            }
        }
    } else {
        int[] the_pixels = (int[]) pixelGrabber.getPixels();
        int index = 0;
        for (int y = 0; y < hight; y++) {
            for (int x = 0; x < width; x++) {
                Color PixCol = new Color(color_model.getRed(the_pixels[index]), color_model.getGreen(the_pixels[index]), color_model.getBlue(the_pixels[index++]));
                g2.setColor(PixCol);
                g2.fillRect(x, y, 1, 1);
            }
        }
    }
    ByteArrayOutputStream blaat = new ByteArrayOutputStream();
    try {
        ImageIO.write(result, "jpg", blaat);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        logger.error("JPEG Writer exception: {}", e.getMessage());
    }
    byte[] data = blaat.toByteArray();
    CodeTable = createCodeTable(data);
    AsciiStream = new StringBuilder();
    AsciiStream.append(V2_Identifier);
    for (byte datum : data) {
        String code = CodeTable[datum + 128];
        AsciiStream.append(code);
    }
}
Also used : Color(java.awt.Color) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D) ColorModel(java.awt.image.ColorModel) PixelGrabber(java.awt.image.PixelGrabber)

Example 87 with PixelGrabber

use of java.awt.image.PixelGrabber in project TrakEM2 by trakem2.

the class ExportBestFlatImage method makeFlatGrayImageAndAlpha.

/**
 * @return Return null when dimensions make the array larger than 2GB.
 */
public Pair<ByteProcessor, ByteProcessor> makeFlatGrayImageAndAlpha() {
    if (canUseAWTImage()) {
        // In color to preserve the alpha channel present in mipmaps
        final Image img = createAWTImage(ImagePlus.COLOR_RGB);
        final int width = img.getWidth(null);
        final int height = img.getHeight(null);
        final int[] pixels = new int[width * height];
        final PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
        try {
            pg.grabPixels();
        } catch (final InterruptedException e) {
        }
        ;
        final byte[] grey = new byte[pixels.length];
        final byte[] alpha = new byte[pixels.length];
        for (int i = 0; i < pixels.length; ++i) {
            final int p = pixels[i];
            alpha[i] = (byte) ((p & 0xff000000) >> 24);
            grey[i] = (byte) ((((p & 0x00ff0000) >> 16) + ((p & 0x0000ff00) >> 8) + (p & 0x000000ff)) / 3f);
        }
        return new Pair<ByteProcessor, ByteProcessor>(new ByteProcessor(width, height, grey, null), new ByteProcessor(width, height, alpha, null));
    }
    if (!isSmallerThan2GB()) {
        Utils.log("Cannot create an image larger than 2 GB.");
        return null;
    }
    if (loader.isMipMapsRegenerationEnabled()) {
        // Use mipmaps directly: they are already Gaussian-downsampled
        return ExportUnsignedByte.makeFlatImageFromMipMaps(patches, finalBox, 0, scale);
    }
    // Else: no mipmaps
    return ExportUnsignedByte.makeFlatImageFromOriginals(patches, finalBox, 0, scale);
}
Also used : ByteProcessor(ij.process.ByteProcessor) PixelGrabber(java.awt.image.PixelGrabber) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) Pair(mpicbg.trakem2.util.Pair)

Example 88 with PixelGrabber

use of java.awt.image.PixelGrabber in project Smack by igniterealtime.

the class ImageTransmitter method start.

public void start() {
    byte[] buf = new byte[1024];
    final DatagramPacket p = new DatagramPacket(buf, 1024);
    int keyframe = 0;
    while (on) {
        if (transmit) {
            BufferedImage capture = robot.createScreenCapture(area);
            QuantizeFilter filter = new QuantizeFilter();
            capture = filter.filter(capture, null);
            long trace = System.currentTimeMillis();
            if (++keyframe > KEYFRAME) {
                keyframe = 0;
            }
            LOGGER.fine("KEYFRAME:" + keyframe);
            for (int i = 0; i < maxI; i++) {
                for (int j = 0; j < maxJ; j++) {
                    final BufferedImage bufferedImage = capture.getSubimage(i * tileWidth, j * tileWidth, tileWidth, tileWidth);
                    int[] pixels = new int[tileWidth * tileWidth];
                    PixelGrabber pg = new PixelGrabber(bufferedImage, 0, 0, tileWidth, tileWidth, pixels, 0, tileWidth);
                    try {
                        if (pg.grabPixels()) {
                            if (keyframe == KEYFRAME || !Arrays.equals(tiles[i][j], pixels)) {
                                ByteArrayOutputStream baos = encoder.encode(bufferedImage);
                                if (baos != null) {
                                    try {
                                        Thread.sleep(1);
                                        baos.write(i);
                                        baos.write(j);
                                        byte[] bytesOut = baos.toByteArray();
                                        if (bytesOut.length > 1000)
                                            LOGGER.severe("Bytes out > 1000. Equals " + bytesOut.length);
                                        p.setData(bytesOut);
                                        p.setAddress(remoteHost);
                                        p.setPort(remotePort);
                                        try {
                                            socket.send(p);
                                        } catch (IOException e) {
                                            LOGGER.log(Level.WARNING, "exception", e);
                                        }
                                        tiles[i][j] = pixels;
                                    } catch (Exception e) {
                                        LOGGER.log(Level.WARNING, "exception", e);
                                    }
                                }
                            }
                        }
                    } catch (InterruptedException e) {
                        LOGGER.log(Level.WARNING, "exception", e);
                    }
                }
            }
            trace = System.currentTimeMillis() - trace;
            LOGGER.fine("Loop Time:" + trace);
            if (trace < 500) {
                try {
                    Thread.sleep(500 - trace);
                } catch (InterruptedException e) {
                    LOGGER.log(Level.WARNING, "exception", e);
                }
            }
        }
    }
}
Also used : DatagramPacket(java.net.DatagramPacket) PixelGrabber(java.awt.image.PixelGrabber) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) AWTException(java.awt.AWTException)

Aggregations

PixelGrabber (java.awt.image.PixelGrabber)88 BufferedImage (java.awt.image.BufferedImage)37 ColorModel (java.awt.image.ColorModel)20 IOException (java.io.IOException)17 Image (java.awt.Image)13 Color (java.awt.Color)9 DirectColorModel (java.awt.image.DirectColorModel)7 MemoryImageSource (java.awt.image.MemoryImageSource)6 Point2D (java.awt.geom.Point2D)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ObfuscatedName (net.runelite.mapping.ObfuscatedName)5 ObfuscatedSignature (net.runelite.mapping.ObfuscatedSignature)5 Graphics2D (java.awt.Graphics2D)3 MediaTracker (java.awt.MediaTracker)3 ByteProcessor (ij.process.ByteProcessor)2 AWTException (java.awt.AWTException)2 Graphics (java.awt.Graphics)2 IndexColorModel (java.awt.image.IndexColorModel)2 WritableRaster (java.awt.image.WritableRaster)2