feat: implement cubic upsampling in shaders and fix DNG DefaultScale

This commit is contained in:
zhoubinjia committed 2026-05-19 17:55:48 +08:00
1 parent c447f08a6d
commit 11145b7cb8
4 files changed
+68 -42

No files matched your search

@@ -248,7 +248,7 @@ void processPlaneGather(ivec2 outPos, uint planeIndex) {
float geometricLikelihood =
exp(-0.5 * max(mahalanobis, 0.0) / effectiveSigma2);
float inlierPosterior = clamp(pInlier * pc.frameWeight, 0.0, 1.0);
float w = inlierDecisionGate(inlierPosterior) *
float w = ((pc.isFirstFrame == 1u) ? 1.0 : inlierDecisionGate(inlierPosterior)) *
geometricLikelihood * reconWeight *
sensorPrecisionWeight(val);
if (w <= 0.0) continue;
@@ -263,7 +263,7 @@ void processGreenPlaneGather(ivec2 outPos, int planeIndex) {
float geometricLikelihood =
exp(-0.5 * max(mahalanobis, 0.0) / effectiveSigma2);
float inlierPosterior = clamp(pInlier * pc.frameWeight * highlightSuppression, 0.0, 1.0);
float w = inlierDecisionGate(inlierPosterior) *
float w = ((pc.isFirstFrame == 1u) ? 1.0 : inlierDecisionGate(inlierPosterior)) *
geometricLikelihood * reconWeight *
sensorPrecisionWeight(val);
if (w <= 0.0) continue;
@@ -103,37 +103,73 @@ vec2 outputToSourceCenter(vec2 outputPos) {
return (outputPos + vec2(0.5)) / max(pc.scale, 1e-5) - vec2(0.5);
}
float fetchNormalizedSameColorRaw(int bayerIndex, ivec2 offset, ivec2 latticeCoord) {
latticeCoord.x = clamp(latticeCoord.x, 0, int(pc.planeWidth) - 1);
latticeCoord.y = clamp(latticeCoord.y, 0, int(pc.planeHeight) - 1);
ivec2 samplePos = offset + 2 * latticeCoord;
uint idx = uint(samplePos.y) * pc.sensorWidth + uint(samplePos.x);
return float(normalizedReferenceBuffer.data[idx]) / 65535.0;
}
float catmullRom(float v_1, float v0, float v1, float v2, float x) {
float x2 = x * x;
float x3 = x2 * x;
return 0.5 * (
v_1 * (-x3 + 2.0 * x2 - x) +
v0 * (3.0 * x3 - 5.0 * x2 + 2.0) +
v1 * (-3.0 * x3 + 4.0 * x2 + x) +
v2 * (x3 - x2)
);
}
float sampleCubicSameColor(int bayerIndex, ivec2 offset, ivec2 base, vec2 frac) {
float arr[4];
for (int j = -1; j <= 2; ++j) {
float v_1 = fetchNormalizedSameColorRaw(bayerIndex, offset, base + ivec2(-1, j));
float v0 = fetchNormalizedSameColorRaw(bayerIndex, offset, base + ivec2(0, j));
float v1 = fetchNormalizedSameColorRaw(bayerIndex, offset, base + ivec2(1, j));
float v2 = fetchNormalizedSameColorRaw(bayerIndex, offset, base + ivec2(2, j));
arr[j + 1] = catmullRom(v_1, v0, v1, v2, frac.x);
}
return catmullRom(arr[0], arr[1], arr[2], arr[3], frac.y);
}
void samplePrior(int bayerIndex, vec2 sourcePos,
out float normalizedRaw, out float support) {
if (pc.scale <= 1.0001) {
normalizedRaw = fetchNormalizedRaw(bayerIndex, ivec2(round(sourcePos)));
support = 0.60;
return;
}
ivec2 offset = getBayerOffset(pc.cfaPattern, bayerIndex);
vec2 latticePos = 0.5 * (sourcePos - vec2(offset));
ivec2 base = ivec2(floor(latticePos));
vec2 frac = latticePos - vec2(base);
ivec2 p00 = offset + 2 * base;
ivec2 p10 = p00 + ivec2(2, 0);
ivec2 p01 = p00 + ivec2(0, 2);
ivec2 p11 = p00 + ivec2(2, 2);
float v00 = fetchNormalizedSameColorRaw(bayerIndex, offset, base);
float v10 = fetchNormalizedSameColorRaw(bayerIndex, offset, base + ivec2(1, 0));
float v01 = fetchNormalizedSameColorRaw(bayerIndex, offset, base + ivec2(0, 1));
float v11 = fetchNormalizedSameColorRaw(bayerIndex, offset, base + ivec2(1, 1));
float v00 = fetchNormalizedRaw(bayerIndex, p00);
float v10 = fetchNormalizedRaw(bayerIndex, p10);
float v01 = fetchNormalizedRaw(bayerIndex, p01);
float v11 = fetchNormalizedRaw(bayerIndex, p11);
float sampleMin = min(min(v00, v10), min(v01, v11));
float sampleMax = max(max(v00, v10), max(v01, v11));
float vx0 = mix(v00, v10, frac.x);
float vx1 = mix(v01, v11, frac.x);
float bilinearRaw = mix(vx0, vx1, frac.y);
float cubicRaw = sampleCubicSameColor(bayerIndex, offset, base, frac);
// Allow slight overshoot/undershoot for sharpness (Catmull-Rom), but prevent excessive ringing
float padding = 0.08 * max(sampleMax - sampleMin, 0.01);
cubicRaw = clamp(cubicRaw, max(sampleMin - padding, 0.0), min(sampleMax + padding, 1.0));
float g00 = referenceGuide(p00);
float g10 = referenceGuide(p10);
float g01 = referenceGuide(p01);
float g11 = referenceGuide(p11);
float g00 = referenceGuide(offset + 2 * base);
float g10 = referenceGuide(offset + 2 * (base + ivec2(1, 0)));
float g01 = referenceGuide(offset + 2 * (base + ivec2(0, 1)));
float g11 = referenceGuide(offset + 2 * (base + ivec2(1, 1)));
float gx0 = mix(g00, g10, frac.x);
float gx1 = mix(g01, g11, frac.x);
float guideCenter = mix(gx0, gx1, frac.y);
ivec2 nearestPos = (frac.x < 0.5)
? ((frac.y < 0.5) ? p00 : p01)
: ((frac.y < 0.5) ? p10 : p11);
? ((frac.y < 0.5) ? (offset + 2 * base) : (offset + 2 * (base + ivec2(0, 1))))
: ((frac.y < 0.5) ? (offset + 2 * (base + ivec2(1, 0))) : (offset + 2 * (base + ivec2(1, 1))));
float nearestGuide = referenceGuide(nearestPos);
float guideTarget = mix(guideCenter, nearestGuide, 0.65);
@@ -152,10 +188,13 @@ void samplePrior(int bayerIndex, vec2 sourcePos,
float guideMin = min(min(g00, g10), min(g01, g11));
float guideMax = max(max(g00, g10), max(g01, g11));
float edgeStrength = smoothstep(0.035, 0.14, guideMax - guideMin);
normalizedRaw = mix(bilinearRaw, edgeRaw, edgeStrength);
float sampleMin = min(min(v00, v10), min(v01, v11));
float sampleMax = max(max(v00, v10), max(v01, v11));
// Suppress bilateral edge weighting when extremely close to direct sensor lattice lines/points to prevent blurring
float distToLattice = max(frac.x * (1.0 - frac.x), frac.y * (1.0 - frac.y));
edgeStrength *= smoothstep(0.0, 0.06, distToLattice);
normalizedRaw = mix(cubicRaw, edgeRaw, edgeStrength);
float coherence = 1.0 - smoothstep(0.04, 0.18,
max(sampleMax - sampleMin, guideMax - guideMin));
float interpolationPenalty =
@@ -5,7 +5,6 @@ import android.hardware.camera2.CaptureResult
import android.hardware.camera2.params.ColorSpaceTransform
import android.hardware.camera2.params.LensShadingMap
import android.os.Build
import android.util.Size
import com.hinnka.mycamera.raw.RawMetadata
import java.io.ByteArrayOutputStream
import java.io.FileOutputStream
@@ -16,7 +15,6 @@ import java.nio.channels.Channels
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import kotlin.math.abs
import kotlin.math.roundToInt
import kotlin.math.roundToLong
@@ -139,9 +137,13 @@ object SuperResolutionDngWriter {
whiteLevel: Int,
imageByteCount: Long,
): List<TiffEntry> {
val referenceSize = chooseReferenceSensorSize(characteristics, width, height)
val defaultScaleX = referenceSize?.let { it.width.toDouble() / width.toDouble() } ?: 1.0
val defaultScaleY = referenceSize?.let { it.height.toDouble() / height.toDouble() } ?: 1.0
// The custom writer is used when the fused RAW dimensions no longer
// match the camera sensor. The fused buffer already lives in its final
// Bayer pixel grid, so DefaultScale must stay 1:1. Scaling it back to
// the physical sensor size makes RAW decoders downsample the >1x DNG
// before demosaic, which looks blurrier than the original 1x frame.
val defaultScaleX = 1.0
val defaultScaleY = 1.0
val cameraModel = buildCameraModel(characteristics)
val illuminant1 = characteristics.get(CameraCharacteristics.SENSOR_REFERENCE_ILLUMINANT1) ?: 21
val illuminant2 = characteristics.get(CameraCharacteristics.SENSOR_REFERENCE_ILLUMINANT2)?.toInt()
@@ -272,21 +274,6 @@ object SuperResolutionDngWriter {
}
}
private fun chooseReferenceSensorSize(
characteristics: CameraCharacteristics,
outputWidth: Int,
outputHeight: Int,
): Size? {
val candidates = listOfNotNull(
characteristics.get(CameraCharacteristics.SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE)
?.let { Size(it.width(), it.height()) },
characteristics.get(CameraCharacteristics.SENSOR_INFO_PIXEL_ARRAY_SIZE)
)
return candidates.minByOrNull {
abs(outputWidth.toDouble() / it.width.toDouble() - outputHeight.toDouble() / it.height.toDouble())
}
}
private fun buildCameraModel(characteristics: CameraCharacteristics): String {
val hardwareLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL)
return listOfNotNull(