1282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/* 2282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Copyright (C) 2010 The Android Open Source Project 3282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 4282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); 5282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * you may not use this file except in compliance with the License. 6282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * You may obtain a copy of the License at 7282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 8282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 9282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 10282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Unless required by applicable law or agreed to in writing, software 11282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, 12282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * See the License for the specific language governing permissions and 14282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * limitations under the License. 15282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 16282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 17282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskipackage android.graphics; 18282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 19282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.LayoutLog; 20491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Guptaimport com.android.ide.common.rendering.api.RenderResources; 21491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Guptaimport com.android.ide.common.rendering.api.ResourceValue; 22282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.Bridge; 23491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Guptaimport com.android.layoutlib.bridge.android.BridgeContext; 24282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.impl.DelegateManager; 25491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Guptaimport com.android.layoutlib.bridge.impl.RenderAction; 26282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.resources.Density; 27491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Guptaimport com.android.resources.ResourceType; 28282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.tools.layoutlib.annotations.LayoutlibDelegate; 29282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 30476e582d2ffdf25102d4c55f8c242baa3d21d37fDeepanshu Guptaimport android.annotation.Nullable; 31282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.Bitmap.Config; 32282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.os.Parcel; 33282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 34282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.awt.Graphics2D; 35282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.awt.image.BufferedImage; 36282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.File; 37282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.IOException; 38282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.InputStream; 39282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.OutputStream; 40282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.nio.Buffer; 41282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.util.Arrays; 4270f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Guptaimport java.util.EnumSet; 4370f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Guptaimport java.util.Set; 44282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 45282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport javax.imageio.ImageIO; 46491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Guptaimport libcore.util.NativeAllocationRegistry_Delegate; 47282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 48282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/** 49282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Delegate implementing the native methods of android.graphics.Bitmap 50282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 51282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Through the layoutlib_create tool, the original native methods of Bitmap have been replaced 52282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * by calls to methods of the same name in this delegate class. 53282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 54282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * This class behaves like the original native implementation, but in Java, keeping previously 55282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * native data into its own objects and mapping them to int that are sent back and forth between 56282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * it and the original Bitmap class. 57282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 58282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @see DelegateManager 59282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 60282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 61282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskipublic final class Bitmap_Delegate { 62282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 6326e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta 6470f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta public enum BitmapCreateFlags { 6570f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta PREMULTIPLIED, MUTABLE 6670f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta } 6770f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta 68282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // ---- delegate manager ---- 69282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private static final DelegateManager<Bitmap_Delegate> sManager = 70adf62b3ebc8ae5e3e76cf6cfce978e8d1ca5bf8eDiego Perez new DelegateManager<>(Bitmap_Delegate.class); 71491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta private static long sFinalizer = -1; 72282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 73282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // ---- delegate helper data ---- 74282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 75282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // ---- delegate data ---- 76282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private final Config mConfig; 77491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta private final BufferedImage mImage; 78282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private boolean mHasAlpha = true; 79282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private boolean mHasMipMap = false; // TODO: check the default. 8026e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta private boolean mIsPremultiplied = true; 81282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private int mGenerationId = 0; 82282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 83282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 84282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // ---- Public Helper methods ---- 85282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 86282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 87282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Returns the native delegate associated to a given an int referencing a {@link Bitmap} object. 88282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 8988a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath public static Bitmap_Delegate getDelegate(long native_bitmap) { 90282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return sManager.getDelegate(native_bitmap); 91282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 92282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 93d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta @Nullable 94d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta public static Bitmap_Delegate getDelegate(@Nullable Bitmap bitmap) { 95d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta // refSkPixelRef is a hack to get the native pointer: see #nativeRefPixelRef() 96d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta return bitmap == null ? null : getDelegate(bitmap.refSkPixelRef()); 97d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta } 98d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta 99282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 100282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Creates and returns a {@link Bitmap} initialized with the given file content. 101282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 102282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param input the file from which to read the bitmap content 103282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param isMutable whether the bitmap is mutable 104282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param density the density associated with the bitmap 105282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 106282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @see Bitmap#isMutable() 107282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @see Bitmap#getDensity() 108282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 109282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public static Bitmap createBitmap(File input, boolean isMutable, Density density) 110282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski throws IOException { 11170f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return createBitmap(input, getPremultipliedBitmapCreateFlags(isMutable), density); 11270f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta } 113bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta 11470f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta /** 11570f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * Creates and returns a {@link Bitmap} initialized with the given file content. 11670f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * 11770f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @param input the file from which to read the bitmap content 11870f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @param density the density associated with the bitmap 11970f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * 120bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta * @see Bitmap#isPremultiplied() 12170f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @see Bitmap#isMutable() 12270f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @see Bitmap#getDensity() 12370f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta */ 124491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta private static Bitmap createBitmap(File input, Set<BitmapCreateFlags> createFlags, 12570f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta Density density) throws IOException { 126282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // create a delegate with the content of the file. 127491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta BufferedImage image = ImageIO.read(input); 128491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta if (image == null && input.exists()) { 129491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta // There was a problem decoding the image, or the decoder isn't registered. Webp maybe. 130491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta // Replace with a broken image icon. 131491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta BridgeContext currentContext = RenderAction.getCurrentContext(); 132491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta if (currentContext != null) { 133491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta RenderResources resources = currentContext.getRenderResources(); 134491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta ResourceValue broken = resources.getFrameworkResource(ResourceType.DRAWABLE, 135491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta "ic_menu_report_image"); 136491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta File brokenFile = new File(broken.getValue()); 137491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta if (brokenFile.exists()) { 138491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta image = ImageIO.read(brokenFile); 139491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta } 140491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta } 141491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta } 142491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.ARGB_8888); 143bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta 14470f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return createBitmap(delegate, createFlags, density.getDpiValue()); 145282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 146282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 147282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 148282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Creates and returns a {@link Bitmap} initialized with the given stream content. 149282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 150282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param input the stream from which to read the bitmap content 151282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param isMutable whether the bitmap is mutable 152282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param density the density associated with the bitmap 153282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 154282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @see Bitmap#isMutable() 155282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @see Bitmap#getDensity() 156282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 157282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public static Bitmap createBitmap(InputStream input, boolean isMutable, Density density) 158282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski throws IOException { 15970f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return createBitmap(input, getPremultipliedBitmapCreateFlags(isMutable), density); 16070f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta } 16170f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta 16270f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta /** 16370f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * Creates and returns a {@link Bitmap} initialized with the given stream content. 16470f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * 16570f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @param input the stream from which to read the bitmap content 16670f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @param density the density associated with the bitmap 16770f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * 168bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta * @see Bitmap#isPremultiplied() 16970f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @see Bitmap#isMutable() 17070f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @see Bitmap#getDensity() 17170f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta */ 17270f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta public static Bitmap createBitmap(InputStream input, Set<BitmapCreateFlags> createFlags, 17370f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta Density density) throws IOException { 174282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // create a delegate with the content of the stream. 175282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = new Bitmap_Delegate(ImageIO.read(input), Config.ARGB_8888); 176282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 17770f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return createBitmap(delegate, createFlags, density.getDpiValue()); 178282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 179282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 180282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 181282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Creates and returns a {@link Bitmap} initialized with the given {@link BufferedImage} 182282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 183282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param image the bitmap content 184282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param isMutable whether the bitmap is mutable 185282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param density the density associated with the bitmap 186282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 187282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @see Bitmap#isMutable() 188282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @see Bitmap#getDensity() 189282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 1909173c8a2e90a48959dedc9d4aa415482f5014844Deepanshu Gupta public static Bitmap createBitmap(BufferedImage image, boolean isMutable, Density density) { 19170f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return createBitmap(image, getPremultipliedBitmapCreateFlags(isMutable), density); 19270f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta } 19370f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta 19470f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta /** 19570f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * Creates and returns a {@link Bitmap} initialized with the given {@link BufferedImage} 19670f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * 19770f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @param image the bitmap content 19870f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @param density the density associated with the bitmap 19970f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * 200bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta * @see Bitmap#isPremultiplied() 20170f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @see Bitmap#isMutable() 20270f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta * @see Bitmap#getDensity() 20370f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta */ 20470f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta public static Bitmap createBitmap(BufferedImage image, Set<BitmapCreateFlags> createFlags, 2059173c8a2e90a48959dedc9d4aa415482f5014844Deepanshu Gupta Density density) { 206282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // create a delegate with the given image. 207282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.ARGB_8888); 208282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 20970f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return createBitmap(delegate, createFlags, density.getDpiValue()); 210282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 211282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 212d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta private static int getBufferedImageType() { 213282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return BufferedImage.TYPE_INT_ARGB; 214282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 215282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 216282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 217282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Returns the {@link BufferedImage} used by the delegate of the given {@link Bitmap}. 218282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 219282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public BufferedImage getImage() { 220282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return mImage; 221282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 222282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 223282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 224282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Returns the Android bitmap config. Note that this not the config of the underlying 225282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Java2D bitmap. 226282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 227282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public Config getConfig() { 228282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return mConfig; 229282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 230282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 231282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 232282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Returns the hasAlpha rendering hint 233282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @return true if the bitmap alpha should be used at render time 234282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 235282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public boolean hasAlpha() { 236282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return mHasAlpha && mConfig != Config.RGB_565; 237282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 238282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 239282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 240282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Update the generationId. 241282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 242282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @see Bitmap#getGenerationId() 243282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 244282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public void change() { 245282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski mGenerationId++; 246282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 247282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 248282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // ---- native methods ---- 249282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 250282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 251282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /*package*/ static Bitmap nativeCreate(int[] colors, int offset, int stride, int width, 25270f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta int height, int nativeConfig, boolean isMutable) { 253d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta int imageType = getBufferedImageType(); 254282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 255282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // create the image 256282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski BufferedImage image = new BufferedImage(width, height, imageType); 257282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 258282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (colors != null) { 259282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski image.setRGB(0, 0, width, height, colors, offset, stride); 260282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 261282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 262282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // create a delegate with the content of the stream. 263282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.nativeToConfig(nativeConfig)); 264282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 26570f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return createBitmap(delegate, getPremultipliedBitmapCreateFlags(isMutable), 266bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta Bitmap.getDefaultDensity()); 267282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 268282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 269282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 27088a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static Bitmap nativeCopy(long srcBitmap, int nativeConfig, boolean isMutable) { 271282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate srcBmpDelegate = sManager.getDelegate(srcBitmap); 272282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (srcBmpDelegate == null) { 273282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 274282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 275282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 276282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski BufferedImage srcImage = srcBmpDelegate.getImage(); 277282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 278282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int width = srcImage.getWidth(); 279282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int height = srcImage.getHeight(); 280282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 281d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta int imageType = getBufferedImageType(); 282282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 283282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // create the image 284282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski BufferedImage image = new BufferedImage(width, height, imageType); 285282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 286282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // copy the source image into the image. 287282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int[] argb = new int[width * height]; 288282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski srcImage.getRGB(0, 0, width, height, argb, 0, width); 289282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski image.setRGB(0, 0, width, height, argb, 0, width); 290282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 291282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // create a delegate with the content of the stream. 292282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.nativeToConfig(nativeConfig)); 293282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 29470f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return createBitmap(delegate, getPremultipliedBitmapCreateFlags(isMutable), 29570f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta Bitmap.getDefaultDensity()); 296282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 297282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 298282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 299a5e6298b35fa212a7f20740174c553ce4079cce2Deepanshu Gupta /*package*/ static Bitmap nativeCopyAshmem(long nativeSrcBitmap) { 300a5e6298b35fa212a7f20740174c553ce4079cce2Deepanshu Gupta // Unused method; no implementation provided. 301a5e6298b35fa212a7f20740174c553ce4079cce2Deepanshu Gupta assert false; 302a5e6298b35fa212a7f20740174c553ce4079cce2Deepanshu Gupta return null; 303a5e6298b35fa212a7f20740174c553ce4079cce2Deepanshu Gupta } 304a5e6298b35fa212a7f20740174c553ce4079cce2Deepanshu Gupta 305a5e6298b35fa212a7f20740174c553ce4079cce2Deepanshu Gupta @LayoutlibDelegate 306d7d2119fe37f48e4157c3bc471c1153544cf5604Diego Perez /*package*/ static Bitmap nativeCopyAshmemConfig(long nativeSrcBitmap, int nativeConfig) { 307d7d2119fe37f48e4157c3bc471c1153544cf5604Diego Perez // Unused method; no implementation provided. 308d7d2119fe37f48e4157c3bc471c1153544cf5604Diego Perez assert false; 309d7d2119fe37f48e4157c3bc471c1153544cf5604Diego Perez return null; 310d7d2119fe37f48e4157c3bc471c1153544cf5604Diego Perez } 311d7d2119fe37f48e4157c3bc471c1153544cf5604Diego Perez 312d7d2119fe37f48e4157c3bc471c1153544cf5604Diego Perez @LayoutlibDelegate 313491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta /*package*/ static long nativeGetNativeFinalizer() { 314491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta synchronized (Bitmap_Delegate.class) { 315491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta if (sFinalizer == -1) { 316491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta sFinalizer = NativeAllocationRegistry_Delegate.createFinalizer(sManager::removeJavaReferenceFor); 317491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta } 318491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta return sFinalizer; 319491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta } 320282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 321282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 322282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 323bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta /*package*/ static boolean nativeRecycle(long nativeBitmap) { 324adf62b3ebc8ae5e3e76cf6cfce978e8d1ca5bf8eDiego Perez // In our case reycle() is a no-op. We will let the finalizer to dispose the bitmap. 325bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta return true; 326bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta } 327bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta 328bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta @LayoutlibDelegate 329bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta /*package*/ static void nativeReconfigure(long nativeBitmap, int width, int height, 330bb5d0cc4369590ce892cca2f717f5d5568c5f655Deepanshu Gupta int config, int allocSize, boolean isPremultiplied) { 331bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED, 332bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta "Bitmap.reconfigure() is not supported", null /*data*/); 333282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 334282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 335282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 33688a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static boolean nativeCompress(long nativeBitmap, int format, int quality, 337282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski OutputStream stream, byte[] tempStorage) { 338282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED, 339282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "Bitmap.compress() is not supported", null /*data*/); 340282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return true; 341282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 342282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 343282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 34488a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static void nativeErase(long nativeBitmap, int color) { 345282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the delegate from the native int. 346282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 347282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate == null) { 348282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return; 349282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 350282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 351282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski BufferedImage image = delegate.mImage; 352282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 353282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Graphics2D g = image.createGraphics(); 354282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 355282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski g.setColor(new java.awt.Color(color, true)); 356282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 357282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski g.fillRect(0, 0, image.getWidth(), image.getHeight()); 358282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } finally { 359282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski g.dispose(); 360282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 361282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 362282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 363282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 36488a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static int nativeRowBytes(long nativeBitmap) { 365282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the delegate from the native int. 366282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 367282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate == null) { 368282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return 0; 369282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 370282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 371282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return delegate.mImage.getWidth(); 372282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 373282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 374282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 37588a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static int nativeConfig(long nativeBitmap) { 376282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the delegate from the native int. 377282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 378282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate == null) { 379282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return 0; 380282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 381282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 382282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return delegate.mConfig.nativeInt; 383282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 384282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 385282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 38688a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static boolean nativeHasAlpha(long nativeBitmap) { 387282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the delegate from the native int. 388282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 389d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta return delegate == null || delegate.mHasAlpha; 390282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 391282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 392282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 393282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 39488a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static boolean nativeHasMipMap(long nativeBitmap) { 395282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the delegate from the native int. 396282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 397d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta return delegate == null || delegate.mHasMipMap; 398282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 399282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 400282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 401282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 40226e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta /*package*/ static int nativeGetPixel(long nativeBitmap, int x, int y) { 403282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the delegate from the native int. 404282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 405282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate == null) { 406282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return 0; 407282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 408282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 409282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return delegate.mImage.getRGB(x, y); 410282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 411282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 412282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 41388a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static void nativeGetPixels(long nativeBitmap, int[] pixels, int offset, 41426e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta int stride, int x, int y, int width, int height) { 415282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 416282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate == null) { 417282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return; 418282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 419282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 420282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski delegate.getImage().getRGB(x, y, width, height, pixels, offset, stride); 421282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 422282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 423282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 424282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 42526e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta /*package*/ static void nativeSetPixel(long nativeBitmap, int x, int y, int color) { 426282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 427282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate == null) { 428282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return; 429282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 430282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 431282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski delegate.getImage().setRGB(x, y, color); 432282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 433282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 434282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 43588a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static void nativeSetPixels(long nativeBitmap, int[] colors, int offset, 43626e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta int stride, int x, int y, int width, int height) { 437282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 438282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate == null) { 439282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return; 440282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 441282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 442282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski delegate.getImage().setRGB(x, y, width, height, colors, offset, stride); 443282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 444282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 445282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 44688a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static void nativeCopyPixelsToBuffer(long nativeBitmap, Buffer dst) { 447282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // FIXME implement native delegate 448282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, 449282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "Bitmap.copyPixelsToBuffer is not supported.", null, null /*data*/); 450282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 451282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 452282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 45388a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static void nativeCopyPixelsFromBuffer(long nb, Buffer src) { 454282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // FIXME implement native delegate 455282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, 456282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "Bitmap.copyPixelsFromBuffer is not supported.", null, null /*data*/); 457282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 458282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 459282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 46088a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static int nativeGenerationId(long nativeBitmap) { 461282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 462282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate == null) { 463282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return 0; 464282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 465282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 466282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return delegate.mGenerationId; 467282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 468282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 469282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 470282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /*package*/ static Bitmap nativeCreateFromParcel(Parcel p) { 471282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // This is only called by Bitmap.CREATOR (Parcelable.Creator<Bitmap>), which is only 472282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // used during aidl call so really this should not be called. 473282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED, 474282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "AIDL is not suppored, and therefore Bitmaps cannot be created from parcels.", 475282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski null /*data*/); 476282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 477282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 478282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 479282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 48088a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static boolean nativeWriteToParcel(long nativeBitmap, boolean isMutable, 481282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int density, Parcel p) { 482282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // This is only called when sending a bitmap through aidl, so really this should not 483282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // be called. 484282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED, 485282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "AIDL is not suppored, and therefore Bitmaps cannot be written to parcels.", 486282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski null /*data*/); 487282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 488282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 489282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 490282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 49188a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static Bitmap nativeExtractAlpha(long nativeBitmap, long nativePaint, 492282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int[] offsetXY) { 493282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate bitmap = sManager.getDelegate(nativeBitmap); 494282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (bitmap == null) { 495282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 496282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 497282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 498282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the paint which can be null if nativePaint is 0. 499282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Paint_Delegate paint = Paint_Delegate.getDelegate(nativePaint); 500282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 501282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (paint != null && paint.getMaskFilter() != null) { 502282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER, 503282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "MaskFilter not supported in Bitmap.extractAlpha", 504282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski null, null /*data*/); 505282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 506282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 507282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int alpha = paint != null ? paint.getAlpha() : 0xFF; 508282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski BufferedImage image = createCopy(bitmap.getImage(), BufferedImage.TYPE_INT_ARGB, alpha); 509282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 510282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // create the delegate. The actual Bitmap config is only an alpha channel 511282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.ALPHA_8); 512282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 513282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // the density doesn't matter, it's set by the Java method. 51470f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return createBitmap(delegate, EnumSet.of(BitmapCreateFlags.MUTABLE), 515282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Density.DEFAULT_DENSITY /*density*/); 516282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 517282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 518282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 51926e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta /*package*/ static boolean nativeIsPremultiplied(long nativeBitmap) { 52026e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta // get the delegate from the native int. 52126e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 52226e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta return delegate != null && delegate.mIsPremultiplied; 52326e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta 52426e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta } 52526e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta 52626e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta @LayoutlibDelegate 52726e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta /*package*/ static void nativeSetPremultiplied(long nativeBitmap, boolean isPremul) { 52826e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta // get the delegate from the native int. 52926e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 53026e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta if (delegate == null) { 53126e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta return; 53226e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta } 53326e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta 53426e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta delegate.mIsPremultiplied = isPremul; 53526e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta } 53626e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta 53726e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta @LayoutlibDelegate 53826e32ea559da98dcdebaefb74d4f3c1a5ce705c3Deepanshu Gupta /*package*/ static void nativeSetHasAlpha(long nativeBitmap, boolean hasAlpha, 539e05bb956ce429618fd4f971a9dc708b9313c59eaDeepanshu Gupta boolean isPremul) { 540282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the delegate from the native int. 541282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 542282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate == null) { 543282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return; 544282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 545282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 546282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski delegate.mHasAlpha = hasAlpha; 547282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 548282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 549282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 55088a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static void nativeSetHasMipMap(long nativeBitmap, boolean hasMipMap) { 551282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the delegate from the native int. 552282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap); 553282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate == null) { 554282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return; 555282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 556282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 557282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski delegate.mHasMipMap = hasMipMap; 558282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 559282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 560282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @LayoutlibDelegate 56188a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath /*package*/ static boolean nativeSameAs(long nb0, long nb1) { 562282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate1 = sManager.getDelegate(nb0); 563282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate1 == null) { 564282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 565282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 566282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 567282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap_Delegate delegate2 = sManager.getDelegate(nb1); 568282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate2 == null) { 569282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 570282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 571282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 572282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski BufferedImage image1 = delegate1.getImage(); 573282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski BufferedImage image2 = delegate2.getImage(); 574282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate1.mConfig != delegate2.mConfig || 575282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski image1.getWidth() != image2.getWidth() || 576282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski image1.getHeight() != image2.getHeight()) { 577282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 578282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 579282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 580282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the internal data 581282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int w = image1.getWidth(); 582282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int h = image2.getHeight(); 583282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int[] argb1 = new int[w*h]; 584282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int[] argb2 = new int[w*h]; 585282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 586282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski image1.getRGB(0, 0, w, h, argb1, 0, w); 587282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski image2.getRGB(0, 0, w, h, argb2, 0, w); 588282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 589282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // compares 590282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (delegate1.mConfig == Config.ALPHA_8) { 591282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // in this case we have to manually compare the alpha channel as the rest is garbage. 592282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski final int length = w*h; 593282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski for (int i = 0 ; i < length ; i++) { 594282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if ((argb1[i] & 0xFF000000) != (argb2[i] & 0xFF000000)) { 595282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 596282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 597282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 598282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return true; 599282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 600282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 601282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return Arrays.equals(argb1, argb2); 602282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 603282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 604d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta // Only used by AssetAtlasService, which we don't care about. 605d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta @LayoutlibDelegate 606d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta /*package*/ static long nativeRefPixelRef(long nativeBitmap) { 607d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta // Hack: This is called by Bitmap.refSkPixelRef() and LayoutLib uses that method to get 608d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta // the native pointer from a Bitmap. So, we return nativeBitmap here. 609d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta return nativeBitmap; 610d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta } 611d77b9ed7dcc42efca33b225c4594a30aab9e709cDeepanshu Gupta 612282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // ---- Private delegate/helper methods ---- 613282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 614282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private Bitmap_Delegate(BufferedImage image, Config config) { 615282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski mImage = image; 616282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski mConfig = config; 617282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 618282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 61970f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta private static Bitmap createBitmap(Bitmap_Delegate delegate, 62070f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta Set<BitmapCreateFlags> createFlags, int density) { 621282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get its native_int 62288a8364c386c694f7ad56662ef89713dbf7c9d63Narayan Kamath long nativeInt = sManager.addNewDelegate(delegate); 623282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 62470f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta int width = delegate.mImage.getWidth(); 62570f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta int height = delegate.mImage.getHeight(); 62670f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta boolean isMutable = createFlags.contains(BitmapCreateFlags.MUTABLE); 62770f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta boolean isPremultiplied = createFlags.contains(BitmapCreateFlags.PREMULTIPLIED); 62870f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta 629282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // and create/return a new Bitmap with it 63070f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return new Bitmap(nativeInt, null /* buffer */, width, height, density, isMutable, 63170f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta isPremultiplied, null /*ninePatchChunk*/, null /* layoutBounds */); 632282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 633282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 63470f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta private static Set<BitmapCreateFlags> getPremultipliedBitmapCreateFlags(boolean isMutable) { 635bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta Set<BitmapCreateFlags> createFlags = EnumSet.of(BitmapCreateFlags.PREMULTIPLIED); 63670f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta if (isMutable) { 63770f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta createFlags.add(BitmapCreateFlags.MUTABLE); 63870f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta } 63970f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta return createFlags; 64070f5cc1d5bb9c67781fa6e076e21547547301a3bDeepanshu Gupta } 641bd28e2d9d3c2a7e80babd39471d5788843a48d4aDeepanshu Gupta 642282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 643282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Creates and returns a copy of a given BufferedImage. 644282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * <p/> 645282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * if alpha is different than 255, then it is applied to the alpha channel of each pixel. 646282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 647282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param image the image to copy 648282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param imageType the type of the new image 649282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param alpha an optional alpha modifier 650282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @return a new BufferedImage 651282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 652282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /*package*/ static BufferedImage createCopy(BufferedImage image, int imageType, int alpha) { 653282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int w = image.getWidth(); 654282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int h = image.getHeight(); 655282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 656282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski BufferedImage result = new BufferedImage(w, h, imageType); 657282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 658282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int[] argb = new int[w * h]; 659282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski image.getRGB(0, 0, image.getWidth(), image.getHeight(), argb, 0, image.getWidth()); 660282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 661282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (alpha != 255) { 662282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski final int length = argb.length; 663282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski for (int i = 0 ; i < length; i++) { 664282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int a = (argb[i] >>> 24 * alpha) / 255; 665282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski argb[i] = (a << 24) | (argb[i] & 0x00FFFFFF); 666282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 667282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 668282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 669282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski result.setRGB(0, 0, w, h, argb, 0, w); 670282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 671282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return result; 672282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 673282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 674282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski} 675