Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.graphics.Bitmap;

import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicHistogram;

public class Main {
    private static final int COLOR_BIT_DEPTH = 256;
    public static int NUM_CHANNELS = 4;

    /**
     * Return the histograms for each color channel (interleaved).
     *
     * @param rs a {@link RenderScript} context to use.
     * @param bmap a {@link Bitmap} to generate the histograms for.
     * @return an array containing NUM_CHANNELS * COLOR_BIT_DEPTH histogram bucket values, with
     * the color channels interleaved.
     */
    public static int[] calcHistograms(RenderScript rs, Bitmap bmap) {
        ScriptIntrinsicHistogram hist = ScriptIntrinsicHistogram.create(rs, Element.U8_4(rs));
        Allocation sums = Allocation.createSized(rs, Element.I32_4(rs), COLOR_BIT_DEPTH);

        // Setup input allocation (ARGB 8888 bitmap).
        Allocation input = Allocation.createFromBitmap(rs, bmap);

        hist.setOutput(sums);
        hist.forEach(input);
        int[] output = new int[COLOR_BIT_DEPTH * NUM_CHANNELS];
        sums.copyTo(output);
        return output;
    }
}