Java Code Examples for java.awt.image.Raster#getSample()

The following examples show how to use java.awt.image.Raster#getSample() . 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: ShortHistogramTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 2
Source File: ShortHistogramTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 3
Source File: CsvExporter.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
static double getValue(RasterDataNode raster, int pixelX, int pixelY, int currentLevel) {
    final RenderedImage image = raster.getGeophysicalImage().getImage(currentLevel);
    final Rectangle pixelRect = new Rectangle(pixelX, pixelY, 1, 1);
    final Raster data = image.getData(pixelRect);
    final MultiLevelImage validMaskImage = raster.getValidMaskImage();
    Raster validMaskData = null;
    if (validMaskImage != null) {
        final RenderedImage validMask = validMaskImage.getImage(currentLevel);
        validMaskData = validMask.getData(pixelRect);
    }
    final double value;
    if (validMaskData == null || validMaskData.getSample(pixelX, pixelY, 0) > 0) {
        value = data.getSampleDouble(pixelX, pixelY, 0);
    } else {
        value = Double.NaN;
    }
    return value;
}
 
Example 4
Source File: ImageLoader.java    From DataVec with Apache License 2.0 6 votes vote down vote up
protected int[][] toIntArrayArray(BufferedImage image) {
    int w = image.getWidth(), h = image.getHeight();
    int[][] ret = new int[h][w];
    if (image.getRaster().getNumDataElements() == 1) {
        Raster raster = image.getRaster();
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                ret[i][j] = raster.getSample(j, i, 0);
            }
        }
    } else {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                ret[i][j] = image.getRGB(j, i);
            }
        }
    }
    return ret;
}
 
Example 5
Source File: ShortHistogramTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 6
Source File: ShortHistogramTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 7
Source File: DLSegment.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
private static BufferedImage combineMasks(BufferedImage m1, BufferedImage m2, int dx, int dy) {
    BufferedImage combined = new BufferedImage(m1.getWidth(),m1.getHeight(),m1.getType());
    combined.getGraphics().drawImage(m1,0,0,null);
    int fg = Color.white.getRGB();
    Raster r2 = m2.getRaster();
    int minx = -dx<0?0:-dx;
    int miny = -dy<0?0:-dy;
    int maxx = m2.getWidth()-dx<m2.getWidth()?m2.getWidth()-dx:m2.getWidth();
    int maxy = m2.getHeight()-dy<m2.getHeight()?m2.getHeight()-dy:m2.getHeight();
    
    for (int x=minx; x<maxx; x++)
        for (int y=miny; y<maxy; y++) {
            if (r2.getSample(x,y,0)>0) {
                combined.setRGB(x+dx,y+dy,fg);
            }
        }
    return combined;
}
 
Example 8
Source File: ShortHistogramTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 9
Source File: ImageLoader.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
protected int[][] toIntArrayArray(BufferedImage image) {
    int w = image.getWidth(), h = image.getHeight();
    int[][] ret = new int[h][w];
    if (image.getRaster().getNumDataElements() == 1) {
        Raster raster = image.getRaster();
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                ret[i][j] = raster.getSample(j, i, 0);
            }
        }
    } else {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                ret[i][j] = image.getRGB(j, i);
            }
        }
    }
    return ret;
}
 
Example 10
Source File: BMPEncoder.java    From jclic with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Encodes and writes raster data as a 24-bit bitmap.
 * @param raster the source raster data
 * @param out the output to which the bitmap will be written
 * @throws java.io.IOException if an error occurs
 */
public static void write24(Raster raster, net.sf.image4j.io.LittleEndianOutputStream out) throws IOException {
  
  int width = raster.getWidth();    
  int height = raster.getHeight();
  
  // calculate bytes per line
  int bytesPerLine = getBytesPerLine24(width);
  
  // write lines
  for (int y = height - 1; y >= 0; y--) {
    
    // write pixel data for each line
    for (int x = 0; x < width; x++) {
      
      // get RGB values for pixel
      int r = raster.getSample(x, y, 0);
      int g = raster.getSample(x, y, 1);
      int b = raster.getSample(x, y, 2);
      
      // write RGB values
      out.writeByte(b);
      out.writeByte(g);
      out.writeByte(r);
    }
    
    // write padding bytes at end of line
    for (int i = width * 3; i < bytesPerLine; i++) {
      out.writeByte(0);
    }
  }
}
 
