diff --git a/TODO.md b/TODO.md index 72fafe7a..b9da93e8 100644 --- a/TODO.md +++ b/TODO.md @@ -49,4 +49,15 @@ # Crashes * Pixel 7 Android 16 - 拍照后预览黑屏 -* Pixel 7 Android 16 - 切换摄像头闪退 \ No newline at end of file +* Pixel 7 Android 16 - 切换摄像头闪退 + + +if (warmMask > 0.0) { +// 6.2.1 "去脏":只在一定范围内应用,避免把鲜艳的红色变黑 +color.b = mix(color.b, color.b * 0.85, warmMask * strength); +// 6.2.2 密度调整 +color.g = mix(color.g, color.g * 0.95, warmMask * strength); +// 6.2.3 胶片感增强:使用非线性缩放而不是简单的乘法,保护亮度 +vec3 sCurve = color * color * (3.0 - 2.0 * color); +color = mix(color, sCurve, warmMask * strength * 0.2); +} \ No newline at end of file diff --git a/app/src/main/java/com/hinnka/mycamera/gallery/PhotoManager.kt b/app/src/main/java/com/hinnka/mycamera/gallery/PhotoManager.kt index 90bd27c6..1a5c9178 100644 --- a/app/src/main/java/com/hinnka/mycamera/gallery/PhotoManager.kt +++ b/app/src/main/java/com/hinnka/mycamera/gallery/PhotoManager.kt @@ -250,14 +250,15 @@ object PhotoManager { generateThumbnail(thumbnail, thumbnailFile) } - // 计算处理后的尺寸 - val dimensions = BitmapUtils.calculateProcessedDimensions(width, height, aspectRatio, rotation) - val finalWidth = dimensions.first - val finalHeight = dimensions.second + val cropRegion = captureResult.get(CaptureResult.SCALER_CROP_REGION) + // 根据图像格式处理 when (format) { ImageFormat.YUV_420_888, ImageFormat.YCBCR_P010, ImageFormat.NV21 -> { + val dimensions = BitmapUtils.calculateProcessedDimensions(width, height, aspectRatio, null, rotation) + val finalWidth = dimensions.first + val finalHeight = dimensions.second processingScope.launch { withContext(Dispatchers.IO) { photoFile.createNewFile() @@ -286,13 +287,16 @@ object PhotoManager { previewBitmap.compress(Bitmap.CompressFormat.JPEG, 95, outputStream) } if (shouldAutoSave) { - exportPhoto(context, photoId, photoProcessor, metadata, sharpeningValue, noiseReductionValue, chromaNoiseReductionValue) + exportPhoto(context, photoId, photoProcessor, metadataWithInfo, sharpeningValue, noiseReductionValue, chromaNoiseReductionValue) } } } } } ImageFormat.RAW_SENSOR, ImageFormat.RAW10, ImageFormat.RAW12 -> { + val dimensions = BitmapUtils.calculateProcessedDimensions(width, height, aspectRatio, cropRegion, rotation) + val finalWidth = dimensions.first + val finalHeight = dimensions.second processingScope.launch { withContext(Dispatchers.IO) { photoFile.createNewFile() @@ -310,9 +314,10 @@ object PhotoManager { withContext(Dispatchers.IO) { // 保存元数据 val metadataWithInfo = metadata.copy( - width = width, - height = height, + width = finalWidth, + height = finalHeight, ratio = aspectRatio, + cropRegion = cropRegion, sharpening = 0.2f, noiseReduction = 0f, chromaNoiseReduction = 0.25f, @@ -322,8 +327,8 @@ object PhotoManager { RawProcessor.saveToDng(image, characteristics, captureResult, it, rotation) } if (shouldAutoSave) { - exportDng(context, image, metadata, characteristics, captureResult, rotation) - exportPhoto(context, photoId, photoProcessor, metadata, sharpeningValue, noiseReductionValue, chromaNoiseReductionValue) + exportDng(context, image, metadataWithInfo, characteristics, captureResult, rotation) + exportPhoto(context, photoId, photoProcessor, metadataWithInfo, sharpeningValue, noiseReductionValue, chromaNoiseReductionValue) } } } 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 c6537551..765a149a 100644 --- a/app/src/main/java/com/hinnka/mycamera/gallery/PhotoMetadata.kt +++ b/app/src/main/java/com/hinnka/mycamera/gallery/PhotoMetadata.kt @@ -1,6 +1,7 @@ package com.hinnka.mycamera.gallery import android.content.Context +import android.graphics.Rect import android.net.Uri import android.os.Build import androidx.exifinterface.media.ExifInterface @@ -32,6 +33,7 @@ data class PhotoMetadata( val width: Int = 0, val height: Int = 0, val ratio: AspectRatio? = null, + val cropRegion: Rect? = null, // 拍摄信息 val deviceModel: String? = null, val brand: String? = null, @@ -123,6 +125,16 @@ data class PhotoMetadata( put("width", width) put("height", height) put("ratio", ratio?.getDisplayName() ?: JSONObject.NULL) + put("cropRegion", if (cropRegion != null) { + JSONObject().apply { + put("left", cropRegion.left) + put("top", cropRegion.top) + put("right", cropRegion.right) + put("bottom", cropRegion.bottom) + } + } else { + JSONObject.NULL + }) // 拍摄信息 put("deviceModel", deviceModel ?: JSONObject.NULL) put("brand", brand ?: JSONObject.NULL) @@ -194,6 +206,15 @@ data class PhotoMetadata( width = obj.optInt("width", 0), height = obj.optInt("height", 0), ratio = if (obj.isNull("ratio")) null else AspectRatio.fromString(obj.optString("ratio")), + cropRegion = if (obj.isNull("cropRegion")) null else { + val cropObj = obj.getJSONObject("cropRegion") + Rect( + cropObj.optInt("left", 0), + cropObj.optInt("top", 0), + cropObj.optInt("right", 0), + cropObj.optInt("bottom", 0) + ) + }, // 拍摄信息 deviceModel = if (obj.isNull("deviceModel")) null else obj.optString("deviceModel"), brand = if (obj.isNull("brand")) null else obj.optString("brand"), diff --git a/app/src/main/java/com/hinnka/mycamera/gallery/PhotoProcessor.kt b/app/src/main/java/com/hinnka/mycamera/gallery/PhotoProcessor.kt index 376d197d..c9af0f36 100644 --- a/app/src/main/java/com/hinnka/mycamera/gallery/PhotoProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/gallery/PhotoProcessor.kt @@ -93,9 +93,11 @@ class PhotoProcessor( // 1. 应用 LUT val lutConfig = metadata.lutId?.let { lutManager.loadLut(it) } val colorRecipeParams = metadata.lutId?.let { lutManager.loadColorRecipeParams(it) } + val cropRegion = metadata.cropRegion val lutResult = RawDemosaicProcessor.getInstance().process( dngPath, - metadata.ratio ?: AspectRatio.RATIO_4_3, lutConfig, colorRecipeParams, + metadata.ratio ?: AspectRatio.RATIO_4_3, cropRegion, + lutConfig, colorRecipeParams, finalSharpening, finalNoiseReduction, finalChromaNoiseReduction) result = lutResult 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 0e521eec..cb994670 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt @@ -986,14 +986,14 @@ class LutImageProcessor { float baseBlue = color.b - (color.r + color.g) * 0.5; float blueMask = smoothstep(0.0, 0.2, baseBlue); if (blueMask > 0.0) { - // 增加蓝色的纯度(减去R和G的干扰) - vec3 blueDensity = vec3(0.3, 0.3, 0.0) * blueMask * strength; - color.r -= blueDensity.r * color.r; - color.g -= blueDensity.g * color.g; - // 稍微压暗蓝色,制造胶片重感 - color.b -= 0.05 * blueMask * strength; + // 增加蓝色的纯度 (使用比例混合,避免绝对值减法产生噪点) + color.r = mix(color.r, color.r * 0.7, blueMask * strength); + color.g = mix(color.g, color.g * 0.7, blueMask * strength); + // 稍微压暗蓝色,制造胶片重感 (同样使用比例混合) + color.b = mix(color.b, color.b * 0.95, blueMask * strength); // 使用 S 曲线增加蓝色区域的对比度/通透感 - color.rgb = mix(color.rgb, color.rgb * color.rgb * (3.0 - 2.0 * color.rgb), blueMask * strength * 0.2); + vec3 sCurve = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb); + color.rgb = mix(color.rgb, sCurve, blueMask * strength * 0.2); } // --- 6.2 暖色增强 (新增逻辑:红润肤色/日落) --- // 去除浑浊的蓝色杂质,呈现奶油般质感的红/橙色 @@ -1001,14 +1001,13 @@ class LutImageProcessor { float baseWarm = color.r - (color.g * 0.3 + color.b * 0.7); float warmMask = smoothstep(0.05, 0.25, baseWarm); if (warmMask > 0.0) { - // 6.2.1 "去脏":在暖色区域减去互补色(蓝色),使暖色更干净、通透 - color.b -= 0.15 * warmMask * strength; - // 6.2.2 密度调整:轻微减去绿色,会让黄色向橙/红色偏移 - // 如果想要更黄的暖色,可以注释掉下面这行 - color.g -= 0.05 * warmMask * strength; - // 6.2.3 胶片感增强:同样使用 S 曲线混合,增加暖色的"厚度"和饱和度 - // 这里的 mix 系数比蓝色稍高,因为人眼对肤色对比度更敏感 - color.rgb = mix(color.rgb, color.rgb * color.rgb * (3.0 - 2.0 * color.rgb), warmMask * strength * 0.25); + // 6.2.1 "去脏":只在一定范围内应用,避免把鲜艳的红色变黑 + color.b = mix(color.b, color.b * 0.85, warmMask * strength); + // 6.2.2 密度调整 + color.g = mix(color.g, color.g * 0.95, warmMask * strength); + // 6.2.3 胶片感增强:使用非线性缩放而不是简单的乘法,保护亮度 + vec3 sCurve = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb); + color.rgb = mix(color.rgb, sCurve, warmMask * strength * 0.25); } // 7. 褪色效果 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 91be11fd..65497bec 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/Shaders.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/Shaders.kt @@ -227,14 +227,14 @@ object Shaders { float baseBlue = color.b - (color.r + color.g) * 0.5; float blueMask = smoothstep(0.0, 0.2, baseBlue); if (blueMask > 0.0) { - // 增加蓝色的纯度(减去R和G的干扰) - vec3 blueDensity = vec3(0.3, 0.3, 0.0) * blueMask * strength; - color.r -= blueDensity.r * color.r; - color.g -= blueDensity.g * color.g; - // 稍微压暗蓝色,制造胶片重感 - color.b -= 0.05 * blueMask * strength; + // 增加蓝色的纯度 (使用比例混合,避免绝对值减法产生噪点) + color.r = mix(color.r, color.r * 0.7, blueMask * strength); + color.g = mix(color.g, color.g * 0.7, blueMask * strength); + // 稍微压暗蓝色,制造胶片重感 (同样使用比例混合) + color.b = mix(color.b, color.b * 0.95, blueMask * strength); // 使用 S 曲线增加蓝色区域的对比度/通透感 - color.rgb = mix(color.rgb, color.rgb * color.rgb * (3.0 - 2.0 * color.rgb), blueMask * strength * 0.2); + vec3 sCurve = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb); + color.rgb = mix(color.rgb, sCurve, blueMask * strength * 0.2); } // --- 6.2 暖色增强 (新增逻辑:红润肤色/日落) --- // 去除浑浊的蓝色杂质,呈现奶油般质感的红/橙色 @@ -242,14 +242,13 @@ object Shaders { float baseWarm = color.r - (color.g * 0.3 + color.b * 0.7); float warmMask = smoothstep(0.05, 0.25, baseWarm); if (warmMask > 0.0) { - // 6.2.1 "去脏":在暖色区域减去互补色(蓝色),使暖色更干净、通透 - color.b -= 0.15 * warmMask * strength; - // 6.2.2 密度调整:轻微减去绿色,会让黄色向橙/红色偏移 - // 如果想要更黄的暖色,可以注释掉下面这行 - color.g -= 0.05 * warmMask * strength; - // 6.2.3 胶片感增强:同样使用 S 曲线混合,增加暖色的"厚度"和饱和度 - // 这里的 mix 系数比蓝色稍高,因为人眼对肤色对比度更敏感 - color.rgb = mix(color.rgb, color.rgb * color.rgb * (3.0 - 2.0 * color.rgb), warmMask * strength * 0.25); + // 6.2.1 "去脏":只在一定范围内应用,避免把鲜艳的红色变黑 + color.b = mix(color.b, color.b * 0.85, warmMask * strength); + // 6.2.2 密度调整 + color.g = mix(color.g, color.g * 0.95, warmMask * strength); + // 6.2.3 胶片感增强:使用非线性缩放而不是简单的乘法,保护亮度 + vec3 sCurve = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb); + color.rgb = mix(color.rgb, sCurve, warmMask * strength * 0.25); } // 7. 褪色效果(降低对比度 + 提升黑电平) diff --git a/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt b/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt index dff3d779..2a7c451f 100644 --- a/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt @@ -1,6 +1,7 @@ package com.hinnka.mycamera.raw import android.graphics.Bitmap +import android.graphics.Rect import android.hardware.camera2.CameraCharacteristics import android.hardware.camera2.CaptureResult import android.media.Image @@ -361,21 +362,32 @@ class RawDemosaicProcessor { // 6. 色彩增强(Vibrance) float strength = uVibrance * 0.5; + // --- 6.1 蓝色增强 (深邃天空/水面) --- float baseBlue = color.b - (color.r + color.g) * 0.5; - float blueMask = smoothstep(0.0, 0.2, baseBlue); + float blueMask = smoothstep(0.0, 0.2, baseBlue); if (blueMask > 0.0) { - vec3 blueDensity = vec3(0.3, 0.3, 0.0) * blueMask * strength; - color.r -= blueDensity.r * color.r; - color.g -= blueDensity.g * color.g; - color.b -= 0.05 * blueMask * strength; - color.rgb = mix(color.rgb, color.rgb * color.rgb * (3.0 - 2.0 * color.rgb), blueMask * strength * 0.2); + // 增加蓝色的纯度 (使用比例混合,避免绝对值减法产生噪点) + color.r = mix(color.r, color.r * 0.7, blueMask * strength); + color.g = mix(color.g, color.g * 0.7, blueMask * strength); + // 稍微压暗蓝色,制造胶片重感 (同样使用比例混合) + color.b = mix(color.b, color.b * 0.95, blueMask * strength); + // 使用 S 曲线增加蓝色区域的对比度/通透感 + vec3 sCurve = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb); + color.rgb = mix(color.rgb, sCurve, blueMask * strength * 0.2); } - float baseWarm = color.r - (color.g * 0.3 + color.b * 0.7); + // --- 6.2 暖色增强 (新增逻辑:红润肤色/日落) --- + // 去除浑浊的蓝色杂质,呈现奶油般质感的红/橙色 + // 算法:检测红色分量是否显著高于蓝色 (捕捉皮肤、夕阳、木头等) + float baseWarm = color.r - (color.g * 0.3 + color.b * 0.7); float warmMask = smoothstep(0.05, 0.25, baseWarm); if (warmMask > 0.0) { - color.b -= 0.15 * warmMask * strength; - color.g -= 0.05 * warmMask * strength; - color.rgb = mix(color.rgb, color.rgb * color.rgb * (3.0 - 2.0 * color.rgb), warmMask * strength * 0.25); + // 6.2.1 "去脏":只在一定范围内应用,避免把鲜艳的红色变黑 + color.b = mix(color.b, color.b * 0.85, warmMask * strength); + // 6.2.2 密度调整 + color.g = mix(color.g, color.g * 0.95, warmMask * strength); + // 6.2.3 胶片感增强:使用非线性缩放而不是简单的乘法,保护亮度 + vec3 sCurve = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb); + color.rgb = mix(color.rgb, sCurve, warmMask * strength * 0.25); } // 7. 褪色效果 @@ -559,6 +571,7 @@ class RawDemosaicProcessor { * * @param dngFilePath DNG 文件路径 * @param aspectRatio 目标宽高比 + * @param cropRegion 可选裁切区域(在 RAW 纹理空间) * @param lutConfig LUT 配置(可选) * @param colorRecipeParams 色彩配方参数(可选) * @param sharpeningValue 锐化强度 (0.0-1.0) @@ -569,6 +582,7 @@ class RawDemosaicProcessor { suspend fun process( dngFilePath: String, aspectRatio: AspectRatio, + cropRegion: Rect?, lutConfig: LutConfig? = null, colorRecipeParams: ColorRecipeParams? = null, sharpeningValue: Float = 0f, @@ -600,6 +614,7 @@ class RawDemosaicProcessor { rowStride = it.rowStride, metadata = it.metadata, aspectRatio = aspectRatio, + cropRegion = cropRegion, rotation = it.rotation, // 使用从 DNG 文件读取的 rotation lutConfig = lutConfig, colorRecipeParams = colorRecipeParams, @@ -654,6 +669,7 @@ class RawDemosaicProcessor { // 提取元数据 val metadata = RawMetadata.create(width, height, characteristics, captureResult) + val cropRegion = captureResult.get(CaptureResult.SCALER_CROP_REGION) // 使用内部处理方法 processInternal( @@ -663,6 +679,7 @@ class RawDemosaicProcessor { rowStride = rawImage.planes[0].rowStride, metadata = metadata, aspectRatio = aspectRatio, + cropRegion = cropRegion, rotation = rotation, lutConfig = lutConfig, colorRecipeParams = colorRecipeParams, @@ -686,6 +703,7 @@ class RawDemosaicProcessor { rowStride: Int, metadata: RawMetadata, aspectRatio: AspectRatio, + cropRegion: Rect?, rotation: Int, lutConfig: LutConfig?, colorRecipeParams: ColorRecipeParams?, @@ -715,28 +733,27 @@ class RawDemosaicProcessor { uploadRawTextureFromBuffer(rawData, width, height, rowStride) PLog.d(TAG, "Texture upload took: ${System.currentTimeMillis() - uploadStart}ms") - // 1. 计算裁切后的尺寸 - // 关键:直接使用 targetRatio 在原始纹理空间裁切,然后根据旋转交换最终尺寸 - val isSwapped = rotation == 90 || rotation == 270 - val srcRatio = width.toFloat() / height.toFloat() + // 1. 根据 cropRegion 和 aspectRatio 计算裁切后的尺寸 + val baseWidth = cropRegion?.width() ?: width + val baseHeight = cropRegion?.height() ?: height + val srcRatio = baseWidth.toFloat() / baseHeight.toFloat() val targetRatio = aspectRatio.getValue(true) // 使用横向比例,因为 RAW 纹理始终是横向的 - // 在原始空间计算裁切后的尺寸(不翻转 targetRatio) - val croppedWidth: Int - val croppedHeight: Int + var finalCropWidth = baseWidth.toFloat() + var finalCropHeight = baseHeight.toFloat() + if (srcRatio > targetRatio) { - // 原图更宽,水平方向裁切 - croppedHeight = height - croppedWidth = (height * targetRatio).toInt() + // 基础区域更宽,水平方向裁切 + finalCropWidth = baseHeight * targetRatio } else { - // 原图更高,垂直方向裁切 - croppedWidth = width - croppedHeight = (width / targetRatio).toInt() + // 基础区域更高,垂直方向裁切 + finalCropHeight = baseWidth / targetRatio } // 旋转后的最终输出尺寸 - val finalWidth = if (isSwapped) croppedHeight else croppedWidth - val finalHeight = if (isSwapped) croppedWidth else croppedHeight + val isSwapped = rotation == 90 || rotation == 270 + val finalWidth = if (isSwapped) finalCropHeight.toInt() else finalCropWidth.toInt() + val finalHeight = if (isSwapped) finalCropWidth.toInt() else finalCropHeight.toInt() // 3. 曝光增益计算 var exposureGain = 0f @@ -779,7 +796,7 @@ class RawDemosaicProcessor { // 6. 第三步:缩放、旋转、裁剪并输出 (Output Pass) setupOutputFramebuffer(finalWidth, finalHeight) val outputStart = System.currentTimeMillis() - renderOutputPass(metadata, rotation, aspectRatio, finalWidth, finalHeight, sourceTextureForOutput) + renderOutputPass(metadata, rotation, aspectRatio, cropRegion, finalWidth, finalHeight, sourceTextureForOutput) PLog.d(TAG, "Output Pass took: ${System.currentTimeMillis() - outputStart}ms") // 7. 读取结果 @@ -1556,6 +1573,7 @@ class RawDemosaicProcessor { metadata: RawMetadata, rotation: Int, aspectRatio: AspectRatio, + cropRegion: Rect?, finalWidth: Int, finalHeight: Int, sourceTextureId: Int @@ -1596,22 +1614,36 @@ class RawDemosaicProcessor { GlMatrix.translateM(texMatrix, 0, -0.5f, -0.5f, 0f) // === 第二步:裁切变换(实际先于旋转执行)=== - // 在原始纹理空间(横向)进行裁切 - // 直接使用 targetRatio,不需要翻转 - val srcRatio = metadata.width.toFloat() / metadata.height.toFloat() - val targetRatio = aspectRatio.getValue(true) // 使用横向比例,因为 RAW 纹理始终是横向的 + // 处理 cropRegion 和 aspectRatio 双重裁切 + val baseLeft = cropRegion?.left ?: 0 + val baseTop = cropRegion?.top ?: 0 + val baseWidth = cropRegion?.width() ?: metadata.width + val baseHeight = cropRegion?.height() ?: metadata.height + + val srcRatio = baseWidth.toFloat() / baseHeight.toFloat() + val targetRatio = aspectRatio.getValue(true) + + var finalCropWidth = baseWidth.toFloat() + var finalCropHeight = baseHeight.toFloat() - var scaleX = 1.0f - var scaleY = 1.0f if (srcRatio > targetRatio) { - // 原图更宽,水平方向缩放裁切 - scaleX = targetRatio / srcRatio + finalCropWidth = baseHeight * targetRatio } else { - // 原图更高,垂直方向缩放裁切 - scaleY = srcRatio / targetRatio + finalCropHeight = baseWidth / targetRatio } - GlMatrix.translateM(texMatrix, 0, 0.5f, 0.5f, 0f) + val finalCropLeft = baseLeft + (baseWidth - finalCropWidth) / 2f + val finalCropTop = baseTop + (baseHeight - finalCropHeight) / 2f + + // 计算归一化的中心点和缩放比例 + val scaleX = finalCropWidth / metadata.width + val scaleY = finalCropHeight / metadata.height + val centerX = (finalCropLeft + finalCropWidth / 2f) / metadata.width + val centerY = (finalCropTop + finalCropHeight / 2f) / metadata.height + + // 应用变换矩阵 + // 注意:这里的平移是移动到裁切区域的中心 + GlMatrix.translateM(texMatrix, 0, centerX, centerY, 0f) GlMatrix.scaleM(texMatrix, 0, scaleX, scaleY, 1.0f) GlMatrix.translateM(texMatrix, 0, -0.5f, -0.5f, 0f) diff --git a/app/src/main/java/com/hinnka/mycamera/raw/RawShaders.kt b/app/src/main/java/com/hinnka/mycamera/raw/RawShaders.kt index 5d8a0ca5..b6160986 100644 --- a/app/src/main/java/com/hinnka/mycamera/raw/RawShaders.kt +++ b/app/src/main/java/com/hinnka/mycamera/raw/RawShaders.kt @@ -309,8 +309,8 @@ object RawShaders { // 中值钳位:限制在邻居的 [min, max] 范围内 (适当放宽范围以减少边缘断层) avgDiff = clamp(avgDiff, minDiff - 0.02, maxDiff + 0.02); - // 收紧色差范围:在 Raw 域超过 ±0.15 往往是噪点 - avgDiff = clamp(avgDiff, -0.15, 0.15); + // 收紧色差范围 + avgDiff = clamp(avgDiff, -0.2, 0.2); b = g + avgDiff; @@ -354,7 +354,7 @@ object RawShaders { float avgDiff = diffSum / (weightSum + epsilon); // 中值钳位:限制在邻居的 [min, max] 范围内 (适当放宽范围以减少边缘断层) avgDiff = clamp(avgDiff, minDiff - 0.02, maxDiff + 0.02); - avgDiff = clamp(avgDiff, -0.15, 0.15); + avgDiff = clamp(avgDiff, -0.2, 0.2); r = g + avgDiff; @@ -378,7 +378,7 @@ object RawShaders { float w_right = computeBilateralWeight(g, gEst_right, 1.0); float rDiff = (diff_left * w_left + diff_right * w_right) / (w_left + w_right + epsilon); - rDiff = clamp(rDiff, -0.15, 0.15); + rDiff = clamp(rDiff, -0.2, 0.2); r = g + rDiff; // B 通道 (垂直方向) @@ -394,7 +394,7 @@ object RawShaders { float w_bottom = computeBilateralWeight(g, gEst_bottom, 1.0); float bDiff = (diff_top * w_top + diff_bottom * w_bottom) / (w_top + w_bottom + epsilon); - bDiff = clamp(bDiff, -0.15, 0.15); + bDiff = clamp(bDiff, -0.2, 0.2); b = g + bDiff; } else { @@ -413,7 +413,7 @@ object RawShaders { float w_bottom = computeBilateralWeight(g, gEst_bottom, 1.0); float rDiff = (diff_top * w_top + diff_bottom * w_bottom) / (w_top + w_bottom + epsilon); - rDiff = clamp(rDiff, -0.15, 0.15); + rDiff = clamp(rDiff, -0.2, 0.2); r = g + rDiff; // B 通道 (水平方向) @@ -429,7 +429,7 @@ object RawShaders { float w_right = computeBilateralWeight(g, gEst_right, 1.0); float bDiff = (diff_left * w_left + diff_right * w_right) / (w_left + w_right + epsilon); - bDiff = clamp(bDiff, -0.15, 0.15); + bDiff = clamp(bDiff, -0.2, 0.2); b = g + bDiff; } } @@ -620,11 +620,12 @@ object RawShaders { // C. 【通透美白】: 提升亮度 (Luma Boost) // 这是“冷白皮”的关键。非线性提亮,保护高光不溢出。 - float lumaBoost = 0.08; + float lumaBoost = 0.15; // 使用 pow 曲线保护高光,或者简单的加法 // 这里使用加法但在高光处衰减 - float highlightProtect = 1.0 - smoothstep(0.7, 1.0, hsl.z); + float highlightProtect = 1.0 - smoothstep(0.8, 1.0, hsl.z); hsl.z += lumaBoost * isSkin * highlightProtect; + hsl.z = max(hsl.z, 0.0); // ------------------------------------------ @@ -647,14 +648,14 @@ object RawShaders { float baseBlue = color.b - (color.r + color.g) * 0.5; float blueMask = smoothstep(0.0, 0.2, baseBlue); if (blueMask > 0.0) { - // 增加蓝色的纯度(减去R和G的干扰) - vec3 blueDensity = vec3(0.3, 0.3, 0.0) * blueMask * strength; - color.r -= blueDensity.r * color.r; - color.g -= blueDensity.g * color.g; - // 稍微压暗蓝色,制造胶片重感 - color.b -= 0.05 * blueMask * strength; + // 增加蓝色的纯度 (使用比例混合,避免绝对值减法产生噪点) + color.r = mix(color.r, color.r * 0.7, blueMask * strength); + color.g = mix(color.g, color.g * 0.7, blueMask * strength); + // 稍微压暗蓝色,制造胶片重感 (同样使用比例混合) + color.b = mix(color.b, color.b * 0.95, blueMask * strength); // 使用 S 曲线增加蓝色区域的对比度/通透感 - color = mix(color, color * color * (3.0 - 2.0 * color), blueMask * strength * 0.2); + vec3 sCurve = color * color * (3.0 - 2.0 * color); + color = mix(color, sCurve, blueMask * strength * 0.2); } // --- 6.2 暖色增强 (新增逻辑:红润肤色/日落) --- // 去除浑浊的蓝色杂质,呈现奶油般质感的红/橙色 @@ -662,14 +663,13 @@ object RawShaders { float baseWarm = color.r - (color.g * 0.3 + color.b * 0.7); float warmMask = smoothstep(0.05, 0.25, baseWarm); if (warmMask > 0.0) { - // 6.2.1 "去脏":在暖色区域减去互补色(蓝色),使暖色更干净、通透 - color.b -= 0.15 * warmMask * strength; - // 6.2.2 密度调整:轻微减去绿色,会让黄色向橙/红色偏移 - // 如果想要更黄的暖色,可以注释掉下面这行 - color.g -= 0.05 * warmMask * strength; - // 6.2.3 胶片感增强:同样使用 S 曲线混合,增加暖色的"厚度"和饱和度 - // 这里的 mix 系数比蓝色稍高,因为人眼对肤色对比度更敏感 - color = mix(color, color * color * (3.0 - 2.0 * color), warmMask * strength * 0.25); + // 6.2.1 "去脏":只在一定范围内应用,避免把鲜艳的红色变黑 + color.b = mix(color.b, color.b * 0.85, warmMask * strength); + // 6.2.2 密度调整 + color.g = mix(color.g, color.g * 0.95, warmMask * strength); + // 6.2.3 胶片感增强:使用非线性缩放而不是简单的乘法,保护亮度 + vec3 sCurve = color * color * (3.0 - 2.0 * color); + color = mix(color, sCurve, warmMask * strength * 0.25); } return color; } diff --git a/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoDetailScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoDetailScreen.kt index 2f0f5f05..fde585f9 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoDetailScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoDetailScreen.kt @@ -367,7 +367,7 @@ fun PhotoDetailScreen( Column { InfoRow(stringResource(R.string.photo_info_date), currentPhoto.getFormattedDate()) InfoRow(stringResource(R.string.photo_info_resolution), currentPhoto.getResolution()) - InfoRow(stringResource(R.string.photo_info_size), currentPhoto.getFormattedSize()) +// InfoRow(stringResource(R.string.photo_info_size), currentPhoto.getFormattedSize()) currentPhoto.metadata?.let { InfoRow(stringResource(R.string.photo_info_focal_length), it.focalLength35mm ?: "N/A") InfoRow(stringResource(R.string.photo_info_aperture), it.aperture ?: "N/A") diff --git a/app/src/main/java/com/hinnka/mycamera/utils/BitmapUtils.kt b/app/src/main/java/com/hinnka/mycamera/utils/BitmapUtils.kt index 271e71f7..440c8c49 100644 --- a/app/src/main/java/com/hinnka/mycamera/utils/BitmapUtils.kt +++ b/app/src/main/java/com/hinnka/mycamera/utils/BitmapUtils.kt @@ -166,6 +166,7 @@ object BitmapUtils { * @param width 原始宽度 * @param height 原始高度 * @param aspectRatio 目标宽高比 + * @param cropRect 裁切区域 (可选) * @param rotation 旋转角度 (0, 90, 180, 270) * @return Pair<宽度, 高度> 处理后的尺寸 */ @@ -173,47 +174,34 @@ object BitmapUtils { width: Int, height: Int, aspectRatio: AspectRatio, + cropRect: Rect?, rotation: Int ): Pair { - // Step 1: 计算旋转后的尺寸 - val rotatedWidth: Int - val rotatedHeight: Int + // Step 1: 以 cropRect 或原始尺寸作为基础尺寸 + val baseWidth = cropRect?.width() ?: width + val baseHeight = cropRect?.height() ?: height - when (rotation) { - 90, 270 -> { - // 旋转 90 或 270 度,宽高互换 - rotatedWidth = height - rotatedHeight = width - } - else -> { - // 旋转 0 或 180 度,宽高不变 - rotatedWidth = width - rotatedHeight = height - } - } + // Step 2: 在原始空间(横向)计算裁切后的尺寸 + // 逻辑与 RawDemosaicProcessor.processInternal 保持一致 + val srcRatio = baseWidth.toFloat() / baseHeight.toFloat() + val targetRatio = aspectRatio.getValue(true) // 使用横向比例进行计算 - // Step 2: 计算裁切后的尺寸 - val srcRatio = rotatedWidth.toFloat() / rotatedHeight.toFloat() - val targetRatio = aspectRatio.getValue(false) // width/height - - // 如果宽高比已经匹配,直接返回旋转后的尺寸 - if (kotlin.math.abs(srcRatio - targetRatio) < 0.01f) { - return Pair(rotatedWidth, rotatedHeight) - } - - val finalWidth: Int - val finalHeight: Int + var finalCropWidth = baseWidth.toFloat() + var finalCropHeight = baseHeight.toFloat() if (srcRatio > targetRatio) { - // 原图更宽,裁切左右,高度不变 - finalHeight = rotatedHeight - finalWidth = (rotatedHeight * targetRatio).toInt() - } else { - // 原图更高,裁切上下,宽度不变 - finalWidth = rotatedWidth - finalHeight = (rotatedWidth / targetRatio).toInt() + // 基础区域更宽,水平方向裁切 + finalCropWidth = baseHeight * targetRatio + } else if (srcRatio < targetRatio) { + // 基础区域更高,垂直方向裁切 + finalCropHeight = baseWidth / targetRatio } + // Step 3: 根据旋转角度确定最终宽高 + val isSwapped = rotation == 90 || rotation == 270 + val finalWidth = if (isSwapped) finalCropHeight.toInt() else finalCropWidth.toInt() + val finalHeight = if (isSwapped) finalCropWidth.toInt() else finalCropHeight.toInt() + return Pair(finalWidth, finalHeight) } } diff --git a/app/src/main/java/com/hinnka/mycamera/utils/YuvProcessor.kt b/app/src/main/java/com/hinnka/mycamera/utils/YuvProcessor.kt index 70a672a6..3f0f84b3 100644 --- a/app/src/main/java/com/hinnka/mycamera/utils/YuvProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/utils/YuvProcessor.kt @@ -46,7 +46,7 @@ object YuvProcessor { val format = image.format val targetRatio = aspectRatio.getValue(true) - val dimensions = BitmapUtils.calculateProcessedDimensions(width, height, aspectRatio, rotation) + val dimensions = BitmapUtils.calculateProcessedDimensions(width, height, aspectRatio, null, rotation) val previewBitmap = createBitmap(dimensions.first, dimensions.second) processToBitmap( diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index a7bf901f..db68b1a2 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -91,8 +91,8 @@ 设置 拍摄设置 显示设置 - 实验性功能:RAW 拍摄 - 功能未完成,请谨慎使用 + RAW 拍摄 + 使用 RAW 格式 (DNG) 拍摄。关闭后将使用 YCBCR_P010/YUV_420 格式。 水平仪显示 在拍摄时显示水平仪,帮助保持画面水平 网格线显示