001package armyc2.c5isr.renderer.utilities; 002 003import android.annotation.TargetApi; 004import android.graphics.Bitmap; 005import android.graphics.Canvas; 006import android.graphics.Point; 007import android.graphics.Rect; 008import android.graphics.Bitmap.Config; 009import android.os.Build; 010 011/** 012 * Object that holds an image of the symbol and all the information 013 * needed to place the symbol on the screen. 014* 015 */ 016public class ImageInfo implements SymbolDimensionInfo{ 017 018 private Point _centerPoint = null; 019 private Rect _symbolBounds = null; 020 private Rect _imageBounds = null; 021 private Bitmap _image = null; 022 private int _byteCount = 0; 023 024 /** 025 * Creates a new Image info with copied values but a link to the original image. 026 * @param original {@link ImageInfo} 027 */ 028 public ImageInfo(ImageInfo original) 029 { 030 _centerPoint = new Point(original.getCenterPoint()); 031 _symbolBounds = new Rect(original.getSymbolBounds()); 032 _image = original.getImage(); 033 _imageBounds = new Rect(original.getImageBounds()); 034 _byteCount = original.getByteCount(); 035 } 036 037 /** 038 * Creates a new ImageInfo object 039 * @param image {@link Bitmap} 040 * @param centerPoint {@link Point} center/anchor point of the symbol within the image 041 * @param symbolBounds {@link Rect} bounds of the symbol within the image 042 */ 043 public ImageInfo(Bitmap image, Point centerPoint, Rect symbolBounds) 044 { 045 _centerPoint = centerPoint; 046 _symbolBounds = symbolBounds; 047 _image = image; 048 049 _imageBounds = RectUtilities.makeRect(0, 0, image.getWidth(), image.getHeight()); 050 051 _byteCount = image.getAllocationByteCount(); 052 053 } 054 055 /** 056 * Not a full clone. Only centerPoint and symbolBounds are copies. Bitmap is still a reference. 057 */ 058 public ImageInfo getLightClone() 059 { 060 return new ImageInfo(_image, new Point(_centerPoint), new Rect(_symbolBounds)); 061 } 062 063 public ImageInfo getClone(ImageInfo original) 064 { 065 Point centerPoint = new Point(original.getCenterPoint()); 066 Rect symbolBounds = new Rect(original.getSymbolBounds()); 067 Bitmap image = original.getImage().copy(Config.ARGB_8888, false); 068 return new ImageInfo(_image, new Point(_centerPoint), new Rect(_symbolBounds)); 069 } 070 071 public Bitmap getImage() 072 { 073 return _image; 074 } 075 076 /** 077 * The x value the image should be centered on or the "anchor point". 078 * @return {@link Integer} 079 */ 080 public int getCenterX() 081 { 082 return _centerPoint.x; 083 } 084 085 /** 086 * The y value the image should be centered on or the "anchor point". 087 * @return {@link Integer} 088 */ 089 public int getCenterY() 090 { 091 return _centerPoint.y; 092 } 093 public Point getCenterPoint() 094 { 095 return _centerPoint; 096 } 097 098 public Rect getSymbolBounds() 099 { 100 return _symbolBounds; 101 } 102 103 public Rect getImageBounds() 104 { 105 return _imageBounds; 106 } 107 108 public int getByteCount() 109 { 110 return _byteCount; 111 } 112 public ImageInfo getSquareImageInfo() 113 { 114 ImageInfo ii = null; 115 int iwidth, iheight, x, y; 116 int width = this._imageBounds.width(); 117 int height = this._imageBounds.height(); 118 119 if(this._imageBounds.width() > this._imageBounds.height()) 120 { 121 iwidth = this._imageBounds.width(); 122 iheight = this._imageBounds.width(); 123 x=0; 124 y=(iheight - height)/2; 125 } 126 else if(this._imageBounds.width() < this._imageBounds.height()) 127 { 128 iwidth = this._imageBounds.height(); 129 iheight = this._imageBounds.height(); 130 x = (iwidth - width)/2; 131 y = 0; 132 } 133 else 134 { 135 iwidth = this._imageBounds.width(); 136 iheight = this._imageBounds.height(); 137 x=0; 138 y=0; 139 } 140 141 //Draw glyphs to bitmap 142 Bitmap bmp = Bitmap.createBitmap(iwidth, iheight, Config.ARGB_8888); 143 Canvas ctx = new Canvas(bmp); 144 145 146 ctx.drawBitmap(_image,x,y,null); 147 148 //create new ImageInfo 149 Point center = new Point(_centerPoint); 150 center.offset(x,y); 151 Rect symbolBounds = new Rect(_symbolBounds); 152 symbolBounds.offset(x,y); 153 154 ii = new ImageInfo(bmp,center, symbolBounds); 155 156 157 return ii; 158 } 159}