Java FloatBuffer to Array floatBufferToHistogram(FloatBuffer ib, int width, int height, int widthStep, float[] histogram)

Here you can find the source of floatBufferToHistogram(FloatBuffer ib, int width, int height, int widthStep, float[] histogram)

Description

float Buffer To Histogram

License

Open Source License

Declaration

public static void floatBufferToHistogram(FloatBuffer ib, int width, int height, int widthStep,
            float[] histogram) 

Method Source Code

//package com.java2s;
/*//from   w w w.  j  a  v  a 2s.c om
 * 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.nio.FloatBuffer;

import java.util.Arrays;

public class Main {
    public static void floatBufferToHistogram(FloatBuffer ib, int width, int height, int widthStep,
            float[] histogram) {
        Arrays.fill(histogram, 0);
        ib.rewind();
        int totalPoints = 0;
        for (int y = 0; y < height; y++)
            for (int x = 0; x < width; x++) {
                int v = (int) ib.get(y * widthStep + x);
                if (v > 0 && v < histogram.length) {
                    histogram[v]++;
                    totalPoints++;
                }
            }
        accumulateHistogram(histogram, totalPoints);
    }

    private static void accumulateHistogram(float[] histogram, int totalPoints) {
        for (int i = 1; i < histogram.length; i++)
            histogram[i] += histogram[i - 1];

        if (totalPoints > 0) {
            for (int i = 1; i < histogram.length; i++)
                histogram[i] = (totalPoints - histogram[i]) / (float) totalPoints;
        }
    }
}

Related

  1. convertFloatBufferToArray(java.nio.FloatBuffer buf)
  2. floatArrayToFloatBuffer(float[] data)
  3. floatBufferToString(FloatBuffer buffer)
  4. getFloatArray(final FloatBuffer buff)
  5. getFloatArray(FloatBuffer buff)