From bad1a15b8641ca69939bc191cc76589aeadeb65a Mon Sep 17 00:00:00 2001 From: Hinnka Date: Sun, 1 Mar 2026 18:17:26 +0800 Subject: [PATCH] fix: lut color --- .../mycamera/data/CustomImportManager.kt | 9 +- .../java/com/hinnka/mycamera/lut/LutConfig.kt | 29 +- .../com/hinnka/mycamera/lut/LutConverter.kt | 16 +- .../hinnka/mycamera/lut/LutImageProcessor.kt | 44 +- .../java/com/hinnka/mycamera/lut/LutParser.kt | 7 +- .../com/hinnka/mycamera/lut/LutRenderer.kt | 3 + .../java/com/hinnka/mycamera/lut/Shaders.kt | 43 +- .../com/hinnka/mycamera/lut/XmpLutParser.kt | 32 +- .../com/hinnka/mycamera/raw/ColorSpace.kt | 6 + .../mycamera/ui/components/LutSelector.kt | 1 + .../ui/settings/CustomImportSection.kt | 459 ------------------ .../ui/settings/FilterManagementScreen.kt | 144 ++++-- app/src/main/res/values-zh/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + scripts/calc_matrices.py | 62 +++ 15 files changed, 309 insertions(+), 550 deletions(-) delete mode 100644 app/src/main/java/com/hinnka/mycamera/ui/settings/CustomImportSection.kt create mode 100644 scripts/calc_matrices.py 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 0f1e7767..aade049c 100644 --- a/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt +++ b/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt @@ -7,6 +7,7 @@ import com.hinnka.mycamera.lut.LutConverter import com.hinnka.mycamera.lut.LutCurve import com.hinnka.mycamera.lut.LutInfo import com.hinnka.mycamera.lut.XmpLutParser +import com.hinnka.mycamera.raw.ColorSpace import com.hinnka.mycamera.utils.PLog import org.json.JSONArray import org.json.JSONObject @@ -82,7 +83,7 @@ class CustomImportManager(private val context: Context) { * @param curve 输入曲线类型(可选) * @return 导入成功的 LUT ID,失败返回 null */ - fun importLut(uri: Uri, displayName: String? = null, category: String? = null, curve: LutCurve = LutCurve.SRGB): String? { + fun importLut(uri: Uri, displayName: String? = null, category: String? = null, colorSpace: ColorSpace = ColorSpace.SRGB, curve: LutCurve = LutCurve.SRGB): String? { return try { val fileName = getFileName(uri) ?: "lut_${System.currentTimeMillis()}.cube" val lutId = "custom_${UUID.randomUUID()}" @@ -93,10 +94,10 @@ class CustomImportManager(private val context: Context) { context.contentResolver.openInputStream(uri)?.use { inputStream -> FileOutputStream(plutFile).use { outputStream -> val success = if (fileName.endsWith(".xmp", ignoreCase = true)) { - XmpLutParser.parse(inputStream, outputStream, curve = curve) + XmpLutParser.parse(inputStream, outputStream, colorSpace = colorSpace, curve = curve) } else if (fileName.endsWith(".png", ignoreCase = true)) { - LutConverter.convertPngToplut(inputStream, outputStream, curve = curve) - } else LutConverter.convertCubeToplut(inputStream, outputStream, curve = curve) + LutConverter.convertPngToplut(inputStream, outputStream, colorSpace = colorSpace, curve = curve) + } else LutConverter.convertCubeToplut(inputStream, outputStream, colorSpace = colorSpace, curve = curve) if (!success) { plutFile.delete() diff --git a/app/src/main/java/com/hinnka/mycamera/lut/LutConfig.kt b/app/src/main/java/com/hinnka/mycamera/lut/LutConfig.kt index a1c02154..8be6b013 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutConfig.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutConfig.kt @@ -1,6 +1,7 @@ package com.hinnka.mycamera.lut import com.hinnka.mycamera.model.ColorRecipeParams +import com.hinnka.mycamera.raw.ColorSpace import java.nio.ByteBuffer import java.nio.ByteOrder import java.nio.FloatBuffer @@ -19,7 +20,8 @@ data class LutConfig( val byteBuffer: ByteBuffer? = null, val title: String = "", val configDataType: Int = CONFIG_DATA_TYPE_UINT8, - val curve: LutCurve = LutCurve.SRGB + val curve: LutCurve = LutCurve.SRGB, + val colorSpace: ColorSpace = ColorSpace.SRGB ) { companion object { const val CONFIG_DATA_TYPE_UINT8 = 0 @@ -91,31 +93,6 @@ data class LutConfig( val expectedCapacity = if (configDataType == CONFIG_DATA_TYPE_UINT16) count * 2 else count return size > 0 && (data?.size == count || byteBuffer?.capacity() == expectedCapacity) } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as LutConfig - - if (size != other.size) return false - if (data != null) { - if (other.data == null || !data.contentEquals(other.data)) return false - } else if (other.data != null) return false - - if (byteBuffer != other.byteBuffer) return false - if (title != other.title) return false - - return true - } - - override fun hashCode(): Int { - var result = size - result = 31 * result + (data?.contentHashCode() ?: 0) - result = 31 * result + (byteBuffer?.hashCode() ?: 0) - result = 31 * result + title.hashCode() - return result - } } /** 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 228edfd6..49a722fe 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutConverter.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutConverter.kt @@ -8,6 +8,7 @@ import kotlin.math.max import kotlin.math.min import android.graphics.BitmapFactory import android.graphics.Color +import com.hinnka.mycamera.raw.ColorSpace import com.hinnka.mycamera.utils.PLog import kotlin.math.roundToInt @@ -20,7 +21,7 @@ import kotlin.math.roundToInt object LutConverter { private const val MAGIC_PLUT = "PLUT" - private const val VERSION = 2 + private const val VERSION = 3 private const val DATA_TYPE_UINT16 = 1 /** @@ -41,6 +42,7 @@ object LutConverter { fun convertCubeToplut( cubeInputStream: InputStream, plutOutputStream: OutputStream, + colorSpace: ColorSpace = ColorSpace.SRGB, curve: LutCurve = LutCurve.SRGB ): Boolean { return try { @@ -53,7 +55,7 @@ object LutConverter { // 不再进行重采样,而是直接写入原始数据并记录曲线类型 // 写入 .plut 格式 - writePLutFile(cubeData, plutOutputStream, curve) + writePLutFile(cubeData, plutOutputStream, colorSpace, curve) true } catch (e: Exception) { @@ -73,6 +75,7 @@ object LutConverter { fun convertPngToplut( pngInputStream: InputStream, plutOutputStream: OutputStream, + colorSpace: ColorSpace = ColorSpace.SRGB, curve: LutCurve = LutCurve.SRGB ): Boolean { return try { @@ -160,7 +163,7 @@ object LutConverter { if (cubeData.size > 65) { cubeData = resampleSize(cubeData, 33) } - writePLutFile(cubeData, plutOutputStream, curve) + writePLutFile(cubeData, plutOutputStream, colorSpace, curve) true } catch (e: Exception) { e.printStackTrace() @@ -447,8 +450,8 @@ object LutConverter { /** * 写入 .plut 文件 */ - private fun writePLutFile(cubeData: CubeData, outputStream: OutputStream, curve: LutCurve) { - val buffer = ByteBuffer.allocate(20 + cubeData.data.size * 2) + private fun writePLutFile(cubeData: CubeData, outputStream: OutputStream, colorSpace: ColorSpace, curve: LutCurve) { + val buffer = ByteBuffer.allocate(24 + cubeData.data.size * 2) .order(ByteOrder.LITTLE_ENDIAN) // Magic @@ -465,6 +468,9 @@ object LutConverter { // Curve Type buffer.putInt(curve.ordinal) + + // Color Space + buffer.putInt(colorSpace.ordinal) // Data val shortBuffer = buffer.asShortBuffer() 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 91c4ba90..fffa2f0a 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt @@ -66,6 +66,7 @@ class LutImageProcessor { private var uLutIntensityLoc = 0 private var uLutEnabledLoc = 0 private var uLutCurveLoc = 0 + private var uLutColorSpaceLoc = 0 private var uMVPMatrixLoc = 0 // 色彩配方 Uniform 位置 @@ -382,6 +383,7 @@ class LutImageProcessor { ) GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uLutEnabled"), if (lutConfig != null) 1 else 0) GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uLutCurve"), lutConfig?.curve?.ordinal ?: 0) + GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uLutColorSpace"), lutConfig?.colorSpace?.ordinal ?: 0) // 设置色彩配方参数 GLES30.glUniform1i( @@ -627,6 +629,7 @@ class LutImageProcessor { uLutIntensityLoc = GLES30.glGetUniformLocation(shaderProgram, "uLutIntensity") uLutEnabledLoc = GLES30.glGetUniformLocation(shaderProgram, "uLutEnabled") uLutCurveLoc = GLES30.glGetUniformLocation(shaderProgram, "uLutCurve") + uLutColorSpaceLoc = GLES30.glGetUniformLocation(shaderProgram, "uLutColorSpace") uMVPMatrixLoc = GLES30.glGetUniformLocation(shaderProgram, "uMVPMatrix") // 获取色彩配方 uniform 位置 @@ -951,6 +954,7 @@ class LutImageProcessor { uniform float uLutIntensity; uniform bool uLutEnabled; uniform int uLutCurve; + uniform int uLutColorSpace; // 色彩配方控制 uniform bool uColorRecipeEnabled; @@ -981,13 +985,18 @@ class LutImageProcessor { float log10(float x) { return log(x) * 0.4342944819; } vec3 log10(vec3 x) { return log(x) * 0.4342944819; } + vec3 linearToSrgb(vec3 l) { + return mix(l * 12.92, 1.055 * pow(l, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, l)); + } + vec3 srgbToLinear(vec3 c) { return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), step(0.04045, c)); } - vec3 applyLutCurve(vec3 rgb, int curveType) { - if (curveType == 0) return rgb; // SRGB - vec3 l = srgbToLinear(rgb); + vec3 applyLutCurve(vec3 l, int curveType) { + if (curveType == 0) { // sRGB + return linearToSrgb(l); + } if (curveType == 1) return l; // LINEAR if (curveType == 2) { // V-Log return mix(5.6 * l + 0.125, 0.241514 * log10(l + 0.00873) + 0.598206, step(0.01, l)); @@ -1010,6 +1019,31 @@ class LutImageProcessor { float hc = 0.5 - ha * log(4.0 * ha); return mix(sqrt(3.0 * l), ha * log(12.0 * l - hb) + hc, step(1.0 / 12.0, l)); } + return l; + } + + vec3 applyLutColorSpace(vec3 rgb, int colorSpace) { + if (colorSpace == 0) return rgb; // sRGB + + // Matrices for Linear sRGB to target color space (aligned with ColorSpace.kt) + if (colorSpace == 1) { // DCI-P3 (Bradford adapted) + return mat3(0.875905, 0.035332, 0.016382, 0.122070, 0.964542, 0.063767, 0.002025, 0.000126, 0.919851) * rgb; + } + if (colorSpace == 2) { // BT2020 + return mat3(0.627404, 0.069097, 0.016391, 0.329283, 0.919540, 0.088013, 0.043313, 0.011362, 0.895595) * rgb; + } + if (colorSpace == 3) { // ARRI4 + return mat3(0.565837, 0.088626, 0.017750, 0.340331, 0.809347, 0.109448, 0.093832, 0.102028, 0.872802) * rgb; + } + if (colorSpace == 4) { // AppleLog2 + return mat3(0.608104, 0.062316, 0.031133, 0.259353, 0.804609, 0.133756, 0.132543, 0.133076, 0.835112) * rgb; + } + if (colorSpace == 5) { // S-Gamut3.Cine + return mat3(0.645679, 0.087530, 0.036957, 0.259115, 0.759700, 0.129281, 0.095206, 0.152770, 0.833762) * rgb; + } + if (colorSpace == 6) { // ACES_AP1 + return mat3(0.613083, 0.070004, 0.020491, 0.341167, 0.918063, 0.106764, 0.045750, 0.011934, 0.872745) * rgb; + } return rgb; } @@ -1174,7 +1208,9 @@ class LutImageProcessor { // === LUT 处理(在色彩配方之后) === if (uLutEnabled && uLutIntensity > 0.0) { - vec3 lutInColor = applyLutCurve(color.rgb, uLutCurve); + vec3 linearRGB = srgbToLinear(color.rgb); + vec3 colorSpaceRGB = applyLutColorSpace(linearRGB, uLutColorSpace); + vec3 lutInColor = applyLutCurve(colorSpaceRGB, uLutCurve); float scale = (uLutSize - 1.0) / uLutSize; float offset = 1.0 / (2.0 * uLutSize); vec3 lutCoord = lutInColor * scale + offset; diff --git a/app/src/main/java/com/hinnka/mycamera/lut/LutParser.kt b/app/src/main/java/com/hinnka/mycamera/lut/LutParser.kt index 17468212..fac02164 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutParser.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutParser.kt @@ -2,6 +2,7 @@ package com.hinnka.mycamera.lut import android.content.Context import android.util.Log +import com.hinnka.mycamera.raw.ColorSpace import com.hinnka.mycamera.utils.PLog import org.json.JSONObject import java.io.InputStream @@ -51,6 +52,9 @@ object LutParser { val dataType = buffer.int val curveOrdinal = if (version >= 2) buffer.int else LutCurve.SRGB.ordinal val curve = LutCurve.entries.getOrElse(curveOrdinal) { LutCurve.SRGB } + + val colorSpaceOrdinal = if (version >= 3) buffer.int else ColorSpace.SRGB.ordinal + val colorSpace = ColorSpace.entries.getOrElse(colorSpaceOrdinal) { ColorSpace.SRGB } val count = size * size * size * 3 val bytesPerComponent = if (dataType == 1) 2 else 1 @@ -72,7 +76,8 @@ object LutParser { byteBuffer = directBuffer, title = title, configDataType = if (dataType == 1) LutConfig.CONFIG_DATA_TYPE_UINT16 else LutConfig.CONFIG_DATA_TYPE_UINT8, - curve = curve + curve = curve, + colorSpace = colorSpace ) } else { // 未来可以支持 Float32 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 d9bbb89b..139c2f09 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutRenderer.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutRenderer.kt @@ -80,6 +80,7 @@ class LutRenderer : GLSurfaceView.Renderer { private var uLutIntensityLocation: Int = 0 private var uLutEnabledLocation: Int = 0 private var uLutCurveLocation: Int = 0 + private var uLutColorSpaceLocation: Int = 0 // 色彩配方 Uniform 位置 private var uColorRecipeEnabledLocation: Int = 0 @@ -469,6 +470,7 @@ class LutRenderer : GLSurfaceView.Renderer { GLES30.glUniform1f(uLutIntensityLocation, lutIntensity) GLES30.glUniform1i(uLutEnabledLocation, if (lutEnabled && lutTextureId != 0) 1 else 0) GLES30.glUniform1i(uLutCurveLocation, currentLutConfig?.curve?.ordinal ?: 0) + GLES30.glUniform1i(uLutColorSpaceLocation, currentLutConfig?.colorSpace?.ordinal ?: 0) // 设置色彩配方 Uniforms GLES30.glUniform1i(uColorRecipeEnabledLocation, if (colorRecipeEnabled) 1 else 0) @@ -573,6 +575,7 @@ class LutRenderer : GLSurfaceView.Renderer { uLutIntensityLocation = GLES30.glGetUniformLocation(programId, "uLutIntensity") uLutEnabledLocation = GLES30.glGetUniformLocation(programId, "uLutEnabled") uLutCurveLocation = GLES30.glGetUniformLocation(programId, "uLutCurve") + uLutColorSpaceLocation = GLES30.glGetUniformLocation(programId, "uLutColorSpace") // 获取色彩配方 Uniform 位置 uColorRecipeEnabledLocation = GLES30.glGetUniformLocation(programId, "uColorRecipeEnabled") diff --git a/app/src/main/java/com/hinnka/mycamera/lut/Shaders.kt b/app/src/main/java/com/hinnka/mycamera/lut/Shaders.kt index ff997fa2..8424c477 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/Shaders.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/Shaders.kt @@ -111,6 +111,7 @@ object Shaders { uniform float uLutIntensity; uniform bool uLutEnabled; uniform int uLutCurve; // 0=sRGB, 1=Linear, 2=V-Log, 3=S-Log3, 4=F-Log2, 5=LogC, 6=AppleLog, 7=HLG + uniform int uLutColorSpace; // 0=sRGB, 1=DCI-P3, 2=BT2020, 3=ARRI4, 4=AppleLog2, 5=ProPhoto, 6=ACES_AP1 // 色彩配方控制 uniform bool uColorRecipeEnabled; @@ -144,9 +145,14 @@ object Shaders { return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), step(0.04045, c)); } - vec3 applyLutCurve(vec3 rgb, int curveType) { - if (curveType == 0) return rgb; // SRGB - vec3 l = srgbToLinear(rgb); + vec3 linearToSrgb(vec3 l) { + return mix(12.92 * l, 1.055 * pow(l, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, l)); + } + + vec3 applyLutCurve(vec3 l, int curveType) { + if (curveType == 0) { // sRGB + return linearToSrgb(l); + } if (curveType == 1) return l; // LINEAR if (curveType == 2) { // V-Log return mix(5.6 * l + 0.125, 0.241514 * log10(l + 0.00873) + 0.598206, step(0.01, l)); @@ -169,6 +175,31 @@ object Shaders { float hc = 0.5 - ha * log(4.0 * ha); return mix(sqrt(3.0 * l), ha * log(12.0 * l - hb) + hc, step(1.0 / 12.0, l)); } + return l; + } + + vec3 applyLutColorSpace(vec3 rgb, int colorSpace) { + if (colorSpace == 0) return rgb; // sRGB + + // Matrices for Linear sRGB to target color space (aligned with ColorSpace.kt) + if (colorSpace == 1) { // DCI-P3 (Bradford adapted) + return mat3(0.875905, 0.035332, 0.016382, 0.122070, 0.964542, 0.063767, 0.002025, 0.000126, 0.919851) * rgb; + } + if (colorSpace == 2) { // BT2020 + return mat3(0.627404, 0.069097, 0.016391, 0.329283, 0.919540, 0.088013, 0.043313, 0.011362, 0.895595) * rgb; + } + if (colorSpace == 3) { // ARRI4 + return mat3(0.565837, 0.088626, 0.017750, 0.340331, 0.809347, 0.109448, 0.093832, 0.102028, 0.872802) * rgb; + } + if (colorSpace == 4) { // AppleLog2 + return mat3(0.608104, 0.062316, 0.031133, 0.259353, 0.804609, 0.133756, 0.132543, 0.133076, 0.835112) * rgb; + } + if (colorSpace == 5) { // S-Gamut3.Cine + return mat3(0.645679, 0.087530, 0.036957, 0.259115, 0.759700, 0.129281, 0.095206, 0.152770, 0.833762) * rgb; + } + if (colorSpace == 6) { // ACES_AP1 + return mat3(0.613083, 0.070004, 0.020491, 0.341167, 0.918063, 0.106764, 0.045750, 0.011934, 0.872745) * rgb; + } return rgb; } @@ -293,9 +324,11 @@ object Shaders { color.rgb = clamp(color.rgb, 0.0, 1.0); } - // === LUT 处理 === + // === LUT 处理(在色彩配方之后) === if (uLutEnabled && uLutIntensity > 0.0) { - vec3 lutInColor = applyLutCurve(color.rgb, uLutCurve); + vec3 linearRGB = srgbToLinear(color.rgb); + vec3 colorSpaceRGB = applyLutColorSpace(linearRGB, uLutColorSpace); + vec3 lutInColor = applyLutCurve(colorSpaceRGB, uLutCurve); float scale = (uLutSize - 1.0) / uLutSize; float offset = 1.0 / (2.0 * uLutSize); vec3 lutCoord = lutInColor * scale + offset; diff --git a/app/src/main/java/com/hinnka/mycamera/lut/XmpLutParser.kt b/app/src/main/java/com/hinnka/mycamera/lut/XmpLutParser.kt index 1e1a014c..10dc286d 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/XmpLutParser.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/XmpLutParser.kt @@ -1,5 +1,6 @@ package com.hinnka.mycamera.lut +import com.hinnka.mycamera.raw.ColorSpace import com.hinnka.mycamera.utils.PLog import java.io.ByteArrayOutputStream import java.io.InputStream @@ -30,7 +31,7 @@ object XmpLutParser { } } - fun parse(inputStream: InputStream, outputStream: OutputStream, curve: LutCurve = LutCurve.SRGB): Boolean { + fun parse(inputStream: InputStream, outputStream: OutputStream, colorSpace: ColorSpace = ColorSpace.SRGB, curve: LutCurve = LutCurve.SRGB): Boolean { val factory = DocumentBuilderFactory.newInstance() factory.isNamespaceAware = true val builder = factory.newDocumentBuilder() @@ -88,7 +89,7 @@ object XmpLutParser { lutData = resampleSize(lutData, 33) } - return writePlutFile(outputStream, lutData, curve) + return writePlutFile(outputStream, lutData, colorSpace, curve) } private fun resampleLut(lutData: LutData, curve: LutCurve): LutData { @@ -290,22 +291,26 @@ object XmpLutParser { } val nopValue = IntArray(divisions) + val step = 65535.0 / (divisions - 1.0) for (i in 0 until divisions) { - nopValue[i] = ((i * 0xFFFF + (divisions / 2)) / (divisions - 1)) + nopValue[i] = (i * step + 0.5).toInt() } val size = divisions * divisions * divisions val samples = ShortArray(size * 3) - for (bd in 0 until divisions) { + for (rd in 0 until divisions) { for (gd in 0 until divisions) { - for (rd in 0 until divisions) { + for (bd in 0 until divisions) { + val rf_res = buffer.short.toInt() + val gf_res = buffer.short.toInt() + val bf_res = buffer.short.toInt() + + val rf = (rf_res + nopValue[rd]).coerceIn(0, 65535) + val gf = (gf_res + nopValue[gd]).coerceIn(0, 65535) + val bf = (bf_res + nopValue[bd]).coerceIn(0, 65535) + val index = ((bd * divisions + gd) * divisions + rd) * 3 - - val bf = ((buffer.short.toInt() and 0xFFFF) + nopValue[bd]) and 0xFFFF - val gf = ((buffer.short.toInt() and 0xFFFF) + nopValue[gd]) and 0xFFFF - val rf = ((buffer.short.toInt() and 0xFFFF) + nopValue[rd]) and 0xFFFF - samples[index] = rf.toShort() samples[index + 1] = gf.toShort() samples[index + 2] = bf.toShort() @@ -316,17 +321,18 @@ object XmpLutParser { return LutData(divisions, samples) } - private fun writePlutFile(outputStream: OutputStream, lut: LutData, curve: LutCurve): Boolean { + private fun writePlutFile(outputStream: OutputStream, lut: LutData, colorSpace: ColorSpace, curve: LutCurve): Boolean { val size = lut.divisions val count = size * size * size * 3 val expectedSizeInBytes = count * 2 // 16-bit - val buffer = ByteBuffer.allocate(20 + expectedSizeInBytes).order(ByteOrder.LITTLE_ENDIAN) + val buffer = ByteBuffer.allocate(24 + expectedSizeInBytes).order(ByteOrder.LITTLE_ENDIAN) buffer.putInt(MAGIC_PLUT) - buffer.putInt(2) // version + buffer.putInt(3) // version buffer.putInt(size) buffer.putInt(1) // dataType: 1 = UINT16 buffer.putInt(curve.ordinal) + buffer.putInt(colorSpace.ordinal) for (s in lut.samples) { buffer.putShort(s) diff --git a/app/src/main/java/com/hinnka/mycamera/raw/ColorSpace.kt b/app/src/main/java/com/hinnka/mycamera/raw/ColorSpace.kt index 635361ec..a68df1e0 100644 --- a/app/src/main/java/com/hinnka/mycamera/raw/ColorSpace.kt +++ b/app/src/main/java/com/hinnka/mycamera/raw/ColorSpace.kt @@ -39,6 +39,12 @@ enum class ColorSpace( 0.068f, -0.076f, 0.3127f, 0.3290f ), + S_GAMUT3_CINE( + 0.766f, 0.275f, + 0.225f, 0.8f, + 0.089f, -0.087f, + 0.3127f, 0.329f + ), ACES_AP1( 0.713f, 0.293f, 0.165f, 0.83f, diff --git a/app/src/main/java/com/hinnka/mycamera/ui/components/LutSelector.kt b/app/src/main/java/com/hinnka/mycamera/ui/components/LutSelector.kt index 6ca52783..34ba4f4e 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/components/LutSelector.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/components/LutSelector.kt @@ -33,6 +33,7 @@ import com.hinnka.mycamera.R import com.hinnka.mycamera.data.ContentRepository import com.hinnka.mycamera.lut.LutInfo import com.hinnka.mycamera.ui.camera.LutEditBottomSheet +import com.hinnka.mycamera.utils.PLog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext diff --git a/app/src/main/java/com/hinnka/mycamera/ui/settings/CustomImportSection.kt b/app/src/main/java/com/hinnka/mycamera/ui/settings/CustomImportSection.kt deleted file mode 100644 index ff2bdec1..00000000 --- a/app/src/main/java/com/hinnka/mycamera/ui/settings/CustomImportSection.kt +++ /dev/null @@ -1,459 +0,0 @@ -package com.hinnka.mycamera.ui.settings - -import android.net.Uri -import androidx.activity.compose.rememberLauncherForActivityResult -import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.foundation.background -import androidx.compose.foundation.layout.* -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Add -import androidx.compose.material.icons.filled.Delete -import androidx.compose.material.icons.filled.Edit -import androidx.compose.material3.* -import androidx.compose.runtime.* -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import com.hinnka.mycamera.R -import com.hinnka.mycamera.data.CustomImportManager -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext - -/** - * 自定义导入设置组件 - * - * 提供 LUT 和边框样式的导入功能 - */ -@Composable -fun CustomImportSection( - customImportManager: CustomImportManager, - onImportSuccess: () -> Unit, - modifier: Modifier = Modifier -) { - var importResult by remember { mutableStateOf(null) } - var customLuts by remember { mutableStateOf(customImportManager.getCustomLuts()) } - var customFrames by remember { mutableStateOf(customImportManager.getCustomFrames()) } - var isImporting by remember { mutableStateOf(false) } // 添加导入中状态 - var importProgress by remember { mutableStateOf?>(null) } // 当前进度和总数 - val scope = rememberCoroutineScope() - - // 刷新自定义内容列表 - fun refreshCustomContent() { - customLuts = customImportManager.getCustomLuts() - customFrames = customImportManager.getCustomFrames() - onImportSuccess() - } - - // LUT 文件选择器(支持批量导入) - val lutLauncher = rememberLauncherForActivityResult( - contract = ActivityResultContracts.OpenMultipleDocuments() - ) { uris: List -> - scope.launch { - if (uris.isNotEmpty()) { - isImporting = true - importProgress = Pair(0, uris.size) - - var successCount = 0 - var failCount = 0 - - uris.forEachIndexed { index, uri -> - importProgress = Pair(index + 1, uris.size) - val result = withContext(Dispatchers.IO) { - customImportManager.importLut(uri) - } - if (result != null) { - successCount++ - } else { - failCount++ - } - } - - isImporting = false - importProgress = null - - importResult = when { - failCount == 0 -> "成功导入 $successCount 个 LUT" - successCount == 0 -> "导入失败,共 $failCount 个文件" - else -> "成功导入 $successCount 个,失败 $failCount 个" - } - - if (successCount > 0) { - refreshCustomContent() - } - } - } - } - - // 边框样式文件选择器 - val frameLauncher = rememberLauncherForActivityResult( - contract = ActivityResultContracts.GetContent() - ) { uri: Uri? -> - scope.launch { - uri?.let { - isImporting = true // 开始导入 - val result = withContext(Dispatchers.IO) { - customImportManager.importFrame(it) - } - isImporting = false // 导入完成 - importResult = if (result != null) { - refreshCustomContent() - "边框样式导入成功" - } else { - "边框样式导入失败" - } - } - } - } - - // 图片边框文件选择器 - val imageFrameLauncher = rememberLauncherForActivityResult( - contract = ActivityResultContracts.GetContent() - ) { uri: Uri? -> - scope.launch { - uri?.let { - isImporting = true // 开始导入 - val result = withContext(Dispatchers.IO) { - customImportManager.importImageFrame(it) - } - isImporting = false // 导入完成 - importResult = if (result != null) { - refreshCustomContent() - "图片边框导入成功" - } else { - "图片边框导入失败" - } - } - } - } - - Column(modifier = modifier.fillMaxWidth()) { - Text( - text = stringResource(R.string.settings_custom_import), - color = Color.White, - fontSize = 16.sp, - fontWeight = FontWeight.Normal, - modifier = Modifier.padding(bottom = 12.dp) - ) - - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.spacedBy(12.dp) - ) { - // 导入 LUT 按钮 - ImportButton( - text = stringResource(R.string.import_lut), - onClick = { lutLauncher.launch(arrayOf("*/*")) }, - enabled = !isImporting, // 导入时禁用 - modifier = Modifier.weight(1f) - ) - - // 导入边框样式按钮 - ImportButton( - text = stringResource(R.string.import_frame), - onClick = { imageFrameLauncher.launch("image/*") }, - enabled = !isImporting, - modifier = Modifier.weight(1f) - ) - } - - // 显示导入中状态 - if (isImporting) { - Spacer(modifier = Modifier.height(12.dp)) - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.Center, - verticalAlignment = Alignment.CenterVertically - ) { - CircularProgressIndicator( - modifier = Modifier.size(20.dp), - color = Color(0xFFFF6B35), - strokeWidth = 2.dp - ) - Spacer(modifier = Modifier.width(12.dp)) - Text( - text = importProgress?.let { (current, total) -> - "${stringResource(R.string.importing)} ($current/$total)" - } ?: stringResource(R.string.importing), - color = Color.White.copy(alpha = 0.8f), - fontSize = 13.sp - ) - } - } - - // 显示导入结果 - importResult?.let { result -> - Spacer(modifier = Modifier.height(8.dp)) - Text( - text = result, - color = if (result.contains("成功")) Color(0xFF4CAF50) else Color(0xFFFF5252), - fontSize = 12.sp, - modifier = Modifier.padding(top = 4.dp) - ) - LaunchedEffect(result) { - kotlinx.coroutines.delay(3000) - importResult = null - } - } - - Spacer(modifier = Modifier.height(12.dp)) - - Text( - text = stringResource(R.string.settings_import_description), - color = Color.White.copy(alpha = 0.6f), - fontSize = 13.sp - ) - - // 已导入内容管理 - if (customLuts.isNotEmpty() || customFrames.isNotEmpty()) { - Spacer(modifier = Modifier.height(20.dp)) - - Text( - text = stringResource(R.string.custom_content_management), - color = Color.White, - fontSize = 14.sp, - fontWeight = FontWeight.Medium, - modifier = Modifier.padding(bottom = 8.dp) - ) - - // 自定义 LUT 列表 - customLuts.forEach { lut -> - CustomContentItem( - name = lut.getName(), - type = stringResource(R.string.lut_type), - showEditButton = true, // LUT 支持编辑 - onEdit = { newName -> - scope.launch { - val success = withContext(Dispatchers.IO) { - customImportManager.updateLutName(lut.id, newName) - } - if (success) { - importResult = "已更新: $newName" - refreshCustomContent() - } else { - importResult = "更新失败" - } - } - }, - onDelete = { - scope.launch { - withContext(Dispatchers.IO) { - customImportManager.deleteCustomLut(lut.id) - } - importResult = "已删除: ${lut.getName()}" - refreshCustomContent() - } - } - ) - Spacer(modifier = Modifier.height(8.dp)) - } - - // 自定义边框列表 - customFrames.forEach { frame -> - CustomContentItem( - name = frame.name, - type = stringResource(R.string.frame_type), - onDelete = { - scope.launch { - withContext(Dispatchers.IO) { - customImportManager.deleteCustomFrame(frame.id) - } - importResult = "已删除: ${frame.name}" - refreshCustomContent() - } - } - ) - Spacer(modifier = Modifier.height(8.dp)) - } - } - } -} - -/** - * 自定义内容项 - */ -@Composable -private fun CustomContentItem( - name: String, - type: String, - onDelete: () -> Unit, - modifier: Modifier = Modifier, - showEditButton: Boolean = false, - onEdit: ((String) -> Unit)? = null -) { - var showDeleteDialog by remember { mutableStateOf(false) } - var showEditDialog by remember { mutableStateOf(false) } - var editedName by remember { mutableStateOf(name) } - - Row( - modifier = modifier - .fillMaxWidth() - .background( - Color.White.copy(alpha = 0.1f), - RoundedCornerShape(8.dp) - ) - .padding(horizontal = 12.dp, vertical = 10.dp), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.SpaceBetween - ) { - Column(modifier = Modifier.weight(1f)) { - Text( - text = name, - color = Color.White, - fontSize = 14.sp, - fontWeight = FontWeight.Medium - ) - Spacer(modifier = Modifier.height(2.dp)) - Text( - text = type, - color = Color.White.copy(alpha = 0.6f), - fontSize = 12.sp - ) - } - - Row( - horizontalArrangement = Arrangement.spacedBy(4.dp) - ) { - // 编辑按钮(仅当 showEditButton 为 true 时显示) - if (showEditButton && onEdit != null) { - IconButton( - onClick = { - editedName = name - showEditDialog = true - }, - modifier = Modifier.size(36.dp) - ) { - Icon( - imageVector = Icons.Default.Edit, - contentDescription = stringResource(R.string.edit), - tint = Color(0xFF4CAF50) - ) - } - } - - // 删除按钮 - IconButton( - onClick = { showDeleteDialog = true }, - modifier = Modifier.size(36.dp) - ) { - Icon( - imageVector = Icons.Default.Delete, - contentDescription = stringResource(R.string.delete), - tint = Color(0xFFFF5252) - ) - } - } - } - - // 编辑对话框 - if (showEditDialog) { - AlertDialog( - onDismissRequest = { showEditDialog = false }, - title = { - Text(text = stringResource(R.string.edit_name)) - }, - text = { - OutlinedTextField( - value = editedName, - onValueChange = { editedName = it }, - singleLine = true, - label = { Text(stringResource(R.string.name)) }, - modifier = Modifier.fillMaxWidth() - ) - }, - confirmButton = { - TextButton( - onClick = { - if (editedName.isNotBlank() && editedName != name) { - onEdit?.invoke(editedName) - } - showEditDialog = false - }, - enabled = editedName.isNotBlank() - ) { - Text( - text = stringResource(R.string.confirm), - color = Color(0xFF4CAF50) - ) - } - }, - dismissButton = { - TextButton(onClick = { showEditDialog = false }) { - Text(text = stringResource(R.string.cancel)) - } - } - ) - } - - // 删除确认对话框 - if (showDeleteDialog) { - AlertDialog( - onDismissRequest = { showDeleteDialog = false }, - title = { - Text(text = stringResource(R.string.delete_confirm_title)) - }, - text = { - Text(text = stringResource(R.string.delete_custom_content_confirm, name)) - }, - confirmButton = { - TextButton( - onClick = { - onDelete() - showDeleteDialog = false - } - ) { - Text( - text = stringResource(R.string.delete), - color = Color(0xFFFF5252) - ) - } - }, - dismissButton = { - TextButton(onClick = { showDeleteDialog = false }) { - Text(text = stringResource(R.string.cancel)) - } - } - ) - } -} - -/** - * 导入按钮 - */ -@Composable -private fun ImportButton( - text: String, - onClick: () -> Unit, - enabled: Boolean = true, // 添加 enabled 参数 - modifier: Modifier = Modifier -) { - Button( - onClick = onClick, - enabled = enabled, // 使用 enabled 状态 - modifier = modifier.height(48.dp), - shape = RoundedCornerShape(8.dp), - colors = ButtonDefaults.buttonColors( - containerColor = Color(0xFFFF6B35), - disabledContainerColor = Color(0xFFFF6B35).copy(alpha = 0.5f) // 禁用时半透明 - ) - ) { - Icon( - imageVector = Icons.Default.Add, - contentDescription = null, - tint = Color.White.copy(alpha = if (enabled) 1f else 0.5f), - modifier = Modifier.size(18.dp) - ) - Spacer(modifier = Modifier.width(4.dp)) - Text( - text = text, - color = Color.White.copy(alpha = if (enabled) 1f else 0.5f), - fontSize = 14.sp, - fontWeight = FontWeight.Medium - ) - } -} 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 3baba7de..3c2f9d97 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 @@ -33,6 +33,7 @@ import androidx.compose.ui.unit.sp import com.hinnka.mycamera.R import com.hinnka.mycamera.lut.LutCurve import com.hinnka.mycamera.lut.LutInfo +import com.hinnka.mycamera.raw.ColorSpace import com.hinnka.mycamera.ui.camera.autoRotate import com.hinnka.mycamera.ui.camera.LutEditBottomSheet import com.hinnka.mycamera.viewmodel.CameraViewModel @@ -780,7 +781,9 @@ fun FilterManagementScreen( // 导入时分类选择对话框 if (showImportCategoryDialog && pendingImportUris.isNotEmpty()) { + var selectedColorSpace by remember { mutableStateOf(ColorSpace.SRGB) } var selectedCurve by remember { mutableStateOf(LutCurve.SRGB) } + var showAdvancedOptions by remember { mutableStateOf(false) } AlertDialog( onDismissRequest = { //showImportCategoryDialog = false @@ -836,43 +839,117 @@ fun FilterManagementScreen( } } - var expanded by remember { mutableStateOf(false) } Spacer(modifier = Modifier.height(12.dp)) - - ExposedDropdownMenuBox( - expanded = expanded, - onExpandedChange = { expanded = !expanded }, - modifier = Modifier.fillMaxWidth() + + Row( + modifier = Modifier + .fillMaxWidth() + .clip(RoundedCornerShape(8.dp)) + .clickable { showAdvancedOptions = !showAdvancedOptions } + .padding(vertical = 8.dp), + verticalAlignment = Alignment.CenterVertically ) { - OutlinedTextField( - value = selectedCurve.name, - onValueChange = {}, - readOnly = true, - label = { Text(stringResource(R.string.input_curve)) }, - trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) }, - colors = ExposedDropdownMenuDefaults.outlinedTextFieldColors( - focusedBorderColor = Color(0xFFFF6B35), - focusedLabelColor = Color(0xFFFF6B35), - unfocusedLabelColor = Color.White.copy(alpha = 0.5f) - ), - modifier = Modifier.fillMaxWidth().menuAnchor() + Text( + text = stringResource(R.string.advanced_options), + fontSize = 14.sp, + color = Color.White.copy(alpha = 0.7f), + modifier = Modifier.weight(1f) ) - ExposedDropdownMenu( + Icon( + imageVector = if (showAdvancedOptions) Icons.Default.ExpandLess else Icons.Default.ExpandMore, + contentDescription = null, + tint = Color.White.copy(alpha = 0.7f) + ) + } + + if (showAdvancedOptions) { + var expanded by remember { mutableStateOf(false) } + Spacer(modifier = Modifier.height(8.dp)) + + ExposedDropdownMenuBox( expanded = expanded, - onDismissRequest = { expanded = false }, - modifier = Modifier.background(Color(0xFF2C2C2C)) + onExpandedChange = { expanded = !expanded }, + modifier = Modifier.fillMaxWidth() ) { - LutCurve.entries.forEach { curve -> - DropdownMenuItem( - text = { Text(curve.name) }, - onClick = { - selectedCurve = curve - expanded = false - }, - colors = MenuDefaults.itemColors( - textColor = if (selectedCurve == curve) Color(0xFFFF6B35) else Color.White + OutlinedTextField( + value = selectedCurve.name, + onValueChange = {}, + readOnly = true, + label = { Text(stringResource(R.string.input_curve)) }, + trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) }, + colors = ExposedDropdownMenuDefaults.outlinedTextFieldColors( + focusedBorderColor = Color(0xFFFF6B35), + focusedLabelColor = Color(0xFFFF6B35), + unfocusedLabelColor = Color.White.copy(alpha = 0.5f) + ), + modifier = Modifier.fillMaxWidth().menuAnchor() + ) + ExposedDropdownMenu( + expanded = expanded, + onDismissRequest = { expanded = false }, + modifier = Modifier.background(Color(0xFF2C2C2C)) + ) { + LutCurve.entries.forEach { curve -> + DropdownMenuItem( + text = { Text(curve.name) }, + onClick = { + selectedCurve = curve + expanded = false + }, + colors = MenuDefaults.itemColors( + textColor = if (selectedCurve == curve) Color( + 0xFFFF6B35 + ) else Color.White + ) ) - ) + } + } + } + + var colorSpaceExpanded by remember { mutableStateOf(false) } + Spacer(modifier = Modifier.height(12.dp)) + + ExposedDropdownMenuBox( + expanded = colorSpaceExpanded, + onExpandedChange = { colorSpaceExpanded = !colorSpaceExpanded }, + modifier = Modifier.fillMaxWidth() + ) { + OutlinedTextField( + value = selectedColorSpace.name, + onValueChange = {}, + readOnly = true, + label = { Text(stringResource(R.string.color_space)) }, + trailingIcon = { + ExposedDropdownMenuDefaults.TrailingIcon( + expanded = colorSpaceExpanded + ) + }, + colors = ExposedDropdownMenuDefaults.outlinedTextFieldColors( + focusedBorderColor = Color(0xFFFF6B35), + focusedLabelColor = Color(0xFFFF6B35), + unfocusedLabelColor = Color.White.copy(alpha = 0.5f) + ), + modifier = Modifier.fillMaxWidth().menuAnchor() + ) + ExposedDropdownMenu( + expanded = colorSpaceExpanded, + onDismissRequest = { colorSpaceExpanded = false }, + modifier = Modifier.background(Color(0xFF2C2C2C)) + ) { + ColorSpace.entries.forEach { colorSpace -> + DropdownMenuItem( + text = { Text(colorSpace.name) }, + onClick = { + selectedColorSpace = colorSpace + colorSpaceExpanded = false + }, + colors = MenuDefaults.itemColors( + textColor = if (selectedColorSpace == colorSpace) Color( + 0xFFFF6B35 + ) else Color.White + ) + ) + } } } } @@ -883,7 +960,8 @@ fun FilterManagementScreen( onClick = { val targetCategory = categoryText val urisToImport = pendingImportUris - val curveToUse = selectedCurve // Capture state + val curveToUse = selectedCurve + val colorSpace = selectedColorSpace showImportCategoryDialog = false pendingImportUris = emptyList() @@ -896,7 +974,7 @@ fun FilterManagementScreen( urisToImport.forEachIndexed { index, uri -> importProgress = Pair(index + 1, urisToImport.size) val result = withContext(Dispatchers.IO) { - customImportManager.importLut(uri, category = targetCategory, curve = curveToUse) + customImportManager.importLut(uri, category = targetCategory, colorSpace = colorSpace, curve = curveToUse) } if (result != null) { successCount++ diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index f0adbbcc..909cd1df 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -336,4 +336,6 @@ 在幻影模式下隐藏屏幕上的悬浮按钮。你仍可以通过快捷设置或应用快捷方式来控制。 启动系统相机 通过快捷设置或应用快捷方式开启幻影模式时,自动启动系统相机。 + 色彩空间 + 高级选项 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8f8a7754..963fec3a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -336,4 +336,6 @@ Hide the floating button in Phantom Mode. You can still control it via Quick Settings or App Shortcuts. Launch System Camera Automatically launch the system camera when enabling Phantom Mode via Quick Settings or App Shortcut. + Color Space + Advanced Options diff --git a/scripts/calc_matrices.py b/scripts/calc_matrices.py new file mode 100644 index 00000000..bb37ee07 --- /dev/null +++ b/scripts/calc_matrices.py @@ -0,0 +1,62 @@ + +import numpy as np + +def xy_to_XYZ(x, y): + return [x/y, 1.0, (1.0-x-y)/y] + +def get_xyz_matrix(r, g, b, w): + X = np.array([ + [r[0]/r[1], g[0]/g[1], b[0]/b[1]], + [1.0, 1.0, 1.0], + [(1.0-r[0]-r[1])/r[1], (1.0-g[0]-g[1])/g[1], (1.0-b[0]-b[1])/b[1]] + ]) + W = np.array(xy_to_XYZ(*w)) + S = np.linalg.solve(X, W) + return X * S + +def bradford_adaptation(src_wp, dst_wp): + if src_wp == dst_wp: + return np.eye(3) + + M_cat02 = np.array([ + [ 0.7328, 0.4296, -0.1624], + [-0.7036, 1.6975, 0.0061], + [ 0.0030, 0.0136, 0.9834] + ]) + + src_XYZ = np.array(xy_to_XYZ(*src_wp)) + dst_XYZ = np.array(xy_to_XYZ(*dst_wp)) + + src_LMS = M_cat02 @ src_XYZ + dst_LMS = M_cat02 @ dst_XYZ + + LMS_ratio = dst_LMS / src_LMS + M_adapt = np.diag(LMS_ratio) + + return np.linalg.inv(M_cat02) @ M_adapt @ M_cat02 + +spaces = [ + # (Name, r, g, b, w) + ("SRGB", (0.640, 0.330), (0.300, 0.600), (0.150, 0.060), (0.3127, 0.3290)), + ("DCI_P3", (0.680, 0.320), (0.265, 0.690), (0.150, 0.060), (0.314, 0.351)), + ("BT2020", (0.708, 0.292), (0.170, 0.797), (0.131, 0.046), (0.3127, 0.3290)), + ("ARRI4", (0.7347, 0.2653), (0.1424, 0.8576), (0.0991, -0.0308), (0.3127, 0.3290)), + ("AppleLog2", (0.725, 0.301), (0.221, 0.814), (0.068, -0.076), (0.3127, 0.3290)), + ("S_GAMUT3_CINE", (0.766, 0.275), (0.225, 0.8), (0.089, -0.087), (0.3127, 0.3290)), + ("ACES_AP1", (0.713, 0.293), (0.165, 0.83), (0.128, 0.044), (0.32168, 0.33767)), +] + +M_srgb_xyz = get_xyz_matrix(spaces[0][1], spaces[0][2], spaces[0][3], spaces[0][4]) + +for i in range(1, len(spaces)): + name, r, g, b, w = spaces[i] + M_target_xyz = get_xyz_matrix(r, g, b, w) + M_adapt = bradford_adaptation(spaces[0][4], w) + + # Forward: SRGB_Lin -> Target_Lin + M_fwd = np.linalg.inv(M_target_xyz) @ M_adapt @ M_srgb_xyz + + print(f"---{name} (Ordinal {i})---") + entries = M_fwd.flatten(order='F') + for e in entries: + print(f"{e:.6f}")