feat: configurable sharpening tuning and quality preset
This commit is contained in:
1 parent
0bf02ba5b4
commit
bfbc2bf194
19 files changed
+628
-158
No files matched your search
@@ -1777,111 +1777,6 @@ int RunYuvToRgb(
|
||||
return function(nullptr, &input_buffer, &output_buffer);
|
||||
}
|
||||
|
||||
bool BuildDefaultSharpenCurves(
|
||||
float snr,
|
||||
const float interpolation_scales[3],
|
||||
SharpenCurveSelection* output) {
|
||||
if (output == nullptr || interpolation_scales == nullptr ||
|
||||
!std::isfinite(snr) || snr <= 0.0f ||
|
||||
!std::all_of(
|
||||
interpolation_scales,
|
||||
interpolation_scales + 3,
|
||||
[](float value) { return std::isfinite(value); })) {
|
||||
return false;
|
||||
}
|
||||
std::fill_n(output->relative_corner_acutance_correction, 3, 0.0f);
|
||||
|
||||
struct Node {
|
||||
float snr;
|
||||
float parameters[3][5];
|
||||
};
|
||||
// Decoded mechanically from MGC 9.6.080's generic
|
||||
// sharpen_default.binarypb asset. BuildForSNR converts each node to
|
||||
// piecewise points first, then interpolates those converted points.
|
||||
constexpr Node nodes[] = {
|
||||
{5.0f, {{1.6f, 0.05f, 1.0f, 1.0f, 0.0f},
|
||||
{1.8f, 0.03f, 1.0f, 1.0f, 0.0f},
|
||||
{1.2f, 0.02f, 1.0f, 1.0f, 0.0f}}},
|
||||
{10.0f, {{2.5f, 0.05f, 1.0f, 1.0f, 0.0f},
|
||||
{2.2f, 0.03f, 1.0f, 1.0f, 0.0f},
|
||||
{1.3f, 0.02f, 1.0f, 1.0f, 0.0f}}},
|
||||
{20.0f, {{3.2f, 0.05f, 1.0f, 1.0f, 0.0f},
|
||||
{2.6f, 0.03f, 1.0f, 1.0f, 0.0f},
|
||||
{1.4f, 0.02f, 1.0f, 1.0f, 0.0f}}},
|
||||
{40.0f, {{5.2f, 0.02f, 1.0f, 1.0f, 0.0f},
|
||||
{2.1f, 0.02f, 1.0f, 1.0f, 0.0f},
|
||||
{1.4f, 0.02f, 1.0f, 1.0f, 0.0f}}},
|
||||
{80.0f, {{4.7f, 0.02f, 1.0f, 1.0f, 0.0f},
|
||||
{2.1f, 0.02f, 1.0f, 1.0f, 0.0f},
|
||||
{1.4f, 0.02f, 1.0f, 1.0f, 0.0f}}},
|
||||
};
|
||||
const Node* lower = &nodes[0];
|
||||
const Node* upper = &nodes[0];
|
||||
if (snr >= nodes[std::size(nodes) - 1].snr) {
|
||||
lower = upper = &nodes[std::size(nodes) - 1];
|
||||
} else if (snr > nodes[0].snr) {
|
||||
for (size_t index = 1; index < std::size(nodes); ++index) {
|
||||
if (snr <= nodes[index].snr) {
|
||||
lower = &nodes[index - 1];
|
||||
upper = &nodes[index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
const float interpolation = lower == upper
|
||||
? 0.0f
|
||||
: (snr - lower->snr) / (upper->snr - lower->snr);
|
||||
|
||||
const auto convert = [](const float parameters[5], float points[2][5]) {
|
||||
const float gain = parameters[0];
|
||||
const float first_x = parameters[1];
|
||||
const float second_x = parameters[2];
|
||||
const float transition_width = parameters[3];
|
||||
const float transition_mix = parameters[4];
|
||||
points[0][0] = 0.0f;
|
||||
points[1][0] = 0.0f;
|
||||
points[0][1] = first_x;
|
||||
const float initial_gain = gain >= 1.0f
|
||||
? std::max(gain * 0.5f, 1.0f)
|
||||
: std::min(gain * 2.0f, 1.0f);
|
||||
points[1][1] = initial_gain * first_x;
|
||||
points[0][2] = second_x;
|
||||
points[1][2] = gain * second_x;
|
||||
points[0][3] = second_x + transition_width;
|
||||
points[1][3] =
|
||||
transition_mix *
|
||||
((gain * second_x - second_x) +
|
||||
(second_x + transition_width)) +
|
||||
(second_x + transition_width) * (1.0f - transition_mix);
|
||||
points[0][4] = points[0][3] + 1.0f;
|
||||
points[1][4] = points[1][3] + 1.0f;
|
||||
};
|
||||
|
||||
for (int frequency = 0; frequency < 3; ++frequency) {
|
||||
// Photon exposes one independent interpolation scale per sharpening frequency group.
|
||||
const float frequency_interpolation =
|
||||
interpolation * interpolation_scales[frequency];
|
||||
float lower_points[2][5] = {};
|
||||
float upper_points[2][5] = {};
|
||||
convert(lower->parameters[frequency], lower_points);
|
||||
convert(upper->parameters[frequency], upper_points);
|
||||
for (int coordinate = 0; coordinate < 2; ++coordinate) {
|
||||
for (int point = 0; point < 5; ++point) {
|
||||
const float lower_value = lower_points[coordinate][point];
|
||||
const float upper_value = upper_points[coordinate][point];
|
||||
output->curves[
|
||||
point + 5 * frequency + 15 * coordinate] =
|
||||
lower_value * (1.0f - frequency_interpolation) +
|
||||
upper_value * frequency_interpolation;
|
||||
}
|
||||
}
|
||||
}
|
||||
output->lower_snr = lower->snr;
|
||||
output->upper_snr = upper->snr;
|
||||
output->interpolation = interpolation;
|
||||
return true;
|
||||
}
|
||||
|
||||
int RunSharpenTo16Bit(
|
||||
const int16_t* input_yuv,
|
||||
int width,
|
||||
|
||||
@@ -37,17 +37,6 @@ struct SpatialStrengthResult {
|
||||
float output_weights_sum_total_diag_1[3] = {};
|
||||
};
|
||||
|
||||
struct SharpenCurveSelection {
|
||||
float lower_snr = 0.0f;
|
||||
float upper_snr = 0.0f;
|
||||
float interpolation = 0.0f;
|
||||
// Halide layout [guide=1, point=5, frequency=3, coordinate=2].
|
||||
float curves[30] = {};
|
||||
// Optional CurveParams field, one value per sharpening frequency. The
|
||||
// generic sharpen_default.binarypb profile leaves all three unset.
|
||||
float relative_corner_acutance_correction[3] = {};
|
||||
};
|
||||
|
||||
/**
|
||||
* Reproduces CreateLumaDenoiseNoiseBuffers for one luma noise channel.
|
||||
* correlation is MGC's 128-bin normalized power correlation spectrum. A
|
||||
@@ -222,15 +211,6 @@ int RunYuvToRgb(
|
||||
int height,
|
||||
int16_t* output);
|
||||
|
||||
/**
|
||||
* Reproduces SharpenTuning::BuildForSNR for MGC's generic
|
||||
* sharpen_default.binarypb profile.
|
||||
*/
|
||||
bool BuildDefaultSharpenCurves(
|
||||
float snr,
|
||||
const float interpolation_scales[3],
|
||||
SharpenCurveSelection* output);
|
||||
|
||||
/**
|
||||
* Runs MGC 9.6.080's exact SharpenTo16BitHalide FinishRaw kernel.
|
||||
*
|
||||
|
||||
@@ -102,9 +102,9 @@ void SharpenRgbToRgba(const uint16_t *rgb, size_t count, uint8_t *rgba) {
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_hinnka_mycamera_raw_MgcSharpen_nativeSharpenRgba8(
|
||||
JNIEnv *env, jobject, jobject rgba_buffer, jobject scratch_buffer,
|
||||
jint width, jint height, jfloat snr, jfloat attenuation) {
|
||||
jint width, jint height, jfloat snr, jfloat attenuation, jfloatArray curve_points) {
|
||||
using namespace photon::mgc_denoise;
|
||||
if (!rgba_buffer || !scratch_buffer || width <= 0 || height <= 0 ||
|
||||
if (!rgba_buffer || !scratch_buffer || !curve_points || width <= 0 || height <= 0 ||
|
||||
!std::isfinite(snr) || snr <= 0 || !std::isfinite(attenuation) ||
|
||||
attenuation < 0)
|
||||
return -1;
|
||||
@@ -122,17 +122,31 @@ Java_com_hinnka_mycamera_raw_MgcSharpen_nativeSharpenRgba8(
|
||||
env->GetDirectBufferCapacity(scratch_buffer) < jlong(scratch_bytes))
|
||||
return -1;
|
||||
auto *rgb = reinterpret_cast<uint16_t *>(yuv + count * 3);
|
||||
SharpenCurveSelection curves;
|
||||
constexpr float scales[3] = {1, 1, 1};
|
||||
if (!BuildDefaultSharpenCurves(snr, scales, &curves))
|
||||
// Curves are resolved once in Kotlin from Photon's local, named tuning table.
|
||||
// Halide layout: point + 5 * frequency + 15 * coordinate.
|
||||
if (env->GetArrayLength(curve_points) != 30)
|
||||
return -1;
|
||||
float curves[30];
|
||||
env->GetFloatArrayRegion(curve_points, 0, 30, curves);
|
||||
if (env->ExceptionCheck())
|
||||
return -1;
|
||||
for (int band = 0; band < 3; ++band) {
|
||||
for (int point = 0; point < 5; ++point) {
|
||||
const int index = point + 5 * band;
|
||||
const float x = curves[index], y = curves[index + 15];
|
||||
if (!std::isfinite(x) || !std::isfinite(y) || x < 0 || y < 0 ||
|
||||
(point == 0 && (x != 0 || y != 0)) ||
|
||||
(point > 0 && x <= curves[index - 1]))
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
constexpr float corner_correction[3] = {0, 0, 0};
|
||||
using Clock = std::chrono::steady_clock;
|
||||
const auto start = Clock::now();
|
||||
SharpenRgbaToYuv(rgba, count, yuv);
|
||||
const auto converted = Clock::now();
|
||||
const int result = RunSharpenTo16Bit(
|
||||
yuv, width, height, curves.curves,
|
||||
curves.relative_corner_acutance_correction, attenuation, rgb);
|
||||
yuv, width, height, curves, corner_correction, attenuation, rgb);
|
||||
if (result != 0)
|
||||
return result;
|
||||
const auto sharpened = Clock::now();
|
||||
|
||||
@@ -2244,6 +2244,8 @@ object GalleryManager {
|
||||
captureExposureCompensationEv = captureExposureCompensationEv,
|
||||
).let(physicalRawCrop::rebase).copy(
|
||||
exposureCompensation = captureExposureCompensationEv,
|
||||
rawMaxQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(metadata.customProperties),
|
||||
rawMaxQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(metadata.customProperties),
|
||||
)
|
||||
@@ -2375,6 +2377,8 @@ object GalleryManager {
|
||||
rawWhiteLevelMode = updatedMetadata.rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = updatedMetadata.rawCustomWhiteLevel,
|
||||
sharpeningValue = rawSharpening,
|
||||
processLocalQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(updatedMetadata.customProperties),
|
||||
processLocalQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(updatedMetadata.customProperties),
|
||||
denoiseValue = rawNoiseReduction,
|
||||
@@ -2466,6 +2470,8 @@ object GalleryManager {
|
||||
rawWhiteLevelMode = updatedMetadata.rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = updatedMetadata.rawCustomWhiteLevel,
|
||||
sharpeningValue = rawSharpening,
|
||||
processLocalQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(updatedMetadata.customProperties),
|
||||
processLocalQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(updatedMetadata.customProperties),
|
||||
denoiseValue = rawNoiseReduction,
|
||||
@@ -3260,6 +3266,8 @@ object GalleryManager {
|
||||
colorSpace = RawDemosaicProcessor.getInstance().getRawColorSpace(),
|
||||
).let(physicalRawCrop::rebase).copy(
|
||||
exposureCompensation = captureExposureCompensationEv,
|
||||
rawMaxQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(metadata.customProperties),
|
||||
rawMaxQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(metadata.customProperties),
|
||||
)
|
||||
@@ -3711,6 +3719,8 @@ object GalleryManager {
|
||||
rawWhiteLevelMode = updatedMetadata.rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = updatedMetadata.rawCustomWhiteLevel,
|
||||
sharpeningValue = rawSharpening,
|
||||
processLocalQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(updatedMetadata.customProperties),
|
||||
processLocalQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(updatedMetadata.customProperties),
|
||||
processLocalMgcSharpenTuningSnr = finalStackResult.mgcSharpenTuningSnr,
|
||||
@@ -3803,6 +3813,8 @@ object GalleryManager {
|
||||
rawWhiteLevelMode = updatedMetadata.rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = updatedMetadata.rawCustomWhiteLevel,
|
||||
sharpeningValue = rawSharpening,
|
||||
processLocalQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(updatedMetadata.customProperties),
|
||||
processLocalQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(updatedMetadata.customProperties),
|
||||
denoiseValue = rawNoiseReduction,
|
||||
@@ -4084,6 +4096,8 @@ object GalleryManager {
|
||||
rawWhiteLevelMode = updatedMetadata.rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = updatedMetadata.rawCustomWhiteLevel,
|
||||
sharpeningValue = rawSharpening,
|
||||
processLocalQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(updatedMetadata.customProperties),
|
||||
processLocalQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(updatedMetadata.customProperties),
|
||||
denoiseValue = rawNoiseReduction,
|
||||
@@ -5533,6 +5547,8 @@ object GalleryManager {
|
||||
rawWhiteLevelMode = updatedMetadata.rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = updatedMetadata.rawCustomWhiteLevel,
|
||||
sharpeningValue = RawSharpeningDefaults.DEFAULT_STRENGTH,
|
||||
processLocalQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(updatedMetadata.customProperties),
|
||||
processLocalQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(updatedMetadata.customProperties),
|
||||
denoiseValue = rawNoiseReduction,
|
||||
@@ -5707,6 +5723,8 @@ object GalleryManager {
|
||||
rawCustomWhiteLevel = updatedMetadata?.rawCustomWhiteLevel,
|
||||
sharpeningValue = updatedMetadata?.sharpening
|
||||
?: RawSharpeningDefaults.DEFAULT_STRENGTH,
|
||||
processLocalQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(updatedMetadata?.customProperties.orEmpty()),
|
||||
processLocalQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(updatedMetadata?.customProperties.orEmpty()),
|
||||
denoiseValue = rawNoiseReduction,
|
||||
|
||||
@@ -588,6 +588,8 @@ class PhotoProcessor(
|
||||
rawWhiteLevelMode = metadata.rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = metadata.rawCustomWhiteLevel,
|
||||
sharpeningValue = rawSharpening,
|
||||
processLocalQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(metadata.customProperties),
|
||||
processLocalQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(metadata.customProperties),
|
||||
denoiseValue = rawNoiseReduction,
|
||||
@@ -670,6 +672,8 @@ class PhotoProcessor(
|
||||
rawWhiteLevelMode = metadata.rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = metadata.rawCustomWhiteLevel,
|
||||
sharpeningValue = finalSharpening,
|
||||
processLocalQualityTuningEnabled =
|
||||
PhotonSensorSizeTuning.enabledFromProperties(metadata.customProperties),
|
||||
processLocalQualityTuningSensorAreaMm2 =
|
||||
PhotonSensorSizeTuning.areaFromProperties(metadata.customProperties),
|
||||
denoiseValue = finalNoiseReduction,
|
||||
|
||||
@@ -4,6 +4,8 @@ package com.hinnka.mycamera.processor
|
||||
object PhotonCoreImagingTuning {
|
||||
val fusion: PhotonFusionTuning = PhotonFusionTuning.DEFAULT
|
||||
val denoise: PhotonDenoiseTuning = PhotonDenoiseTuning.DEFAULT
|
||||
/** Local SNR/contrast curves for the original MGC final sharpening kernel. */
|
||||
val sharpen: PhotonSharpenTuning = PhotonSharpenTuning.DEFAULT
|
||||
/** Dehaze + DHA baked into HDRNet's ProfileGainTableMap output. */
|
||||
val dehaze: PhotonDehazeTuning = PhotonDehazeTuning.DEFAULT
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.hinnka.mycamera.processor
|
||||
|
||||
/** Fixed quality-toggle preset, shared by every sensor and by capture/reprocessing. */
|
||||
object PhotonQualitySharpenTuning {
|
||||
/**
|
||||
* Sabre selector 0 is clamped to 1: first table, nodes 2/8/16.
|
||||
* raw A/B=1.125, depth1=0, depth2=1.375; last-node gains=10/.05/.9.
|
||||
* sharpness A=.34375, B=0. No RAISR/Polysharp or denoise controls here.
|
||||
*/
|
||||
val FIXED = PhotonSharpenTuning(
|
||||
amount = PhotonSharpenBands(.34375f, .34375f, 0f),
|
||||
nodes = listOf(
|
||||
PhotonSharpenSnrNode(2f,
|
||||
band0 = curve(.006f, 2.5f * 1.125f),
|
||||
band1 = curve(.002f, 1.8f * 1.125f),
|
||||
band2 = curve(.002f, 1.2f * 1.125f)),
|
||||
PhotonSharpenSnrNode(8f,
|
||||
band0 = curve(.001f, 2.8f * 1.125f),
|
||||
band1 = curve(.0005f, 2.1f * 1.125f),
|
||||
band2 = curve(.0005f, 1.4f * 1.125f)),
|
||||
PhotonSharpenSnrNode(16f,
|
||||
band0 = curve(.0002f, 10f * 1.125f),
|
||||
band1 = curve(.0001f, .05f * 1.125f, lowContrastGain = 2f * .05f * 1.125f),
|
||||
band2 = curve(.0002f, .9f * 1.125f)),
|
||||
),
|
||||
)
|
||||
|
||||
fun resolve(enabled: Boolean): PhotonSharpenTuning =
|
||||
if (enabled) FIXED else PhotonCoreImagingTuning.sharpen
|
||||
|
||||
private fun curve(
|
||||
lowContrastInput: Float,
|
||||
mainContrastGain: Float,
|
||||
lowContrastGain: Float = 1.375f,
|
||||
): PhotonSharpenCurve {
|
||||
// Original Sabre constants at 0x6ad348: second_x=.02, width=.01.
|
||||
// With transition_mix=1 and depth2=1.375 the third point is
|
||||
// y3=(gain*.02-.02+x3)+x3*(1.375-1). Keep every term of that mapping.
|
||||
val mainInput = .02f
|
||||
val highInput = mainInput + .01f
|
||||
val highOutput = (mainContrastGain * mainInput - mainInput + highInput) +
|
||||
highInput * .375f
|
||||
return PhotonSharpenCurve(
|
||||
lowContrastInput = lowContrastInput,
|
||||
lowContrastGain = lowContrastGain,
|
||||
mainContrastInput = mainInput,
|
||||
mainContrastGain = mainContrastGain,
|
||||
highContrastInput = highInput,
|
||||
highContrastGain = highOutput / highInput,
|
||||
tailSpan = 1.375f,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -37,17 +37,21 @@ object PhotonSensorSizeTuning {
|
||||
changePerAreaStop = 0.033127858f,
|
||||
)
|
||||
|
||||
/** Only the capture choice and sensor area are stored, never a core-parameter override. */
|
||||
/** Capture choice survives missing area so fixed sharpening can still be selected. */
|
||||
fun captureProperties(enabled: Boolean, sensorPhysicalAreaMm2: Float?): Map<String, String> {
|
||||
if (!enabled) return emptyMap()
|
||||
val area = sensorPhysicalAreaMm2?.takeIf { it.isFinite() && it > 0f }
|
||||
return if (enabled && area != null) mapOf(
|
||||
MODEL_PROPERTY to MODEL_ID,
|
||||
SENSOR_AREA_PROPERTY to area.toString(),
|
||||
) else emptyMap()
|
||||
return buildMap {
|
||||
put(MODEL_PROPERTY, MODEL_ID)
|
||||
if (area != null) put(SENSOR_AREA_PROPERTY, area.toString())
|
||||
}
|
||||
}
|
||||
|
||||
fun enabledFromProperties(properties: Map<String, String>): Boolean =
|
||||
properties[MODEL_PROPERTY] == MODEL_ID
|
||||
|
||||
fun areaFromProperties(properties: Map<String, String>): Float? =
|
||||
if (properties[MODEL_PROPERTY] == MODEL_ID) {
|
||||
if (enabledFromProperties(properties)) {
|
||||
properties[SENSOR_AREA_PROPERTY]?.toFloatOrNull()?.takeIf { it.isFinite() && it > 0f }
|
||||
} else null
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.hinnka.mycamera.processor
|
||||
|
||||
/**
|
||||
* Local final-sharpening defaults; no preferences or photo-metadata serialization.
|
||||
* See docs/photon-sharpen-tuning.md for units, equations and editing examples.
|
||||
*
|
||||
* [nodes] must be ordered by reference-frame SNR, not merged-frame SNR.
|
||||
* [amount] scales each interpolated curve's departure from y=x. It does not
|
||||
* change SNR interpolation. The existing user slider/runtime attenuation remains
|
||||
* a separate input to the original kernel.
|
||||
*/
|
||||
data class PhotonSharpenTuning(
|
||||
val nodes: List<PhotonSharpenSnrNode>,
|
||||
val amount: PhotonSharpenBands = PhotonSharpenBands(),
|
||||
) {
|
||||
init {
|
||||
require(nodes.isNotEmpty()) { "Sharpen tuning needs at least one SNR node" }
|
||||
require(nodes.zipWithNext().all { (a, b) -> a.snr < b.snr }) {
|
||||
"Sharpen SNR nodes must be strictly increasing"
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/** Explicit equivalent of the previous MGC generic curve table. */
|
||||
val DEFAULT = PhotonSharpenTuning(
|
||||
nodes = listOf(
|
||||
PhotonSharpenSnrNode(5f,
|
||||
band0 = PhotonSharpenCurve(lowContrastInput = .05f, lowContrastGain = 1f, mainContrastGain = 1.6f),
|
||||
band1 = PhotonSharpenCurve(lowContrastInput = .03f, lowContrastGain = 1f, mainContrastGain = 1.8f),
|
||||
band2 = PhotonSharpenCurve(lowContrastInput = .02f, lowContrastGain = 1f, mainContrastGain = 1.2f)),
|
||||
PhotonSharpenSnrNode(10f,
|
||||
band0 = PhotonSharpenCurve(lowContrastInput = .05f, lowContrastGain = 1.25f, mainContrastGain = 2.5f),
|
||||
band1 = PhotonSharpenCurve(lowContrastInput = .03f, lowContrastGain = 1.1f, mainContrastGain = 2.2f),
|
||||
band2 = PhotonSharpenCurve(lowContrastInput = .02f, lowContrastGain = 1f, mainContrastGain = 1.3f)),
|
||||
PhotonSharpenSnrNode(20f,
|
||||
band0 = PhotonSharpenCurve(lowContrastInput = .05f, lowContrastGain = 1.6f, mainContrastGain = 3.2f),
|
||||
band1 = PhotonSharpenCurve(lowContrastInput = .03f, lowContrastGain = 1.3f, mainContrastGain = 2.6f),
|
||||
band2 = PhotonSharpenCurve(lowContrastInput = .02f, lowContrastGain = 1f, mainContrastGain = 1.4f)),
|
||||
PhotonSharpenSnrNode(40f,
|
||||
band0 = PhotonSharpenCurve(lowContrastInput = .02f, lowContrastGain = 2.6f, mainContrastGain = 5.2f),
|
||||
band1 = PhotonSharpenCurve(lowContrastInput = .02f, lowContrastGain = 1.05f, mainContrastGain = 2.1f),
|
||||
band2 = PhotonSharpenCurve(lowContrastInput = .02f, lowContrastGain = 1f, mainContrastGain = 1.4f)),
|
||||
PhotonSharpenSnrNode(80f,
|
||||
band0 = PhotonSharpenCurve(lowContrastInput = .02f, lowContrastGain = 2.35f, mainContrastGain = 4.7f),
|
||||
band1 = PhotonSharpenCurve(lowContrastInput = .02f, lowContrastGain = 1.05f, mainContrastGain = 2.1f),
|
||||
band2 = PhotonSharpenCurve(lowContrastInput = .02f, lowContrastGain = 1f, mainContrastGain = 1.4f)),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dimensionless enhancement amounts, indexed exactly like the MGC frequency axis.
|
||||
* 0 = identity curve, 1 = configured curve; >1 extrapolates its enhancement or
|
||||
* suppression. These are frequency groups, not RGB channels or denoise levels.
|
||||
*/
|
||||
data class PhotonSharpenBands(val band0: Float = 1f, val band1: Float = 1f, val band2: Float = 1f) {
|
||||
init {
|
||||
require(listOf(band0, band1, band2).all { it.isFinite() && it >= 0f })
|
||||
}
|
||||
|
||||
operator fun get(band: Int): Float = when (band) {
|
||||
0 -> band0
|
||||
1 -> band1
|
||||
2 -> band2
|
||||
else -> error("Invalid sharpening frequency index: $band")
|
||||
}
|
||||
}
|
||||
|
||||
/** One positive, linear reference SNR and one contrast curve per native frequency group. */
|
||||
data class PhotonSharpenSnrNode(
|
||||
val snr: Float,
|
||||
val band0: PhotonSharpenCurve,
|
||||
val band1: PhotonSharpenCurve,
|
||||
val band2: PhotonSharpenCurve,
|
||||
) {
|
||||
init { require(snr.isFinite() && snr > 0f) }
|
||||
|
||||
operator fun get(band: Int): PhotonSharpenCurve = when (band) {
|
||||
0 -> band0
|
||||
1 -> band1
|
||||
2 -> band2
|
||||
else -> error("Invalid sharpening frequency index: $band")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Five-point response: origin, three explicit contrast knots, then a unit-slope tail.
|
||||
* Input coordinates are in the MGC response-curve domain, NOT pixel radius,
|
||||
* scene brightness or an ISO value. Gains are output/input at each knot;
|
||||
* only [lowContrastGain] is also the slope from the origin.
|
||||
*
|
||||
* The high-contrast gain may be smaller than the main gain (roll-off). Output
|
||||
* knots therefore need not be monotonic. Input knots must always increase.
|
||||
*/
|
||||
data class PhotonSharpenCurve(
|
||||
/** First knot x; marks the end of the initial constant-gain segment. */
|
||||
val lowContrastInput: Float = .02f,
|
||||
/** Gain throughout the segment from zero to lowContrastInput. */
|
||||
val lowContrastGain: Float = 1f,
|
||||
/** Second knot x; center of the configurable contrast enhancement. */
|
||||
val mainContrastInput: Float = 1f,
|
||||
/** Second knot y/x, not a multiplier of the other gains. */
|
||||
val mainContrastGain: Float = 1f,
|
||||
/** Third knot x; end of the transition from the main gain to the high gain. */
|
||||
val highContrastInput: Float = 2f,
|
||||
/** Third knot y/x; 1 rejoins identity at this knot, >1 retains enhancement. */
|
||||
val highContrastGain: Float = 1f,
|
||||
/** x span to the final knot; y increases by the same span (slope exactly 1). */
|
||||
val tailSpan: Float = 1f,
|
||||
) {
|
||||
init {
|
||||
require(listOf(lowContrastInput, mainContrastInput, highContrastInput, tailSpan)
|
||||
.all { it.isFinite() && it > 0f })
|
||||
require(lowContrastInput < mainContrastInput && mainContrastInput < highContrastInput)
|
||||
require(listOf(lowContrastGain, mainContrastGain, highContrastGain)
|
||||
.all { it.isFinite() && it >= 0f })
|
||||
require((lowContrastInput * lowContrastGain).isFinite())
|
||||
require((mainContrastInput * mainContrastGain).isFinite())
|
||||
require((highContrastInput * highContrastGain + tailSpan).isFinite())
|
||||
require((highContrastInput + tailSpan).isFinite() && highContrastInput + tailSpan > highContrastInput)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package com.hinnka.mycamera.raw
|
||||
import android.opengl.GLES30
|
||||
import android.opengl.GLES31
|
||||
import com.hinnka.mycamera.processor.GlesGpuCompletion
|
||||
import com.hinnka.mycamera.processor.PhotonCoreImagingTuning
|
||||
import com.hinnka.mycamera.processor.PhotonSharpenTuning
|
||||
import com.hinnka.mycamera.utils.LargeDirectBuffer
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import java.nio.ByteBuffer
|
||||
@@ -25,9 +27,11 @@ internal class MgcSharpen {
|
||||
height: Int,
|
||||
snr: Float,
|
||||
attenuation: Float,
|
||||
tuning: PhotonSharpenTuning = PhotonCoreImagingTuning.sharpen,
|
||||
) {
|
||||
require(width > 0 && height > 0 && snr.isFinite() && snr > 0f)
|
||||
require(attenuation.isFinite() && attenuation >= 0f)
|
||||
val curves = MgcSharpenCurveBuilder.build(snr, tuning)
|
||||
val pixels = width.toLong() * height
|
||||
val outputPixels = width.toLong() * ((height.toLong() + 1) and -2L)
|
||||
val scratchBytes = (pixels + outputPixels) * 6
|
||||
@@ -70,7 +74,7 @@ internal class MgcSharpen {
|
||||
val nativeStart = System.nanoTime()
|
||||
try {
|
||||
val result = nativeSharpenRgba8(mapped.order(ByteOrder.nativeOrder()),
|
||||
checkNotNull(scratch), width, height, snr, attenuation)
|
||||
checkNotNull(scratch), width, height, snr, attenuation, curves.points)
|
||||
check(result == 0) { "MGC original sharpen failed: $result" }
|
||||
} finally {
|
||||
check(GLES30.glUnmapBuffer(GLES30.GL_PIXEL_PACK_BUFFER)) {
|
||||
@@ -85,6 +89,8 @@ internal class MgcSharpen {
|
||||
}
|
||||
PLog.i(TAG, "originalMgc=true size=${width}x$height referenceSnr=$snr " +
|
||||
"attenuation=$attenuation transfer=${if (useCompute) "SSBO" else "PBO"} " +
|
||||
"curveSnr=${curves.lowerSnr}:${curves.upperSnr} curveMix=${curves.interpolation} " +
|
||||
"bandAmount=${tuning.amount} mainGains=${curves.mainGains} " +
|
||||
"allocationMs=$allocationMs upstreamGpuWaitMs=$upstreamWaitMs " +
|
||||
"transferAndMapMs=$transferMs nativeAndUnmapMs=$nativeMs " +
|
||||
"uploadSubmitMs=${elapsedMs(uploadStart)} totalCpuMs=${elapsedMs(start)}")
|
||||
@@ -147,6 +153,7 @@ internal class MgcSharpen {
|
||||
|
||||
private external fun nativeSharpenRgba8(
|
||||
rgba: ByteBuffer, scratch: ByteBuffer, width: Int, height: Int, snr: Float, attenuation: Float,
|
||||
curves: FloatArray,
|
||||
): Int
|
||||
private external fun nativeUploadRgba8(pbo: Int, texture: Int, width: Int, height: Int): Boolean
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.hinnka.mycamera.raw
|
||||
|
||||
import com.hinnka.mycamera.processor.PhotonSharpenCurve
|
||||
import com.hinnka.mycamera.processor.PhotonSharpenTuning
|
||||
|
||||
/** Converts Photon's named controls to the original Halide [point, frequency, coordinate] ABI. */
|
||||
internal object MgcSharpenCurveBuilder {
|
||||
data class Selection(
|
||||
val points: FloatArray,
|
||||
val lowerSnr: Float,
|
||||
val upperSnr: Float,
|
||||
val interpolation: Float,
|
||||
) {
|
||||
val mainGains: List<Float>
|
||||
get() = (0..2).map { points[17 + 5 * it] / points[2 + 5 * it] }
|
||||
}
|
||||
|
||||
fun build(snr: Float, tuning: PhotonSharpenTuning): Selection {
|
||||
require(snr.isFinite() && snr > 0f)
|
||||
val nodes = tuning.nodes
|
||||
// Revalidate the list at the boundary: callers may have supplied a mutable List.
|
||||
require(nodes.isNotEmpty() && nodes.zipWithNext().all { (a, b) -> a.snr < b.snr })
|
||||
val upperIndex = nodes.indexOfFirst { it.snr >= snr }
|
||||
.let { if (it < 0) nodes.lastIndex else it }
|
||||
val lowerIndex = if (snr >= nodes.last().snr) nodes.lastIndex
|
||||
else (upperIndex - 1).coerceAtLeast(0)
|
||||
val lower = nodes[lowerIndex]
|
||||
val upper = nodes[upperIndex]
|
||||
val t = if (lowerIndex == upperIndex) 0f else (snr - lower.snr) / (upper.snr - lower.snr)
|
||||
val result = FloatArray(30)
|
||||
for (band in 0..2) {
|
||||
val a = points(lower[band])
|
||||
val b = points(upper[band])
|
||||
val amount = tuning.amount[band]
|
||||
for (point in 0..4) {
|
||||
// Interpolate actual x/y points, never gains or an amount-scaled SNR fraction.
|
||||
val x = a[point] * (1f - t) + b[point] * t
|
||||
val y = a[point + 5] * (1f - t) + b[point + 5] * t
|
||||
val adjusted = when (amount) {
|
||||
0f -> x
|
||||
1f -> y // Preserve the original default's Float rounding.
|
||||
else -> x + amount * (y - x)
|
||||
}
|
||||
require(x.isFinite() && adjusted.isFinite() && adjusted >= 0f) {
|
||||
"Invalid sharpen response at band=$band point=$point amount=$amount"
|
||||
}
|
||||
result[point + 5 * band] = x
|
||||
result[point + 5 * band + 15] = adjusted
|
||||
require(point == 0 || x > result[point - 1 + 5 * band]) {
|
||||
"Sharpen input knots collapsed at band=$band point=$point"
|
||||
}
|
||||
}
|
||||
}
|
||||
return Selection(result, lower.snr, upper.snr, t)
|
||||
}
|
||||
|
||||
private fun points(curve: PhotonSharpenCurve): FloatArray = with(curve) {
|
||||
floatArrayOf(
|
||||
0f, lowContrastInput, mainContrastInput, highContrastInput, highContrastInput + tailSpan,
|
||||
0f, lowContrastInput * lowContrastGain, mainContrastInput * mainContrastGain,
|
||||
highContrastInput * highContrastGain, highContrastInput * highContrastGain + tailSpan,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import com.hinnka.mycamera.camera.RawBlackBorderCrop
|
||||
import com.hinnka.mycamera.data.ContentRepository
|
||||
import com.hinnka.mycamera.lut.ChromaDenoiseAlgorithm
|
||||
import com.hinnka.mycamera.ml.SharedDepthEstimator
|
||||
import com.hinnka.mycamera.processor.PhotonQualitySharpenTuning
|
||||
import com.hinnka.mycamera.processor.GlesGpuCompletion
|
||||
import com.hinnka.mycamera.processor.GlesGpuScheduler
|
||||
import com.hinnka.mycamera.processor.GlesComputeWorkGroup
|
||||
@@ -228,6 +229,7 @@ class RawDemosaicProcessor {
|
||||
mgcDenoiseTuningSnr = baseMetadata?.mgcDenoiseTuningSnr,
|
||||
mgcSharpenTuningSnr = baseMetadata?.mgcSharpenTuningSnr,
|
||||
mgcSharpenAttenuationScale = baseMetadata?.mgcSharpenAttenuationScale,
|
||||
rawMaxQualityTuningEnabled = baseMetadata?.rawMaxQualityTuningEnabled ?: false,
|
||||
rawMaxQualityTuningSensorAreaMm2 = baseMetadata?.rawMaxQualityTuningSensorAreaMm2,
|
||||
rotation = dngRawData.rotation,
|
||||
profileGainTableMap = baseMetadata?.profileGainTableMap
|
||||
@@ -1816,6 +1818,7 @@ class RawDemosaicProcessor {
|
||||
rawWhiteLevelMode: String? = null,
|
||||
rawCustomWhiteLevel: Float? = null,
|
||||
sharpeningValue: Float = 0f,
|
||||
processLocalQualityTuningEnabled: Boolean? = null,
|
||||
processLocalQualityTuningSensorAreaMm2: Float? = null,
|
||||
denoiseValue: Float? = null,
|
||||
chromaDenoiseValue: Float? = null,
|
||||
@@ -1864,6 +1867,7 @@ class RawDemosaicProcessor {
|
||||
rawWhiteLevelMode = rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = rawCustomWhiteLevel,
|
||||
sharpeningValue = sharpeningValue,
|
||||
processLocalQualityTuningEnabled = processLocalQualityTuningEnabled,
|
||||
processLocalQualityTuningSensorAreaMm2 = processLocalQualityTuningSensorAreaMm2,
|
||||
denoiseValue = denoiseValue,
|
||||
chromaDenoiseValue = chromaDenoiseValue,
|
||||
@@ -1915,6 +1919,7 @@ class RawDemosaicProcessor {
|
||||
rawWhitePointCorrection: Float = 0f,
|
||||
applyLensShadingCorrection: Boolean = true,
|
||||
sharpeningValue: Float = 0f,
|
||||
processLocalQualityTuningEnabled: Boolean? = null,
|
||||
processLocalQualityTuningSensorAreaMm2: Float? = null,
|
||||
denoiseValue: Float? = null,
|
||||
chromaDenoiseValue: Float? = null,
|
||||
@@ -1957,6 +1962,7 @@ class RawDemosaicProcessor {
|
||||
rawWhitePointCorrection = rawWhitePointCorrection,
|
||||
applyLensShadingCorrection = applyLensShadingCorrection,
|
||||
sharpeningValue = sharpeningValue,
|
||||
processLocalQualityTuningEnabled = processLocalQualityTuningEnabled,
|
||||
processLocalQualityTuningSensorAreaMm2 = processLocalQualityTuningSensorAreaMm2,
|
||||
denoiseValue = denoiseValue,
|
||||
chromaDenoiseValue = chromaDenoiseValue,
|
||||
@@ -2059,6 +2065,7 @@ class RawDemosaicProcessor {
|
||||
rawWhiteLevelMode: String? = null,
|
||||
rawCustomWhiteLevel: Float? = null,
|
||||
sharpeningValue: Float = 0f,
|
||||
processLocalQualityTuningEnabled: Boolean? = null,
|
||||
processLocalQualityTuningSensorAreaMm2: Float? = null,
|
||||
processLocalMgcSharpenTuningSnr: Float? = null,
|
||||
processLocalMgcSharpenAttenuationScale: Float? = null,
|
||||
@@ -2109,6 +2116,7 @@ class RawDemosaicProcessor {
|
||||
rawWhiteLevelMode = rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = rawCustomWhiteLevel,
|
||||
sharpeningValue = sharpeningValue,
|
||||
processLocalQualityTuningEnabled = processLocalQualityTuningEnabled,
|
||||
processLocalQualityTuningSensorAreaMm2 = processLocalQualityTuningSensorAreaMm2,
|
||||
processLocalMgcSharpenTuningSnr = processLocalMgcSharpenTuningSnr,
|
||||
processLocalMgcSharpenAttenuationScale = processLocalMgcSharpenAttenuationScale,
|
||||
@@ -2172,6 +2180,7 @@ class RawDemosaicProcessor {
|
||||
rawWhiteLevelMode: String? = null,
|
||||
rawCustomWhiteLevel: Float? = null,
|
||||
sharpeningValue: Float = 0f,
|
||||
processLocalQualityTuningEnabled: Boolean? = null,
|
||||
processLocalQualityTuningSensorAreaMm2: Float? = null,
|
||||
denoiseValue: Float? = null,
|
||||
chromaDenoiseValue: Float? = null,
|
||||
@@ -2270,6 +2279,7 @@ class RawDemosaicProcessor {
|
||||
rawWhiteLevelMode = rawWhiteLevelMode,
|
||||
rawCustomWhiteLevel = rawCustomWhiteLevel,
|
||||
sharpeningValue = sharpeningValue,
|
||||
processLocalQualityTuningEnabled = processLocalQualityTuningEnabled,
|
||||
processLocalQualityTuningSensorAreaMm2 = processLocalQualityTuningSensorAreaMm2,
|
||||
denoiseValue = denoiseValue,
|
||||
chromaDenoiseValue = chromaDenoiseValue,
|
||||
@@ -2329,6 +2339,7 @@ class RawDemosaicProcessor {
|
||||
rawWhiteLevelMode: String? = null,
|
||||
rawCustomWhiteLevel: Float? = null,
|
||||
sharpeningValue: Float = 0f,
|
||||
processLocalQualityTuningEnabled: Boolean? = null,
|
||||
processLocalQualityTuningSensorAreaMm2: Float? = null,
|
||||
processLocalMgcSharpenTuningSnr: Float? = null,
|
||||
processLocalMgcSharpenAttenuationScale: Float? = null,
|
||||
@@ -2585,6 +2596,8 @@ class RawDemosaicProcessor {
|
||||
)
|
||||
actualMetadata = actualMetadata?.let {
|
||||
it.copy(
|
||||
rawMaxQualityTuningEnabled =
|
||||
processLocalQualityTuningEnabled ?: it.rawMaxQualityTuningEnabled,
|
||||
rawMaxQualityTuningSensorAreaMm2 =
|
||||
processLocalQualityTuningSensorAreaMm2 ?: it.rawMaxQualityTuningSensorAreaMm2,
|
||||
)
|
||||
@@ -7641,7 +7654,7 @@ class RawDemosaicProcessor {
|
||||
stackCompletionTimeline: GpuStackCompletionTimeline? = null,
|
||||
) {
|
||||
val sliderValue = RawSharpeningDefaults.normalize(sharpeningValue)
|
||||
val algorithmStrength = RawSharpeningDefaults.toAlgorithmStrength(sliderValue)
|
||||
val algorithmStrength = RawSharpeningDefaults.toMgcStrength(sliderValue)
|
||||
val runtimeAttenuation = metadata.mgcSharpenAttenuationScale?.also { attenuation ->
|
||||
check(attenuation.isFinite() && attenuation >= 0f) {
|
||||
"MGC sharpen attenuation is invalid: $attenuation"
|
||||
@@ -7667,11 +7680,16 @@ class RawDemosaicProcessor {
|
||||
height = metadata.height,
|
||||
snr = snr,
|
||||
attenuation = effectiveStrength,
|
||||
tuning = PhotonQualitySharpenTuning.resolve(metadata.rawMaxQualityTuningEnabled),
|
||||
)
|
||||
} else {
|
||||
// Non-MGC GPU-only sources may have no reference RAW/statistics available.
|
||||
PLog.w(TAG, "Original MGC sharpen unavailable: missing reference SNR; using GLES USM")
|
||||
renderSharpenPass(metadata, effectiveStrength, inputTextureId)
|
||||
renderSharpenPass(
|
||||
metadata,
|
||||
RawSharpeningDefaults.toAlgorithmStrength(sliderValue) * runtimeAttenuation,
|
||||
inputTextureId,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -172,7 +172,9 @@ data class RawMetadata(
|
||||
val maxAnalogSensitivity: Int = 0,
|
||||
val shutterSpeed: Long = 0L,
|
||||
val aperture: Float = FALLBACK_APERTURE_F_NUMBER,
|
||||
/** Capture-scoped sensor area when RAWmax quality tuning is enabled; null uses defaults. */
|
||||
/** Capture-scoped quality choice; fixed sharpening does not require sensor geometry. */
|
||||
val rawMaxQualityTuningEnabled: Boolean = false,
|
||||
/** Optional area for the enabled quality mode's fusion/denoise fit only. */
|
||||
val rawMaxQualityTuningSensorAreaMm2: Float? = null,
|
||||
/** CameraCharacteristics sensor geometry used only for adaptive noise-model estimation. */
|
||||
val sensorPhysicalWidthMm: Float = 0f,
|
||||
@@ -1221,6 +1223,7 @@ data class RawMetadata(
|
||||
if (mgcDenoiseTuningSnr != other.mgcDenoiseTuningSnr) return false
|
||||
if (mgcSharpenTuningSnr != other.mgcSharpenTuningSnr) return false
|
||||
if (mgcSharpenAttenuationScale != other.mgcSharpenAttenuationScale) return false
|
||||
if (rawMaxQualityTuningEnabled != other.rawMaxQualityTuningEnabled) return false
|
||||
if (rawMaxQualityTuningSensorAreaMm2 != other.rawMaxQualityTuningSensorAreaMm2) return false
|
||||
if (rotation != other.rotation) return false
|
||||
|
||||
@@ -1260,6 +1263,7 @@ data class RawMetadata(
|
||||
result = 31 * result + (mgcDenoiseTuningSnr?.hashCode() ?: 0)
|
||||
result = 31 * result + (mgcSharpenTuningSnr?.hashCode() ?: 0)
|
||||
result = 31 * result + (mgcSharpenAttenuationScale?.hashCode() ?: 0)
|
||||
result = 31 * result + rawMaxQualityTuningEnabled.hashCode()
|
||||
result = 31 * result + (rawMaxQualityTuningSensorAreaMm2?.hashCode() ?: 0)
|
||||
result = 31 * result + (rotation ?: 0)
|
||||
return result
|
||||
|
||||
@@ -11,7 +11,10 @@ object RawSharpeningDefaults {
|
||||
DEFAULT_STRENGTH
|
||||
}
|
||||
|
||||
/** Maps the persisted/UI 0..1 RAW sharpening control onto the algorithm's 0..2 domain. */
|
||||
/** MGC: the default UI value is unity, so 0/.4/1 map to 0/1/2.5. */
|
||||
fun toMgcStrength(value: Float): Float = normalize(value) / DEFAULT_STRENGTH
|
||||
|
||||
/** Maps the persisted/UI 0..1 control onto the legacy GLES USM 0..2 domain. */
|
||||
fun toAlgorithmStrength(value: Float): Float =
|
||||
normalize(value) * ALGORITHM_STRENGTH_SCALE
|
||||
}
|
||||
@@ -112,13 +112,19 @@ static void VerifyConversions() {
|
||||
"U12 to U8 exact");
|
||||
puts("PASS all 16777216 RGB8 colors, SIMD tails, all uint16 output values");
|
||||
}
|
||||
static void Sharpen(int16_t *in, uint16_t *out, int w, int h, float snr = 20) {
|
||||
SharpenCurveSelection curves;
|
||||
const float scales[] = {1, 1, 1};
|
||||
Check(BuildDefaultSharpenCurves(snr, scales, &curves), "curves");
|
||||
static void Sharpen(int16_t *in, uint16_t *out, int w, int h) {
|
||||
// Frozen SNR20 generic fixture for adapter/tile/performance checks.
|
||||
// Production defaults now live only in PhotonSharpenTuning; the JVM regression
|
||||
// checks cover its table and SNR interpolation independently of this probe.
|
||||
const float curves[] = {
|
||||
0, .05f, 1, 2, 3, 0, .03f, 1, 2, 3, 0, .02f, 1, 2, 3,
|
||||
0, .05f * 1.6f, 3.2f, 2, 3,
|
||||
0, .03f * 1.3f, 2.6f, 2, 3,
|
||||
0, .02f, 1.4f, 2, 3,
|
||||
};
|
||||
const float corner_correction[] = {0, 0, 0};
|
||||
int rc =
|
||||
RunSharpenTo16Bit(in, w, h, curves.curves,
|
||||
curves.relative_corner_acutance_correction, .8f, out);
|
||||
RunSharpenTo16Bit(in, w, h, curves, corner_correction, .8f, out);
|
||||
if (rc)
|
||||
fprintf(stderr, "kernel size=%dx%d rc=%d\n", w, h, rc);
|
||||
Check(rc == 0, "kernel");
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.hinnka.mycamera.processor
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import kotlin.math.log2
|
||||
|
||||
@@ -129,8 +131,26 @@ class PhotonSensorSizeTuningTest {
|
||||
assertNull(PhotonSensorSizeTuning.forSensorAreaMm2(0f))
|
||||
assertEquals(PhotonSensorSizeTuning.Parameters(), PhotonSensorSizeTuning.resolve(null))
|
||||
assertEquals(PhotonSensorSizeTuning.Parameters(), PhotonSensorSizeTuning.resolve(Float.NaN))
|
||||
assertEquals(emptyMap<String, String>(), PhotonSensorSizeTuning.captureProperties(true, null))
|
||||
assertEquals(emptyMap<String, String>(), PhotonSensorSizeTuning.captureProperties(true, Float.POSITIVE_INFINITY))
|
||||
for (area in listOf(null, Float.POSITIVE_INFINITY)) {
|
||||
val properties = PhotonSensorSizeTuning.captureProperties(true, area)
|
||||
assertTrue(PhotonSensorSizeTuning.enabledFromProperties(properties))
|
||||
assertNull(PhotonSensorSizeTuning.areaFromProperties(properties))
|
||||
assertEquals(1, properties.size)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fixedSharpeningUsesCaptureChoiceEvenWhenSensorAreaIsMissing() {
|
||||
for (area in listOf(null, Float.NaN, 0f, 1f, 20.48f, 32f, 128f, 1000f)) {
|
||||
val enabled = PhotonSensorSizeTuning.captureProperties(true, area)
|
||||
val disabled = PhotonSensorSizeTuning.captureProperties(false, area)
|
||||
assertSame(PhotonQualitySharpenTuning.FIXED, PhotonQualitySharpenTuning.resolve(
|
||||
PhotonSensorSizeTuning.enabledFromProperties(enabled),
|
||||
))
|
||||
assertSame(PhotonCoreImagingTuning.sharpen, PhotonQualitySharpenTuning.resolve(
|
||||
PhotonSensorSizeTuning.enabledFromProperties(disabled),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package com.hinnka.mycamera.raw
|
||||
|
||||
import com.hinnka.mycamera.processor.PhotonSharpenBands
|
||||
import com.hinnka.mycamera.processor.PhotonSharpenCurve
|
||||
import com.hinnka.mycamera.processor.PhotonSharpenSnrNode
|
||||
import com.hinnka.mycamera.processor.PhotonSharpenTuning
|
||||
import com.hinnka.mycamera.processor.PhotonQualitySharpenTuning
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Test
|
||||
|
||||
class MgcSharpenCurveBuilderTest {
|
||||
@Test
|
||||
fun qualityPresetUsesSabreDomainAndP0Slot0FrequencyAmounts() {
|
||||
val tuning = PhotonQualitySharpenTuning.FIXED
|
||||
assertEquals(listOf(2f, 8f, 16f), tuning.nodes.map { it.snr })
|
||||
val selected = MgcSharpenCurveBuilder.build(16f, tuning)
|
||||
assertEquals(.0002f, selected.points[1], 0f)
|
||||
assertEquals(.02f, selected.points[2], 0f)
|
||||
assertEquals(.03f, selected.points[3], 1e-8f)
|
||||
assertEquals(1.405f, selected.points[4], 1e-7f)
|
||||
// Patched AGC: gain=10*1.125, low gain=1.375, depth2=1.375;
|
||||
// resulting y2=.225, y3=.24625, y4=1.62125, before sharpness A.
|
||||
val expectedY = floatArrayOf(
|
||||
0f,
|
||||
.0002f + .34375f * (.000275f - .0002f),
|
||||
.02f + .34375f * (.225f - .02f),
|
||||
.03f + .34375f * (.24625f - .03f),
|
||||
1.405f + .34375f * (1.62125f - 1.405f),
|
||||
)
|
||||
assertArrayEquals(expectedY, selected.points.copyOfRange(15, 20), 2e-7f)
|
||||
assertEquals(.67558594f, selected.mainGains[1], 1e-6f)
|
||||
// Global sharpness B=0 makes frequency 2 identity at every SNR.
|
||||
for (snr in listOf(.1f, 2f, 5f, 8f, 12f, 16f, 100f)) {
|
||||
val points = MgcSharpenCurveBuilder.build(snr, tuning).points
|
||||
assertArrayEquals(points.copyOfRange(10, 15), points.copyOfRange(25, 30), 0f)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun defaultSnr20PreservesOriginalNativeCurveAndHalideLayout() {
|
||||
val actual = MgcSharpenCurveBuilder.build(20f, PhotonSharpenTuning.DEFAULT)
|
||||
// Original MGC generic SNR20 five-point curves, x blocks followed by y blocks.
|
||||
val expected = floatArrayOf(
|
||||
0f, .05f, 1f, 2f, 3f, 0f, .03f, 1f, 2f, 3f, 0f, .02f, 1f, 2f, 3f,
|
||||
0f, .05f * 1.6f, 3.2f, 2f, 3f,
|
||||
0f, .03f * 1.3f, 2.6f, 2f, 3f,
|
||||
0f, .02f, 1.4f, 2f, 3f,
|
||||
)
|
||||
assertArrayEquals(expected, actual.points, 0f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun snrInterpolationUsesConvertedPointsRatherThanInterpolatedGains() {
|
||||
val selected = MgcSharpenCurveBuilder.build(30f, PhotonSharpenTuning.DEFAULT)
|
||||
assertEquals(20f, selected.lowerSnr, 0f)
|
||||
assertEquals(40f, selected.upperSnr, 0f)
|
||||
assertEquals(.5f, selected.interpolation, 0f)
|
||||
assertEquals(.035f, selected.points[1], 1e-8f)
|
||||
assertEquals(.066f, selected.points[16], 1e-8f)
|
||||
assertEquals(4.2f, selected.points[17], 1e-6f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bandAmountWorksAtNodesBetweenNodesAndBeyondTableAndDoesNotMoveOtherBands() {
|
||||
val original = PhotonSharpenTuning.DEFAULT
|
||||
val adjusted = original.copy(amount = PhotonSharpenBands(.5f, 0f, 1f))
|
||||
for (snr in listOf(.1f, 5f, 7.5f, 20f, 30f, 80f, 160f)) {
|
||||
val baseline = MgcSharpenCurveBuilder.build(snr, original)
|
||||
val actual = MgcSharpenCurveBuilder.build(snr, adjusted)
|
||||
assertEquals(baseline.interpolation, actual.interpolation, 0f)
|
||||
for (point in 0..4) {
|
||||
val x = baseline.points[point]
|
||||
val y = baseline.points[point + 15]
|
||||
assertEquals(x + .5f * (y - x), actual.points[point + 15], 0f)
|
||||
assertEquals(actual.points[point + 5], actual.points[point + 20], 0f)
|
||||
assertEquals(baseline.points[point + 25], actual.points[point + 25], 0f)
|
||||
}
|
||||
assertArrayEquals(baseline.points.copyOfRange(0, 15), actual.points.copyOfRange(0, 15), 0f)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun customContrastKnotsAndTailReachTheKernelWithoutHiddenDepthCoupling() {
|
||||
val curve = PhotonSharpenCurve(
|
||||
lowContrastInput = .1f, lowContrastGain = 1.5f,
|
||||
mainContrastInput = .5f, mainContrastGain = 3f,
|
||||
highContrastInput = 1.5f, highContrastGain = 1.25f,
|
||||
tailSpan = .5f,
|
||||
)
|
||||
val tuning = PhotonSharpenTuning(listOf(PhotonSharpenSnrNode(10f, curve, curve, curve)))
|
||||
val selected = MgcSharpenCurveBuilder.build(40f, tuning)
|
||||
assertArrayEquals(floatArrayOf(0f, .1f, .5f, 1.5f, 2f), selected.points.copyOfRange(0, 5), 0f)
|
||||
assertArrayEquals(floatArrayOf(0f, .15f, 1.5f, 1.875f, 2.375f), selected.points.copyOfRange(15, 20), 0f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun invalidLocalTuningIsRejectedRatherThanSilentlyClamped() {
|
||||
assertThrows(IllegalArgumentException::class.java) { PhotonSharpenCurve(lowContrastInput = 1f) }
|
||||
assertThrows(IllegalArgumentException::class.java) { PhotonSharpenCurve(highContrastGain = Float.NaN) }
|
||||
assertThrows(IllegalArgumentException::class.java) { PhotonSharpenBands(band1 = -1f) }
|
||||
assertThrows(IllegalArgumentException::class.java) { PhotonSharpenTuning(emptyList()) }
|
||||
val node = PhotonSharpenTuning.DEFAULT.nodes.first()
|
||||
assertThrows(IllegalArgumentException::class.java) { PhotonSharpenTuning(listOf(node, node)) }
|
||||
assertThrows(IllegalArgumentException::class.java) { MgcSharpenCurveBuilder.build(0f, PhotonSharpenTuning.DEFAULT) }
|
||||
val suppress = PhotonSharpenCurve(lowContrastGain = 0f)
|
||||
val invalidExtrapolation = PhotonSharpenTuning(
|
||||
listOf(PhotonSharpenSnrNode(5f, suppress, suppress, suppress)),
|
||||
PhotonSharpenBands(2f, 2f, 2f),
|
||||
)
|
||||
assertThrows(IllegalArgumentException::class.java) { MgcSharpenCurveBuilder.build(5f, invalidExtrapolation) }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user