Example 11
Source File: ExportMaskPixelsAction.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private static long getNumMaskPixels(final RenderedImage maskImage, int sceneRasterWidth, int sceneRasterHeight) {
    final int minTileX = maskImage.getMinTileX();
    final int minTileY = maskImage.getMinTileY();

    final int numXTiles = maskImage.getNumXTiles();
    final int numYTiles = maskImage.getNumYTiles();

    final Rectangle imageRect = new Rectangle(0, 0, sceneRasterWidth, sceneRasterHeight);

    long numMaskPixels = 0;
    for (int tileX = minTileX; tileX < minTileX + numXTiles; ++tileX) {
        for (int tileY = minTileY; tileY < minTileY + numYTiles; ++tileY) {
            final Rectangle tileRectangle = new Rectangle(maskImage.getTileGridXOffset() + tileX * maskImage.getTileWidth(),
                                                          maskImage.getTileGridYOffset() + tileY * maskImage.getTileHeight(),
                                                          maskImage.getTileWidth(), maskImage.getTileHeight());

            final Rectangle r = imageRect.intersection(tileRectangle);
            if (!r.isEmpty()) {
                Raster maskTile = maskImage.getTile(tileX, tileY);
                for (int y = r.y; y < r.y + r.height; y++) {
                    for (int x = r.x; x < r.x + r.width; x++) {
                        if (maskTile.getSample(x, y, 0) != 0) {
                            numMaskPixels++;
                        }
                    }
                }
            }
        }
    }
    return numMaskPixels;
}
 
Example 12
Source File: BMPEncoder.java    From jclic with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Encodes and writes raster data as an 8-bit bitmap.
 * @param raster the source raster data
 * @param out the output to which the bitmap will be written
 * @throws java.io.IOException if an error occurs
 */
public static void write8(Raster raster, net.sf.image4j.io.LittleEndianOutputStream out) throws IOException {
  
  int width = raster.getWidth();
  int height = raster.getHeight();
  
  // calculate bytes per line
  int bytesPerLine = getBytesPerLine8(width);
  
  // write lines
  for (int y = height - 1; y >= 0; y--) {
    
    // write raster data for each line
    for (int x = 0; x < width; x++) {
      
      // get color index for pixel
      int index = raster.getSample(x, y, 0);
      
      
      out.writeByte(index);
    }
    
    // write padding bytes at end of line
    for (int i = width; i < bytesPerLine; i++) {
      out.writeByte(0);
    }
    
  }
}
 
Example 13
Source File: NerveDetectionWorkerMultiCore.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
public List<Shape> detectSegmentations(int minSegmentationSize, PlanarImage sourceImage, Shape roi) {
    logger.trace("getSegmentations (minSegmentation=" + minSegmentationSize + ")");
    if (sourceImage == null) return new ArrayList<Shape>(0);
    short[][] smap = new short[sourceImage.getWidth()][sourceImage.getHeight()];

    for (int x = 0; x < sourceImage.getWidth(); x++)
        for (int y = 0; y < sourceImage.getHeight(); y++)
            smap[x][y] = Short.MAX_VALUE;

    // init
    Point[] tileArr = sourceImage.getTileIndices(null);
    int c;
    for (Point tileNum : tileArr) {
        if (isCancelled()) break;
        final int b = 2;

        Raster raster = sourceImage.getTile(tileNum.x, tileNum.y);
        for (int x = sourceImage.tileXToX(tileNum.x) + b; x < Math.min(sourceImage.tileXToX(tileNum.x) + sourceImage.getTileWidth() - b, sourceImage.getWidth()); x++) {
            for (int y = sourceImage.tileYToY(tileNum.y) + b; y < Math.min(sourceImage.tileYToY(tileNum.y) + sourceImage.getTileHeight() - b, sourceImage.getHeight()); y++) {
                if (fullRoi != null && !(fullRoi.contains(x + roi.getBounds().x, y + roi.getBounds().y))) {
                    continue;
                }

                c = raster.getSample(x, y, 0) < 200 ? 0 : 1; // dark=foreground, white=background (only red channel is taken into account)
                if (c == 0) {  // not background, not assigned
                    smap[x][y] = 1; // 1
                } else {
                    smap[x][y] = 0; // 0
                }
            } //y
        } // x
    } // tileNum


    if (isCancelled()) return null;
    return findPolygons(smap, orderPoints, minSegmentationSize, roi);
}
 
