feat: new color recipe

This commit is contained in:
Hinnka committed 2026-01-16 23:13:06 +08:00
1 parent b990fd7741
commit c783b433a8
9 files changed
+206 -3

No files matched your search

+2
View File
@@ -13,6 +13,8 @@
- [ ] 闪光灯异常
- [ ] 影调配方
- [ ] 水印定制持久化
- [ ] 边框定制(5203...)
- [ ] 关闭锐化降噪
- [ ] raw 格式照片处理 / Halide
- [ ] 视频拍摄
@@ -65,6 +65,9 @@ class LutImageProcessor {
private var uVibranceLoc = 0
private var uHighlightsLoc = 0
private var uShadowsLoc = 0
private var uFilmGrainLoc = 0
private var uVignetteLoc = 0
private var uBleachBypassLoc = 0
/**
* 初始化 EGL 环境
@@ -175,6 +178,9 @@ class LutImageProcessor {
val vibrance = colorRecipeParams?.vibrance ?: 1f
val highlights = colorRecipeParams?.highlights ?: 0f
val shadows = colorRecipeParams?.shadows ?: 0f
val filmGrain = colorRecipeParams?.filmGrain ?: 0f
val vignette = colorRecipeParams?.vignette ?: 0f
val bleachBypass = colorRecipeParams?.bleachBypass ?: 0f
val intensity = colorRecipeParams?.lutIntensity ?: 1f
// 确保上下文激活
@@ -228,6 +234,9 @@ class LutImageProcessor {
GLES30.glUniform1f(uVibranceLoc, vibrance)
GLES30.glUniform1f(uHighlightsLoc, highlights)
GLES30.glUniform1f(uShadowsLoc, shadows)
GLES30.glUniform1f(uFilmGrainLoc, filmGrain)
GLES30.glUniform1f(uVignetteLoc, vignette)
GLES30.glUniform1f(uBleachBypassLoc, bleachBypass)
}
// 设置 MVP 矩阵(单位矩阵)
@@ -384,6 +393,9 @@ class LutImageProcessor {
uVibranceLoc = GLES30.glGetUniformLocation(shaderProgram, "uVibrance")
uHighlightsLoc = GLES30.glGetUniformLocation(shaderProgram, "uHighlights")
uShadowsLoc = GLES30.glGetUniformLocation(shaderProgram, "uShadows")
uFilmGrainLoc = GLES30.glGetUniformLocation(shaderProgram, "uFilmGrain")
uVignetteLoc = GLES30.glGetUniformLocation(shaderProgram, "uVignette")
uBleachBypassLoc = GLES30.glGetUniformLocation(shaderProgram, "uBleachBypass")
}
private fun compileShader(type: Int, source: String): Int {
@@ -515,6 +527,9 @@ class LutImageProcessor {
uniform float uVibrance; // 0.0 ~ 2.0 (蓝色增强)
uniform float uHighlights; // -1.0 ~ +1.0 (高光调整)
uniform float uShadows; // -1.0 ~ +1.0 (阴影调整)
uniform float uFilmGrain; // 0.0 ~ 1.0 (颗粒强度)
uniform float uVignette; // -1.0 ~ +1.0 (晕影)
uniform float uBleachBypass; // 0.0 ~ 1.0 (留银冲洗强度)
void main() {
// 从图片纹理采样原始颜色
@@ -569,6 +584,61 @@ class LutImageProcessor {
color.rgb += fadeAmount * 0.1;
}
// 8. 留银冲洗(Bleach Bypass - 胶片银盐保留效果)
if (uBleachBypass > 0.0) {
// 保留部分银盐:降低饱和度
float luma = dot(color.rgb, vec3(0.299, 0.587, 0.114));
vec3 desaturated = mix(color.rgb, vec3(luma), 0.6);
// 增强对比度
desaturated = (desaturated - 0.5) * 1.3 + 0.5;
// 色调偏移到冷色调(青绿色)
desaturated.r *= 0.95;
desaturated.g *= 1.02;
desaturated.b *= 1.05;
// 根据强度混合
color.rgb = mix(color.rgb, desaturated, uBleachBypass);
}
// 9. 晕影(Vignette - 边缘光线衰减/增强)
if (abs(uVignette) > 0.0) {
// 计算从中心到边缘的距离
vec2 center = vec2(0.5, 0.5);
float dist = distance(vTexCoord, center);
// 使用 smoothstep 创建平滑过渡
float vignetteMask = smoothstep(0.8, 0.3, dist);
// 根据 uVignette 符号决定是暗角还是亮角
if (uVignette < 0.0) {
// 暗角:边缘变暗(更强的效果:从0.1到1.0)
color.rgb *= mix(0.1, 1.0, vignetteMask) * abs(uVignette) + (1.0 + uVignette);
} else {
// 亮角:边缘变亮(增强效果)
color.rgb = mix(color.rgb, vec3(1.0), (1.0 - vignetteMask) * uVignette * 0.7);
}
}
// 10. 颗粒(Film Grain - 胶片颗粒感)
if (uFilmGrain > 0.0) {
// 使用纹理坐标生成伪随机噪声
float noise = fract(sin(dot(vTexCoord * 1000.0, vec2(12.9898, 78.233))) * 43758.5453);
// 将噪声从 [0,1] 映射到 [-1,1]
noise = (noise - 0.5) * 2.0;
// 根据亮度自适应调整颗粒强度
float luma = dot(color.rgb, vec3(0.299, 0.587, 0.114));
float grainMask = 1.0 - abs(luma - 0.5) * 2.0;
grainMask = grainMask * 0.5 + 0.5;
// 应用颗粒(增强强度)
float grainStrength = uFilmGrain * 0.1 * grainMask;
color.rgb += noise * grainStrength;
}
// Clamp 到合法范围
color.rgb = clamp(color.rgb, 0.0, 1.0);
}
@@ -45,6 +45,9 @@ class LutManager(private val context: Context) {
private fun vibranceKey(lutId: String) = floatPreferencesKey("${lutId}_vibrance")
private fun highlightsKey(lutId: String) = floatPreferencesKey("${lutId}_highlights")
private fun shadowsKey(lutId: String) = floatPreferencesKey("${lutId}_shadows")
private fun filmGrainKey(lutId: String) = floatPreferencesKey("${lutId}_filmGrain")
private fun vignetteKey(lutId: String) = floatPreferencesKey("${lutId}_vignette")
private fun bleachBypassKey(lutId: String) = floatPreferencesKey("${lutId}_bleachBypass")
private fun lutIntensityKey(lutId: String) = floatPreferencesKey("${lutId}_lutIntensity")
}
@@ -72,6 +75,9 @@ class LutManager(private val context: Context) {
vibrance = preferences[vibranceKey(lutId)] ?: 1f,
highlights = preferences[highlightsKey(lutId)] ?: 0f,
shadows = preferences[shadowsKey(lutId)] ?: 0f,
filmGrain = preferences[filmGrainKey(lutId)] ?: 0f,
vignette = preferences[vignetteKey(lutId)] ?: 0f,
bleachBypass = preferences[bleachBypassKey(lutId)] ?: 0f,
lutIntensity = preferences[lutIntensityKey(lutId)] ?: 1f
)
}
@@ -210,6 +216,9 @@ class LutManager(private val context: Context) {
preferences[vibranceKey(lutId)] = params.vibrance
preferences[highlightsKey(lutId)] = params.highlights
preferences[shadowsKey(lutId)] = params.shadows
preferences[filmGrainKey(lutId)] = params.filmGrain
preferences[vignetteKey(lutId)] = params.vignette
preferences[bleachBypassKey(lutId)] = params.bleachBypass
preferences[lutIntensityKey(lutId)] = params.lutIntensity
}
PLog.d(TAG, "Color recipe params saved for LUT [$lutId]: $params")
@@ -233,6 +242,9 @@ class LutManager(private val context: Context) {
vibrance = preferences[vibranceKey(lutId)] ?: 1f,
highlights = preferences[highlightsKey(lutId)] ?: 0f,
shadows = preferences[shadowsKey(lutId)] ?: 0f,
filmGrain = preferences[filmGrainKey(lutId)] ?: 0f,
vignette = preferences[vignetteKey(lutId)] ?: 0f,
bleachBypass = preferences[bleachBypassKey(lutId)] ?: 0f,
lutIntensity = preferences[lutIntensityKey(lutId)] ?: 1f
)
}.firstOrNull() ?: ColorRecipeParams.DEFAULT
@@ -264,6 +276,9 @@ class LutManager(private val context: Context) {
preferences.remove(vibranceKey(lutId))
preferences.remove(highlightsKey(lutId))
preferences.remove(shadowsKey(lutId))
preferences.remove(filmGrainKey(lutId))
preferences.remove(vignetteKey(lutId))
preferences.remove(bleachBypassKey(lutId))
preferences.remove(lutIntensityKey(lutId))
}
PLog.d(TAG, "Color recipe params deleted for LUT [$lutId]")
@@ -66,6 +66,9 @@ class LutRenderer : GLSurfaceView.Renderer {
private var uVibranceLocation: Int = 0
private var uHighlightsLocation: Int = 0
private var uShadowsLocation: Int = 0
private var uFilmGrainLocation: Int = 0
private var uVignetteLocation: Int = 0
private var uBleachBypassLocation: Int = 0
// Attribute 位置
private var aPositionLocation: Int = 0
@@ -115,6 +118,12 @@ class LutRenderer : GLSurfaceView.Renderer {
var highlights: Float = 0f // -1.0 ~ +1.0
@Volatile
var shadows: Float = 0f // -1.0 ~ +1.0
@Volatile
var filmGrain: Float = 0f // 0.0 ~ 1.0
@Volatile
var vignette: Float = 0f // -1.0 ~ +1.0
@Volatile
var bleachBypass: Float = 0f // 0.0 ~ 1.0
// 渲染尺寸
private var viewportWidth: Int = 0
@@ -248,6 +257,9 @@ class LutRenderer : GLSurfaceView.Renderer {
GLES30.glUniform1f(uVibranceLocation, vibrance)
GLES30.glUniform1f(uHighlightsLocation, highlights)
GLES30.glUniform1f(uShadowsLocation, shadows)
GLES30.glUniform1f(uFilmGrainLocation, filmGrain)
GLES30.glUniform1f(uVignetteLocation, vignette)
GLES30.glUniform1f(uBleachBypassLocation, bleachBypass)
}
// 绑定顶点缓冲
@@ -341,6 +353,9 @@ class LutRenderer : GLSurfaceView.Renderer {
uVibranceLocation = GLES30.glGetUniformLocation(programId, "uVibrance")
uHighlightsLocation = GLES30.glGetUniformLocation(programId, "uHighlights")
uShadowsLocation = GLES30.glGetUniformLocation(programId, "uShadows")
uFilmGrainLocation = GLES30.glGetUniformLocation(programId, "uFilmGrain")
uVignetteLocation = GLES30.glGetUniformLocation(programId, "uVignette")
uBleachBypassLocation = GLES30.glGetUniformLocation(programId, "uBleachBypass")
// 获取 Attribute 位置
aPositionLocation = GLES30.glGetAttribLocation(programId, "aPosition")
@@ -171,6 +171,11 @@ object Shaders {
uniform float uHighlights; // -1.0 ~ +1.0 (高光调整)
uniform float uShadows; // -1.0 ~ +1.0 (阴影调整)
// 色彩配方参数(阶段3:质感效果)
uniform float uFilmGrain; // 0.0 ~ 1.0 (颗粒强度)
uniform float uVignette; // -1.0 ~ +1.0 (晕影,负值暗角,正值亮角)
uniform float uBleachBypass; // 0.0 ~ 1.0 (留银冲洗强度)
void main() {
// 从相机纹理采样原始颜色
vec4 color = texture(uCameraTexture, vTexCoord);
@@ -226,6 +231,63 @@ object Shaders {
color.rgb += fadeAmount * 0.1;
}
// 8. 留银冲洗(Bleach Bypass - 胶片银盐保留效果)
if (uBleachBypass > 0.0) {
// 保留部分银盐:降低饱和度
float luma = dot(color.rgb, vec3(0.299, 0.587, 0.114));
vec3 desaturated = mix(color.rgb, vec3(luma), 0.6);
// 增强对比度
desaturated = (desaturated - 0.5) * 1.3 + 0.5;
// 色调偏移到冷色调(青绿色)
desaturated.r *= 0.95;
desaturated.g *= 1.02;
desaturated.b *= 1.05;
// 根据强度混合
color.rgb = mix(color.rgb, desaturated, uBleachBypass);
}
// 9. 晕影(Vignette - 边缘光线衰减/增强)
if (abs(uVignette) > 0.0) {
// 计算从中心到边缘的距离
vec2 center = vec2(0.5, 0.5);
float dist = distance(vTexCoord, center);
// 使用 smoothstep 创建平滑过渡
// 调整衰减曲线:从0.3到0.8的范围
float vignetteMask = smoothstep(0.8, 0.3, dist);
// 根据 uVignette 符号决定是暗角还是亮角
if (uVignette < 0.0) {
// 暗角:边缘变暗(更强的效果:从0.1到1.0)
color.rgb *= mix(0.1, 1.0, vignetteMask) * abs(uVignette) + (1.0 + uVignette);
} else {
// 亮角:边缘变亮(增强效果)
color.rgb = mix(color.rgb, vec3(1.0), (1.0 - vignetteMask) * uVignette * 0.7);
}
}
// 10. 颗粒(Film Grain - 胶片颗粒感)
if (uFilmGrain > 0.0) {
// 使用纹理坐标生成伪随机噪声
// 基于片段位置的简单哈希函数
float noise = fract(sin(dot(vTexCoord * 1000.0, vec2(12.9898, 78.233))) * 43758.5453);
// 将噪声从 [0,1] 映射到 [-1,1]
noise = (noise - 0.5) * 2.0;
// 根据亮度自适应调整颗粒强度(高光和阴影更明显)
float luma = dot(color.rgb, vec3(0.299, 0.587, 0.114));
float grainMask = 1.0 - abs(luma - 0.5) * 2.0; // 中间调弱,高光阴影强
grainMask = grainMask * 0.5 + 0.5; // 调整到 [0.5, 1.0] 范围
// 应用颗粒(增强强度)
float grainStrength = uFilmGrain * 0.1 * grainMask;
color.rgb += noise * grainStrength;
}
// Clamp 到合法范围
color.rgb = clamp(color.rgb, 0.0, 1.0);
}
@@ -15,6 +15,9 @@ data class ColorRecipeParams(
val vibrance: Float = 1f, // 0.0 ~ 2.0 (蓝色增强/自然饱和度,1为无调整)
val highlights: Float = 0f, // -1.0 ~ +1.0 (高光调整,0为无调整)
val shadows: Float = 0f, // -1.0 ~ +1.0 (阴影调整,0为无调整)
val filmGrain: Float = 0f, // 0.0 ~ 1.0 (颗粒强度,0为无颗粒)
val vignette: Float = 0f, // -1.0 ~ +1.0 (晕影,负值暗角,正值亮角)
val bleachBypass: Float = 0f, // 0.0 ~ 1.0 (留银冲洗强度,0为无效果)
val lutIntensity: Float = 1f // 0.0 ~ 1.0 (LUT强度,1为完全应用)
) {
/**
@@ -30,6 +33,9 @@ data class ColorRecipeParams(
vibrance == 1f &&
highlights == 0f &&
shadows == 0f &&
filmGrain == 0f &&
vignette == 0f &&
bleachBypass == 0f &&
lutIntensity == 1f
}
@@ -46,6 +52,9 @@ data class ColorRecipeParams(
vibrance == other.vibrance &&
highlights == other.highlights &&
shadows == other.shadows &&
filmGrain == other.filmGrain &&
vignette == other.vignette &&
bleachBypass == other.bleachBypass &&
lutIntensity == other.lutIntensity
}
@@ -91,6 +100,9 @@ enum class RecipeParam(
VIBRANCE("蓝色增强", 0.0f, 2.0f, 1f),
HIGHLIGHTS("高光", -1.0f, 1.0f, 0f),
SHADOWS("阴影", -1.0f, 1.0f, 0f),
FILM_GRAIN("颗粒", 0.0f, 1.0f, 0f),
VIGNETTE("晕影", -1.0f, 1.0f, 0f),
BLEACH_BYPASS("留银冲洗", 0.0f, 1.0f, 0f),
LUT_INTENSITY("滤镜强度", 0.0f, 1.0f, 1f);
/**
@@ -114,6 +126,9 @@ enum class RecipeParam(
VIBRANCE -> params.vibrance
HIGHLIGHTS -> params.highlights
SHADOWS -> params.shadows
FILM_GRAIN -> params.filmGrain
VIGNETTE -> params.vignette
BLEACH_BYPASS -> params.bleachBypass
LUT_INTENSITY -> params.lutIntensity
}
}
@@ -133,6 +148,9 @@ enum class RecipeParam(
VIBRANCE -> params.copy(vibrance = clampedValue)
HIGHLIGHTS -> params.copy(highlights = clampedValue)
SHADOWS -> params.copy(shadows = clampedValue)
FILM_GRAIN -> params.copy(filmGrain = clampedValue)
VIGNETTE -> params.copy(vignette = clampedValue)
BLEACH_BYPASS -> params.copy(bleachBypass = clampedValue)
LUT_INTENSITY -> params.copy(lutIntensity = clampedValue)
}
}
@@ -124,6 +124,9 @@ class CameraGLSurfaceView @JvmOverloads constructor(
vibrance: Float,
highlights: Float,
shadows: Float,
filmGrain: Float,
vignette: Float,
bleachBypass: Float,
lutIntensity: Float,
) {
renderer.exposure = exposure
@@ -135,6 +138,9 @@ class CameraGLSurfaceView @JvmOverloads constructor(
renderer.vibrance = vibrance
renderer.highlights = highlights
renderer.shadows = shadows
renderer.filmGrain = filmGrain
renderer.vignette = vignette
renderer.bleachBypass = bleachBypass
renderer.lutIntensity = lutIntensity
requestRender()
}
@@ -141,6 +141,9 @@ fun CameraPreviewGL(
vibrance = colorRecipeParams.vibrance,
highlights = colorRecipeParams.highlights,
shadows = colorRecipeParams.shadows,
filmGrain = colorRecipeParams.filmGrain,
vignette = colorRecipeParams.vignette,
bleachBypass = colorRecipeParams.bleachBypass,
lutIntensity = colorRecipeParams.lutIntensity,
)
} else {
@@ -35,7 +35,7 @@ fun ColorRecipePanel(
) {
var selectedTabIndex by remember { mutableIntStateOf(0) }
val tabs = listOf("基础", "色彩", "高级")
val tabs = listOf("基础", "色温", "光影", "质感")
val parameterGroups = listOf(
listOf(
RecipeParam.EXPOSURE,
@@ -50,7 +50,12 @@ fun ColorRecipePanel(
listOf(
RecipeParam.HIGHLIGHTS,
RecipeParam.SHADOWS,
RecipeParam.FADE
RecipeParam.VIGNETTE
),
listOf(
RecipeParam.FILM_GRAIN,
RecipeParam.FADE,
RecipeParam.BLEACH_BYPASS
)
)
@@ -193,7 +198,7 @@ private fun formatParamValue(param: RecipeParam, value: Float): String {
RecipeParam.TINT,
RecipeParam.HIGHLIGHTS,
RecipeParam.SHADOWS,
RecipeParam.FADE -> {
RecipeParam.VIGNETTE -> {
if (value >= 0) {
String.format("+%.2f", value)
} else {
@@ -201,6 +206,10 @@ private fun formatParamValue(param: RecipeParam, value: Float): String {
}
}
RecipeParam.FADE,
RecipeParam.FILM_GRAIN,
RecipeParam.BLEACH_BYPASS -> String.format("%.2f", value)
RecipeParam.LUT_INTENSITY -> String.format("%.2f", value)
}
}
@@ -219,6 +228,9 @@ private fun getParamColor(param: RecipeParam): Color {
RecipeParam.VIBRANCE -> Color(0xFF2196F3) // 蓝色
RecipeParam.HIGHLIGHTS -> Color(0xFFF44336) // 红色
RecipeParam.SHADOWS -> Color(0xFF3F51B5) // 深蓝色
RecipeParam.FILM_GRAIN -> Color(0xFF9E9E9E) // 灰色
RecipeParam.VIGNETTE -> Color(0xFF795548) // 棕色
RecipeParam.BLEACH_BYPASS -> Color(0xFF00BCD4) // 青色
RecipeParam.LUT_INTENSITY -> Color(0xFF9E9E9E) // 灰色
}
}