Example usage for java.awt Image flush

List of usage examples for java.awt Image flush

Introduction

In this page you can find the example usage for java.awt Image flush.

Prototype

public void flush() 

Source Link

Document

Flushes all reconstructable resources being used by this Image object.

Usage

From source file:com.jcraft.weirdx.XPixmap.java

Image getImage(XWindow win, int fgPixel, int bgPixel) {
    if (img == null)
        return null;
    if (win == null)
        return getImage();
    if (imageDirty && colormap != win.getColormap()) {
        data = getData();/*  w  w  w  . jav a2s. c o  m*/
    }
    if (data != null) {
        if (depth != 1 && (filter != win.getColormap().cm)) {
            if (imageDirty) {
                image2data();
            }
            filter = win.getColormap().cm;
            dirty = true;
        } else {
        }
        if (time < win.getColormap().icmtime) {
            dirty = true;
        }

        if (dirty) {
            dirty = false;
            mkMIS();
            Image dataImg = Toolkit.getDefaultToolkit().createImage(mis);
            time = System.currentTimeMillis();
            imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow); //??
            dataImg.flush();
        }
    }
    return getImage();
}

From source file:com.jcraft.weirdx.XPixmap.java

Image getImage(XWindow win, GC gc) {
    if (img == null)
        return null;
    if (win == null)
        return getImage();

    if (imageDirty && colormap != win.getColormap()) {
        data = getData();//from   w  w  w .  j  a  va2 s .com
    }

    if (data != null) {
        if (depth != 1) {
            if (imageDirty) {
                image2data();
            }
            dirty = true;
        } else {
        }

        if (time < win.getColormap().icmtime) {
            dirty = true;
        }

        if (dirty) {
            dirty = false;
            mkMIS();
            Image dataImg = Toolkit.getDefaultToolkit().createImage(mis);
            time = System.currentTimeMillis();
            imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow); //??
            dataImg.flush();
        }
    }
    return getImage();
}

From source file:com.jcraft.weirdx.XPixmap.java

