1 package fi.jyu.mit.graphics;
2
3 import java.awt.Graphics;
4 import java.awt.image.BufferedImage;
5 import java.awt.image.WritableRaster;
6 import java.io.File;
7 import java.net.URL;
8 import java.util.Hashtable;
9
10 import javax.imageio.ImageIO;
11
12
13
21 public class Bitmap extends BasicShape {
22
23 private final RPoint upperLeft;
24 int imgWidth = 0;
25 int imgHeight = 0;
26
27 private BufferedImage rawImg;
28
29
30
37 public Bitmap(double x,double y, double z, String filename) {
38 super();
39 upperLeft = new RPoint(x,y,z);
40 try {
41 if ( filename.startsWith("http") )
42 rawImg = ImageIO.read(new URL(filename));
43 else
44 rawImg = ImageIO.read(new File(filename));
45 } catch (Exception e) {
46 throw new NullPointerException("filename " + filename + " not found");
48 }
49 imgWidth = rawImg.getWidth(null);
50 imgHeight = rawImg.getHeight(null);
51 }
52
53
54
60 public Bitmap(double x,double y, String filename) {
61 this(x,y,0,filename);
62 }
63
64
65
73 public Bitmap(double x,double y, double z, int w, int h) {
74 super();
75 upperLeft = new RPoint(x,y,z);
76 rawImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
77 imgWidth = rawImg.getWidth(null);
78 imgHeight = rawImg.getHeight(null);
79 }
80
81
82
89 public Bitmap(double x,double y, int w, int h) {
90 this(x,y,0,w,h);
91 }
92
93
94
99 public static BufferedImage cloneBufferedImage(BufferedImage image) {
100 String[] pnames = image.getPropertyNames();
101 Hashtable<String, Object> cproperties = new Hashtable<String, Object>();
102 if (pnames != null) {
103 for (int i = 0; i < pnames.length; i++) {
104 cproperties.put(pnames[i], image.getProperty(pnames[i]));
105 }
106 }
107 WritableRaster wr = image.getRaster();
108 WritableRaster cwr = wr.createCompatibleWritableRaster();
109 cwr.setRect(wr);
110 BufferedImage cimage = new BufferedImage(image.getColorModel(),
111 cwr, image.isAlphaPremultiplied(), cproperties);
112 return cimage;
113 }
114
115
116
123 public Bitmap(double x,double y, double z, Bitmap bm) {
124 super();
125 upperLeft = new RPoint(x,y,z);
126 rawImg = cloneBufferedImage(bm.getRawImage());
127 imgWidth = rawImg.getWidth(null);
128 imgHeight = rawImg.getHeight(null);
129 }
130
131
132
138 public Bitmap(double x,double y, Bitmap bm) {
139 this(x,y,0,bm);
140 }
141
142
143
147 public static int brightness(int p) {
148 int r = p & 0xff;
149 p >>= 8; r += p & 0xff;
150 p >>= 8; r += p & 0xff;
151 return r / 3;
152 }
153
154
155
158 public int getWidth() {
159 return imgWidth;
160 }
161
162
163
166 public int getHeight() {
167 return imgHeight;
168 }
169
170
171
177 public int getRGB(int x, int y) {
178 return rawImg.getRGB(x,y);
179 }
180
181
182
188 public void setRGB(int x, int y, int rgb) {
189 rawImg.setRGB(x,y,rgb);
190 }
191
192
193
201 public static int toRGB(int r,int g,int b,int a) {
202 return ( a << 24 ) | (r << 16) | (g << 8) | b;
203 }
204
205
206
213 public static int packRGB(int r,int g,int b) {
214 return ( 255 << 24 ) | (r << 16) | (g << 8) | b;
215 }
216
217
218
222 public static int getRed(int c) {
223 return (c >> 16) & 0xff;
224 }
225
226
227
231 public static int getGreen(int c) {
232 return (c >> 8) & 0xff;
233 }
234
235
236
240 public static int getBlue(int c) {
241 return (c >> 0) & 0xff;
242 }
243
244
245
249 public static int getA(int c) {
250 return (c >> 24) & 0xff;
251 }
252
253
254
258 public BufferedImage getRawImage() {
259 return rawImg;
260 }
261
262
263 @Override
264 protected void drawShape(Graphics g, Matrix a) {
265 SPoint sp = new SPoint(0,0);
266 a.transform(upperLeft,sp);
267 g.drawImage(rawImg,sp.getX(),sp.getY(),null);
268 }
269
270
271
274 public void convertGrayScale() {
275 for (int y = 0; y < rawImg.getHeight(); y++) {
276 for (int x = 0; x < rawImg.getWidth(); x++) {
277 int c = rawImg.getRGB(x, y);
278 int b = Bitmap.brightness(c);
279 b = (b << 16) | (b << 8) | b;
280 rawImg.setRGB(x, y, b);
281 }
282 }
283 }
284
285
286
290 public void andPixels(int mask) {
291 for (int y = 0; y < rawImg.getHeight(); y++) {
292 for (int x = 0; x < rawImg.getWidth(); x++) {
293 int c = rawImg.getRGB(x, y);
294 rawImg.setRGB(x, y, c & mask);
295 }
296 }
297 }
298
299
300
304 public void orPixels(int mask) {
305 for (int y = 0; y < rawImg.getHeight(); y++) {
306 for (int x = 0; x < rawImg.getWidth(); x++) {
307 int c = rawImg.getRGB(x, y);
308 rawImg.setRGB(x, y, c | mask);
309 }
310 }
311 }
312
313
314
322 public int[][] getData(int ox,int oy, int w, int h) {
323 int maxx = rawImg.getWidth()-ox;
324 int maxy = rawImg.getHeight()-oy;
325 if ( ox+w < maxx ) maxx = w;
326 if ( oy+h < maxy ) maxy = h;
327
328 int[][] data = new int[maxy][maxx];
329 for (int y = 0; y < maxy; y++)
330 for (int x = 0; x < maxx; x++)
331 data[y][x] = rawImg.getRGB(x+ox, y+oy);
332 return data;
333 }
334
335
336
339 public int[][] getData() {
340 return getData(0,0,rawImg.getWidth(),rawImg.getHeight());
341 }
342
343
344
355 public void setData(int ox,int oy, int w, int h, int[][] data) {
356 int imgw = rawImg.getWidth();
358 int imgh = rawImg.getHeight();
359 int maxy = data.length;
360 if ( h < maxy ) maxy = h;
361 if ( imgh < maxy+oy ) maxy = imgh-oy;
362 for (int y=0; y<maxy;y++) {
363 int maxx = data[y].length;
364 if ( w < maxx ) maxx = w;
365 if ( imgw < maxx+ox ) maxx = imgw-ox;
366 for (int x=0; x<maxx;x++)
367 rawImg.setRGB(ox+x, oy+y, data[y][x]);
368 }
369 }
370
371
372
381 public void setData(int ox,int oy,int[][] data) {
382 setData(ox,oy,data[0].length,data.length,data);
383 }
384
385
386 }
387