List of usage examples for java.awt.image BufferedImage isAlphaPremultiplied
public boolean isAlphaPremultiplied()
From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java
public static BufferedImage makeTransparent(BufferedImage image, int x, int y) { ColorModel cm = image.getColorModel(); if (!(cm instanceof IndexColorModel)) return image; // sorry... IndexColorModel icm = (IndexColorModel) cm; WritableRaster raster = image.getRaster(); int pixel = raster.getSample(x, y, 0); // pixel is offset in ICM's // palette// www . j av a 2 s . co m int size = icm.getMapSize(); byte[] reds = new byte[size]; byte[] greens = new byte[size]; byte[] blues = new byte[size]; icm.getReds(reds); icm.getGreens(greens); icm.getBlues(blues); IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel); return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null); }
From source file:com.boundlessgeo.geoserver.api.controllers.ThumbnailController.java
/** * Utility method for scaling thumbnails. Scales byte[] image by a scale factor. * Optionally crops images to square./*from w w w .j a v a2 s. co m*/ * @param src RenderedImage containing the input image * @param scale Scale amount * @param square Boolean flag to crop to a square image * @return RenderedImage containing the transformed image * @throws IOException */ public static BufferedImage scaleImage(BufferedImage image, double scale, boolean square) throws IOException { int sx = 0, sy = 0; int swidth = image.getWidth(); int sheight = image.getHeight(); if (square) { if (image.getHeight() > image.getWidth()) { sy = (int) ((image.getHeight() - image.getWidth()) / 2.0); sheight = swidth; } else if (image.getHeight() < image.getWidth()) { sx = (int) ((image.getWidth() - image.getHeight()) / 2.0); swidth = sheight; } } int width = (int) (swidth * scale); int height = (int) (sheight * scale); BufferedImage scaled = new BufferedImage(image.getColorModel(), image.getRaster().createCompatibleWritableRaster(width, height), image.isAlphaPremultiplied(), null); Graphics g = scaled.getGraphics(); g.drawImage(image, 0, 0, width, height, sx, sy, sx + swidth, sy + sheight, null); g.dispose(); return scaled; }
From source file:Main.java
/** * Snapshots the specified {@link BufferedImage} and stores a copy of * its pixels into a JavaFX {@link Image} object, creating a new * object if needed./* w w w .ja va 2 s .c om*/ * The returned {@code Image} will be a static snapshot of the state * of the pixels in the {@code BufferedImage} at the time the method * completes. Further changes to the {@code BufferedImage} will not * be reflected in the {@code Image}. * <p> * The optional JavaFX {@link WritableImage} parameter may be reused * to store the copy of the pixels. * A new {@code Image} will be created if the supplied object is null, * is too small or of a type which the image pixels cannot be easily * converted into. * * @param bimg the {@code BufferedImage} object to be converted * @param wimg an optional {@code WritableImage} object that can be * used to store the returned pixel data * @return an {@code Image} object representing a snapshot of the * current pixels in the {@code BufferedImage}. * @since JavaFX 2.2 */ public static WritableImage toFXImage(BufferedImage bimg, WritableImage wimg) { int bw = bimg.getWidth(); int bh = bimg.getHeight(); switch (bimg.getType()) { case BufferedImage.TYPE_INT_ARGB: case BufferedImage.TYPE_INT_ARGB_PRE: break; default: BufferedImage converted = new BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2d = converted.createGraphics(); g2d.drawImage(bimg, 0, 0, null); g2d.dispose(); bimg = converted; break; } // assert(bimg.getType == TYPE_INT_ARGB[_PRE]); if (wimg != null) { int iw = (int) wimg.getWidth(); int ih = (int) wimg.getHeight(); if (iw < bw || ih < bh) { wimg = null; } else if (bw < iw || bh < ih) { int empty[] = new int[iw]; PixelWriter pw = wimg.getPixelWriter(); PixelFormat<IntBuffer> pf = PixelFormat.getIntArgbPreInstance(); if (bw < iw) { pw.setPixels(bw, 0, iw - bw, bh, pf, empty, 0, 0); } if (bh < ih) { pw.setPixels(0, bh, iw, ih - bh, pf, empty, 0, 0); } } } if (wimg == null) { wimg = new WritableImage(bw, bh); } PixelWriter pw = wimg.getPixelWriter(); IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster(); int data[] = icr.getDataStorage(); int offset = icr.getDataOffset(0); int scan = icr.getScanlineStride(); PixelFormat<IntBuffer> pf = (bimg.isAlphaPremultiplied() ? PixelFormat.getIntArgbPreInstance() : PixelFormat.getIntArgbInstance()); pw.setPixels(0, 0, bw, bh, pf, data, offset, scan); return wimg; }
From source file:org.csa.rstb.dat.toolviews.HaAlphaPlotPanel.java
private void toggleColor() { BufferedImage image = plot.getImage(); if (image != null) { if (!plotColorsInverted) { image = new BufferedImage(untoggledColorModel, image.getRaster(), image.isAlphaPremultiplied(), null);/*ww w . ja v a 2s .c o m*/ } else { image = new BufferedImage(toggledColorModel, image.getRaster(), image.isAlphaPremultiplied(), null); } plot.setImage(image); densityPlotDisplay.getChart().setNotify(true); plotColorsInverted = !plotColorsInverted; } }
From source file:GraphicsUtil.java
/** * Copies data from one bufferedImage to another paying attention * to the state of AlphaPreMultiplied./*ww w.ja v a 2s . c om*/ * * @param src The source * @param srcRect The Rectangle of source data to be copied * @param dst The destination * @param destP The Place for the upper left corner of srcRect in dst. */ public static void copyData(BufferedImage src, Rectangle srcRect, BufferedImage dst, Point destP) { /* if (srcCS != dstCS) throw new IllegalArgumentException ("Images must be in the same ColorSpace in order "+ "to copy Data between them"); */ boolean srcAlpha = src.getColorModel().hasAlpha(); boolean dstAlpha = dst.getColorModel().hasAlpha(); // System.out.println("Src has: " + srcAlpha + // " is: " + src.isAlphaPremultiplied()); // // System.out.println("Dst has: " + dstAlpha + // " is: " + dst.isAlphaPremultiplied()); if (srcAlpha == dstAlpha) if (!srcAlpha || src.isAlphaPremultiplied() == dst.isAlphaPremultiplied()) { // They match one another so just copy everything... copyData(src.getRaster(), dst.getRaster()); return; } // System.out.println("Using Slow CopyData"); int[] pixel = null; Raster srcR = src.getRaster(); WritableRaster dstR = dst.getRaster(); int bands = dstR.getNumBands(); int dx = destP.x - srcRect.x; int dy = destP.y - srcRect.y; int w = srcRect.width; int x0 = srcRect.x; int y0 = srcRect.y; int y1 = y0 + srcRect.height - 1; if (!srcAlpha) { // Src has no alpha dest does so set alpha to 1.0 everywhere. // System.out.println("Add Alpha"); int[] oPix = new int[bands * w]; int out = (w * bands) - 1; // The 2 skips alpha channel while (out >= 0) { // Fill alpha channel with 255's oPix[out] = 255; out -= bands; } int b, in; for (int y = y0; y <= y1; y++) { pixel = srcR.getPixels(x0, y, w, 1, pixel); in = w * (bands - 1) - 1; out = (w * bands) - 2; // The 2 skips alpha channel on last pix switch (bands) { case 4: while (in >= 0) { oPix[out--] = pixel[in--]; oPix[out--] = pixel[in--]; oPix[out--] = pixel[in--]; out--; } break; default: while (in >= 0) { for (b = 0; b < bands - 1; b++) oPix[out--] = pixel[in--]; out--; } } dstR.setPixels(x0 + dx, y + dy, w, 1, oPix); } } else if (dstAlpha && dst.isAlphaPremultiplied()) { // Src and dest have Alpha but we need to multiply it for dst. // System.out.println("Mult Case"); int a, b, alpha, in, fpNorm = (1 << 24) / 255, pt5 = 1 << 23; for (int y = y0; y <= y1; y++) { pixel = srcR.getPixels(x0, y, w, 1, pixel); in = bands * w - 1; switch (bands) { case 4: while (in >= 0) { a = pixel[in]; if (a == 255) in -= 4; else { in--; alpha = fpNorm * a; pixel[in] = (pixel[in] * alpha + pt5) >>> 24; in--; pixel[in] = (pixel[in] * alpha + pt5) >>> 24; in--; pixel[in] = (pixel[in] * alpha + pt5) >>> 24; in--; } } break; default: while (in >= 0) { a = pixel[in]; if (a == 255) in -= bands; else { in--; alpha = fpNorm * a; for (b = 0; b < bands - 1; b++) { pixel[in] = (pixel[in] * alpha + pt5) >>> 24; in--; } } } } dstR.setPixels(x0 + dx, y + dy, w, 1, pixel); } } else if (dstAlpha && !dst.isAlphaPremultiplied()) { // Src and dest have Alpha but we need to divide it out for dst. // System.out.println("Div Case"); int a, b, ialpha, in, fpNorm = 0x00FF0000, pt5 = 1 << 15; for (int y = y0; y <= y1; y++) { pixel = srcR.getPixels(x0, y, w, 1, pixel); in = (bands * w) - 1; switch (bands) { case 4: while (in >= 0) { a = pixel[in]; if ((a <= 0) || (a >= 255)) in -= 4; else { in--; ialpha = fpNorm / a; pixel[in] = (pixel[in] * ialpha + pt5) >>> 16; in--; pixel[in] = (pixel[in] * ialpha + pt5) >>> 16; in--; pixel[in] = (pixel[in] * ialpha + pt5) >>> 16; in--; } } break; default: while (in >= 0) { a = pixel[in]; if ((a <= 0) || (a >= 255)) in -= bands; else { in--; ialpha = fpNorm / a; for (b = 0; b < bands - 1; b++) { pixel[in] = (pixel[in] * ialpha + pt5) >>> 16; in--; } } } } dstR.setPixels(x0 + dx, y + dy, w, 1, pixel); } } else if (src.isAlphaPremultiplied()) { int[] oPix = new int[bands * w]; // Src has alpha dest does not so unpremult and store... // System.out.println("Remove Alpha, Div Case"); int a, b, ialpha, in, out, fpNorm = 0x00FF0000, pt5 = 1 << 15; for (int y = y0; y <= y1; y++) { pixel = srcR.getPixels(x0, y, w, 1, pixel); in = (bands + 1) * w - 1; out = (bands * w) - 1; while (in >= 0) { a = pixel[in]; in--; if (a > 0) { if (a < 255) { ialpha = fpNorm / a; for (b = 0; b < bands; b++) oPix[out--] = (pixel[in--] * ialpha + pt5) >>> 16; } else for (b = 0; b < bands; b++) oPix[out--] = pixel[in--]; } else { in -= bands; for (b = 0; b < bands; b++) oPix[out--] = 255; } } dstR.setPixels(x0 + dx, y + dy, w, 1, oPix); } } else { // Src has unpremult alpha, dest does not have alpha, // just copy the color channels over. Rectangle dstRect = new Rectangle(destP.x, destP.y, srcRect.width, srcRect.height); for (int b = 0; b < bands; b++) copyBand(srcR, srcRect, b, dstR, dstRect, b); } }
From source file:net.d53dev.dslfy.web.service.GifService.java
public BufferedImage convertRGBAToGIF(BufferedImage src, int transColor) { BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); Graphics g = dst.getGraphics(); g.setColor(new Color(transColor)); g.fillRect(0, 0, dst.getWidth(), dst.getHeight()); {/*from w ww .ja v a 2s. co m*/ IndexColorModel indexedModel = (IndexColorModel) dst.getColorModel(); WritableRaster raster = dst.getRaster(); int sample = raster.getSample(0, 0, 0); int size = indexedModel.getMapSize(); byte[] rr = new byte[size]; byte[] gg = new byte[size]; byte[] bb = new byte[size]; indexedModel.getReds(rr); indexedModel.getGreens(gg); indexedModel.getBlues(bb); IndexColorModel newModel = new IndexColorModel(8, size, rr, gg, bb, sample); dst = new BufferedImage(newModel, raster, dst.isAlphaPremultiplied(), null); } dst.createGraphics().drawImage(src, 0, 0, null); return dst; }
From source file:org.csa.rstb.polarimetric.rcp.toolviews.HaAlphaPlotPanel.java
@Override protected void updateChartData() { final ChartPagePanel chartPanel = this; final RasterDataNode rasterX = getRaster(X_VAR); final RasterDataNode rasterY = getRaster(Y_VAR); if (rasterX == null || rasterY == null) { return;//w w w.ja v a 2 s .com } ProgressHandleMonitor pm = ProgressHandleMonitor.create("Computing plot"); Runnable operation = () -> { pm.beginTask("Computing plot...", 100); try { checkBandsForRange(); setRange(X_VAR, rasterX, dataSourceConfig.useRoiMask ? dataSourceConfig.roiMask : null, SubProgressMonitor.create(pm, 15)); setRange(Y_VAR, rasterY, dataSourceConfig.useRoiMask ? dataSourceConfig.roiMask : null, SubProgressMonitor.create(pm, 15)); BufferedImage densityPlotImage = ProductUtils.createDensityPlotImage(rasterX, axisRangeControls[X_VAR].getMin().floatValue(), axisRangeControls[X_VAR].getMax().floatValue(), rasterY, axisRangeControls[Y_VAR].getMin().floatValue(), axisRangeControls[Y_VAR].getMax().floatValue(), dataSourceConfig.useRoiMask ? dataSourceConfig.roiMask : null, 512, 512, backgroundColor, null, SubProgressMonitor.create(pm, 70)); densityPlotImage = new BufferedImage(untoggledColorModel, densityPlotImage.getRaster(), densityPlotImage.isAlphaPremultiplied(), null); plotColorsInverted = false; checkBandsForRange(); double minX = axisRangeControls[X_VAR].getMin(); double maxX = axisRangeControls[X_VAR].getMax(); double minY = axisRangeControls[Y_VAR].getMin(); double maxY = axisRangeControls[Y_VAR].getMax(); if (minX > maxX || minY > maxY) { JOptionPane.showMessageDialog(chartPanel, "Failed to compute plot.\n" + "No Pixels considered..", CHART_TITLE, JOptionPane.ERROR_MESSAGE); plot.setDataset(null); return; } if (MathUtils.equalValues(minX, maxX, 1.0e-4)) { minX = Math.floor(minX); maxX = Math.ceil(maxX); } if (MathUtils.equalValues(minY, maxY, 1.0e-4)) { minY = Math.floor(minY); maxY = Math.ceil(maxY); } plot.setImage(densityPlotImage); plot.setImageDataBounds(new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY)); axisRangeControls[X_VAR].adjustComponents(minX, maxX, NUM_DECIMALS); axisRangeControls[Y_VAR].adjustComponents(minY, maxY, NUM_DECIMALS); // plot.getDomainAxis().setLabel(StatisticChartStyling.getAxisLabel(getRaster(X_VAR), "Entropy", false)); // plot.getRangeAxis().setLabel(StatisticChartStyling.getAxisLabel(getRaster(Y_VAR), "Alpha", false)); plot.getDomainAxis().setLabel("Entropy"); plot.getRangeAxis().setLabel("Alpha"); toggleZoneOverlayCheckBox.setEnabled(true); // clear the list java.util.List<XYAnnotation> annotList = plot.getAnnotations(); for (XYAnnotation an : annotList) { plot.removeAnnotation(an); } if (toggleZoneOverlayCheckBox.isSelected()) { drawZoneOverlay(); } } catch (Exception e) { SnapApp.getDefault().handleError("Failed to compute plot", e); } finally { pm.done(); } }; ProgressUtils.runOffEventThreadWithProgressDialog(operation, "Computing plot", pm.getProgressHandle(), true, 50, 1000); }
From source file:org.csa.rstb.dat.toolviews.HaAlphaPlotPanel.java
@Override protected void updateChartData() { final RasterDataNode rasterX = getRaster(X_VAR); final RasterDataNode rasterY = getRaster(Y_VAR); if (rasterX == null || rasterY == null) { return;/*from www . jav a 2 s. co m*/ } ProgressMonitorSwingWorker<BufferedImage, Object> swingWorker = new ProgressMonitorSwingWorker<BufferedImage, Object>( this, "Computing plot") { @Override protected BufferedImage doInBackground(ProgressMonitor pm) throws Exception { pm.beginTask("Computing plot...", 100); try { checkBandsForRange(); setRange(X_VAR, rasterX, dataSourceConfig.useRoiMask ? dataSourceConfig.roiMask : null, SubProgressMonitor.create(pm, 15)); setRange(Y_VAR, rasterY, dataSourceConfig.useRoiMask ? dataSourceConfig.roiMask : null, SubProgressMonitor.create(pm, 15)); BufferedImage densityPlotImage = ProductUtils.createDensityPlotImage(rasterX, axisRangeControls[X_VAR].getMin().floatValue(), axisRangeControls[X_VAR].getMax().floatValue(), rasterY, axisRangeControls[Y_VAR].getMin().floatValue(), axisRangeControls[Y_VAR].getMax().floatValue(), dataSourceConfig.useRoiMask ? dataSourceConfig.roiMask : null, 512, 512, backgroundColor, null, SubProgressMonitor.create(pm, 70)); densityPlotImage = new BufferedImage(untoggledColorModel, densityPlotImage.getRaster(), densityPlotImage.isAlphaPremultiplied(), null); plotColorsInverted = false; return densityPlotImage; } finally { pm.done(); } } @Override public void done() { try { checkBandsForRange(); final BufferedImage densityPlotImage = get(); double minX = axisRangeControls[X_VAR].getMin(); double maxX = axisRangeControls[X_VAR].getMax(); double minY = axisRangeControls[Y_VAR].getMin(); double maxY = axisRangeControls[Y_VAR].getMax(); if (minX > maxX || minY > maxY) { JOptionPane.showMessageDialog(getParentDialogContentPane(), "Failed to compute plot.\n" + "No Pixels considered..", /*I18N*/ CHART_TITLE, /*I18N*/ JOptionPane.ERROR_MESSAGE); plot.setDataset(null); return; } if (MathUtils.equalValues(minX, maxX, 1.0e-4)) { minX = Math.floor(minX); maxX = Math.ceil(maxX); } if (MathUtils.equalValues(minY, maxY, 1.0e-4)) { minY = Math.floor(minY); maxY = Math.ceil(maxY); } plot.setImage(densityPlotImage); plot.setImageDataBounds(new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY)); axisRangeControls[X_VAR].adjustComponents(minX, maxX, NUM_DECIMALS); axisRangeControls[Y_VAR].adjustComponents(minY, maxY, NUM_DECIMALS); // plot.getDomainAxis().setLabel(StatisticChartStyling.getAxisLabel(getRaster(X_VAR), "Entropy", false)); // plot.getRangeAxis().setLabel(StatisticChartStyling.getAxisLabel(getRaster(Y_VAR), "Alpha", false)); plot.getDomainAxis().setLabel("Entropy"); plot.getRangeAxis().setLabel("Alpha"); toggleZoneOverlayCheckBox.setEnabled(true); // clear the list java.util.List<XYAnnotation> annotList = plot.getAnnotations(); for (XYAnnotation an : annotList) { plot.removeAnnotation(an); } if (toggleZoneOverlayCheckBox.isSelected()) { drawZoneOverlay(); } } catch (InterruptedException | CancellationException e) { e.printStackTrace(); JOptionPane.showMessageDialog(getParentDialogContentPane(), "Failed to compute plot.\n" + "Calculation canceled.", /*I18N*/ CHART_TITLE, /*I18N*/ JOptionPane.ERROR_MESSAGE); } catch (ExecutionException | IllegalArgumentException e) { e.printStackTrace(); JOptionPane.showMessageDialog(getParentDialogContentPane(), "Failed to compute plot.\n" + "An error occurred:\n" + e.getCause().getMessage(), CHART_TITLE, /*I18N*/ JOptionPane.ERROR_MESSAGE); } } }; swingWorker.execute(); }
From source file:org.apache.fop.visual.BitmapComparator.java
/** * Builds a new BufferedImage that is the difference between the two input images * @param ref the reference bitmap/* w ww . ja v a 2s .c o m*/ * @param gen the newly generated bitmap * @return the diff bitmap */ public static BufferedImage buildDiffImage(BufferedImage ref, BufferedImage gen) { BufferedImage diff = new BufferedImage(ref.getWidth(), ref.getHeight(), BufferedImage.TYPE_INT_ARGB); WritableRaster refWR = ref.getRaster(); WritableRaster genWR = gen.getRaster(); WritableRaster dstWR = diff.getRaster(); boolean refPre = ref.isAlphaPremultiplied(); if (!refPre) { ColorModel cm = ref.getColorModel(); cm = GraphicsUtil.coerceData(refWR, cm, true); ref = new BufferedImage(cm, refWR, true, null); } boolean genPre = gen.isAlphaPremultiplied(); if (!genPre) { ColorModel cm = gen.getColorModel(); cm = GraphicsUtil.coerceData(genWR, cm, true); gen = new BufferedImage(cm, genWR, true, null); } int w = ref.getWidth(); int h = ref.getHeight(); int y, i, val; int[] refPix = null; int[] genPix = null; for (y = 0; y < h; y++) { refPix = refWR.getPixels(0, y, w, 1, refPix); genPix = genWR.getPixels(0, y, w, 1, genPix); for (i = 0; i < refPix.length; i++) { // val = ((genPix[i] - refPix[i]) * 5) + 128; val = ((refPix[i] - genPix[i]) * 10) + 128; if ((val & 0xFFFFFF00) != 0) { if ((val & 0x80000000) != 0) { val = 0; } else { val = 255; } } genPix[i] = val; } dstWR.setPixels(0, y, w, 1, genPix); } if (!genPre) { ColorModel cm = gen.getColorModel(); cm = GraphicsUtil.coerceData(genWR, cm, false); } if (!refPre) { ColorModel cm = ref.getColorModel(); cm = GraphicsUtil.coerceData(refWR, cm, false); } return diff; }
From source file:org.deegree.tile.persistence.geotiff.GeoTIFFTile.java
@Override public BufferedImage getAsImage() throws TileIOException { ImageReader reader = null;/* w w w .jav a2s. com*/ try { reader = (ImageReader) readerPool.borrowObject(); BufferedImage img = reader.readTile(imageIndex, x, y); if (img.getWidth() != sizeX || img.getHeight() != sizeY) { Hashtable<Object, Object> table = new Hashtable<Object, Object>(); String[] props = img.getPropertyNames(); if (props != null) { for (String p : props) { table.put(p, img.getProperty(p)); } } BufferedImage img2 = new BufferedImage(img.getColorModel(), img.getData().createCompatibleWritableRaster(sizeX, sizeY), img.isAlphaPremultiplied(), table); Graphics2D g = img2.createGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); img = img2; } return img; } catch (Exception e) { throw new TileIOException("Error retrieving image: " + e.getMessage(), e); } finally { try { readerPool.returnObject(reader); } catch (Exception e) { // ignore closing error } } }