feat: chroma denoise optimize
This commit is contained in:
1 parent
5553cbb224
commit
8516f3750f
7 files changed
+651
-96
No files matched your search
@@ -1,10 +1,44 @@
|
||||
package com.hinnka.mycamera.lut
|
||||
|
||||
object ChromaDenoiseDefaults {
|
||||
const val RAW_HIDDEN_BASE_STRENGTH = 0.5f
|
||||
const val RAW_HIDDEN_BASE_STRENGTH = 0.25f
|
||||
private const val MIN_ACTIVE_NOISE_BANDWIDTH = 1.0f
|
||||
private const val MAX_NOISE_BANDWIDTH = 8.0f
|
||||
private const val FULL_OUTPUT_STRENGTH_POINT = 0.25f
|
||||
private const val EDGE_GUIDANCE_START_POINT = 0.75f
|
||||
|
||||
fun rawDefaultStrength(userStrength: Float): Float {
|
||||
val clamped = userStrength.coerceIn(0f, 1f)
|
||||
return RAW_HIDDEN_BASE_STRENGTH + clamped * (1f - RAW_HIDDEN_BASE_STRENGTH)
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the bilateral kernel wide enough to recognize noise at every active
|
||||
* slider value. Once output mixing reaches full strength at 0.25, the remaining
|
||||
* slider range continues widening chroma similarity.
|
||||
*/
|
||||
fun noiseBandwidth(strength: Float): Float {
|
||||
val clamped = strength.coerceIn(0f, 1f)
|
||||
if (clamped <= 0f) return 0f
|
||||
return MIN_ACTIVE_NOISE_BANDWIDTH +
|
||||
clamped * (MAX_NOISE_BANDWIDTH - MIN_ACTIVE_NOISE_BANDWIDTH)
|
||||
}
|
||||
|
||||
/** Smoothly reaches full filter output at one quarter of the slider range. */
|
||||
fun outputStrength(strength: Float): Float {
|
||||
val t = (strength.coerceIn(0f, 1f) / FULL_OUTPUT_STRENGTH_POINT)
|
||||
.coerceIn(0f, 1f)
|
||||
return t * t * (3f - 2f * t)
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses strict edge rejection through the lower half of the slider. Above 0.5
|
||||
* the guide is relaxed smoothly so stronger settings can cross weak or noisy
|
||||
* pseudo-edges and establish enough support for dense chroma noise.
|
||||
*/
|
||||
fun edgeGuidanceRelaxation(strength: Float): Float {
|
||||
val t = ((strength.coerceIn(0f, 1f) - EDGE_GUIDANCE_START_POINT) /
|
||||
(1f - EDGE_GUIDANCE_START_POINT)).coerceIn(0f, 1f)
|
||||
return t * t * (3f - 2f * t)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,43 @@
|
||||
package com.hinnka.mycamera.lut
|
||||
|
||||
/**
|
||||
* Shared BM3D shader snippets used by bitmap chroma noise reduction.
|
||||
* Shared multi-scale edge-guided chroma denoise shader.
|
||||
*
|
||||
* Only pass0 is kept here: it is a cross-bilateral chroma filter guided by luma,
|
||||
* and does not change the existing denoiseprofile luminance/noise-reduction path.
|
||||
* RAW input is filtered in normalized camera RGB opponent space before white
|
||||
* balance and color conversion. Bitmap input retains its luma/chroma transform.
|
||||
* Above half strength, a prefiltered green/luma guide is relaxed independently
|
||||
* of the chroma bandwidth so dense color noise can regain spatial support.
|
||||
*/
|
||||
object ChromaDenoiseShaders {
|
||||
const val SIGMA_STRENGTH_AT_SLIDER_ONE = 6.0f
|
||||
val PASS_EDGE_GUIDE = """
|
||||
#version 300 es
|
||||
precision highp float;
|
||||
in vec2 vTexCoord;
|
||||
out vec4 fragColor;
|
||||
uniform sampler2D uInputTexture;
|
||||
uniform vec2 uTexelSize;
|
||||
uniform int uCameraRgbInput;
|
||||
|
||||
float guideSignal(vec3 rgb) {
|
||||
if (uCameraRgbInput != 0) {
|
||||
return rgb.g;
|
||||
}
|
||||
return dot(rgb, vec3(0.2126, 0.7152, 0.0722));
|
||||
}
|
||||
|
||||
float guideAt(vec2 coord) {
|
||||
return guideSignal(texture(uInputTexture, coord).rgb);
|
||||
}
|
||||
|
||||
void main() {
|
||||
float guide = guideAt(vTexCoord) * 0.5;
|
||||
guide += guideAt(vTexCoord + vec2( uTexelSize.x, 0.0)) * 0.125;
|
||||
guide += guideAt(vTexCoord + vec2(-uTexelSize.x, 0.0)) * 0.125;
|
||||
guide += guideAt(vTexCoord + vec2(0.0, uTexelSize.y)) * 0.125;
|
||||
guide += guideAt(vTexCoord + vec2(0.0, -uTexelSize.y)) * 0.125;
|
||||
fragColor = vec4(guide, 0.0, 0.0, 1.0);
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val PASS_CHROMA_DENOISE = """
|
||||
#version 300 es
|
||||
@@ -15,57 +45,240 @@ object ChromaDenoiseShaders {
|
||||
in vec2 vTexCoord;
|
||||
out vec4 fragColor;
|
||||
uniform sampler2D uInputTexture;
|
||||
uniform sampler2D uGuideTexture;
|
||||
uniform vec2 uTexelSize;
|
||||
uniform mat4 uTexMatrix;
|
||||
uniform float uH;
|
||||
uniform vec2 uNoiseModel;
|
||||
uniform float uOutputStrength;
|
||||
uniform float uEdgeGuidanceRelaxation;
|
||||
uniform int uCameraRgbInput;
|
||||
// [red slope, red offset, blue slope, blue offset].
|
||||
uniform vec4 uNoiseModelRB;
|
||||
|
||||
vec3 rgb2ycbcr(vec3 rgb) {
|
||||
float y = dot(rgb, vec3(0.2126, 0.7152, 0.0722));
|
||||
return vec3(y,
|
||||
dot(rgb, vec3(-0.169, -0.331, 0.5 )) + 0.5,
|
||||
dot(rgb, vec3( 0.5, -0.419, -0.081 )) + 0.5);
|
||||
dot(rgb, vec3(-0.114572, -0.385428, 0.5 )),
|
||||
dot(rgb, vec3( 0.5, -0.454153, -0.045847)));
|
||||
}
|
||||
vec3 ycbcr2rgb(vec3 yuv) {
|
||||
float cb = yuv.y - 0.5, cr = yuv.z - 0.5;
|
||||
return vec3(yuv.x + 1.402 * cr,
|
||||
yuv.x - 0.3441 * cb - 0.7141 * cr,
|
||||
yuv.x + 1.772 * cb);
|
||||
return vec3(yuv.x + 1.5748 * yuv.z,
|
||||
yuv.x - 0.187324 * yuv.y - 0.468124 * yuv.z,
|
||||
yuv.x + 1.8556 * yuv.y);
|
||||
}
|
||||
float noiseSigmaForLuma(float luma) {
|
||||
return sqrt(max(uNoiseModel.x * max(luma, 0.0) + uNoiseModel.y, 1e-10));
|
||||
|
||||
vec2 redBlueNoiseSigma(vec3 rgb) {
|
||||
float redVariance =
|
||||
uNoiseModelRB.x * max(rgb.r, 0.0) + uNoiseModelRB.y;
|
||||
float blueVariance =
|
||||
uNoiseModelRB.z * max(rgb.b, 0.0) + uNoiseModelRB.w;
|
||||
return sqrt(max(vec2(redVariance, blueVariance), vec2(1e-10)));
|
||||
}
|
||||
|
||||
float cameraChromaScale(vec3 rgb) {
|
||||
// Above the noise floor this is ordinary green-normalized opponent
|
||||
// color. In deep shadows the R/B model supplies a stable denominator
|
||||
// so chroma ratios cannot explode as green approaches zero.
|
||||
vec2 rbSigma = redBlueNoiseSigma(rgb);
|
||||
float noiseFloor = 4.0 * max(rbSigma.x, rbSigma.y);
|
||||
return max(max(rgb.g, 0.0), max(noiseFloor, 1e-4));
|
||||
}
|
||||
|
||||
vec3 rgbToFilterSpace(vec3 rgb) {
|
||||
if (uCameraRgbInput != 0) {
|
||||
float scale = cameraChromaScale(rgb);
|
||||
return vec3(
|
||||
rgb.g,
|
||||
(rgb.r - rgb.g) / scale,
|
||||
(rgb.b - rgb.g) / scale
|
||||
);
|
||||
}
|
||||
return rgb2ycbcr(rgb);
|
||||
}
|
||||
|
||||
vec3 filterSpaceToRgb(vec3 value, vec3 referenceRgb) {
|
||||
if (uCameraRgbInput != 0) {
|
||||
float green = value.x;
|
||||
float scale = cameraChromaScale(referenceRgb);
|
||||
return max(
|
||||
vec3(
|
||||
green + value.y * scale,
|
||||
green,
|
||||
green + value.z * scale
|
||||
),
|
||||
vec3(0.0)
|
||||
);
|
||||
}
|
||||
return ycbcr2rgb(value);
|
||||
}
|
||||
|
||||
vec2 chromaNoiseSigma(vec3 rgb) {
|
||||
vec2 rbSigma = redBlueNoiseSigma(rgb);
|
||||
if (uCameraRgbInput != 0) {
|
||||
// Normalized opponent order is (R-G)/scale, (B-G)/scale.
|
||||
return rbSigma / cameraChromaScale(rgb);
|
||||
}
|
||||
// Bitmap filter-space order is Cb, Cr.
|
||||
return rbSigma.yx;
|
||||
}
|
||||
|
||||
float guideNoiseSigma(vec3 rgb) {
|
||||
// The guide remains in linear green/luma units, so its range bandwidth
|
||||
// must use the unnormalized R/B sigma rather than opponent-space sigma.
|
||||
vec2 rbSigma = redBlueNoiseSigma(rgb);
|
||||
return max(rbSigma.x, rbSigma.y);
|
||||
}
|
||||
|
||||
void filterScale(
|
||||
float radius,
|
||||
float centerGuide,
|
||||
vec2 baseChroma,
|
||||
vec2 chromaGuide,
|
||||
float invGuideH2,
|
||||
vec2 invChromaH2,
|
||||
out vec2 filteredChroma,
|
||||
out float edgeSupport
|
||||
) {
|
||||
// The center estimate anchors each wider scale and prevents a sparse
|
||||
// coarse neighborhood from replacing a small valid color feature.
|
||||
vec2 sumChroma = baseChroma;
|
||||
float sumWeight = 1.0;
|
||||
float acceptedLumaSupport = 0.0;
|
||||
float totalSupport = 0.0;
|
||||
|
||||
for (int x = -1; x <= 1; x++) {
|
||||
for (int y = -1; y <= 1; y++) {
|
||||
if (x == 0 && y == 0) continue;
|
||||
|
||||
vec2 gridOffset = vec2(float(x), float(y));
|
||||
float distance2 = dot(gridOffset, gridOffset);
|
||||
float spatialWeight = exp(-0.72 * distance2);
|
||||
vec2 offset = gridOffset * uTexelSize * radius;
|
||||
vec2 sampleCoord = vTexCoord + offset;
|
||||
vec3 sampleValue = rgbToFilterSpace(
|
||||
texture(uInputTexture, sampleCoord).rgb
|
||||
);
|
||||
|
||||
float guideDelta =
|
||||
texture(uGuideTexture, sampleCoord).r - centerGuide;
|
||||
float guideWeight =
|
||||
exp(-(guideDelta * guideDelta) * invGuideH2);
|
||||
vec2 chromaDelta = sampleValue.yz - chromaGuide;
|
||||
float chromaWeight = exp(-dot(chromaDelta * chromaDelta, invChromaH2));
|
||||
float weight = spatialWeight * guideWeight * chromaWeight;
|
||||
|
||||
sumChroma += sampleValue.yz * weight;
|
||||
sumWeight += weight;
|
||||
acceptedLumaSupport += spatialWeight * guideWeight;
|
||||
totalSupport += spatialWeight;
|
||||
}
|
||||
}
|
||||
|
||||
filteredChroma = sumChroma / max(sumWeight, 1e-6);
|
||||
// The guide is always active. Above half strength its bandwidth grows,
|
||||
// admitting weak/noisy pseudo-edges while real strong edges still
|
||||
// receive exponential rejection.
|
||||
edgeSupport = acceptedLumaSupport / max(totalSupport, 1e-6);
|
||||
}
|
||||
|
||||
float featheredEdgeSupport(float support, float exponent) {
|
||||
return pow(clamp(support, 0.0, 1.0), exponent);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 oriRgb = texture(uInputTexture, vTexCoord).rgb;
|
||||
if (uH <= 0.00001) { fragColor = vec4(oriRgb, 1.0); return; }
|
||||
|
||||
vec3 yuv = rgb2ycbcr(oriRgb);
|
||||
vec2 sumUV = vec2(0.0);
|
||||
float sumW = 0.0;
|
||||
|
||||
float stepScale = 1.25 + uH * 3.5;
|
||||
float localH = uH * noiseSigmaForLuma(yuv.x);
|
||||
float invChromaH2 = 1.0 / max((localH * 1.5) * (localH * 1.5), 1e-6);
|
||||
float invLumaH2 = 1.0 / max((localH * 0.5) * (localH * 0.5), 1e-6);
|
||||
|
||||
for (int x = -2; x <= 2; x++) {
|
||||
for (int y = -2; y <= 2; y++) {
|
||||
vec2 ofs = vec2(float(x), float(y)) * uTexelSize * stepScale;
|
||||
vec3 sYuv = rgb2ycbcr(texture(uInputTexture, vTexCoord + ofs).rgb);
|
||||
|
||||
float wS = exp(-float(x * x + y * y) * 0.25);
|
||||
float dL = sYuv.x - yuv.x;
|
||||
vec2 dC = sYuv.yz - yuv.yz;
|
||||
float wR = exp(-(dL * dL) * invLumaH2 - dot(dC, dC) * invChromaH2);
|
||||
float w = wS * wR;
|
||||
if (x == 0 && y == 0) w = max(w, 0.1);
|
||||
sumUV += sYuv.yz * w;
|
||||
sumW += w;
|
||||
}
|
||||
vec4 source = texture(uInputTexture, vTexCoord);
|
||||
if (uH <= 0.00001) {
|
||||
fragColor = source;
|
||||
return;
|
||||
}
|
||||
yuv.yz = sumUV / sumW;
|
||||
fragColor = vec4(ycbcr2rgb(yuv), 1.0);
|
||||
|
||||
vec3 centerValue = rgbToFilterSpace(source.rgb);
|
||||
vec2 noiseSigma = chromaNoiseSigma(source.rgb);
|
||||
vec2 localH = max(uH * noiseSigma, vec2(1e-5));
|
||||
float shadowFactor =
|
||||
1.0 - smoothstep(0.03, 0.35, max(centerValue.x, 0.0));
|
||||
|
||||
float centerGuide = texture(uGuideTexture, vTexCoord).r;
|
||||
float guideBandwidthScale =
|
||||
mix(1.0, 4.0, uEdgeGuidanceRelaxation);
|
||||
float guideH =
|
||||
guideNoiseSigma(source.rgb) * 4.0 * guideBandwidthScale;
|
||||
float invGuideH2 = 1.0 / max(guideH * guideH, 1e-8);
|
||||
|
||||
vec2 fineCandidate;
|
||||
float fineSupport;
|
||||
// Radius one directly covers the one-to-two-pixel sensor grain that
|
||||
// was skipped by the previous sparse fine scale.
|
||||
vec2 fineH = localH * 4.0;
|
||||
filterScale(
|
||||
1.0,
|
||||
centerGuide,
|
||||
centerValue.yz,
|
||||
centerValue.yz,
|
||||
invGuideH2,
|
||||
1.0 / max(fineH * fineH, vec2(1e-8)),
|
||||
fineCandidate,
|
||||
fineSupport
|
||||
);
|
||||
vec2 fineChroma =
|
||||
mix(centerValue.yz, fineCandidate, uOutputStrength);
|
||||
|
||||
vec2 mediumCandidate;
|
||||
float mediumSupport;
|
||||
vec2 mediumH = localH * 1.8;
|
||||
filterScale(
|
||||
4.0,
|
||||
centerGuide,
|
||||
fineChroma,
|
||||
fineChroma,
|
||||
invGuideH2,
|
||||
1.0 / max(mediumH * mediumH, vec2(1e-8)),
|
||||
mediumCandidate,
|
||||
mediumSupport
|
||||
);
|
||||
float mediumMix =
|
||||
uOutputStrength * featheredEdgeSupport(mediumSupport, 1.5);
|
||||
vec2 mediumChroma = mix(fineChroma, mediumCandidate, mediumMix);
|
||||
|
||||
vec2 coarseCandidate;
|
||||
float coarseSupport;
|
||||
vec2 coarseH = localH * 2.1;
|
||||
filterScale(
|
||||
14.0,
|
||||
centerGuide,
|
||||
mediumChroma,
|
||||
mediumChroma,
|
||||
invGuideH2,
|
||||
1.0 / max(coarseH * coarseH, vec2(1e-8)),
|
||||
coarseCandidate,
|
||||
coarseSupport
|
||||
);
|
||||
float coarseMix =
|
||||
uOutputStrength * featheredEdgeSupport(coarseSupport, 2.0);
|
||||
vec2 coarseChroma = mix(mediumChroma, coarseCandidate, coarseMix);
|
||||
|
||||
// Low-frequency chroma clouds need samples well outside the grain
|
||||
// neighborhood. The radius and bandwidth expand only in shadows;
|
||||
// guide support rejects cross-structure fusion while it is active,
|
||||
// while the chroma bandwidth remains wide enough to treat a cast as noise.
|
||||
vec2 cloudCandidate;
|
||||
float cloudSupport;
|
||||
vec2 cloudH = localH * mix(2.5, 3.5, shadowFactor);
|
||||
float cloudRadius = 56.0 * mix(1.0, 1.25, shadowFactor);
|
||||
filterScale(
|
||||
cloudRadius,
|
||||
centerGuide,
|
||||
coarseChroma,
|
||||
coarseChroma,
|
||||
invGuideH2,
|
||||
1.0 / max(cloudH * cloudH, vec2(1e-8)),
|
||||
cloudCandidate,
|
||||
cloudSupport
|
||||
);
|
||||
float cloudMix =
|
||||
uOutputStrength * featheredEdgeSupport(cloudSupport, 1.5);
|
||||
|
||||
centerValue.yz = mix(coarseChroma, cloudCandidate, cloudMix);
|
||||
fragColor = vec4(filterSpaceToRgb(centerValue, source.rgb), source.a);
|
||||
}
|
||||
""".trimIndent()
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,7 @@ class LutImageProcessor {
|
||||
private var bitmapDenoiseNlmFinishProgram = 0
|
||||
private var bitmapDenoisePassthroughProgram = 0
|
||||
private var naturalLightSrgbToLinearProgram = 0
|
||||
private var bitmapChromaDenoiseGuideProgram = 0
|
||||
private var bitmapChromaDenoiseProgram = 0
|
||||
private var lutSharpenProgram = 0
|
||||
|
||||
@@ -1514,7 +1515,8 @@ class LutImageProcessor {
|
||||
bitmapDenoiseNlmFinishProgram = compileComputeProgram(DenoiseProfileShaders.FINISH_V2, "BitmapDenoise_NLM_FinishV2")
|
||||
bitmapDenoisePassthroughProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, TEXTURE_PASSTHROUGH_SHADER, "BitmapDenoise_Passthrough")
|
||||
naturalLightSrgbToLinearProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, SRGB_TO_LINEAR_SHADER, "NaturalLight_SrgbToLinear")
|
||||
bitmapChromaDenoiseProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, ChromaDenoiseShaders.PASS_CHROMA_DENOISE, "BitmapChromaDenoise_BM3DPass0")
|
||||
bitmapChromaDenoiseGuideProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, ChromaDenoiseShaders.PASS_EDGE_GUIDE, "BitmapChromaDenoise_EdgeGuide")
|
||||
bitmapChromaDenoiseProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, ChromaDenoiseShaders.PASS_CHROMA_DENOISE, "BitmapChromaDenoise_MultiScale")
|
||||
lutSharpenProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, RawShaders.SHARPEN_FRAGMENT_SHADER, "LutSharpen")
|
||||
PLog.d(
|
||||
TAG,
|
||||
@@ -1522,6 +1524,7 @@ class LutImageProcessor {
|
||||
"init=$bitmapDenoiseNlmInitProgram fusedAccu=$bitmapDenoiseNlmFusedAccuProgram " +
|
||||
"finish=$bitmapDenoiseNlmFinishProgram " +
|
||||
"pass=$bitmapDenoisePassthroughProgram linearize=$naturalLightSrgbToLinearProgram " +
|
||||
"chromaGuide=$bitmapChromaDenoiseGuideProgram " +
|
||||
"chroma=$bitmapChromaDenoiseProgram " +
|
||||
"sharpen=$lutSharpenProgram"
|
||||
)
|
||||
@@ -1642,14 +1645,26 @@ class LutImageProcessor {
|
||||
|
||||
val strength = chromaNoiseReduction.coerceIn(0f, 1f)
|
||||
val target = targetIndex.coerceIn(0, bitmapDenoiseTexId.lastIndex)
|
||||
if (strength <= 0f || bitmapChromaDenoiseProgram == 0) {
|
||||
val edgeGuidanceRelaxation =
|
||||
ChromaDenoiseDefaults.edgeGuidanceRelaxation(strength)
|
||||
if (strength <= 0f || bitmapChromaDenoiseProgram == 0 ||
|
||||
bitmapChromaDenoiseGuideProgram == 0
|
||||
) {
|
||||
renderTexturePassthrough(sourceTextureId, bitmapDenoiseFboId[target], width, height)
|
||||
return
|
||||
}
|
||||
|
||||
val identityMatrix = FloatArray(16)
|
||||
android.opengl.Matrix.setIdentityM(identityMatrix, 0)
|
||||
val h = strength * strength * ChromaDenoiseShaders.SIGMA_STRENGTH_AT_SLIDER_ONE
|
||||
val h = ChromaDenoiseDefaults.noiseBandwidth(strength)
|
||||
val guideIndex = if (target == 0) 1 else 0
|
||||
renderBitmapChromaDenoiseEdgeGuide(
|
||||
sourceTextureId,
|
||||
bitmapDenoiseFboId[guideIndex],
|
||||
width,
|
||||
height
|
||||
)
|
||||
val guideTextureId = bitmapDenoiseTexId[guideIndex]
|
||||
|
||||
GLES30.glUseProgram(bitmapChromaDenoiseProgram)
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, bitmapDenoiseFboId[target])
|
||||
@@ -1657,6 +1672,12 @@ class LutImageProcessor {
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, sourceTextureId)
|
||||
GLES30.glUniform1i(GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uInputTexture"), 0)
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE1)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, guideTextureId)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uGuideTexture"),
|
||||
1
|
||||
)
|
||||
GLES30.glUniform2f(
|
||||
GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uTexelSize"),
|
||||
1.0f / width,
|
||||
@@ -1669,9 +1690,26 @@ class LutImageProcessor {
|
||||
identityMatrix,
|
||||
0
|
||||
)
|
||||
GLES30.glUniform1f(
|
||||
GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uOutputStrength"),
|
||||
ChromaDenoiseDefaults.outputStrength(strength)
|
||||
)
|
||||
GLES30.glUniform1f(
|
||||
GLES30.glGetUniformLocation(
|
||||
bitmapChromaDenoiseProgram,
|
||||
"uEdgeGuidanceRelaxation"
|
||||
),
|
||||
edgeGuidanceRelaxation
|
||||
)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uCameraRgbInput"),
|
||||
0
|
||||
)
|
||||
GLES30.glUniform1f(GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uH"), h)
|
||||
GLES30.glUniform2f(
|
||||
GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uNoiseModel"),
|
||||
GLES30.glUniform4f(
|
||||
GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uNoiseModelRB"),
|
||||
BITMAP_DENOISE_A * 2f,
|
||||
BITMAP_DENOISE_B * 2f,
|
||||
BITMAP_DENOISE_A * 2f,
|
||||
BITMAP_DENOISE_B * 2f
|
||||
)
|
||||
@@ -1679,6 +1717,43 @@ class LutImageProcessor {
|
||||
checkGlError("renderBitmapChromaDenoise")
|
||||
}
|
||||
|
||||
private fun renderBitmapChromaDenoiseEdgeGuide(
|
||||
sourceTextureId: Int,
|
||||
targetFramebufferId: Int,
|
||||
width: Int,
|
||||
height: Int,
|
||||
) {
|
||||
GLES30.glUseProgram(bitmapChromaDenoiseGuideProgram)
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFramebufferId)
|
||||
GLES30.glViewport(0, 0, width, height)
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, sourceTextureId)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(bitmapChromaDenoiseGuideProgram, "uInputTexture"),
|
||||
0
|
||||
)
|
||||
GLES30.glUniform2f(
|
||||
GLES30.glGetUniformLocation(bitmapChromaDenoiseGuideProgram, "uTexelSize"),
|
||||
1.0f / width,
|
||||
1.0f / height
|
||||
)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(bitmapChromaDenoiseGuideProgram, "uCameraRgbInput"),
|
||||
0
|
||||
)
|
||||
val identityMatrix = FloatArray(16)
|
||||
android.opengl.Matrix.setIdentityM(identityMatrix, 0)
|
||||
GLES30.glUniformMatrix4fv(
|
||||
GLES30.glGetUniformLocation(bitmapChromaDenoiseGuideProgram, "uMVPMatrix"),
|
||||
1,
|
||||
false,
|
||||
identityMatrix,
|
||||
0
|
||||
)
|
||||
drawQuad(bitmapChromaDenoiseGuideProgram)
|
||||
checkGlError("renderBitmapChromaDenoiseEdgeGuide")
|
||||
}
|
||||
|
||||
private fun resolvePreToneMapChromaDenoise(
|
||||
userStrength: Float,
|
||||
applyNaturalLightDefault: Boolean
|
||||
@@ -2729,6 +2804,7 @@ class LutImageProcessor {
|
||||
if (bitmapDenoiseNlmFinishProgram != 0) GLES31.glDeleteProgram(bitmapDenoiseNlmFinishProgram)
|
||||
if (bitmapDenoisePassthroughProgram != 0) GLES30.glDeleteProgram(bitmapDenoisePassthroughProgram)
|
||||
if (naturalLightSrgbToLinearProgram != 0) GLES30.glDeleteProgram(naturalLightSrgbToLinearProgram)
|
||||
if (bitmapChromaDenoiseGuideProgram != 0) GLES30.glDeleteProgram(bitmapChromaDenoiseGuideProgram)
|
||||
if (bitmapChromaDenoiseProgram != 0) GLES30.glDeleteProgram(bitmapChromaDenoiseProgram)
|
||||
if (lutSharpenProgram != 0) {
|
||||
GLES30.glDeleteProgram(lutSharpenProgram)
|
||||
|
||||
@@ -256,6 +256,7 @@ class RawDemosaicProcessor {
|
||||
private var sharpenProgram = 0
|
||||
private var passthroughProgram = 0
|
||||
private var hdrReferenceProgram = 0
|
||||
private var chromaDenoiseGuideProgram = 0
|
||||
private var chromaDenoiseProgram = 0
|
||||
private var loggedShadowsHighlightsUniforms = false
|
||||
|
||||
@@ -1839,12 +1840,6 @@ class RawDemosaicProcessor {
|
||||
applyDngBaselineExposure = applyProfileDngBaselineExposure,
|
||||
useRamp = useProfileExposureRamp
|
||||
)
|
||||
val profileLinearExposureGain = 2.0f.pow(profileExposureUniforms.exposureEv)
|
||||
val linearExposureGain = if (applyLinearDngBaselineExposure) {
|
||||
profileLinearExposureGain * exactDngBaselineExposureGain(actualMetadata)
|
||||
} else {
|
||||
profileLinearExposureGain
|
||||
}
|
||||
val shadowsHighlightsParams = ShadowsHighlightsParams(
|
||||
highlights = effectiveHighlightsAdjustment,
|
||||
shadows = rawShadowsAdjustment,
|
||||
@@ -1867,14 +1862,24 @@ class RawDemosaicProcessor {
|
||||
"dcpBaselineExposureOffsetApplied=$applyDcpBaselineExposureOffset"
|
||||
)
|
||||
|
||||
// darktable runs denoiseprofile after demosaic and before input color conversion.
|
||||
// The RCD output is un-white-balanced camera RGB, so the VST keeps WB only in its
|
||||
// adaptive exponent and returns the same camera-RGB domain for LinearRcdPass.
|
||||
renderDenoiseProfilePass(
|
||||
// Chroma denoise runs first in the un-white-balanced camera RGB domain,
|
||||
// where the DNG/Camera2 R/B noise model is valid. It writes to the spare
|
||||
// full-resolution texture and preserves green while filtering R-G/B-G.
|
||||
val chromaDenoiseTextureId = renderDefaultChromaDenoise(
|
||||
sourceTextureId = demosaicTextureId,
|
||||
width = actualWidth,
|
||||
height = actualHeight,
|
||||
metadata = actualMetadata,
|
||||
chromaDenoiseValue = chromaDenoiseValue,
|
||||
)
|
||||
|
||||
// darktable denoiseprofile follows chroma denoise and still runs before
|
||||
// input color conversion. Its VST returns un-white-balanced camera RGB.
|
||||
renderDenoiseProfilePass(
|
||||
sourceTextureId = chromaDenoiseTextureId,
|
||||
width = actualWidth,
|
||||
height = actualHeight,
|
||||
metadata = actualMetadata,
|
||||
denoiseValue = denoiseValue,
|
||||
)
|
||||
val denoiseProfileTextureId = gfTexId[1]
|
||||
@@ -1925,14 +1930,7 @@ class RawDemosaicProcessor {
|
||||
rawTextureId = 0
|
||||
}
|
||||
val workingColorSpace = resolveWorkingColorSpace()
|
||||
val outputTexture = renderDefaultChromaDenoise(
|
||||
sourceTextureId = demosaicTextureId,
|
||||
width = actualWidth,
|
||||
height = actualHeight,
|
||||
metadata = actualMetadata,
|
||||
linearExposureGain = linearExposureGain,
|
||||
chromaDenoiseValue = chromaDenoiseValue,
|
||||
)
|
||||
val outputTexture = demosaicTextureId
|
||||
|
||||
// 重点:不要在此处销毁常驻双缓冲的 framebuffer,由 setupFullResFramebuffer 或 release() 统一管理其生命周期
|
||||
// if (demosaicFramebufferId != 0) {
|
||||
@@ -2686,7 +2684,7 @@ class RawDemosaicProcessor {
|
||||
// 初始化着色器和缓冲区
|
||||
initShaderProgram()
|
||||
if (sharpenProgram == 0 || passthroughProgram == 0 ||
|
||||
chromaDenoiseProgram == 0 ||
|
||||
chromaDenoiseGuideProgram == 0 || chromaDenoiseProgram == 0 ||
|
||||
rcdPopulateProgram == 0 || rcdStep1Program == 0 || rcdStep2Program == 0 ||
|
||||
rcdStep3Program == 0 || rcdStep40Program == 0 || rcdStep41Program == 0 ||
|
||||
rcdStep42Program == 0 || rcdStep43Program == 0 || rcdWriteOutputProgram == 0 ||
|
||||
@@ -2697,6 +2695,7 @@ class RawDemosaicProcessor {
|
||||
PLog.e(
|
||||
TAG, "Critical shader programs failed to compile or link. " +
|
||||
"sharpen=$sharpenProgram pass=$passthroughProgram " +
|
||||
"chromaGuide=$chromaDenoiseGuideProgram " +
|
||||
"chromaDenoise=$chromaDenoiseProgram " +
|
||||
"populate=$rcdPopulateProgram step1=$rcdStep1Program step2=$rcdStep2Program " +
|
||||
"step3=$rcdStep3Program step40=$rcdStep40Program step41=$rcdStep41Program " +
|
||||
@@ -2782,7 +2781,31 @@ class RawDemosaicProcessor {
|
||||
// 2.7 NLM Programs
|
||||
initNLMPrograms(vShader)
|
||||
|
||||
// 2.75 RAW 默认色度降噪 Program
|
||||
// 2.75 RAW 默认色度降噪 Programs
|
||||
val fShaderChromaDenoiseGuide =
|
||||
compileShader(
|
||||
GLES30.GL_FRAGMENT_SHADER,
|
||||
ChromaDenoiseShaders.PASS_EDGE_GUIDE,
|
||||
"rawChromaDenoiseGuideFragment"
|
||||
)
|
||||
if (vShader != 0 && fShaderChromaDenoiseGuide != 0) {
|
||||
chromaDenoiseGuideProgram = GLES30.glCreateProgram()
|
||||
GLES30.glAttachShader(chromaDenoiseGuideProgram, vShader)
|
||||
GLES30.glAttachShader(chromaDenoiseGuideProgram, fShaderChromaDenoiseGuide)
|
||||
val linkStart = System.currentTimeMillis()
|
||||
GLES30.glLinkProgram(chromaDenoiseGuideProgram)
|
||||
if (!logProgramLinkResult(
|
||||
chromaDenoiseGuideProgram,
|
||||
"rawChromaDenoiseGuideProgram",
|
||||
linkStart
|
||||
)
|
||||
) {
|
||||
chromaDenoiseGuideProgram = 0
|
||||
}
|
||||
|
||||
GLES30.glDeleteShader(fShaderChromaDenoiseGuide)
|
||||
}
|
||||
|
||||
val fShaderChromaDenoise =
|
||||
compileShader(
|
||||
GLES30.GL_FRAGMENT_SHADER,
|
||||
@@ -3869,7 +3892,6 @@ class RawDemosaicProcessor {
|
||||
width: Int,
|
||||
height: Int,
|
||||
metadata: RawMetadata,
|
||||
linearExposureGain: Float,
|
||||
chromaDenoiseValue: Float?,
|
||||
): Int {
|
||||
val strength = ChromaDenoiseDefaults.rawDefaultStrength(chromaDenoiseValue ?: 0f)
|
||||
@@ -3877,7 +3899,9 @@ class RawDemosaicProcessor {
|
||||
return sourceTextureId
|
||||
}
|
||||
|
||||
if (chromaDenoiseProgram == 0 || linearOutputFramebufferId == 0 || linearOutputTextureId == 0) {
|
||||
if (chromaDenoiseGuideProgram == 0 || chromaDenoiseProgram == 0 ||
|
||||
linearOutputFramebufferId == 0 || linearOutputTextureId == 0
|
||||
) {
|
||||
PLog.w(
|
||||
TAG,
|
||||
"RAW chroma denoise program not initialized, falling back to source"
|
||||
@@ -3887,8 +3911,13 @@ class RawDemosaicProcessor {
|
||||
|
||||
val profileGain =
|
||||
(metadata.iso / 100.0f * metadata.postRawSensitivityBoost).coerceAtLeast(1f)
|
||||
val (noiseA, noiseB) = resolveDenoiseNoiseModel(metadata, linearExposureGain, profileGain)
|
||||
val h = strength * strength * ChromaDenoiseShaders.SIGMA_STRENGTH_AT_SLIDER_ONE
|
||||
val noiseModel = resolveChromaDenoiseNoiseModel(metadata, profileGain)
|
||||
val h = ChromaDenoiseDefaults.noiseBandwidth(strength)
|
||||
val edgeGuidanceRelaxation =
|
||||
ChromaDenoiseDefaults.edgeGuidanceRelaxation(strength)
|
||||
setupNLMFramebuffers(width, height)
|
||||
renderChromaDenoiseEdgeGuide(sourceTextureId, width, height)
|
||||
val guideTextureId = gfTexId[0]
|
||||
val identityMatrix = FloatArray(16)
|
||||
GlMatrix.setIdentityM(identityMatrix, 0)
|
||||
|
||||
@@ -3898,10 +3927,11 @@ class RawDemosaicProcessor {
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, sourceTextureId)
|
||||
GLES30.glUniform1i(GLES30.glGetUniformLocation(chromaDenoiseProgram, "uInputTexture"), 0)
|
||||
GLES30.glUniform2f(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uTexelSize"),
|
||||
1.0f / width,
|
||||
1.0f / height
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE1)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, guideTextureId)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uGuideTexture"),
|
||||
1
|
||||
)
|
||||
GLES30.glUniformMatrix4fv(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uTexMatrix"),
|
||||
@@ -3910,11 +3940,30 @@ class RawDemosaicProcessor {
|
||||
identityMatrix,
|
||||
0
|
||||
)
|
||||
GLES30.glUniform1f(GLES30.glGetUniformLocation(chromaDenoiseProgram, "uH"), h)
|
||||
GLES30.glUniform2f(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uNoiseModel"),
|
||||
noiseA,
|
||||
noiseB
|
||||
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uTexelSize"),
|
||||
1.0f / width,
|
||||
1.0f / height
|
||||
)
|
||||
GLES30.glUniform1f(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uOutputStrength"),
|
||||
ChromaDenoiseDefaults.outputStrength(strength)
|
||||
)
|
||||
GLES30.glUniform1f(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uEdgeGuidanceRelaxation"),
|
||||
edgeGuidanceRelaxation
|
||||
)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uCameraRgbInput"),
|
||||
1
|
||||
)
|
||||
GLES30.glUniform1f(GLES30.glGetUniformLocation(chromaDenoiseProgram, "uH"), h)
|
||||
GLES30.glUniform4f(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uNoiseModelRB"),
|
||||
noiseModel.redSlope,
|
||||
noiseModel.redOffset,
|
||||
noiseModel.blueSlope,
|
||||
noiseModel.blueOffset
|
||||
)
|
||||
drawQuad(chromaDenoiseProgram)
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
@@ -3922,11 +3971,51 @@ class RawDemosaicProcessor {
|
||||
|
||||
PLog.d(
|
||||
TAG,
|
||||
"RAW chroma denoise: strength=$strength h=$h a=$noiseA b=$noiseB"
|
||||
"RAW chroma denoise before luma: strength=$strength h=$h " +
|
||||
"edgeGuidanceRelaxation=$edgeGuidanceRelaxation " +
|
||||
"red=(${noiseModel.redSlope}, ${noiseModel.redOffset}) " +
|
||||
"blue=(${noiseModel.blueSlope}, ${noiseModel.blueOffset})"
|
||||
)
|
||||
return linearOutputTextureId
|
||||
}
|
||||
|
||||
private fun renderChromaDenoiseEdgeGuide(
|
||||
sourceTextureId: Int,
|
||||
width: Int,
|
||||
height: Int,
|
||||
) {
|
||||
GLES30.glUseProgram(chromaDenoiseGuideProgram)
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, gfFboId[0])
|
||||
GLES30.glViewport(0, 0, width, height)
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, sourceTextureId)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseGuideProgram, "uInputTexture"),
|
||||
0
|
||||
)
|
||||
GLES30.glUniform2f(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseGuideProgram, "uTexelSize"),
|
||||
1.0f / width,
|
||||
1.0f / height
|
||||
)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseGuideProgram, "uCameraRgbInput"),
|
||||
1
|
||||
)
|
||||
val identityMatrix = FloatArray(16)
|
||||
GlMatrix.setIdentityM(identityMatrix, 0)
|
||||
GLES30.glUniformMatrix4fv(
|
||||
GLES30.glGetUniformLocation(chromaDenoiseGuideProgram, "uTexMatrix"),
|
||||
1,
|
||||
false,
|
||||
identityMatrix,
|
||||
0
|
||||
)
|
||||
drawQuad(chromaDenoiseGuideProgram)
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
checkGlError("RAW chroma denoise edge guide")
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 渲染 darktable denoiseprofile NLM 降噪。
|
||||
@@ -4400,24 +4489,49 @@ class RawDemosaicProcessor {
|
||||
(offset * frameNoiseScale).coerceAtLeast(1e-10f)
|
||||
}
|
||||
|
||||
private fun resolveDenoiseNoiseModel(
|
||||
metadata: RawMetadata,
|
||||
linearExposureGain: Float,
|
||||
fallbackGain: Float
|
||||
): Pair<Float, Float> {
|
||||
var s = metadata.noiseProfile.getOrElse(0) { 0f }
|
||||
var o = metadata.noiseProfile.getOrElse(1) { 0f }
|
||||
private data class ChromaDenoiseNoiseModel(
|
||||
val redSlope: Float,
|
||||
val redOffset: Float,
|
||||
val blueSlope: Float,
|
||||
val blueOffset: Float
|
||||
)
|
||||
|
||||
if (s <= 0f || o <= 0f) {
|
||||
s = 1E-4f * fallbackGain
|
||||
o = 4.5E-7f * sqrt(fallbackGain)
|
||||
private fun resolveChromaDenoiseNoiseModel(
|
||||
metadata: RawMetadata,
|
||||
fallbackGain: Float
|
||||
): ChromaDenoiseNoiseModel {
|
||||
val redBlueProfile = RawMetadata.redBlueNoiseProfile(
|
||||
metadata.channelNoiseProfile,
|
||||
metadata.cfaPattern
|
||||
)
|
||||
val fallbackSlope = metadata.noiseProfile.getOrElse(0) { 0f }
|
||||
.takeIf { it.isFinite() && it > 0f }
|
||||
?: (1E-4f * fallbackGain)
|
||||
val fallbackOffset = metadata.noiseProfile.getOrElse(1) { 0f }
|
||||
.takeIf { it.isFinite() && it > 0f }
|
||||
?: (4.5E-7f * sqrt(fallbackGain))
|
||||
|
||||
fun coefficient(index: Int, fallback: Float): Float {
|
||||
return redBlueProfile.getOrElse(index) { 0f }
|
||||
.takeIf { it.isFinite() && it > 0f }
|
||||
?: fallback
|
||||
}
|
||||
|
||||
val frameNoiseScale = 1f / metadata.frameCount.coerceAtLeast(1).toFloat()
|
||||
val gain = linearExposureGain.coerceAtLeast(1e-6f)
|
||||
val transformedS = s * frameNoiseScale * gain
|
||||
val transformedO = o * frameNoiseScale * gain * gain
|
||||
return transformedS.coerceAtLeast(1e-10f) to transformedO.coerceAtLeast(1e-10f)
|
||||
return ChromaDenoiseNoiseModel(
|
||||
redSlope =
|
||||
(coefficient(0, fallbackSlope) * frameNoiseScale)
|
||||
.coerceAtLeast(1e-10f),
|
||||
redOffset =
|
||||
(coefficient(1, fallbackOffset) * frameNoiseScale)
|
||||
.coerceAtLeast(1e-10f),
|
||||
blueSlope =
|
||||
(coefficient(2, fallbackSlope) * frameNoiseScale)
|
||||
.coerceAtLeast(1e-10f),
|
||||
blueOffset =
|
||||
(coefficient(3, fallbackOffset) * frameNoiseScale)
|
||||
.coerceAtLeast(1e-10f)
|
||||
)
|
||||
}
|
||||
|
||||
private fun dhtSetCommonUniforms(program: Int, metadata: RawMetadata) {
|
||||
@@ -8669,6 +8783,7 @@ class RawDemosaicProcessor {
|
||||
if (sharpenProgram != 0) GLES30.glDeleteProgram(sharpenProgram)
|
||||
if (passthroughProgram != 0) GLES30.glDeleteProgram(passthroughProgram)
|
||||
if (hdrReferenceProgram != 0) GLES30.glDeleteProgram(hdrReferenceProgram)
|
||||
if (chromaDenoiseGuideProgram != 0) GLES30.glDeleteProgram(chromaDenoiseGuideProgram)
|
||||
if (chromaDenoiseProgram != 0) GLES30.glDeleteProgram(chromaDenoiseProgram)
|
||||
releaseDarktableFilmicHighlightReconstructionPrograms()
|
||||
|
||||
|
||||
@@ -410,6 +410,46 @@ data class RawMetadata(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns red and blue noise models as [redSlope, redOffset, blueSlope, blueOffset].
|
||||
*
|
||||
* Camera2 profiles follow CFA position order; DNG profiles use [R, G, B]
|
||||
* and may contain a zero-padded fourth pair after native parsing.
|
||||
*/
|
||||
internal fun redBlueNoiseProfile(
|
||||
channelNoiseProfile: FloatArray,
|
||||
cfaPattern: Int
|
||||
): FloatArray {
|
||||
fun pairAt(pairIndex: Int): FloatArray? {
|
||||
val offset = pairIndex * 2
|
||||
if (offset + 1 >= channelNoiseProfile.size) return null
|
||||
val slope = sanitizeNoiseCoefficient(channelNoiseProfile[offset])
|
||||
val intercept = sanitizeNoiseCoefficient(channelNoiseProfile[offset + 1])
|
||||
if (slope <= 0f && intercept <= 0f) return null
|
||||
return floatArrayOf(slope, intercept)
|
||||
}
|
||||
|
||||
val hasFourChannels = pairAt(3) != null
|
||||
val (red, blue) = if (!hasFourChannels) {
|
||||
pairAt(0) to pairAt(2)
|
||||
} else {
|
||||
val indices = when (cfaPattern.mod(4)) {
|
||||
CFA_GRBG -> 1 to 2
|
||||
CFA_GBRG -> 2 to 1
|
||||
CFA_BGGR -> 3 to 0
|
||||
else -> 0 to 3
|
||||
}
|
||||
pairAt(indices.first) to pairAt(indices.second)
|
||||
}
|
||||
|
||||
return floatArrayOf(
|
||||
red?.get(0) ?: 0f,
|
||||
red?.get(1) ?: 0f,
|
||||
blue?.get(0) ?: 0f,
|
||||
blue?.get(1) ?: 0f
|
||||
)
|
||||
}
|
||||
|
||||
private fun sanitizeNoiseCoefficient(value: Float): Float {
|
||||
return value.takeIf { it.isFinite() }?.coerceAtLeast(0f) ?: 0f
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.hinnka.mycamera.lut
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class ChromaDenoiseDefaultsTest {
|
||||
@Test
|
||||
fun activeNoiseBandwidthRespondsAcrossTheWholeSliderRange() {
|
||||
assertEquals(0f, ChromaDenoiseDefaults.noiseBandwidth(0f), 0f)
|
||||
assertEquals(5.25f, ChromaDenoiseDefaults.noiseBandwidth(0.25f), 1e-6f)
|
||||
assertEquals(9.5f, ChromaDenoiseDefaults.noiseBandwidth(0.5f), 1e-6f)
|
||||
assertEquals(18f, ChromaDenoiseDefaults.noiseBandwidth(1f), 0f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun outputStrengthSmoothlyReachesFullAtOneQuarter() {
|
||||
assertEquals(0f, ChromaDenoiseDefaults.outputStrength(0f), 0f)
|
||||
assertEquals(0.5f, ChromaDenoiseDefaults.outputStrength(0.125f), 1e-6f)
|
||||
assertEquals(1f, ChromaDenoiseDefaults.outputStrength(0.25f), 0f)
|
||||
assertEquals(1f, ChromaDenoiseDefaults.outputStrength(1f), 0f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun edgeGuidanceOnlyRelaxesAboveHalfStrength() {
|
||||
assertEquals(0f, ChromaDenoiseDefaults.edgeGuidanceRelaxation(0f), 0f)
|
||||
assertEquals(0f, ChromaDenoiseDefaults.edgeGuidanceRelaxation(0.5f), 0f)
|
||||
assertEquals(0.5f, ChromaDenoiseDefaults.edgeGuidanceRelaxation(0.75f), 1e-6f)
|
||||
assertEquals(1f, ChromaDenoiseDefaults.edgeGuidanceRelaxation(1f), 0f)
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,53 @@ class RawNoiseProfileSelectionTest {
|
||||
assertArrayEquals(floatArrayOf(11f / 3f, 110f / 3f), RawMetadata.averageNoiseProfile(profile), 1e-6f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun camera2ProfileSelectsRedAndBlueFromCfaPositionOrder() {
|
||||
val profile = floatArrayOf(
|
||||
1f, 10f,
|
||||
2f, 20f,
|
||||
4f, 40f,
|
||||
8f, 80f,
|
||||
)
|
||||
|
||||
assertArrayEquals(
|
||||
floatArrayOf(1f, 10f, 8f, 80f),
|
||||
RawMetadata.redBlueNoiseProfile(profile, RawMetadata.CFA_RGGB),
|
||||
0f
|
||||
)
|
||||
assertArrayEquals(
|
||||
floatArrayOf(2f, 20f, 4f, 40f),
|
||||
RawMetadata.redBlueNoiseProfile(profile, RawMetadata.CFA_GRBG),
|
||||
0f
|
||||
)
|
||||
assertArrayEquals(
|
||||
floatArrayOf(4f, 40f, 2f, 20f),
|
||||
RawMetadata.redBlueNoiseProfile(profile, RawMetadata.CFA_GBRG),
|
||||
0f
|
||||
)
|
||||
assertArrayEquals(
|
||||
floatArrayOf(8f, 80f, 1f, 10f),
|
||||
RawMetadata.redBlueNoiseProfile(profile, RawMetadata.CFA_BGGR),
|
||||
0f
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun threePlaneDngProfileSelectsRedAndBlueAndIgnoresPadding() {
|
||||
val profile = floatArrayOf(
|
||||
1f, 10f,
|
||||
2f, 20f,
|
||||
8f, 80f,
|
||||
0f, 0f,
|
||||
)
|
||||
|
||||
assertArrayEquals(
|
||||
floatArrayOf(1f, 10f, 8f, 80f),
|
||||
RawMetadata.redBlueNoiseProfile(profile, RawMetadata.CFA_BGGR),
|
||||
0f
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun denoiseProfileShaderUsesCameraDomainSignalScaleInsteadOfWhiteBalanceScale() {
|
||||
val shader = DenoiseProfileShaders.PRECONDITION_V2 + DenoiseProfileShaders.FINISH_V2
|
||||
|
||||
Reference in new issue
Block a user