feat: raw base lut
This commit is contained in:
1 parent
d6ba2e0a64
commit
747a940cd9
5 files changed
+101
-45
No files matched your search
Binary file not shown.
@@ -12,6 +12,8 @@ import android.opengl.EGLDisplay
|
||||
import android.opengl.EGLSurface
|
||||
import android.opengl.GLES30
|
||||
import com.hinnka.mycamera.camera.AspectRatio
|
||||
import com.hinnka.mycamera.lut.GlUtils
|
||||
import com.hinnka.mycamera.lut.LutParser
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import kotlinx.coroutines.asCoroutineDispatcher
|
||||
import kotlinx.coroutines.runBlocking
|
||||
@@ -66,6 +68,8 @@ class RawDemosaicProcessor {
|
||||
private var rawTextureId = 0
|
||||
private var framebufferId = 0
|
||||
private var outputTextureId = 0
|
||||
private var baseLutTextureId = 0
|
||||
private var baseLutSize = 32f
|
||||
|
||||
// 缓冲区
|
||||
private var vertexBuffer: FloatBuffer? = null
|
||||
@@ -84,6 +88,8 @@ class RawDemosaicProcessor {
|
||||
private var uDeconvStrengthLoc = 0
|
||||
private var uStructureAmountLoc = 0
|
||||
private var uOutputSharpAmountLoc = 0
|
||||
private var uBaseLutTextureLoc = 0
|
||||
private var uBaseLutSizeLoc = 0
|
||||
|
||||
private var isInitialized = false
|
||||
|
||||
@@ -98,6 +104,7 @@ class RawDemosaicProcessor {
|
||||
* @return 处理后的 Bitmap,失败返回 null
|
||||
*/
|
||||
suspend fun process(
|
||||
context: android.content.Context,
|
||||
rawImage: Image,
|
||||
characteristics: CameraCharacteristics,
|
||||
captureResult: CaptureResult,
|
||||
@@ -106,7 +113,7 @@ class RawDemosaicProcessor {
|
||||
): Bitmap? = withContext(glDispatcher) {
|
||||
try {
|
||||
if (!isInitialized) {
|
||||
if (!initializeOnGlThread()) {
|
||||
if (!initializeOnGlThread(context)) {
|
||||
PLog.e(TAG, "Failed to initialize processor")
|
||||
return@withContext null
|
||||
}
|
||||
@@ -190,24 +197,24 @@ class RawDemosaicProcessor {
|
||||
/**
|
||||
* 预加载 EGL 环境和 Shader
|
||||
*/
|
||||
fun preload() {
|
||||
fun preload(context: android.content.Context) {
|
||||
Executors.newSingleThreadExecutor().execute {
|
||||
runBlocking {
|
||||
withContext(glDispatcher) {
|
||||
initializeOnGlThread()
|
||||
initializeOnGlThread(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun initializeOnGlThread(): Boolean = withContext(glDispatcher) {
|
||||
initialize()
|
||||
private suspend fun initializeOnGlThread(context: android.content.Context): Boolean = withContext(glDispatcher) {
|
||||
initialize(context)
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化 EGL 环境
|
||||
*/
|
||||
fun initialize(): Boolean {
|
||||
fun initialize(context: android.content.Context): Boolean {
|
||||
if (isInitialized) return true
|
||||
|
||||
try {
|
||||
@@ -278,6 +285,16 @@ class RawDemosaicProcessor {
|
||||
initShaderProgram()
|
||||
initBuffers()
|
||||
|
||||
// 加载基础 LUT
|
||||
try {
|
||||
val lutConfig = LutParser.parseFromAssets(context, "luts/base.plut")
|
||||
baseLutTextureId = GlUtils.create3DTexture(lutConfig)
|
||||
baseLutSize = lutConfig.size.toFloat()
|
||||
PLog.d(TAG, "Base LUT loaded: id=$baseLutTextureId, size=$baseLutSize")
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to load base LUT", e)
|
||||
}
|
||||
|
||||
isInitialized = true
|
||||
PLog.d(TAG, "RawDemosaicProcessor initialized")
|
||||
return true
|
||||
@@ -329,6 +346,8 @@ class RawDemosaicProcessor {
|
||||
uDeconvStrengthLoc = GLES30.glGetUniformLocation(shaderProgram, "uDeconvStrength")
|
||||
uStructureAmountLoc = GLES30.glGetUniformLocation(shaderProgram, "uStructureAmount")
|
||||
uOutputSharpAmountLoc = GLES30.glGetUniformLocation(shaderProgram, "uOutputSharpAmount")
|
||||
uBaseLutTextureLoc = GLES30.glGetUniformLocation(shaderProgram, "uBaseLutTexture")
|
||||
uBaseLutSizeLoc = GLES30.glGetUniformLocation(shaderProgram, "uBaseLutSize")
|
||||
|
||||
PLog.d(TAG, "Shader program created: $shaderProgram")
|
||||
}
|
||||
@@ -581,6 +600,12 @@ class RawDemosaicProcessor {
|
||||
GLES30.glUniform1f(uStructureAmountLoc, 0.5f) // 结构增强强度 (中等)
|
||||
GLES30.glUniform1f(uOutputSharpAmountLoc, 0.4f) // 输出锐化强度 (轻微)
|
||||
|
||||
// 绑定基础 LUT 纹理
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE1)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_3D, baseLutTextureId)
|
||||
GLES30.glUniform1i(uBaseLutTextureLoc, 1)
|
||||
GLES30.glUniform1f(uBaseLutSizeLoc, baseLutSize)
|
||||
|
||||
// 绘制四边形
|
||||
val positionHandle = GLES30.glGetAttribLocation(shaderProgram, "aPosition")
|
||||
val texCoordHandle = GLES30.glGetAttribLocation(shaderProgram, "aTexCoord")
|
||||
@@ -680,6 +705,9 @@ class RawDemosaicProcessor {
|
||||
if (framebufferId != 0) {
|
||||
GLES30.glDeleteFramebuffers(1, intArrayOf(framebufferId), 0)
|
||||
}
|
||||
if (baseLutTextureId != 0) {
|
||||
GLES30.glDeleteTextures(1, intArrayOf(baseLutTextureId), 0)
|
||||
}
|
||||
|
||||
EGL14.eglMakeCurrent(eglDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT)
|
||||
EGL14.eglDestroySurface(eglDisplay, eglSurface)
|
||||
|
||||
@@ -75,6 +75,10 @@ object RawShaders {
|
||||
uniform vec4 uWhiteBalanceGains;
|
||||
uniform mat3 uColorCorrectionMatrix;
|
||||
|
||||
// Base LUT
|
||||
uniform mediump sampler3D uBaseLutTexture;
|
||||
uniform float uBaseLutSize;
|
||||
|
||||
// Capture One 风格控制参数
|
||||
uniform float uDeconvStrength; // 输入反卷积强度 (0.0-1.0)
|
||||
uniform float uStructureAmount; // 结构增强强度 (0.0-1.0)
|
||||
@@ -377,6 +381,12 @@ object RawShaders {
|
||||
// 步骤 8: 输出锐化 (在 gamma 空间进行)
|
||||
rgb = applyOutputSharpening(rgb, coord);
|
||||
|
||||
// 步骤 9: 应用基础 LUT
|
||||
float lutScale = (uBaseLutSize - 1.0) / uBaseLutSize;
|
||||
float lutOffset = 1.0 / (2.0 * uBaseLutSize);
|
||||
vec3 lutCoord = clamp(rgb, 0.0, 1.0) * lutScale + lutOffset;
|
||||
rgb = texture(uBaseLutTexture, lutCoord).rgb;
|
||||
|
||||
// 最终输出
|
||||
fragColor = vec4(clamp(rgb, 0.0, 1.0), 1.0);
|
||||
}
|
||||
|
||||
@@ -21,19 +21,19 @@ import java.nio.ByteBuffer
|
||||
* 使用 GPU 加速的解马赛克算法处理 RAW 数据
|
||||
*/
|
||||
object RawProcessor {
|
||||
|
||||
|
||||
private const val TAG = "RawProcessor"
|
||||
|
||||
|
||||
/**
|
||||
* 检查图像是否为 RAW 格式
|
||||
*/
|
||||
fun isRawImage(image: Image): Boolean {
|
||||
return image.format == ImageFormat.RAW_SENSOR ||
|
||||
image.format == ImageFormat.RAW_PRIVATE ||
|
||||
image.format == ImageFormat.RAW10 ||
|
||||
image.format == ImageFormat.RAW12
|
||||
image.format == ImageFormat.RAW_PRIVATE ||
|
||||
image.format == ImageFormat.RAW10 ||
|
||||
image.format == ImageFormat.RAW12
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将 RAW 图像转换为 Bitmap
|
||||
*
|
||||
@@ -52,6 +52,7 @@ object RawProcessor {
|
||||
* @return 处理后的 Bitmap,如果失败返回 null
|
||||
*/
|
||||
fun processAndToBitmap(
|
||||
context: android.content.Context,
|
||||
image: Image,
|
||||
characteristics: CameraCharacteristics,
|
||||
captureResult: CaptureResult,
|
||||
@@ -62,14 +63,14 @@ object RawProcessor {
|
||||
PLog.e(TAG, "Image is not RAW format: ${image.format}")
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
return try {
|
||||
// 使用 GPU 加速的 RAW 处理器
|
||||
val processor = RawDemosaicProcessor.getInstance()
|
||||
val result = runBlocking {
|
||||
processor.process(image, characteristics, captureResult, aspectRatio, rotation)
|
||||
processor.process(context, image, characteristics, captureResult, aspectRatio, rotation)
|
||||
}
|
||||
|
||||
|
||||
if (result != null) {
|
||||
PLog.d(TAG, "RAW processed with GPU demosaic: ${result.width}x${result.height}")
|
||||
} else {
|
||||
@@ -77,16 +78,16 @@ object RawProcessor {
|
||||
// 回退到原始方法
|
||||
return processWithImageDecoder(image, characteristics, captureResult, aspectRatio, rotation)
|
||||
}
|
||||
|
||||
|
||||
result
|
||||
|
||||
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "GPU RAW processing failed, trying fallback", e)
|
||||
// 回退到原始方法
|
||||
processWithImageDecoder(image, characteristics, captureResult, aspectRatio, rotation)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用 ImageDecoder 的回退方法(原始实现)
|
||||
*
|
||||
@@ -105,10 +106,10 @@ object RawProcessor {
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
dngCreator.writeImage(outputStream, image)
|
||||
dngCreator.close()
|
||||
|
||||
|
||||
val dngBytes = outputStream.toByteArray()
|
||||
PLog.d(TAG, "DNG created (fallback): ${dngBytes.size} bytes")
|
||||
|
||||
|
||||
// Step 2: 使用 ImageDecoder 解码 DNG
|
||||
val source = ImageDecoder.createSource(ByteBuffer.wrap(dngBytes))
|
||||
var decodedBitmap = ImageDecoder.decodeBitmap(source) { decoder, info, _ ->
|
||||
@@ -116,16 +117,16 @@ object RawProcessor {
|
||||
decoder.setTargetColorSpace(android.graphics.ColorSpace.get(android.graphics.ColorSpace.Named.SRGB))
|
||||
decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
|
||||
}
|
||||
|
||||
|
||||
PLog.d(TAG, "DNG decoded (fallback): ${decodedBitmap.width}x${decodedBitmap.height}")
|
||||
|
||||
|
||||
// Step 3: 处理旋转
|
||||
if (rotation != 0) {
|
||||
val matrix = Matrix()
|
||||
matrix.postRotate(rotation.toFloat())
|
||||
val rotatedBitmap = Bitmap.createBitmap(
|
||||
decodedBitmap, 0, 0,
|
||||
decodedBitmap.width, decodedBitmap.height,
|
||||
decodedBitmap, 0, 0,
|
||||
decodedBitmap.width, decodedBitmap.height,
|
||||
matrix, true
|
||||
)
|
||||
if (rotatedBitmap != decodedBitmap) {
|
||||
@@ -133,21 +134,21 @@ object RawProcessor {
|
||||
}
|
||||
decodedBitmap = rotatedBitmap
|
||||
}
|
||||
|
||||
|
||||
// Step 4: 裁切到目标宽高比
|
||||
val croppedBitmap = cropToAspectRatio(decodedBitmap, aspectRatio)
|
||||
if (croppedBitmap != decodedBitmap) {
|
||||
decodedBitmap.recycle()
|
||||
}
|
||||
|
||||
|
||||
croppedBitmap
|
||||
|
||||
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Fallback RAW processing also failed", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 裁切 Bitmap 到目标宽高比(居中裁切)
|
||||
*/
|
||||
@@ -156,17 +157,17 @@ object RawProcessor {
|
||||
val srcHeight = bitmap.height
|
||||
val srcRatio = srcWidth.toFloat() / srcHeight.toFloat()
|
||||
val targetRatio = aspectRatio.getValue(false) // width/height
|
||||
|
||||
|
||||
// 如果宽高比已经匹配,直接返回
|
||||
if (kotlin.math.abs(srcRatio - targetRatio) < 0.01f) {
|
||||
return bitmap
|
||||
}
|
||||
|
||||
|
||||
val cropWidth: Int
|
||||
val cropHeight: Int
|
||||
val cropX: Int
|
||||
val cropY: Int
|
||||
|
||||
|
||||
if (srcRatio > targetRatio) {
|
||||
// 原图更宽,裁切左右
|
||||
cropHeight = srcHeight
|
||||
@@ -180,10 +181,10 @@ object RawProcessor {
|
||||
cropX = 0
|
||||
cropY = (srcHeight - cropHeight) / 2
|
||||
}
|
||||
|
||||
|
||||
return Bitmap.createBitmap(bitmap, cropX, cropY, cropWidth, cropHeight)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将 RAW 图像保存为 DNG 文件
|
||||
*
|
||||
@@ -201,7 +202,7 @@ object RawProcessor {
|
||||
if (!isRawImage(image)) {
|
||||
throw IllegalArgumentException("Image is not RAW format: ${image.format}")
|
||||
}
|
||||
|
||||
|
||||
val dngCreator = DngCreator(characteristics, captureResult)
|
||||
try {
|
||||
dngCreator.writeImage(outputStream, image)
|
||||
|
||||
@@ -133,6 +133,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
val useSoftwareProcessing: StateFlow<Boolean> = userPreferencesRepository.userPreferences
|
||||
.map { it.useSoftwareProcessing }
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), false)
|
||||
|
||||
// 软件处理参数 Flow
|
||||
val sharpening: StateFlow<Float> = userPreferencesRepository.userPreferences
|
||||
.map { it.sharpening }
|
||||
@@ -153,7 +154,10 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
init {
|
||||
cameraController.initialize()
|
||||
cameraController.onImageCaptured = { image, captureInfo, characteristics, captureResult ->
|
||||
PLog.d(TAG, "onImageCaptured callback triggered - image: ${image.width}x${image.height}, format: ${image.format}")
|
||||
PLog.d(
|
||||
TAG,
|
||||
"onImageCaptured callback triggered - image: ${image.width}x${image.height}, format: ${image.format}"
|
||||
)
|
||||
viewModelScope.launch {
|
||||
saveImage(image, captureInfo, characteristics, captureResult)
|
||||
}
|
||||
@@ -263,7 +267,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
fun openCamera(surfaceTexture: SurfaceTexture) {
|
||||
PLog.d(TAG, "openCamera")
|
||||
// 预加载 RAW 处理器
|
||||
RawDemosaicProcessor.getInstance().preload()
|
||||
RawDemosaicProcessor.getInstance().preload(getApplication())
|
||||
currentSurfaceTexture = surfaceTexture
|
||||
cameraController.openCamera(surfaceTexture)
|
||||
}
|
||||
@@ -838,7 +842,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
var waitCount = 0
|
||||
while (waitCount < 20) {
|
||||
kotlinx.coroutines.delay(10)
|
||||
finalCaptureResult = cameraController.getLastCaptureResult() as android.hardware.camera2.CaptureResult?
|
||||
finalCaptureResult =
|
||||
cameraController.getLastCaptureResult() as android.hardware.camera2.CaptureResult?
|
||||
if (finalCaptureResult != null) {
|
||||
if (waitCount > 2) { // 只有延迟超过 20ms 时才记录日志
|
||||
PLog.d(TAG, "captureResult became available after ${waitCount * 10}ms")
|
||||
@@ -878,9 +883,19 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
// RAW 图像:使用 RawProcessor 处理
|
||||
if (characteristics != null && finalCaptureResult != null) {
|
||||
PLog.d(TAG, "Processing RAW image")
|
||||
RawProcessor.processAndToBitmap(image, characteristics, finalCaptureResult, aspectRatio, rotation)
|
||||
RawProcessor.processAndToBitmap(
|
||||
context,
|
||||
image,
|
||||
characteristics,
|
||||
finalCaptureResult,
|
||||
aspectRatio,
|
||||
rotation
|
||||
)
|
||||
} else {
|
||||
PLog.e(TAG, "Cannot process RAW: characteristics=${characteristics != null}, finalCaptureResult=${finalCaptureResult != null}")
|
||||
PLog.e(
|
||||
TAG,
|
||||
"Cannot process RAW: characteristics=${characteristics != null}, finalCaptureResult=${finalCaptureResult != null}"
|
||||
)
|
||||
null
|
||||
}
|
||||
} else {
|
||||
@@ -896,11 +911,12 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
|
||||
try {
|
||||
// 如果之前没有 captureResult 但现在等到了,尝试重新构建更完整的 CaptureInfo 供 EXIF 使用
|
||||
val finalCaptureInfo = if (captureResult == null && finalCaptureResult is android.hardware.camera2.TotalCaptureResult) {
|
||||
cameraController.rebuildCaptureInfo(finalCaptureResult, image.width, image.height)
|
||||
} else {
|
||||
captureInfo
|
||||
}
|
||||
val finalCaptureInfo =
|
||||
if (captureResult == null && finalCaptureResult is android.hardware.camera2.TotalCaptureResult) {
|
||||
cameraController.rebuildCaptureInfo(finalCaptureResult, image.width, image.height)
|
||||
} else {
|
||||
captureInfo
|
||||
}
|
||||
|
||||
// 创建统一的 PhotoMetadata,包含编辑配置和拍摄信息
|
||||
val metadata = PhotoMetadata(
|
||||
@@ -948,7 +964,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
|
||||
val filename = "PhotonCamera_${
|
||||
java.text.SimpleDateFormat("yyyyMMdd_HHmmss", java.util.Locale.US).format(java.util.Date())
|
||||
java.text.SimpleDateFormat("yyyyMMdd_HHmmss", java.util.Locale.US)
|
||||
.format(java.util.Date())
|
||||
}.jpg"
|
||||
val contentValues = android.content.ContentValues().apply {
|
||||
put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, filename)
|
||||
|
||||
Reference in new issue
Block a user