File indexing completed on 2025-01-19 03:55:57

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-02-23
0007  * Description : item metadata interface - libraw helpers.
0008  *
0009  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0011  * SPDX-FileCopyrightText: 2011      by Leif Huhn <leif at dkstat dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "dmetadata.h"
0018 
0019 // Qt includes
0020 
0021 #include <QString>
0022 #include <QScopedPointer>
0023 
0024 // Local includes
0025 
0026 #include "digikam_config.h"
0027 #include "digikam_debug.h"
0028 #include "drawinfo.h"
0029 #include "drawdecoder.h"
0030 #include "iccprofile.h"
0031 
0032 namespace Digikam
0033 {
0034 
0035 bool DMetadata::loadUsingRawEngine(const QString& filePath)
0036 {
0037     QScopedPointer<DRawInfo> identify(new DRawInfo);
0038 
0039     if (DRawDecoder::rawFileIdentify(*identify, filePath))
0040     {
0041         long int num = 1;
0042         long int den = 1;
0043 
0044         if (!identify->model.isNull())
0045         {
0046             setExifTagString("Exif.Image.Model", identify->model);
0047         }
0048 
0049         if (!identify->make.isNull())
0050         {
0051             setExifTagString("Exif.Image.Make", identify->make);
0052         }
0053 
0054         if (!identify->owner.isNull())
0055         {
0056             setExifTagString("Exif.Image.Artist", identify->owner);
0057         }
0058 
0059         if (!identify->software.isNull())
0060         {
0061             setExifTagString("Exif.Image.ProcessingSoftware", identify->software);
0062         }
0063 
0064         if (!identify->firmware.isNull())
0065         {
0066             setExifTagString("Exif.Image.Software", identify->firmware);
0067         }
0068 
0069         if (!identify->DNGVersion.isNull())
0070         {
0071             QByteArray ba = identify->DNGVersion.toLatin1();
0072             ba.truncate(4);
0073             setExifTagData("Exif.Image.DNGVersion", ba);
0074         }
0075 
0076         if (!identify->uniqueCameraModel.isNull())
0077         {
0078             setExifTagString("Exif.Image.UniqueCameraModel", identify->uniqueCameraModel);
0079         }
0080 
0081         if (!identify->localizedCameraModel.isNull())
0082         {
0083             setExifTagData("Exif.Image.LocalizedCameraModel", identify->localizedCameraModel.toLatin1());
0084         }
0085 
0086         if (!identify->imageID.isNull())
0087         {
0088             setExifTagString("Exif.Image.ImageID", identify->imageID);
0089         }
0090 
0091         if (!identify->rawDataUniqueID.isNull())
0092         {
0093             setExifTagData("Exif.Image.RawDataUniqueID", identify->rawDataUniqueID.toLatin1());
0094         }
0095 
0096         if (!identify->originalRawFileName.isNull())
0097         {
0098             setExifTagData("Exif.Image.OriginalRawFileName", identify->originalRawFileName.toLatin1());
0099         }
0100 
0101         if (!identify->lensModel.isNull())
0102         {
0103             setExifTagString("Exif.Photo.LensModel", identify->lensModel);
0104         }
0105 
0106         if (!identify->lensMake.isNull())
0107         {
0108             setExifTagString("Exif.Photo.LensMake", identify->lensMake);
0109         }
0110 
0111         if (!identify->lensSerial.isNull())
0112         {
0113             setExifTagString("Exif.Photo.LensSerialNumber", identify->lensSerial);
0114         }
0115 
0116         if (identify->focalLengthIn35mmFilm != -1)
0117         {
0118             setExifTagLong("Exif.Photo.FocalLengthIn35mmFilm", identify->focalLengthIn35mmFilm);
0119         }
0120 
0121         if (identify->maxAperture != -1.0F)
0122         {
0123             convertToRational(identify->maxAperture, &num, &den, 8);
0124             setExifTagRational("Exif.Image.MaxApertureValue", num, den);
0125         }
0126 
0127         if (identify->serialNumber != 0)
0128         {
0129             setExifTagLong("Exif.Image.ImageNumber", identify->serialNumber);
0130         }
0131 
0132         if (identify->flashUsed != -1)
0133         {
0134             setExifTagLong("Exif.Photo.Flash", identify->flashUsed);
0135         }
0136 
0137         if (identify->meteringMode != -1)
0138         {
0139             setExifTagLong("Exif.Image.MeteringMode", identify->meteringMode);
0140         }
0141 
0142         if (identify->exposureProgram != -1)
0143         {
0144             setExifTagLong("Exif.Photo.ExposureProgram", identify->exposureProgram);
0145         }
0146 
0147         if (identify->sensitivity != -1)
0148         {
0149             setExifTagLong("Exif.Photo.ISOSpeedRatings", lroundf(identify->sensitivity));
0150         }
0151 
0152         if (identify->baselineExposure != -999.0F)
0153         {
0154             convertToRational(identify->baselineExposure, &num, &den, 8);
0155             setExifTagRational("Exif.Image.BaselineExposure", num, den);
0156         }
0157 
0158         if (identify->ambientTemperature != -1000.0F)
0159         {
0160             convertToRational(identify->ambientTemperature, &num, &den, 8);
0161             setExifTagRational("Exif.Photo.AmbientTemperature", num, den);
0162         }
0163 
0164         if (identify->ambientHumidity != -1000.0F)
0165         {
0166             convertToRational(identify->ambientHumidity, &num, &den, 8);
0167             setExifTagRational("Exif.Photo.0x9401", num, den);
0168         }
0169 
0170         if (identify->ambientPressure != -1000.0F)
0171         {
0172             convertToRational(identify->ambientPressure, &num, &den, 8);
0173             setExifTagRational("Exif.Photo.0x9402", num, den);
0174         }
0175 
0176         if (identify->ambientWaterDepth != 1000.0F)
0177         {
0178             convertToRational(identify->ambientWaterDepth, &num, &den, 8);
0179             setExifTagRational("Exif.Photo.0x9403", num, den);
0180         }
0181 
0182         if (identify->ambientAcceleration != -1000.0F)
0183         {
0184             convertToRational(identify->ambientAcceleration, &num, &den, 8);
0185             setExifTagRational("Exif.Photo.0x9404", num, den);
0186         }
0187 
0188         if (identify->ambientElevationAngle != -1000.0F)
0189         {
0190             convertToRational(identify->ambientElevationAngle, &num, &den, 8);
0191             setExifTagRational("Exif.Photo.0x9405", num, den);
0192         }
0193 
0194         if (identify->exposureIndex != -1.0F)
0195         {
0196             convertToRational(identify->exposureIndex, &num, &den, 8);
0197             setExifTagRational("Exif.Photo.ExposureIndex", num, den);
0198         }
0199 
0200         if (identify->dateTime.isValid())
0201         {
0202             setImageDateTime(identify->dateTime, false);
0203         }
0204 
0205         if (identify->exposureTime != -1.0F)
0206         {
0207             convertToRationalSmallDenominator(identify->exposureTime, &num, &den);
0208             setExifTagRational("Exif.Photo.ExposureTime", num, den);
0209         }
0210 
0211         if (identify->aperture != -1.0F)
0212         {
0213             convertToRational(identify->aperture, &num, &den, 8);
0214             setExifTagRational("Exif.Photo.ApertureValue", num, den);
0215         }
0216 
0217         if (identify->focalLength != -1.0F)
0218         {
0219             convertToRational(identify->focalLength, &num, &den, 8);
0220             setExifTagRational("Exif.Photo.FocalLength", num, den);
0221         }
0222 
0223         if (identify->orientation != DRawInfo::ORIENTATION_NONE)
0224         {
0225             ImageOrientation orientation;
0226 
0227             switch (identify->orientation)
0228             {
0229                 case DRawInfo::ORIENTATION_180:
0230                 {
0231                     orientation = ORIENTATION_ROT_180;
0232                     break;
0233                 }
0234 
0235                 case DRawInfo::ORIENTATION_Mirror90CCW:
0236                 {
0237                     orientation = ORIENTATION_ROT_90_HFLIP;
0238                     break;
0239                 }
0240 
0241                 case DRawInfo::ORIENTATION_90CCW:
0242                 {
0243                     orientation = ORIENTATION_ROT_270;
0244                     break;
0245                 }
0246 
0247                 case DRawInfo::ORIENTATION_90CW:
0248                 {
0249                     orientation = ORIENTATION_ROT_90;
0250                     break;
0251                 }
0252 
0253                 default:
0254                 {
0255                     orientation = ORIENTATION_NORMAL;
0256                     break;
0257                 }
0258             }
0259 
0260             setExifTagLong("Exif.Image.Orientation", orientation);
0261         }
0262 
0263         if (identify->imageSize.isValid())
0264         {
0265             setItemDimensions(identify->imageSize);
0266         }
0267 
0268         // A RAW image is always uncalibrated. */
0269 
0270         setItemColorWorkSpace(WORKSPACE_UNCALIBRATED);
0271 
0272         // Handle ICC color profile byte-array
0273 
0274         if (!identify->iccData.isEmpty())
0275         {
0276             setIccProfile(IccProfile(identify->iccData));
0277         }
0278 
0279         // Handle GPS information
0280 
0281         if (identify->hasGpsInfo)
0282         {
0283             setGPSInfo(identify->altitude, identify->latitude, identify->longitude);
0284         }
0285 
0286         // Handle image description
0287 
0288         if (!identify->description.isEmpty())
0289         {
0290             setExifComment(identify->description);
0291         }
0292 
0293         // Handle thumbnail image
0294 
0295         if (!identify->thumbnail.isNull())
0296         {
0297             QImage thumb = QImage::fromData(identify->thumbnail);
0298 
0299             if (!thumb.isNull())
0300             {
0301                 setExifThumbnail(thumb.scaled(160, 120, Qt::KeepAspectRatio, Qt::SmoothTransformation));
0302             }
0303         }
0304 
0305         // Handle XMP metadata byte-array
0306 
0307         if (!identify->xmpData.isEmpty())
0308         {
0309             setXmp(identify->xmpData);
0310         }
0311 
0312         return true;
0313     }
0314 
0315     return false;
0316 }
0317 
0318 } // namespace Digikam