Java examples for 2D Graphics:BufferedImage Convert
Converts an array of depth values to a gray BufferedImage.
/*//w w w .ja va 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. */ //package com.java2s; import java.awt.image.BufferedImage; import java.awt.image.DataBufferUShort; import java.nio.ShortBuffer; 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)); } } } /** * Converts depth values in <code>ShortBuffer</code> to a gray scale * <code>BufferedImage</code>. * @param buffer * @param bi <BufferedImage> of with type <code>TYPE_USHORT_GRAY</code> */ public static void depthToGrayBufferedImage(ShortBuffer buffer, BufferedImage bi) { final int MAX_DEPTH = 65535; short[] imageArray = ((DataBufferUShort) bi.getRaster() .getDataBuffer()).getData(); buffer.rewind(); int max = 0; int min = MAX_DEPTH; // Two bytes. while (buffer.remaining() > 0) { int value = buffer.get() & 0x0000ffff; if (value != 0) { max = Math.max(max, value); min = Math.min(min, value); } } if (min == max) { Arrays.fill(imageArray, (short) 0); } else { buffer.rewind(); while (buffer.remaining() > 0) { int pos = buffer.position(); int value = buffer.get() & 0x0000ffff; imageArray[pos] = value == 0 ? 0 : (short) ((max - value) * MAX_DEPTH / (max - min)); } } } }