fix: hdr effect

This commit is contained in:
Hinnka committed 2026-04-06 19:15:39 +08:00
1 parent ed9d1d3198
commit b0074224a6
8 files changed
+124 -15

No files matched your search

+2 -1
View File
@@ -3,7 +3,8 @@
"allow": [
"Bash(wc -l:*)",
"Bash(./gradlew :app:compileDefaultDebugKotlin)",
"Bash(./gradlew :app:compileDebugKotlin)"
"Bash(./gradlew :app:compileDebugKotlin)",
"Bash(grep:*)"
]
}
}
@@ -72,6 +72,8 @@ import com.hinnka.mycamera.viewmodel.GalleryViewModel
import com.hinnka.mycamera.lut.creator.LutCreatorScreen
import com.hinnka.mycamera.lut.creator.LutCreatorViewModel
import com.hinnka.mycamera.utils.DeviceUtil
import com.hinnka.mycamera.gallery.PhotoManager
import com.hinnka.mycamera.utils.PLog
/**
* 路由常量
@@ -135,6 +137,10 @@ class MainActivity : ComponentActivity() {
applyPreferredWindowColorMode()
OrientationObserver.observe(this)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
PhotoManager.hdrSdrRatio = display?.hdrSdrRatio ?: 0f
}
PLog.d("MainActivity", "hdrSdrRatio=${PhotoManager.hdrSdrRatio}")
// 检查权限
hasPermissions = permissions.all {
@@ -78,6 +78,9 @@ object PhotoManager {
val processingScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
private val gainmapProducer = UnifiedGainmapProducer()
@Volatile
var hdrSdrRatio: Float = 0f
private val detailHdrBuildJobs = ConcurrentHashMap<String, Job>()
private val _detailHdrReadyEvents = MutableSharedFlow<String>(extraBufferCapacity = 16)
val detailHdrReadyEvents: SharedFlow<String> = _detailHdrReadyEvents.asSharedFlow()
@@ -103,7 +103,8 @@ class PhotoProcessor(
sdrBase = sdrBitmap,
hdrReference = HdrBuffer(hdrReferenceBitmap, "hlg_sidecar_rgba16"),
sourceKind = SourceKind.HLG_CAPTURE,
confidence = 0.75f
confidence = 0.75f,
displayHdrSdrRatio = readDisplayHdrSdrRatio()
)
}
@@ -154,7 +155,8 @@ class PhotoProcessor(
sdrBase = sdrBitmap,
hdrReference = hdrReferenceBitmap?.let { HdrBuffer(it, "hlg_bt2020_linear") },
sourceKind = SourceKind.HLG_CAPTURE,
confidence = source.confidence
confidence = source.confidence,
displayHdrSdrRatio = readDisplayHdrSdrRatio()
)
} finally {
YuvProcessor.free(data)
@@ -178,7 +180,8 @@ class PhotoProcessor(
sdrBase = sdrBitmap,
hdrReference = null,
sourceKind = SourceKind.SDR_BITMAP,
confidence = 0.35f
confidence = 0.35f,
displayHdrSdrRatio = readDisplayHdrSdrRatio()
)
}
@@ -189,6 +192,8 @@ class PhotoProcessor(
return hlgImageProcessor.isHlgCapture(metadata)
}
private fun readDisplayHdrSdrRatio(): Float = PhotoManager.hdrSdrRatio
suspend fun prepareUltraHdrSourceFromRawResult(
context: Context,
photoId: String?,
@@ -199,6 +204,7 @@ class PhotoProcessor(
chromaNoiseReduction: Float = 0f,
applyMirror: Boolean = false,
): GainmapSourceSet? = withContext(Dispatchers.IO) {
val displayHdrSdrRatio = readDisplayHdrSdrRatio()
val finalSharpening = metadata.sharpening ?: (if (metadata.isImported) 0f else sharpening)
val finalNoiseReduction = metadata.noiseReduction ?: (if (metadata.isImported) 0f else noiseReduction)
val finalChromaNoiseReduction =
@@ -260,7 +266,8 @@ class PhotoProcessor(
)
},
sourceKind = SourceKind.RAW,
confidence = 0.8f
confidence = 0.8f,
displayHdrSdrRatio = displayHdrSdrRatio
)
}
@@ -16,6 +16,7 @@ class EstimatedSdrGainmapProducer : GainmapProducer {
if (source.sourceKind != SourceKind.SDR_BITMAP) return null
val gainmapBitmap = createGainmapBitmap(source.sdrBase) ?: return null
val fullHdrRatio = source.displayHdrSdrRatio.takeIf { it > 1f } ?: DEFAULT_FULL_HDR_RATIO
val gainmap = Gainmap(gainmapBitmap).apply {
setRatioMin(MIN_GAIN_RATIO, MIN_GAIN_RATIO, MIN_GAIN_RATIO)
setRatioMax(MAX_GAIN_RATIO, MAX_GAIN_RATIO, MAX_GAIN_RATIO)
@@ -23,7 +24,7 @@ class EstimatedSdrGainmapProducer : GainmapProducer {
setEpsilonSdr(EPSILON, EPSILON, EPSILON)
setEpsilonHdr(EPSILON, EPSILON, EPSILON)
setMinDisplayRatioForHdrTransition(1.02f)
setDisplayRatioForFullHdr(1.8f)
setDisplayRatioForFullHdr(fullHdrRatio)
}
return GainmapResult(
@@ -72,10 +73,36 @@ class EstimatedSdrGainmapProducer : GainmapProducer {
}
}
contents.copyPixelsFromBuffer(ByteBuffer.wrap(pixels))
val blurred = blurGainmap(pixels, width, height)
contents.copyPixelsFromBuffer(ByteBuffer.wrap(blurred))
return contents
}
private fun blurGainmap(pixels: ByteArray, width: Int, height: Int): ByteArray {
val r = BLUR_RADIUS
val temp = ByteArray(width * height)
for (y in 0 until height) {
for (x in 0 until width) {
var sum = 0
for (dx in -r..r) {
sum += pixels[y * width + (x + dx).coerceIn(0, width - 1)].toInt() and 0xFF
}
temp[y * width + x] = (sum / (2 * r + 1)).toByte()
}
}
val result = ByteArray(width * height)
for (y in 0 until height) {
for (x in 0 until width) {
var sum = 0
for (dy in -r..r) {
sum += temp[(y + dy).coerceIn(0, height - 1) * width + x].toInt() and 0xFF
}
result[y * width + x] = (sum / (2 * r + 1)).toByte()
}
}
return result
}
private fun srgbToLinear(value: Float): Float {
return if (value <= 0.04045f) {
value / 12.92f
@@ -95,8 +122,8 @@ class EstimatedSdrGainmapProducer : GainmapProducer {
private const val MIN_GAIN_RATIO = 1.0f
private const val MAX_GAIN_RATIO = 7.0f
private const val EPSILON = 1e-4f
private const val HIGHLIGHT_START = 0.6f
private const val HIGHLIGHT_END = 0.95f
private const val HIGHLIGHT_START = 0.7f
private const val HIGHLIGHT_END = 1f
private const val PEAK_START = 0.72f
private const val PEAK_END = 1.0f
private const val CLIP_START = 0.96f
@@ -106,5 +133,7 @@ class EstimatedSdrGainmapProducer : GainmapProducer {
private const val BASE_BOOST = 0.55f
private const val PEAK_POWER = 0.7f
private const val PEAK_BOOST = 1.5f
private const val BLUR_RADIUS = 3
private const val DEFAULT_FULL_HDR_RATIO = 1.8f
}
}
@@ -19,6 +19,7 @@ data class GainmapSourceSet(
val hdrReference: HdrBuffer? = null,
val sourceKind: SourceKind,
val confidence: Float = 1.0f,
val displayHdrSdrRatio: Float = 0f,
)
data class GainmapResult(
@@ -3,6 +3,8 @@ package com.hinnka.mycamera.hdr
import android.graphics.Bitmap
import android.graphics.Gainmap
import android.os.Build
import android.util.Log
import com.hinnka.mycamera.utils.PLog
import java.nio.ByteBuffer
import kotlin.math.ln
import kotlin.math.pow
@@ -24,6 +26,7 @@ class HlgGainmapProducer : GainmapProducer {
}
val gainmapBitmap = createGainmapBitmap(sdrBase, alignedHdr) ?: return null
val fullHdrRatio = source.displayHdrSdrRatio.takeIf { it > 1f } ?: DEFAULT_FULL_HDR_RATIO
val gainmap = Gainmap(gainmapBitmap).apply {
setRatioMin(MIN_GAIN_RATIO, MIN_GAIN_RATIO, MIN_GAIN_RATIO)
setRatioMax(MAX_GAIN_RATIO, MAX_GAIN_RATIO, MAX_GAIN_RATIO)
@@ -31,7 +34,7 @@ class HlgGainmapProducer : GainmapProducer {
setEpsilonSdr(EPSILON, EPSILON, EPSILON)
setEpsilonHdr(EPSILON, EPSILON, EPSILON)
setMinDisplayRatioForHdrTransition(1.0f)
setDisplayRatioForFullHdr(1.45f)
setDisplayRatioForFullHdr(fullHdrRatio)
}
return GainmapResult(
@@ -60,7 +63,8 @@ class HlgGainmapProducer : GainmapProducer {
val rawRatio = (hdrLuma / (sdrLuma + EPSILON)).coerceIn(MIN_GAIN_RATIO, MAX_GAIN_RATIO)
val hdrEligibility = smoothstep(HDR_ELIGIBILITY_START, HDR_ELIGIBILITY_END, hdrLuma)
val deltaMask = smoothstep(DELTA_START, DELTA_END, hdrLuma - sdrLuma)
val selectiveWeight = (hdrEligibility * deltaMask).pow(WEIGHT_POWER)
val highlightMask = smoothstep(0.7f, 1.0f, sdrLuma)
val selectiveWeight = (hdrEligibility * deltaMask * highlightMask).pow(WEIGHT_POWER)
val targetRatio = (1.0f + (rawRatio - 1.0f) * selectiveWeight)
.coerceIn(MIN_GAIN_RATIO, MAX_GAIN_RATIO)
val encoded = ((ln(targetRatio / MIN_GAIN_RATIO) / logRatioSpan) * 255.0f)
@@ -71,10 +75,36 @@ class HlgGainmapProducer : GainmapProducer {
}
}
contents.copyPixelsFromBuffer(ByteBuffer.wrap(pixels))
val blurred = blurGainmap(pixels, width, height)
contents.copyPixelsFromBuffer(ByteBuffer.wrap(blurred))
return contents
}
private fun blurGainmap(pixels: ByteArray, width: Int, height: Int): ByteArray {
val r = BLUR_RADIUS
val temp = ByteArray(width * height)
for (y in 0 until height) {
for (x in 0 until width) {
var sum = 0
for (dx in -r..r) {
sum += pixels[y * width + (x + dx).coerceIn(0, width - 1)].toInt() and 0xFF
}
temp[y * width + x] = (sum / (2 * r + 1)).toByte()
}
}
val result = ByteArray(width * height)
for (y in 0 until height) {
for (x in 0 until width) {
var sum = 0
for (dy in -r..r) {
sum += temp[(y + dy).coerceIn(0, height - 1) * width + x].toInt() and 0xFF
}
result[y * width + x] = (sum / (2 * r + 1)).toByte()
}
}
return result
}
private fun sampleSdrLuma(bitmap: Bitmap, x: Int, y: Int): Float {
val c = bitmap.getColor(x, y)
val r = srgbToLinear(c.red())
@@ -112,5 +142,7 @@ class HlgGainmapProducer : GainmapProducer {
private const val DELTA_START = 0.03f
private const val DELTA_END = 0.30f
private const val WEIGHT_POWER = 1.2f
private const val BLUR_RADIUS = 3
private const val DEFAULT_FULL_HDR_RATIO = 1.45f
}
}
@@ -24,6 +24,7 @@ class RawGainmapProducer : GainmapProducer {
}
val gainmapBitmap = createGainmapBitmap(sdrBase, alignedHdr) ?: return null
val fullHdrRatio = source.displayHdrSdrRatio.takeIf { it > 1f } ?: DEFAULT_FULL_HDR_RATIO
val gainmap = Gainmap(gainmapBitmap).apply {
setRatioMin(MIN_GAIN_RATIO, MIN_GAIN_RATIO, MIN_GAIN_RATIO)
setRatioMax(MAX_GAIN_RATIO, MAX_GAIN_RATIO, MAX_GAIN_RATIO)
@@ -31,7 +32,7 @@ class RawGainmapProducer : GainmapProducer {
setEpsilonSdr(EPSILON, EPSILON, EPSILON)
setEpsilonHdr(EPSILON, EPSILON, EPSILON)
setMinDisplayRatioForHdrTransition(1.0f)
setDisplayRatioForFullHdr(1.35f)
setDisplayRatioForFullHdr(fullHdrRatio)
}
return GainmapResult(
@@ -61,7 +62,8 @@ class RawGainmapProducer : GainmapProducer {
val rawRatio = (hdrLuma / (sdrLuma + EPSILON)).coerceIn(MIN_GAIN_RATIO, MAX_GAIN_RATIO)
val hdrEligibility = smoothstep(HDR_ELIGIBILITY_START, HDR_ELIGIBILITY_END, hdrLuma)
val deltaMask = smoothstep(DELTA_START, DELTA_END, hdrLuma - sdrLuma)
val selectiveWeight = (hdrEligibility * deltaMask).pow(WEIGHT_POWER)
val highlightMask = smoothstep(0.7f, 1.0f, sdrLuma)
val selectiveWeight = (hdrEligibility * deltaMask * highlightMask).pow(WEIGHT_POWER)
val targetRatio = (1.0f + (rawRatio - 1.0f) * selectiveWeight)
.coerceIn(MIN_GAIN_RATIO, MAX_GAIN_RATIO)
val encoded = ((ln(targetRatio / MIN_GAIN_RATIO) / logRatioSpan) * 255.0f)
@@ -72,10 +74,36 @@ class RawGainmapProducer : GainmapProducer {
}
}
contents.copyPixelsFromBuffer(ByteBuffer.wrap(pixels))
val blurred = blurGainmap(pixels, width, height)
contents.copyPixelsFromBuffer(ByteBuffer.wrap(blurred))
return contents
}
private fun blurGainmap(pixels: ByteArray, width: Int, height: Int): ByteArray {
val r = BLUR_RADIUS
val temp = ByteArray(width * height)
for (y in 0 until height) {
for (x in 0 until width) {
var sum = 0
for (dx in -r..r) {
sum += pixels[y * width + (x + dx).coerceIn(0, width - 1)].toInt() and 0xFF
}
temp[y * width + x] = (sum / (2 * r + 1)).toByte()
}
}
val result = ByteArray(width * height)
for (y in 0 until height) {
for (x in 0 until width) {
var sum = 0
for (dy in -r..r) {
sum += temp[(y + dy).coerceIn(0, height - 1) * width + x].toInt() and 0xFF
}
result[y * width + x] = (sum / (2 * r + 1)).toByte()
}
}
return result
}
private fun sampleSdrLuma(bitmap: Bitmap, x: Int, y: Int): Float {
val c = bitmap.getColor(x, y)
val r = srgbToLinear(c.red())
@@ -114,5 +142,7 @@ class RawGainmapProducer : GainmapProducer {
private const val DELTA_START = 0.02f
private const val DELTA_END = 0.22f
private const val WEIGHT_POWER = 1.15f
private const val BLUR_RADIUS = 3
private const val DEFAULT_FULL_HDR_RATIO = 1.35f
}
}