1 /*******************************************************************************
2 **
3 ** Photivo
4 **
5 ** Copyright (C) 2008 Jos De Laender <jos.de_laender@telenet.be>
6 ** Copyright (C) 2010-2012 Michael Munzert <mail@mm-log.com>
7 ** Copyright (C) 2012-2013 Bernd Schoeler <brjohn@brother-john.net>
8 **
9 ** This file is part of Photivo.
10 **
11 ** Photivo is free software: you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License version 3
13 ** as published by the Free Software Foundation.
14 **
15 ** Photivo is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ** GNU General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with Photivo.  If not, see <http://www.gnu.org/licenses/>.
22 **
23 *******************************************************************************/
24 #ifndef PTCURVE_H
25 #define PTCURVE_H
26 
27 #include "ptStorable.h"
28 
29 #include <array>
30 #include <vector>
31 #include <utility>
32 
33 class ptImage;
34 
35 typedef std::pair<double,double> TAnchor;
36 typedef std::vector<TAnchor>     TAnchorList;
37 
38 class ptCurve: public ptStorable {
39 public:
40   // Don’t mess with the enum values!
41   enum TInterpolation { LinearInterpol = 0, SplineInterpol = 1, CosineInterpol = 2 };
42   enum TType          { FullPrecalcType = 0, AnchorType = 1 };
43   enum TMask {
44     NoMask       = 0,
45     GammaMask    = 1,
46     LumaMask     = 2,
47     ChromaMask   = 4,
48     AChannelMask = 8,
49     BChannelMask = 16
50   };
51   typedef QFlags<TMask> TMasks;
52 
53 public:
54   /*! \name Static helper functions that return common null curves. *//*! @{*/
55   static TAnchorList diagonalNull();
56   static TAnchorList horizontalMidNull();
57   static TAnchorList horizontalQuarterNull();
58   /*! @}*/
59 
60 public:
61   ptCurve();
62   ptCurve(const TAnchorList   &AAnchors,
63           const bool           AImmediateCalc = true);
64   ptCurve(const TAnchorList    &ANullAnchors,
65           const TMasks         &ASupportedMasks,
66           const TMask          &ACurrentMask,
67           const TInterpolation &AInterpolType   = SplineInterpol);
68   ~ptCurve();
69 
70   void            calcCurve();
71   void            set(const ptCurve &AOther);
72   void            reset();
73   void            setNullCurve(const TAnchorList &AAnchors);
74   void            setFromAnchors(const TAnchorList &AAnchors);
75   void            setAnchor(const int AIdx, const TAnchor &APoint);
76   void            setAnchorY(const int AIdx, const float y);
77   void            setFromFunc(double(*Function)(double r, double Arg1, double Arg2),
78                               double Arg1,
79                               double Arg2);
80   int             readCurveFile(const QString &AFileName,
81                                 const bool     AOnlyAnchors);
82 
83   /*! \name Standard setters and getters. *//*! @{*/
anchors()84   TAnchorList        *anchors        ()       { return &FAnchors; }
anchorCount()85   int                 anchorCount    () const { return FAnchors.size(); }
mask()86   TMask               mask           () const { return FCurrentMask; }
supportedMasks()87   TMasks              supportedMasks () const { return FSupportedMasks; }
88   void                setMask        (const TMask AMask);
curveType()89   TType               curveType      () const { return FCurveType; }
90   bool                isNull         () const;
interpolType()91   TInterpolation      interpolType   () const { return FInterpolType; }
92   void                setInterpolType(const TInterpolation AInterpolType);
93   /*! @}*/
94 
95 
96   /*! 16bit Lookup table between original channel value (array indexes)
97       and curve processed value (array values). */
98   std::array<uint16_t, 0x10000> Curve;
99 
100 protected:
101   TConfigStore doStoreConfig(const QString &APrefix) const;
102   void         doLoadConfig(const TConfigStore &AConfig, const QString &APrefix);
103 
104 private:
105   void            calcCosineCurve();
106   void            calcLinearCurve();
107   void            calcSplineCurve();
108   void            setCurveType(const TType AType);
109 
110   // Spline functions for spline interpolated anchor points.
111   double *d3_np_fs        (int n, double a[], double b[]);
112   double *spline_cubic_set(int n, const std::vector<double> t, const std::vector<double> y,
113                            int ibcbeg, double ybcbeg, int ibcend, double ybcend );
114   double  spline_cubic_val(int n, const std::vector<double> t, double tval,
115                            const std::vector<double> y, double ypp[], double *ypval, double *yppval);
116 
117   TAnchorList         FAnchors;
118   TAnchorList         FNullAnchors;
119   TType               FCurveType;   // use setCurveType() to change it
120   TMask               FCurrentMask;
121   TMasks              FSupportedMasks;
122   TInterpolation      FInterpolType;
123   QString             FFileName;  // only used for the FullPrecalc curve type
124 
125 public:
126   static double Sigmoidal(double r, double Threshold, double Contrast);
127   static double GammaTool(double r, double Gamma, double Linearity);
128   static double DeltaGammaTool(double r, double Gamma, double Linearity);
129   static double InverseGammaSRGB(double r, double Dummy1, double Dummy2);
130 
131 #ifdef PT_CREATE_CURVES_PROJECT
132   static double GammaBT709(double r, double Dummy1, double Dummy2);
133   static double GammaSRGB(double r, double Dummy1, double Dummy2);
134   static double GammaPure22(double r, double Dummy1, double Dummy2);
135   // Read the anchors from a simple file in the format X0 Y0 \n X1 Y1 ...
136   // Returns 0 on success.
137   short ReadAnchors(const char *FileName);
138   // More complete reading and writing function (compatible).
139   // Header is a free text that is inserted as comment to describe
140   // the curve.
141   short WriteCurve(const char* FileName,const char *Header = NULL);
142 #endif
143 
144 };
145 
146 //------------------------------------------------------------------------------
147 /*! Qt macro that defines \c operator|() for \c TComponents. */
148 Q_DECLARE_OPERATORS_FOR_FLAGS(ptCurve::TMasks)
149 
150 #endif // PTCURVE_H
151