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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-09-25
0007  * Description : a tool to convert RAW file to DNG - Export to target DNG.
0008  *
0009  * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2010-2011 by Jens Mueller <tschenser at gmx dot de>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "dngwriter_p.h"
0017 
0018 // Local includes
0019 
0020 #include "dngwriterhost.h"
0021 
0022 namespace Digikam
0023 {
0024 
0025 int DNGWriter::Private::exportTarget(DNGWriterHost& host,
0026                                      AutoPtr<dng_negative>& negative,
0027                                      AutoPtr<dng_image>& image)
0028 {
0029     qCDebug(DIGIKAM_GENERAL_LOG) << "DNGWriter: Build DNG Negative";
0030 
0031     // Assign Raw image data.
0032 
0033     negative->SetStage1Image(image);
0034 
0035     // Compute linearized and range mapped image
0036 
0037     negative->BuildStage2Image(host);
0038 
0039     // Compute demosaiced image (used by preview and thumbnail)
0040 
0041     negative->SetStage3Gain(0.6);
0042     negative->BuildStage3Image(host);
0043 
0044     negative->SynchronizeMetadata();
0045     negative->RebuildIPTC(true);
0046 
0047     if (cancel)
0048     {
0049         return PROCESS_CANCELED;
0050     }
0051 
0052     // -----------------------------------------------------------------------------------------
0053 
0054     dng_preview_list previewList;
0055     dng_render negRender(host, *negative.Get());
0056 
0057     if (previewMode != DNGWriter::NONE)
0058     {
0059         qCDebug(DIGIKAM_GENERAL_LOG) << "DNGWriter: DNG preview image creation";
0060 
0061         dng_jpeg_preview* const jpeg_preview = new dng_jpeg_preview();
0062         jpeg_preview->fInfo.fApplicationName.Set_ASCII(QString::fromLatin1("digiKam").toLatin1().constData());
0063         jpeg_preview->fInfo.fApplicationVersion.Set_ASCII(digiKamVersion().toLatin1().constData());
0064         jpeg_preview->fInfo.fDateTime        = orgDateTimeInfo.Encode_ISO_8601();
0065         jpeg_preview->fInfo.fColorSpace      = previewColorSpace_sRGB;
0066 
0067         negRender.SetMaximumSize(previewMode == MEDIUM ? 1280 : width);
0068         AutoPtr<dng_image> negImage(negRender.Render());
0069         dng_image_writer jpegWriter;
0070         jpegWriter.EncodeJPEGPreview(host, *negImage.Get(), *jpeg_preview, 5);
0071         AutoPtr<dng_preview> jp(static_cast<dng_preview*>(jpeg_preview));
0072         previewList.Append(jp);
0073     }
0074 
0075     if (cancel)
0076     {
0077         return PROCESS_CANCELED;
0078     }
0079 
0080     // -----------------------------------------------------------------------------------------
0081 
0082     qCDebug(DIGIKAM_GENERAL_LOG) << "DNGWriter: DNG thumbnail creation";
0083 
0084     dng_image_preview* const thumbnail = new dng_image_preview();
0085     thumbnail->fInfo.fApplicationName.Set_ASCII(QString::fromLatin1("digiKam").toLatin1().constData());
0086     thumbnail->fInfo.fApplicationVersion.Set_ASCII(digiKamVersion().toLatin1().constData());
0087     thumbnail->fInfo.fDateTime         = orgDateTimeInfo.Encode_ISO_8601();
0088     thumbnail->fInfo.fColorSpace       = previewColorSpace_sRGB;
0089 
0090     negRender.SetMaximumSize(256);
0091     thumbnail->fImage.Reset(negRender.Render());
0092     AutoPtr<dng_preview> tn(static_cast<dng_preview*>(thumbnail));
0093     previewList.Append(tn);
0094 
0095     if (cancel)
0096     {
0097         return PROCESS_CANCELED;
0098     }
0099 
0100     // -----------------------------------------------------------------------------------------
0101 
0102     qCDebug(DIGIKAM_GENERAL_LOG) << "DNGWriter: Creating DNG file" << outputInfo.fileName();
0103 
0104     dng_image_writer writer;
0105     dng_file_stream filestream(QFile::encodeName(dngFilePath).constData(), true);
0106 
0107     writer.WriteDNG(host,
0108                     filestream,
0109                     *negative.Get(),
0110                     &previewList,
0111                     dngVersion_SaveDefault,
0112                     !jpegLossLessCompression
0113                    );
0114 
0115     // -----------------------------------------------------------------------------------------
0116     // Metadata makernote cleanup using Exiv2 for some RAW file types
0117     // See bug #204437 and #210371, and write XMP Sidecar if necessary
0118     // We disable writing to RAW files, see bug #381432
0119 /*
0120     QScopedPointer<DMetadata> meta(new DMetadata);
0121 
0122     if (meta->load(dngFilePath))
0123     {
0124         if (inputInfo.suffix().toUpper() == QLatin1String("ORF"))
0125         {
0126             qCDebug(DIGIKAM_GENERAL_LOG) << "DNGWriter: cleanup makernotes using Exiv2";
0127 
0128             meta->setWriteDngFiles(true);
0129             meta->removeExifTag("Exif.OlympusIp.BlackLevel");
0130         }
0131         else
0132         {
0133             // Don't touch DNG file and create XMP sidecar depending of host application settings.
0134             meta->setWriteDngFiles(false);
0135         }
0136 
0137         meta->applyChanges();
0138     }
0139 */
0140     // -----------------------------------------------------------------------------------------
0141     // update modification time if desired
0142 
0143     if (updateFileDate)
0144     {
0145         qCDebug(DIGIKAM_GENERAL_LOG) << "DNGWriter: Setting modification date from meta data:" << fileDate.toString();
0146 
0147         DFileOperations::setModificationTime(dngFilePath, fileDate);
0148     }
0149 
0150     return PROCESS_CONTINUE;
0151 }
0152 
0153 } // namespace Digikam