static void reqGetImage(Client c) throws IOException {
    int n, foo, format;
    InputOutput io = c.client;/*from  w  ww.  ja  va 2 s. co  m*/

    format = c.data;
    foo = io.readInt();
    XDrawable d = c.lookupDrawable(foo);
    c.length -= 2;
    if (d == null) {
        c.errorValue = foo;
        c.errorReason = 9; // BadDrawable;
        return;
    }

    int x, y, width, height;
    x = (short) io.readShort();
    y = (short) io.readShort();
    width = (short) io.readShort();
    height = (short) io.readShort();
    foo = io.readInt();
    c.length = 0;
    Image img = null;
    XColormap colormap = d.getColormap();
    img = d.getImage(null, x, y, width, height);

    //
    //if(d instanceof Window &&
    //   ((Window)d)==((Window)d).screen.root){
    //  Window tmp=(Window)d;
    //  img=RepaintManager.currentManager(tmp.ddxwindow).
    //   getOffscreenBuffer(tmp.ddxwindow, tmp.width, tmp.height);
    //}
    //else{ img=d.getImage(null, x, y, width, height); }

    int[] pixels = new int[width * height];
    PixelGrabber pg = new PixelGrabber(img, x, y, width, height, pixels, 0, width);

    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        LOG.error("interrupted waiting for pixels!");
        for (int i = 0; i < pixels.length; i++)
            pixels[i] = 0;
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        LOG.error("image fetch aborted or errored");
        for (int i = 0; i < pixels.length; i++)
            pixels[i] = 0;
    }

    if (d instanceof XWindow) {
        if (((XWindow) d) != ((XWindow) d).screen.root && img != ((XWindow) d).getImage()) {
            img.flush();
        }
    } else {
        if (img != ((XPixmap) d).getImage()) {
            img.flush();
        }
    }

    int i;
    int ww;
    if (d.depth == 1) {
        int www = (width % 32) / 8;
        int wwww = (width % 32) % 8;

        synchronized (io) {
            io.writeByte((byte) 1);
            io.writeByte((byte) 1);
            io.writeShort(c.seq);
            io.writeInt(((width + 31) / 32) * height);
            io.writeInt(0);
            io.writePad(20);
            i = 0;
            if (format == 1) {
                for (int hh = 0; hh < height; hh++) {
                    ww = width;
                    while (true) {
                        foo = 0;
                        if (32 < ww) {
                            for (int ii = 0; ii < 4; ii++) {
                                foo = 0;
                                i += 8;
                                for (int iii = 0; iii < 8; iii++) {
                                    i--;
                                    foo = (foo << 1) | ((pixels[i] & 0xffffff) != 0 ? 1 : 0);
                                }
                                i += 8;
                                io.writeByte((byte) (foo & 0xff));
                            }
                            ww -= 32;
                            continue;
                        }
                        if (ww != 0) {
                            for (int ii = 0; ii < www; ii++) {
                                foo = 0;
                                i += 8;
                                for (int iii = 0; iii < 8; iii++) {
                                    i--;
                                    foo = (foo << 1) | ((pixels[i] & 0xffffff) != 0 ? 1 : 0);
                                }
                                i += 8;
                                io.writeByte((byte) (foo & 0xff));
                            }
                            if (wwww != 0) {
                                foo = 0;
                                i += wwww;
                                for (int iii = 0; iii < wwww; iii++) {
                                    i--;
                                    foo = (foo << 1) | ((pixels[i] & 0xffffff) != 0 ? 1 : 0);
                                }
                                i += wwww;
                                io.writeByte((byte) (foo));
                                for (int ii = www + 1; ii < 4; ii++) {
                                    io.writeByte((byte) 0);
                                }
                            } else {
                                for (int ii = www; ii < 4; ii++) {
                                    io.writeByte((byte) 0);
                                }
                            }
                        }
                        break;
                    }
                }
            } else {
                // LSB
                for (int hh = 0; hh < height; hh++) {
                    ww = width;
                    while (true) {
                        foo = 0;
                        if (32 < ww) {
                            for (int ii = 0; ii < 32; ii++) {
                                foo = (foo << 1) | ((pixels[i] & 0xffffff) != 0 ? 1 : 0);
                                i++;
                                if (ii == 7 || ii == 15 || ii == 23 || ii == 31) {
                                    io.writeByte((byte) (bi_reverse(foo)));
                                    foo = 0;
                                }
                            }
                            ww -= 32;
                            continue;
                        }
                        if (ww != 0) {
                            for (int ii = 0; ii < ww; ii++) {
                                foo = foo << 1 | ((pixels[i] & 0xffffff) != 0 ? 1 : 0);
                                i++;
                                if (ii == 7 || ii == 15 || ii == 23 || ii == 31) {
                                    io.writeByte((byte) (bi_reverse(foo)));
                                    foo = 0;
                                }
                            }
                            for (int ii = ww; ii < 32; ii++) {
                                foo = (foo << 1) | 0;
                                if (ii == 7 || ii == 15 || ii == 23 || ii == 31) {
                                    io.writeByte((byte) (bi_reverse(foo)));
                                    foo = 0;
                                }
                            }
                        }
                        break;
                    }
                }
            }
            io.flush();
            return;
        }
    } else if (d.depth == 8) {
        if (format == 1) {

            synchronized (io) {
                io.writeByte((byte) 1);
                io.writeByte((byte) d.depth);
                io.writeShort(c.seq);
                n = (width + 3) / 4;
                io.writeInt(n * height);
                io.writeInt(0);
                io.writePad(20);

                i = 0;
                for (int hh = 0; hh < height; hh++) {
                    ww = width;
                    while (true) {
                        foo = 0;
                        if (4 < ww) {
                            for (int ii = 0; ii < 4; ii++) {
                                io.writeByte((colormap.rgb2pixel(pixels[i])) & 0xff);
                                i++;
                            }
                            ww -= 4;
                            continue;
                        }
                        if (ww != 0) {
                            for (int ii = 0; ii < ww; ii++) {
                                io.writeByte((colormap.rgb2pixel(pixels[i])) & 0xff);
                                i++;
                            }
                            ww = 4 - ww;
                            while (ww != 0) {
                                io.writeByte(0);
                                ww--;
                            }
                        }
                        break;
                    }
                }
                io.flush();
                return;
            }
        } else { // format==2

            synchronized (io) {
                io.writeByte((byte) 1);
                io.writeByte((byte) d.depth);
                io.writeShort(c.seq);
                n = (width + 3) / 4;
                io.writeInt(n * height);
                io.writeInt(0);
                io.writePad(20);

                i = 0;
                for (int hh = 0; hh < height; hh++) {
                    ww = width;
                    while (true) {
                        foo = 0;
                        if (4 < ww) {
                            for (int ii = 0; ii < 4; ii++) {
                                io.writeByte((colormap.rgb2pixel(pixels[i])) & 0xff);
                                i++;
                            }
                            ww -= 4;
                            continue;
                        }
                        if (ww != 0) {
                            for (int ii = 0; ii < ww; ii++) {
                                io.writeByte((colormap.rgb2pixel(pixels[i])) & 0xff);
                                i++;
                            }
                            ww = 4 - ww;
                            while (ww != 0) {
                                io.writeByte(0);
                                ww--;
                            }
                        }
                        break;
                    }
                }
                io.flush();
                return;
            }
        }
    } else if (d.depth == 16) {
        if (format == 2) {
            synchronized (io) {
                io.writeByte((byte) 1);
                io.writeByte((byte) d.depth);
                io.writeShort(c.seq);
                n = (width / 2 + (width % 2)) * 4;
                io.writeInt(n * height / 4);
                io.writeInt(0);
                io.writePad(20);
                i = 0;
                int iii;
                for (int hh = 0; hh < height; hh++) {
                    for (int ii = 0; ii < width; ii++) {
                        iii = pixels[i];
                        iii = ((iii >> 16) & 0xff) / 8 << 11 | ((iii >> 8) & 0xff) / 4 << 5
                                | ((iii) & 0xff) / 8;
                        io.writeByte((iii) & 0xff);
                        io.writeByte((iii >> 8) & 0xff);
                        i++;
                    }
                    if (width % 2 != 0)
                        io.writePad(2);
                }
                io.flush();
                return;
            }
        }
    }

    synchronized (io) {
        io.writeByte((byte) 0); // error!! Implementation
        io.writeByte((byte) 17);
        io.writeShort(c.seq);
        io.writePad(4);
        io.writeShort(0);
        io.writeByte((byte) 73);
        io.writePad(21);
        io.flush();
    }
}

