Java ByteBuffer Size depthToGrayBufferedImage(int[] depth, int width, int height)

Here you can find the source of depthToGrayBufferedImage(int[] depth, int width, int height)

Description

Converts an array of depth values to a gray BufferedImage.

License

Open Source License

Parameter

Parameter Description
depth an integer array of depth values.
width width of the image returned.
height height of the image returned.

Return

a gray BufferedImage such that the brightness of each pixel is inversely proportional to the depth from the camera.

Declaration

public static BufferedImage depthToGrayBufferedImage(int[] depth, int width, int height) 

Method Source Code

//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));
            }
        }
    }
}

Related

  1. cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop, int sourceLeft, int sourceWidth, int sourceHeight)
  2. cutAndScaleToRoundSquare(BufferedImage src, int size, int radius)
  3. cutAndScaleToSquare(BufferedImage src, int size)
  4. decreaseBufferCapatity(final ByteBuffer byteBuffer, final int size, final int minSize)
  5. decryptData(final ByteBuffer buffer, final int size, final int key)
  6. doThumb(BufferedImage from, int width, int height)
  7. enhance(int size, ByteBuffer bbuf)
  8. ensureBufferCapacity(final int width, final int height, BufferedImage img)
  9. erodeImage(final BufferedImage img, int structElementWidth, int structElementHeight, final int rgbForegroundColor, final int rgbBackgroundColor)