1 #pragma once
2 
3 #ifndef VECTORIZERPARAMETERS_H
4 #define VECTORIZERPARAMETERS_H
5 
6 // TnzCore includes
7 #include "tpersist.h"
8 #include "tvectorimage.h"
9 
10 #undef DVAPI
11 #undef DVVAR
12 #ifdef TOONZLIB_EXPORTS
13 #define DVAPI DV_EXPORT_API
14 #define DVVAR DV_EXPORT_VAR
15 #else
16 #define DVAPI DV_IMPORT_API
17 #define DVVAR DV_IMPORT_VAR
18 #endif
19 
20 //=======================================================
21 
22 //    Forward declarations
23 
24 class TIStream;
25 class TOStream;
26 
27 //=======================================================
28 
29 //*****************************************************************************
30 //    VectorizerConfiguration  definition
31 //*****************************************************************************
32 
33 /*!
34   \brief    Provides a base container for vectorization options.
35 
36   \details  All vectorization options and informations are passed from higher
37             (like \p VectorizerPopup) to lower layers (\p VectorizerCore) of
38             the vectorization process inside a \p VectorizerConfiguration
39   variable.
40             This typically includes vectorization modes, various sensitivity and
41             accuracy parameters, and post-processing informations. This class
42             merely acts as a base parameters container (although no pure virtual
43             method is present) - meaning that every vectorization method
44   inherits
45             this base class to include its specific parameters.
46 
47   \sa       Classes \p OutlineConfiguration, \p CenterlineConfiguration,
48             \p VectorizerPopup, \p Vectorizer and \p VectorizerCore.
49 */
50 class DVAPI VectorizerConfiguration {
51 public:
52   bool m_outline;  //!< Vectorization mode between outline and centerline
53 
54   int m_threshold;  //!< Cut-out parameter to distinguish paper or painted
55                     //! background
56   //!  from recognizable strokes. A pixel whose tone (for colormaps)
57   //!  or HSV value is under \p m_threshold is considered ink-colored.
58   bool m_leaveUnpainted;  //!< Whether color recognition for areas should be
59                           //! applied
60 
61   TAffine m_affine;  //!< Affine transform applied to the vectorization results
62   double m_thickScale;  //!< Impose a thickness reduction by this ratio
63 
64   // Align stroke directions to be the same (clockwise, i.e. left to right as
65   // viewed from inside of the shape).
66   bool m_alignBoundaryStrokesDirection;
67 
68 public:
VectorizerConfiguration(bool outline)69   VectorizerConfiguration(bool outline)
70       : m_outline(outline)
71       , m_threshold(200)
72       , m_leaveUnpainted(true)
73       , m_affine()
74       , m_thickScale(1.0)
75       , m_alignBoundaryStrokesDirection(false) {}
76 };
77 
78 //*****************************************************************************
79 //    CenterlineConfiguration  definition
80 //*****************************************************************************
81 
82 /*!
83   \brief    CenterlineConfiguration is the VectorizerConfiguration
84             specialization for the centerline vectorization method.
85 */
86 
87 class DVAPI CenterlineConfiguration final : public VectorizerConfiguration {
88 public:
89   /*!After threshold is done, raster zones of uniform ink or paint color whose
90 area is under this parameter
91 are discarded from vectorization process. This typically helps in reducing image
92 scannerization noise.*/
93   int m_despeckling;
94 
95   /*!Specifies the maximum thickness allowed for stroke detection. Large ink
96 regions can
97 therefore be painted with dark colors, rather than covered with very thick
98 strokes.
99 Observe that placing 0 here has the effect of an outline vectorization.*/
100   double m_maxThickness;
101 
102   /*!The m_accuracy dual (see VectorizerPopup). Specifies the user preference
103 between
104 accuracy of the identified strokes, and their simplicity. It generally does not
105 affect the vectorization speed.
106 For the conversion accuracy=>penalty, see
107 VectorizerParameters::getCenterlineConfiguration, defined in
108 vectorizerparameters.cpp
109 */
110   double m_penalty;
111 
112   //! Imposes a thickness reduction by this ratio, at the end of
113   //! VectorizerCore::vectorize method.
114   double m_thicknessRatio;
115 
116   /*!Includes the transparent frame of the image in the output. Region computing
117 can take
118 advantage of it to identify close-to-boundary regions.*/
119   bool m_makeFrame;
120 
121   /*!Assume that the source input is a full-color non-antialiased image (e.g.
122 painted level made with Retas).
123 This kind of image must be pre-processed and transformed to toonz-image */
124   bool m_naaSource;
125 
126 public:
127   /*!Constructs a VectorizerConfiguration with default values.
128 Default options consists of a full-thickness centerline vectorization, medium
129 accuracy settings,
130 with activated region computing and painting.*/
CenterlineConfiguration()131   CenterlineConfiguration()
132       : VectorizerConfiguration(false)
133       , m_despeckling(10)
134       , m_maxThickness(100.0)
135       , m_penalty(0.5)
136       , m_thicknessRatio(100.0)
137       , m_makeFrame(false)
138       , m_naaSource(false) {}
139 };
140 
141 //*****************************************************************************
142 //    NewOutlineConfiguration  definition
143 //*****************************************************************************
144 
145 /*!
146   \brief    NewOutlineConfiguration is the \p VectorizerConfiguration
147             specialization for the (new) outline vectorization method.
148 */
149 
150 class DVAPI NewOutlineConfiguration final : public VectorizerConfiguration {
151 public:
152   double m_adherenceTol;  //!< Adherence to contour corners
153   double m_angleTol;      //!< Angle-based corners tolerance
154   double m_relativeTol;   //!< Relative curvature radius-based corners tolerance
155   double m_mergeTol;      //!< Quadratics merging factor
156   int m_despeckling;  //!< Despeckling edge size (size x size gets despeckled)
157 
158   int m_maxColors;  //!< Maximum number of palette color from fullcolor
159                     //! quantization
160   TPixel32 m_transparentColor;  //!< Color to be recognized as transparent in
161                                 //! the fullcolor case
162 
163   int m_toneTol;  //!< Tone threshold to be used in the colormap case
164 
165 public:
NewOutlineConfiguration()166   NewOutlineConfiguration()
167       : VectorizerConfiguration(true)
168       , m_adherenceTol(0.5)
169       , m_angleTol(0.25)
170       , m_relativeTol(0.25)
171       , m_mergeTol(1.0)
172       , m_despeckling(4)
173       , m_maxColors(50)
174       , m_transparentColor(TPixel32::White)
175       , m_toneTol(128) {}
176 };
177 
178 //*****************************************************************************
179 //    OutlineConfiguration  definition
180 //*****************************************************************************
181 
182 /*!
183   \brief        OutlineConfiguration is the \p VectorizerConfiguration
184                 specialization for the outline vectorization method.
185 
186   \deprecated   Substituted by \p NewOutlineConfiguration, along
187                 with a different outline vectorization algorithm.
188 */
189 
190 class DVAPI OutlineConfiguration final : public VectorizerConfiguration {
191 public:
192   int m_smoothness;  // Outline
193 
194   //! User can specify a color to recognize as ink, in outline vectorization
195   //! mode.
196   TPixel32 m_inkColor;  // Outline
197   int m_strokeStyleId;  // Outline
198   //! Ignore colors separation for ink areas, in outline mode.
199   bool m_ignoreInkColors;       // Outline
200   double m_interpolationError;  // Outline
201 
202   double m_resolution;  // Outline
203 
204 public:
OutlineConfiguration()205   OutlineConfiguration()
206       : VectorizerConfiguration(true)
207       , m_smoothness(3)
208       , m_inkColor(TPixel::Black)
209       , m_strokeStyleId(1)
210       , m_ignoreInkColors(false)
211       , m_interpolationError(0.4)
212       , m_resolution(1) {}
213 };
214 
215 //*****************************************************************************
216 //    VectorizerParameters  declaration
217 //*****************************************************************************
218 
219 /*!
220   \brief    Container class for scene-bound vectorizer options.
221 */
222 
223 class DVAPI VectorizerParameters final : public TPersist {
224   PERSIST_DECLARATION(VectorizerParameters)
225 
226 public:
227   // Centerline options
228 
229   int m_cThreshold;
230   int m_cAccuracy;
231   int m_cDespeckling;
232   int m_cMaxThickness;
233 
234   double m_cThicknessRatioFirst;
235   double m_cThicknessRatioLast;
236 
237   bool m_cMakeFrame;
238   bool m_cPaintFill;
239   bool m_cAlignBoundaryStrokesDirection;
240   bool m_cNaaSource;
241 
242   // Outline options
243 
244   int m_oDespeckling;
245   int m_oAccuracy;
246   int m_oAdherence;
247   int m_oAngle;
248   int m_oRelative;
249   int m_oMaxColors;
250   int m_oToneThreshold;
251 
252   TPixel32 m_oTransparentColor;
253 
254   bool m_oPaintFill;
255   bool m_oAlignBoundaryStrokesDirection;
256 
257   // Generic data
258 
259   unsigned int
260       m_visibilityBits;  //!< Variable storing the visibility of each parameter
261                          //!  in the \p VectorizerPopup. Reset on version change
262                          //!  is considered acceptable in case params layout
263                          //!  does not match any more.
264   bool m_isOutline;      //!< Specifies the currently active parameters
265                          //!  selection (outline / centerline).
266 public:
267   VectorizerParameters();
268 
269   CenterlineConfiguration getCenterlineConfiguration(double weight) const;
270   NewOutlineConfiguration getOutlineConfiguration(double weight) const;
271 
272   /*! \brief    Builds a copy of the currently active configuration allocated
273           using the <I>new operator</I>.
274 
275 \param    weight  \a Position of the requested configuration in an abstract
276                   frame range, normalized to the <TT>[0, 1]</TT> range.       */
277 
getCurrentConfiguration(double weight)278   VectorizerConfiguration *getCurrentConfiguration(double weight) const {
279     return m_isOutline ? (VectorizerConfiguration *)new NewOutlineConfiguration(
280                              getOutlineConfiguration(weight))
281                        : (VectorizerConfiguration *)new CenterlineConfiguration(
282                              getCenterlineConfiguration(weight));
283   }
284 
285   void saveData(TOStream &os) override;
286   void loadData(TIStream &is) override;
287 };
288 
289 #endif  // VECTORIZERPARAMETERS_H
290