Convert a drawable object into a Bitmap.
@param drawable Drawable to extract a Bitmap from.
@return A Bitmap created from the drawable parameter.
779088
public Bitmap drawableToBitmap(Drawable drawable) {
		if (drawable == null)   // Don't do anything without a proper drawable
			return null;
		else if (drawable instanceof BitmapDrawable) {  // Use the getBitmap() method instead if BitmapDrawable
			Log.i(TAG, "Bitmap drawable!");
			return ((BitmapDrawable) drawable).getBitmap();
		}

		int intrinsicWidth = drawable.getIntrinsicWidth();
		int intrinsicHeight = drawable.getIntrinsicHeight();

		if (!(intrinsicWidth > 0 && intrinsicHeight > 0))
			return null;

		try {
			// Create Bitmap object out of the drawable
			Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888);
			Canvas canvas = new Canvas(bitmap);
			drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
			drawable.draw(canvas);
			return bitmap;
		} catch (OutOfMemoryError e) {
			// Simply return null of failed bitmap creations
			Log.e(TAG, "Encountered OutOfMemoryError while generating bitmap!");
			return null;
		}
	}
950320
public Drawable createFromStream(InputStream is, String srcName) {
        return createFromResourceStream(null, is, srcName);
    }
247257
public T image(Drawable drawable) {

        if (view instanceof ImageView) {
            ImageView iv = (ImageView) view;
            iv.setImageDrawable(drawable);
        }

        return self();
    }
1127100
private void downloadBitmap(final String path, final Object into, final Transformation transformation) {
        //download the bitmap from bluetooth
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                Bitmap bitmap = getBitmap(path);
                Log.d(TAG, "bitmap from bluetooth " + path + " " + bitmap);
                if (bitmap != null) {
                    Log.d(TAG, "save bitmap " + path + " into cache");
                    saveBitmap(getKey(path), bitmap);

                    if (transformation != null) {
                        return transformAndSaveBitmap(path, transformation);
                    }
                }
                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                if (into != null)
                    returnBitmapInto(bitmap, path, into);
            }
        }.execute();
    }
1232575
public void setBackgroundDrawable(@NonNull Drawable drawable) {
    if (drawable == null) {
      throw new IllegalArgumentException("Cannot be null");
    }
    this.backgroundDrawable = drawable;
    isDecorated = true;
  }
159189
@VisibleForTesting
  @Nullable
  static CloseableReference<Bitmap> convertToBitmapReferenceAndClose(
      final @Nullable CloseableReference<CloseableImage> closeableImage) {
    try {
      if (CloseableReference.isValid(closeableImage) &&
          closeableImage.get() instanceof CloseableStaticBitmap) {

        CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage.get();
        if (closeableStaticBitmap != null) {
          // We return a clone of the underlying bitmap reference that has to be manually closed
          // and then close the passed CloseableStaticBitmap in order to preserve correct
          // cache size calculations.
          return closeableStaticBitmap.cloneUnderlyingBitmapReference();
        }
      }
      // Not a bitmap reference, so we return null
      return null;
    } finally {
      CloseableReference.closeSafely(closeableImage);
    }
  }
630096
public static Drawable getCompatDrawable(Context c, int drawableRes) {
		Drawable d = null;
		try {
			d = ContextCompat.getDrawable(c, drawableRes);
		} catch (Exception ex) {
			logException(ex);
		}
		return d;
	}
950369
public void setImageDrawable(Drawable drawable, boolean animate) {
        if (view != null) {
            ((ImageView) view).setImageDrawable(drawable);
        }
    }
1190577
private CloseableReference<Bitmap> createRainbowBitmap() {
    final int w = 256;
    final int h = 256;
    mOriginalBitmap = mPlatformBitmapFactory.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    final int[] colors = new int[w * h];
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        final float hue = 360f * j / (float) w;
        final float saturation = 2f * (h - i) / (float) h;
        final float value = 1;
        colors[i * h + j] = Color.HSVToColor(255, new float[]{hue, saturation, value});
      }
    }
    mOriginalBitmap.get().setPixels(colors, 0, w, 0, 0, w, h);

    return mOriginalBitmap;
  }
