File indexing completed on 2025-01-05 03:56:31

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 28/08/2021
0007  * Description : Extraction of focus points by exiftool data - Nikon devices
0008  *
0009  * SPDX-FileCopyrightText: 2021-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2021 by Phuoc Khanh Le <phuockhanhnk94 at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "focuspoints_extractor.h"
0017 
0018 // Local includes
0019 
0020 #include "digikam_debug.h"
0021 
0022 namespace Digikam
0023 {
0024 
0025 // Internal function to create af point from meta data
0026 namespace NikonInternal
0027 {
0028 
0029 FocusPoint create_af_point(float imageWidth,
0030                            float imageHeight,
0031                            float afPointWidth,
0032                            float afPointHeight,
0033                            float af_x_position,
0034                            float af_y_position)
0035 {
0036     return FocusPoint(af_x_position / imageWidth,
0037                       af_y_position / imageHeight,
0038                       afPointWidth  / imageWidth,
0039                       afPointHeight / imageHeight,
0040                       FocusPoint::TypePoint::SelectedInFocus);
0041 }
0042 
0043 } // namespace NikonInternal
0044 
0045 // Main function to extract af point
0046 FocusPointsExtractor::ListAFPoints FocusPointsExtractor::getAFPoints_nikon() const
0047 {
0048     QString TagNameRoot = QLatin1String("MakerNotes.Nikon.Camera");
0049 
0050     // Filter model
0051 
0052     QString model       = findValue(QLatin1String("EXIF.IFD0.Camera.Model")).toString().toLower();
0053 
0054     if (!model.contains(QLatin1String("nikon z"), Qt::CaseInsensitive))
0055     {
0056         qCDebug(DIGIKAM_METAENGINE_LOG) << "FocusPointsExtractor: unsupported Nikon Camera.";
0057 
0058         return getAFPoints_exif();
0059     }
0060 
0061     // Get image size
0062 
0063     QVariant imageWidth, imageHeight;
0064 
0065     imageWidth = findValueFirstMatch(QStringList()
0066                                      << QLatin1String("MakerNotes.Nikon.Camera.AFImageWidth")
0067                                      << QLatin1String("EXIF.ExifIFD.Image.ExifImageWidth")
0068                                     );
0069 
0070     imageHeight = findValueFirstMatch(QStringList()
0071                                       << QLatin1String("MakerNotes.Nikon.Camera.AFImageHeight")
0072                                       << QLatin1String("EXIF.ExifIFD.Image.ExifImageHeight")
0073                                      );
0074 
0075     if (imageWidth.isNull() || imageHeight.isNull())
0076     {
0077         qCDebug(DIGIKAM_METAENGINE_LOG) << "FocusPointsExtractor: invalid Nikon image sizes.";
0078 
0079         return getAFPoints_exif();
0080     }
0081 
0082     setOriginalSize(QSize(imageWidth.toInt(), imageHeight.toInt()));
0083 
0084     // Get size of point
0085 
0086     QVariant afPointWidth  = findValue(TagNameRoot, QLatin1String("AFAreaWidth"));
0087     QVariant afPointHeight = findValue(TagNameRoot, QLatin1String("AFAreaHeight"));
0088 
0089     if ((afPointWidth.isNull()) || (afPointHeight.isNull()))
0090     {
0091         qCDebug(DIGIKAM_METAENGINE_LOG) << "FocusPointsExtractor: invalid sizes from Nikon makernotes.";
0092 
0093         return getAFPoints_exif();
0094     }
0095 
0096     // Get coordinate of af points
0097 
0098     QVariant af_x_position = findValue(TagNameRoot, QLatin1String("AFAreaXPosition"));
0099     QVariant af_y_position = findValue(TagNameRoot, QLatin1String("AFAreaYPosition"));
0100 
0101     if (af_x_position.isNull() || af_y_position.isNull())
0102     {
0103         qCDebug(DIGIKAM_METAENGINE_LOG) << "FocusPointsExtractor: invalid positions from Nikon makernotes.";
0104 
0105         return getAFPoints_exif();
0106     }
0107 
0108     qCDebug(DIGIKAM_METAENGINE_LOG) << "FocusPointsExtractor: Nikon Makernotes Focus Location:" << af_x_position
0109                                                                                                 << af_y_position
0110                                                                                                 << afPointWidth
0111                                                                                                 << afPointHeight;
0112 
0113     ListAFPoints points;
0114     FocusPoint afpoint = NikonInternal::create_af_point(
0115                                                         imageWidth.toFloat(),
0116                                                         imageHeight.toFloat(),
0117                                                         afPointWidth.toFloat(),
0118                                                         afPointHeight.toFloat(),
0119                                                         af_x_position.toFloat(),
0120                                                         af_y_position.toFloat()
0121                                                        );
0122 
0123     if (afpoint.getRect().isValid())
0124     {
0125         points << afpoint;
0126     }
0127 
0128     return points;
0129 }
0130 
0131 } // namspace Digikam