Example usage for java.awt Image getSource

List of usage examples for java.awt Image getSource

Introduction

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

Prototype

public abstract ImageProducer getSource();

Source Link

Document

Gets the object that produces the pixels for the image.

Usage

From source file:com.piaoyou.util.ImageUtil.java

/**
 * This method returns a fit-sized image for a source image,
 * this method retains the ratio of the source image
 *//*  w  w  w . j a  v  a  2 s  .  co m*/
public static Image getFitSizeImage(Image srcImage, int fitWidth, int fitHeight) throws IOException {
    if ((fitWidth < 100) || (fitHeight < 100))
        throw new IllegalArgumentException("Cannot accept values < 100");

    int srcWidth = srcImage.getWidth(null);//xem lai cho nay vi neu dung BufferedImage thi khong can null
    int srcHeight = srcImage.getHeight(null);
    //log.trace("src w = " + srcWidth + " h = " + srcHeight);

    // don't need any transforms
    if ((srcWidth == fitWidth) && (srcHeight == fitHeight))
        return srcImage;

    int newWidth = srcWidth;
    int newHeight = srcHeight;

    double fitRatio = (double) fitWidth / fitHeight;
    double srcRatio = (double) srcWidth / srcHeight;
    if (srcRatio > fitRatio) {// must cut the width of the source image
        newWidth = (int) (srcHeight * fitRatio);
    } else {// must cut the height of the source image
        newHeight = (int) (srcWidth / fitRatio);
    }
    //log.trace("new w = " + newWidth + " h = " + newHeight);

    ImageFilter cropFilter = new CropImageFilter((srcWidth - newWidth) / 2, (srcHeight - newHeight) / 2,
            newWidth, newHeight);
    ImageProducer cropProd = new FilteredImageSource(srcImage.getSource(), cropFilter);
    Image cropImage = Toolkit.getDefaultToolkit().createImage(cropProd);

    Image retImage = new ImageIcon(getScaledInstance(cropImage, fitWidth, fitHeight)).getImage();

    return retImage;
}

From source file:GifEncoder.java

/**
 * Constructor//from w ww .j a va  2s  .  co  m
 *  @param img The image to encode.
 *  @param out The stream to write the bytes to.
 */
public GifEncoder(Image img, OutputStream out) throws IOException {
    this(img.getSource(), out);
}

From source file:GifEncoder.java

/** Constructor from Image with interlace setting.
 * @param img The image to encode.//from   w w w  . ja v a  2  s .  co m
 * @param out The stream to write the GIF to.
 * @param interlace Whether to interlace.
 * @param transparentColor The color to use for transparency
 */
public GifEncoder(Image img, OutputStream out, boolean interlace, Color transparentColor) throws IOException {
    RGBImageFilter f = new TransparentFilter(transparentColor);
    this.producer = new FilteredImageSource(img.getSource(), f);
    this.out = out;
    this.interlace = interlace;
}

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

public Image getImage(GC gc, int x, int y, int w, int h) {
    Image i = getImage();
    if (gc != null && gc.clip_mask != null && gc.clip_mask instanceof ClipPixmap) {
        TransparentFilter tf = new TransparentFilter(0, 0, (XPixmap) (gc.clip_mask.getMask()));
        i = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(i.getSource(), tf));
    }//ww w .  ja v  a 2 s .c  o m
    return i;
}

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

Image getImage(GC gc, int x, int y, int w, int h) {
    if (data != null && time < colormap.icmtime) {
        mkMIS();//  ww  w.j a  va2 s. co m
        Image dataImg = Toolkit.getDefaultToolkit().createImage(mis);
        time = System.currentTimeMillis();
        imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow); //??
        dataImg.flush();
    }

    Image i = getImage();
    if (gc != null && gc.clip_mask != null && gc.clip_mask instanceof ClipPixmap) {
        TransparentFilter tf = new TransparentFilter(0, 0, (XPixmap) (gc.clip_mask.getMask()));
        i = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(i.getSource(), tf));
    }
    return i;
}

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

Image getImage(GC gc, int x, int y, int w, int h) {
    if (data != null && time < colormap.icmtime) {
        mkMIS();/*from   w w w .j  ava 2s.  co m*/
        Image dataImg = Toolkit.getDefaultToolkit().createImage(mis);
        time = System.currentTimeMillis();
        imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow); //??
        dataImg.flush();
    }

    Image i = getImage();

    if (gc != null && gc.clip_mask != null && gc.clip_mask instanceof ClipPixmap) {
        TransparentFilter tf = new TransparentFilter(0, 0, (XPixmap) (gc.clip_mask.getMask()));
        i = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(i.getSource(), tf));
    }
    return i;
}

From source file:nz.co.fortytwo.freeboard.server.util.ChartProcessor.java

