Java BufferedImage Operation intensityArrayToBufferedImage(byte[] array, int w, int h)

Here you can find the source of intensityArrayToBufferedImage(byte[] array, int w, int h)

Description

Converts the given intensity array into a grayscale image of w x h dimensions.

License

Open Source License

Parameter

Parameter Description
array The array to convert
w the width of the resulting image
h The height of the resulting image

Return

The grayscale image that the intensity values represent

Declaration

public static BufferedImage intensityArrayToBufferedImage(byte[] array, int w, int h) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
 * |                                                                         |
 *    faint - The Face Annotation Interface
 * |  Copyright (C) 2007  Malte Mathiszig                                    |
 * // ww w .  j  a  va 2 s  . c  o  m
 * |  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 3 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, see <http://www.gnu.org/licenses/>.
 * |                                                                         |
 * + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
 *******************************************************************************/

import java.awt.Dimension;

import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;

public class Main {
    /**
     * Converts the given intensity array into a grayscale image of w x h dimensions.
     *
     * @param array The array to convert
     * @param w the width of the resulting image
     * @param h The height of the resulting image
     * @return The grayscale image that the intensity values represent
     */
    public static BufferedImage intensityArrayToBufferedImage(byte[] array, int w, int h) {
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        int[] nBits = { 8 };
        ColorModel cm = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        SampleModel sm = cm.createCompatibleSampleModel(w, h);
        DataBufferByte db = new DataBufferByte(array, w * h);
        WritableRaster raster = Raster.createWritableRaster(sm, db, null);
        BufferedImage bm = new BufferedImage(cm, raster, false, null);
        return bm;
    }

    /**
      * Convert the given intensity array to a buffered image of {@code size} dimensions.
      *
     * @param averageFace The intensity of the pixels
     * @param face_thumbnail_size The size of the image.
     * @return The image.
     */
    public static BufferedImage intensityArrayToBufferedImage(byte[] averageFace, Dimension size) {
        return intensityArrayToBufferedImage(averageFace, size.width, size.height);
    }
}

Related

  1. hitTest(BufferedImage image, int x, int y)
  2. hueShift(BufferedImage image, int hue)
  3. inColormap(float[][] in, float min, float max, float[][] colormap, BufferedImage b)
  4. indexToDirectColorModel(BufferedImage image)
  5. intBuffer2BufferedImage(IntBuffer ib, BufferedImage img)
  6. interp(BufferedImage img1, BufferedImage img2, int weight1, int weight2)
  7. knit(BufferedImage[] buffImages)
  8. layer(BufferedImage source, BufferedImage destination, int x, int y)
  9. makeBinary(BufferedImage image, int threshold)