1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2012-02-04
7  * Description : a tool to create panorama by fusion of several images.
8  *               This type is based on pto file format described here:
9  *               hugin.sourceforge.net/docs/nona/nona.txt, and
10  *               on pto files produced by Hugin's tools.
11  *
12  * Copyright (C) 2012-2015 by Benjamin Girault <benjamin dot girault at gmail dot com>
13  *
14  * This program is free software; you can redistribute it
15  * and/or modify it under the terms of the GNU General
16  * Public License as published by the Free Software Foundation;
17  * either version 2, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * ============================================================ */
25 
26 #ifndef DIGIKAM_PTO_TYPE_H
27 #define DIGIKAM_PTO_TYPE_H
28 
29 // Qt includes
30 
31 #include <QPoint>
32 #include <QVector>
33 #include <QSize>
34 #include <QString>
35 #include <QRect>
36 #include <QStringList>
37 #include <QPair>
38 #include <QTextStream>
39 
40 namespace Digikam
41 {
42 
43 struct PTOType
44 {
45     struct Project
46     {
47         struct FileFormat
48         {
49             typedef enum
50             {
51                 PNG,
52                 TIFF,
53                 TIFF_m,
54                 TIFF_multilayer,
55                 JPEG
56             } FileType;
57 
58             typedef enum
59             {
60                 PANO_NONE,
61                 LZW,
62                 DEFLATE
63             } CompressionMethod;
64 
65             FileType          fileType;
66             unsigned char     quality;            ///< for JPEG
67             CompressionMethod compressionMethod;  ///< for TIFF
68             bool              cropped;            ///< for TIFF
69             bool              savePositions;      ///< for TIFF
70 
FileFormatPTOType::Project::FileFormat71             FileFormat()
72               : fileType         (JPEG),
73                 quality          (90),
74                 compressionMethod(LZW),
75                 cropped          (false),
76                 savePositions    (false)
77             {
78             }
79         };
80 
81         typedef enum
82         {
83             RECTILINEAR      = 0,
84             CYLINDRICAL      = 1,
85             EQUIRECTANGULAR  = 2,
86             FULLFRAMEFISHEYE = 3
87         } ProjectionType;
88 
89         typedef enum
90         {
91             UINT8,
92             UINT16,
93             FLOAT
94         } BitDepth;
95 
96         QStringList    previousComments;
97         QSize          size;
98         QRect          crop;
99         ProjectionType projection;
100         double         fieldOfView;
101         FileFormat     fileFormat;
102         double         exposure;
103         bool           hdr;
104         BitDepth       bitDepth;
105         int            photometricReferenceId;
106         QStringList    unmatchedParameters;
107 
ProjectPTOType::Project108         Project()
109           : size                  (0, 0),
110             crop                  (0, 0, 0, 0),
111             projection            (RECTILINEAR),
112             fieldOfView           (0),
113             exposure              (0),
114             hdr                   (false),
115             bitDepth              (UINT8),
116             photometricReferenceId(0)
117         {
118         }
119     };
120 
121     // --------------------------------------------------------------------------------------
122 
123     struct Stitcher
124     {
125         typedef enum
126         {
127             POLY3           = 0,
128             SPLINE16        = 1,
129             SPLINE36        = 2,
130             SINC256         = 3,
131             SPLINE64        = 4,
132             BILINEAR        = 5,
133             NEARESTNEIGHBOR = 6,
134             SINC1024        = 7
135         } Interpolator;
136 
137         typedef enum
138         {
139             SLOW,
140             MEDIUM = 1,
141             FAST   = 2
142         } SpeedUp;
143 
144         QStringList  previousComments;
145         double       gamma;
146         Interpolator interpolator;
147         SpeedUp      speedUp;
148         double       huberSigma;
149         double       photometricHuberSigma;
150         QStringList  unmatchedParameters;
151 
StitcherPTOType::Stitcher152         Stitcher()
153           : gamma                (1),
154             interpolator         (POLY3),
155             speedUp              (FAST),
156             huberSigma           (0),
157             photometricHuberSigma(0)
158         {
159         }
160     };
161 
162     // --------------------------------------------------------------------------------------
163 
164     struct Mask
165     {
166         typedef enum
167         {
168             NEGATIVE      = 0,
169             POSTIVE       = 1,
170             NEGATIVESTACK = 2,
171             POSITIVESTACK = 3,
172             NEGATIVELENS  = 4
173         } MaskType;
174 
175         QStringList   previousComments;
176         MaskType      type;
177         QList<QPoint> hull;
178     };
179 
180     // --------------------------------------------------------------------------------------
181 
182     struct Optimization
183     {
184         typedef enum
185         {
186             LENSA,
187             LENSB,
188             LENSC,
189             LENSD,
190             LENSE,
191             LENSHFOV,
192             LENSYAW,
193             LENSPITCH,
194             LENSROLL,
195             EXPOSURE,
196             WBR,
197             WBB,
198             VA,
199             VB,
200             VC,
201             VD,
202             VX,
203             VY,
204             RA,
205             RB,
206             RC,
207             RD,
208             RE,
209             UNKNOWN
210         } Parameter;
211 
212         QStringList previousComments;
213         Parameter   parameter;
214     };
215 
216     // --------------------------------------------------------------------------------------
217 
218     struct Image
219     {
220         template<typename T>
221         struct LensParameter
222         {
LensParameterPTOType::Image::LensParameter223             LensParameter()
224               : value      (T()),
225                 referenceId(-1)
226             {
227             }
228 
LensParameterPTOType::Image::LensParameter229             explicit LensParameter(const T& v)
230               : value      (v),
231                 referenceId(-1)
232             {
233             }
234 
235             T   value;
236             int referenceId;
237 
238             friend QTextStream& operator<<(QTextStream& qts, const LensParameter<T>& p)
239             {
240                if (p.referenceId == -1)
241                {
242                    qts << p.value;
243                }
244                else
245                {
246                    qts << "=" << p.referenceId;
247                }
248 
249                return qts;
250             }
251         };
252 
253         typedef enum
254         {
255             RECTILINEAR      = 0,
256             PANORAMIC        = 1,
257             CIRCULARFISHEYE  = 2,
258             FULLFRAMEFISHEYE = 3,
259             EQUIRECTANGULAR  = 4
260         } LensProjection;
261 
262         typedef enum
263         {
264             PANO_NONE              = 0,
265             RADIAL                 = 1,
266             FLATFIELD              = 2,
267             PROPORTIONNALRADIAL    = 5,
268             PROPORTIONNALFLATFIELD = 6
269         } VignettingMode;
270 
271         QStringList                   previousComments;
272         QSize                         size;
273         int                           id;
274         QList<Mask>                   masks;
275         QList<Optimization>           optimizationParameters;
276         LensProjection                lensProjection;
277         LensParameter<double>         fieldOfView;
278         double                        yaw;
279         double                        pitch;
280         double                        roll;
281         LensParameter<double>         lensBarrelCoefficientA;
282         LensParameter<double>         lensBarrelCoefficientB;
283         LensParameter<double>         lensBarrelCoefficientC;
284         LensParameter<int>            lensCenterOffsetX;
285         LensParameter<int>            lensCenterOffsetY;
286         LensParameter<int>            lensShearX;
287         LensParameter<int>            lensShearY;
288         LensParameter<double>         exposure;
289         LensParameter<double>         whiteBalanceRed;
290         LensParameter<double>         whiteBalanceBlue;
291         LensParameter<VignettingMode> vignettingMode;
292         LensParameter<double>         vignettingCorrectionI;      ///< Va
293         LensParameter<double>         vignettingCorrectionJ;      ///< Vb
294         LensParameter<double>         vignettingCorrectionK;      ///< Vc
295         LensParameter<double>         vignettingCorrectionL;      ///< Vd
296         LensParameter<int>            vignettingOffsetX;
297         LensParameter<int>            vignettingOffsetY;
298         QString                       vignettingFlatfieldImageName;
299         LensParameter<double>         photometricEMoRA;
300         LensParameter<double>         photometricEMoRB;
301         LensParameter<double>         photometricEMoRC;
302         LensParameter<double>         photometricEMoRD;
303         LensParameter<double>         photometricEMoRE;
304         double                        mosaicCameraPositionX;
305         double                        mosaicCameraPositionY;
306         double                        mosaicCameraPositionZ;
307         double                        mosaicProjectionPlaneYaw;
308         double                        mosaicProjectionPlanePitch;
309         QRect                         crop;
310         LensParameter<int>            stackNumber;
311         QString                       fileName;
312         QStringList                   unmatchedParameters;
313 
ImagePTOType::Image314         Image()
315           : size                      (0, 0),
316             id                        (0),
317             lensProjection            (RECTILINEAR),
318             fieldOfView               (0),
319             yaw                       (0),
320             pitch                     (0),
321             roll                      (0),
322             lensBarrelCoefficientA    (0),
323             lensBarrelCoefficientB    (0),
324             lensBarrelCoefficientC    (0),
325             lensCenterOffsetX         (0),
326             lensCenterOffsetY         (0),
327             lensShearX                (0),
328             lensShearY                (0),
329             exposure                  (0),
330             whiteBalanceRed           (1),
331             whiteBalanceBlue          (1),
332             vignettingMode            (PANO_NONE),
333             vignettingCorrectionI     (0),
334             vignettingCorrectionJ     (0),
335             vignettingCorrectionK     (0),
336             vignettingCorrectionL     (0),
337             vignettingOffsetX         (0),
338             vignettingOffsetY         (0),
339             photometricEMoRA          (0),
340             photometricEMoRB          (0),
341             photometricEMoRC          (0),
342             photometricEMoRD          (0),
343             photometricEMoRE          (0),
344             mosaicCameraPositionX     (0),
345             mosaicCameraPositionY     (0),
346             mosaicCameraPositionZ     (0),
347             mosaicProjectionPlaneYaw  (0),
348             mosaicProjectionPlanePitch(0),
349             crop                      (0, 0, 0, 0),
350             stackNumber               (0)
351         {
352         }
353     };
354 
355     // --------------------------------------------------------------------------------------
356 
357     struct ControlPoint
358     {
359         QStringList previousComments;
360         int         image1Id;
361         int         image2Id;
362         double      p1_x;
363         double      p1_y;
364         double      p2_x;
365         double      p2_y;
366         int         type;                   // FIXME: change that for an enum if possible
367         QStringList unmatchedParameters;
368     };
369 
370     // --------------------------------------------------------------------------------------
371 
372     enum
373     {
374         PRE_V2014,
375         V2014
376     } version;
377 
PTOTypePTOType378     PTOType()
379       : version(PRE_V2014)
380     {
381     }
382 
PTOTypePTOType383     explicit PTOType(const QString& version)
384       : version((version.split(QLatin1Char('.'))[0].toInt() >= 2014) ? V2014
385                                                                      : PRE_V2014)
386     {
387     }
388 
389     bool createFile(const QString& filepath);
390 
391 /*
392     NOTE: Work in progress
393     QPair<double, int>  standardDeviation(int image1Id, int image2Id);
394     QPair<double, int>  standardDeviation(int imageId);
395     QPair<double, int>  standardDeviation();
396 */
397 
398     Project             project;
399     Stitcher            stitcher;
400     QVector<Image>      images;
401     QList<ControlPoint> controlPoints;
402     QStringList         lastComments;
403 };
404 
405 } // namespace Digikam
406 
407 #endif // DIGIKAM_PTO_TYPE_H
408