List of usage examples for java.awt.image BufferedImage copyData
public WritableRaster copyData(WritableRaster outRaster)
From source file:tilt.image.Blob.java
/** * Test with small image/*from w w w . j ava2 s . c o m*/ * @param args ignored */ public static void main(String[] args) { try { Options opts = new Options(new JSONObject()); String url = "http://ecdosis.net/test.png"; Double[][] cc = { { 0.0, 0.0 }, { 100.0, 0.0 }, { 100.0, 100.0 }, { 0.0, 100.0 } }; Picture p = new Picture(opts, url, new TextIndex("", ""), cc, InetAddress.getByName("127.0.0.1")); p.convertToTwoTone(); BufferedImage bandw = ImageIO.read(p.twotone); WritableRaster wr = bandw.getRaster(); Blob largest = null; int max = 0; int[] iArray = new int[1]; WritableRaster darkRegions = bandw.copyData(null); Blob.setToWhite(darkRegions); for (int y = 0; y < wr.getHeight(); y++) { for (int x = 0; x < wr.getWidth(); x++) { Blob b = new Blob(darkRegions, opts, null); wr.getPixel(x, y, iArray); if (iArray[0] == 0) { b.expandArea(wr, new Point(x, y)); if (b.size() > max) { System.out.println("Found new blob at " + x + "," + y + " size=" + b.size()); largest = b; max = b.size(); } } } } if (largest != null) { WritableRaster dirt = bandw.copyData(null); Blob.setToWhite(dirt); largest.save(dirt, wr, largest.firstBlackPixel); System.out.println(largest.toString()); } } catch (Exception e) { e.printStackTrace(System.out); } }
From source file:oct.util.Util.java
public static BufferedImage deepCopyBufferedImage(BufferedImage bi) { ColorModel cm = bi.getColorModel(); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); WritableRaster raster = bi.copyData(null); return new BufferedImage(cm, raster, isAlphaPremultiplied, null); }
From source file:pl.dp.bz.poid.fouriertest.FourierProc.java
public static BufferedImage copyImage(BufferedImage bi) { ColorModel cm = bi.getColorModel(); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); WritableRaster raster = bi.copyData(null); return new BufferedImage(cm, raster, isAlphaPremultiplied, null); }
From source file:app.springapp.ImageConverter.java
BufferedImage deepCopy(BufferedImage bi) { ColorModel cm = bi.getColorModel(); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); WritableRaster raster = bi.copyData(null); return new BufferedImage(cm, raster, isAlphaPremultiplied, null); }
From source file:tilt.image.Picture.java
/** * Convert from greyscale to twotone (black and white) * Adapted from OCRopus/*w ww .j a v a2 s .c o m*/ * Copyright 2006-2008 Deutsches Forschungszentrum fuer Kuenstliche * Intelligenz or its licensors, as applicable. * http://ocropus.googlecode.com/svn/trunk/ocr-binarize/ocr-binarize-sauvola.cc * @throws Exception */ void convertToTwoTone() throws ImageException { try { int MAXVAL = 256; double k = 0.34; if (greyscale == null) convertToGreyscale(); BufferedImage grey = ImageIO.read(greyscale); WritableRaster grey_image = grey.getRaster(); WritableRaster bin_image = grey.copyData(null); int square = (int) Math.floor(grey_image.getWidth() * 0.025); if (square == 0) square = Math.min(20, grey_image.getWidth()); if (square > grey_image.getHeight()) square = grey_image.getHeight(); int whalf = square >> 1; if (whalf == 0) throw new Exception("whalf==0!"); int image_width = grey_image.getWidth(); int image_height = grey_image.getHeight(); // Calculate the integral image, and integral of the squared image // original algorithm ate up too much memory, use floats for longs float[][] integral_image = new float[image_width][image_height]; float[][] rowsum_image = new float[image_width][image_height]; float[][] integral_sqimg = new float[image_width][image_height]; float[][] rowsum_sqimg = new float[image_width][image_height]; int xmin, ymin, xmax, ymax; double diagsum, idiagsum, diff, sqdiagsum, sqidiagsum, sqdiff, area; double mean, std, threshold; // for get/setPixel int[] iArray = new int[1]; int[] oArray = new int[1]; for (int j = 0; j < image_height; j++) { grey_image.getPixel(0, j, iArray); rowsum_image[0][j] = iArray[0]; rowsum_sqimg[0][j] = iArray[0] * iArray[0]; } for (int i = 1; i < image_width; i++) { for (int j = 0; j < image_height; j++) { grey_image.getPixel(i, j, iArray); rowsum_image[i][j] = rowsum_image[i - 1][j] + iArray[0]; rowsum_sqimg[i][j] = rowsum_sqimg[i - 1][j] + iArray[0] * iArray[0]; } } for (int i = 0; i < image_width; i++) { integral_image[i][0] = rowsum_image[i][0]; integral_sqimg[i][0] = rowsum_sqimg[i][0]; } for (int i = 0; i < image_width; i++) { for (int j = 1; j < image_height; j++) { integral_image[i][j] = integral_image[i][j - 1] + rowsum_image[i][j]; integral_sqimg[i][j] = integral_sqimg[i][j - 1] + rowsum_sqimg[i][j]; } } // compute mean and std.dev. using the integral image for (int i = 0; i < image_width; i++) { for (int j = 0; j < image_height; j++) { xmin = Math.max(0, i - whalf); ymin = Math.max(0, j - whalf); xmax = Math.min(image_width - 1, i + whalf); ymax = Math.min(image_height - 1, j + whalf); area = (xmax - xmin + 1) * (ymax - ymin + 1); grey_image.getPixel(i, j, iArray); // area can't be 0 here if (area == 0) throw new Exception("area can't be 0 here!"); if (xmin == 0 && ymin == 0) { // Point at origin diff = integral_image[xmax][ymax]; sqdiff = integral_sqimg[xmax][ymax]; } else if (xmin == 0 && ymin != 0) { // first column diff = integral_image[xmax][ymax] - integral_image[xmax][ymin - 1]; sqdiff = integral_sqimg[xmax][ymax] - integral_sqimg[xmax][ymin - 1]; } else if (xmin != 0 && ymin == 0) { // first row diff = integral_image[xmax][ymax] - integral_image[xmin - 1][ymax]; sqdiff = integral_sqimg[xmax][ymax] - integral_sqimg[xmin - 1][ymax]; } else { // rest of the image diagsum = integral_image[xmax][ymax] + integral_image[xmin - 1][ymin - 1]; idiagsum = integral_image[xmax][ymin - 1] + integral_image[xmin - 1][ymax]; diff = diagsum - idiagsum; sqdiagsum = integral_sqimg[xmax][ymax] + integral_sqimg[xmin - 1][ymin - 1]; sqidiagsum = integral_sqimg[xmax][ymin - 1] + integral_sqimg[xmin - 1][ymax]; sqdiff = sqdiagsum - sqidiagsum; } mean = diff / area; std = Math.sqrt((sqdiff - diff * diff / area) / (area - 1)); threshold = mean * (1 + k * ((std / 128) - 1)); if (iArray[0] < threshold) oArray[0] = 0; else oArray[0] = MAXVAL - 1; bin_image.setPixel(i, j, oArray); } } twotone = File.createTempFile(PictureRegistry.PREFIX, PictureRegistry.SUFFIX); grey.setData(bin_image); ImageIO.write(grey, "png", twotone); } catch (Exception e) { throw new ImageException(e); } }
From source file:net.pms.util.GenericIcons.java
/** * Add the format(container) name of the media to the generic icon image. * * @param image BufferdImage to be the label added * @param label the media container name to be added as a label * @param renderer the renderer configuration * * @return the generic icon with the container label added and scaled in accordance with renderer setting *///from w w w. j ava 2 s. co m private DLNAThumbnail addFormatLabelToImage(String label, ImageFormat imageFormat, IconType iconType) throws IOException { BufferedImage image; switch (iconType) { case AUDIO: image = genericAudioIcon; break; case IMAGE: image = genericImageIcon; break; case VIDEO: image = genericVideoIcon; break; default: image = genericUnknownIcon; } if (image != null) { // Make a copy ColorModel colorModel = image.getColorModel(); image = new BufferedImage(colorModel, image.copyData(null), colorModel.isAlphaPremultiplied(), null); } ByteArrayOutputStream out = null; if (label != null && image != null) { out = new ByteArrayOutputStream(); Graphics2D g = image.createGraphics(); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); try { int size = 40; Font font = new Font(Font.SANS_SERIF, Font.BOLD, size); FontMetrics metrics = g.getFontMetrics(font); while (size > 7 && metrics.stringWidth(label) > 135) { size--; font = new Font(Font.SANS_SERIF, Font.BOLD, size); metrics = g.getFontMetrics(font); } // Text center point 127x, 49y - calculate centering coordinates int x = 127 - metrics.stringWidth(label) / 2; int y = 46 + metrics.getAscent() / 2; g.drawImage(image, 0, 0, null); g.setColor(Color.WHITE); g.setFont(font); g.drawString(label, x, y); ImageIO.setUseCache(false); ImageIOTools.imageIOWrite(image, imageFormat.toString(), out); } finally { g.dispose(); } } return out != null ? DLNAThumbnail.toThumbnail(out.toByteArray(), 0, 0, ScaleType.MAX, imageFormat, false) : null; }
From source file:org.jtrfp.trcl.core.ResourceManager.java
private BufferedImage[] getSpecialRAWImage(String name, Color[] palette, int upscalePowerOfTwo) throws IllegalAccessException, FileLoadException, IOException { RAWFile dat = getRAW(name);/*from w ww. j a v a2 s . c o m*/ dat.setPalette(palette); BufferedImage[] segs = dat.asSegments(upscalePowerOfTwo); for (BufferedImage seg : segs) { Graphics g = seg.getGraphics(); BufferedImage scaled = new BufferedImage(seg.getColorModel(), seg.copyData(null), seg.isAlphaPremultiplied(), null); g.drawImage( scaled.getScaledInstance(seg.getWidth() - 2, seg.getHeight() - 2, Image.SCALE_AREA_AVERAGING), 1, 1, seg.getWidth() - 2, seg.getHeight() - 2, null); g.dispose(); } return segs; }
From source file:com.occamlab.te.parsers.ImageParser.java
private static void processBufferedImage(BufferedImage buffimage, String formatName, NodeList nodes) throws Exception { HashMap<Object, Object> bandMap = new HashMap<Object, Object>(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getLocalName().equals("subimage")) { Element e = (Element) node; int x = Integer.parseInt(e.getAttribute("x")); int y = Integer.parseInt(e.getAttribute("y")); int w = Integer.parseInt(e.getAttribute("width")); int h = Integer.parseInt(e.getAttribute("height")); processBufferedImage(buffimage.getSubimage(x, y, w, h), formatName, e.getChildNodes()); } else if (node.getLocalName().equals("checksum")) { CRC32 checksum = new CRC32(); Raster raster = buffimage.getRaster(); DataBufferByte buffer; if (node.getParentNode().getLocalName().equals("subimage")) { WritableRaster outRaster = raster.createCompatibleWritableRaster(); buffimage.copyData(outRaster); buffer = (DataBufferByte) outRaster.getDataBuffer(); } else { buffer = (DataBufferByte) raster.getDataBuffer(); }/*from w ww . ja v a 2 s. c o m*/ int numbanks = buffer.getNumBanks(); for (int j = 0; j < numbanks; j++) { checksum.update(buffer.getData(j)); } Document doc = node.getOwnerDocument(); node.appendChild(doc.createTextNode(Long.toString(checksum.getValue()))); } else if (node.getLocalName().equals("count")) { String band = ((Element) node).getAttribute("bands"); String sample = ((Element) node).getAttribute("sample"); if (sample.equals("all")) { bandMap.put(band, null); } else { HashMap<Object, Object> sampleMap = (HashMap<Object, Object>) bandMap.get(band); if (sampleMap == null) { if (!bandMap.containsKey(band)) { sampleMap = new HashMap<Object, Object>(); bandMap.put(band, sampleMap); } } sampleMap.put(Integer.decode(sample), new Integer(0)); } } else if (node.getLocalName().equals("transparentNodata")) { // 2011-08-24 // PwD String transparentNodata = checkTransparentNodata(buffimage, node); node.setTextContent(transparentNodata); } } } Iterator bandIt = bandMap.keySet().iterator(); while (bandIt.hasNext()) { String band_str = (String) bandIt.next(); int band_indexes[]; if (buffimage.getType() == BufferedImage.TYPE_BYTE_BINARY || buffimage.getType() == BufferedImage.TYPE_BYTE_GRAY) { band_indexes = new int[1]; band_indexes[0] = 0; } else { band_indexes = new int[band_str.length()]; for (int i = 0; i < band_str.length(); i++) { if (band_str.charAt(i) == 'A') band_indexes[i] = 3; if (band_str.charAt(i) == 'B') band_indexes[i] = 2; if (band_str.charAt(i) == 'G') band_indexes[i] = 1; if (band_str.charAt(i) == 'R') band_indexes[i] = 0; } } Raster raster = buffimage.getRaster(); java.util.HashMap sampleMap = (java.util.HashMap) bandMap.get(band_str); boolean addall = (sampleMap == null); if (sampleMap == null) { sampleMap = new java.util.HashMap(); bandMap.put(band_str, sampleMap); } int minx = raster.getMinX(); int maxx = minx + raster.getWidth(); int miny = raster.getMinY(); int maxy = miny + raster.getHeight(); int bands[][] = new int[band_indexes.length][raster.getWidth()]; for (int y = miny; y < maxy; y++) { for (int i = 0; i < band_indexes.length; i++) { raster.getSamples(minx, y, maxx, 1, band_indexes[i], bands[i]); } for (int x = minx; x < maxx; x++) { int sample = 0; for (int i = 0; i < band_indexes.length; i++) { sample |= bands[i][x] << ((band_indexes.length - i - 1) * 8); } Integer sampleObj = new Integer(sample); boolean add = addall; if (!addall) { add = sampleMap.containsKey(sampleObj); } if (add) { Integer count = (Integer) sampleMap.get(sampleObj); if (count == null) { count = new Integer(0); } count = new Integer(count.intValue() + 1); sampleMap.put(sampleObj, count); } } } } Node node = nodes.item(0); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getLocalName().equals("count")) { String band = ((Element) node).getAttribute("bands"); String sample = ((Element) node).getAttribute("sample"); HashMap sampleMap = (HashMap) bandMap.get(band); Document doc = node.getOwnerDocument(); if (sample.equals("all")) { Node parent = node.getParentNode(); Node prevSibling = node.getPreviousSibling(); Iterator sampleIt = sampleMap.keySet().iterator(); Element countnode = null; int digits; String prefix; switch (buffimage.getType()) { case BufferedImage.TYPE_BYTE_BINARY: digits = 1; prefix = ""; break; case BufferedImage.TYPE_BYTE_GRAY: digits = 2; prefix = "0x"; break; default: prefix = "0x"; digits = band.length() * 2; } while (sampleIt.hasNext()) { countnode = doc.createElementNS(node.getNamespaceURI(), "count"); Integer sampleInt = (Integer) sampleIt.next(); Integer count = (Integer) sampleMap.get(sampleInt); if (band.length() > 0) { countnode.setAttribute("bands", band); } countnode.setAttribute("sample", prefix + HexString(sampleInt.intValue(), digits)); Node textnode = doc.createTextNode(count.toString()); countnode.appendChild(textnode); parent.insertBefore(countnode, node); if (sampleIt.hasNext()) { if (prevSibling != null && prevSibling.getNodeType() == Node.TEXT_NODE) { parent.insertBefore(prevSibling.cloneNode(false), node); } } } parent.removeChild(node); node = countnode; } else { Integer count = (Integer) sampleMap.get(Integer.decode(sample)); if (count == null) count = new Integer(0); Node textnode = doc.createTextNode(count.toString()); node.appendChild(textnode); } } } node = node.getNextSibling(); } }