diff --git a/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt b/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt index aade049c..a53ca18d 100644 --- a/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt +++ b/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt @@ -90,14 +90,19 @@ class CustomImportManager(private val context: Context) { val plutFileName = "$lutId.plut" val plutFile = File(customLutDir, plutFileName) - // 读取 .cube / .png / .xmp 文件并转换为 .plut + // 读取 .cube / .png / .xmp / .plut 文件并转换(或复制)为内部 .plut (v3) context.contentResolver.openInputStream(uri)?.use { inputStream -> FileOutputStream(plutFile).use { outputStream -> - val success = if (fileName.endsWith(".xmp", ignoreCase = true)) { - XmpLutParser.parse(inputStream, outputStream, colorSpace = colorSpace, curve = curve) - } else if (fileName.endsWith(".png", ignoreCase = true)) { - LutConverter.convertPngToplut(inputStream, outputStream, colorSpace = colorSpace, curve = curve) - } else LutConverter.convertCubeToplut(inputStream, outputStream, colorSpace = colorSpace, curve = curve) + val success = when { + fileName.endsWith(".xmp", ignoreCase = true) -> + XmpLutParser.parse(inputStream, outputStream, colorSpace = colorSpace, curve = curve) + fileName.endsWith(".png", ignoreCase = true) -> + LutConverter.convertPngToplut(inputStream, outputStream, colorSpace = colorSpace, curve = curve) + fileName.endsWith(".plut", ignoreCase = true) -> + LutConverter.importPlutStrippingRecipe(inputStream, outputStream) + else -> + LutConverter.convertCubeToplut(inputStream, outputStream, colorSpace = colorSpace, curve = curve) + } if (!success) { plutFile.delete() diff --git a/app/src/main/java/com/hinnka/mycamera/gallery/PhotoMetadata.kt b/app/src/main/java/com/hinnka/mycamera/gallery/PhotoMetadata.kt index 28ec7b9a..4bc499bb 100644 --- a/app/src/main/java/com/hinnka/mycamera/gallery/PhotoMetadata.kt +++ b/app/src/main/java/com/hinnka/mycamera/gallery/PhotoMetadata.kt @@ -139,58 +139,7 @@ data class PhotoMetadata( put("lutId", lutId ?: JSONObject.NULL) // 色彩配方配置 if (colorRecipeParams != null) { - put("colorRecipeParams", JSONObject().apply { - put("exposure", colorRecipeParams.exposure.toDouble()) - put("contrast", colorRecipeParams.contrast.toDouble()) - put("saturation", colorRecipeParams.saturation.toDouble()) - put("temperature", colorRecipeParams.temperature.toDouble()) - put("tint", colorRecipeParams.tint.toDouble()) - put("fade", colorRecipeParams.fade.toDouble()) - put("vibrance", colorRecipeParams.color.toDouble()) - put("highlights", colorRecipeParams.highlights.toDouble()) - put("shadows", colorRecipeParams.shadows.toDouble()) - put("toneToe", colorRecipeParams.toneToe.toDouble()) - put("toneShoulder", colorRecipeParams.toneShoulder.toDouble()) - put("tonePivot", colorRecipeParams.tonePivot.toDouble()) - put("paletteX", colorRecipeParams.paletteX.toDouble()) - put("paletteY", colorRecipeParams.paletteY.toDouble()) - put("paletteDensity", colorRecipeParams.paletteDensity.toDouble()) - put("filmGrain", colorRecipeParams.filmGrain.toDouble()) - put("vignette", colorRecipeParams.vignette.toDouble()) - put("bleachBypass", colorRecipeParams.bleachBypass.toDouble()) - put("halation", colorRecipeParams.halation.toDouble()) - put("chromaticAberration", colorRecipeParams.chromaticAberration.toDouble()) - put("noise", colorRecipeParams.noise.toDouble()) - put("lowRes", colorRecipeParams.lowRes.toDouble()) - put("skinHue", colorRecipeParams.skinHue.toDouble()) - put("skinChroma", colorRecipeParams.skinChroma.toDouble()) - put("skinLightness", colorRecipeParams.skinLightness.toDouble()) - put("redHue", colorRecipeParams.redHue.toDouble()) - put("redChroma", colorRecipeParams.redChroma.toDouble()) - put("redLightness", colorRecipeParams.redLightness.toDouble()) - put("orangeHue", colorRecipeParams.orangeHue.toDouble()) - put("orangeChroma", colorRecipeParams.orangeChroma.toDouble()) - put("orangeLightness", colorRecipeParams.orangeLightness.toDouble()) - put("yellowHue", colorRecipeParams.yellowHue.toDouble()) - put("yellowChroma", colorRecipeParams.yellowChroma.toDouble()) - put("yellowLightness", colorRecipeParams.yellowLightness.toDouble()) - put("greenHue", colorRecipeParams.greenHue.toDouble()) - put("greenChroma", colorRecipeParams.greenChroma.toDouble()) - put("greenLightness", colorRecipeParams.greenLightness.toDouble()) - put("cyanHue", colorRecipeParams.cyanHue.toDouble()) - put("cyanChroma", colorRecipeParams.cyanChroma.toDouble()) - put("cyanLightness", colorRecipeParams.cyanLightness.toDouble()) - put("blueHue", colorRecipeParams.blueHue.toDouble()) - put("blueChroma", colorRecipeParams.blueChroma.toDouble()) - put("blueLightness", colorRecipeParams.blueLightness.toDouble()) - put("purpleHue", colorRecipeParams.purpleHue.toDouble()) - put("purpleChroma", colorRecipeParams.purpleChroma.toDouble()) - put("purpleLightness", colorRecipeParams.purpleLightness.toDouble()) - put("magentaHue", colorRecipeParams.magentaHue.toDouble()) - put("magentaChroma", colorRecipeParams.magentaChroma.toDouble()) - put("magentaLightness", colorRecipeParams.magentaLightness.toDouble()) - put("lutIntensity", colorRecipeParams.lutIntensity.toDouble()) - }) + put("colorRecipeParams", JSONObject(colorRecipeParams.toJson())) } else { put("colorRecipeParams", JSONObject.NULL) } @@ -319,58 +268,11 @@ data class PhotoMetadata( // 解析色彩配方参数 val colorRecipeParamsObj = obj.optJSONObject("colorRecipeParams") val colorRecipeParams = if (colorRecipeParamsObj != null && !obj.isNull("colorRecipeParams")) { - ColorRecipeParams( - exposure = colorRecipeParamsObj.optDouble("exposure", 0.0).toFloat(), - contrast = colorRecipeParamsObj.optDouble("contrast", 1.0).toFloat(), - saturation = colorRecipeParamsObj.optDouble("saturation", 1.0).toFloat(), - temperature = colorRecipeParamsObj.optDouble("temperature", 0.0).toFloat(), - tint = colorRecipeParamsObj.optDouble("tint", 0.0).toFloat(), - fade = colorRecipeParamsObj.optDouble("fade", 0.0).toFloat(), - color = colorRecipeParamsObj.optDouble("vibrance", 0.0).toFloat(), - highlights = colorRecipeParamsObj.optDouble("highlights", 0.0).toFloat(), - shadows = colorRecipeParamsObj.optDouble("shadows", 0.0).toFloat(), - toneToe = colorRecipeParamsObj.optDouble("toneToe", 0.0).toFloat(), - toneShoulder = colorRecipeParamsObj.optDouble("toneShoulder", 0.0).toFloat(), - tonePivot = colorRecipeParamsObj.optDouble("tonePivot", 0.0).toFloat(), - paletteX = colorRecipeParamsObj.optDouble("paletteX", 0.5).toFloat(), - paletteY = colorRecipeParamsObj.optDouble("paletteY", 0.5).toFloat(), - paletteDensity = colorRecipeParamsObj.optDouble("paletteDensity", 1.0).toFloat(), - filmGrain = colorRecipeParamsObj.optDouble("filmGrain", 0.0).toFloat(), - vignette = colorRecipeParamsObj.optDouble("vignette", 0.0).toFloat(), - bleachBypass = colorRecipeParamsObj.optDouble("bleachBypass", 0.0).toFloat(), - halation = colorRecipeParamsObj.optDouble("halation", 0.0).toFloat(), - chromaticAberration = colorRecipeParamsObj.optDouble("chromaticAberration", 0.0).toFloat(), - noise = colorRecipeParamsObj.optDouble("noise", 0.0).toFloat(), - lowRes = colorRecipeParamsObj.optDouble("lowRes", 0.0).toFloat(), - skinHue = colorRecipeParamsObj.optDouble("skinHue", 0.0).toFloat(), - skinChroma = colorRecipeParamsObj.optDouble("skinChroma", 0.0).toFloat(), - skinLightness = colorRecipeParamsObj.optDouble("skinLightness", 0.0).toFloat(), - redHue = colorRecipeParamsObj.optDouble("redHue", 0.0).toFloat(), - redChroma = colorRecipeParamsObj.optDouble("redChroma", 0.0).toFloat(), - redLightness = colorRecipeParamsObj.optDouble("redLightness", 0.0).toFloat(), - orangeHue = colorRecipeParamsObj.optDouble("orangeHue", 0.0).toFloat(), - orangeChroma = colorRecipeParamsObj.optDouble("orangeChroma", 0.0).toFloat(), - orangeLightness = colorRecipeParamsObj.optDouble("orangeLightness", 0.0).toFloat(), - yellowHue = colorRecipeParamsObj.optDouble("yellowHue", 0.0).toFloat(), - yellowChroma = colorRecipeParamsObj.optDouble("yellowChroma", 0.0).toFloat(), - yellowLightness = colorRecipeParamsObj.optDouble("yellowLightness", 0.0).toFloat(), - greenHue = colorRecipeParamsObj.optDouble("greenHue", 0.0).toFloat(), - greenChroma = colorRecipeParamsObj.optDouble("greenChroma", 0.0).toFloat(), - greenLightness = colorRecipeParamsObj.optDouble("greenLightness", 0.0).toFloat(), - cyanHue = colorRecipeParamsObj.optDouble("cyanHue", 0.0).toFloat(), - cyanChroma = colorRecipeParamsObj.optDouble("cyanChroma", 0.0).toFloat(), - cyanLightness = colorRecipeParamsObj.optDouble("cyanLightness", 0.0).toFloat(), - blueHue = colorRecipeParamsObj.optDouble("blueHue", 0.0).toFloat(), - blueChroma = colorRecipeParamsObj.optDouble("blueChroma", 0.0).toFloat(), - blueLightness = colorRecipeParamsObj.optDouble("blueLightness", 0.0).toFloat(), - purpleHue = colorRecipeParamsObj.optDouble("purpleHue", 0.0).toFloat(), - purpleChroma = colorRecipeParamsObj.optDouble("purpleChroma", 0.0).toFloat(), - purpleLightness = colorRecipeParamsObj.optDouble("purpleLightness", 0.0).toFloat(), - magentaHue = colorRecipeParamsObj.optDouble("magentaHue", 0.0).toFloat(), - magentaChroma = colorRecipeParamsObj.optDouble("magentaChroma", 0.0).toFloat(), - magentaLightness = colorRecipeParamsObj.optDouble("magentaLightness", 0.0).toFloat(), - lutIntensity = colorRecipeParamsObj.optDouble("lutIntensity", 1.0).toFloat() - ) + // 兼容旧版本将 color 字段序列化为 "vibrance" 的历史数据 + if (colorRecipeParamsObj.has("vibrance") && !colorRecipeParamsObj.has("color")) { + colorRecipeParamsObj.put("color", colorRecipeParamsObj.optDouble("vibrance", 0.0)) + } + ColorRecipeParams.fromJson(colorRecipeParamsObj.toString()) } else { null } diff --git a/app/src/main/java/com/hinnka/mycamera/lut/LutConverter.kt b/app/src/main/java/com/hinnka/mycamera/lut/LutConverter.kt index 49a722fe..c768343c 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutConverter.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutConverter.kt @@ -10,6 +10,7 @@ import android.graphics.BitmapFactory import android.graphics.Color import com.hinnka.mycamera.raw.ColorSpace import com.hinnka.mycamera.utils.PLog +// LutConfig is in the same package (com.hinnka.mycamera.lut), no import needed import kotlin.math.roundToInt /** @@ -21,7 +22,9 @@ import kotlin.math.roundToInt object LutConverter { private const val MAGIC_PLUT = "PLUT" + private const val MAGIC_PLUT_INT = 0x54554C50 // 'PLUT' in Little Endian private const val VERSION = 3 + private const val VERSION_WITH_RECIPE = 4 private const val DATA_TYPE_UINT16 = 1 /** @@ -480,6 +483,120 @@ object LutConverter { outputStream.flush() } + /** + * 将 LutConfig 导出为 .plut 格式(可选嵌入色彩配方) + * + * - recipeJson == null:写 v3 格式(不含配方) + * - recipeJson != null:写 v4 格式,像素数据后追加 recipeLength(uint32) + recipeJSON(UTF-8) + */ + fun exportToPlut(lutConfig: LutConfig, outputStream: OutputStream, recipeJson: String? = null) { + val pixelBuf = lutConfig.toByteBuffer() + val pixelBytes = ByteArray(pixelBuf.capacity()) + pixelBuf.position(0) + pixelBuf.get(pixelBytes) + + val recipeBytes = recipeJson?.toByteArray(Charsets.UTF_8) + val version = if (recipeBytes != null) VERSION_WITH_RECIPE else VERSION + val recipeSection = if (recipeBytes != null) 4 + recipeBytes.size else 0 + + val outBuffer = ByteBuffer.allocate(24 + pixelBytes.size + recipeSection) + .order(ByteOrder.LITTLE_ENDIAN) + outBuffer.put(MAGIC_PLUT.toByteArray(Charsets.US_ASCII)) + outBuffer.putInt(version) + outBuffer.putInt(lutConfig.size) + outBuffer.putInt(lutConfig.configDataType) + outBuffer.putInt(lutConfig.curve.ordinal) + outBuffer.putInt(lutConfig.colorSpace.ordinal) + outBuffer.put(pixelBytes) + if (recipeBytes != null) { + outBuffer.putInt(recipeBytes.size) + outBuffer.put(recipeBytes) + } + + outputStream.write(outBuffer.array()) + outputStream.flush() + } + + /** + * 将输入的 .plut 文件(任意版本)复制到输出,统一写成 v3 格式(去除 v4 中嵌入的配方段) + * + * @return 转换成功返回 true + */ + fun importPlutStrippingRecipe(inputStream: InputStream, outputStream: OutputStream): Boolean { + return try { + val fullData = inputStream.readBytes() + val inBuffer = ByteBuffer.wrap(fullData).order(ByteOrder.LITTLE_ENDIAN) + + val magic = inBuffer.int + if (magic != MAGIC_PLUT_INT) return false + + val version = inBuffer.int + val size = inBuffer.int + val dataType = inBuffer.int + val curveOrdinal = if (version >= 2) inBuffer.int else LutCurve.SRGB.ordinal + val colorSpaceOrdinal = if (version >= 3) inBuffer.int else ColorSpace.SRGB.ordinal + + val bytesPerComponent = if (dataType == DATA_TYPE_UINT16) 2 else 1 + val dataByteCount = size * size * size * 3 * bytesPerComponent + val dataBytes = ByteArray(dataByteCount) + inBuffer.get(dataBytes) + + // 始终写为 v3(去掉 v4 的配方尾部) + val outBuffer = ByteBuffer.allocate(24 + dataByteCount).order(ByteOrder.LITTLE_ENDIAN) + outBuffer.put(MAGIC_PLUT.toByteArray(Charsets.US_ASCII)) + outBuffer.putInt(VERSION) + outBuffer.putInt(size) + outBuffer.putInt(dataType) + outBuffer.putInt(curveOrdinal) + outBuffer.putInt(colorSpaceOrdinal) + outBuffer.put(dataBytes) + + outputStream.write(outBuffer.array()) + outputStream.flush() + true + } catch (e: Exception) { + e.printStackTrace() + false + } + } + + /** + * 从 .plut 输入流中提取嵌入的色彩配方 JSON(仅 v4 格式含有) + * + * @return 配方 JSON 字符串,若不含配方则返回 null + */ + fun extractRecipeJsonFromPlut(inputStream: InputStream): String? { + return try { + val fullData = inputStream.readBytes() + val buffer = ByteBuffer.wrap(fullData).order(ByteOrder.LITTLE_ENDIAN) + + val magic = buffer.int + if (magic != MAGIC_PLUT_INT) return null + + val version = buffer.int + if (version < VERSION_WITH_RECIPE) return null + + val size = buffer.int + val dataType = buffer.int + buffer.int // curve + buffer.int // colorSpace + + val bytesPerComponent = if (dataType == DATA_TYPE_UINT16) 2 else 1 + val dataByteCount = size * size * size * 3 * bytesPerComponent + if (buffer.remaining() < dataByteCount + 4) return null + buffer.position(buffer.position() + dataByteCount) + + val recipeLength = buffer.int + if (recipeLength <= 0 || buffer.remaining() < recipeLength) return null + + val recipeBytes = ByteArray(recipeLength) + buffer.get(recipeBytes) + String(recipeBytes, Charsets.UTF_8) + } catch (e: Exception) { + null + } + } + /** * .cube 数据 */ diff --git a/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt b/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt index d2f7e720..26e8d771 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt @@ -228,28 +228,7 @@ class LutImageProcessor { // 提取色彩配方参数 val effectiveRecipeParams = colorRecipeParams?.let(ColorPaletteMapper::mergeIntoEffectiveParams) - val colorRecipeEnabled = effectiveRecipeParams != null && !effectiveRecipeParams.isDefault() - val exposure = effectiveRecipeParams?.exposure ?: 0f - val contrast = effectiveRecipeParams?.contrast ?: 1f - val saturation = effectiveRecipeParams?.saturation ?: 1f - val temperature = effectiveRecipeParams?.temperature ?: 0f - val tint = effectiveRecipeParams?.tint ?: 0f - val fade = effectiveRecipeParams?.fade ?: 0f - val vibrance = effectiveRecipeParams?.color ?: 0f - val highlights = effectiveRecipeParams?.highlights ?: 0f - val shadows = effectiveRecipeParams?.shadows ?: 0f - val toneToe = effectiveRecipeParams?.toneToe ?: 0f - val toneShoulder = effectiveRecipeParams?.toneShoulder ?: 0f - val tonePivot = effectiveRecipeParams?.tonePivot ?: 0f - val filmGrain = effectiveRecipeParams?.filmGrain ?: 0f - val vignette = effectiveRecipeParams?.vignette ?: 0f - val bleachBypass = effectiveRecipeParams?.bleachBypass ?: 0f val halation = effectiveRecipeParams?.halation ?: 0f - val chromaticAberration = effectiveRecipeParams?.chromaticAberration ?: 0f - val noise = effectiveRecipeParams?.noise ?: 0f - val lowRes = effectiveRecipeParams?.lowRes ?: 0f - val intensity = effectiveRecipeParams?.lutIntensity ?: 1f - val (lchHueAdjustments, lchChromaAdjustments, lchLightnessAdjustments) = buildLchAdjustmentArrays(effectiveRecipeParams) // 后期处理参数 val sharpening: Float = sharpeningValue @@ -280,8 +259,7 @@ class LutImageProcessor { val inputTexId = if (noiseReduction > 0 || chromaNoiseReduction > 0) nlmPassTexId[1] else imageTextureId // HDF 光晕效果预处理(在主 shader 之前,需要模糊的光晕纹理) - val hdfEnabled = halation > 0f - if (hdfEnabled) { + if (halation > 0f) { renderHDFBlur(inputTexId, width, height, halation) currentCoroutineContext().ensureActive() } @@ -292,14 +270,8 @@ class LutImageProcessor { inputTexId, colorSpace, lutConfig, - colorRecipeEnabled, - exposure, contrast, saturation, temperature, tint, fade, - vibrance, highlights, shadows, toneToe, toneShoulder, tonePivot, - filmGrain, vignette, bleachBypass, - halation, chromaticAberration, noise, lowRes, - intensity, sharpening, - lchHueAdjustments, lchChromaAdjustments, lchLightnessAdjustments - // GL_RGBA16 已自动归一化,使用标准 shader + effectiveRecipeParams, + sharpening, ) outputBitmap @@ -332,28 +304,7 @@ class LutImageProcessor { // 提取色彩配方参数 val effectiveRecipeParams = colorRecipeParams?.let(ColorPaletteMapper::mergeIntoEffectiveParams) - val colorRecipeEnabled = effectiveRecipeParams != null && !effectiveRecipeParams.isDefault() - val exposure = effectiveRecipeParams?.exposure ?: 0f - val contrast = effectiveRecipeParams?.contrast ?: 1f - val saturation = effectiveRecipeParams?.saturation ?: 1f - val temperature = effectiveRecipeParams?.temperature ?: 0f - val tint = effectiveRecipeParams?.tint ?: 0f - val fade = effectiveRecipeParams?.fade ?: 0f - val vibrance = effectiveRecipeParams?.color ?: 0f - val highlights = effectiveRecipeParams?.highlights ?: 0f - val shadows = effectiveRecipeParams?.shadows ?: 0f - val toneToe = effectiveRecipeParams?.toneToe ?: 0f - val toneShoulder = effectiveRecipeParams?.toneShoulder ?: 0f - val tonePivot = effectiveRecipeParams?.tonePivot ?: 0f - val filmGrain = effectiveRecipeParams?.filmGrain ?: 0f - val vignette = effectiveRecipeParams?.vignette ?: 0f - val bleachBypass = effectiveRecipeParams?.bleachBypass ?: 0f val halation = effectiveRecipeParams?.halation ?: 0f - val chromaticAberration = effectiveRecipeParams?.chromaticAberration ?: 0f - val intensity = effectiveRecipeParams?.lutIntensity ?: 1f - val noise = effectiveRecipeParams?.noise ?: 0f - val lowRes = effectiveRecipeParams?.lowRes ?: 0f - val (lchHueAdjustments, lchChromaAdjustments, lchLightnessAdjustments) = buildLchAdjustmentArrays(effectiveRecipeParams) // 后期处理参数(仅在软件处理模式下生效) val sharpening: Float = sharpeningValue @@ -387,8 +338,7 @@ class LutImageProcessor { val inputTexId = if (noiseReduction > 0 || chromaNoiseReduction > 0) nlmPassTexId[1] else imageTextureId // HDF 光晕效果预处理 - val hdfEnabled = halation > 0f - if (hdfEnabled) { + if (halation > 0f) { renderHDFBlur(inputTexId, width, height, halation) currentCoroutineContext().ensureActive() } @@ -399,13 +349,8 @@ class LutImageProcessor { inputTexId, bitmap.colorSpace ?: ColorSpace.get(ColorSpace.Named.SRGB), lutConfig, - colorRecipeEnabled, - exposure, contrast, saturation, temperature, tint, fade, - vibrance, highlights, shadows, toneToe, toneShoulder, tonePivot, - filmGrain, vignette, bleachBypass, - halation, chromaticAberration, - noise, lowRes, intensity, sharpening, - lchHueAdjustments, lchChromaAdjustments, lchLightnessAdjustments + effectiveRecipeParams, + sharpening, ) outputBitmap @@ -465,32 +410,31 @@ class LutImageProcessor { inputTextureId: Int, inputColorSpace: ColorSpace, lutConfig: LutConfig?, - colorRecipeEnabled: Boolean, - exposure: Float, - contrast: Float, - saturation: Float, - temperature: Float, - tint: Float, - fade: Float, - vibrance: Float, - highlights: Float, - shadows: Float, - toneToe: Float, - toneShoulder: Float, - tonePivot: Float, - filmGrain: Float, - vignette: Float, - bleachBypass: Float, - halation: Float, - chromaticAberration: Float, - noise: Float, - lowRes: Float, - intensity: Float, + effectiveRecipeParams: ColorRecipeParams?, sharpening: Float, - lchHueAdjustments: FloatArray, - lchChromaAdjustments: FloatArray, - lchLightnessAdjustments: FloatArray ): Bitmap { + val colorRecipeEnabled = effectiveRecipeParams != null && !effectiveRecipeParams.isDefault() + val exposure = effectiveRecipeParams?.exposure ?: 0f + val contrast = effectiveRecipeParams?.contrast ?: 1f + val saturation = effectiveRecipeParams?.saturation ?: 1f + val temperature = effectiveRecipeParams?.temperature ?: 0f + val tint = effectiveRecipeParams?.tint ?: 0f + val fade = effectiveRecipeParams?.fade ?: 0f + val vibrance = effectiveRecipeParams?.color ?: 0f + val highlights = effectiveRecipeParams?.highlights ?: 0f + val shadows = effectiveRecipeParams?.shadows ?: 0f + val toneToe = effectiveRecipeParams?.toneToe ?: 0f + val toneShoulder = effectiveRecipeParams?.toneShoulder ?: 0f + val tonePivot = effectiveRecipeParams?.tonePivot ?: 0f + val filmGrain = effectiveRecipeParams?.filmGrain ?: 0f + val vignette = effectiveRecipeParams?.vignette ?: 0f + val bleachBypass = effectiveRecipeParams?.bleachBypass ?: 0f + val halation = effectiveRecipeParams?.halation ?: 0f + val chromaticAberration = effectiveRecipeParams?.chromaticAberration ?: 0f + val noise = effectiveRecipeParams?.noise ?: 0f + val lowRes = effectiveRecipeParams?.lowRes ?: 0f + val intensity = effectiveRecipeParams?.lutIntensity ?: 1f + val (lchHueAdjustments, lchChromaAdjustments, lchLightnessAdjustments) = buildLchAdjustmentArrays(effectiveRecipeParams) val program = shaderProgram GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, framebufferId) GLES30.glViewport(0, 0, width, height) diff --git a/app/src/main/java/com/hinnka/mycamera/lut/LutManager.kt b/app/src/main/java/com/hinnka/mycamera/lut/LutManager.kt index 6bc51446..a63e5b65 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutManager.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutManager.kt @@ -6,6 +6,7 @@ import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.floatPreferencesKey +import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.preferencesDataStore import com.hinnka.mycamera.data.CustomImportManager import com.hinnka.mycamera.model.ColorRecipeParams @@ -35,59 +36,92 @@ class LutManager(private val context: Context) { // 内置 LUT 目录 private const val BUILT_IN_LUT_FOLDER = "luts" - // 色彩配方 DataStore Key 生成函数(每个 LUT ID 独立) - private fun exposureKey(lutId: String) = floatPreferencesKey("${lutId}_exposure") - private fun contrastKey(lutId: String) = floatPreferencesKey("${lutId}_contrast") - private fun saturationKey(lutId: String) = floatPreferencesKey("${lutId}_saturation") - private fun temperatureKey(lutId: String) = floatPreferencesKey("${lutId}_temperature") - private fun tintKey(lutId: String) = floatPreferencesKey("${lutId}_tint") - private fun fadeKey(lutId: String) = floatPreferencesKey("${lutId}_fade") - private fun colorKey(lutId: String) = floatPreferencesKey("${lutId}_color") - private fun highlightsKey(lutId: String) = floatPreferencesKey("${lutId}_highlights") - private fun shadowsKey(lutId: String) = floatPreferencesKey("${lutId}_shadows") - private fun toneToeKey(lutId: String) = floatPreferencesKey("${lutId}_toneToe") - private fun toneShoulderKey(lutId: String) = floatPreferencesKey("${lutId}_toneShoulder") - private fun tonePivotKey(lutId: String) = floatPreferencesKey("${lutId}_tonePivot") - private fun paletteXKey(lutId: String) = floatPreferencesKey("${lutId}_paletteX") - private fun paletteYKey(lutId: String) = floatPreferencesKey("${lutId}_paletteY") - private fun paletteDensityKey(lutId: String) = floatPreferencesKey("${lutId}_paletteDensity") - 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 halationKey(lutId: String) = floatPreferencesKey("${lutId}_halation") - private fun chromaticAberrationKey(lutId: String) = floatPreferencesKey("${lutId}_chromaticAberration") - private fun noiseKey(lutId: String) = floatPreferencesKey("${lutId}_noise") - private fun lowResKey(lutId: String) = floatPreferencesKey("${lutId}_lowRes") - private fun skinHueKey(lutId: String) = floatPreferencesKey("${lutId}_skinHue") - private fun skinChromaKey(lutId: String) = floatPreferencesKey("${lutId}_skinChroma") - private fun skinLightnessKey(lutId: String) = floatPreferencesKey("${lutId}_skinLightness") - private fun redHueKey(lutId: String) = floatPreferencesKey("${lutId}_redHue") - private fun redChromaKey(lutId: String) = floatPreferencesKey("${lutId}_redChroma") - private fun redLightnessKey(lutId: String) = floatPreferencesKey("${lutId}_redLightness") - private fun orangeHueKey(lutId: String) = floatPreferencesKey("${lutId}_orangeHue") - private fun orangeChromaKey(lutId: String) = floatPreferencesKey("${lutId}_orangeChroma") - private fun orangeLightnessKey(lutId: String) = floatPreferencesKey("${lutId}_orangeLightness") - private fun yellowHueKey(lutId: String) = floatPreferencesKey("${lutId}_yellowHue") - private fun yellowChromaKey(lutId: String) = floatPreferencesKey("${lutId}_yellowChroma") - private fun yellowLightnessKey(lutId: String) = floatPreferencesKey("${lutId}_yellowLightness") - private fun greenHueKey(lutId: String) = floatPreferencesKey("${lutId}_greenHue") - private fun greenChromaKey(lutId: String) = floatPreferencesKey("${lutId}_greenChroma") - private fun greenLightnessKey(lutId: String) = floatPreferencesKey("${lutId}_greenLightness") - private fun cyanHueKey(lutId: String) = floatPreferencesKey("${lutId}_cyanHue") - private fun cyanChromaKey(lutId: String) = floatPreferencesKey("${lutId}_cyanChroma") - private fun cyanLightnessKey(lutId: String) = floatPreferencesKey("${lutId}_cyanLightness") - private fun blueHueKey(lutId: String) = floatPreferencesKey("${lutId}_blueHue") - private fun blueChromaKey(lutId: String) = floatPreferencesKey("${lutId}_blueChroma") - private fun blueLightnessKey(lutId: String) = floatPreferencesKey("${lutId}_blueLightness") - private fun purpleHueKey(lutId: String) = floatPreferencesKey("${lutId}_purpleHue") - private fun purpleChromaKey(lutId: String) = floatPreferencesKey("${lutId}_purpleChroma") - private fun purpleLightnessKey(lutId: String) = floatPreferencesKey("${lutId}_purpleLightness") - private fun magentaHueKey(lutId: String) = floatPreferencesKey("${lutId}_magentaHue") - private fun magentaChromaKey(lutId: String) = floatPreferencesKey("${lutId}_magentaChroma") - private fun magentaLightnessKey(lutId: String) = floatPreferencesKey("${lutId}_magentaLightness") - private fun lutIntensityKey(lutId: String) = floatPreferencesKey("${lutId}_lutIntensity") - private fun remarksKey(lutId: String) = - androidx.datastore.preferences.core.stringPreferencesKey("${lutId}_remarks") + // 色彩配方 DataStore Key(每个 LUT ID 存一条 JSON) + private fun recipeKey(lutId: String) = stringPreferencesKey("${lutId}_recipe") + + // 旧版逐字段 Key(仅用于迁移读取,新数据不再写入) + private val legacyFieldNames = listOf( + "exposure", "contrast", "saturation", "temperature", "tint", "fade", "color", + "highlights", "shadows", "toneToe", "toneShoulder", "tonePivot", + "paletteX", "paletteY", "paletteDensity", + "filmGrain", "vignette", "bleachBypass", "halation", "chromaticAberration", + "noise", "lowRes", + "skinHue", "skinChroma", "skinLightness", + "redHue", "redChroma", "redLightness", + "orangeHue", "orangeChroma", "orangeLightness", + "yellowHue", "yellowChroma", "yellowLightness", + "greenHue", "greenChroma", "greenLightness", + "cyanHue", "cyanChroma", "cyanLightness", + "blueHue", "blueChroma", "blueLightness", + "purpleHue", "purpleChroma", "purpleLightness", + "magentaHue", "magentaChroma", "magentaLightness", + "lutIntensity" + ) + + private fun readLegacyParams(preferences: Preferences, lutId: String): ColorRecipeParams { + fun f(name: String, default: Float = 0f) = + preferences[floatPreferencesKey("${lutId}_$name")] ?: default + fun s(name: String) = + preferences[stringPreferencesKey("${lutId}_$name")] ?: "" + return ColorRecipeParams( + exposure = f("exposure"), + contrast = f("contrast", 1f), + saturation = f("saturation", 1f), + temperature = f("temperature"), + tint = f("tint"), + fade = f("fade"), + color = f("color"), + highlights = f("highlights"), + shadows = f("shadows"), + toneToe = f("toneToe"), + toneShoulder = f("toneShoulder"), + tonePivot = f("tonePivot"), + paletteX = f("paletteX", 0.5f), + paletteY = f("paletteY", 0.5f), + paletteDensity = f("paletteDensity", 1f), + filmGrain = f("filmGrain"), + vignette = f("vignette"), + bleachBypass = f("bleachBypass"), + halation = f("halation"), + chromaticAberration = f("chromaticAberration"), + noise = f("noise"), + lowRes = f("lowRes"), + skinHue = f("skinHue"), + skinChroma = f("skinChroma"), + skinLightness = f("skinLightness"), + redHue = f("redHue"), + redChroma = f("redChroma"), + redLightness = f("redLightness"), + orangeHue = f("orangeHue"), + orangeChroma = f("orangeChroma"), + orangeLightness = f("orangeLightness"), + yellowHue = f("yellowHue"), + yellowChroma = f("yellowChroma"), + yellowLightness = f("yellowLightness"), + greenHue = f("greenHue"), + greenChroma = f("greenChroma"), + greenLightness = f("greenLightness"), + cyanHue = f("cyanHue"), + cyanChroma = f("cyanChroma"), + cyanLightness = f("cyanLightness"), + blueHue = f("blueHue"), + blueChroma = f("blueChroma"), + blueLightness = f("blueLightness"), + purpleHue = f("purpleHue"), + purpleChroma = f("purpleChroma"), + purpleLightness = f("purpleLightness"), + magentaHue = f("magentaHue"), + magentaChroma = f("magentaChroma"), + magentaLightness = f("magentaLightness"), + lutIntensity = f("lutIntensity", 1f), + remarks = s("remarks"), + ) + } + + private fun androidx.datastore.preferences.core.MutablePreferences.removeLegacyKeys(lutId: String) { + legacyFieldNames.forEach { name -> remove(floatPreferencesKey("${lutId}_$name")) } + remove(stringPreferencesKey("${lutId}_remarks")) + } } // LUT 缓存 @@ -104,59 +138,9 @@ class LutManager(private val context: Context) { */ fun getColorRecipeParams(lutId: String): Flow { return context.colorRecipeDataStore.data.map { preferences -> - ColorRecipeParams( - exposure = preferences[exposureKey(lutId)] ?: 0f, - contrast = preferences[contrastKey(lutId)] ?: 1f, - saturation = preferences[saturationKey(lutId)] ?: 1f, - temperature = preferences[temperatureKey(lutId)] ?: 0f, - tint = preferences[tintKey(lutId)] ?: 0f, - fade = preferences[fadeKey(lutId)] ?: 0f, - color = preferences[colorKey(lutId)] ?: 0f, - highlights = preferences[highlightsKey(lutId)] ?: 0f, - shadows = preferences[shadowsKey(lutId)] ?: 0f, - toneToe = preferences[toneToeKey(lutId)] ?: 0f, - toneShoulder = preferences[toneShoulderKey(lutId)] ?: 0f, - tonePivot = preferences[tonePivotKey(lutId)] ?: 0f, - paletteX = preferences[paletteXKey(lutId)] ?: 0.5f, - paletteY = preferences[paletteYKey(lutId)] ?: 0.5f, - paletteDensity = preferences[paletteDensityKey(lutId)] ?: 1f, - filmGrain = preferences[filmGrainKey(lutId)] ?: 0f, - vignette = preferences[vignetteKey(lutId)] ?: 0f, - bleachBypass = preferences[bleachBypassKey(lutId)] ?: 0f, - halation = preferences[halationKey(lutId)] ?: 0f, - chromaticAberration = preferences[chromaticAberrationKey(lutId)] ?: 0f, - noise = preferences[noiseKey(lutId)] ?: 0f, - lowRes = preferences[lowResKey(lutId)] ?: 0f, - skinHue = preferences[skinHueKey(lutId)] ?: 0f, - skinChroma = preferences[skinChromaKey(lutId)] ?: 0f, - skinLightness = preferences[skinLightnessKey(lutId)] ?: 0f, - redHue = preferences[redHueKey(lutId)] ?: 0f, - redChroma = preferences[redChromaKey(lutId)] ?: 0f, - redLightness = preferences[redLightnessKey(lutId)] ?: 0f, - orangeHue = preferences[orangeHueKey(lutId)] ?: 0f, - orangeChroma = preferences[orangeChromaKey(lutId)] ?: 0f, - orangeLightness = preferences[orangeLightnessKey(lutId)] ?: 0f, - yellowHue = preferences[yellowHueKey(lutId)] ?: 0f, - yellowChroma = preferences[yellowChromaKey(lutId)] ?: 0f, - yellowLightness = preferences[yellowLightnessKey(lutId)] ?: 0f, - greenHue = preferences[greenHueKey(lutId)] ?: 0f, - greenChroma = preferences[greenChromaKey(lutId)] ?: 0f, - greenLightness = preferences[greenLightnessKey(lutId)] ?: 0f, - cyanHue = preferences[cyanHueKey(lutId)] ?: 0f, - cyanChroma = preferences[cyanChromaKey(lutId)] ?: 0f, - cyanLightness = preferences[cyanLightnessKey(lutId)] ?: 0f, - blueHue = preferences[blueHueKey(lutId)] ?: 0f, - blueChroma = preferences[blueChromaKey(lutId)] ?: 0f, - blueLightness = preferences[blueLightnessKey(lutId)] ?: 0f, - purpleHue = preferences[purpleHueKey(lutId)] ?: 0f, - purpleChroma = preferences[purpleChromaKey(lutId)] ?: 0f, - purpleLightness = preferences[purpleLightnessKey(lutId)] ?: 0f, - magentaHue = preferences[magentaHueKey(lutId)] ?: 0f, - magentaChroma = preferences[magentaChromaKey(lutId)] ?: 0f, - magentaLightness = preferences[magentaLightnessKey(lutId)] ?: 0f, - lutIntensity = preferences[lutIntensityKey(lutId)] ?: 1f, - remarks = preferences[remarksKey(lutId)] ?: "" - ) + val json = preferences[recipeKey(lutId)] + if (json != null) ColorRecipeParams.fromJson(json) + else readLegacyParams(preferences, lutId) } } @@ -280,122 +264,23 @@ class LutManager(private val context: Context) { */ suspend fun saveColorRecipeParams(lutId: String, params: ColorRecipeParams) { context.colorRecipeDataStore.edit { preferences -> - preferences[exposureKey(lutId)] = params.exposure - preferences[contrastKey(lutId)] = params.contrast - preferences[saturationKey(lutId)] = params.saturation - preferences[temperatureKey(lutId)] = params.temperature - preferences[tintKey(lutId)] = params.tint - preferences[fadeKey(lutId)] = params.fade - preferences[colorKey(lutId)] = params.color - preferences[highlightsKey(lutId)] = params.highlights - preferences[shadowsKey(lutId)] = params.shadows - preferences[toneToeKey(lutId)] = params.toneToe - preferences[toneShoulderKey(lutId)] = params.toneShoulder - preferences[tonePivotKey(lutId)] = params.tonePivot - preferences[paletteXKey(lutId)] = params.paletteX - preferences[paletteYKey(lutId)] = params.paletteY - preferences[paletteDensityKey(lutId)] = params.paletteDensity - preferences[filmGrainKey(lutId)] = params.filmGrain - preferences[vignetteKey(lutId)] = params.vignette - preferences[bleachBypassKey(lutId)] = params.bleachBypass - preferences[halationKey(lutId)] = params.halation - preferences[chromaticAberrationKey(lutId)] = params.chromaticAberration - preferences[noiseKey(lutId)] = params.noise - preferences[lowResKey(lutId)] = params.lowRes - preferences[skinHueKey(lutId)] = params.skinHue - preferences[skinChromaKey(lutId)] = params.skinChroma - preferences[skinLightnessKey(lutId)] = params.skinLightness - preferences[redHueKey(lutId)] = params.redHue - preferences[redChromaKey(lutId)] = params.redChroma - preferences[redLightnessKey(lutId)] = params.redLightness - preferences[orangeHueKey(lutId)] = params.orangeHue - preferences[orangeChromaKey(lutId)] = params.orangeChroma - preferences[orangeLightnessKey(lutId)] = params.orangeLightness - preferences[yellowHueKey(lutId)] = params.yellowHue - preferences[yellowChromaKey(lutId)] = params.yellowChroma - preferences[yellowLightnessKey(lutId)] = params.yellowLightness - preferences[greenHueKey(lutId)] = params.greenHue - preferences[greenChromaKey(lutId)] = params.greenChroma - preferences[greenLightnessKey(lutId)] = params.greenLightness - preferences[cyanHueKey(lutId)] = params.cyanHue - preferences[cyanChromaKey(lutId)] = params.cyanChroma - preferences[cyanLightnessKey(lutId)] = params.cyanLightness - preferences[blueHueKey(lutId)] = params.blueHue - preferences[blueChromaKey(lutId)] = params.blueChroma - preferences[blueLightnessKey(lutId)] = params.blueLightness - preferences[purpleHueKey(lutId)] = params.purpleHue - preferences[purpleChromaKey(lutId)] = params.purpleChroma - preferences[purpleLightnessKey(lutId)] = params.purpleLightness - preferences[magentaHueKey(lutId)] = params.magentaHue - preferences[magentaChromaKey(lutId)] = params.magentaChroma - preferences[magentaLightnessKey(lutId)] = params.magentaLightness - preferences[lutIntensityKey(lutId)] = params.lutIntensity - preferences[remarksKey(lutId)] = params.remarks + preferences[recipeKey(lutId)] = params.toJson() + preferences.removeLegacyKeys(lutId) } // PLog.d(TAG, "Color recipe params saved for LUT [$lutId]: $params") } /** - * 加载指定 LUT 的色彩配方参数(同步方法) + * 加载指定 LUT 的色彩配方参数(一次性读取) * * @param lutId LUT ID * @return 色彩配方参数,如果未设置则返回默认值 */ suspend fun loadColorRecipeParams(lutId: String): ColorRecipeParams { return context.colorRecipeDataStore.data.map { preferences -> - ColorRecipeParams( - exposure = preferences[exposureKey(lutId)] ?: 0f, - contrast = preferences[contrastKey(lutId)] ?: 1f, - saturation = preferences[saturationKey(lutId)] ?: 1f, - temperature = preferences[temperatureKey(lutId)] ?: 0f, - tint = preferences[tintKey(lutId)] ?: 0f, - fade = preferences[fadeKey(lutId)] ?: 0f, - color = preferences[colorKey(lutId)] ?: 0f, - highlights = preferences[highlightsKey(lutId)] ?: 0f, - shadows = preferences[shadowsKey(lutId)] ?: 0f, - toneToe = preferences[toneToeKey(lutId)] ?: 0f, - toneShoulder = preferences[toneShoulderKey(lutId)] ?: 0f, - tonePivot = preferences[tonePivotKey(lutId)] ?: 0f, - paletteX = preferences[paletteXKey(lutId)] ?: 0.5f, - paletteY = preferences[paletteYKey(lutId)] ?: 0.5f, - paletteDensity = preferences[paletteDensityKey(lutId)] ?: 1f, - filmGrain = preferences[filmGrainKey(lutId)] ?: 0f, - vignette = preferences[vignetteKey(lutId)] ?: 0f, - bleachBypass = preferences[bleachBypassKey(lutId)] ?: 0f, - halation = preferences[halationKey(lutId)] ?: 0f, - chromaticAberration = preferences[chromaticAberrationKey(lutId)] ?: 0f, - noise = preferences[noiseKey(lutId)] ?: 0f, - lowRes = preferences[lowResKey(lutId)] ?: 0f, - skinHue = preferences[skinHueKey(lutId)] ?: 0f, - skinChroma = preferences[skinChromaKey(lutId)] ?: 0f, - skinLightness = preferences[skinLightnessKey(lutId)] ?: 0f, - redHue = preferences[redHueKey(lutId)] ?: 0f, - redChroma = preferences[redChromaKey(lutId)] ?: 0f, - redLightness = preferences[redLightnessKey(lutId)] ?: 0f, - orangeHue = preferences[orangeHueKey(lutId)] ?: 0f, - orangeChroma = preferences[orangeChromaKey(lutId)] ?: 0f, - orangeLightness = preferences[orangeLightnessKey(lutId)] ?: 0f, - yellowHue = preferences[yellowHueKey(lutId)] ?: 0f, - yellowChroma = preferences[yellowChromaKey(lutId)] ?: 0f, - yellowLightness = preferences[yellowLightnessKey(lutId)] ?: 0f, - greenHue = preferences[greenHueKey(lutId)] ?: 0f, - greenChroma = preferences[greenChromaKey(lutId)] ?: 0f, - greenLightness = preferences[greenLightnessKey(lutId)] ?: 0f, - cyanHue = preferences[cyanHueKey(lutId)] ?: 0f, - cyanChroma = preferences[cyanChromaKey(lutId)] ?: 0f, - cyanLightness = preferences[cyanLightnessKey(lutId)] ?: 0f, - blueHue = preferences[blueHueKey(lutId)] ?: 0f, - blueChroma = preferences[blueChromaKey(lutId)] ?: 0f, - blueLightness = preferences[blueLightnessKey(lutId)] ?: 0f, - purpleHue = preferences[purpleHueKey(lutId)] ?: 0f, - purpleChroma = preferences[purpleChromaKey(lutId)] ?: 0f, - purpleLightness = preferences[purpleLightnessKey(lutId)] ?: 0f, - magentaHue = preferences[magentaHueKey(lutId)] ?: 0f, - magentaChroma = preferences[magentaChromaKey(lutId)] ?: 0f, - magentaLightness = preferences[magentaLightnessKey(lutId)] ?: 0f, - lutIntensity = preferences[lutIntensityKey(lutId)] ?: 1f, - remarks = preferences[remarksKey(lutId)] ?: "" - ) + val json = preferences[recipeKey(lutId)] + if (json != null) ColorRecipeParams.fromJson(json) + else readLegacyParams(preferences, lutId) }.firstOrNull() ?: ColorRecipeParams.DEFAULT } @@ -416,57 +301,8 @@ class LutManager(private val context: Context) { */ suspend fun deleteColorRecipeParams(lutId: String) { context.colorRecipeDataStore.edit { preferences -> - preferences.remove(exposureKey(lutId)) - preferences.remove(contrastKey(lutId)) - preferences.remove(saturationKey(lutId)) - preferences.remove(temperatureKey(lutId)) - preferences.remove(tintKey(lutId)) - preferences.remove(fadeKey(lutId)) - preferences.remove(colorKey(lutId)) - preferences.remove(highlightsKey(lutId)) - preferences.remove(shadowsKey(lutId)) - preferences.remove(toneToeKey(lutId)) - preferences.remove(toneShoulderKey(lutId)) - preferences.remove(tonePivotKey(lutId)) - preferences.remove(paletteXKey(lutId)) - preferences.remove(paletteYKey(lutId)) - preferences.remove(paletteDensityKey(lutId)) - preferences.remove(filmGrainKey(lutId)) - preferences.remove(vignetteKey(lutId)) - preferences.remove(bleachBypassKey(lutId)) - preferences.remove(halationKey(lutId)) - preferences.remove(chromaticAberrationKey(lutId)) - preferences.remove(noiseKey(lutId)) - preferences.remove(lowResKey(lutId)) - preferences.remove(skinHueKey(lutId)) - preferences.remove(skinChromaKey(lutId)) - preferences.remove(skinLightnessKey(lutId)) - preferences.remove(redHueKey(lutId)) - preferences.remove(redChromaKey(lutId)) - preferences.remove(redLightnessKey(lutId)) - preferences.remove(orangeHueKey(lutId)) - preferences.remove(orangeChromaKey(lutId)) - preferences.remove(orangeLightnessKey(lutId)) - preferences.remove(yellowHueKey(lutId)) - preferences.remove(yellowChromaKey(lutId)) - preferences.remove(yellowLightnessKey(lutId)) - preferences.remove(greenHueKey(lutId)) - preferences.remove(greenChromaKey(lutId)) - preferences.remove(greenLightnessKey(lutId)) - preferences.remove(cyanHueKey(lutId)) - preferences.remove(cyanChromaKey(lutId)) - preferences.remove(cyanLightnessKey(lutId)) - preferences.remove(blueHueKey(lutId)) - preferences.remove(blueChromaKey(lutId)) - preferences.remove(blueLightnessKey(lutId)) - preferences.remove(purpleHueKey(lutId)) - preferences.remove(purpleChromaKey(lutId)) - preferences.remove(purpleLightnessKey(lutId)) - preferences.remove(magentaHueKey(lutId)) - preferences.remove(magentaChromaKey(lutId)) - preferences.remove(magentaLightnessKey(lutId)) - preferences.remove(lutIntensityKey(lutId)) - preferences.remove(remarksKey(lutId)) + preferences.remove(recipeKey(lutId)) + preferences.removeLegacyKeys(lutId) } PLog.d(TAG, "Color recipe params deleted for LUT [$lutId]") } diff --git a/app/src/main/java/com/hinnka/mycamera/lut/LutRenderer.kt b/app/src/main/java/com/hinnka/mycamera/lut/LutRenderer.kt index a6370820..3ad45fb4 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutRenderer.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutRenderer.kt @@ -1787,6 +1787,46 @@ class LutRenderer : GLSurfaceView.Renderer { } } + /** + * 从 ColorRecipeParams 统一设置所有渲染参数 + */ + fun setRecipeParams(params: com.hinnka.mycamera.model.ColorRecipeParams) { + lutIntensity = params.lutIntensity + exposure = params.exposure + contrast = params.contrast + saturation = params.saturation + temperature = params.temperature + tint = params.tint + fade = params.fade + vibrance = params.color + highlights = params.highlights + shadows = params.shadows + toneToe = params.toneToe + toneShoulder = params.toneShoulder + tonePivot = params.tonePivot + filmGrain = params.filmGrain + vignette = params.vignette + bleachBypass = params.bleachBypass + chromaticAberration = params.chromaticAberration + halation = params.halation + noise = params.noise + lowRes = params.lowRes + setLchAdjustments( + floatArrayOf( + params.skinHue, params.redHue, params.orangeHue, params.yellowHue, + params.greenHue, params.cyanHue, params.blueHue, params.purpleHue, params.magentaHue, + ), + floatArrayOf( + params.skinChroma, params.redChroma, params.orangeChroma, params.yellowChroma, + params.greenChroma, params.cyanChroma, params.blueChroma, params.purpleChroma, params.magentaChroma, + ), + floatArrayOf( + params.skinLightness, params.redLightness, params.orangeLightness, params.yellowLightness, + params.greenLightness, params.cyanLightness, params.blueLightness, params.purpleLightness, params.magentaLightness, + ) + ) + } + private fun initBokehProgram() { val vs = GlUtils.compileShader(GLES30.GL_VERTEX_SHADER, Shaders.SIMPLE_VERTEX_SHADER) val fs = GlUtils.compileShader(GLES30.GL_FRAGMENT_SHADER, Shaders.BOKEH_FRAGMENT_SHADER) diff --git a/app/src/main/java/com/hinnka/mycamera/model/ColorRecipe.kt b/app/src/main/java/com/hinnka/mycamera/model/ColorRecipe.kt index 13e1536d..40f460ab 100644 --- a/app/src/main/java/com/hinnka/mycamera/model/ColorRecipe.kt +++ b/app/src/main/java/com/hinnka/mycamera/model/ColorRecipe.kt @@ -1,5 +1,6 @@ package com.hinnka.mycamera.model +import com.google.gson.Gson import com.hinnka.mycamera.R /** @@ -173,7 +174,20 @@ data class ColorRecipeParams( remarks == other.remarks } + /** + * 序列化为 JSON 字符串 + */ + fun toJson(): String = gson.toJson(this) + companion object { + private val gson = Gson() + + /** + * 从 JSON 字符串反序列化 + */ + fun fromJson(json: String): ColorRecipeParams = + gson.fromJson(json, ColorRecipeParams::class.java) ?: DEFAULT + /** * 默认参数(无调整) */ diff --git a/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraGLSurfaceView.kt b/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraGLSurfaceView.kt index ec2f4f07..966bc09e 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraGLSurfaceView.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraGLSurfaceView.kt @@ -173,61 +173,7 @@ class CameraGLSurfaceView @JvmOverloads constructor( ) { val effectiveParams = ColorPaletteMapper.mergeIntoEffectiveParams(params) - renderer.exposure = effectiveParams.exposure - renderer.contrast = effectiveParams.contrast - renderer.saturation = effectiveParams.saturation - renderer.temperature = effectiveParams.temperature - renderer.tint = effectiveParams.tint - renderer.fade = effectiveParams.fade - renderer.vibrance = effectiveParams.color - renderer.highlights = effectiveParams.highlights - renderer.shadows = effectiveParams.shadows - renderer.toneToe = effectiveParams.toneToe - renderer.toneShoulder = effectiveParams.toneShoulder - renderer.tonePivot = effectiveParams.tonePivot - renderer.filmGrain = effectiveParams.filmGrain - renderer.vignette = effectiveParams.vignette - renderer.bleachBypass = effectiveParams.bleachBypass - renderer.chromaticAberration = effectiveParams.chromaticAberration - renderer.noise = effectiveParams.noise - renderer.lowRes = effectiveParams.lowRes - renderer.halation = effectiveParams.halation - renderer.lutIntensity = effectiveParams.lutIntensity - renderer.setLchAdjustments( - floatArrayOf( - effectiveParams.skinHue, - effectiveParams.redHue, - effectiveParams.orangeHue, - effectiveParams.yellowHue, - effectiveParams.greenHue, - effectiveParams.cyanHue, - effectiveParams.blueHue, - effectiveParams.purpleHue, - effectiveParams.magentaHue, - ), - floatArrayOf( - effectiveParams.skinChroma, - effectiveParams.redChroma, - effectiveParams.orangeChroma, - effectiveParams.yellowChroma, - effectiveParams.greenChroma, - effectiveParams.cyanChroma, - effectiveParams.blueChroma, - effectiveParams.purpleChroma, - effectiveParams.magentaChroma, - ), - floatArrayOf( - effectiveParams.skinLightness, - effectiveParams.redLightness, - effectiveParams.orangeLightness, - effectiveParams.yellowLightness, - effectiveParams.greenLightness, - effectiveParams.cyanLightness, - effectiveParams.blueLightness, - effectiveParams.purpleLightness, - params.magentaLightness, - ) - ) + renderer.setRecipeParams(effectiveParams) renderer.aperture = aperture requestRender() } diff --git a/app/src/main/java/com/hinnka/mycamera/ui/settings/FilterManagementScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/settings/FilterManagementScreen.kt index d6416779..95ae2648 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/settings/FilterManagementScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/settings/FilterManagementScreen.kt @@ -184,16 +184,16 @@ fun FilterManagementScreen( } var pendingExportName by remember { mutableStateOf("") } - var pendingExportContent by remember { mutableStateOf("") } + var pendingExportBytes by remember { mutableStateOf(ByteArray(0)) } val exportLauncher = rememberLauncherForActivityResult( - contract = ActivityResultContracts.CreateDocument("text/plain"), + contract = ActivityResultContracts.CreateDocument("application/octet-stream"), ) { uri -> if (uri != null) { scope.launch { withContext(Dispatchers.IO) { try { context.contentResolver.openOutputStream(uri)?.use { output -> - output.write(pendingExportContent.toByteArray()) + output.write(pendingExportBytes) } withContext(Dispatchers.Main) { android.widget.Toast.makeText( @@ -580,10 +580,10 @@ fun FilterManagementScreen( onExport = if (!isSelectionMode) { { scope.launch { - val content = viewModel.getLutCubeString(lutInfo.id) - if (content != null) { - pendingExportContent = content - pendingExportName = "${lutInfo.getName()}.cube" + val bytes = viewModel.exportLutToPlut(lutInfo.id) + if (bytes != null) { + pendingExportBytes = bytes + pendingExportName = "${lutInfo.getName()}.plut" exportLauncher.launch(pendingExportName) } } @@ -1028,7 +1028,7 @@ fun FilterManagementScreen( urisToImport.forEachIndexed { index, uri -> importProgress = Pair(index + 1, urisToImport.size) - val result = withContext(Dispatchers.IO) { + val lutId = withContext(Dispatchers.IO) { customImportManager.importLut( uri, category = targetCategory, @@ -1036,8 +1036,10 @@ fun FilterManagementScreen( curve = curveToUse ) } - if (result != null) { + if (lutId != null) { successCount++ + // 若为 .plut v4 文件,提取嵌入的色彩配方并保存 + viewModel.extractAndSaveColorRecipeFromPlut(lutId, uri) } else { failCount++ } diff --git a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt index 3429422e..ec346501 100644 --- a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt +++ b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt @@ -22,6 +22,7 @@ import com.hinnka.mycamera.frame.FrameInfo import com.hinnka.mycamera.gallery.PhotoManager import com.hinnka.mycamera.gallery.PhotoMetadata import com.hinnka.mycamera.lut.LutConfig +import com.hinnka.mycamera.lut.LutConverter import com.hinnka.mycamera.lut.LutInfo import com.hinnka.mycamera.lut.creator.LutGenerator import com.hinnka.mycamera.lut.creator.OpenAIApiClient @@ -2541,6 +2542,36 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) LutGenerator.exportToCubeString(floatArray, lutConfig.size, lutInfo.getName()) } + + /** + * 将 LUT(含色彩配方)导出为 .plut v4 字节数组 + * 若该 LUT 没有色彩配方则导出为标准 v3 格式 + */ + suspend fun exportLutToPlut(lutId: String): ByteArray? = withContext(Dispatchers.IO) { + val lutConfig = contentRepository.lutManager.loadLut(lutId) ?: return@withContext null + val recipe = contentRepository.lutManager.loadColorRecipeParams(lutId) + val recipeJson = if (!recipe.isDefault()) recipe.toJson() else null + + val outputStream = java.io.ByteArrayOutputStream() + LutConverter.exportToPlut(lutConfig, outputStream, recipeJson) + outputStream.toByteArray() + } + + /** + * 从导入的 .plut URI 中提取嵌入的色彩配方并保存到指定 LUT(仅 v4 文件含有配方) + */ + suspend fun extractAndSaveColorRecipeFromPlut(lutId: String, uri: android.net.Uri) = withContext(Dispatchers.IO) { + try { + val recipeJson = getApplication().contentResolver + .openInputStream(uri)?.use { LutConverter.extractRecipeJsonFromPlut(it) } + ?: return@withContext + val params = ColorRecipeParams.fromJson(recipeJson) + contentRepository.lutManager.saveColorRecipeParams(lutId, params) + } catch (e: Exception) { + PLog.e("CameraViewModel", "Failed to extract recipe from plut", e) + } + } + } private fun shouldIncludeCropRegionInOutputSize(imageFormat: Int): Boolean {