From source file:com.jcraft.weirdx.DDXWindowImpSwing.java

public void setSize(int w, int h) {
    boolean resized = false;
    if (w != this.width || h != this.height)
        resized = true;//from   w  w w .  j  a v a  2 s.  c o  m

    if (!resized && !bwc) {
        return;
    }

    int offx = 0, offy = 0;

    int bitgrabity = window.attr & (15 << 8);
    if (bitgrabity != 0) {
        if (bitgrabity == (7 << 8) || // SouthWest
                bitgrabity == (8 << 8) || // South
                bitgrabity == (9 << 8)) { // SouthEast
            offy = this.height - h;
        }
        if (bitgrabity == (3 << 8) || // NorthEast
                bitgrabity == (6 << 8) || // East
                bitgrabity == (9 << 8)) { // SouthEast
            offx = this.width - w;
        }
    }

    //int originalWidth = this.width, originalHeight = this.height;
    this.width = w;
    this.height = h;
    super.setSize(w + 2 * bw, h + 2 * bw);

    bwc = false;

    if (window.screen.windowmode != WeirdX.InBrowser && window.hasFrame()) {
        java.awt.Window frame = window.getFrame();
        frame.validate();
        Insets insets = frame.getInsets();
        frame.setSize(w + 2 * bw + insets.left + insets.right, h + 2 * bw + insets.top + insets.bottom);
        frame.validate();
    }

    if (window.clss == InputOnly) {
        return;
    }

    if (offi != null && resized) {
        if (w < exposed.x || h < exposed.y) {
            exposed.setBounds(0, 0, 0, 0);
        } else if (w < (exposed.x + exposed.width) || h < (exposed.y + exposed.height)) {
            exposed.setBounds(exposed.x, exposed.y,
                    (exposed.width < (w - exposed.x) ? exposed.width : (w - exposed.x)),
                    (exposed.height < (h - exposed.y) ? exposed.height : (h - exposed.y)));
        }

        try {
            Image tmp = offi;
            Graphics tmpg = offg;

            offi = createImage(w, h);
            offg = offi.getGraphics();
            window.makeBackgroundTile(0, 0, w, h);
            if (bitgrabity != 0) {
                offg.drawImage(tmp, 0, 0, this);
            }

            if (offx != 0 || offy != 0) {
                int copyx = 0, copyy = 0, copyw = w, copyh = h, dx = 0, dy = 0;
                if (offy > 0) {
                    copyy = offy;
                    copyh = h - offy;
                    dy = offy * -1;
                } else if (offy < 0) {
                    dy = offy * -1;
                }
                if (offx > 0) {
                    copyx = offx;
                    copyw = w - offx;
                    dx = offx * -1;
                } else if (offx < 0) {
                    dx = offx * -1;
                }
                offg.copyArea(copyx, copyy, copyw, copyh, dx, dy);
            }

            if (tmp != offi) {
                tmp.flush();
                tmpg.dispose();
            }

            if (bitgrabity == 0) {
                exposed.setBounds(0, 0, 0, 0);
                window.makeBackgroundTile(0, 0, width, height);
            }
            window.currentGC = null;
        } catch (Exception e) {
            LOG.error(e);
            offi = null;
            offg = null;
        } catch (java.lang.OutOfMemoryError e) {
            LOG.error(e);
            offi = null;
            offg = null;
        }
        //System.out.println("DDXW: "+getBackground());
    }
}

