Skip to content

Commit

Permalink
apply clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
KaoCC committed Dec 29, 2021
1 parent 264a0b5 commit 4b7314c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 51 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Checks: 'performance-*,portability-*,readability-*,clang-analyzer-*,bugprone-*,modernize-*'
2 changes: 1 addition & 1 deletion HDRI/debevec_weight.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace HDRI {
class debevec_weight final {
public:
debevec_weight() = delete;
[[nodiscard]] static double get_weight(const int index) noexcept;
[[nodiscard]] static double get_weight(const int z_value) noexcept;
[[nodiscard]] static std::size_t get_size() noexcept;
};

Expand Down
54 changes: 27 additions & 27 deletions HDRI/hdr_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@
namespace {

template <typename weight_function>
cv::Mat construct_radiance(const std::vector<HDRI::raw_image> &image_files, const std::array<cv::Mat, 3UL> &curves,
weight_function &&weighting, const std::vector<double> &expo) noexcept {
auto construct_radiance(const std::vector<HDRI::raw_image> &image_files, const std::array<cv::Mat, 3UL> &curves,
weight_function &&weighting, const std::vector<double> &expo) noexcept -> cv::Mat {
// Size
const auto width = image_files[0UL].get_width();
const auto height = image_files[0UL].get_height();

// Radiance
cv::Mat radiance(height, width, CV_32FC3);

for (auto idx = 0UL; idx < std::size(curves); ++idx) { // r, g, b
for (auto y = 0UL; y < height; ++y) {
for (auto x = 0UL; x < width; ++x) {
double weighted_sum = 0.0;
double result = 0.0;
for (int idx = 0; idx < std::size(curves); ++idx) { // r, g, b
for (auto y = 0; y < height; ++y) {
for (auto x = 0; x < width; ++x) {
auto weighted_sum = 0.0;
auto result = 0.0;

// loop over all images
for (auto k = 0UL; k < std::size(image_files); ++k) {
const auto color = static_cast<int>(image_files[k].get_image_data().at<cv::Vec3b>(y, x)[idx]);
const double w = std::invoke(std::forward<weight_function>(weighting), color);

result += static_cast<double>(w * (curves[idx].at<double>(color, 0) - std::log(expo[k])));
result += w * (curves[idx].at<double>(color, 0) - std::log(expo[k]));
weighted_sum += w;
}

if (weighted_sum < std::numeric_limits<double>::epsilon() &&
weighted_sum > -std::numeric_limits<double>::epsilon()) { // near 0.0
radiance.at<cv::Vec3f>(y, x)[idx] = 0;
radiance.at<cv::Vec3f>(y, x)[idx] = 0.0F;
} else {
radiance.at<cv::Vec3f>(y, x)[idx] = std::exp(result / weighted_sum);
radiance.at<cv::Vec3f>(y, x)[idx] = static_cast<float>(std::exp(result / weighted_sum));
}
}
}
Expand All @@ -49,24 +49,24 @@ cv::Mat construct_radiance(const std::vector<HDRI::raw_image> &image_files, cons
return radiance;
}

std::vector<cv::Mat> shrink_images(const std::vector<HDRI::raw_image> &input) noexcept {
auto shrink_images(const std::vector<HDRI::raw_image> &input) noexcept -> std::vector<cv::Mat> {
std::vector<cv::Mat> out;
out.reserve(std::size(input));

constexpr size_t ratio = 50UL;
constexpr auto ratio = 50;

for (const auto &img : input) {
const auto &image_data = img.get_image_data();

size_t resized_col = image_data.cols / ratio;
size_t resized_row = image_data.rows / ratio;
auto resized_col = image_data.cols / ratio;
auto resized_row = image_data.rows / ratio;

if (resized_col < 15UL) {
resized_col = 15UL;
if (resized_col < 15) {
resized_col = 15;
}

if (resized_row < 15UL) {
resized_row = 15UL;
if (resized_row < 15) {
resized_row = 15;
}

cv::Mat shrink_mat;
Expand All @@ -79,17 +79,17 @@ std::vector<cv::Mat> shrink_images(const std::vector<HDRI::raw_image> &input) no
return out;
}

std::vector<std::vector<pixel>> generate_raw_pixel(const std::vector<cv::Mat> &shrink_mat) noexcept {
auto generate_raw_pixel(const std::vector<cv::Mat> &shrink_mat) noexcept -> std::vector<std::vector<pixel>> {
const auto width = shrink_mat[0UL].size().width;
const auto height = shrink_mat[0UL].size().height;

std::vector<std::vector<pixel>> pixels(shrink_mat.size());

for (auto idx = 0UL; idx < shrink_mat.size(); ++idx) {
pixels[idx].resize(width * height);
pixels[idx].resize(static_cast<size_t>(width) * height);

for (auto y = 0UL; y < height; ++y) {
for (auto x = 0UL; x < width; ++x) {
for (auto y = 0; y < height; ++y) {
for (auto x = 0; x < width; ++x) {
pixels[idx][y * width + x].b = shrink_mat[idx].at<cv::Vec3b>(y, x)[0UL];
pixels[idx][y * width + x].g = shrink_mat[idx].at<cv::Vec3b>(y, x)[1UL];
pixels[idx][y * width + x].r = shrink_mat[idx].at<cv::Vec3b>(y, x)[2UL];
Expand All @@ -100,8 +100,8 @@ std::vector<std::vector<pixel>> generate_raw_pixel(const std::vector<cv::Mat> &s
return pixels;
}

std::array<std::vector<std::vector<int>>, 3UL> convert_to_z(const std::vector<std::vector<pixel>> &pixel,
const size_t image_size, const size_t num_images) noexcept {
auto convert_to_z(const std::vector<std::vector<pixel>> &pixel, const size_t image_size,
const size_t num_images) noexcept -> std::array<std::vector<std::vector<int>>, 3UL> {
std::array<std::vector<std::vector<int>>, 3UL> z_values; // r, g, b

for (auto &z_colors : z_values) {
Expand Down Expand Up @@ -141,12 +141,12 @@ hdr_image::hdr_image(const std::vector<raw_image> &raw_images) {
compute_radiance(raw_images, exposure);
}

const std::array<cv::Mat, 3UL> &hdr_image::get_curves() const noexcept { return curves; }
auto hdr_image::get_curves() const noexcept -> const std::array<cv::Mat, 3UL> & { return curves; }

void hdr_image::compute_curves(const std::vector<HDRI::raw_image> &raw_images,
const std::vector<double> &exposure) noexcept {
const auto shrink_mat = shrink_images(raw_images);
std::vector<std::vector<pixel>> raw_pixel = generate_raw_pixel(shrink_mat);
const auto raw_pixel = generate_raw_pixel(shrink_mat);

// convert
const auto z_values = convert_to_z(raw_pixel, shrink_mat[0UL].total(), shrink_mat.size());
Expand All @@ -171,6 +171,6 @@ void hdr_image::compute_radiance(const std::vector<HDRI::raw_image> &raw_images,
radiance = construct_radiance(raw_images, curves, debevec_weight::get_weight, exposure);
}

const cv::Mat &hdr_image::get_radiance() const noexcept { return radiance; }
auto hdr_image::get_radiance() const noexcept -> const cv::Mat & { return radiance; }

} // namespace HDRI
9 changes: 5 additions & 4 deletions HDRI/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ void output_curve(const cv::Mat &curve) noexcept {

std::ofstream fout("out_curve.txt");

for (auto q = 0UL; q < height; ++q) {
for (auto p = 0UL; p < width; ++p) {
for (auto q = 0; q < height; ++q) {
for (auto p = 0; p < width; ++p) {
fout << curve.at<double>(q, p) << std::endl;
}
}
}

std::vector<HDRI::raw_image> load_raw_images(const std::string &base_path, const std::string &file_name) noexcept {
auto load_raw_images(const std::string &base_path, const std::string &file_name) noexcept
-> std::vector<HDRI::raw_image> {
const auto full_path = base_path + file_name;

std::ifstream input(full_path);
Expand Down Expand Up @@ -93,7 +94,7 @@ std::vector<HDRI::raw_image> load_raw_images(const std::string &base_path, const

} // namespace

int main(int argc, char *argv[]) {
auto main(int argc, char *argv[]) -> int {
constexpr auto default_base_path{"../InputImage/"};
constexpr auto default_files{"list.txt"};

Expand Down
4 changes: 2 additions & 2 deletions HDRI/raw_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace HDRI {

raw_image::raw_image(std::string file_name, const double shutter_speed)
: image_data{cv::imread(file_name)}, expo{1.0 / shutter_speed}, name{std::move(file_name)} {
raw_image::raw_image(const std::string& file_name, const double shutter_speed)
: image_data{cv::imread(file_name)}, expo{1.0 / shutter_speed} {
if (image_data.empty()) {
std::cerr << "Fail to load: " + file_name << std::endl;
throw std::runtime_error("Fail to load" + file_name);
Expand Down
3 changes: 1 addition & 2 deletions HDRI/raw_image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace HDRI {

class raw_image final {
public:
explicit raw_image(std::string file_name, const double shutter_speed);
explicit raw_image(const std::string &file_name, const double shutter_speed);

raw_image(const raw_image &) noexcept = default;
raw_image(raw_image &&) noexcept = default;
Expand All @@ -27,7 +27,6 @@ class raw_image final {

private:
cv::Mat image_data;
std::string name;
double expo;
};

Expand Down
30 changes: 15 additions & 15 deletions HDRI/tone_map_algo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,56 @@

namespace {

constexpr float rgb_to_luminance(const float r, const float g, const float b) noexcept {
return 0.2126f * r + 0.7152f * g + 0.0722f * b;
constexpr auto rgb_to_luminance(const float r, const float g, const float b) noexcept -> float {
return 0.2126F * r + 0.7152F * g + 0.0722F * b;
}

} // namespace

namespace HDRI {

auto reinhard_tone_map_algo(const cv::Mat &input_radiance) noexcept -> cv::Mat {
constexpr float epsilon = 0.00001f;
constexpr float a = 0.18f; // from paper
constexpr auto epsilon = 0.00001F;
constexpr auto a = 0.18F; // from paper

// color space transform
cv::Mat lumi(input_radiance.size(), CV_32FC1);
for (auto y = 0U; y < input_radiance.size().height; ++y) {
for (auto x = 0U; x < input_radiance.size().width; ++x) {
for (auto y = 0; y < input_radiance.size().height; ++y) {
for (auto x = 0; x < input_radiance.size().width; ++x) {
lumi.at<float>(y, x) =
rgb_to_luminance(input_radiance.at<cv::Vec3f>(y, x)[2], input_radiance.at<cv::Vec3f>(y, x)[1],
input_radiance.at<cv::Vec3f>(y, x)[0]);
}
}

double Lw_bar = 0.0;
auto Lw_bar = 0.0F;

// loop over all values in the Mat
for (auto y = 0U; y < input_radiance.size().height; ++y) {
for (auto x = 0U; x < input_radiance.size().width; ++x) {
for (auto y = 0; y < input_radiance.size().height; ++y) {
for (auto x = 0; x < input_radiance.size().width; ++x) {
Lw_bar += std::log(lumi.at<float>(y, x) + epsilon); // from paper
}
}

std::cerr << "Lw_bar: " << Lw_bar << std::endl;

const size_t N = input_radiance.total();
const auto N = static_cast<float>(input_radiance.total());

// Equation 1 in the paper is wrong. The division by N should be placed
// before the summation, not outside the exponentiation.
Lw_bar = std::exp(Lw_bar / N);

const float coeff = a / Lw_bar;
const auto coeff = a / Lw_bar;

const float L_white = 1.7f; // test
const auto L_white = 1.7F; // test

// compute Ld
cv::Mat Ld(input_radiance.size(), CV_32FC1);

for (auto y = 0; y < input_radiance.size().height; ++y) {
for (auto x = 0; x < input_radiance.size().width; ++x) {
float L = coeff * lumi.at<float>(y, x); // Ld = (a / Lw_bar ) * (Lw(x,y))
Ld.at<float>(y, x) = L * (1.0f + L / (L_white * L_white)) / (1.0f + L);
auto L = coeff * lumi.at<float>(y, x); // Ld = (a / Lw_bar ) * (Lw(x,y))
Ld.at<float>(y, x) = L * (1.0F + L / (L_white * L_white)) / (1.0F + L);

if (L > L_white) {
Ld.at<float>(y, x) = 1;
Expand All @@ -62,7 +62,7 @@ auto reinhard_tone_map_algo(const cv::Mat &input_radiance) noexcept -> cv::Mat {
}

cv::Mat output_image(input_radiance.size(), CV_8UC3);
for (auto idx = 0U; idx < 3U; ++idx) { // rgb
for (auto idx = 0; idx < 3; ++idx) { // rgb
for (auto y = 0; y < input_radiance.size().height; ++y) {
for (auto x = 0; x < input_radiance.size().width; ++x) {
output_image.at<cv::Vec3b>(y, x)[idx] = cv::saturate_cast<uchar>(
Expand Down

0 comments on commit 4b7314c

Please sign in to comment.