blob: 701613611e3473b4f4f5acb6463f0b3154d4591a [file] [log] [blame]
Adam Lesinski282e1812014-01-23 18:17:42 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
19import com.android.ide.common.rendering.api.LayoutLog;
20import com.android.layoutlib.bridge.Bridge;
21import com.android.layoutlib.bridge.impl.DelegateManager;
22import com.android.resources.Density;
23import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
24
25import android.graphics.Bitmap.Config;
26import android.os.Parcel;
27
28import java.awt.Graphics2D;
29import java.awt.image.BufferedImage;
30import java.io.File;
31import java.io.IOException;
32import java.io.InputStream;
33import java.io.OutputStream;
34import java.nio.Buffer;
35import java.util.Arrays;
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -070036import java.util.EnumSet;
37import java.util.Set;
Adam Lesinski282e1812014-01-23 18:17:42 -080038
39import javax.imageio.ImageIO;
40
41/**
42 * Delegate implementing the native methods of android.graphics.Bitmap
43 *
44 * Through the layoutlib_create tool, the original native methods of Bitmap have been replaced
45 * by calls to methods of the same name in this delegate class.
46 *
47 * This class behaves like the original native implementation, but in Java, keeping previously
48 * native data into its own objects and mapping them to int that are sent back and forth between
49 * it and the original Bitmap class.
50 *
51 * @see DelegateManager
52 *
53 */
54public final class Bitmap_Delegate {
55
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -070056 public enum BitmapCreateFlags {
57 PREMULTIPLIED, MUTABLE
58 }
59
Adam Lesinski282e1812014-01-23 18:17:42 -080060 // ---- delegate manager ----
61 private static final DelegateManager<Bitmap_Delegate> sManager =
62 new DelegateManager<Bitmap_Delegate>(Bitmap_Delegate.class);
63
64 // ---- delegate helper data ----
65
66 // ---- delegate data ----
67 private final Config mConfig;
68 private BufferedImage mImage;
69 private boolean mHasAlpha = true;
70 private boolean mHasMipMap = false; // TODO: check the default.
71 private int mGenerationId = 0;
72
73
74 // ---- Public Helper methods ----
75
76 /**
77 * Returns the native delegate associated to a given {@link Bitmap_Delegate} object.
78 */
79 public static Bitmap_Delegate getDelegate(Bitmap bitmap) {
80 return sManager.getDelegate(bitmap.mNativeBitmap);
81 }
82
83 /**
84 * Returns the native delegate associated to a given an int referencing a {@link Bitmap} object.
85 */
Narayan Kamath88a83642014-01-27 14:24:16 +000086 public static Bitmap_Delegate getDelegate(long native_bitmap) {
Adam Lesinski282e1812014-01-23 18:17:42 -080087 return sManager.getDelegate(native_bitmap);
88 }
89
90 /**
91 * Creates and returns a {@link Bitmap} initialized with the given file content.
92 *
93 * @param input the file from which to read the bitmap content
94 * @param isMutable whether the bitmap is mutable
95 * @param density the density associated with the bitmap
96 *
97 * @see Bitmap#isMutable()
98 * @see Bitmap#getDensity()
99 */
100 public static Bitmap createBitmap(File input, boolean isMutable, Density density)
101 throws IOException {
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700102 return createBitmap(input, getPremultipliedBitmapCreateFlags(isMutable), density);
103 }
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800104
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700105 /**
106 * Creates and returns a {@link Bitmap} initialized with the given file content.
107 *
108 * @param input the file from which to read the bitmap content
109 * @param density the density associated with the bitmap
110 *
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800111 * @see Bitmap#isPremultiplied()
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700112 * @see Bitmap#isMutable()
113 * @see Bitmap#getDensity()
114 */
115 public static Bitmap createBitmap(File input, Set<BitmapCreateFlags> createFlags,
116 Density density) throws IOException {
Adam Lesinski282e1812014-01-23 18:17:42 -0800117 // create a delegate with the content of the file.
118 Bitmap_Delegate delegate = new Bitmap_Delegate(ImageIO.read(input), Config.ARGB_8888);
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800119
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700120 return createBitmap(delegate, createFlags, density.getDpiValue());
Adam Lesinski282e1812014-01-23 18:17:42 -0800121 }
122
123 /**
124 * Creates and returns a {@link Bitmap} initialized with the given stream content.
125 *
126 * @param input the stream from which to read the bitmap content
127 * @param isMutable whether the bitmap is mutable
128 * @param density the density associated with the bitmap
129 *
130 * @see Bitmap#isMutable()
131 * @see Bitmap#getDensity()
132 */
133 public static Bitmap createBitmap(InputStream input, boolean isMutable, Density density)
134 throws IOException {
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700135 return createBitmap(input, getPremultipliedBitmapCreateFlags(isMutable), density);
136 }
137
138 /**
139 * Creates and returns a {@link Bitmap} initialized with the given stream content.
140 *
141 * @param input the stream from which to read the bitmap content
142 * @param createFlags
143 * @param density the density associated with the bitmap
144 *
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800145 * @see Bitmap#isPremultiplied()
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700146 * @see Bitmap#isMutable()
147 * @see Bitmap#getDensity()
148 */
149 public static Bitmap createBitmap(InputStream input, Set<BitmapCreateFlags> createFlags,
150 Density density) throws IOException {
Adam Lesinski282e1812014-01-23 18:17:42 -0800151 // create a delegate with the content of the stream.
152 Bitmap_Delegate delegate = new Bitmap_Delegate(ImageIO.read(input), Config.ARGB_8888);
153
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700154 return createBitmap(delegate, createFlags, density.getDpiValue());
Adam Lesinski282e1812014-01-23 18:17:42 -0800155 }
156
157 /**
158 * Creates and returns a {@link Bitmap} initialized with the given {@link BufferedImage}
159 *
160 * @param image the bitmap content
161 * @param isMutable whether the bitmap is mutable
162 * @param density the density associated with the bitmap
163 *
164 * @see Bitmap#isMutable()
165 * @see Bitmap#getDensity()
166 */
167 public static Bitmap createBitmap(BufferedImage image, boolean isMutable,
168 Density density) throws IOException {
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700169 return createBitmap(image, getPremultipliedBitmapCreateFlags(isMutable), density);
170 }
171
172 /**
173 * Creates and returns a {@link Bitmap} initialized with the given {@link BufferedImage}
174 *
175 * @param image the bitmap content
176 * @param createFlags
177 * @param density the density associated with the bitmap
178 *
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800179 * @see Bitmap#isPremultiplied()
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700180 * @see Bitmap#isMutable()
181 * @see Bitmap#getDensity()
182 */
183 public static Bitmap createBitmap(BufferedImage image, Set<BitmapCreateFlags> createFlags,
184 Density density) throws IOException {
Adam Lesinski282e1812014-01-23 18:17:42 -0800185 // create a delegate with the given image.
186 Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.ARGB_8888);
187
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700188 return createBitmap(delegate, createFlags, density.getDpiValue());
Adam Lesinski282e1812014-01-23 18:17:42 -0800189 }
190
191 /**
192 * Returns the {@link BufferedImage} used by the delegate of the given {@link Bitmap}.
193 */
194 public static BufferedImage getImage(Bitmap bitmap) {
195 // get the delegate from the native int.
196 Bitmap_Delegate delegate = sManager.getDelegate(bitmap.mNativeBitmap);
197 if (delegate == null) {
198 return null;
199 }
200
201 return delegate.mImage;
202 }
203
204 public static int getBufferedImageType(int nativeBitmapConfig) {
205 switch (Config.nativeToConfig(nativeBitmapConfig)) {
206 case ALPHA_8:
207 return BufferedImage.TYPE_INT_ARGB;
208 case RGB_565:
209 return BufferedImage.TYPE_INT_ARGB;
210 case ARGB_4444:
211 return BufferedImage.TYPE_INT_ARGB;
212 case ARGB_8888:
213 return BufferedImage.TYPE_INT_ARGB;
214 }
215
216 return BufferedImage.TYPE_INT_ARGB;
217 }
218
219 /**
220 * Returns the {@link BufferedImage} used by the delegate of the given {@link Bitmap}.
221 */
222 public BufferedImage getImage() {
223 return mImage;
224 }
225
226 /**
227 * Returns the Android bitmap config. Note that this not the config of the underlying
228 * Java2D bitmap.
229 */
230 public Config getConfig() {
231 return mConfig;
232 }
233
234 /**
235 * Returns the hasAlpha rendering hint
236 * @return true if the bitmap alpha should be used at render time
237 */
238 public boolean hasAlpha() {
239 return mHasAlpha && mConfig != Config.RGB_565;
240 }
241
242 public boolean hasMipMap() {
243 // TODO: check if more checks are required as in hasAlpha.
244 return mHasMipMap;
245 }
246 /**
247 * Update the generationId.
248 *
249 * @see Bitmap#getGenerationId()
250 */
251 public void change() {
252 mGenerationId++;
253 }
254
255 // ---- native methods ----
256
257 @LayoutlibDelegate
258 /*package*/ static Bitmap nativeCreate(int[] colors, int offset, int stride, int width,
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700259 int height, int nativeConfig, boolean isMutable) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800260 int imageType = getBufferedImageType(nativeConfig);
261
262 // create the image
263 BufferedImage image = new BufferedImage(width, height, imageType);
264
265 if (colors != null) {
266 image.setRGB(0, 0, width, height, colors, offset, stride);
267 }
268
269 // create a delegate with the content of the stream.
270 Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.nativeToConfig(nativeConfig));
271
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700272 return createBitmap(delegate, getPremultipliedBitmapCreateFlags(isMutable),
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800273 Bitmap.getDefaultDensity());
Adam Lesinski282e1812014-01-23 18:17:42 -0800274 }
275
276 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000277 /*package*/ static Bitmap nativeCopy(long srcBitmap, int nativeConfig, boolean isMutable) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800278 Bitmap_Delegate srcBmpDelegate = sManager.getDelegate(srcBitmap);
279 if (srcBmpDelegate == null) {
280 return null;
281 }
282
283 BufferedImage srcImage = srcBmpDelegate.getImage();
284
285 int width = srcImage.getWidth();
286 int height = srcImage.getHeight();
287
288 int imageType = getBufferedImageType(nativeConfig);
289
290 // create the image
291 BufferedImage image = new BufferedImage(width, height, imageType);
292
293 // copy the source image into the image.
294 int[] argb = new int[width * height];
295 srcImage.getRGB(0, 0, width, height, argb, 0, width);
296 image.setRGB(0, 0, width, height, argb, 0, width);
297
298 // create a delegate with the content of the stream.
299 Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.nativeToConfig(nativeConfig));
300
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700301 return createBitmap(delegate, getPremultipliedBitmapCreateFlags(isMutable),
302 Bitmap.getDefaultDensity());
Adam Lesinski282e1812014-01-23 18:17:42 -0800303 }
304
305 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000306 /*package*/ static void nativeDestructor(long nativeBitmap) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800307 sManager.removeJavaReferenceFor(nativeBitmap);
308 }
309
310 @LayoutlibDelegate
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800311 /*package*/ static boolean nativeRecycle(long nativeBitmap) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800312 sManager.removeJavaReferenceFor(nativeBitmap);
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800313 return true;
314 }
315
316 @LayoutlibDelegate
317 /*package*/ static void nativeReconfigure(long nativeBitmap, int width, int height,
318 int config, int allocSize) {
319 Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
320 "Bitmap.reconfigure() is not supported", null /*data*/);
Adam Lesinski282e1812014-01-23 18:17:42 -0800321 }
322
323 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000324 /*package*/ static boolean nativeCompress(long nativeBitmap, int format, int quality,
Adam Lesinski282e1812014-01-23 18:17:42 -0800325 OutputStream stream, byte[] tempStorage) {
326 Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
327 "Bitmap.compress() is not supported", null /*data*/);
328 return true;
329 }
330
331 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000332 /*package*/ static void nativeErase(long nativeBitmap, int color) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800333 // get the delegate from the native int.
334 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
335 if (delegate == null) {
336 return;
337 }
338
339 BufferedImage image = delegate.mImage;
340
341 Graphics2D g = image.createGraphics();
342 try {
343 g.setColor(new java.awt.Color(color, true));
344
345 g.fillRect(0, 0, image.getWidth(), image.getHeight());
346 } finally {
347 g.dispose();
348 }
349 }
350
351 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000352 /*package*/ static int nativeRowBytes(long nativeBitmap) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800353 // get the delegate from the native int.
354 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
355 if (delegate == null) {
356 return 0;
357 }
358
359 return delegate.mImage.getWidth();
360 }
361
362 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000363 /*package*/ static int nativeConfig(long nativeBitmap) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800364 // get the delegate from the native int.
365 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
366 if (delegate == null) {
367 return 0;
368 }
369
370 return delegate.mConfig.nativeInt;
371 }
372
373 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000374 /*package*/ static boolean nativeHasAlpha(long nativeBitmap) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800375 // get the delegate from the native int.
376 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
377 if (delegate == null) {
378 return true;
379 }
380
381 return delegate.mHasAlpha;
382 }
383
384 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000385 /*package*/ static boolean nativeHasMipMap(long nativeBitmap) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800386 // get the delegate from the native int.
387 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
388 if (delegate == null) {
389 return true;
390 }
391
392 return delegate.mHasMipMap;
393 }
394
395 @LayoutlibDelegate
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800396 /*package*/ static int nativeGetPixel(long nativeBitmap, int x, int y,
397 boolean isPremultiplied) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800398 // get the delegate from the native int.
399 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
400 if (delegate == null) {
401 return 0;
402 }
403
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800404 // TODO: Support isPremultiplied.
Adam Lesinski282e1812014-01-23 18:17:42 -0800405 return delegate.mImage.getRGB(x, y);
406 }
407
408 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000409 /*package*/ static void nativeGetPixels(long nativeBitmap, int[] pixels, int offset,
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800410 int stride, int x, int y, int width, int height, boolean isPremultiplied) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800411 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
412 if (delegate == null) {
413 return;
414 }
415
416 delegate.getImage().getRGB(x, y, width, height, pixels, offset, stride);
417 }
418
419
420 @LayoutlibDelegate
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800421 /*package*/ static void nativeSetPixel(long nativeBitmap, int x, int y, int color,
422 boolean isPremultiplied) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800423 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
424 if (delegate == null) {
425 return;
426 }
427
428 delegate.getImage().setRGB(x, y, color);
429 }
430
431 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000432 /*package*/ static void nativeSetPixels(long nativeBitmap, int[] colors, int offset,
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800433 int stride, int x, int y, int width, int height, boolean isPremultiplied) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800434 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
435 if (delegate == null) {
436 return;
437 }
438
439 delegate.getImage().setRGB(x, y, width, height, colors, offset, stride);
440 }
441
442 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000443 /*package*/ static void nativeCopyPixelsToBuffer(long nativeBitmap, Buffer dst) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800444 // FIXME implement native delegate
445 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
446 "Bitmap.copyPixelsToBuffer is not supported.", null, null /*data*/);
447 }
448
449 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000450 /*package*/ static void nativeCopyPixelsFromBuffer(long nb, Buffer src) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800451 // FIXME implement native delegate
452 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
453 "Bitmap.copyPixelsFromBuffer is not supported.", null, null /*data*/);
454 }
455
456 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000457 /*package*/ static int nativeGenerationId(long nativeBitmap) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800458 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
459 if (delegate == null) {
460 return 0;
461 }
462
463 return delegate.mGenerationId;
464 }
465
466 @LayoutlibDelegate
467 /*package*/ static Bitmap nativeCreateFromParcel(Parcel p) {
468 // This is only called by Bitmap.CREATOR (Parcelable.Creator<Bitmap>), which is only
469 // used during aidl call so really this should not be called.
470 Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
471 "AIDL is not suppored, and therefore Bitmaps cannot be created from parcels.",
472 null /*data*/);
473 return null;
474 }
475
476 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000477 /*package*/ static boolean nativeWriteToParcel(long nativeBitmap, boolean isMutable,
Adam Lesinski282e1812014-01-23 18:17:42 -0800478 int density, Parcel p) {
479 // This is only called when sending a bitmap through aidl, so really this should not
480 // be called.
481 Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
482 "AIDL is not suppored, and therefore Bitmaps cannot be written to parcels.",
483 null /*data*/);
484 return false;
485 }
486
487 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000488 /*package*/ static Bitmap nativeExtractAlpha(long nativeBitmap, long nativePaint,
Adam Lesinski282e1812014-01-23 18:17:42 -0800489 int[] offsetXY) {
490 Bitmap_Delegate bitmap = sManager.getDelegate(nativeBitmap);
491 if (bitmap == null) {
492 return null;
493 }
494
495 // get the paint which can be null if nativePaint is 0.
496 Paint_Delegate paint = Paint_Delegate.getDelegate(nativePaint);
497
498 if (paint != null && paint.getMaskFilter() != null) {
499 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER,
500 "MaskFilter not supported in Bitmap.extractAlpha",
501 null, null /*data*/);
502 }
503
504 int alpha = paint != null ? paint.getAlpha() : 0xFF;
505 BufferedImage image = createCopy(bitmap.getImage(), BufferedImage.TYPE_INT_ARGB, alpha);
506
507 // create the delegate. The actual Bitmap config is only an alpha channel
508 Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.ALPHA_8);
509
510 // the density doesn't matter, it's set by the Java method.
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700511 return createBitmap(delegate, EnumSet.of(BitmapCreateFlags.MUTABLE),
Adam Lesinski282e1812014-01-23 18:17:42 -0800512 Density.DEFAULT_DENSITY /*density*/);
513 }
514
515 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000516 /*package*/ static void nativePrepareToDraw(long nativeBitmap) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800517 // nothing to be done here.
518 }
519
520 @LayoutlibDelegate
Deepanshu Guptae05bb952014-01-28 18:55:33 -0800521 /*package*/ static void nativeSetAlphaAndPremultiplied(long nativeBitmap, boolean hasAlpha,
522 boolean isPremul) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800523 // get the delegate from the native int.
524 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
525 if (delegate == null) {
526 return;
527 }
528
529 delegate.mHasAlpha = hasAlpha;
530 }
531
532 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000533 /*package*/ static void nativeSetHasMipMap(long nativeBitmap, boolean hasMipMap) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800534 // get the delegate from the native int.
535 Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
536 if (delegate == null) {
537 return;
538 }
539
540 delegate.mHasMipMap = hasMipMap;
541 }
542
543 @LayoutlibDelegate
Narayan Kamath88a83642014-01-27 14:24:16 +0000544 /*package*/ static boolean nativeSameAs(long nb0, long nb1) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800545 Bitmap_Delegate delegate1 = sManager.getDelegate(nb0);
546 if (delegate1 == null) {
547 return false;
548 }
549
550 Bitmap_Delegate delegate2 = sManager.getDelegate(nb1);
551 if (delegate2 == null) {
552 return false;
553 }
554
555 BufferedImage image1 = delegate1.getImage();
556 BufferedImage image2 = delegate2.getImage();
557 if (delegate1.mConfig != delegate2.mConfig ||
558 image1.getWidth() != image2.getWidth() ||
559 image1.getHeight() != image2.getHeight()) {
560 return false;
561 }
562
563 // get the internal data
564 int w = image1.getWidth();
565 int h = image2.getHeight();
566 int[] argb1 = new int[w*h];
567 int[] argb2 = new int[w*h];
568
569 image1.getRGB(0, 0, w, h, argb1, 0, w);
570 image2.getRGB(0, 0, w, h, argb2, 0, w);
571
572 // compares
573 if (delegate1.mConfig == Config.ALPHA_8) {
574 // in this case we have to manually compare the alpha channel as the rest is garbage.
575 final int length = w*h;
576 for (int i = 0 ; i < length ; i++) {
577 if ((argb1[i] & 0xFF000000) != (argb2[i] & 0xFF000000)) {
578 return false;
579 }
580 }
581 return true;
582 }
583
584 return Arrays.equals(argb1, argb2);
585 }
586
587 // ---- Private delegate/helper methods ----
588
589 private Bitmap_Delegate(BufferedImage image, Config config) {
590 mImage = image;
591 mConfig = config;
592 }
593
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700594 private static Bitmap createBitmap(Bitmap_Delegate delegate,
595 Set<BitmapCreateFlags> createFlags, int density) {
Adam Lesinski282e1812014-01-23 18:17:42 -0800596 // get its native_int
Narayan Kamath88a83642014-01-27 14:24:16 +0000597 long nativeInt = sManager.addNewDelegate(delegate);
Adam Lesinski282e1812014-01-23 18:17:42 -0800598
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700599 int width = delegate.mImage.getWidth();
600 int height = delegate.mImage.getHeight();
601 boolean isMutable = createFlags.contains(BitmapCreateFlags.MUTABLE);
602 boolean isPremultiplied = createFlags.contains(BitmapCreateFlags.PREMULTIPLIED);
603
Adam Lesinski282e1812014-01-23 18:17:42 -0800604 // and create/return a new Bitmap with it
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700605 return new Bitmap(nativeInt, null /* buffer */, width, height, density, isMutable,
606 isPremultiplied, null /*ninePatchChunk*/, null /* layoutBounds */);
Adam Lesinski282e1812014-01-23 18:17:42 -0800607 }
608
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700609 private static Set<BitmapCreateFlags> getPremultipliedBitmapCreateFlags(boolean isMutable) {
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800610 Set<BitmapCreateFlags> createFlags = EnumSet.of(BitmapCreateFlags.PREMULTIPLIED);
Deepanshu Gupta70f5cc12013-09-09 12:32:19 -0700611 if (isMutable) {
612 createFlags.add(BitmapCreateFlags.MUTABLE);
613 }
614 return createFlags;
615 }
Deepanshu Guptabd28e2d2014-01-27 11:45:47 -0800616
Adam Lesinski282e1812014-01-23 18:17:42 -0800617 /**
618 * Creates and returns a copy of a given BufferedImage.
619 * <p/>
620 * if alpha is different than 255, then it is applied to the alpha channel of each pixel.
621 *
622 * @param image the image to copy
623 * @param imageType the type of the new image
624 * @param alpha an optional alpha modifier
625 * @return a new BufferedImage
626 */
627 /*package*/ static BufferedImage createCopy(BufferedImage image, int imageType, int alpha) {
628 int w = image.getWidth();
629 int h = image.getHeight();
630
631 BufferedImage result = new BufferedImage(w, h, imageType);
632
633 int[] argb = new int[w * h];
634 image.getRGB(0, 0, image.getWidth(), image.getHeight(), argb, 0, image.getWidth());
635
636 if (alpha != 255) {
637 final int length = argb.length;
638 for (int i = 0 ; i < length; i++) {
639 int a = (argb[i] >>> 24 * alpha) / 255;
640 argb[i] = (a << 24) | (argb[i] & 0x00FFFFFF);
641 }
642 }
643
644 result.setRGB(0, 0, w, h, argb, 0, w);
645
646 return result;
647 }
648
649}