From source file:com.jcraft.weirdx.DDXWindowImp.java

public void setSize(int w, int h) {
    boolean resized = false;
    if (w != this.width || h != this.height)
        resized = true;/*from w  w w.  j  av a2 s  .  c o  m*/

    if (!resized && !bwc) {
        return;
    }

    int offx = 0, offy = 0;

    int bitgrabity = window.attr & (15 << 8);
    if (bitgrabity != 0) {
        if (bitgrabity == (7 << 8) || // SouthWest
                bitgrabity == (8 << 8) || // South
                bitgrabity == (9 << 8)) { // SouthEast
            offy = this.height - h;
        }
        if (bitgrabity == (3 << 8) || // NorthEast
                bitgrabity == (6 << 8) || // East
                bitgrabity == (9 << 8)) { // SouthEast
            offx = this.width - w;
        }
    }

    //int originalWidth=this.width, originalHeight = this.height;
    this.width = w;
    this.height = h;
    super.setSize(w + 2 * bw, h + 2 * bw);

    bwc = false;

    if (window.screen.windowmode != WeirdX.InBrowser && window.hasFrame()) {
        java.awt.Window frame = window.getFrame();
        frame.validate();
        Insets insets = frame.getInsets();
        frame.setSize(w + 2 * bw + insets.left + insets.right, h + 2 * bw + insets.top + insets.bottom);
        frame.validate();
    }

    if (window.clss == InputOnly) {
        return;
    }

    if (offi != null && resized) {
        if (w < exposed.x || h < exposed.y) {
            exposed.setBounds(0, 0, 0, 0);
        } else if (w < (exposed.x + exposed.width) || h < (exposed.y + exposed.height)) {
            exposed.setBounds(exposed.x, exposed.y,
                    (exposed.width < (w - exposed.x) ? exposed.width : (w - exposed.x)),
                    (exposed.height < (h - exposed.y) ? exposed.height : (h - exposed.y)));
        }

        try {
            Image tmp = offi;
            Graphics tmpg = offg;

            offi = createImage(w, h);
            offg = offi.getGraphics();
            window.makeBackgroundTile(0, 0, w, h);
            if (bitgrabity != 0) {
                offg.drawImage(tmp, 0, 0, this);
            }

            if (offx != 0 || offy != 0) {
                int copyx = 0, copyy = 0, copyw = w, copyh = h, dx = 0, dy = 0;
                if (offy > 0) {
                    copyy = offy;
                    copyh = h - offy;
                    dy = offy * -1;
                } else if (offy < 0) {
                    dy = offy * -1;
                }
                if (offx > 0) {
                    copyx = offx;
                    copyw = w - offx;
                    dx = offx * -1;
                } else if (offx < 0) {
                    dx = offx * -1;
                }
                offg.copyArea(copyx, copyy, copyw, copyh, dx, dy);
            }

            if (tmp != offi) {
                tmp.flush();
                tmpg.dispose();
            }

            if (bitgrabity == 0) {
                exposed.setBounds(0, 0, 0, 0);
                window.makeBackgroundTile(0, 0, width, height);
            }
            window.currentGC = null;
        } catch (Exception e) {
            LOG.equals(e);
            offi = null;
            offg = null;
        } catch (java.lang.OutOfMemoryError e) {
            LOG.error(e);
            offi = null;
            offg = null;
        }
        //System.out.println("DDXW: "+getBackground());
    }
}

