fix: adobe exposure ramp

This commit is contained in:
zhoubinjia committed 2026-07-16 17:27:27 +08:00
1 parent 8b840b9c1f
commit 41d3096f40
7 files changed
+280 -89

No files matched your search

+1
View File
@@ -102,6 +102,7 @@ std::string parseDcpToJson(const std::string &filePath) {
out << "\"calibrationIlluminant2\":" << profile.CalibrationIlluminant2() << ",";
out << "\"baselineExposureOffset\":" << profile.BaselineExposureOffset().As_real64() << ",";
out << "\"defaultBlackRender\":" << profile.DefaultBlackRender() << ",";
out << "\"supportsOverrange\":" << (profile.IsHDR() ? "true" : "false") << ",";
appendMatrix(out, "colorMatrix1", profile.ColorMatrix1());
out << ",";
appendMatrix(out, "colorMatrix2", profile.ColorMatrix2());
@@ -21,6 +21,17 @@ internal object DcpHueSatMapGl {
: pow((value + 0.055) / 1.055, 2.4);
}
float dngEncodeOverrangeValue(float value) {
value = max(value, 0.0);
return value * (256.0 + value) / (256.0 * (1.0 + value));
}
float dngDecodeOverrangeValue(float value) {
value = max(value, 0.0);
float discriminant = max(64.0 * value * value - 127.0 * value + 64.0, 0.0);
return 16.0 * (8.0 * value - 8.0 + sqrt(discriminant));
}
float dngEncodeLookupValue(float value, int encoding) {
value = dngClampTableCoordinate(value);
return encoding == 1 ? dngLinearToSrgbValue(value) : value;
@@ -114,9 +125,19 @@ internal object DcpHueSatMapGl {
vec3 color,
sampler3D tableTexture,
ivec3 divisions,
int encoding
int encoding,
bool supportOverrange
) {
vec3 hsv = dngRgbToHsv(color);
vec3 mapColor = supportOverrange ? max(color, vec3(0.0)) : color;
bool encodeOverrange = supportOverrange && divisions.z > 1;
if (encodeOverrange) {
mapColor = vec3(
dngEncodeOverrangeValue(mapColor.r),
dngEncodeOverrangeValue(mapColor.g),
dngEncodeOverrangeValue(mapColor.b)
);
}
vec3 hsv = dngRgbToHsv(mapColor);
float encodedValue = hsv.z;
float lookupValue = hsv.z;
// RefBaselineHueSatMap uses the encoded value axis only for a true 3D map.
@@ -136,7 +157,15 @@ internal object DcpHueSatMapGl {
hsv.y = dngClampTableCoordinate(hsv.y * modify.y);
encodedValue = clamp(encodedValue * modify.z, 0.0, 1.0);
hsv.z = dngDecodeScaledValue(encodedValue, encoding);
return dngHsvToRgb(hsv);
vec3 mapped = dngHsvToRgb(hsv);
if (encodeOverrange) {
mapped = vec3(
dngDecodeOverrangeValue(mapped.r),
dngDecodeOverrangeValue(mapped.g),
dngDecodeOverrangeValue(mapped.b)
);
}
return mapped;
}
""".trimIndent()
}
@@ -103,6 +103,7 @@ data class DcpProfile(
val calibrationIlluminant2: Int,
val baselineExposureOffset: Float,
val defaultBlackRender: DcpDefaultBlackRender = DcpDefaultBlackRender.Auto,
val supportsOverrange: Boolean = false,
val colorMatrix1: FloatArray?,
val colorMatrix2: FloatArray?,
val forwardMatrix1: FloatArray?,
@@ -121,6 +122,7 @@ data class DcpRenderPlan(
val workingColorSpace: ColorSpace,
val baselineExposureOffset: Float,
val defaultBlackRender: DcpDefaultBlackRender,
val supportsOverrange: Boolean = false,
val colorCorrectionMatrix: FloatArray,
val cameraWhite: FloatArray = floatArrayOf(1f, 1f, 1f),
val hueSatMap: DcpHueSatMap?,
@@ -207,6 +209,7 @@ object DcpProfileParser {
workingColorSpace = workingColorSpace,
baselineExposureOffset = profile.baselineExposureOffset,
defaultBlackRender = profile.defaultBlackRender,
supportsOverrange = profile.supportsOverrange,
colorCorrectionMatrix = selectedMatrix,
cameraWhite = selectedCameraWhite,
hueSatMap = selectedHueSat,
@@ -266,6 +269,7 @@ object DcpProfileParser {
calibrationIlluminant2 = root.optInt("calibrationIlluminant2", 0),
baselineExposureOffset = root.optDouble("baselineExposureOffset", 0.0).toFloat(),
defaultBlackRender = DcpDefaultBlackRender.fromTagValue(root.optInt("defaultBlackRender", 0)),
supportsOverrange = root.optBoolean("supportsOverrange", false),
colorMatrix1 = root.optJSONArray("colorMatrix1")?.toFloatArray(),
colorMatrix2 = root.optJSONArray("colorMatrix2")?.toFloatArray(),
forwardMatrix1 = root.optJSONArray("forwardMatrix1")?.toFloatArray(),
@@ -44,6 +44,7 @@ internal object DngEmbeddedProfile {
private const val TAG_PROFILE_LOOK_TABLE_ENCODING = 51108
private const val TAG_BASELINE_EXPOSURE_OFFSET = 51109
private const val TAG_DEFAULT_BLACK_RENDER = 51110
private const val TAG_PROFILE_DYNAMIC_RANGE = 52551
private val IDENTITY_3X3 = floatArrayOf(
1f, 0f, 0f,
@@ -66,7 +67,8 @@ internal object DngEmbeddedProfile {
"hueSat=${plan.hueSatMap?.isValid == true} " +
"look=${plan.lookTable?.isValid == true} " +
"baselineExposureOffset=${plan.baselineExposureOffset} " +
"defaultBlackRender=${plan.defaultBlackRender}"
"defaultBlackRender=${plan.defaultBlackRender} " +
"supportsOverrange=${plan.supportsOverrange}"
)
}
}
@@ -204,6 +206,11 @@ internal object DngEmbeddedProfile {
?.firstOrNull()
?.toInt()
)
val supportsOverrange = readProfileSupportsOverrange(
raf,
ifd[TAG_PROFILE_DYNAMIC_RANGE],
byteOrder
)
val hasProfileData = colorMatrix1 != null ||
colorMatrix2 != null ||
@@ -214,7 +221,8 @@ internal object DngEmbeddedProfile {
lookTable != null ||
toneCurve != null ||
abs(baselineExposureOffset) > 1e-6f ||
defaultBlackRender != DcpDefaultBlackRender.Auto
defaultBlackRender != DcpDefaultBlackRender.Auto ||
supportsOverrange
if (!hasProfileData) {
return null
}
@@ -225,6 +233,7 @@ internal object DngEmbeddedProfile {
calibrationIlluminant2 = illuminant2,
baselineExposureOffset = baselineExposureOffset,
defaultBlackRender = defaultBlackRender,
supportsOverrange = supportsOverrange,
colorMatrix1 = colorMatrix1,
colorMatrix2 = colorMatrix2,
forwardMatrix1 = forwardMatrix1,
@@ -248,6 +257,25 @@ internal object DngEmbeddedProfile {
return DcpToneCurve(values).takeIf { it.isValid }
}
private fun readProfileSupportsOverrange(
raf: RandomAccessFile,
entry: TiffEntry?,
byteOrder: ByteOrder
): Boolean {
if (entry == null || entry.type != TIFF_TYPE_UNDEFINED || entry.count != 8L) return false
val bytes = readEntryBytes(raf, entry, byteOrder)
if (bytes.size != 8) return false
val buffer = ByteBuffer.wrap(bytes).order(byteOrder)
val version = buffer.short.toInt() and 0xFFFF
val dynamicRange = buffer.short.toInt() and 0xFFFF
val hintMaxOutputValue = buffer.float
val valid = version == 1 &&
dynamicRange in 0..1 &&
hintMaxOutputValue.isFinite() &&
(dynamicRange != 0 || hintMaxOutputValue <= 1f)
return valid && dynamicRange != 0
}
private fun readHueSatMap(
raf: RandomAccessFile,
dataEntry: TiffEntry?,
@@ -842,7 +842,9 @@ class RawDemosaicProcessor {
),
applyDngBaselineExposure = false,
applyProfileGainTableMap = false,
profileBaselineExposureOffsetEv = 0f,
clampProfileRgb = false,
supportProfileOverrange = false,
label = "LinearRcdExport"
)
@@ -1345,11 +1347,15 @@ class RawDemosaicProcessor {
)
}
val hasProfileGainTableMap = actualMetadata.profileGainTableMap?.isValid == true
val applyLinearDngBaselineExposure = shouldApplyLinearDngBaselineExposure(actualMetadata)
val applyProfileDngBaselineExposure = !applyLinearDngBaselineExposure
val hasDngBaselineExposure = shouldApplyLinearDngBaselineExposure(actualMetadata)
val applyLinearDngBaselineExposure = !useAdobeProfilePipeline && hasDngBaselineExposure
val applyProfileDngBaselineExposure = useAdobeProfilePipeline
val applyDcpBaselineExposureOffset =
shouldApplyDcpBaselineExposureOffset(activeDcpRenderPlan)
val useProfileExposureRamp = useAdobeProfilePipeline
val supportProfileOverrange =
useAdobeProfilePipeline && activeDcpRenderPlan?.supportsOverrange == true
val clampProfileRgb = useAdobeProfilePipeline
val engineWorkingColorSpace = colorEngine.workingColorSpace
val profileToEngineTransform = computeWorkingToOutputTransform(
profileWorkingColorSpace,
@@ -1764,8 +1770,9 @@ class RawDemosaicProcessor {
cameraWhite = linearCameraWhite,
dcpRenderPlan = activeDcpRenderPlan,
applyLinearDngBaselineExposure = applyLinearDngBaselineExposure,
applyProfileDngBaselineExposure = applyProfileDngBaselineExposure,
applyProfileGainTableMap = false,
clampProfileRgb = useAdobeProfilePipeline,
clampProfileRgb = clampProfileRgb,
outputBounds = bounds,
outputRotation = actualRotation,
spectralFilmLut = spektrafilmLut,
@@ -1774,7 +1781,8 @@ class RawDemosaicProcessor {
profileToEngineTransform = profileToEngineTransform,
rawToneMappingParameters = rawToneMappingParameters,
useProfileExposureRamp = useProfileExposureRamp,
applyProfileDcpBaselineExposureOffset = applyDcpBaselineExposureOffset
applyProfileDcpBaselineExposureOffset = applyDcpBaselineExposureOffset,
supportProfileOverrange = supportProfileOverrange
)
onExposurePreviewSolved?.invoke(solvedExposureEv)
return@withContext null
@@ -1813,16 +1821,18 @@ class RawDemosaicProcessor {
"profileExposureEv=${profileExposureUniforms.exposureEv} " +
"defaultBlackRender=${if (useProfileExposureRamp) dcpDefaultBlackRenderOrAuto(activeDcpRenderPlan) else DcpDefaultBlackRender.None} " +
"profileRampBlack=${profileExposureUniforms.rampBlack} " +
"profileSupportOverrange=${profileExposureUniforms.supportOverrange} " +
"dngShadowScale=${actualMetadata.shadowScale} " +
"dngBaselineExposure=${actualMetadata.baselineExposure} " +
"linearCameraWhite=${linearCameraWhite.contentToString()} " +
"dngBaselineExposureInLinear=$applyLinearDngBaselineExposure " +
"dngBaselineExposureInProfileRamp=$applyProfileDngBaselineExposure " +
"profileGainTableMapActive=$hasProfileGainTableMap " +
"dcpBaselineExposureOffsetApplied=$applyDcpBaselineExposureOffset"
)
// 运行 linearRcdProgram 将相机矩阵应用在已解马赛克的浮点 RCD 图像上。
// DNG BaselineExposure 按规范在线性 ProPhoto/RIMM 阶段应用,PGTM 只作为可选局部 tone map。
// AdobeCurve keeps BaselineExposure for the DNG SDK exposure ramp. Linear-domain
// engines apply the same EV as an exact 2^EV gain in this ProPhoto/RIMM pass.
checkGlError("Before LinearRcdPass")
renderLinearRcdPass(
@@ -1837,7 +1847,13 @@ class RawDemosaicProcessor {
hueSatMap = activeDcpRenderPlan?.hueSatMap,
applyDngBaselineExposure = applyLinearDngBaselineExposure,
applyProfileGainTableMap = hasProfileGainTableMap,
clampProfileRgb = useAdobeProfilePipeline,
profileBaselineExposureOffsetEv = if (applyDcpBaselineExposureOffset) {
dcpBaselineExposureOffsetOrZero(activeDcpRenderPlan)
} else {
0f
},
clampProfileRgb = clampProfileRgb,
supportProfileOverrange = supportProfileOverrange,
label = "LinearRcdPass"
)
@@ -2525,6 +2541,8 @@ class RawDemosaicProcessor {
uniform vec3 uCameraWhite;
uniform float uExposureGain;
uniform int uClampProfileRgb;
uniform int uClampProfileGainOutput;
uniform int uProfileSupportOverrange;
uniform int uProfileGainEnabled;
uniform ivec3 uProfileGainTableSize;
uniform vec4 uProfileGainGrid;
@@ -2615,13 +2633,14 @@ class RawDemosaicProcessor {
rgb,
uLinearDcpHueSatMap,
uLinearDcpHueSatDivisions,
uLinearDcpHueSatEncoding
uLinearDcpHueSatEncoding,
uProfileSupportOverrange != 0
);
}
vec3 profileInputRgb = rgb * uProfileGainBaselineGain;
float tableInput = profileGainTableInput(profileInputRgb);
rgb *= profileGain(profileInputRgb, tableInput);
if (uClampProfileRgb != 0) {
if (uClampProfileGainOutput != 0) {
rgb = clamp(rgb, vec3(0.0), vec3(1.0));
}
rgb *= uExposureGain;
@@ -4034,7 +4053,9 @@ class RawDemosaicProcessor {
),
applyDngBaselineExposure = false,
applyProfileGainTableMap = false,
profileBaselineExposureOffsetEv = 0f,
clampProfileRgb = false,
supportProfileOverrange = false,
label = label
)
if (rawTextureId != 0) {
@@ -6942,6 +6963,7 @@ class RawDemosaicProcessor {
workingColorSpace = basePlan?.workingColorSpace ?: workingColorSpace,
baselineExposureOffset = basePlan?.baselineExposureOffset ?: 0f,
defaultBlackRender = DcpDefaultBlackRender.Auto,
supportsOverrange = basePlan?.supportsOverrange ?: false,
colorCorrectionMatrix = basePlan?.colorCorrectionMatrix ?: metadata.colorCorrectionMatrix,
cameraWhite = basePlan?.cameraWhite ?: metadata.cameraWhite,
hueSatMap = basePlan?.hueSatMap,
@@ -6961,6 +6983,7 @@ class RawDemosaicProcessor {
workingColorSpace = basePlan?.workingColorSpace ?: workingColorSpace,
baselineExposureOffset = basePlan?.baselineExposureOffset ?: 0f,
defaultBlackRender = basePlan?.defaultBlackRender ?: DcpDefaultBlackRender.Auto,
supportsOverrange = basePlan?.supportsOverrange ?: false,
colorCorrectionMatrix = basePlan?.colorCorrectionMatrix ?: metadata.colorCorrectionMatrix,
cameraWhite = basePlan?.cameraWhite ?: metadata.cameraWhite,
hueSatMap = basePlan?.hueSatMap,
@@ -7054,6 +7077,7 @@ class RawDemosaicProcessor {
"look=$lookEnabled " +
"profileToneCurve=$profileToneCurveEnabled " +
"profileExposureRamp=$useProfileExposureRamp " +
"profileSupportsOverrange=${dcpRenderPlan?.supportsOverrange == true} " +
"defaultBlackRender=$defaultBlackRender " +
"baselineExposureOffset=$dcpBaselineExposureOffset " +
"cameraWhite=${cameraWhite.contentToString()} " +
@@ -7075,7 +7099,7 @@ class RawDemosaicProcessor {
}
private fun sanitizeDngShadowScale(shadowScale: Float): Float {
return if (shadowScale.isFinite() && shadowScale > 0f && shadowScale <= 1f) {
return if (shadowScale.isFinite() && shadowScale > 0f) {
shadowScale
} else {
1f
@@ -7121,6 +7145,7 @@ class RawDemosaicProcessor {
DcpDefaultBlackRender.None
},
shadowScale = metadata.shadowScale,
supportOverrange = useRamp && dcpRenderPlan?.supportsOverrange == true,
useRamp = useRamp
)
}
@@ -7154,7 +7179,9 @@ class RawDemosaicProcessor {
hueSatMap: DcpHueSatMap? = null,
applyDngBaselineExposure: Boolean,
applyProfileGainTableMap: Boolean,
profileBaselineExposureOffsetEv: Float,
clampProfileRgb: Boolean,
supportProfileOverrange: Boolean,
label: String
) {
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFramebufferId)
@@ -7191,18 +7218,27 @@ class RawDemosaicProcessor {
)
bindLinearDcpHueSatMap(
program = linearRcdProgram,
hueSatMap = hueSatMap?.takeIf { clampProfileRgb },
hueSatMap = hueSatMap,
)
bindProfileGainTableMapForLinearRcd(
linearRcdProgram,
metadata,
hasActiveProfileGainTableMap
hasActiveProfileGainTableMap,
profileBaselineExposureOffsetEv
)
GLES30.glUniform1f(GLES30.glGetUniformLocation(linearRcdProgram, "uExposureGain"), exposureGain)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(linearRcdProgram, "uClampProfileRgb"),
if (clampProfileRgb) 1 else 0
)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(linearRcdProgram, "uClampProfileGainOutput"),
if (clampProfileRgb && !supportProfileOverrange) 1 else 0
)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(linearRcdProgram, "uProfileSupportOverrange"),
if (supportProfileOverrange) 1 else 0
)
val identityMatrix = FloatArray(16)
GlMatrix.setIdentityM(identityMatrix, 0)
@@ -7255,7 +7291,8 @@ class RawDemosaicProcessor {
private fun bindProfileGainTableMapForLinearRcd(
program: Int,
metadata: RawMetadata,
applyProfileGainTableMap: Boolean
applyProfileGainTableMap: Boolean,
profileBaselineExposureOffsetEv: Float
) {
val profileGainTableMap = metadata.profileGainTableMap?.takeIf { it.isValid }
if (profileGainTableMap == null || !applyProfileGainTableMap) {
@@ -7307,10 +7344,14 @@ class RawDemosaicProcessor {
GLES30.glGetUniformLocation(program, "uProfileGainGamma"),
profileGainTableMap.gamma.coerceIn(0.125f, 8.0f)
)
// Match the DNG SDK PGTM path: scale table input by DNG baseline exposure only.
// dng_render_task uses TotalBaselineExposure, including the selected profile's
// BaselineExposureOffset, for PGTM input weighting.
val totalProfileBaselineExposureEv =
DngBaselineExposure.sanitize(metadata.baselineExposure) +
(profileBaselineExposureOffsetEv.takeIf { it.isFinite() } ?: 0f)
GLES30.glUniform1f(
GLES30.glGetUniformLocation(program, "uProfileGainBaselineGain"),
exactDngBaselineExposureGain(metadata)
2.0f.pow(totalProfileBaselineExposureEv)
)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(program, "uProfileGainDebugOverlay"),
@@ -7399,6 +7440,7 @@ class RawDemosaicProcessor {
cameraWhite: FloatArray,
dcpRenderPlan: DcpRenderPlan?,
applyLinearDngBaselineExposure: Boolean,
applyProfileDngBaselineExposure: Boolean,
applyProfileGainTableMap: Boolean,
clampProfileRgb: Boolean,
outputBounds: Rect,
@@ -7409,7 +7451,8 @@ class RawDemosaicProcessor {
profileToEngineTransform: FloatArray,
rawToneMappingParameters: RawToneMappingParameters,
useProfileExposureRamp: Boolean,
applyProfileDcpBaselineExposureOffset: Boolean
applyProfileDcpBaselineExposureOffset: Boolean,
supportProfileOverrange: Boolean
): Float? {
val previewSize = resolveLongEdgePreviewSize(
sourceWidth = metadata.width,
@@ -7420,6 +7463,36 @@ class RawDemosaicProcessor {
val previewHeight = previewSize.height
return try {
setupLinearExposurePreviewFramebuffer(previewWidth, previewHeight)
val useAdobeSdkExposure =
colorEngine == RawRenderingEngine.AdobeCurve && useProfileExposureRamp
val profileBaselineExposureOffsetEv = if (applyProfileDcpBaselineExposureOffset) {
dcpBaselineExposureOffsetOrZero(dcpRenderPlan)
} else {
0f
}
if (useAdobeSdkExposure) {
renderLinearRcdPass(
metadata = metadata,
sourceTextureId = sourceTextureId,
targetFramebufferId = linearExposurePreviewFramebufferId,
viewportWidth = previewWidth,
viewportHeight = previewHeight,
rawExposureCompensation = 0f,
colorCorrectionMatrix = colorCorrectionMatrix,
cameraWhite = cameraWhite,
hueSatMap = dcpRenderPlan?.hueSatMap,
applyDngBaselineExposure = false,
applyProfileGainTableMap = applyProfileGainTableMap,
profileBaselineExposureOffsetEv = profileBaselineExposureOffsetEv,
clampProfileRgb = clampProfileRgb,
supportProfileOverrange = supportProfileOverrange,
label = "AdobeExposurePreviewLinearPass"
)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
GLES31.glMemoryBarrier(
GLES31.GL_FRAMEBUFFER_BARRIER_BIT or GLES31.GL_TEXTURE_FETCH_BARRIER_BIT
)
}
setupCombinedFramebuffer(previewWidth, previewHeight)
val previewBounds = scaleExposurePreviewBounds(
bounds = outputBounds,
@@ -7445,7 +7518,9 @@ class RawDemosaicProcessor {
"source=WRITER_INPUT_BUFFER " +
"layout=${if (samplesPerPixel >= 3) "LINEAR_RAW_RGB" else "CFA_RAW"} " +
"samplesPerPixel=$samplesPerPixel size=${metadata.width}x${metadata.height} " +
"sourceBaselineEv=$sourceBaselineEv candidateDomain=LINEAR_BASELINE"
"sourceBaselineEv=$sourceBaselineEv " +
"profileBaselineExposureOffsetEv=$profileBaselineExposureOffsetEv " +
"candidateDomain=${if (useAdobeSdkExposure) "ADOBE_DNG_SDK" else "LINEAR_GAIN"}"
)
try {
request.solve { exposureEv ->
@@ -7455,38 +7530,43 @@ class RawDemosaicProcessor {
)
PLog.d(
TAG,
"RAW viewfinder linear candidate: sourceBaselineEv=$sourceBaselineEv " +
"RAW viewfinder candidate: domain=" +
"${if (useAdobeSdkExposure) "ADOBE_DNG_SDK" else "LINEAR_GAIN"} " +
"sourceBaselineEv=$sourceBaselineEv " +
"offsetEv=$clampedExposureEv " +
"effectiveBaselineEv=${sourceBaselineEv + clampedExposureEv}"
)
// Render the exact baseline that would be written to the DNG. BaselineExposure
// belongs to the linear RCD stage; applying the candidate in the profile ramp
// would produce a different image than rendering the written DNG.
renderLinearRcdPass(
metadata = metadata,
sourceTextureId = sourceTextureId,
targetFramebufferId = linearExposurePreviewFramebufferId,
viewportWidth = previewWidth,
viewportHeight = previewHeight,
rawExposureCompensation = clampedExposureEv,
colorCorrectionMatrix = colorCorrectionMatrix,
cameraWhite = cameraWhite,
hueSatMap = dcpRenderPlan?.hueSatMap,
applyDngBaselineExposure = applyLinearDngBaselineExposure,
applyProfileGainTableMap = applyProfileGainTableMap,
clampProfileRgb = clampProfileRgb,
label = "LinearExposurePreviewPass"
)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
GLES31.glMemoryBarrier(
GLES31.GL_FRAMEBUFFER_BARRIER_BIT or GLES31.GL_TEXTURE_FETCH_BARRIER_BIT
"effectiveBaselineEv=${sourceBaselineEv + clampedExposureEv} " +
"totalProfileExposureEv=" +
"${sourceBaselineEv + profileBaselineExposureOffsetEv + clampedExposureEv}"
)
if (!useAdobeSdkExposure) {
renderLinearRcdPass(
metadata = metadata,
sourceTextureId = sourceTextureId,
targetFramebufferId = linearExposurePreviewFramebufferId,
viewportWidth = previewWidth,
viewportHeight = previewHeight,
rawExposureCompensation = clampedExposureEv,
colorCorrectionMatrix = colorCorrectionMatrix,
cameraWhite = cameraWhite,
hueSatMap = dcpRenderPlan?.hueSatMap,
applyDngBaselineExposure = applyLinearDngBaselineExposure,
applyProfileGainTableMap = applyProfileGainTableMap,
profileBaselineExposureOffsetEv = profileBaselineExposureOffsetEv,
clampProfileRgb = clampProfileRgb,
supportProfileOverrange = supportProfileOverrange,
label = "LinearExposurePreviewPass"
)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
GLES31.glMemoryBarrier(
GLES31.GL_FRAMEBUFFER_BARRIER_BIT or GLES31.GL_TEXTURE_FETCH_BARRIER_BIT
)
}
renderToneMappedExposurePreviewFrame(
metadata = metadata,
linearTextureId = linearExposurePreviewTextureId,
width = previewWidth,
height = previewHeight,
exposureEv = 0f,
exposureEv = if (useAdobeSdkExposure) clampedExposureEv else 0f,
dcpRenderPlan = dcpRenderPlan,
spectralFilmLut = spectralFilmLut,
colorEngine = colorEngine,
@@ -7497,7 +7577,8 @@ class RawDemosaicProcessor {
rawWhitesAdjustment = rawWhitePointCorrection,
useProfileExposureRamp = useProfileExposureRamp,
applyProfileDcpBaselineExposureOffset = applyProfileDcpBaselineExposureOffset,
applyProfileDngBaselineExposure = false,
applyProfileDngBaselineExposure =
useAdobeSdkExposure && applyProfileDngBaselineExposure,
readbackBounds = previewBounds,
readbackWidth = request.width.coerceAtLeast(1),
readbackHeight = request.height.coerceAtLeast(1),
@@ -18,6 +18,7 @@ object RawProfileExposureGl {
val rampBlack: Float,
val rampRadius: Float,
val rampQScale: Float,
val supportOverrange: Boolean,
val toneEnabled: Boolean,
val toneSlope: Float,
val toneA: Float,
@@ -33,6 +34,7 @@ object RawProfileExposureGl {
rampBlack = 0f,
rampRadius = 0f,
rampQScale = 0f,
supportOverrange = false,
toneEnabled = false,
toneSlope = 1f,
toneA = 0f,
@@ -49,6 +51,7 @@ object RawProfileExposureGl {
defaultBlackRender: DcpDefaultBlackRender = DcpDefaultBlackRender.None,
blackRenderShadows: Float = DEFAULT_BLACK_RENDER_SHADOWS,
shadowScale: Float = 1f,
supportOverrange: Boolean = false,
useRamp: Boolean
): Uniforms {
val exposureEv = profileExposureCompensation + dngBaselineExposure + dcpBaselineExposureOffset
@@ -62,6 +65,7 @@ object RawProfileExposureGl {
rampBlack = 0f,
rampRadius = 0f,
rampQScale = 0f,
supportOverrange = false,
toneEnabled = false,
toneSlope = 1f,
toneA = 0f,
@@ -72,7 +76,7 @@ object RawProfileExposureGl {
val positiveExposureEv = max(0f, exposureEv)
val white = 1f / 2.0f.pow(positiveExposureEv)
val safeShadowScale = if (shadowScale.isFinite() && shadowScale > 0f && shadowScale <= 1f) {
val safeShadowScale = if (shadowScale.isFinite() && shadowScale > 0f) {
shadowScale
} else {
DEFAULT_BLACK_RENDER_SHADOW_SCALE
@@ -98,6 +102,7 @@ object RawProfileExposureGl {
rampBlack = black,
rampRadius = radius,
rampQScale = qScale,
supportOverrange = supportOverrange,
toneEnabled = false,
toneSlope = 1f,
toneA = 0f,
@@ -118,6 +123,7 @@ object RawProfileExposureGl {
rampBlack = black,
rampRadius = radius,
rampQScale = qScale,
supportOverrange = supportOverrange,
toneEnabled = true,
toneSlope = toneSlope,
toneA = toneA,
@@ -151,6 +157,10 @@ object RawProfileExposureGl {
GLES30.glGetUniformLocation(program, "uProfileExposureRampQScale"),
exposure.rampQScale
)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(program, "uProfileExposureSupportOverrange"),
if (exposure.supportOverrange) 1 else 0
)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(program, "uProfileExposureToneEnabled"),
if (exposure.toneEnabled) 1 else 0
@@ -330,6 +330,7 @@ object RawShaders {
uniform float uProfileExposureRampBlack;
uniform float uProfileExposureRampRadius;
uniform float uProfileExposureRampQScale;
uniform bool uProfileExposureSupportOverrange;
uniform bool uProfileExposureToneEnabled;
uniform float uProfileExposureToneSlope;
uniform float uProfileExposureToneA;
@@ -349,7 +350,7 @@ object RawShaders {
""".trimIndent()
private val CURVE_COMBINED_FUNCTIONS = """
float sampleCurve(float value) {
float sampleBaseCurve(float value) {
if (!uCurveEnabled || uCurveSize <= 1.0) {
return value;
}
@@ -358,12 +359,31 @@ object RawShaders {
return texture(uCurveTexture, vec2(coordX, 0.5)).r;
}
float sampleProfileToneCurve(float value) {
value = clamp(value, 0.0, 1.0);
return sampleBaseCurve(applyProfileExposureToneValue(value));
}
float sampleProfileToneCurveOverrange(float value) {
if (!(value > 1.0)) {
return sampleProfileToneCurve(max(value, 0.0));
}
const float dx = 0.001;
float y1 = sampleProfileToneCurve(1.0);
float slope = (y1 - sampleProfileToneCurve(1.0 - dx)) / dx;
return y1 + slope * (value - 1.0);
}
void adobeRgbTone(inout float maxValue, inout float midValue, inout float minValue) {
float oldMax = maxValue;
float oldMid = midValue;
float oldMin = minValue;
maxValue = sampleCurve(oldMax);
minValue = sampleCurve(oldMin);
maxValue = uProfileExposureSupportOverrange
? sampleProfileToneCurveOverrange(oldMax)
: sampleProfileToneCurve(oldMax);
minValue = uProfileExposureSupportOverrange
? sampleProfileToneCurveOverrange(oldMin)
: sampleProfileToneCurve(oldMin);
if (abs(oldMax - oldMin) < 1e-6) {
midValue = minValue;
} else {
@@ -372,7 +392,13 @@ object RawShaders {
}
vec3 applyAdobeCurve(vec3 color) {
color = clamp(color, vec3(0.0), vec3(1.0));
color = uProfileExposureSupportOverrange
? vec3(
encodeProfileOverrangeValue(color.r),
encodeProfileOverrangeValue(color.g),
encodeProfileOverrangeValue(color.b)
)
: clamp(color, vec3(0.0), vec3(1.0));
float r = color.r;
float g = color.g;
float b = color.b;
@@ -385,8 +411,12 @@ object RawShaders {
} else if (b > g) {
adobeRgbTone(r, b, g);
} else {
r = sampleCurve(r);
g = sampleCurve(g);
r = uProfileExposureSupportOverrange
? sampleProfileToneCurveOverrange(r)
: sampleProfileToneCurve(r);
g = uProfileExposureSupportOverrange
? sampleProfileToneCurveOverrange(g)
: sampleProfileToneCurve(g);
b = g;
}
} else {
@@ -399,20 +429,34 @@ object RawShaders {
}
}
return clamp(vec3(r, g, b), vec3(0.0), vec3(1.0));
vec3 toned = vec3(r, g, b);
if (uProfileExposureSupportOverrange) {
return vec3(
decodeProfileOverrangeValue(toned.r),
decodeProfileOverrangeValue(toned.g),
decodeProfileOverrangeValue(toned.b)
);
}
return clamp(toned, vec3(0.0), vec3(1.0));
}
""".trimIndent()
private val ADOBE_PROFILE_COMBINED_FUNCTIONS = """
const float PROFILE_HIGHLIGHT_SHOULDER_START = 0.58;
const float PROFILE_HIGHLIGHT_SHOULDER_SOFTNESS = 0.30;
const float PROFILE_HIGHLIGHT_NEUTRAL_BLEND_MAX = 0.71;
const float PROFILE_HIGHLIGHT_NEUTRAL_BLEND_POWER = 1.91;
float clampDcpTableCoordinate(float value) {
return clamp(value, 0.0, 1.0);
}
float encodeProfileOverrangeValue(float value) {
value = max(value, 0.0);
return value * (256.0 + value) / (256.0 * (1.0 + value));
}
float decodeProfileOverrangeValue(float value) {
value = max(value, 0.0);
float discriminant = max(64.0 * value * value - 127.0 * value + 64.0, 0.0);
return 16.0 * (8.0 * value - 8.0 + sqrt(discriminant));
}
float encodeLookupValue(float value, int encoding) {
value = clampDcpTableCoordinate(value);
if (encoding == 1) {
@@ -540,7 +584,16 @@ object RawShaders {
}
vec3 applyDcpHsvMap(vec3 color, sampler3D tableTexture, ivec3 divisions, int encoding) {
vec3 hsv = rgbToDcpHsv(color);
bool encodeOverrange = uProfileExposureSupportOverrange && divisions.z > 1;
vec3 mapColor = max(color, vec3(0.0));
if (encodeOverrange) {
mapColor = vec3(
encodeProfileOverrangeValue(mapColor.r),
encodeProfileOverrangeValue(mapColor.g),
encodeProfileOverrangeValue(mapColor.b)
);
}
vec3 hsv = rgbToDcpHsv(mapColor);
float lookupValue = hsv.z;
float vEncoded = hsv.z;
if (encoding == 1 && divisions.z > 1) {
@@ -559,7 +612,15 @@ object RawShaders {
hsv.z = vEncoded;
}
return dcpHsvToRgb(hsv);
vec3 mapped = dcpHsvToRgb(hsv);
if (encodeOverrange) {
mapped = vec3(
decodeProfileOverrangeValue(mapped.r),
decodeProfileOverrangeValue(mapped.g),
decodeProfileOverrangeValue(mapped.b)
);
}
return mapped;
}
float applyProfileExposureRampValue(float value) {
@@ -569,7 +630,8 @@ object RawShaders {
return 0.0;
}
if (value >= black + radius) {
return max((value - black) * uProfileExposureRampSlope, 0.0);
float ramped = max((value - black) * uProfileExposureRampSlope, 0.0);
return uProfileExposureSupportOverrange ? ramped : min(ramped, 1.0);
}
float y = value - (black - radius);
return uProfileExposureRampQScale * y * y;
@@ -584,19 +646,7 @@ object RawShaders {
applyProfileExposureRampValue(color.g),
applyProfileExposureRampValue(color.b)
);
float peak = max(ramped.r, max(ramped.g, ramped.b));
if (peak <= PROFILE_HIGHLIGHT_SHOULDER_START) {
return ramped;
}
float shoulderInput = peak - PROFILE_HIGHLIGHT_SHOULDER_START;
float shoulderAmount = shoulderInput / (shoulderInput + PROFILE_HIGHLIGHT_SHOULDER_SOFTNESS);
float compressedPeak = PROFILE_HIGHLIGHT_SHOULDER_START +
(1.0 - PROFILE_HIGHLIGHT_SHOULDER_START) *
shoulderAmount;
vec3 compressed = ramped * (compressedPeak / max(peak, 1e-6));
float neutralBlend = PROFILE_HIGHLIGHT_NEUTRAL_BLEND_MAX *
pow(shoulderAmount, PROFILE_HIGHLIGHT_NEUTRAL_BLEND_POWER);
return mix(compressed, vec3(compressedPeak), neutralBlend);
return ramped;
}
float applyProfileExposureToneValue(float value) {
@@ -610,17 +660,6 @@ object RawShaders {
uProfileExposureToneC;
}
vec3 applyProfileExposureTone(vec3 color) {
if (!uProfileExposureRampEnabled) {
return color;
}
return vec3(
applyProfileExposureToneValue(color.r),
applyProfileExposureToneValue(color.g),
applyProfileExposureToneValue(color.b)
);
}
vec3 applyDcpHueSatMap(vec3 color) {
if (uDcpHueSatEnabled) {
color = applyDcpHsvMap(color, uDcpHueSatTexture, uDcpHueSatDivisions, uDcpHueSatEncoding);
@@ -640,7 +679,6 @@ object RawShaders {
if (uProfileExposureRampEnabled) {
color = applyProfileExposureRamp(color);
color = applyDcpLookTable(color);
color = applyProfileExposureTone(color);
} else {
color = applyDcpLookTable(color);
color = applyProfileExposureRamp(color);