Example 14
Source File: NerveDetectionWorker.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
public List<Shape> detectSegmentations(int minSegmentationSize, PlanarImage sourceImage) {
    logger.trace("getSegmentations (minSegmentation=" + minSegmentationSize + ")");
    rf.initializeClassColors();
    //if (rf.getNegativeChannel() != null) rf.getNegativeChannel().initializeClassColors();
    if (sourceImage == null) return new ArrayList<Shape>(0);
    short[][] smap = new short[sourceImage.getWidth()][sourceImage.getHeight()];

    for (int x = 0; x < sourceImage.getWidth(); x++)
        for (int y = 0; y < sourceImage.getHeight(); y++)
            smap[x][y] = Short.MAX_VALUE;

    // init
    Point[] tileArr = sourceImage.getTileIndices(null);
    int c;
    for (Point tileNum : tileArr) {
        if (isCancelled()) break;
        final int b = 2;

        Raster raster = sourceImage.getTile(tileNum.x, tileNum.y);
        for (int x = sourceImage.tileXToX(tileNum.x) + b; x < Math.min(sourceImage.tileXToX(tileNum.x) + sourceImage.getTileWidth() - b, sourceImage.getWidth()); x++) {
            for (int y = sourceImage.tileYToY(tileNum.y) + b; y < Math.min(sourceImage.tileYToY(tileNum.y) + sourceImage.getTileHeight() - b, sourceImage.getHeight()); y++) {
                if (fullRoi != null && !(fullRoi.contains(x + roi.getBounds().x, y + roi.getBounds().y))) {
                    continue;
                }

                c = raster.getSample(x, y, 0) < 200 ? 0 : 1; // dark=foreground, white=background (only red channel is taken into account)
                if (c == 0) {  // not background, not assigned
                    smap[x][y] = 1; // 1
                } else {
                    smap[x][y] = 0; // 0
                }
            } //y
        } // x
    } // tileNum


    if (isCancelled()) return null;
    return findPolygons(smap, minSegmentationSize);
}
 
Example 15
Source File: ImageLoader.java    From Canova with Apache License 2.0 5 votes vote down vote up
public int[][] fromFile(File file) throws IOException {
    BufferedImage image = ImageIO.read(file);
    if (height > 0 && width > 0)
        image = toBufferedImage(image.getScaledInstance(height, width, Image.SCALE_SMOOTH));
    Raster raster = image.getData();
    int w = raster.getWidth(), h = raster.getHeight();
    int[][] ret = new int[w][h];
    for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++)
            ret[i][j] = raster.getSample(i, j, 0);

    return ret;
}
 
Example 16
Source File: SpectrumTopComponent.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private boolean isPixelValid(RasterDataNode raster, int pixelX, int pixelY, int level) {
    if (raster.isValidMaskUsed()) {
        PlanarImage image = ImageManager.getInstance().getValidMaskImage(raster, level);
        Raster data = getRasterTile(image, pixelX, pixelY);
        return data.getSample(pixelX, pixelY, 0) != 0;
    } else {
        return true;
    }
}
 
Example 17
Source File: PixelInfoViewModelUpdater.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private boolean isPixelValid(RasterDataNode raster, int pixelX, int pixelY, int level) {
    if (raster.isValidMaskUsed()) {
        PlanarImage image = ImageManager.getInstance().getValidMaskImage(raster, level);
        Raster data = getRasterTile(image, pixelX, pixelY);
        return data.getSample(pixelX, pixelY, 0) != 0;
    } else {
        return true;
    }
}
 
Example 18
Source File: FuzzyImageDifferenceCalculator.java    From recheck with GNU Affero General Public License v3.0 5 votes vote down vote up
protected int getAverageBrightness( final BufferedImage img ) {
	final Raster r = img.getData();
	int total = 0;
	for ( int y = 0; y < r.getHeight(); y++ ) {
		for ( int x = 0; x < r.getWidth(); x++ ) {
			total += r.getSample( r.getMinX() + x, r.getMinY() + y, 0 );
		}
	}
	return (int) (total / (r.getWidth() / STABILIZER * (r.getHeight() / STABILIZER)));
}
 
Example 19
Source File: S3HistogramEqualizerRIF.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
int getBlue (Raster raster, int x,int y)
{
   return raster.getSample(x, y, 2);
}
 
Example 20
Source File: S3HistogramEqualizerRIF.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
int getRed (Raster raster, int x,int y)
{
   return raster.getSample(x, y, 0);
}