feat: implement shader-based lens shading using DNG gain maps
This commit is contained in:
1 parent
5078f56188
commit
c7efb32f52
3 files changed
+94
-4
No files matched your search
@@ -557,6 +557,65 @@ static bool applySupportedDngGainMaps(LibRaw &rawProcessor, const float blackLev
|
||||
return true;
|
||||
}
|
||||
|
||||
static jfloatArray buildDngLensShadingArray(JNIEnv *env, LibRaw &rawProcessor,
|
||||
int &outWidth, int &outHeight) {
|
||||
outWidth = 0;
|
||||
outHeight = 0;
|
||||
|
||||
std::vector<DngGainMap> gainMaps;
|
||||
if (!parseDngGainMaps(rawProcessor.imgdata.color.dng_levels.rawopcodes[1], gainMaps) ||
|
||||
gainMaps.size() != 4) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const uint32_t mapWidth = gainMaps[0].mapPointsH;
|
||||
const uint32_t mapHeight = gainMaps[0].mapPointsV;
|
||||
if (mapWidth == 0 || mapHeight == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (const auto &gainMap : gainMaps) {
|
||||
if (gainMap.mapPointsH != mapWidth || gainMap.mapPointsV != mapHeight ||
|
||||
gainMap.mapPlanes != 1 || gainMap.mapGain.empty()) {
|
||||
LOGI("dng gain map export: incompatible map layout");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<float> packed(static_cast<size_t>(mapWidth) * mapHeight * 4, 1.0f);
|
||||
for (const auto &gainMap : gainMaps) {
|
||||
int cfa = rawProcessor.FC(gainMap.top, gainMap.left);
|
||||
int outChannel;
|
||||
if (cfa == 0) {
|
||||
outChannel = 0; // R
|
||||
} else if (cfa == 1) {
|
||||
outChannel = 1; // Gr
|
||||
} else if (cfa == 2) {
|
||||
outChannel = 3; // B in LibRaw CFA order, packed as A
|
||||
} else {
|
||||
outChannel = 2; // Gb
|
||||
}
|
||||
|
||||
for (uint32_t y = 0; y < mapHeight; ++y) {
|
||||
for (uint32_t x = 0; x < mapWidth; ++x) {
|
||||
const size_t src = static_cast<size_t>(y) * mapWidth + x;
|
||||
const size_t dst = src * 4 + static_cast<size_t>(outChannel);
|
||||
packed[dst] = gainMap.mapGain[src];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
outWidth = static_cast<int>(mapWidth);
|
||||
outHeight = static_cast<int>(mapHeight);
|
||||
jfloatArray result = env->NewFloatArray(static_cast<jsize>(packed.size()));
|
||||
if (!result) {
|
||||
return nullptr;
|
||||
}
|
||||
env->SetFloatArrayRegion(result, 0, static_cast<jsize>(packed.size()), packed.data());
|
||||
LOGI("dng gain map export: %dx%d", outWidth, outHeight);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool estimateRawAutoWhiteBalance(LibRaw &rawProcessor, const float blackLevels[4],
|
||||
float outUserMul[4], int &sampleCount) {
|
||||
sampleCount = 0;
|
||||
@@ -1952,9 +2011,12 @@ Java_com_hinnka_mycamera_raw_RawDemosaicProcessor_processDngNative(
|
||||
float dngBlackLevels[4] = {};
|
||||
computeEffectiveBlackLevels(RawProcessor, dngBlackLevels);
|
||||
LOGI("dng black levels (metadata): %f %f %f %f", dngBlackLevels[0], dngBlackLevels[1], dngBlackLevels[2], dngBlackLevels[3]);
|
||||
const bool appliedDngGainMap = applySupportedDngGainMaps(RawProcessor, dngBlackLevels);
|
||||
LOGI("dng gain map: opcode2_len=%u applied=%d", RawProcessor.imgdata.color.dng_levels.rawopcodes[1].len,
|
||||
appliedDngGainMap ? 1 : 0);
|
||||
int exportedLscWidth = 0;
|
||||
int exportedLscHeight = 0;
|
||||
jfloatArray exportedLscArray = buildDngLensShadingArray(env, RawProcessor, exportedLscWidth, exportedLscHeight);
|
||||
LOGI("dng gain map: opcode2_len=%u native_apply=0 exported=%d",
|
||||
RawProcessor.imgdata.color.dng_levels.rawopcodes[1].len,
|
||||
exportedLscArray ? 1 : 0);
|
||||
|
||||
RawProcessor.imgdata.params.output_bps = 16;
|
||||
RawProcessor.imgdata.params.gamm[0] = 1.0; // Linear
|
||||
@@ -2341,7 +2403,7 @@ Java_com_hinnka_mycamera_raw_RawDemosaicProcessor_processDngNative(
|
||||
jobject dngData = env->NewObject(
|
||||
dngDataClass, constructor, rawDataBuffer, width, height, rowStride,
|
||||
whiteLevel, blackLevelArray, preMulArray, wbArray, colorMatrixArray,
|
||||
cfaPattern, 0, baselineExposure, nullptr, 0, 0, exposureBias, iso,
|
||||
cfaPattern, 0, baselineExposure, exportedLscArray, exportedLscWidth, exportedLscHeight, exposureBias, iso,
|
||||
shutterSpeedLong, aperture, activeArray, noiseProfileArray,
|
||||
embeddedPreviewBitmap);
|
||||
|
||||
|
||||
@@ -1918,6 +1918,22 @@ class RawDemosaicProcessor {
|
||||
)
|
||||
checkGlError("renderCombinedPass base uniforms")
|
||||
|
||||
val hasLensShading = metadata.lensShadingMap != null &&
|
||||
metadata.lensShadingMapWidth > 0 &&
|
||||
metadata.lensShadingMapHeight > 0
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE4)
|
||||
if (hasLensShading) {
|
||||
uploadLensShadingTexture(metadata)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, lensShadingTextureId)
|
||||
} else {
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, createDummyShadingTexture())
|
||||
}
|
||||
GLES30.glUniform1i(GLES30.glGetUniformLocation(combinedProgram, "uLensShadingMap"), 4)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(combinedProgram, "uLensShadingEnabled"),
|
||||
if (hasLensShading) 1 else 0
|
||||
)
|
||||
|
||||
bindDcpCombinedResources(dcpRenderPlan)
|
||||
|
||||
GLES30.glUniformMatrix3fv(
|
||||
|
||||
@@ -134,11 +134,13 @@ object RawShaders {
|
||||
|
||||
uniform sampler2D uInputTexture;
|
||||
uniform sampler2D uCurveTexture;
|
||||
uniform sampler2D uLensShadingMap;
|
||||
uniform sampler3D uDcpHueSatTexture;
|
||||
uniform sampler3D uDcpLookTableTexture;
|
||||
uniform mat3 uOutputTransform;
|
||||
uniform float uCurveSize;
|
||||
uniform bool uCurveEnabled;
|
||||
uniform bool uLensShadingEnabled;
|
||||
uniform bool uDcpHueSatEnabled;
|
||||
uniform bool uDcpLookTableEnabled;
|
||||
uniform ivec3 uDcpHueSatDivisions;
|
||||
@@ -150,6 +152,15 @@ object RawShaders {
|
||||
return max(dot(color, vec3(0.2126, 0.7152, 0.0722)), 1e-4);
|
||||
}
|
||||
|
||||
vec3 applyLensShading(vec3 color) {
|
||||
if (!uLensShadingEnabled) {
|
||||
return color;
|
||||
}
|
||||
vec4 gain = texture(uLensShadingMap, vTexCoord);
|
||||
vec3 rgbGain = vec3(gain.r, 0.5 * (gain.g + gain.b), gain.a);
|
||||
return color * max(rgbGain, vec3(0.0));
|
||||
}
|
||||
|
||||
float sampleCurve(float value) {
|
||||
if (!uCurveEnabled || uCurveSize <= 1.0) {
|
||||
return value;
|
||||
@@ -380,6 +391,7 @@ object RawShaders {
|
||||
|
||||
void main() {
|
||||
vec3 color = texture(uInputTexture, vTexCoord).rgb;
|
||||
color = applyLensShading(color);
|
||||
color = reinhardLocalTonemapping(color);
|
||||
|
||||
if (uDcpHueSatEnabled) {
|
||||
|
||||
Reference in new issue
Block a user