Search in sources :

Example 6 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) {
                                    }
                                }
                            }
                        }
                    } 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)

Example 7 with PixelGrabber

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);
    }
}
Also used : PixelGrabber(java.awt.image.PixelGrabber) IOException(java.io.IOException)

Example 8 with PixelGrabber

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;
}
Also used : PixelGrabber(java.awt.image.PixelGrabber) BufferedImage(java.awt.image.BufferedImage) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with PixelGrabber

use of java.awt.image.PixelGrabber in project JMRI by JMRI.

the class NamedIcon method createRotatedImage.

/**
     * The following was based on a text-rotating applet from David Risner,
     * available at http://www.risner.org/java/rotate_text.html
     *
     * @param pImage     Image to transform
     * @param pComponent Component containing the image, needed to obtain a
     *                   MediaTracker to process the image consistently with
     *                   display
     * @param pRotation  0-3 number of 90-degree rotations needed
     * @return new Image object containing the rotated input image
     */
public Image createRotatedImage(Image pImage, Component pComponent, int pRotation) {
    if (log.isDebugEnabled()) {
        log.debug("createRotatedImage: pRotation= " + pRotation + ", mRotation= " + mRotation);
    }
    if (pRotation == 0) {
        return pImage;
    }
    MediaTracker mt = new MediaTracker(pComponent);
    mt.addImage(pImage, 0);
    try {
        mt.waitForAll();
    } catch (InterruptedException ie) {
        // retain if needed later
        Thread.currentThread().interrupt();
    }
    int w = pImage.getWidth(null);
    int h = pImage.getHeight(null);
    int[] pixels = new int[w * h];
    PixelGrabber pg = new PixelGrabber(pImage, 0, 0, w, h, pixels, 0, w);
    try {
        pg.grabPixels();
    } catch (InterruptedException ie) {
    }
    int[] newPixels = new int[w * h];
    // transform the pixels
    MemoryImageSource imageSource = null;
    switch(pRotation) {
        case // 90 degrees
        1:
            for (int y = 0; y < h; ++y) {
                for (int x = 0; x < w; ++x) {
                    newPixels[x * h + y] = pixels[y * w + (w - 1 - x)];
                }
            }
            imageSource = new MemoryImageSource(h, w, ColorModel.getRGBdefault(), newPixels, 0, h);
            break;
        case // 180 degrees
        2:
            for (int y = 0; y < h; ++y) {
                for (int x = 0; x < w; ++x) {
                    newPixels[x * h + y] = pixels[(w - 1 - x) * h + (h - 1 - y)];
                }
            }
            imageSource = new MemoryImageSource(w, h, ColorModel.getRGBdefault(), newPixels, 0, w);
            break;
        case // 270 degrees
        3:
            for (int y = 0; y < h; ++y) {
                for (int x = 0; x < w; ++x) {
                    newPixels[x * h + y] = pixels[(h - 1 - y) * w + x];
                }
            }
            imageSource = new MemoryImageSource(h, w, ColorModel.getRGBdefault(), newPixels, 0, h);
            break;
        default:
            log.warn("Unhandled rotation code: {}", pRotation);
            break;
    }
    Image myImage = pComponent.createImage(imageSource);
    mt.addImage(myImage, 1);
    try {
        mt.waitForAll();
    } catch (InterruptedException ie) {
    }
    return myImage;
}
Also used : MediaTracker(java.awt.MediaTracker) PixelGrabber(java.awt.image.PixelGrabber) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) MemoryImageSource(java.awt.image.MemoryImageSource)

Example 10 with PixelGrabber

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());
    }
}
Also used : ColorModel(java.awt.image.ColorModel) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel) DirectColorModel(java.awt.image.DirectColorModel) PixelGrabber(java.awt.image.PixelGrabber) IOException(java.io.IOException) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

PixelGrabber (java.awt.image.PixelGrabber)12 BufferedImage (java.awt.image.BufferedImage)6 Image (java.awt.Image)3 IOException (java.io.IOException)3 Color (java.awt.Color)2 ColorModel (java.awt.image.ColorModel)2 DirectColorModel (java.awt.image.DirectColorModel)2 IndexColorModel (java.awt.image.IndexColorModel)2 MemoryImageSource (java.awt.image.MemoryImageSource)2 AWTException (java.awt.AWTException)1 Dimension (java.awt.Dimension)1 MediaTracker (java.awt.MediaTracker)1 ComponentColorModel (java.awt.image.ComponentColorModel)1 PackedColorModel (java.awt.image.PackedColorModel)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DatagramPacket (java.net.DatagramPacket)1 JTextArea (javax.swing.JTextArea)1 Nullable (org.jetbrains.annotations.Nullable)1