From source file:com.jcraft.weirdx.XPixmap.java

@SuppressWarnings("unused")
void copyArea(XPixmap dst, GC gc, int sx, int sy, int dx, int dy, int w, int h) {
    if ((width - sx) < w)
        w = width - sx;/*from w w w  .  j a  v  a  2  s  . c  o  m*/
    if ((dst.width - dx) < w)
        w = dst.width - dx;

    if ((height - sy) < h)
        h = height - sy;
    if ((dst.height - dy) < h)
        h = dst.height - dy;

    int s = sy * width + sx;
    int d = dy * dst.width + dx;

    Graphics g = dst.getGraphics();
    Image ii = getImage(gc, sx, sy, w, h);

    if (sx == 0 && sy == 0 && w == width && h == height) {
        g.drawImage(ii, dx, dy, width, height, Screen.screen[0].root.ddxwindow);
    } else {
        java.awt.Shape tmp = g.getClip();
        g.setClip(dx, dy, w, h);
        g.drawImage(ii, dx - sx, dy - sy, width, height, Screen.screen[0].root.ddxwindow);

        if (tmp == null) {
            g.setClip(0, 0, dst.width, dst.height);
        } else {
            g.setClip(tmp);
        }
    }
    dst.draw(dx, dy, w, h);

    if (ii != getImage()) {
        ii.flush();
    }
}

From source file:edu.harvard.iq.dvn.core.web.servlet.FileDownloadServlet.java

