Here you can find the source of depthToGrayBufferedImage(int[] depth, int width, int height)
Parameter | Description |
---|---|
depth | an integer array of depth values. |
width | width of the image returned. |
height | height of the image returned. |
public static BufferedImage depthToGrayBufferedImage(int[] depth, int width, int height)
//package com.java2s; /*/*from ww w . j a v a 2s . c o m*/ * Image conversion utilities. * * Copyright (c) 2006 Jean-Sebastien Senecal (js@drone.ws) * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation; either version 2 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 675 Mass * Ave, Cambridge, MA 02139, USA. */ import java.awt.image.BufferedImage; import java.awt.image.DataBufferUShort; import java.util.Arrays; public class Main { /** * Converts an array of depth values to a gray BufferedImage. * * The size of the array must equal to the product of width and height. * * @param depth an integer array of depth values. * @param width width of the image returned. * @param height height of the image returned. * @return a gray BufferedImage such that the brightness of each pixel is * <i>inversely</i> proportional to the depth from the camera. */ public static BufferedImage depthToGrayBufferedImage(int[] depth, int width, int height) { final int MAX_DEPTH = 65535; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY); short[] imageArray = ((DataBufferUShort) image.getRaster().getDataBuffer()).getData(); int totalPixels = width * height; int max = 0; int min = MAX_DEPTH; // Two bytes. for (int i = 0; i < totalPixels; i++) { int value = depth[i]; if (value != 0) { max = Math.max(max, value); min = Math.min(min, value); } } if (min == max) { Arrays.fill(imageArray, (short) 0); } else { for (int i = 0; i < totalPixels; i++) { int value = depth[i]; imageArray[i] = value == 0 ? 0 : (short) ((max - value) * MAX_DEPTH / (max - min)); } } return image; } public static void depthToGrayBufferedImage(short[] depth, BufferedImage bi) { final int MAX_DEPTH = 65535; short[] imageArray = ((DataBufferUShort) bi.getRaster().getDataBuffer()).getData(); int totalPixels = bi.getWidth() * bi.getHeight(); int max = 0; int min = MAX_DEPTH; // Two bytes. for (int i = 0; i < totalPixels; i++) { int value = depth[i] & 0x0000ffff; if (value != 0) { max = Math.max(max, value); min = Math.min(min, value); } } if (min == max) { Arrays.fill(imageArray, (short) 0); } else { for (int i = 0; i < totalPixels; i++) { int value = depth[i] & 0x0000ffff; imageArray[i] = value == 0 ? 0 : (short) ((max - value) * MAX_DEPTH / (max - min)); } } } }