Skip to content

Commit

Permalink
Merge pull request cashapp#526 from fcduarte/small-refactoring-image-…
Browse files Browse the repository at this point in the history
…utils

Move the thumbnail scaling to ImageUtils
  • Loading branch information
jrodbx authored Aug 1, 2022
2 parents 2734c4d + 54a227b commit b0d62ff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ class Paparazzi @JvmOverloads constructor(
private val snapshotHandler: SnapshotHandler = determineHandler(maxPercentDifference),
private val renderExtensions: Set<RenderExtension> = setOf()
) : TestRule {
private val THUMBNAIL_SIZE = 1000

private val logger = PaparazziLogger()
private lateinit var renderSession: RenderSessionImpl
private lateinit var bridgeRenderSession: RenderSession
Expand Down Expand Up @@ -370,8 +368,7 @@ class Paparazzi @JvmOverloads constructor(
}

private fun scaleImage(image: BufferedImage): BufferedImage {
val maxDimension = Math.max(image.width, image.height)
val scale = THUMBNAIL_SIZE / maxDimension.toDouble()
val scale = ImageUtils.getThumbnailScale(image)
return ImageUtils.scale(image, scale, scale)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import java.io.File
import java.io.File.separatorChar
import java.io.IOException
import javax.imageio.ImageIO
import kotlin.math.max

/**
* Utilities related to image processing.
Expand All @@ -47,7 +48,7 @@ internal object ImageUtils {
*/
private val FAIL_ON_MISSING_THUMBNAIL = true

private val THUMBNAIL_SIZE = 1000
private const val THUMBNAIL_SIZE = 1000

/** Directory where to write the thumbnails and deltas. */
private val failureDir: File
Expand All @@ -64,8 +65,7 @@ internal object ImageUtils {
image: BufferedImage,
maxPercentDifference: Double
) {
val maxDimension = Math.max(image.width, image.height)
val scale = THUMBNAIL_SIZE / maxDimension.toDouble()
val scale = getThumbnailScale(image)
val thumbnail = scale(image, scale, scale)

val `is` = ImageUtils::class.java.classLoader.getResourceAsStream(relativePath)
Expand Down Expand Up @@ -312,6 +312,11 @@ internal object ImageUtils {
}
}

fun getThumbnailScale(image: BufferedImage): Double {
val maxDimension = max(image.width, image.height)
return THUMBNAIL_SIZE / maxDimension.toDouble()
}

private fun setRenderingHints(g2: Graphics2D) {
g2.setRenderingHint(KEY_INTERPOLATION, VALUE_INTERPOLATION_BILINEAR)
g2.setRenderingHint(KEY_RENDERING, VALUE_RENDER_QUALITY)
Expand Down

0 comments on commit b0d62ff

Please sign in to comment.