private boolean generateImageThumb(StudyFile file) {

    String fileLocation = file.getFileSystemLocation();
    if (fileLocation == null || fileLocation.trim().equals("")) {
        return false;
    }/*from   w  w w  .  j  a v  a 2  s .  c om*/
    String thumbFileLocation = fileLocation + ".thumb";

    // see if the thumb is already generated and saved:

    if (new File(thumbFileLocation).exists()) {
        return true;
    }

    // let's attempt to generate the thumb:

    // the default size of the thumbnail is 64 pixels horizontally.
    // The number 64 was picked arbitrarily; if a different size is 
    // desired, it can be configured via the dvn.image.thumbnail.size 
    // JVM option.

    Long thumbSize = Long.valueOf(64);

    String thumbSizeOption = System.getProperty("dvn.image.thumbnail.size");

    if (thumbSizeOption != null) {
        Long thumbSizeOptionValue = null;
        try {
            thumbSizeOptionValue = new Long(thumbSizeOption);
        } catch (NumberFormatException nfe) {
            // if the supplied option value is invalid/unparseable, we
            // ignore it and fall back to the default value. 
        }
        if (thumbSizeOptionValue != null && thumbSizeOptionValue.longValue() > 0) {
            thumbSize = thumbSizeOptionValue;
        }
    }

    // it is also possible to configure the thumbnail size for a 
    // specific dataverse: 

    VDC vdc = file.getStudy().getOwner();

    if (vdc != null) {
        thumbSizeOption = System.getProperty("dvn.image.thumbnail.size." + vdc.getAlias());

        if (thumbSizeOption != null) {
            Long thumbSizeOptionValue = null;
            try {
                thumbSizeOptionValue = new Long(thumbSizeOption);
            } catch (NumberFormatException nfe) {
                // if the supplied option value is invalid/unparseable, we
                // ignore it and fall back to the default value. 
            }
            if (thumbSizeOptionValue != null && thumbSizeOptionValue.longValue() > 0) {
                thumbSize = thumbSizeOptionValue;
            }
        }
    }

    // This is the default location of the "convert" executable from the
    // ImageMagick package. If it's installed in a different locaiton, 
    // it can be configured via the dvn.image.convert.exec JVM option. 

    String imageMagickConvertExec = "/usr/bin/convert";

    String imageMagickConvertExecOption = System.getProperty("dvn.image.convrt.exec");

    if (imageMagickConvertExecOption != null) {
        if (!imageMagickConvertExecOption.trim().equals("")) {
            imageMagickConvertExec = imageMagickConvertExecOption.trim();
        }
    }

    if (new File(imageMagickConvertExec).exists()) {
        String sizeOption = " -size " + thumbSize + "x" + thumbSize + " ";

        String ImageMagickCommandLine = imageMagickConvertExec + sizeOption + fileLocation + " -resize "
                + thumbSize + " -flatten png:" + thumbFileLocation;
        int exitValue = 1;

        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(ImageMagickCommandLine);
            exitValue = process.waitFor();
        } catch (Exception e) {
            exitValue = 1;
        }

        if (exitValue == 0) {
            return true;
        }
    }

    // For whatever reason, creating the thumbnail with ImageMagick
    // has failed.
    // Let's try again, this time with Java's standard Image
    // library:

    try {
        BufferedImage fullSizeImage = ImageIO.read(new File(fileLocation));

        if (fullSizeImage == null) {
            return false;
        }

        double scaleFactor = (thumbSize.doubleValue()) / (double) fullSizeImage.getWidth(null);
        int thumbHeight = (int) (fullSizeImage.getHeight(null) * scaleFactor);

        // We are willing to spend a few extra CPU cycles to generate
        // better-looking thumbnails, hence the SCALE_SMOOTH flag. 
        // SCALE_FAST would trade quality for speed. 

        java.awt.Image thumbImage = fullSizeImage.getScaledInstance(thumbSize.intValue(), thumbHeight,
                java.awt.Image.SCALE_SMOOTH);

        ImageWriter writer = null;
        Iterator iter = ImageIO.getImageWritersByFormatName("png");
        if (iter.hasNext()) {
            writer = (ImageWriter) iter.next();
        } else {
            return false;
        }

        BufferedImage lowRes = new BufferedImage(thumbSize.intValue(), thumbHeight, BufferedImage.TYPE_INT_RGB);
        lowRes.getGraphics().drawImage(thumbImage, 0, 0, null);

        ImageOutputStream ios = ImageIO.createImageOutputStream(new File(thumbFileLocation));
        writer.setOutput(ios);

        // finally, save thumbnail image:
        writer.write(lowRes);
        writer.dispose();

        ios.close();
        thumbImage.flush();
        fullSizeImage.flush();
        lowRes.flush();
        return true;
    } catch (Exception e) {
        // something went wrong, returning "false":
        dbgLog.info("ImageIO: caught an exception while trying to generate a thumbnail for " + fileLocation);

        return false;
    }
}

From source file:com.jcraft.weirdx.XWindow.java

