SimpleImage.java :  » App » droidslator » com » codemobiles » droidslator_tattoo » Android Open Source

Android Open Source » App » droidslator 
droidslator » com » codemobiles » droidslator_tattoo » SimpleImage.java
/*
 * Copyright (C) 2009 codemobiles.com.
 * http://www.codemobiles.com
 *
 * 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.
 * author: Chaiyasit Tayabovorn 
 * email: chaiyasit.t@gmail.com
 */

package com.codemobiles.droidslator_tattoo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.util.Log;

/**
 * This class is responsible for encapsulating element needed for a bitmap
 * image.
 * 
 * @author jaboho
 * 
 */
public class SimpleImage {
  private Bitmap img;
  private int X = 0;
  private int Y = 0;
  private int beginX = 0;
  private int beginY = 0;
  private String id;
  private int[] orgPix;
  private Context context;
  private int titleID;

  public SimpleImage(Context context, int drawable, Point point, boolean fixedPos, String identifier) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;
    this.context = context;
    img = BitmapFactory.decodeResource(this.context.getResources(), drawable);
    orgPix = new int[getWidth() * getHeight()];
    img.getPixels(orgPix, 0, getWidth(), 0, 0, getWidth(), getHeight());
    id = identifier;
    X = point.x;
    Y = point.y;
    beginX = point.x;
    beginY = point.y;
  }
  
  public void reDrawImage(int drawable, String identifier){
    img = null;
    img = BitmapFactory.decodeResource(this.context.getResources(), drawable);
    orgPix = new int[getWidth() * getHeight()];
    img.getPixels(orgPix, 0, getWidth(), 0, 0, getWidth(), getHeight());
    id = identifier;
  }

  public void setPosition(RegionSpec reg) {
    this.X = reg.getX();
    this.Y = reg.getY();
    this.beginX = reg.getBeginX();
    this.beginY = reg.getBeginY();
  }

  public void swapObjPos(RegionSpec ballTarget) {
    X = ballTarget.getX();
    Y = ballTarget.getY();
    beginX = ballTarget.getBeginX();
    beginY = ballTarget.getBeginY();
  }

  public void revertPosition() {
    X = beginX;
    Y = beginY;
  }

  public double getCenterX() {
    return X + (this.getWidth() / 2);
  }

  public double getCenterY() {
    return Y + (this.getHeight() / 2);
  }

  void setBeginX(int newValue) {
    beginX = newValue;
  }

  public int getBeginX() {
    return beginX;
  }

  void setBeginY(int newValue) {
    beginY = newValue;
  }

  public int getBeginY() {
    return beginY;
  }

  void setX(int newValue) {
    X = newValue;
  }

  public int getX() {
    return X;
  }

  void setY(int newValue) {
    Y = newValue;
  }

  public int getY() {
    return Y;
  }

  public int getWidth() {
    return img.getWidth();
  }

  public int getHeight() {
    return img.getHeight();
  }

  public String getID() {
    return id;
  }

  public Bitmap getBitmap() {
    return img;
  }

  public int getTitleID() {
  return titleID;
  }
  
  public void setTitleID(int newValue) {
  titleID = newValue;
  }
  
  public void halfOpacity(){
    tintImage(0, 0x55);
  }
  
  public void restoreOpacity(){
    img.setPixels(orgPix, 0, getWidth(), 0, 0, getWidth(), getHeight());
  }
  
  private void tintImage(int deg, int opacity) {

    try {
      int picw = getWidth();
      int pich = getHeight();
      int[] pix = new int[picw * pich];
      img.getPixels(pix, 0, picw, 0, 0, picw, pich);

      int RY, BY, RYY, GYY, BYY, R, G, B, Y;
      double angle = (3.14159d * (double) deg) / 180.0d;
      int S = (int) (256.0d * Math.sin(angle));
      int C = (int) (256.0d * Math.cos(angle));

      for (int y = 0; y < pich; y++)
        for (int x = 0; x < picw; x++) {
          int index = y * picw + x;
          int r = (pix[index] >> 16) & 0xff;
          int g = (pix[index] >> 8) & 0xff;
          int b = pix[index] & 0xff;
          if (pix[index] == 0x00) continue;
          
          RY = (70 * r - 59 * g - 11 * b) / 100;
          BY = (-30 * r - 59 * g + 89 * b) / 100;
          Y = (30 * r + 59 * g + 11 * b) / 100;
          RYY = (S * BY + C * RY) / 256;
          BYY = (C * BY - S * RY) / 256;
          GYY = (-51 * RYY - 19 * BYY) / 100;
          R = Y + RYY;
          R = (R < 0) ? 0 : ((R > 255) ? 255 : R);
          G = Y + GYY;
          G = (G < 0) ? 0 : ((G > 255) ? 255 : G);
          B = Y + BYY;
          B = (B < 0) ? 0 : ((B > 255) ? 255 : B);
          pix[index] = 0x00000000 | (opacity << 24) | (R << 16) | (G << 8) | B;
        }

      img = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_4444);
      img.setPixels(pix, 0, picw, 0, 0, picw, pich);
      pix = null;

    } catch (Exception e) {
      Log.i("POS_LOG", e.getMessage());
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.