package org.gearvrf.utility;

import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Color;

/**
 * Utilities for basic image I/O and manipulation.
 */
public class ImageUtils
{
    /**
     * Generates a {@code Bitmap} from a byte array containing {@code width} *
     * {@code height} pixels. The pixel format is RGBA in little endian. The
     * alpha value is not used in the result bitmap. The bitmap is also
     * vertically flipped.
     *
     * @param byteArray The input byte array.
     * @param width The width of the image.
     * @param height The height of the image.
     * @return The generated {@code Bitmap} object.
     */
    public static Bitmap generateBitmapFlipV(final byte[] byteArray, final int width,
            final int height) {
        int[] pixels = new int[width * height];
        for (int row = 0; row < height; row++) {
            int start_position = row * width;
            int reverse_start_position = (height - 1 - row) * width;
            for (int col = 0; col < width; col++) {
                int position = (start_position + col) * 4;
                int r = byteArray[position++] & 0xff;
                int g = byteArray[position++] & 0xff;
                int b = byteArray[position] & 0xff;
                // flip the image vertically
                pixels[reverse_start_position + col] = Color.rgb(r, g, b);
            }
        }
        return Bitmap.createBitmap(pixels, width, height,
                Bitmap.Config.ARGB_8888);
    }

    /**
     * Generates a {@code Bitmap} from a int array containing {@code width} *
     * {@code height} pixels. The pixel format is in ARGB_8888 format for
     * Android.
     *
     * @param argbIntBuffer The int array containing pixels values.
     * @param width The width of the image.
     * @param height The height of the image.
     * @return The generated {@code Bitmap} object.
     */
    public static Bitmap generateBitmap(final int[] argbIntBuffer,
            final int width, final int height) {
        return Bitmap.createBitmap(argbIntBuffer,
                                   width, height, Bitmap.Config.ARGB_8888);
    }

    /**
     * Saves a {@code Bitmap} as a PNG file.
     *
     * @param filename The file path on the file system.
     * @param bitmap The input {@code Bitmap} object.
     * @return {@code true} if successful.
     */
    public static boolean saveBitmapAsPNG(String filename, Bitmap bitmap) {
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(filename);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }

        return true;
    }
}

@@ -85,16 +85,135 @@
 ption;%0A%0A
+import org.gearvrf.GVRAndroidResource;%0Aimport org.gearvrf.GVRContext;%0A%0Aimport android.content.res.AssetFileDescriptor;%0A
 import a
@@ -266,16 +266,50 @@
 s.Color;
+%0Aimport android.media.MediaPlayer;
 %0A%0A/**%0A *
@@ -3221,11 +3221,776 @@
 e;%0A    %7D
+%0A%0A    /**%0A     * Creates a %7B@code MediaPlayer%7D with a specified data source. The returned media player%0A     * is not started.%0A     *%0A     * @param filenameOrURL%0A     *         A string representing a path of the data file, or a URL.%0A     *%0A     * @return The %7B@code MediaPlayer%7D object if successful, or %7B@code null%7D if there is%0A     *         an error.%0A     */%0A    public static MediaPlayer createMediaPlayer(String filenameOrURL) %7B%0A        // create mediaplayer instance%0A        MediaPlayer mediaPlayer = new MediaPlayer();%0A        try %7B%0A            mediaPlayer.setDataSource(filenameOrURL);%0A            mediaPlayer.prepare();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A            return null;%0A        %7D%0A%0A        return mediaPlayer;%0A    %7D
 %0A%7D%0A

Add utility to get a MediaPlayer for a file or URL