static final void reqCopyPlane(Client c) throws IOException {
    int foo;//from  w  ww  .ja  va 2s. com
    XDrawable dsrc = null, ddst = null;
    InputOutput io = c.client;
    foo = io.readInt();
    dsrc = c.lookupDrawable(foo);
    if (dsrc == null) {
        c.errorValue = foo;
        c.errorReason = 9; // Drawable
    }

    int dest = io.readInt();
    ddst = c.lookupDrawable(dest);
    if (ddst == null && c.errorReason == 0) {
        c.errorValue = dest;
        c.errorReason = 9; // Drawable
    }
    foo = io.readInt();
    GC gc = c.lookupGC(foo);
    if (gc == null && c.errorReason == 0) {
        c.errorValue = foo;
        c.errorReason = 13; // BadGC;
    }
    int sx, sy, dx, dy, width, height;

    sx = (short) io.readShort();
    sy = (short) io.readShort();

    int destx, desty;

    destx = (short) io.readShort();
    dx = destx - sx;

    desty = (short) io.readShort();
    dy = desty - sy;

    width = io.readShort();
    height = io.readShort();

    int bplane = io.readInt();
    c.length -= 8;

    if (c.errorReason != 0) {
        return;
    }

    Graphics g = ddst.getGraphics();
    if (((dsrc instanceof XWindow) && !((XWindow) dsrc).ddxwindow.isVisible())) {
        g = null;
    }

    if (dsrc.width <= sx || dsrc.height <= sy || (sx + width) <= 0 || (sy + height) <= 0 || (destx + width) <= 0
            || (desty + height) <= 0) {
        g = null;
    }

    if (g != null) {
        if (dsrc instanceof XWindow) {
            if (ddst instanceof XWindow) {
                ((XWindow) dsrc).ddxwindow.copyArea(((XWindow) ddst), gc, sx, sy, width, height, destx, desty);
            }
        } else {
            Image img = null;
            if (ddst instanceof XPixmap) {
                ((XPixmap) dsrc).copyPlane((XPixmap) ddst, gc, sx, sy, destx, desty, width, height);
            } else {
                img = ((XPixmap) dsrc).getImage((XWindow) ddst, gc, sx, sy, width, height);
                XWindow wdst = (XWindow) ddst;

                if (sx == 0 && sy == 0 && width == dsrc.width && height == dsrc.height) {
                    wdst.ddxwindow.drawImage(gc.clip_mask, img, destx, desty, width, height);
                } else {
                    java.awt.Shape tmp = g.getClip();
                    g.clipRect(destx, desty, width, height);
                    wdst.ddxwindow.drawImage(gc.clip_mask, img, destx - sx, desty - sy, dsrc.width,
                            dsrc.height);
                    if (tmp == null) {
                        g.setClip(0, 0, wdst.width, wdst.height);
                    } else {
                        g.setClip(tmp);
                    }
                }
                wdst.draw(destx, desty, width, height);
                if (img != ((XPixmap) dsrc).getImage()) {
                    img.flush();
                }
            }
        }
    }
}

From source file:com.jcraft.weirdx.XWindow.java

