Use std::vector for LUTs.
authorCarl Hetherington <cth@carlh.net>
Thu, 5 May 2022 18:31:26 +0000 (20:31 +0200)
committerCarl Hetherington <cth@carlh.net>
Thu, 5 May 2022 19:09:40 +0000 (21:09 +0200)
12 files changed:
src/gamma_transfer_function.cc
src/gamma_transfer_function.h
src/identity_transfer_function.cc
src/identity_transfer_function.h
src/modified_gamma_transfer_function.cc
src/modified_gamma_transfer_function.h
src/rgb_xyz.cc
src/s_gamut3_transfer_function.cc
src/s_gamut3_transfer_function.h
src/transfer_function.cc
src/transfer_function.h
test/colour_conversion_test.cc

index 1875221e3ccc296c2b3087519fb9ee9cf8271b4f..4ec09afb0997403f5eb8fa508c2fe391f795ef1e 100644 (file)
 #include <cmath>
 
 
+using std::dynamic_pointer_cast;
 using std::pow;
 using std::shared_ptr;
-using std::dynamic_pointer_cast;
+using std::vector;
 using namespace dcp;
 
 
@@ -54,11 +55,11 @@ GammaTransferFunction::GammaTransferFunction (double gamma)
 }
 
 
-double *
+vector<double>
 GammaTransferFunction::make_lut (int bit_depth, bool inverse) const
 {
        int const bit_length = int(std::pow(2.0f, bit_depth));
-       auto lut = new double[bit_length];
+       auto lut = vector<double>(bit_length);
        double const gamma = inverse ? (1 / _gamma) : _gamma;
        for (int i = 0; i < bit_length; ++i) {
                lut[i] = pow(double(i) / (bit_length - 1), gamma);
index 8402d6af23004add106a03484f50bcfcd90873e8..bfd336df1a3ba5f1ab5beb4ca4af3b17eb5acdbf 100644 (file)
@@ -58,7 +58,7 @@ public:
        bool about_equal (std::shared_ptr<const TransferFunction> other, double epsilon) const override;
 
 protected:
-       double * make_lut (int bit_depth, bool inverse) const override;
+       std::vector<double> make_lut (int bit_depth, bool inverse) const override;
 
 private:
        double _gamma;
index c01e6698af19f05952c6d10cf2a1d593fa3aab36..c888bd3ec9dac142d786d46ede687d956675b24e 100644 (file)
 #include <cmath>
 
 
+using std::dynamic_pointer_cast;
 using std::pow;
 using std::shared_ptr;
-using std::dynamic_pointer_cast;
+using std::vector;
 using namespace dcp;
 
 
-double *
+vector<double>
 IdentityTransferFunction::make_lut (int bit_depth, bool) const
 {
        int const bit_length = int(std::pow(2.0f, bit_depth));
-       auto lut = new double[bit_length];
+       auto lut = vector<double>(bit_length);
        for (int i = 0; i < bit_length; ++i) {
                lut[i] = double(i) / (bit_length - 1);
        }
index de7a897a102f27e78fddd56f7ac62d9e29b9055a..3db71b301b61bcac9e35475f80d3c32f14d17824 100644 (file)
@@ -49,7 +49,7 @@ public:
        bool about_equal (std::shared_ptr<const TransferFunction> other, double epsilon) const override;
 
 protected:
-       double * make_lut (int bit_depth, bool inverse) const override;
+       std::vector<double> make_lut (int bit_depth, bool inverse) const override;
 };
 
 
index 039e9284e0b01a021f38a002489c3e4cf49b2b10..e2cc1e8677baecae36587f8c57e6f863d2173ef1 100644 (file)
 #include <cmath>
 
 
+using std::dynamic_pointer_cast;
 using std::pow;
 using std::shared_ptr;
-using std::dynamic_pointer_cast;
+using std::vector;
 using namespace dcp;
 
 
@@ -57,11 +58,11 @@ ModifiedGammaTransferFunction::ModifiedGammaTransferFunction (double power, doub
 }
 
 
-double *
+vector<double>
 ModifiedGammaTransferFunction::make_lut (int bit_depth, bool inverse) const
 {
        int const bit_length = int(std::pow(2.0f, bit_depth));
-       double* lut = new double[bit_length];
+       auto lut = vector<double>(bit_length);
        if (inverse) {
                double const threshold = _threshold / _B;
                for (int i = 0; i < bit_length; ++i) {
index 349048557808801055aa3dd6332dfc91f65d9198..d4e9cf97fa642de6160751cb9633394865a1c83b 100644 (file)
@@ -77,7 +77,7 @@ public:
        bool about_equal (std::shared_ptr<const TransferFunction>, double epsilon) const override;
 
 protected:
-       double * make_lut (int bit_depth, bool inverse) const override;
+       std::vector<double> make_lut (int bit_depth, bool inverse) const override;
 
 private:
        double _power;
index a8766b8e4000a565db972964ef1dda0263f91ab8..b8acc9b0db0563514e55c83eddc970751b1abd0d 100644 (file)
@@ -80,8 +80,8 @@ dcp::xyz_to_rgba (
        int* xyz_y = xyz_image->data (1);
        int* xyz_z = xyz_image->data (2);
 
-       double const * lut_in = conversion.out()->lut (12, false);
-       double const * lut_out = conversion.in()->lut (16, true);
+       auto lut_in = conversion.out()->lut (12, false);
+       auto lut_out = conversion.in()->lut (16, true);
        boost::numeric::ublas::matrix<double> const matrix = conversion.xyz_to_rgb ();
 
        double fast_matrix[9] = {
@@ -157,8 +157,8 @@ dcp::xyz_to_rgb (
        int* xyz_y = xyz_image->data (1);
        int* xyz_z = xyz_image->data (2);
 
-       double const * lut_in = conversion.out()->lut (12, false);
-       double const * lut_out = conversion.in()->lut (16, true);
+       auto lut_in = conversion.out()->lut (12, false);
+       auto lut_out = conversion.in()->lut (16, true);
        auto const matrix = conversion.xyz_to_rgb ();
 
        double fast_matrix[9] = {
@@ -276,8 +276,8 @@ dcp::rgb_to_xyz (
                double x, y, z;
        } d;
 
-       auto const * lut_in = conversion.in()->lut (12, false);
-       auto const * lut_out = conversion.out()->lut (16, true);
+       auto lut_in = conversion.in()->lut (12, false);
+       auto lut_out = conversion.out()->lut (16, true);
 
        /* This is is the product of the RGB to XYZ matrix, the Bradford transform and the DCI companding */
        double fast_matrix[9];
index edfbe2ad0429a3dc6e1c7d6f97e4852bcc40b2c8..b2e82b5144226bba59b553922ff34aaf8455fa3b 100644 (file)
 #include <cmath>
 
 
+using std::dynamic_pointer_cast;
 using std::pow;
 using std::shared_ptr;
-using std::dynamic_pointer_cast;
+using std::vector;
 using namespace dcp;
 
 
-double *
+vector<double>
 SGamut3TransferFunction::make_lut (int bit_depth, bool inverse) const
 {
        int const bit_length = int(std::pow(2.0f, bit_depth));
-       double* lut = new double[bit_length];
+       auto lut = vector<double>(bit_length);
        if (inverse) {
                for (int i = 0; i < bit_length; ++i) {
                        auto const p = static_cast<double>(i) / (bit_length - 1);
index 0d297e8b2d6a6ea925f8db84c0d817625b65c61c..790cd6b15d5fa0e59668ffa7a7f79e2917bc6b29 100644 (file)
@@ -49,7 +49,7 @@ public:
        bool about_equal (std::shared_ptr<const TransferFunction> other, double epsilon) const override;
 
 protected:
-       double * make_lut (int bit_depth, bool inverse) const override;
+       std::vector<double> make_lut (int bit_depth, bool inverse) const override;
 };
 
 
index 00968e2f633a1651a43f4e1d50147349f183a7a2..dd3512ec3d498fdeb58a312ef8fc883f15100d44 100644 (file)
 #include <cmath>
 
 
-using std::pow;
+using std::make_pair;
 using std::map;
 using std::pair;
-using std::make_pair;
+using std::pow;
 using std::shared_ptr;
+using std::vector;
 using namespace dcp;
 
 
-TransferFunction::~TransferFunction ()
-{
-       boost::mutex::scoped_lock lm (_mutex);
-
-       for (auto const& i: _luts) {
-               delete[] i.second;
-       }
-
-       _luts.clear ();
-}
-
-
-double const *
+vector<double> const&
 TransferFunction::lut (int bit_depth, bool inverse) const
 {
        boost::mutex::scoped_lock lm (_mutex);
index a52011ec003c844db49d1f74b466d3f2a417cdb5..046787335dd5ad9c49aaa98a61db1f4ea20da849 100644 (file)
@@ -44,6 +44,7 @@
 #include <boost/thread/mutex.hpp>
 #include <map>
 #include <memory>
+#include <vector>
 
 
 namespace dcp {
@@ -57,22 +58,19 @@ class TransferFunction
 public:
        TransferFunction () {}
 
-       TransferFunction (TransferFunction const&) = delete;
-       TransferFunction& operator= (TransferFunction const&) = delete;
-
-       virtual ~TransferFunction ();
+       virtual ~TransferFunction () {}
 
        /** @return A look-up table (of size 2^bit_depth) whose values range from 0 to 1 */
-       double const * lut (int bit_depth, bool inverse) const;
+       std::vector<double> const& lut (int bit_depth, bool inverse) const;
 
        virtual bool about_equal (std::shared_ptr<const TransferFunction> other, double epsilon) const = 0;
 
 protected:
        /** Make a LUT and return an array allocated by new */
-       virtual double * make_lut (int bit_depth, bool inverse) const = 0;
+       virtual std::vector<double> make_lut (int bit_depth, bool inverse) const = 0;
 
 private:
-       mutable std::map<std::pair<int, bool>, double*> _luts;
+       mutable std::map<std::pair<int, bool>, std::vector<double>> _luts;
        /** mutex to protect _luts */
        mutable boost::mutex _mutex;
 };
index 5a6bdd85fc434ee9c094b0c4ea54572f560008a5..c1e7fc1a2722459fe9c2c610ace174947c9fd65d 100644 (file)
@@ -44,7 +44,7 @@ using namespace dcp;
 static void
 check_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, bool inverse, float gamma)
 {
-       double const * lut = tf->lut (bit_depth, inverse);
+       auto lut = tf->lut (bit_depth, inverse);
        int const count = rint (pow (2.0, bit_depth));
 
        for (int i = 0; i < count; ++i) {
@@ -55,7 +55,7 @@ check_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, bool inverse,
 static void
 check_modified_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, bool inverse, double power, double threshold, double A, double B)
 {
-       double const * lut = tf->lut (bit_depth, inverse);
+       auto lut = tf->lut (bit_depth, inverse);
        int const count = rint (pow (2.0, bit_depth));
 
        for (int i = 0; i < count; ++i) {