fix: correct DNG gain map sampling and refine shadow lift LUT generation

This commit is contained in:
zhoubinjia committed 2026-05-12 17:01:36 +08:00
1 parent 3d1be62d74
commit 3b00f8f4a0
2 files changed
+35 -19

No files matched your search

+17 -9
View File
@@ -517,26 +517,34 @@ static bool applySupportedDngGainMaps(LibRaw &rawProcessor, const float blackLev
: rawWidth;
auto *rawImage = rawProcessor.imgdata.rawdata.raw_image;
const float colScale = static_cast<float>(gainMaps[0].mapPointsH - 1) /
std::max(1, rawWidth);
const float rowScale = static_cast<float>(gainMaps[0].mapPointsV - 1) /
std::max(1, rawHeight);
const DngGainMap *mapByParity[2][2] = {};
for (const auto &gainMap : gainMaps) {
mapByParity[gainMap.top & 1u][gainMap.left & 1u] = &gainMap;
}
const int activeWidth = std::max(1, static_cast<int>(rawProcessor.imgdata.sizes.width));
const int activeHeight = std::max(1, static_cast<int>(rawProcessor.imgdata.sizes.height));
const int leftMargin = static_cast<int>(rawProcessor.imgdata.sizes.left_margin);
const int topMargin = static_cast<int>(rawProcessor.imgdata.sizes.top_margin);
for (int y = 0; y < rawHeight; ++y) {
const float ys = static_cast<float>(y) * rowScale;
const float rowBlack[2] = {blackLevels[rawProcessor.FC(y, 0)], blackLevels[rawProcessor.FC(y, 1)]};
float xs = 0.0f;
for (int x = 0; x < rawWidth; ++x, xs += colScale) {
const float normY = (static_cast<float>(y - topMargin) + 0.5f) / static_cast<float>(activeHeight);
for (int x = 0; x < rawWidth; ++x) {
const DngGainMap *gainMap = mapByParity[y & 1][x & 1];
if (!gainMap) {
continue;
}
const float gain = sampleDngGainMapBilinear(*gainMap, xs, ys);
const float normX = (static_cast<float>(x - leftMargin) + 0.5f) / static_cast<float>(activeWidth);
const float spacingH = static_cast<float>(gainMap->mapSpacingH);
const float spacingV = static_cast<float>(gainMap->mapSpacingV);
const float mapX = spacingH > 0.0f ? (normX - static_cast<float>(gainMap->mapOriginH)) / spacingH : 0.0f;
const float mapY = spacingV > 0.0f ? (normY - static_cast<float>(gainMap->mapOriginV)) / spacingV : 0.0f;
const float gain = sampleDngGainMapBilinear(*gainMap, mapX, mapY);
const float black = rowBlack[x & 1];
float corrected = (static_cast<float>(rawImage[y * rawPitch + x]) - black) * gain + black;
corrected = std::max(corrected, 0.0f);
@@ -5,14 +5,13 @@ import android.graphics.Bitmap
import android.graphics.Rect
import android.media.Image
import android.opengl.*
import android.util.Log
import com.hinnka.mycamera.data.ContentRepository
import androidx.core.graphics.createBitmap
import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.data.ContentRepository
import com.hinnka.mycamera.ml.SharedDepthEstimator
import com.hinnka.mycamera.utils.BitmapUtils
import com.hinnka.mycamera.utils.PLog
import com.hinnka.mycamera.utils.RawProcessor
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.withContext
@@ -23,12 +22,10 @@ import java.nio.FloatBuffer
import java.nio.ShortBuffer
import java.util.concurrent.Executors
import kotlin.math.abs
import kotlin.math.ln
import kotlin.math.pow
import kotlin.math.sqrt
import android.opengl.Matrix as GlMatrix
import androidx.core.graphics.createBitmap
import kotlin.math.ln
import kotlin.math.pow
/**
* RAW 图像解马赛克处理器
@@ -1873,10 +1870,16 @@ class RawDemosaicProcessor {
val combinedLut = if (shadowLift > 0f) {
FloatArray(baseCurve.size) { i ->
val v = baseCurve[i]
val liftBase = v.toDouble().pow(0.75).toFloat() - v
val blackToe = (v / 0.01f).coerceAtMost(1.0f)
val lift = shadowLift * liftBase * (1.0f - v) * blackToe
(v + lift * 1.5f).coerceIn(0f, 1f)
val gammaPower = 0.75f
val shadowRangeEnd = 0.6f
val blackProtectStart = 0.02f
val black = 0.004f
val liftBase = (v.pow(gammaPower) - v).coerceAtLeast(0f)
val shadowMask = smoothstep(shadowRangeEnd, 0.02f, v)
val blackProtect = smoothstep(black, blackProtectStart, v)
val liftStrength = 1.35f
val lift = shadowLift * liftBase * liftStrength * shadowMask * blackProtect
(v + lift - black).coerceIn(0f, 1f)
}
} else {
baseCurve
@@ -1928,6 +1931,11 @@ class RawDemosaicProcessor {
checkGlError("renderCombinedPass")
}
private fun smoothstep(edge0: Float, edge1: Float, x: Float): Float {
val t = ((x - edge0) / (edge1 - edge0)).coerceIn(0f, 1f)
return t * t * (3f - 2f * t)
}
private fun logProgramLinkResult(program: Int, name: String): Boolean {
if (program == 0) {
PLog.e(TAG, "$name creation failed")