Converts an array of short values to BufferedImage with type TYPE_USHORT_GRAY based on the histogram. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Convert

Description

Converts an array of short values to BufferedImage with type TYPE_USHORT_GRAY based on the histogram.

Demo Code

/*// w  w w .  ja  va  2  s  . co  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;

public class Main {
    /**
     * Converts an array of short values to <code>BufferedImage</code> with type
     * TYPE_USHORT_GRAY based on the <code>histogram</code>.
     * 
     * @param array the array of short values.
     * @param histogram cumulative frequencies of the values in the <code>array
     *    </code>.
     * @param bi converted <code>BufferedImage</code> with type TYPE_USHORT_GRAY. 
     *    The higher the frequency, the greater the pixel value.
     */
    public static void histogramToBufferedImageUShort(short[] array,
            float[] histogram, BufferedImage bi) {
        short[] imageArray = ((DataBufferUShort) bi.getRaster()
                .getDataBuffer()).getData();
        int totalPixels = bi.getWidth() * bi.getHeight();
        for (int i = 0; i < totalPixels; i++) {
            short a = array[i];
            int v = a & 0x0000ffff;
            if (v < 0 || v > histogram.length)
                v = 0;
            imageArray[i] = (short) (histogram[v] * 65535);
        }
    }
}

Related Tutorials