Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (C) 2012-2014 GREE, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import android.graphics.Bitmap;

import java.nio.ByteBuffer;
import javax.microedition.khronos.opengles.GL10;

public class Main {
    /**
     * non-premultiplied alpha version of GLUtils.texImage2D(). Note: this method is Slow and should only be used when really necessary!
     *
     * @param gl
     * @param bitmap
     * @see GLUtils.texImage2D()
     */
    public static void texImage2DNonPremultipliedAlpha(final GL10 gl, final Bitmap bitmap) {
        final int[] pixels = extractPixels(bitmap);
        final byte[] pixelComponents = new byte[pixels.length * 4];
        int byteIndex = 0, p;
        for (int i = 0; i < pixels.length; i++) {
            p = pixels[i];
            // Convert to byte representation RGBA required by gl.glTexImage2D.
            pixelComponents[byteIndex++] = (byte) ((p >> 16) & 0xFF); // red
            pixelComponents[byteIndex++] = (byte) ((p >> 8) & 0xFF); //
            pixelComponents[byteIndex++] = (byte) ((p) & 0xFF); // blue
            pixelComponents[byteIndex++] = (byte) (p >> 24); // alpha
        }
        gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bitmap.getWidth(), bitmap.getHeight(), 0, GL10.GL_RGBA,
                GL10.GL_UNSIGNED_BYTE, ByteBuffer.wrap(pixelComponents));
    }

    public static int[] extractPixels(final Bitmap bitmap) {
        final int w = bitmap.getWidth();
        final int h = bitmap.getHeight();
        final int[] colors = new int[w * h];
        bitmap.getPixels(colors, 0, w, 0, 0, w, h);
        return colors;
    }
}