List of usage examples for java.awt.image RenderedImage getWidth
int getWidth();
From source file:fr.gael.dhus.datastore.processing.impl.ProcessProductImages.java
@Override public void run(final Product product) { if (ImageIO.getUseCache()) ImageIO.setUseCache(false); DrbNode node = null;//from w w w .j a v a 2 s . co m URL url = product.getPath(); // Prepare the DRb node to be processed try { // First : force loading the model before accessing items. @SuppressWarnings("unused") DrbCortexModel model = DrbCortexModel.getDefaultModel(); node = ProcessingUtils.getNodeFromPath(url.getPath()); if (node == null) { throw new IOException("Cannot Instantiate Drb with URI \"" + url.toExternalForm() + "\"."); } } catch (Exception e) { logger.error("Exception raised while processing Quicklook", e); return; } if (!ImageFactory.isImage(node)) { logger.debug("No Image."); return; } RenderedImageList input_list = null; RenderedImage input_image = null; try { input_list = ImageFactory.createImage(node); input_image = RenderingFactory.createDefaultRendering(input_list); } catch (Exception e) { logger.debug("Cannot retrieve default rendering"); if (logger.isDebugEnabled()) { logger.debug("Error occurs during rendered image reader", e); } if (input_list == null) return; input_image = input_list; } int quicklook_width = cfgManager.getProductConfiguration().getQuicklookConfiguration().getWidth(); int quicklook_height = cfgManager.getProductConfiguration().getQuicklookConfiguration().getHeight(); boolean quicklook_cutting = cfgManager.getProductConfiguration().getQuicklookConfiguration().isCutting(); logger.info("Generating Quicklook " + quicklook_width + "x" + quicklook_height + " from " + input_image.getWidth() + "x" + input_image.getHeight()); RenderedImage image = ProcessingUtils.ResizeImage(input_image, quicklook_width, quicklook_height, 10f, quicklook_cutting); String product_id = product.getIdentifier(); if (product_id == null) product_id = "unknown"; // Manages the quicklook output File image_directory = incomingManager.getNewIncomingPath(); LockFactory lf = new NativeFSLockFactory(image_directory); Lock lock = lf.makeLock(".lock-writing"); try { lock.obtain(900000); } catch (Exception e) { logger.warn("Cannot lock incoming directory - continuing without (" + e.getMessage() + ")"); } File file = new File(image_directory, product_id + "-ql.jpg"); try { ImageIO.write(image, "jpg", file); product.setQuicklookPath(file.getPath()); product.setQuicklookSize(file.length()); } catch (IOException e) { logger.error("Cannot save quicklook.", e); } // Thumbnail int thumbnail_width = cfgManager.getProductConfiguration().getThumbnailConfiguration().getWidth(); int thumbnail_height = cfgManager.getProductConfiguration().getThumbnailConfiguration().getHeight(); boolean thumbnail_cutting = cfgManager.getProductConfiguration().getThumbnailConfiguration().isCutting(); logger.info("Generating Thumbnail " + thumbnail_width + "x" + thumbnail_height + " from " + input_image.getWidth() + "x" + input_image.getHeight() + " image."); image = ProcessingUtils.ResizeImage(input_image, thumbnail_width, thumbnail_height, 10f, thumbnail_cutting); // Manages the quicklook output file = new File(image_directory, product_id + "-th.jpg"); try { ImageIO.write(image, "jpg", file); product.setThumbnailPath(file.getPath()); product.setThumbnailSize(file.length()); } catch (IOException e) { logger.error("Cannot save thumbnail.", e); } SdiImageFactory.close(input_list); try { lock.close(); } catch (IOException e) { } }
From source file:fr.gael.dhus.datastore.processing.ProcessingManager.java
/** * Loads product images from Drb node and stores information inside the * product before returning it/*from w w w. j ava2 s . co m*/ */ private Product extractImages(DrbNode productNode, Product product) { if (ImageIO.getUseCache()) ImageIO.setUseCache(false); if (!ImageFactory.isImage(productNode)) { LOGGER.debug("No Image."); return product; } RenderedImageList input_list = null; RenderedImage input_image = null; try { input_list = ImageFactory.createImage(productNode); input_image = RenderingFactory.createDefaultRendering(input_list); } catch (Exception e) { LOGGER.debug("Cannot retrieve default rendering"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Error occurs during rendered image reader", e); } if (input_list == null) { return product; } input_image = input_list; } if (input_image == null) { return product; } // Generate Quicklook int quicklook_width = cfgManager.getProductConfiguration().getQuicklookConfiguration().getWidth(); int quicklook_height = cfgManager.getProductConfiguration().getQuicklookConfiguration().getHeight(); boolean quicklook_cutting = cfgManager.getProductConfiguration().getQuicklookConfiguration().isCutting(); LOGGER.info("Generating Quicklook " + quicklook_width + "x" + quicklook_height + " from " + input_image.getWidth() + "x" + input_image.getHeight()); RenderedImage image = ProcessingUtils.resizeImage(input_image, quicklook_width, quicklook_height, 10f, quicklook_cutting); // Manages the quicklook output File image_directory = incomingManager.getNewIncomingPath(); AsyncFileLock afl = null; try { Path path = Paths.get(image_directory.getAbsolutePath(), ".lock-writing"); afl = new AsyncFileLock(path); afl.obtain(900000); } catch (IOException | InterruptedException | TimeoutException e) { LOGGER.warn("Cannot lock incoming directory - continuing without (" + e.getMessage() + ")"); } String identifier = product.getIdentifier(); File file = new File(image_directory, identifier + "-ql.jpg"); try { if (ImageIO.write(image, "jpg", file)) { product.setQuicklookPath(file.getPath()); product.setQuicklookSize(file.length()); } } catch (IOException e) { LOGGER.error("Cannot save quicklook.", e); } // Generate Thumbnail int thumbnail_width = cfgManager.getProductConfiguration().getThumbnailConfiguration().getWidth(); int thumbnail_height = cfgManager.getProductConfiguration().getThumbnailConfiguration().getHeight(); boolean thumbnail_cutting = cfgManager.getProductConfiguration().getThumbnailConfiguration().isCutting(); LOGGER.info("Generating Thumbnail " + thumbnail_width + "x" + thumbnail_height + " from " + input_image.getWidth() + "x" + input_image.getHeight() + " image."); image = ProcessingUtils.resizeImage(input_image, thumbnail_width, thumbnail_height, 10f, thumbnail_cutting); // Manages the thumbnail output file = new File(image_directory, identifier + "-th.jpg"); try { if (ImageIO.write(image, "jpg", file)) { product.setThumbnailPath(file.getPath()); product.setThumbnailSize(file.length()); } } catch (IOException e) { LOGGER.error("Cannot save thumbnail.", e); } SdiImageFactory.close(input_list); if (afl != null) { afl.close(); } return product; }
From source file:org.apache.batik.transcoder.image.AbstractImageTranscoderTest.java
protected BufferedImage getImage(InputStream is) throws IOException { ImageTagRegistry reg = ImageTagRegistry.getRegistry(); Filter filt = reg.readStream(is); if (filt == null) throw new IOException("Couldn't read Stream"); RenderedImage red = filt.createDefaultRendering(); if (red == null) throw new IOException("Couldn't render Stream"); BufferedImage img = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB); red.copyData(img.getRaster());/*from www . j a v a2s. com*/ return img; }
From source file:org.apache.fop.afp.AFPGraphics2D.java
/** {@inheritDoc} */ @Override//from w w w . j a v a2 s . c om public void drawRenderedImage(RenderedImage img, AffineTransform xform) { int imgWidth = img.getWidth(); int imgHeight = img.getHeight(); AffineTransform gat = gc.getTransform(); int graphicsObjectHeight = graphicsObj.getObjectEnvironmentGroup().getObjectAreaDescriptor().getHeight(); double toMillipointFactor = UnitConv.IN2PT * 1000 / (double) paintingState.getResolution(); double x = gat.getTranslateX(); double y = -(gat.getTranslateY() - graphicsObjectHeight); x = toMillipointFactor * x; y = toMillipointFactor * y; double w = toMillipointFactor * imgWidth * gat.getScaleX(); double h = toMillipointFactor * imgHeight * -gat.getScaleY(); AFPImageHandlerRenderedImage handler = new AFPImageHandlerRenderedImage(); ImageInfo imageInfo = new ImageInfo(null, null); imageInfo.setSize(new ImageSize(img.getWidth(), img.getHeight(), paintingState.getResolution())); imageInfo.getSize().calcSizeFromPixels(); ImageRendered red = new ImageRendered(imageInfo, img, null); Rectangle targetPos = new Rectangle((int) Math.round(x), (int) Math.round(y), (int) Math.round(w), (int) Math.round(h)); AFPRenderingContext context = new AFPRenderingContext(null, resourceManager, paintingState, fontInfo, null); try { handler.handleImage(context, red, targetPos); } catch (IOException ioe) { handleIOException(ioe); } }
From source file:org.apache.fop.render.pcl.PCLGenerator.java
private RenderedImage getMask(RenderedImage img, Dimension targetDim) { ColorModel cm = img.getColorModel(); if (cm.hasAlpha()) { BufferedImage alpha = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY); Raster raster = img.getData(); GraphicsUtil.copyBand(raster, cm.getNumColorComponents(), alpha.getRaster(), 0); BufferedImageOp op1 = new LookupOp(new ByteLookupTable(0, THRESHOLD_TABLE), null); BufferedImage alphat = op1.filter(alpha, null); BufferedImage mask;//from w w w. jav a 2s.c o m if (true) { mask = new BufferedImage(targetDim.width, targetDim.height, BufferedImage.TYPE_BYTE_BINARY); } else { byte[] arr = { (byte) 0, (byte) 0xff }; ColorModel colorModel = new IndexColorModel(1, 2, arr, arr, arr); WritableRaster wraster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, targetDim.width, targetDim.height, 1, 1, null); mask = new BufferedImage(colorModel, wraster, false, null); } Graphics2D g2d = mask.createGraphics(); try { AffineTransform at = new AffineTransform(); double sx = targetDim.getWidth() / img.getWidth(); double sy = targetDim.getHeight() / img.getHeight(); at.scale(sx, sy); g2d.drawRenderedImage(alphat, at); } finally { g2d.dispose(); } /* try { BatchDiffer.saveAsPNG(alpha, new java.io.File("D:/out-alpha.png")); BatchDiffer.saveAsPNG(mask, new java.io.File("D:/out-mask.png")); } catch (IOException e) { e.printStackTrace(); }*/ return mask; } else { return null; } }
From source file:org.apache.fop.render.pcl.PCLGenerator.java
/** * Paint a bitmap at the current cursor position. The bitmap is converted to a monochrome * (1-bit) bitmap image./*from ww w .j av a 2s .c om*/ * @param img the bitmap image * @param targetDim the target Dimention (in mpt) * @param sourceTransparency true if the background should not be erased * @throws IOException In case of an I/O error */ public void paintBitmap(RenderedImage img, Dimension targetDim, boolean sourceTransparency) throws IOException { double targetHResolution = img.getWidth() / UnitConv.mpt2in(targetDim.width); double targetVResolution = img.getHeight() / UnitConv.mpt2in(targetDim.height); double targetResolution = Math.max(targetHResolution, targetVResolution); int resolution = (int) Math.round(targetResolution); int effResolution = calculatePCLResolution(resolution, true); Dimension orgDim = new Dimension(img.getWidth(), img.getHeight()); Dimension effDim; if (targetResolution == effResolution) { effDim = orgDim; //avoid scaling side-effects } else { effDim = new Dimension((int) Math.ceil(UnitConv.mpt2px(targetDim.width, effResolution)), (int) Math.ceil(UnitConv.mpt2px(targetDim.height, effResolution))); } boolean scaled = !orgDim.equals(effDim); //ImageWriterUtil.saveAsPNG(img, new java.io.File("D:/text-0-org.png")); boolean monochrome = isMonochromeImage(img); if (!monochrome) { //Transparency mask disabled. Doesn't work reliably final boolean transparencyDisabled = true; RenderedImage mask = (transparencyDisabled ? null : getMask(img, effDim)); if (mask != null) { pushCursorPos(); selectCurrentPattern(0, 1); //Solid white setTransparencyMode(true, true); paintMonochromeBitmap(mask, effResolution); popCursorPos(); } RenderedImage red = BitmapImageUtil.convertToMonochrome(img, effDim, this.ditheringQuality); selectCurrentPattern(0, 0); //Solid black setTransparencyMode(sourceTransparency || mask != null, true); paintMonochromeBitmap(red, effResolution); } else { RenderedImage effImg = img; if (scaled) { effImg = BitmapImageUtil.convertToMonochrome(img, effDim); } setSourceTransparencyMode(sourceTransparency); selectCurrentPattern(0, 0); //Solid black paintMonochromeBitmap(effImg, effResolution); } }
From source file:org.apache.fop.render.pcl.PCLGenerator.java
/** * Paint a bitmap at the current cursor position. The bitmap must be a monochrome * (1-bit) bitmap image.//from w w w . j ava 2s . co m * @param img the bitmap image (must be 1-bit b/w) * @param resolution the resolution of the image (must be a PCL resolution) * @throws IOException In case of an I/O error */ public void paintMonochromeBitmap(RenderedImage img, int resolution) throws IOException { if (!isValidPCLResolution(resolution)) { throw new IllegalArgumentException("Invalid PCL resolution: " + resolution); } boolean monochrome = isMonochromeImage(img); if (!monochrome) { throw new IllegalArgumentException("img must be a monochrome image"); } setRasterGraphicsResolution(resolution); writeCommand("*r0f" + img.getHeight() + "t" + img.getWidth() + "s1A"); Raster raster = img.getData(); Encoder encoder = new Encoder(img); // Transfer graphics data int imgw = img.getWidth(); IndexColorModel cm = (IndexColorModel) img.getColorModel(); if (cm.getTransferType() == DataBuffer.TYPE_BYTE) { DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer(); MultiPixelPackedSampleModel packedSampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, img.getWidth(), img.getHeight(), 1); if (img.getSampleModel().equals(packedSampleModel) && dataBuffer.getNumBanks() == 1) { //Optimized packed encoding byte[] buf = dataBuffer.getData(); int scanlineStride = packedSampleModel.getScanlineStride(); int idx = 0; int c0 = toGray(cm.getRGB(0)); int c1 = toGray(cm.getRGB(1)); boolean zeroIsWhite = c0 > c1; for (int y = 0, maxy = img.getHeight(); y < maxy; y++) { for (int x = 0, maxx = scanlineStride; x < maxx; x++) { if (zeroIsWhite) { encoder.add8Bits(buf[idx]); } else { encoder.add8Bits((byte) ~buf[idx]); } idx++; } encoder.endLine(); } } else { //Optimized non-packed encoding for (int y = 0, maxy = img.getHeight(); y < maxy; y++) { byte[] line = (byte[]) raster.getDataElements(0, y, imgw, 1, null); for (int x = 0, maxx = imgw; x < maxx; x++) { encoder.addBit(line[x] == 0); } encoder.endLine(); } } } else { //Safe but slow fallback for (int y = 0, maxy = img.getHeight(); y < maxy; y++) { for (int x = 0, maxx = imgw; x < maxx; x++) { int sample = raster.getSample(x, y, 0); encoder.addBit(sample == 0); } encoder.endLine(); } } // End raster graphics writeCommand("*rB"); }
From source file:org.apache.fop.render.pdf.ImageRenderedAdapter.java
/** {@inheritDoc} */ @Override/* w w w. j a v a 2s. c om*/ public int getWidth() { RenderedImage ri = getImage().getRenderedImage(); return ri.getWidth(); }
From source file:org.apache.fop.util.BitmapImageUtilTestCase.java
/** * Tests the convertTo* methods.//from w ww . j a v a2s. c om * @throws Exception if an error occurs */ @Test public void testConvertToMono() throws Exception { BufferedImage testImage = createTestImage(); saveAsPNG(testImage, "test-image"); RenderedImage img; Dimension scaled = new Dimension(320, 240); img = BitmapImageUtil.convertToGrayscale(testImage, null); saveAsPNG(img, "out-gray"); assertEquals(1, img.getColorModel().getNumComponents()); assertEquals(8, img.getColorModel().getPixelSize()); assertEquals(640, img.getWidth()); assertEquals(480, img.getHeight()); assertPixels("5757575757575757575757FFFFFFFFFF", img, 220, 34, 16); img = BitmapImageUtil.convertToGrayscale(testImage, scaled); saveAsPNG(img, "out-gray-scaled"); assertEquals(1, img.getColorModel().getNumComponents()); assertEquals(8, img.getColorModel().getPixelSize()); assertEquals(320, img.getWidth()); assertEquals(240, img.getHeight()); img = BitmapImageUtil.convertToMonochrome(testImage, null); saveAsPNG(img, "out-mono"); assertEquals(1, img.getColorModel().getPixelSize()); assertEquals(640, img.getWidth()); assertEquals(480, img.getHeight()); assertPixels("00000000000000000000000101010101", img, 220, 34, 16); if (isJAIAvailable()) { img = BitmapImageUtil.convertToMonochrome(testImage, null, 0.5f); saveAsPNG(img, "out-mono-jai-0.5"); assertEquals(1, img.getColorModel().getPixelSize()); assertEquals(640, img.getWidth()); assertEquals(480, img.getHeight()); assertPixels("00010000000100000001000101010101", img, 220, 34, 16); img = BitmapImageUtil.convertToMonochrome(testImage, null, 1.0f); saveAsPNG(img, "out-mono-jai-1.0"); assertEquals(1, img.getColorModel().getPixelSize()); assertEquals(640, img.getWidth()); assertEquals(480, img.getHeight()); assertPixels("01000001000001000001000101010101", img, 220, 34, 16); } }
From source file:org.apache.fop.visual.BitmapComparator.java
/** * Loads an image from a URL// w ww.j a v a 2s. c om * @param url the URL to the image * @return the bitmap as BufferedImage * TODO This method doesn't close the InputStream opened by the URL. */ public static BufferedImage getImage(URL url) { ImageTagRegistry reg = ImageTagRegistry.getRegistry(); Filter filt = reg.readURL(new ParsedURL(url)); if (filt == null) { return null; } RenderedImage red = filt.createDefaultRendering(); if (red == null) { return null; } BufferedImage img = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB); red.copyData(img.getRaster()); return img; }