fix: isz crop

This commit is contained in:
zhoubinjia committed 2026-07-30 14:49:44 +08:00
1 parent 25b7a7715e
commit d196e6dce2
4 files changed
+155 -126

No files matched your search

@@ -0,0 +1,36 @@
package com.hinnka.mycamera.raw
import android.graphics.Rect
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.camera.RawBlackBorderCrop
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class RawDefaultCropOverrideTest {
@Test
fun stackedIszPgtmUsesTheSameBoundsAsSubsequentRawRendering() {
val blackBorderDefaultCrop =
RawDefaultCropOverride.resolveRawBlackBorderDefaultCrop(
width = 6118,
height = 4594,
rotation = 90,
rawBlackBorderCrop = RawBlackBorderCrop(leftPx = 750),
metadataDefaultCrop = Rect(0, 0, 6118, 4594),
)
assertEquals(Rect(0, 0, 6118, 3844), blackBorderDefaultCrop)
assertEquals(
Rect(497, 0, 5621, 3844),
RawDefaultCropOverride.resolveOutputSourceBounds(
width = 6118,
height = 4594,
aspectRatio = AspectRatio.RATIO_4_3,
userCrop = Rect(0, 0, 6118, 4594),
metadataDefaultCrop = blackBorderDefaultCrop,
),
)
}
}
@@ -1737,28 +1737,6 @@ object GalleryManager {
)
}
private fun resolveRawStatsBounds(
width: Int,
height: Int,
defaultCrop: Rect?,
cropRegion: Rect?
): Rect? {
val imageBounds = Rect(0, 0, width.coerceAtLeast(1), height.coerceAtLeast(1))
var resolved: Rect? = null
listOf(defaultCrop, cropRegion).forEach { candidate ->
val safeCandidate = sanitizeIntersectingCrop(candidate, imageBounds) ?: return@forEach
resolved = resolved?.let { current ->
Rect(current).takeIf { it.intersect(safeCandidate) && !it.isEmpty }
} ?: safeCandidate
}
return resolved?.takeUnless { it.hasSameBounds(imageBounds) }
}
private fun sanitizeIntersectingCrop(crop: Rect?, imageBounds: Rect): Rect? {
if (crop == null || crop.isEmpty || imageBounds.isEmpty) return null
return Rect(crop).takeIf { it.intersect(imageBounds) && !it.isEmpty }
}
private fun Rect.hasSameBounds(other: Rect): Boolean {
return left == other.left && top == other.top && right == other.right && bottom == other.bottom
}
@@ -3777,12 +3755,21 @@ object GalleryManager {
captureResult: CaptureResult?,
): RawDngProfilePreparationOptions {
val profileToneMapMode = rawDngPgtmModeForMetadata(metadata)
val statsBounds = resolveRawStatsBounds(
val blackBorderDefaultCrop =
RawDefaultCropOverride.resolveRawBlackBorderDefaultCrop(
width = width,
height = height,
rotation = rotation,
rawBlackBorderCrop = metadata.rawBlackBorderCrop,
metadataDefaultCrop = defaultCrop,
)
val statsBounds = RawDefaultCropOverride.resolveOutputSourceBounds(
width = width,
height = height,
defaultCrop = defaultCrop,
cropRegion = metadata.cropRegion,
)
aspectRatio = aspectRatio,
userCrop = metadata.cropRegion,
metadataDefaultCrop = blackBorderDefaultCrop ?: defaultCrop,
).takeUnless { it.hasSameBounds(Rect(0, 0, width, height)) }
val mainFlashFired = didMainFlashFire(captureResult)
val viewfinderMatchEnabled = capturePreviewThumbnail != null &&
!mainFlashFired &&
@@ -3824,6 +3811,7 @@ object GalleryManager {
TAG,
"RAW_VIEWFINDER_BASELINE stage=DNG_PREPARE enabled=$viewfinderMatchEnabled " +
"curve=DEFAULT pgtmOnGpu=$profileGainTableRequired " +
"statsBounds=$statsBounds blackBorderDefaultCrop=$blackBorderDefaultCrop " +
"mainFlashFired=$mainFlashFired " +
"flashState=${captureResult?.get(CaptureResult.FLASH_STATE)} " +
"sourceAutoExposure=${metadata.rawAutoExposure} " +
@@ -1,10 +1,15 @@
package com.hinnka.mycamera.raw
import android.graphics.Rect
import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.camera.RawBlackBorderCrop
import com.hinnka.mycamera.utils.BitmapUtils
import kotlin.math.abs
import kotlin.math.max
internal object RawDefaultCropOverride {
private const val DEFAULT_CROP_ASPECT_TOLERANCE = 0.005f
fun resolveRawBlackBorderDefaultCrop(
width: Int,
height: Int,
@@ -58,6 +63,49 @@ internal object RawDefaultCropOverride {
}
}
/**
* Resolves the source rectangle used by RAW rendering, exposure metering and PGTM statistics.
*
* Capture-time profile generation must use this same calculation as later RAW reconstruction.
* Otherwise a shared PGTM can be trained over black-border pixels even though the renderer
* crops them from the image.
*/
fun resolveOutputSourceBounds(
width: Int,
height: Int,
aspectRatio: AspectRatio?,
userCrop: Rect?,
metadataDefaultCrop: Rect?,
): Rect {
val safeMetadataCrop = sanitizeCropWithinImage(metadataDefaultCrop, width, height)
if (safeMetadataCrop == null) {
return BitmapUtils.calculateProcessedRect(
width = width,
height = height,
aspectRatio = aspectRatio,
cropRegion = userCrop,
rotation = 0,
)
}
val safeUserCrop = sanitizeUserCrop(userCrop, width, height)
val userCropInsideMetadata = safeUserCrop
?.takeUnless { it.isFullImage(width, height) }
?.let { user ->
Rect(safeMetadataCrop).takeIf { it.intersect(user) && !it.isEmpty }
}
val baseCrop = userCropInsideMetadata ?: safeMetadataCrop
val sourceIsLandscape = baseCrop.width() >= baseCrop.height()
return if (
aspectRatio != null &&
!baseCrop.hasEquivalentAspect(aspectRatio, sourceIsLandscape)
) {
cropSourceBoundsToAspect(baseCrop, aspectRatio, sourceIsLandscape)
} else {
Rect(baseCrop)
}
}
private data class CropMargins(
val left: Int,
val top: Int,
@@ -107,6 +155,56 @@ internal object RawDefaultCropOverride {
}
}
private fun cropSourceBoundsToAspect(
bounds: Rect,
aspectRatio: AspectRatio,
sourceIsLandscape: Boolean,
): Rect {
val targetRatio = aspectRatio.getValue(sourceIsLandscape)
val sourceRatio = bounds.width().toFloat() / bounds.height().toFloat()
val cropWidth: Int
val cropHeight: Int
if (sourceRatio > targetRatio) {
cropHeight = bounds.height()
cropWidth = alignDownToEven((cropHeight * targetRatio).toInt())
} else {
cropWidth = bounds.width()
cropHeight = alignDownToEven((cropWidth / targetRatio).toInt())
}
val left = bounds.left + (bounds.width() - cropWidth).coerceAtLeast(0) / 2
val top = bounds.top + (bounds.height() - cropHeight).coerceAtLeast(0) / 2
return Rect(left, top, left + cropWidth, top + cropHeight)
}
private fun Rect.hasEquivalentAspect(
aspectRatio: AspectRatio,
sourceIsLandscape: Boolean,
): Boolean {
if (width() <= 0 || height() <= 0) return false
val targetRatio = aspectRatio.getValue(sourceIsLandscape)
val sourceRatio = width().toFloat() / height().toFloat()
return abs(sourceRatio - targetRatio) / targetRatio <= DEFAULT_CROP_ASPECT_TOLERANCE
}
private fun sanitizeUserCrop(crop: Rect?, width: Int, height: Int): Rect? {
if (crop == null || crop.isEmpty) return null
val currentIsLandscape = width >= height
val cropIsLandscape = crop.width() >= crop.height()
val alignedCrop = if (cropIsLandscape != currentIsLandscape) {
Rect(crop.top, crop.left, crop.bottom, crop.right)
} else {
Rect(crop)
}
val imageBounds = Rect(0, 0, width, height)
return alignedCrop.takeIf {
it.intersect(imageBounds) && !it.isEmpty
}
}
private fun alignDownToEven(value: Int): Int {
return if (value <= 1) value else value and 1.inv()
}
private fun Rect.isFullImage(width: Int, height: Int): Boolean {
return left == 0 && top == 0 && right == width && bottom == height
}
@@ -257,7 +257,6 @@ class RawDemosaicProcessor {
private const val DARKTABLE_FILMIC_HR_BETA_COMP = 0f
private const val DARKTABLE_FILMIC_HR_DELTA = 1f
private const val DARKTABLE_FILMIC_HR_HIGH_QUALITY_ITERATIONS = 1
private const val DNG_DEFAULT_CROP_ASPECT_TOLERANCE = 0.005f
private const val DENOISE_PROFILE_GLES31_MIN_SSBO_BYTES = 128L * 1024L * 1024L
private val BRADFORD_D65_TO_D50 = floatArrayOf(
1.0478112f, 0.0228866f, -0.0501270f,
@@ -3605,6 +3604,7 @@ class RawDemosaicProcessor {
TAG,
"GPU RAW PGTM prepared: mode=$profileToneMapMode size=${width}x$height " +
"source=${rawTextureWidth}x$rawTextureHeight " +
"statsBounds=$safeStatsBounds " +
"grid=${gridWidth}x$gridHeight samplesPerPixel=$samplesPerPixel " +
"lsc=${lensShadingLogString(metadata)} " +
"warpCount=${activeWarpParameters.size / 8} " +
@@ -4614,83 +4614,15 @@ class RawDemosaicProcessor {
cropRegion: Rect?,
metadataDefaultCrop: Rect?
): Rect {
val safeMetadataCrop = RawDefaultCropOverride.sanitizeCropWithinImage(metadataDefaultCrop, width, height)
if (safeMetadataCrop != null) {
return calculateDngDefaultCropSourceBounds(
width = width,
height = height,
metadataCrop = safeMetadataCrop,
userCrop = cropRegion,
aspectRatio = aspectRatio
)
}
return BitmapUtils.calculateProcessedRect(
width,
height,
aspectRatio,
cropRegion,
0
return RawDefaultCropOverride.resolveOutputSourceBounds(
width = width,
height = height,
aspectRatio = aspectRatio,
userCrop = cropRegion,
metadataDefaultCrop = metadataDefaultCrop,
)
}
private fun calculateDngDefaultCropSourceBounds(
width: Int,
height: Int,
metadataCrop: Rect,
userCrop: Rect?,
aspectRatio: AspectRatio?
): Rect {
val safeUserCrop = sanitizeUserCrop(userCrop, width, height)
val userCropInsideMetadata = safeUserCrop
?.takeUnless { it.isFullImage(width, height) }
?.let { user ->
Rect(metadataCrop).takeIf { it.intersect(user) && !it.isEmpty }
}
val baseCrop = userCropInsideMetadata ?: metadataCrop
val sourceIsLandscape = baseCrop.width() >= baseCrop.height()
val targetAspectRatio = aspectRatio
return if (
targetAspectRatio != null &&
!baseCrop.hasEquivalentAspect(targetAspectRatio, sourceIsLandscape)
) {
cropSourceBoundsToAspect(baseCrop, targetAspectRatio, sourceIsLandscape)
} else {
Rect(baseCrop)
}
}
private fun cropSourceBoundsToAspect(
bounds: Rect,
aspectRatio: AspectRatio,
sourceIsLandscape: Boolean
): Rect {
val targetRatio = aspectRatio.getValue(sourceIsLandscape)
val srcRatio = bounds.width().toFloat() / bounds.height().toFloat()
val cropWidth: Int
val cropHeight: Int
if (srcRatio > targetRatio) {
cropHeight = bounds.height()
cropWidth = alignDownToEven((cropHeight * targetRatio).toInt())
} else {
cropWidth = bounds.width()
cropHeight = alignDownToEven((cropWidth / targetRatio).toInt())
}
val left = bounds.left + (bounds.width() - cropWidth).coerceAtLeast(0) / 2
val top = bounds.top + (bounds.height() - cropHeight).coerceAtLeast(0) / 2
return Rect(left, top, left + cropWidth, top + cropHeight)
}
private fun Rect.hasEquivalentAspect(
aspectRatio: AspectRatio,
sourceIsLandscape: Boolean
): Boolean {
if (width() <= 0 || height() <= 0) return false
val targetRatio = aspectRatio.getValue(sourceIsLandscape)
val sourceRatio = width().toFloat() / height().toFloat()
return abs(sourceRatio - targetRatio) / targetRatio <= DNG_DEFAULT_CROP_ASPECT_TOLERANCE
}
private fun Rect.toOutputBounds(rotation: Int): Rect {
return if (rotation == 90 || rotation == 270) {
Rect(top, left, bottom, right)
@@ -4699,31 +4631,6 @@ class RawDemosaicProcessor {
}
}
private fun Rect.isFullImage(width: Int, height: Int): Boolean {
return left == 0 && top == 0 && right == width && bottom == height
}
private fun alignDownToEven(value: Int): Int {
return if (value <= 1) value else value and 1.inv()
}
private fun sanitizeUserCrop(crop: Rect?, width: Int, height: Int): Rect? {
if (crop == null || crop.isEmpty) return null
val currentIsLandscape = width >= height
val cropIsLandscape = crop.width() >= crop.height()
val alignedCrop = if (cropIsLandscape != currentIsLandscape) {
Rect(crop.top, crop.left, crop.bottom, crop.right)
} else {
Rect(crop)
}
val imageBounds = Rect(0, 0, width, height)
return if (alignedCrop.intersect(imageBounds) && !alignedCrop.isEmpty) {
alignedCrop
} else {
null
}
}
private fun sanitizeDngDefaultCrop(crop: IntArray?, width: Int, height: Int): Rect? {
if (crop == null || crop.size != 4) return null
return RawDefaultCropOverride.sanitizeCropWithinImage(