List of usage examples for java.awt.image Raster getSamples
public double[] getSamples(int x, int y, int w, int h, int b, double[] dArray)
From source file:org.esa.nest.gpf.GCPSelectionOp.java
private static void outputComplexImage(final PlanarImage image) { final int w = image.getWidth(); final int h = image.getHeight(); final Raster dftData = image.getData(); final double[] real = dftData.getSamples(0, 0, w, h, 0, (double[]) null); final double[] imag = dftData.getSamples(0, 0, w, h, 1, (double[]) null); System.out.println("Real part:"); for (double v : real) { System.out.print(v + ", "); }/* w ww .ja va 2 s. c o m*/ System.out.println(); System.out.println("Imaginary part:"); for (double v : imag) { System.out.print(v + ", "); } System.out.println(); }
From source file:org.esa.s1tbx.insar.gpf.coregistration.CrossCorrelationOp.java
private void determiningImageOffset(final Band slaveBand1, final Band slaveBand2, int[] offset) { try {//from w w w .j a v a 2 s. co m // get master and slave imagettes final MetadataElement absRoot = AbstractMetadata.getAbstractedMetadata(sourceProduct); double groundRangeSpacing = absRoot.getAttributeDouble(AbstractMetadata.range_spacing, 1); final double azimuthSpacing = absRoot.getAttributeDouble(AbstractMetadata.azimuth_spacing, 1); final boolean srgrFlag = AbstractMetadata.getAttributeBoolean(absRoot, AbstractMetadata.srgr_flag); if (!srgrFlag) { final TiePointGrid incidenceAngle = OperatorUtils.getIncidenceAngle(sourceProduct); final double incidenceAngleAtCentreRangePixel = incidenceAngle.getPixelDouble(sourceImageWidth / 2f, sourceImageHeight / 2f); groundRangeSpacing /= FastMath.sin(incidenceAngleAtCentreRangePixel * Constants.DTOR); } final int nRgLooks = Math.max(1, sourceImageWidth / 2048); final int nAzLooks = Math.max(1, (int) ((double) nRgLooks * groundRangeSpacing / azimuthSpacing + 0.5)); final int targetImageWidth = sourceImageWidth / nRgLooks; final int targetImageHeight = sourceImageHeight / nAzLooks; final int windowWidth = (int) FastMath.pow(2, (int) (Math.log10(targetImageWidth) / Math.log10(2))); final int windowHeight = (int) FastMath.pow(2, (int) (Math.log10(targetImageHeight) / Math.log10(2))); final double[] mI = new double[windowWidth * windowHeight]; final double[] sI = new double[windowWidth * windowHeight]; final int tileCountX = 4; final int tileCountY = 4; final int tileWidth = windowWidth / tileCountX; final int tileHeight = windowHeight / tileCountY; final Rectangle[] tileRectangles = new Rectangle[tileCountX * tileCountY]; int index = 0; for (int tileY = 0; tileY < tileCountY; tileY++) { final int ypos = tileY * tileHeight; for (int tileX = 0; tileX < tileCountX; tileX++) { final Rectangle tileRectangle = new Rectangle(tileX * tileWidth, ypos, tileWidth, tileHeight); tileRectangles[index++] = tileRectangle; } } final StatusProgressMonitor status = new StatusProgressMonitor(StatusProgressMonitor.TYPE.SUBTASK); status.beginTask("Computing offset... ", tileRectangles.length); final ThreadManager threadManager = new ThreadManager(); try { for (final Rectangle rectangle : tileRectangles) { checkForCancellation(); final Thread worker = new Thread() { @Override public void run() { final int x0 = rectangle.x; final int y0 = rectangle.y; final int w = rectangle.width; final int h = rectangle.height; final int xMax = x0 + w; final int yMax = y0 + h; final int xStart = x0 * nRgLooks; final int yStart = y0 * nAzLooks; final int xEnd = xMax * nRgLooks; final int yEnd = yMax * nAzLooks; final Rectangle srcRect = new Rectangle(xStart, yStart, xEnd - xStart, yEnd - yStart); final Tile mstTile1 = getSourceTile(masterBand1, srcRect); final ProductData mstData1 = mstTile1.getDataBuffer(); final TileIndex mstIndex = new TileIndex(mstTile1); final Tile slvTile1 = getSourceTile(slaveBand1, srcRect); final ProductData slvData1 = slvTile1.getDataBuffer(); final TileIndex slvIndex = new TileIndex(slvTile1); ProductData mstData2 = null; ProductData slvData2 = null; if (complexCoregistration) { mstData2 = getSourceTile(masterBand2, srcRect).getDataBuffer(); slvData2 = getSourceTile(slaveBand2, srcRect).getDataBuffer(); } final double rgAzLooks = nRgLooks * nAzLooks; for (int y = y0; y < yMax; y++) { final int yByWidth = y * windowWidth; final int y1 = y * nAzLooks; final int y2 = y1 + nAzLooks; for (int x = x0; x < xMax; x++) { final int x1 = x * nRgLooks; final int x2 = x1 + nRgLooks; mI[yByWidth + x] = getMeanValue(x1, x2, y1, y2, mstData1, mstData2, mstIndex, rgAzLooks); sI[yByWidth + x] = getMeanValue(x1, x2, y1, y2, slvData1, slvData2, slvIndex, rgAzLooks); } } status.worked(1); } }; threadManager.add(worker); } threadManager.finish(); } catch (Throwable e) { OperatorUtils.catchOperatorException("GCPSelectionOp", e); } finally { status.done(); } // correlate master and slave imagettes final RenderedImage masterImage = createRenderedImage(mI, windowWidth, windowHeight); final PlanarImage masterSpectrum = JAIFunctions.dft(masterImage); final RenderedImage slaveImage = createRenderedImage(sI, windowWidth, windowHeight); final PlanarImage slaveSpectrum = JAIFunctions.dft(slaveImage); final PlanarImage conjugateSlaveSpectrum = JAIFunctions.conjugate(slaveSpectrum); final PlanarImage crossSpectrum = JAIFunctions.multiplyComplex(masterSpectrum, conjugateSlaveSpectrum); final PlanarImage correlatedImage = JAIFunctions.idft(crossSpectrum); final PlanarImage crossCorrelatedImage = JAIFunctions.magnitude(correlatedImage); // compute offset final int w = crossCorrelatedImage.getWidth(); final int h = crossCorrelatedImage.getHeight(); final Raster idftData = crossCorrelatedImage.getData(); final double[] real = idftData.getSamples(0, 0, w, h, 0, (double[]) null); int peakRow = 0; int peakCol = 0; double peak = 0; for (int r = 0; r < h; r++) { for (int c = 0; c < w; c++) { if (r >= h / 4 && r <= h * 3 / 4 || c >= w / 4 && c <= w * 3 / 4) { continue; } final int s = r * w + c; if (peak < real[s]) { peak = real[s]; peakRow = r; peakCol = c; } } } // System.out.println("peakRow = " + peakRow + ", peakCol = " + peakCol); if (peakRow <= h / 2) { offset[1] = -peakRow * nAzLooks; } else { offset[1] = (h - peakRow) * nAzLooks; } if (peakCol <= w / 2) { offset[0] = -peakCol * nRgLooks; } else { offset[0] = (w - peakCol) * nRgLooks; } // System.out.println("offsetX = " + offset[0] + ", offsetY = " + offset[1]); } catch (Throwable e) { OperatorUtils.catchOperatorException(getId() + " getCoarseSlaveGCPPosition ", e); } }
From source file:org.hippoecm.frontend.plugins.gallery.imageutil.ImageUtils.java
/** * Converts image raster data to a JPEG with RGB color space. Only images with color space CMYK and YCCK are * converted, other images are left untouched. * * Rationale: Java's ImageIO can't process 4-component images and Java2D can't apply AffineTransformOp either, * so we have to convert raster data to a JPG with RGB color space. * * The technique used in this method is due to Mark Stephens, and free for any use. See * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903 or * http://www.mail-archive.com/java2d-interest@capra.eng.sun.com/msg03247.html * * @param is the image data/*from www .j a v a 2 s. c om*/ * @param colorModel the color model of the image * @return the RGB version of the supplied image */ public static InputStream convertToRGB(InputStream is, ColorModel colorModel) throws IOException, UnsupportedImageException { if (colorModel != ColorModel.CMYK && colorModel != ColorModel.YCCK) { return is; } // Get an ImageReader. ImageInputStream input = ImageIO.createImageInputStream(is); try { Iterator<ImageReader> readers = ImageIO.getImageReaders(input); if (readers == null || !readers.hasNext()) { throw new UnsupportedImageException("No ImageReaders found"); } ImageReader reader = readers.next(); reader.setInput(input); Raster raster = reader.readRaster(0, reader.getDefaultReadParam()); int w = raster.getWidth(); int h = raster.getHeight(); byte[] rgb = new byte[w * h * 3]; switch (colorModel) { case YCCK: { float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null); float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null); float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null); float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null); for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) { float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i]; double val = y + 1.402 * (cr - 128) - k; val = (val - 128) * .65f + 128; rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5); val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k; val = (val - 128) * .65f + 128; rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5); val = y + 1.772 * (cb - 128) - k; val = (val - 128) * .65f + 128; rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5); } break; } case CMYK: { int[] C = raster.getSamples(0, 0, w, h, 0, (int[]) null); int[] M = raster.getSamples(0, 0, w, h, 1, (int[]) null); int[] Y = raster.getSamples(0, 0, w, h, 2, (int[]) null); int[] K = raster.getSamples(0, 0, w, h, 3, (int[]) null); for (int i = 0, imax = C.length, base = 0; i < imax; i++, base += 3) { int c = 255 - C[i]; int m = 255 - M[i]; int y = 255 - Y[i]; int k = 255 - K[i]; float kk = k / 255f; rgb[base] = (byte) (255 - Math.min(255f, c * kk + k)); rgb[base + 1] = (byte) (255 - Math.min(255f, m * kk + k)); rgb[base + 2] = (byte) (255 - Math.min(255f, y * kk + k)); } break; } } // from other image types we know InterleavedRaster's can be // manipulated by AffineTransformOp, so create one of those. raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3, new int[] { 0, 1, 2 }, null); ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); java.awt.image.ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); BufferedImage convertedImage = new BufferedImage(cm, (WritableRaster) raster, true, null); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(convertedImage, "jpg", os); return new ByteArrayInputStream(os.toByteArray()); } finally { IOUtils.closeQuietly(is); if (input != null) { input.close(); } } }