public Image makeColorTransparent(Image im, final Color color) {
    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

From source file:org.exist.xquery.modules.image.CropFunction.java

/**
 * evaluate the call to the xquery crop() function,
 * it is really the main entry point of this class
 * /*  w  w w  .  j  a va  2s  .  co m*/
 * @param args      arguments from the crop() function call
 * @param contextSequence   the Context Sequence to operate on (not used here internally!)
 * @return      A sequence representing the result of the crop() function call
 * 
 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
 */
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    //was an image and a mime-type speficifed
    if (args[0].isEmpty() || args[2].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }

    //get the maximum dimensions to crop to
    int x1 = 0;
    int y1 = 0;
    int x2 = MAXHEIGHT;
    int y2 = MAXWIDTH;

    if (!args[1].isEmpty()) {
        x1 = ((IntegerValue) args[1].itemAt(0)).getInt();
        if (args[1].hasMany()) {
            y1 = ((IntegerValue) args[1].itemAt(1)).getInt();
            x2 = ((IntegerValue) args[1].itemAt(2)).getInt();
            y2 = ((IntegerValue) args[1].itemAt(3)).getInt();
        }
    }

    //get the mime-type
    String mimeType = args[2].itemAt(0).getStringValue();
    String formatName = mimeType.substring(mimeType.indexOf("/") + 1);

    //TODO currently ONLY tested for JPEG!!!
    Image image = null;
    BufferedImage bImage = null;
    try {
        //get the image data
        image = ImageIO.read(((BinaryValue) args[0].itemAt(0)).getInputStream());
        //         image = ImageModule.getImage((Base64BinaryValueType)args[0].itemAt(0));
        //               image = ImageIO.read(new ByteArrayInputStream(getImageData((Base64BinaryValueType)args[0].itemAt(0))));
        if (image == null) {
            logger.error("Unable to read image data!");
            return Sequence.EMPTY_SEQUENCE;
        }

        //crop the image
        Image cropImage = Toolkit.getDefaultToolkit()
                .createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(x1, y1, x2, y2)));
        if (cropImage instanceof BufferedImage) {
            // just in case cropImage is allready an BufferedImage
            bImage = (BufferedImage) cropImage;
        } else {
            bImage = new BufferedImage(cropImage.getHeight(null), cropImage.getWidth(null),
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bImage.createGraphics(); // Paint the image onto the buffered image
            g.drawImage(cropImage, 0, 0, null);
            g.dispose();
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bImage, formatName, os);

        //return the new croped image data
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(),
                new ByteArrayInputStream(os.toByteArray()));

    } catch (Exception e) {
        throw new XPathException(this, e.getMessage());
    }
}

From source file:org.mbari.aved.ui.classifier.ClassModelListRenderer.java

/**
 * Converts and image into gray scale if the color space is grayscale
 * TODO: convert to whatever color space is required here, not just gray
 * @param image/*from www .j a v  a2s  .c om*/
 * @param colorSpace
 * @return
 */
public static ImageIcon convertImageIcon(Image image, ColorSpace colorSpace) {

    // Convert to gray if in that color space. Images
    // are actually stored in color but converted to gray
    // in the classifier. Display them as GRAY to avoid
    // confusion as the classifier can only work in one
    // color space at a time, e.g. classes in different
    // color spaces cannot be mixed
    if (colorSpace != null && colorSpace.equals(ColorSpace.GRAY)) {

        // This isn't necessarily how the image is rendered in the Matlab
        // code, but it makes it easier to see the thumbnails
        ImageFilter filter = new GrayFilter(true, 50);
        ImageProducer prod;

        prod = new FilteredImageSource(image.getSource(), filter);

        Image grayScale = Toolkit.getDefaultToolkit().createImage(prod);

        return new ImageIcon(grayScale);
    } else {
        return new ImageIcon(image);
    }
}

From source file:org.polymap.core.data.image.ImageTransparencyProcessor.java

public static BufferedImage transparency(Image image, final Color markerColor) throws IOException {
    long start = System.currentTimeMillis();

    RGBImageFilter filter = new RGBImageFilter() {
        // the color we are looking for... Alpha bits are set to opaque
        public int markerRGB = markerColor.getRGB() | 0xFF000000;

        byte threshold = 25;

        double range = ((double) 0xFF) / (3 * threshold);

        public final int filterRGB(int x, int y, int rgb) {
            Color probe = new Color(rgb);
            //log.info( "probe=" + probe + ", marker=" + markerColor );

            // delta values
            int dRed = markerColor.getRed() - probe.getRed();
            int dGreen = markerColor.getGreen() - probe.getGreen();
            int dBlue = markerColor.getBlue() - probe.getBlue();
            //log.info( "    dRed=" + dRed + ", dGreen=" + dGreen );

            if (dRed >= 0 && dRed < threshold && dGreen >= 0 && dGreen < threshold && dBlue >= 0
                    && dBlue < threshold) {
                int alpha = (int) Math.round(range * (dRed + dGreen + dBlue));
                //log.info( "    -> alpha=" + alpha );

                return ((alpha << 24) | 0x00FFFFFF) & rgb;
            } else {
                // nothing to do
                return rgb;
            }/*from w w  w.  ja  va  2 s.  co  m*/
        }
    };

    //        BufferedImage bimage = null;
    //        if (image instanceof BufferedImage) {
    //            bimage = (BufferedImage)image;
    //        }
    //        else {
    //            bimage = new BufferedImage(
    //                    image.getHeight( null ), image.getWidth( null ), BufferedImage.TYPE_INT_ARGB );
    //            Graphics g = bimage.getGraphics();
    //            g.drawImage( image, 0, 0, null );
    //            g.dispose();
    //        }

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
    Image result = Toolkit.getDefaultToolkit().createImage(ip);

    BufferedImage bresult = new BufferedImage(image.getHeight(null), image.getWidth(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics g = bresult.getGraphics();
    g.drawImage(result, 0, 0, null);
    g.dispose();

    //        // XXX this can surely be done any more clever
    //        int width = bimage.getWidth();
    //        int height = bimage.getHeight();
    //        for (int x=bimage.getMinX(); x<width; x++) {
    //            for (int y=bimage.getMinY(); y<height; y++) {
    //                int filtered = filter.filterRGB( x, y, bimage.getRGB( x, y ) );
    //                result.setRGB( x, y, filtered );
    //            }
    //        }

    log.debug("Transparency done. (" + (System.currentTimeMillis() - start) + "ms)");
    return bresult;
}