Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Usage:
- // drawable1.bytesEqualTo(drawable2)
- // drawable1.pixelsEqualTo(drawable2)
- // bitmap1.bytesEqualTo(bitmap1)
- // bitmap1.pixelsEqualTo(bitmap2)
- fun <T : Drawable> T.bytesEqualTo(t: T?) = toBitmap().bytesEqualTo(t?.toBitmap(), true)
- fun <T : Drawable> T.pixelsEqualTo(t: T?) = toBitmap().pixelsEqualTo(t?.toBitmap(), true)
- fun Bitmap.bytesEqualTo(otherBitmap: Bitmap?, shouldRecycle: Boolean = false) = otherBitmap?.let { other ->
- if (width == other.width && height == other.height) {
- val res = toBytes().contentEquals(other.toBytes())
- if (shouldRecycle) {
- doRecycle().also { otherBitmap.doRecycle() }
- }
- res
- } else false
- } ?: kotlin.run { false }
- fun Bitmap.pixelsEqualTo(otherBitmap: Bitmap?, shouldRecycle: Boolean = false) = otherBitmap?.let { other ->
- if (width == other.width && height == other.height) {
- val res = Arrays.equals(toPixels(), other.toPixels())
- if (shouldRecycle) {
- doRecycle().also { otherBitmap.doRecycle() }
- }
- res
- } else false
- } ?: kotlin.run { false }
- fun Bitmap.doRecycle() {
- if (!isRecycled) recycle()
- }
- fun <T : Drawable> T.toBitmap(): Bitmap {
- if (this is BitmapDrawable) return bitmap
- val drawable: Drawable = this
- val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
- val canvas = Canvas(bitmap)
- drawable.setBounds(0, 0, canvas.width, canvas.height)
- drawable.draw(canvas)
- return bitmap
- }
- fun Bitmap.toBytes(): ByteArray = ByteArrayOutputStream().use { stream ->
- compress(Bitmap.CompressFormat.JPEG, 100, stream)
- stream.toByteArray()
- }
- fun Bitmap.toPixels() = IntArray(width * height).apply { getPixels(this, 0, width, 0, 0, width, height) }
Add Comment
Please, Sign In to add comment