Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2017 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 
     17 @file:Suppress("NOTHING_TO_INLINE")
     18 
     19 package androidx.core.graphics
     20 
     21 import android.graphics.Bitmap
     22 import android.graphics.Canvas
     23 import android.graphics.ColorSpace
     24 import androidx.annotation.ColorInt
     25 import androidx.annotation.RequiresApi
     26 
     27 /**
     28  * Creates a new [Canvas] to draw on this bitmap and executes the specified
     29  * [block] on the newly created canvas. Example:
     30  *
     31  * ```
     32  * return Bitmap.createBitmap().applyCanvas {
     33  *    drawLine()
     34  *    translate()
     35  *    drawRect()
     36  * }
     37  * ```
     38  */
     39 inline fun Bitmap.applyCanvas(block: Canvas.() -> Unit): Bitmap {
     40     val c = Canvas(this)
     41     c.block()
     42     return this
     43 }
     44 
     45 /**
     46  * Returns the value of the pixel at the specified location. The returned value
     47  * is a [color int][android.graphics.Color] in the sRGB color space.
     48  */
     49 inline operator fun Bitmap.get(x: Int, y: Int) = getPixel(x, y)
     50 
     51 /**
     52  * Writes the specified [color int][android.graphics.Color] into the bitmap
     53  * (assuming it is mutable) at the specified `(x, y)` coordinate. The specified
     54  * color is converted from sRGB to the bitmap's color space if needed.
     55  */
     56 inline operator fun Bitmap.set(x: Int, y: Int, @ColorInt color: Int) = setPixel(x, y, color)
     57 
     58 /**
     59  * Creates a new bitmap, scaled from this bitmap, when possible. If the specified
     60  * [width] and [height] are the same as the current width and height of this bitmap,
     61  * this bitmap is returned and no new bitmap is created.
     62  *
     63  * @param width The new bitmap's desired width
     64  * @param height The new bitmap's desired height
     65  * @param filter `true` if the source should be filtered (`true` by default)
     66  *
     67  * @return The new scaled bitmap or the source bitmap if no scaling is required.
     68  */
     69 inline fun Bitmap.scale(width: Int, height: Int, filter: Boolean = true): Bitmap {
     70     return Bitmap.createScaledBitmap(this, width, height, filter)
     71 }
     72 
     73 /**
     74  * Returns a mutable bitmap with the specified [width] and [height]. A config
     75  * can be optionally specified. If not, the default config is [Bitmap.Config.ARGB_8888].
     76  *
     77  * @param width The new bitmap's desired width
     78  * @param height The new bitmap's desired height
     79  * @param config The new bitmap's desired [config][Bitmap.Config]
     80  *
     81  * @return A new bitmap with the specified dimensions and config
     82  */
     83 inline fun createBitmap(
     84     width: Int,
     85     height: Int,
     86     config: Bitmap.Config = Bitmap.Config.ARGB_8888
     87 ): Bitmap {
     88     return Bitmap.createBitmap(width, height, config)
     89 }
     90 
     91 /**
     92  * Returns a mutable bitmap with the specified [width] and [height]. The config,
     93  * transparency and color space can optionally be specified. They respectively
     94  * default to [Bitmap.Config.ARGB_8888], `true` and [sRGB][ColorSpace.Named.SRGB].
     95  *
     96  * @param width The new bitmap's desired width
     97  * @param height The new bitmap's desired height
     98  * @param config The new bitmap's desired [config][Bitmap.Config]
     99  * @param hasAlpha Whether the new bitmap is opaque or not
    100  * @param colorSpace The new bitmap's color space
    101  *
    102  * @return A new bitmap with the specified dimensions and config
    103  */
    104 @RequiresApi(26)
    105 inline fun createBitmap(
    106     width: Int,
    107     height: Int,
    108     config: Bitmap.Config = Bitmap.Config.ARGB_8888,
    109     hasAlpha: Boolean = true,
    110     colorSpace: ColorSpace = ColorSpace.get(ColorSpace.Named.SRGB)
    111 ): Bitmap {
    112     return Bitmap.createBitmap(width, height, config, hasAlpha, colorSpace)
    113 }
    114