Java Code Examples for java.awt.image.RenderedImage#copyData()

The following examples show how to use java.awt.image.RenderedImage#copyData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FXGraphics2D.java    From openstock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Converts a rendered image to a {@code BufferedImage}.  This utility
 * method has come from a forum post by Jim Moore at:
 * <p>
 * <a href="http://www.jguru.com/faq/view.jsp?EID=114602">
 * http://www.jguru.com/faq/view.jsp?EID=114602</a>
 * 
 * @param img  the rendered image.
 * 
 * @return A buffered image. 
 */
private static BufferedImage convertRenderedImage(RenderedImage img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }
    ColorModel cm = img.getColorModel();
    int width = img.getWidth();
    int height = img.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable properties = new Hashtable();
    String[] keys = img.getPropertyNames();
    if (keys != null) {
        for (int i = 0; i < keys.length; i++) {
            properties.put(keys[i], img.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster, 
            isAlphaPremultiplied, properties);
    img.copyData(raster);
    return result;
}
 
Example 2
Source File: FXGraphics2D.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Converts a rendered image to a {@code BufferedImage}.  This utility
 * method has come from a forum post by Jim Moore at:
 * <p>
 * <a href="http://www.jguru.com/faq/view.jsp?EID=114602">
 * http://www.jguru.com/faq/view.jsp?EID=114602</a>
 * 
 * @param img  the rendered image.
 * 
 * @return A buffered image. 
 */
private static BufferedImage convertRenderedImage(RenderedImage img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }
    ColorModel cm = img.getColorModel();
    int width = img.getWidth();
    int height = img.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable properties = new Hashtable();
    String[] keys = img.getPropertyNames();
    if (keys != null) {
        for (int i = 0; i < keys.length; i++) {
            properties.put(keys[i], img.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster, 
            isAlphaPremultiplied, properties);
    img.copyData(raster);
    return result;
}
 
Example 3
Source File: FXGraphics2D.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a rendered image to a {@code BufferedImage}.  This utility
 * method has come from a forum post by Jim Moore at:
 * <p>
 * <a href="http://www.jguru.com/faq/view.jsp?EID=114602">
 * http://www.jguru.com/faq/view.jsp?EID=114602</a>
 * 
 * @param img  the rendered image.
 * 
 * @return A buffered image. 
 */
private static BufferedImage convertRenderedImage(RenderedImage img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }
    ColorModel cm = img.getColorModel();
    int width = img.getWidth();
    int height = img.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable properties = new Hashtable();
    String[] keys = img.getPropertyNames();
    if (keys != null) {
        for (int i = 0; i < keys.length; i++) {
            properties.put(keys[i], img.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster, 
            isAlphaPremultiplied, properties);
    img.copyData(raster);
    return result;
}
 
Example 4
Source File: PrintManager.java    From ganttproject with GNU General Public License v3.0 6 votes vote down vote up
public static void printChart(Chart chart, GanttExportSettings settings) {
  RenderedImage image = chart.getRenderedImage(settings);
  BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
  image.copyData(bufferedImage.getRaster());

  PrinterJob printJob = PrinterJob.getPrinterJob();

  printJob.setPrintable(new GanttPrintable(bufferedImage, GanttPrintable.REDUCE_FACTOR_DEFAULT));

  PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
  attr.add(MediaSizeName.ISO_A4);
  attr.add(OrientationRequested.LANDSCAPE);

  if (printJob.printDialog()) {
    try {
      printJob.print(attr);
    } catch (Exception e) {
      if (!GPLogger.log(e)) {
        e.printStackTrace(System.err);
      }
    }
  }
}
 
Example 5
Source File: FXGraphics2D.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts a rendered image to a {@code BufferedImage}.  This utility
 * method has come from a forum post by Jim Moore at:
 * <p>
 * <a href="http://www.jguru.com/faq/view.jsp?EID=114602">
 * http://www.jguru.com/faq/view.jsp?EID=114602</a>
 * 
 * @param img  the rendered image.
 * 
 * @return A buffered image. 
 */
private static BufferedImage convertRenderedImage(RenderedImage img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }
    ColorModel cm = img.getColorModel();
    int width = img.getWidth();
    int height = img.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable properties = new Hashtable();
    String[] keys = img.getPropertyNames();
    if (keys != null) {
        for (int i = 0; i < keys.length; i++) {
            properties.put(keys[i], img.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster, 
            isAlphaPremultiplied, properties);
    img.copyData(raster);
    return result;
}
 
Example 6
Source File: ImageUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public static BufferedImage rendereImage2BufferedImage( RenderedImage renderedImage ) {
    if (renderedImage instanceof BufferedImage) {
        return (BufferedImage) renderedImage;
    }
    ColorModel cm = renderedImage.getColorModel();
    int width = renderedImage.getWidth();
    int height = renderedImage.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable<String, Object> properties = new Hashtable<>();
    String[] keys = renderedImage.getPropertyNames();
    if (keys != null) {
        for( int i = 0; i < keys.length; i++ ) {
            properties.put(keys[i], renderedImage.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);
    renderedImage.copyData(raster);
    return result;
}
 
Example 7
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Converts a java.awt.image.RenderedImage into a
 * java.awt.image.BufferedImage. This code was adapted from
 * <a href="http://www.jguru.com/faq/view.jsp?EID=114602">a jGuru post</a>.
 */
public static BufferedImage convertRenderedImage(final RenderedImage img) {
	if (img instanceof BufferedImage) return (BufferedImage) img;
	final ColorModel cm = img.getColorModel();
	final int width = img.getWidth();
	final int height = img.getHeight();
	final WritableRaster raster = cm.createCompatibleWritableRaster(width,
		height);
	final boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
	final Hashtable<String, Object> properties = new Hashtable<>();
	final String[] keys = img.getPropertyNames();
	if (keys != null) {
		for (int i = 0; i < keys.length; i++) {
			properties.put(keys[i], img.getProperty(keys[i]));
		}
	}
	final BufferedImage result = new BufferedImage(cm, raster,
		isAlphaPremultiplied, properties);
	img.copyData(raster);
	return result;
}
 
Example 8
Source File: GraphicsUtils.java    From jfreesvg with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Converts a rendered image to a {@code BufferedImage}.  This utility
 * method has come from a forum post by Jim Moore at:
 * <p>
 * <a href="http://www.jguru.com/faq/view.jsp?EID=114602">
 * http://www.jguru.com/faq/view.jsp?EID=114602</a>
 * 
 * @param img  the rendered image ({@code null} not permitted).
 * 
 * @return A buffered image. 
 */
public static BufferedImage convertRenderedImage(RenderedImage img) {
    Args.nullNotPermitted(img, "img");
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;	
    }
    ColorModel cm = img.getColorModel();
    int width = img.getWidth();
    int height = img.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable properties = new Hashtable();
    String[] keys = img.getPropertyNames();
    if (keys != null) {
        for (int i = 0; i < keys.length; i++) {
            properties.put(keys[i], img.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster, 
            isAlphaPremultiplied, properties);
    img.copyData(raster);
    return result;
}
 
Example 9
Source File: FXGraphics2D.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Converts a rendered image to a {@code BufferedImage}.  This utility
 * method has come from a forum post by Jim Moore at:
 * <p>
 * <a href="http://www.jguru.com/faq/view.jsp?EID=114602">
 * http://www.jguru.com/faq/view.jsp?EID=114602</a>
 * 
 * @param img  the rendered image.
 * 
 * @return A buffered image. 
 */
private static BufferedImage convertRenderedImage(RenderedImage img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }
    ColorModel cm = img.getColorModel();
    int width = img.getWidth();
    int height = img.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable properties = new Hashtable();
    String[] keys = img.getPropertyNames();
    if (keys != null) {
        for (int i = 0; i < keys.length; i++) {
            properties.put(keys[i], img.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster, 
            isAlphaPremultiplied, properties);
    img.copyData(raster);
    return result;
}
 
Example 10
Source File: FXGraphics2D.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Converts a rendered image to a {@code BufferedImage}.  This utility
 * method has come from a forum post by Jim Moore at:
 * <p>
 * <a href="http://www.jguru.com/faq/view.jsp?EID=114602">
 * http://www.jguru.com/faq/view.jsp?EID=114602</a>
 * 
 * @param img  the rendered image.
 * 
 * @return A buffered image. 
 */
private static BufferedImage convertRenderedImage(RenderedImage img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }
    ColorModel cm = img.getColorModel();
    int width = img.getWidth();
    int height = img.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable properties = new Hashtable();
    String[] keys = img.getPropertyNames();
    if (keys != null) {
        for (int i = 0; i < keys.length; i++) {
            properties.put(keys[i], img.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster, 
            isAlphaPremultiplied, properties);
    img.copyData(raster);
    return result;
}
 
Example 11
Source File: ImageIOHelper.java    From tess4j with Apache License 2.0 5 votes vote down vote up
/**
 * Gets pixel data of an <code>RenderedImage</code> object.
 *
 * @param image an <code>RenderedImage</code> object
 * @return a byte buffer of pixel data
 */
public static ByteBuffer getImageByteBuffer(RenderedImage image) {
    ColorModel cm = image.getColorModel();
    WritableRaster wr = image.getData().createCompatibleWritableRaster(image.getWidth(), image.getHeight());
    image.copyData(wr);
    BufferedImage bi = new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
    return convertImageData(bi);
}
 
Example 12
Source File: EpsGraphics2D.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws a RenderedImage on the EPS document.
 */
public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
  Hashtable<String, Object> properties = new Hashtable<String, Object>();
  String[] names = img.getPropertyNames();
  for(int i = 0; i<names.length; i++) {
    properties.put(names[i], img.getProperty(names[i]));
  }
  ColorModel cm = img.getColorModel();
  WritableRaster wr = img.copyData(null);
  BufferedImage img1 = new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), properties);
  AffineTransform at = AffineTransform.getTranslateInstance(img.getMinX(), img.getMinY());
  at.preConcatenate(xform);
  drawImage(img1, at, null);
}