Skip to content
Permalink
Browse files

Trim transparent edges from network logos

  • Loading branch information
chrisbanes committed Nov 28, 2019
1 parent 50cecb9 commit 93941c2b6039148c5fa248071595cd1f34864600
@@ -23,6 +23,7 @@ import app.tivi.data.entities.ShowTmdbImage
import app.tivi.data.entities.TmdbImageEntity
import coil.api.loadAny
import coil.transform.RoundedCornersTransformation
import coil.transform.Transformation

@BindingAdapter(
"tmdbBackdropPath",
@@ -94,8 +95,14 @@ fun ImageView.loadImage(
target(saturatingTarget)
listener(saturatingTarget)
}

val transformations = ArrayList<Transformation>()
if (cornerRadius > 0) {
transformations(RoundedCornersTransformation(cornerRadius))
transformations += RoundedCornersTransformation(cornerRadius)
}
if (image?.type == ImageType.LOGO) {
transformations += TrimTransparentEdgesTransformation()
}
transformations(transformations)
}
}
@@ -0,0 +1,97 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.tivi.common.imageloading

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Rect
import androidx.core.graphics.alpha
import coil.bitmappool.BitmapPool
import coil.transform.Transformation

/**
* A [Transformation] that trims transparent edges from an image.
*/
class TrimTransparentEdgesTransformation : Transformation {
override fun key(): String = TrimTransparentEdgesTransformation::class.java.name

override suspend fun transform(pool: BitmapPool, input: Bitmap): Bitmap {
val inputWidth = input.width
val inputHeight = input.height

var firstX = 0
var firstY = 0
var lastX = inputWidth
var lastY = inputHeight

val pixels = IntArray(inputWidth * inputHeight)
input.getPixels(pixels, 0, inputWidth, 0, 0, inputWidth, inputHeight)

loop@
for (x in 0 until inputWidth) {
for (y in 0 until inputHeight) {
if (pixels[x + y * inputWidth].alpha > 0) {
firstX = x
break@loop
}
}
}

loop@
for (y in 0 until inputHeight) {
for (x in firstX until inputWidth) {
if (pixels[x + y * inputWidth].alpha > 0) {
firstY = y
break@loop
}
}
}

loop@
for (x in inputWidth - 1 downTo firstX) {
for (y in inputHeight - 1 downTo firstY) {
if (pixels[x + y * inputWidth].alpha > 0) {
lastX = x
break@loop
}
}
}

loop@
for (y in inputHeight - 1 downTo firstY) {
for (x in inputWidth - 1 downTo firstX) {
if (pixels[x + y * inputWidth].alpha > 0) {
lastY = y
break@loop
}
}
}

if (firstX == 0 && firstY == 0 && lastX == inputWidth && lastY == inputHeight) {
return input
}

val bitmap = pool.get(1 + lastX - firstX, 1 + lastY - firstY, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)

val src = Rect(firstX, firstY, firstX + bitmap.width, firstY + bitmap.height)
val dst = Rect(0, 0, bitmap.width, bitmap.height)

canvas.drawBitmap(input, src, dst, null)
return bitmap
}
}

0 comments on commit 93941c2

Please sign in to comment.
You can’t perform that action at this time.