/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * AndroidWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./android/image/RasterImage.java * * * * This file is part of AndroidWorld. * * * * AndroidWorld is free software: you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation, either version * * 3 of the License, or (at your option) any later version. * * * * AndroidWorld is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with AndroidWorld. If not, see . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package android.image; import android.graphics.*; /** Represents a raster Image drawn into a Buffer. Because rotating and * scaling arbitrary shapes/images is difficult, we can rasterize, then * apply effects. It also allows us to merge images (i.e., overlays) * into a single image to make drawing more efficient. */ public class RasterImage extends Image{ static final Paint paint = new Paint(); static{ paint.setAntiAlias(true); } protected Bitmap img; protected int w; protected int h; /** Construct a RasterImage with the given width/height */ public RasterImage(int w, int h){ super(w, h); Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); //Canvas c = new Canvas(bm); //c.drawRect(0, 0, w, h, Image.CLEAR); this.init(bm); } /** Construct a RasterImage with the given Bitmap */ public RasterImage(Bitmap img){ super(img.getWidth(), img.getHeight()); this.init(img); } /** Initialize this FromFile with the given Bitmap */ protected void init(Bitmap img){ this.img = img; w = img.getWidth(); h = img.getHeight(); this.pinholeX = w/2; this.pinholeY = h/2; } /** Draw this image into a Graphics */ public void paint(Canvas c, int xx, int yy){ c.drawBitmap(img, (float)(xx-pinholeX), (float)(yy-pinholeY), paint); } /** Return the width of this Image */ public int width(){ return w; } /** Return the height of this Image */ public int height(){ return h; } /** Get the Graphics2D associated with this RasterImage */ public Canvas getCanvas(){ Canvas c = new Canvas(this.img); return c; } /** Get the java.awt.Color of the pixel at the given x/y */ public int getPixel(int x, int y){ return img.getPixel(x, y); } /** Get a String of the Color pixel at the given x/y. E.g., * "#00FF00" would be returned if the color * of the pixel at the given x/y is blue. */ public String getPixelAsString(int x, int y){ int rgb = img.getPixel(x, y); return ColorDatabase.makeColor(rgb); } /** Set the pixel at the given x/y to the given String Color */ public void setPixel(int x, int y, String color){ this.img.setPixel(x, y, ColorDatabase.colorToARGB(color)); } /** Set the pixel at the given x/y to the given RGB intensities. * Intensities must be between 0 and 255, inclusive.*/ public void setPixel(int x, int y, int red, int green, int blue){ this.setPixel(x, y, 255, red, green, blue); } /** Set the pixel at the given x/y to the given ARGB intensities. * Intensities must be between 0 and 255, inclusive.*/ public void setPixel(int x, int y, int alpha, int red, int green, int blue){ this.img.setPixel(x, y, ColorDatabase.colorToARGB(alpha, red, green, blue)); } /** Set the pixel at the given x/y to the given RGB intensities. * Intensities must be between 0 and 1.0, inclusive.*/ public void setPixel(int x, int y, double red, double green, double blue){ this.setPixel(x, y, 1.0, red, green, blue); } /** Set the pixel at the given x/y to the given ARGB intensities. * Intensities must be between 0 and 1.0, inclusive.*/ public void setPixel(int x, int y, double alpha, double red, double green, double blue){ this.setPixel(x, y, (int)(alpha*255), (int)(red*255), (int)(green*255), (int)(blue*255)); } }