fix: hdrnet + dehazeAndDha

This commit is contained in:
zhoubinjia committed 2026-09-03 12:29:48 +08:00
1 parent f476c23af0
commit f5577f9119
32 files changed
+1468 -2773

No files matched your search

+96
View File
@@ -0,0 +1,96 @@
#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
// Exercise the production HDRNet evaluator and Dehaze/DHA curve implementation directly.
#include "../app/src/main/cpp/dng_hdrnet_pgtm_jni.cpp"
namespace {
void TestDisabledCurveIsIdentity() {
std::vector<uint32_t> haze(kDehazeHistogramSize, 0);
std::vector<uint32_t> highlight(kHighlightHistogramSize, 0);
int sample_count = 0;
for (int index = 0; index < 4096; ++index) {
AddDehazeHistogramSample(
RgbSample{0.2f, 0.4f, 0.6f}, &haze, &highlight, &sample_count);
}
const DehazeCurve curve =
EstimateDehazeCurve(haze, highlight, sample_count, 0.0f, 0.0f);
for (int index = 0; index <= 100; ++index) {
const float value = static_cast<float>(index) / 100.0f;
assert(std::abs(MappedDehazeLuminance(value, curve) - value) < 1.0e-6f);
}
}
void TestHdrNetOutputFeedsWholeImageDehaze() {
constexpr int input_width = 256;
constexpr int input_height = 192;
constexpr int input_channels = 4;
constexpr int grid_width = 16;
constexpr int grid_height = 12;
constexpr int grid_depth = 8;
constexpr int coefficient_count = 2;
std::vector<float> model_input(
input_width * input_height * input_channels, 0.0f);
for (size_t offset = 0; offset < model_input.size(); offset += input_channels) {
model_input[offset] = 0.25f;
model_input[offset + 1] = 0.25f;
model_input[offset + 2] = 0.25f;
model_input[offset + 3] = 0.5f;
}
std::vector<float> coefficients(
grid_width * grid_height * grid_depth * coefficient_count, 0.0f);
const float guide_shifts[] = {0.0f};
const float guide_slopes[] = {1.0f};
std::vector<uint32_t> haze(kDehazeHistogramSize, 0);
std::vector<uint32_t> highlight(kHighlightHistogramSize, 0);
int sample_count = 0;
for (int y = 0; y < input_height; ++y) {
for (int x = 0; x < input_width; ++x) {
RgbSample hdr_rgb{};
assert(EvaluateHdrNetRgb(
coefficients.data(), grid_width, grid_height, grid_depth,
coefficient_count, model_input.data(), input_width, input_height,
input_channels, (x + 0.5f) / input_width, (y + 0.5f) / input_height,
2.0f, 0.25f, 8.0f, 0.02f, guide_shifts, guide_slopes, 1,
&hdr_rgb));
assert(std::abs(hdr_rgb.red - 0.25f) < 2.0e-6f);
AddDehazeHistogramSample(
hdr_rgb, &haze, &highlight, &sample_count);
}
}
assert(sample_count == input_width * input_height);
const DehazeCurve curve =
EstimateDehazeCurve(haze, highlight, sample_count, 1.0f, 1.0f);
const float render_gain = 0.25f / (0.25f + kHdrNetGainEpsilon);
const RgbSample hdr_rgb{
0.25f * render_gain,
0.25f * render_gain,
0.25f * render_gain,
};
const RgbSample composed = ApplyDehaze(hdr_rgb, curve);
const float baked_target = DehazedHdrNetTargetLuma(
0.25f, render_gain, RgbSample{0.25f, 0.25f, 0.25f}, 0.25f, curve);
assert(curve.sampled_pixel_count == input_width * input_height);
assert(std::abs(
composed.red - MappedDehazeLuminance(hdr_rgb.red, curve)) < 1.0e-6f);
assert(std::abs(composed.red - baked_target) < 1.0e-6f);
assert(composed.red > 0.25f);
constexpr float post_exposure_gain = 1.25f;
const float exposed_target = std::clamp(
baked_target * post_exposure_gain, 0.0f, 1.0f);
assert(std::abs(exposed_target - composed.red * post_exposure_gain) < 1.0e-6f);
}
} // namespace
int main() {
TestDisabledCurveIsIdentity();
TestHdrNetOutputFeedsWholeImageDehaze();
std::cout << "HDRNet Dehaze/DHA composition tests passed\n";
return 0;
}
-101
View File
@@ -1,101 +0,0 @@
#include <cassert>
#include <cmath>
#include <iostream>
#include <limits>
// Include the production implementation so this host test exercises the real state machine,
// Jacobian construction, damped solve, Broyden update, bounds, and fallback rather than a copy.
#include "../app/src/main/cpp/raw_legacy_auto_exposure_solver.cpp"
namespace {
MatchResult SyntheticMatch(
float highlight_error_ev,
float dynamic_range_error_ev,
float recommended_correction_ev) {
MatchResult match;
const float maximum_error = std::max(
std::abs(highlight_error_ev),
std::abs(dynamic_range_error_ev));
match.match_rate = maximum_error <= kMatchResidualToleranceEv ? 1.0f : 0.0f;
match.mean_absolute_log2_ratio = maximum_error;
match.median_log2_ratio = highlight_error_ev;
match.robust_log2_loss =
highlight_error_ev * highlight_error_ev +
dynamic_range_error_ev * dynamic_range_error_ev;
match.recommended_exposure_correction_ev = recommended_correction_ev;
match.span_error_ev = dynamic_range_error_ev;
match.short_target_correction_ev = -highlight_error_ev;
return match;
}
void TestShortAndRatioCoordinatesConverge() {
auto solver = HdrNetParameterSolver::Create(4.0f, 16.0f);
assert(solver != nullptr);
int submitted_count = 0;
while (const auto parameters = solver->NextParameters()) {
if (submitted_count == 1) {
assert(parameters->axis == HdrNetProbeAxis::kShortJacobian);
assert(std::abs(parameters->ratio_ev) < 1.0e-6f);
} else if (submitted_count == 2) {
assert(parameters->axis == HdrNetProbeAxis::kRatioJacobian);
assert(std::abs(parameters->short_ev) < 1.0e-6f);
}
constexpr float target_short_ev = 0.40f;
constexpr float target_ratio_ev = -0.30f;
const float short_delta = parameters->short_ev - target_short_ev;
const float ratio_delta = parameters->ratio_ev - target_ratio_ev;
const float highlight_error = short_delta + 0.20f * ratio_delta;
const float dynamic_range_error =
0.15f * short_delta + 0.80f * ratio_delta;
assert(solver->Submit(
parameters->short_ev,
parameters->ratio_ev,
SyntheticMatch(
highlight_error,
dynamic_range_error,
-highlight_error)));
++submitted_count;
assert(submitted_count <= kHdrNetMaximumCandidateCount);
}
assert(solver->HasResult());
const HdrNetProbe& result = solver->ResultProbe();
assert(std::abs(result.short_ev - 0.40f) <= 0.10f);
assert(std::abs(result.ratio_ev + 0.30f) <= 0.10f);
}
void TestUnidentifiableRatioFallsBackToGlobalExposure() {
auto solver = HdrNetParameterSolver::Create(4.0f, 16.0f);
assert(solver != nullptr);
int submitted_count = 0;
while (const auto parameters = solver->NextParameters()) {
constexpr float target_short_ev = 0.80f;
const float highlight_error = parameters->short_ev - target_short_ev;
assert(solver->Submit(
parameters->short_ev,
parameters->ratio_ev,
SyntheticMatch(
highlight_error,
0.0f,
target_short_ev - parameters->short_ev)));
++submitted_count;
assert(submitted_count <= kHdrNetMaximumCandidateCount);
}
assert(solver->HasResult());
assert(submitted_count >= 4);
const HdrNetProbe& result = solver->ResultProbe();
assert(std::abs(result.short_ev - 0.80f) <= 0.10f);
assert(std::abs(result.ratio_ev) < 1.0e-6f);
}
} // namespace
int main() {
TestShortAndRatioCoordinatesConverge();
TestUnidentifiableRatioFallsBackToGlobalExposure();
std::cout << "raw HDRNet parameter solver tests passed\n";
return 0;
}
@@ -0,0 +1,83 @@
#include <cassert>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <vector>
// Include the production implementation so this host test exercises the exact 8 x 6 weighting,
// portrait priority, +/-0.1 EV match intervals, and one-pass exposure selection.
#include "../app/src/main/cpp/raw_legacy_auto_exposure_solver.cpp"
namespace {
constexpr int kReferenceCode = 128;
jint GrayPixel(int code) {
const uint32_t channel = static_cast<uint32_t>(code & 0xff);
return static_cast<jint>(
0xff000000U | (channel << 16U) | (channel << 8U) | channel);
}
float ReferenceLuma() {
return *DisplayLinearLuma(GrayPixel(kReferenceCode));
}
std::unique_ptr<ExposureSolver> CreateSolver(
const std::vector<float>* portrait_weights = nullptr) {
std::vector<jint> pixels(kGridCellCount, GrayPixel(kReferenceCode));
return ExposureSolver::Create(
pixels.data(),
portrait_weights != nullptr ? portrait_weights->data() : nullptr,
kGridColumns,
kGridRows);
}
void TestUniformGridResolvesExactExposure() {
auto solver = CreateSolver();
assert(solver != nullptr);
std::vector<float> candidate(kGridCellCount, ReferenceLuma() * 0.5f);
const auto result = solver->SolveSingleGridExposure(
candidate.data(), kGridColumns, kGridRows, -4.0f, 4.0f);
assert(result.has_value());
assert(std::abs(result->exposure_ev - 1.0f) < 1.0e-4f);
assert(std::abs(result->match.match_rate - 1.0f) < 1.0e-6f);
}
void TestPortraitPriorityOutweighsBackground() {
std::vector<float> portrait_weights(kGridCellCount, 0.0f);
std::vector<float> candidate(kGridCellCount, ReferenceLuma() * 2.0f);
for (int y = 2; y <= 3; ++y) {
for (int x = 2; x <= 5; ++x) {
const int cell = y * kGridColumns + x;
portrait_weights[cell] = 1.0f;
candidate[cell] = ReferenceLuma() * 0.5f;
}
}
auto solver = CreateSolver(&portrait_weights);
assert(solver != nullptr);
const auto result = solver->SolveSingleGridExposure(
candidate.data(), kGridColumns, kGridRows, -4.0f, 4.0f);
assert(result.has_value());
assert(std::abs(result->exposure_ev - 1.0f) < 1.0e-4f);
assert(result->match.match_rate > 0.70f);
}
void TestHighlightHeadroomBoundsPositiveMatch() {
auto solver = CreateSolver();
assert(solver != nullptr);
std::vector<float> candidate(kGridCellCount, ReferenceLuma() * 0.5f);
const auto result = solver->SolveSingleGridExposure(
candidate.data(), kGridColumns, kGridRows, -4.0f, 0.25f);
assert(result.has_value());
assert(std::abs(result->exposure_ev - 0.25f) < 1.0e-6f);
}
} // namespace
int main() {
TestUniformGridResolvesExactExposure();
TestPortraitPriorityOutweighsBackground();
TestHighlightHeadroomBoundsPositiveMatch();
std::cout << "raw HDRNet post-exposure solver tests passed\n";
return 0;
}