static final void reqCopyArea(Client c) throws IOException {
    int foo;//  w w  w.j  a va 2  s . c  om
    XDrawable dsrc = null, ddst = null;
    InputOutput io = c.client;

    foo = io.readInt();
    dsrc = c.lookupDrawable(foo);
    if (dsrc == null) {
        c.errorValue = foo;
        c.errorReason = 9; // BadDrawable;
    }

    int dest = io.readInt();
    ddst = c.lookupDrawable(dest);
    if (ddst == null && c.errorReason == 0) {
        c.errorValue = dest;
        c.errorReason = 9; // BadDrawable;
    }

    foo = io.readInt();

    GC gc = c.lookupGC(foo);

    if (gc == null && c.errorReason == 0) {
        c.errorValue = foo;
        c.errorReason = 13; // BadGC;
    }

    int sx, sy;
    sx = (short) io.readShort();
    sy = (short) io.readShort();

    int destx, desty;
    destx = (short) io.readShort();
    desty = (short) io.readShort();

    int width, height;
    width = io.readShort();
    height = io.readShort();

    c.length -= 7;

    if (c.errorReason != 0) {
        return;
    }

    Graphics g = ddst.getGraphics();
    if (((dsrc instanceof XWindow) && !((XWindow) dsrc).ddxwindow.isVisible())) {
        g = null;
    }

    if (dsrc.width <= sx || dsrc.height <= sy || (sx + width) <= 0 || (sy + height) <= 0 || (destx + width) <= 0
            || (desty + height) <= 0) {
        g = null;
    }

    /*
    LOG.info("copyArea: "+dsrc+" sx="+sx+",sy="+sy+
                          ", w="+width+", h="+height+" "+
                           ddst+" destx="+destx+",desty="+desty);
    */

    if (g != null) {
        if (dsrc instanceof XWindow) {

            if (sx < 0) {
                sx = 0;
            }
            if (sy < 0) {
                sy = 0;
            }
            if (destx < 0) {
                destx = 0;
            }
            if (desty < 0) {
                desty = 0;
            }

            if (ddst instanceof XWindow) {
                ((XWindow) dsrc).ddxwindow.copyArea(((XWindow) ddst), gc, sx, sy, width, height, destx, desty);
            }
            //      else{
            //     ((Window)dsrc).ddxwindow.
            //       copyArea((Pixmap)ddst, gc,
            //           sx, sy, destx, desty, width, height);
            //   }
        } else {

            if (ddst instanceof XPixmap) {
                if (sx < 0) {
                    sx = 0;
                }
                if (sy < 0) {
                    sy = 0;
                }
                if (destx < 0) {
                    destx = 0;
                }
                if (desty < 0) {
                    desty = 0;
                }

                ((XPixmap) dsrc).copyArea((XPixmap) ddst, gc, sx, sy, destx, desty, width, height);
            } else {
                Image img = ((XPixmap) dsrc).getImage((XWindow) ddst, gc, sx, sy, width, height);

                XWindow wdst = (XWindow) ddst;

                if (sx == 0 && sy == 0 && width == dsrc.width && height == dsrc.height) {
                    wdst.ddxwindow.drawImage(gc.clip_mask, img, destx, desty, width, height);
                } else {
                    java.awt.Shape tmp = g.getClip();
                    if (destx >= 0 && desty >= 0) {
                        g.clipRect(destx, desty, width, height);
                    } else {
                        g.clipRect((destx < 0 ? 0 : destx), (desty < 0 ? 0 : desty),
                                (destx < 0 ? width + destx : width), (desty < 0 ? height + desty : height));
                    }
                    wdst.ddxwindow.drawImage(gc.clip_mask, img, destx - sx, desty - sy, dsrc.width,
                            dsrc.height);
                    if (tmp == null) {
                        g.setClip(0, 0, wdst.width, wdst.height);
                    } else {
                        g.setClip(tmp);
                    }
                }
                wdst.draw(destx, desty, width, height);
                if (img != ((XPixmap) dsrc).getImage()) {
                    img.flush();
                }
            }
        }
    }

    if ((gc.attr & GC.graphicsExposures) != 0) {
        c.cevent.mkNoExposure(dest, 0, 62);
        c.sendEvent(c.cevent, 1, 0, Event.NoEventMask, null);
    }
}

From source file:org.lnicholls.galleon.util.Tools.java

public static void cacheImage(Image image, int width, int height, String key) {
    if (image != null) {
        try {//from  w  w  w .j  ava 2s.c  o  m
            // TODO What if not bufferedimage??
            if (image instanceof BufferedImage) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(byteArrayOutputStream);
                encoder.encode((BufferedImage) image);
                byteArrayOutputStream.close();

                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        byteArrayOutputStream.toByteArray());
                if (byteArrayOutputStream.size() < 102400) {
                    BlobImpl blob = new BlobImpl(byteArrayInputStream, byteArrayOutputStream.size());

                    Thumbnail thumbnail = null;
                    try {
                        List list = ThumbnailManager.findByKey(key);
                        if (list != null && list.size() > 0)
                            thumbnail = (Thumbnail) list.get(0);
                    } catch (HibernateException ex) {
                        log.error("Thumbnail create failed", ex);
                    }

                    try {
                        if (thumbnail == null) {
                            thumbnail = new Thumbnail("Cached", "jpg", key);
                            thumbnail.setImage(blob);
                            thumbnail.setDateModified(new Date());
                            ThumbnailManager.createThumbnail(thumbnail);
                        } else {
                            thumbnail.setImage(blob);
                            thumbnail.setDateModified(new Date());
                            ThumbnailManager.updateThumbnail(thumbnail);
                        }
                    } catch (HibernateException ex) {
                        log.error("Thumbnail create failed", ex);
                    }
                }
            }
            image.flush();
            image = null;
        } catch (Exception ex) {
            Tools.logException(Tools.class, ex, key);
        }
    }
}