package com.algobase.service;


import java.io.File;
import java.io.FileOutputStream;

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;

public class MyBitmap {

public static Bitmap bitmap_from_image(Context context, int res, int clr)
{
  int r = Color.red(clr);
  int g = Color.green(clr);
  int b = Color.blue(clr);

  Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), res);
  int width = bmp.getWidth();
  int height = bmp.getHeight();

  Bitmap bmp1 = Bitmap.createBitmap(width, height,
                                              Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bmp1);

  Paint paint = new Paint();

  float[] f = new float[] {0,0,0,0, r,
                           0,0,0,0, g,
                           0,0,0,0, b,
                           0,0,0,1, 0,
                           //0,0,0,a,0
                          };
/*
  float[] f = new float[] {0,0,0,1, 0,
                           0,0,0,1, 0,
                           0,0,0,1, 0,
                           0,0,0,1, 0,
                           //0,0,0,a,0
                          };
*/

   ColorMatrixColorFilter filter =
                   new ColorMatrixColorFilter(new ColorMatrix(f));

   paint.setColorFilter(filter);
   canvas.drawBitmap(bmp,0,0,paint);

   return bmp1;
}

public static Bitmap bitmap_from_drawable(Context context, int res, int clr)
{
  int r = Color.red(clr);
  int g = Color.green(clr);
  int b = Color.blue(clr);

  Drawable drawable = context.getDrawable(res);

  int width = 128;
  int height = 128;

  Bitmap bmp1 = Bitmap.createBitmap(width, height,
                                              Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bmp1);


  float[] f = new float[] {0,0,0,0, r,
                           0,0,0,0, g,
                           0,0,0,0, b,
                           0,0,0,1, 0,
                          };

   ColorMatrixColorFilter filter =
                   new ColorMatrixColorFilter(new ColorMatrix(f));

   drawable.setColorFilter(filter);
   drawable.setBounds(12,12,width-12,height-12);
   drawable.draw(canvas);

   return bmp1;
}


static public void writeBitmap(File file, Bitmap bmp)
{ try {
     FileOutputStream out = new FileOutputStream(file);
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
     out.flush();
     out.close();
  } catch (Exception e) { }

}

}

