File indexing completed on 2025-01-19 03:51:01

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2019-04-19
0007  * Description : Magick loader for DImg framework - save method.
0008  *
0009  * SPDX-FileCopyrightText: 2019-2023 by Caulier Gilles <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2019-2023 by Maik Qualmann <metzpinguin at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "dimgimagemagickloader.h"
0017 
0018 // ImageMagick includes
0019 
0020 // Pragma directives to reduce warnings from ImageMagick header files.
0021 #if !defined(Q_OS_DARWIN) && defined(Q_CC_GNU)
0022 #   pragma GCC diagnostic push
0023 #   pragma GCC diagnostic ignored "-Wignored-qualifiers"
0024 #   pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
0025 #endif
0026 
0027 #if defined(Q_CC_CLANG)
0028 #   pragma clang diagnostic push
0029 #   pragma clang diagnostic ignored "-Wignored-qualifiers"
0030 #   pragma clang diagnostic ignored "-Wkeyword-macro"
0031 #endif
0032 
0033 #include <Magick++.h>
0034 
0035 // Restore warnings
0036 
0037 #if defined(Q_CC_CLANG)
0038 #   pragma clang diagnostic pop
0039 #endif
0040 
0041 #if defined(Q_CC_GNU)
0042 #   pragma GCC diagnostic pop
0043 #endif
0044 
0045 // Local includes
0046 
0047 #include "digikam_debug.h"
0048 #include "dimgloaderobserver.h"
0049 
0050 using namespace Magick;
0051 
0052 namespace DigikamImageMagickDImgPlugin
0053 {
0054 
0055 bool DImgImageMagickLoader::save(const QString& filePath, DImgLoaderObserver* const observer)
0056 {
0057     // Saving is opaque to us. No support for stopping from observer,
0058     // progress info are only pseudo values
0059 
0060     if (observer)
0061     {
0062         observer->progressInfo(0.5F);
0063     }
0064 
0065     qCDebug(DIGIKAM_DIMG_LOG) << "Try to save image with ImageMagick codecs";
0066 
0067     try
0068     {
0069         QVariant formatAttr = imageGetAttribute(QLatin1String("format"));
0070         QByteArray format   = formatAttr.toByteArray();
0071 
0072         if (observer)
0073         {
0074             observer->progressInfo(0.8F);
0075         }
0076 
0077         Blob pixelBlob(imageData(), imageNumBytes());
0078 
0079         Image image;
0080         image.size(Geometry(imageWidth(), imageHeight()));
0081         image.magick("BGRA");
0082         image.depth(imageBitsDepth());
0083 
0084 #if MagickLibVersion < 0x700
0085 
0086         image.matte(imageHasAlpha());
0087 
0088 #else
0089 
0090         image.alpha(imageHasAlpha());
0091 
0092 #endif
0093 
0094         image.read(pixelBlob);
0095         image.magick(format.data());
0096 
0097         QByteArray iccRawProfile = m_image->getIccProfile().data();
0098 
0099         if (!iccRawProfile.isEmpty())
0100         {
0101             Blob iccBlob(iccRawProfile.data(), iccRawProfile.size());
0102             image.iccColorProfile(iccBlob);
0103         }
0104 
0105         image.write(filePath.toUtf8().constData());
0106 
0107         if (observer)
0108         {
0109             observer->progressInfo(1.0F);
0110         }
0111 
0112         imageSetAttribute(QLatin1String("format"), format.toUpper());
0113 
0114         saveMetadata(filePath);
0115         return true;
0116     }
0117     catch (Exception& error)
0118     {
0119         qCWarning(DIGIKAM_DIMG_LOG) << "ImageMagick exception [" << filePath << "]" << error.what();
0120         return false;
0121     }
0122 
0123     return true;
0124 }
0125 
0126 } // namespace DigikamImageMagickDImgPlugin