File indexing completed on 2025-01-19 03:56:04

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 - XMP metadata
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 XmpInternal
0027 {
0028 
0029 FocusPoint create_af_point(float afPointWidth,
0030                            float afPointHeight,
0031                            float af_x_position,
0032                            float af_y_position)
0033 {
0034     return FocusPoint(af_x_position,
0035                       af_y_position,
0036                       afPointWidth,
0037                       afPointHeight,
0038                       FocusPoint::TypePoint::SelectedInFocus);
0039 }
0040 
0041 } // namespace XmpInternal
0042 
0043 // Main function to extract af point
0044 FocusPointsExtractor::ListAFPoints FocusPointsExtractor::getAFPoints_xmp() const
0045 {
0046     setAFPointsReadOnly(false);
0047 
0048     QString TagNameRoot    = QLatin1String("XMP.XMP-mwg-rs.Image");
0049     QString desc           = findValue(TagNameRoot, QLatin1String("RegionDescription")).toString();
0050 
0051     if (!desc.startsWith(QLatin1String("digikam")))
0052     {
0053         qCDebug(DIGIKAM_METAENGINE_LOG) << "FocusPointsExtractor: cannot find digiKam XMP namespace.";
0054 
0055         return ListAFPoints();
0056     }
0057 
0058     QVariant af_x_position = findValue(TagNameRoot, QLatin1String("RegionAreaX"));
0059     QVariant af_y_position = findValue(TagNameRoot, QLatin1String("RegionAreaY"));
0060 
0061     if (af_x_position.isNull() || af_y_position.isNull())
0062     {
0063         qCDebug(DIGIKAM_METAENGINE_LOG) << "FocusPointsExtractor: invalid positions from XMP.";
0064 
0065         return ListAFPoints();
0066     }
0067 
0068     QVariant afPointWidth  = findValue(TagNameRoot, QLatin1String("RegionAreaW"));
0069     QVariant afPointHeight = findValue(TagNameRoot, QLatin1String("RegionAreaH"));
0070 
0071     if (afPointWidth.isNull() || afPointHeight.isNull())
0072     {
0073         qCDebug(DIGIKAM_METAENGINE_LOG) << "FocusPointsExtractor: invalid sizes from XMP.";
0074 
0075         return ListAFPoints();
0076     }
0077 
0078     qCDebug(DIGIKAM_METAENGINE_LOG) << "FocusPointsExtractor: XMP Focus Location:" << af_x_position
0079                                                                                    << af_y_position
0080                                                                                    << afPointWidth
0081                                                                                    << afPointHeight;
0082 
0083     ListAFPoints points;
0084     FocusPoint afpoint = XmpInternal::create_af_point(
0085                                                       afPointWidth.toFloat(),
0086                                                       afPointHeight.toFloat(),
0087                                                       af_x_position.toFloat(),
0088                                                       af_y_position.toFloat()
0089                                                      );
0090 
0091     if (afpoint.getRect().isValid())
0092     {
0093         points << afpoint;
0094     }
0095 
0096     return points;
0097 }
0098 
0099 } // namespace Digikam