feat: curve histogram
This commit is contained in:
1 parent
2d92a20c1c
commit
e214e82554
5 files changed
+178
No files matched your search
@@ -27,6 +27,7 @@ import com.hinnka.mycamera.model.ColorPaletteState
|
||||
import com.hinnka.mycamera.model.ColorRecipeParams
|
||||
import com.hinnka.mycamera.ui.components.ColorRecipePanel
|
||||
import com.hinnka.mycamera.ui.components.CustomSlider
|
||||
import com.hinnka.mycamera.ui.components.ImageHistogram
|
||||
import com.hinnka.mycamera.viewmodel.LutEditViewModel
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
@@ -56,6 +57,7 @@ fun LutEditBottomSheet(
|
||||
onDismiss: () -> Unit,
|
||||
initialParams: ColorRecipeParams? = null,
|
||||
onParamsPreviewChange: ((ColorRecipeParams) -> Unit)? = null,
|
||||
imageHistogram: ImageHistogram? = null,
|
||||
// 照片级配方相关(为 null 则隐藏 scope 切换,保持纯 LUT 编辑模式)
|
||||
photoRecipeParams: ColorRecipeParams? = null,
|
||||
onPhotoParamsChange: ((ColorRecipeParams?) -> Unit)? = null,
|
||||
@@ -264,6 +266,7 @@ fun LutEditBottomSheet(
|
||||
}
|
||||
)
|
||||
},
|
||||
imageHistogram = imageHistogram,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ fun ColorRecipePanel(
|
||||
onParamsChange: (ColorRecipeParams) -> Unit,
|
||||
onRemarksChange: (String) -> Unit,
|
||||
onCurveChange: (CurveChannel, FloatArray?) -> Unit = { _, _ -> },
|
||||
imageHistogram: ImageHistogram? = null,
|
||||
hideNonBakeable: Boolean = true,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
@@ -264,6 +265,7 @@ fun ColorRecipePanel(
|
||||
CurveEditorPanel(
|
||||
currentParams = currentParams,
|
||||
onCurveChange = onCurveChange,
|
||||
imageHistogram = imageHistogram,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.hinnka.mycamera.lut.CurveUtils
|
||||
import com.hinnka.mycamera.model.ColorRecipeParams
|
||||
import kotlin.math.sqrt
|
||||
|
||||
/**
|
||||
* 曲线通道枚举
|
||||
@@ -51,6 +52,7 @@ enum class CurveChannel(val label: String, val color: Color) {
|
||||
fun CurveEditorPanel(
|
||||
currentParams: ColorRecipeParams,
|
||||
onCurveChange: (CurveChannel, FloatArray?) -> Unit,
|
||||
imageHistogram: ImageHistogram? = null,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
var selectedChannel by remember { mutableStateOf(CurveChannel.MASTER) }
|
||||
@@ -96,6 +98,7 @@ fun CurveEditorPanel(
|
||||
CurveCanvas(
|
||||
points = currentPoints ?: identityPoints(),
|
||||
curveColor = selectedChannel.color,
|
||||
histogram = imageHistogram?.binsFor(selectedChannel),
|
||||
onPointsChange = { newPoints ->
|
||||
val arr = if (newPoints.size <= 4 && CurveUtils.isIdentityCurve(newPoints)) null else newPoints
|
||||
onCurveChange(selectedChannel, arr)
|
||||
@@ -110,6 +113,7 @@ fun CurveEditorPanel(
|
||||
private fun CurveCanvas(
|
||||
points: FloatArray,
|
||||
curveColor: Color,
|
||||
histogram: IntArray?,
|
||||
onPointsChange: (FloatArray) -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
@@ -258,6 +262,7 @@ private fun CurveCanvas(
|
||||
drawCurveCanvas(
|
||||
controlPoints = controlPoints,
|
||||
curveColor = curveColor,
|
||||
histogram = histogram,
|
||||
canvasSize = size,
|
||||
graphInset = graphInsetPx
|
||||
)
|
||||
@@ -268,6 +273,7 @@ private fun CurveCanvas(
|
||||
private fun DrawScope.drawCurveCanvas(
|
||||
controlPoints: List<Pair<Float, Float>>,
|
||||
curveColor: Color,
|
||||
histogram: IntArray?,
|
||||
canvasSize: Size,
|
||||
graphInset: Float
|
||||
) {
|
||||
@@ -283,6 +289,15 @@ private fun DrawScope.drawCurveCanvas(
|
||||
size = Size(w, h)
|
||||
)
|
||||
|
||||
drawImageHistogram(
|
||||
histogram = histogram,
|
||||
color = curveColor,
|
||||
left = left,
|
||||
top = top,
|
||||
width = w,
|
||||
height = h,
|
||||
)
|
||||
|
||||
// 网格线(4×4)
|
||||
val gridColor = Color.White.copy(alpha = 0.08f)
|
||||
for (i in 1..3) {
|
||||
@@ -347,6 +362,31 @@ private fun DrawScope.drawCurveCanvas(
|
||||
}
|
||||
}
|
||||
|
||||
private fun DrawScope.drawImageHistogram(
|
||||
histogram: IntArray?,
|
||||
color: Color,
|
||||
left: Float,
|
||||
top: Float,
|
||||
width: Float,
|
||||
height: Float,
|
||||
) {
|
||||
if (histogram == null || histogram.size < 2) return
|
||||
|
||||
val peak = histogram.maxOrNull()?.takeIf { it > 0 } ?: return
|
||||
val peakScale = sqrt(peak.toFloat())
|
||||
val path = Path().apply {
|
||||
moveTo(left, top + height)
|
||||
histogram.forEachIndexed { index, count ->
|
||||
val x = left + index.toFloat() / histogram.lastIndex * width
|
||||
val normalizedHeight = sqrt(count.toFloat()) / peakScale
|
||||
lineTo(x, top + height * (1f - normalizedHeight))
|
||||
}
|
||||
lineTo(left + width, top + height)
|
||||
close()
|
||||
}
|
||||
drawPath(path, color.copy(alpha = 0.18f))
|
||||
}
|
||||
|
||||
// ──────────────────────── 辅助函数 ────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.hinnka.mycamera.ui.components
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* Display-referred histogram data for the curve editor.
|
||||
*
|
||||
* The source bitmap is reduced to a bounded software bitmap before reading pixels so histogram
|
||||
* updates do not allocate a full-resolution pixel buffer for the gallery preview.
|
||||
*/
|
||||
class ImageHistogram private constructor(
|
||||
private val luminance: IntArray,
|
||||
private val red: IntArray,
|
||||
private val green: IntArray,
|
||||
private val blue: IntArray,
|
||||
) {
|
||||
fun binsFor(channel: CurveChannel): IntArray = when (channel) {
|
||||
CurveChannel.MASTER -> luminance
|
||||
CurveChannel.RED -> red
|
||||
CurveChannel.GREEN -> green
|
||||
CurveChannel.BLUE -> blue
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val BIN_COUNT = 256
|
||||
private const val MAX_SAMPLE_EDGE = 512
|
||||
|
||||
fun fromBitmap(bitmap: Bitmap): ImageHistogram {
|
||||
check(!bitmap.isRecycled) { "Cannot calculate a histogram from a recycled bitmap" }
|
||||
require(bitmap.width > 0 && bitmap.height > 0) {
|
||||
"Histogram source bitmap must have non-zero dimensions"
|
||||
}
|
||||
|
||||
val scale = minOf(
|
||||
1f,
|
||||
MAX_SAMPLE_EDGE.toFloat() / maxOf(bitmap.width, bitmap.height),
|
||||
)
|
||||
val sampleWidth = (bitmap.width * scale).roundToInt().coerceAtLeast(1)
|
||||
val sampleHeight = (bitmap.height * scale).roundToInt().coerceAtLeast(1)
|
||||
val scaledBitmap = if (sampleWidth != bitmap.width || sampleHeight != bitmap.height) {
|
||||
Bitmap.createScaledBitmap(bitmap, sampleWidth, sampleHeight, true)
|
||||
} else {
|
||||
bitmap
|
||||
}
|
||||
val readableBitmap = if (scaledBitmap.config == Bitmap.Config.HARDWARE) {
|
||||
checkNotNull(scaledBitmap.copy(Bitmap.Config.ARGB_8888, false)) {
|
||||
"Unable to create a software bitmap for histogram calculation"
|
||||
}
|
||||
} else {
|
||||
scaledBitmap
|
||||
}
|
||||
|
||||
try {
|
||||
val pixels = IntArray(readableBitmap.width * readableBitmap.height)
|
||||
readableBitmap.getPixels(
|
||||
pixels,
|
||||
0,
|
||||
readableBitmap.width,
|
||||
0,
|
||||
0,
|
||||
readableBitmap.width,
|
||||
readableBitmap.height,
|
||||
)
|
||||
|
||||
val luminance = IntArray(BIN_COUNT)
|
||||
val red = IntArray(BIN_COUNT)
|
||||
val green = IntArray(BIN_COUNT)
|
||||
val blue = IntArray(BIN_COUNT)
|
||||
|
||||
for (pixel in pixels) {
|
||||
val alpha = pixel ushr 24
|
||||
if (alpha == 0) continue
|
||||
|
||||
val r = pixel shr 16 and 0xFF
|
||||
val g = pixel shr 8 and 0xFF
|
||||
val b = pixel and 0xFF
|
||||
val luma = (0.2126f * r + 0.7152f * g + 0.0722f * b)
|
||||
.roundToInt()
|
||||
.coerceIn(0, BIN_COUNT - 1)
|
||||
|
||||
luminance[luma]++
|
||||
red[r]++
|
||||
green[g]++
|
||||
blue[b]++
|
||||
}
|
||||
|
||||
return ImageHistogram(
|
||||
luminance = luminance,
|
||||
red = red,
|
||||
green = green,
|
||||
blue = blue,
|
||||
)
|
||||
} finally {
|
||||
if (readableBitmap !== scaledBitmap && readableBitmap !== bitmap) {
|
||||
readableBitmap.recycle()
|
||||
}
|
||||
if (scaledBitmap !== bitmap) {
|
||||
scaledBitmap.recycle()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,6 +69,7 @@ import com.hinnka.mycamera.utils.PLog
|
||||
import com.hinnka.mycamera.viewmodel.CameraViewModel
|
||||
import com.hinnka.mycamera.viewmodel.GalleryViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
@@ -185,6 +186,7 @@ fun GalleryEditScreen(
|
||||
// 预览 Bitmap 状态
|
||||
var previewBitmap by remember { mutableStateOf<Bitmap?>(null) }
|
||||
var thumbnailBitmap by remember { mutableStateOf<Bitmap?>(null) }
|
||||
var imageHistogram by remember { mutableStateOf<ImageHistogram?>(null) }
|
||||
|
||||
// 边框编辑状态
|
||||
val editFrameId by viewModel.editFrameId.collectAsState()
|
||||
@@ -255,6 +257,8 @@ fun GalleryEditScreen(
|
||||
if (shouldShowEditControls) 160.dp else 0.dp
|
||||
)
|
||||
var previewRenderRequestId by remember { mutableLongStateOf(0L) }
|
||||
val shouldCalculateImageHistogram =
|
||||
(showLutEditDialog && editLutId != null) || isBaselineLutEditSheetVisible
|
||||
|
||||
fun currentPreviewSignature(fast: Boolean = false): PreviewRenderSignature? {
|
||||
val photo = editSourcePhoto ?: return null
|
||||
@@ -401,6 +405,29 @@ fun GalleryEditScreen(
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(shouldCalculateImageHistogram, previewBitmap) {
|
||||
val bitmap = previewBitmap
|
||||
if (!shouldCalculateImageHistogram || bitmap == null || bitmap.isRecycled) {
|
||||
imageHistogram = null
|
||||
return@LaunchedEffect
|
||||
}
|
||||
|
||||
imageHistogram = try {
|
||||
withContext(Dispatchers.Default) {
|
||||
ImageHistogram.fromBitmap(bitmap)
|
||||
}
|
||||
} catch (error: CancellationException) {
|
||||
throw error
|
||||
} catch (error: Exception) {
|
||||
PLog.e(
|
||||
"GalleryEditScreen",
|
||||
"Failed to calculate curve histogram from the current preview",
|
||||
error,
|
||||
)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(editLutId) {
|
||||
editLutId?.let { lutId ->
|
||||
val selectedIndex = availableLuts.indexOfFirst { it.id == lutId }
|
||||
@@ -1347,6 +1374,7 @@ fun GalleryEditScreen(
|
||||
onParamsPreviewChange = {
|
||||
previewRecipeParamsOverride = it
|
||||
},
|
||||
imageHistogram = imageHistogram,
|
||||
onDismiss = {
|
||||
previewRecipeParamsOverride = null
|
||||
showLutEditDialog = false
|
||||
@@ -1362,6 +1390,7 @@ fun GalleryEditScreen(
|
||||
LutEditBottomSheet(
|
||||
lutId = baselineLutEditId!!,
|
||||
editorTarget = LutEditorTarget.BASELINE_RAW,
|
||||
imageHistogram = imageHistogram,
|
||||
onDismiss = {
|
||||
showBaselineLutEditSheet = false
|
||||
showRawBaselineLutSelectorSheet = true
|
||||
|
||||
Reference in new issue
Block a user