11#include " tone_mapping.hpp"
22#include < cstring>
3+ #include < memory>
4+
5+ // Tone mapping constants
6+ constexpr float MIN_LUMA_THRESHOLD = 0 .001f ; // Threshold to avoid division by near-zero luminance
7+ constexpr float REINHARD_LUMINANCE_SCALE = 1 .2f ; // Gentle luminance boost before tone mapping
38
49cv::Mat* apply_hdr_to_sdr_tone_mapping (
510 const cv::Mat* src,
@@ -41,13 +46,13 @@ cv::Mat* apply_hdr_to_sdr_tone_mapping(
4146
4247 // Handle alpha channel separately - alpha should NOT be tone mapped
4348 bool has_alpha = (channels == 4 );
44- cv::Mat* bgr_only = nullptr ;
45- cv::Mat* alpha_channel = nullptr ;
49+ std::unique_ptr< cv::Mat> bgr_only;
50+ std::unique_ptr< cv::Mat> alpha_channel;
4651 const cv::Mat* src_for_transform = src;
4752
4853 if (has_alpha) {
49- bgr_only = new cv::Mat (src->rows , src->cols , CV_8UC3);
50- alpha_channel = new cv::Mat (src->rows , src->cols , CV_8UC1);
54+ bgr_only = std::make_unique< cv::Mat> (src->rows , src->cols , CV_8UC3);
55+ alpha_channel = std::make_unique< cv::Mat> (src->rows , src->cols , CV_8UC1);
5156
5257 cv::Mat channels_split[4 ];
5358 cv::split (*src, channels_split);
@@ -56,11 +61,11 @@ cv::Mat* apply_hdr_to_sdr_tone_mapping(
5661 cv::merge (bgr_channels, 3 , *bgr_only);
5762 *alpha_channel = channels_split[3 ];
5863
59- src_for_transform = bgr_only;
64+ src_for_transform = bgr_only. get () ;
6065 }
6166
6267 // Apply Reinhard tone mapping
63- cv::Mat* dst_bgr = new cv::Mat (src_for_transform->rows , src_for_transform->cols , CV_8UC3);
68+ std::unique_ptr< cv::Mat> dst_bgr = std::make_unique< cv::Mat> (src_for_transform->rows , src_for_transform->cols , CV_8UC3);
6469
6570 // Apply luminance-based tone mapping to preserve color relationships
6671 // This prevents oversaturation by operating on brightness only
@@ -79,32 +84,26 @@ cv::Mat* apply_hdr_to_sdr_tone_mapping(
7984 float luma = 0 .2126f * r + 0 .7152f * g + 0 .0722f * b;
8085
8186 // Apply gentle Reinhard tone mapping to luminance only
82- float luma_scaled = luma * 1 . 2f ; // Gentle scaling
87+ float luma_scaled = luma * REINHARD_LUMINANCE_SCALE;
8388 float luma_mapped = luma_scaled / (1 .0f + luma_scaled);
8489
8590 // Scale RGB channels by the luminance ratio to preserve color
86- float ratio = (luma > 0 . 001f ) ? (luma_mapped / luma) : 0 .0f ;
91+ float ratio = (luma > MIN_LUMA_THRESHOLD ) ? (luma_mapped / luma) : 0 .0f ;
8792
8893 dst_row[idx + 0 ] = static_cast <uint8_t >(std::min (b * ratio * 255 .0f , 255 .0f ));
8994 dst_row[idx + 1 ] = static_cast <uint8_t >(std::min (g * ratio * 255 .0f , 255 .0f ));
9095 dst_row[idx + 2 ] = static_cast <uint8_t >(std::min (r * ratio * 255 .0f , 255 .0f ));
9196 }
9297 }
9398
94- // Merge alpha back if needed
95- cv::Mat* result = nullptr ;
9699 if (has_alpha) {
97- result = new cv::Mat (src->rows , src->cols , src->type ());
100+ auto result = std::make_unique< cv::Mat> (src->rows , src->cols , src->type ());
98101 cv::Mat bgr_channels_out[3 ];
99102 cv::split (*dst_bgr, bgr_channels_out);
100103 cv::Mat final_channels[4 ] = {bgr_channels_out[0 ], bgr_channels_out[1 ], bgr_channels_out[2 ], *alpha_channel};
101104 cv::merge (final_channels, 4 , *result);
102- delete bgr_only;
103- delete alpha_channel;
104- delete dst_bgr;
105+ return result.release ();
105106 } else {
106- result = dst_bgr;
107+ return dst_bgr. release () ;
107108 }
108-
109- return result;
110109}
0 commit comments