731382
public static Drawable createFromXmlInner(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException {
        Drawable drawable = null;
        final String name = parser.getName();
        try {
            Class<? extends Drawable> clazz = CLASS_MAP.get(name);
            if (clazz != null) {
                drawable = clazz.newInstance();
            } else if (name.indexOf('.') > 0) {
                drawable = (Drawable) Class.forName(name).newInstance();
            }
        } catch (Exception e) {
            throw new XmlPullParserException("Error while inflating drawable resource", parser, e);
        }
        if (drawable == null) {
            if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
                return Drawable.createFromXmlInner(r, parser, attrs, theme);
            } else {
                return Drawable.createFromXmlInner(r, parser, attrs);
            }
        }
        IMPL.inflate(drawable, r, parser, attrs, theme);
        return drawable;
    }
123706
public static RoundedBitmapDrawable fromBitmapDrawable(
      Resources res,
      BitmapDrawable bitmapDrawable) {
    return new RoundedBitmapDrawable(res, bitmapDrawable.getBitmap(), bitmapDrawable.getPaint());
  }
737471
@NonNull
    public Bitmap toBitmap() {
        if (mSizeX == -1 || mSizeY == -1) {
            actionBar();
        }

        Bitmap bitmap = Bitmap.createBitmap(
                getIntrinsicWidth(),
                getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);

        style(Paint.Style.FILL);

        Canvas canvas = new Canvas(bitmap);
        setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        draw(canvas);

        return bitmap;
    }
321868
public static MutableRoaringBitmap naive_or(@SuppressWarnings("rawtypes") Iterator bitmaps) {
    MutableRoaringBitmap answer = new MutableRoaringBitmap();
    while (bitmaps.hasNext()) {
      answer.naivelazyor((ImmutableRoaringBitmap) bitmaps.next());
    }
    answer.repairAfterLazy();
    return answer;
  }
1338436
private byte[] convert(Bitmap bitmap, String format) {
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();

        DataBuffer buffer = new DataBufferByte(bitmap.getPixels(), bitmap.getLength());
        WritableRaster writableRaster = Raster.createPackedRaster(buffer, width, height, width, MASKS, null);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        bufferedImage.setData(writableRaster);

        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            ImageIO.write(bufferedImage, format, outputStream);
            outputStream.close();
            return outputStream.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
1323904
public static Bitmap processTintTransformationMap(Bitmap transformationMap, int tintColor) {
		// tint color
		int[] t = new int[] {
				Color.red(tintColor),
				Color.green(tintColor),
				Color.blue(tintColor) };
		
		int width = transformationMap.getWidth();
		int height = transformationMap.getHeight();
		int[] pixels = new int[width * height];
		transformationMap.getPixels(pixels, 0, width, 0, 0, width, height);
		
		float[] transformation = new float[2];
		
		for (int i=0; i<pixels.length; i++) {
			int color = pixels[i];
			int alpha = Color.alpha(color);
			transformation[0] = Color.red(color) / 255f;
			transformation[1] = Color.green(color) / 255f;
			pixels[i] = applyTransformation(t, alpha, transformation);
		}
		return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
	}
578580
public synchronized void returnBitmap(Bitmap bitmap) {
        if (bitmap == null || bitmapsBySize.contains(bitmap) ) {
            return;
        } else if(getBitmapSize(bitmap) > sizeLimit || !canBePooled(bitmap)) {
            return;
        }
        bitmapsByLastUse.add(bitmap);
        int pos = Collections.binarySearch(bitmapsBySize, bitmap, BITMAP_COMPARATOR);
        if (pos < 0) {
            pos = -pos - 1;
        }
        bitmapsBySize.add(pos, bitmap);
        currentSize += getBitmapSize(bitmap);
        trim();
    }
906792
public static Drawable createFromXml(Resources r, XmlPullParser parser) throws XmlPullParserException, IOException {
        return createFromXml(r, parser, null);
    }