1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2019-04-19
* Description : Magick loader for DImg framework - load method.
*
* SPDX-FileCopyrightText: 2019-2023 by Caulier Gilles <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2019-2023 by Maik Qualmann <metzpinguin at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "dimgimagemagickloader.h"
// ImageMagick includes
// Pragma directives to reduce warnings from ImageMagick header files.
#if !defined(Q_OS_DARWIN) && defined(Q_CC_GNU)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wignored-qualifiers"
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#if defined(Q_CC_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wignored-qualifiers"
# pragma clang diagnostic ignored "-Wkeyword-macro"
#endif
#include <Magick++.h>
// Restore warnings
#if defined(Q_CC_CLANG)
# pragma clang diagnostic pop
#endif
#if !defined(Q_OS_DARWIN) && defined(Q_CC_GNU)
# pragma GCC diagnostic pop
#endif
// Local includes
#include "digikam_debug.h"
#include "dimgloaderobserver.h"
using namespace Magick;
namespace DigikamImageMagickDImgPlugin
{
bool DImgImageMagickLoader::load(const QString& filePath, DImgLoaderObserver* const observer)
{
readMetadata(filePath);
// Loading is opaque to us. No support for stopping from observer,
// progress info are only pseudo values
if (observer)
{
observer->progressInfo(0.5F);
}
qCDebug(DIGIKAM_DIMG_LOG) << "Try to load image with ImageMagick codecs";
try
{
Image image;
if (m_loadFlags & LoadImageData)
{
try
{
image.read(filePath.toUtf8().constData());
}
catch (Warning& warning)
{
qCWarning(DIGIKAM_DIMG_LOG) << "ImageMagick warning [" << filePath << "]" << warning.what();
}
if (observer)
{
observer->progressInfo(0.8F);
}
qCDebug(DIGIKAM_DIMG_LOG) << "IM to DImg :" << image.columns() << image.rows();
qCDebug(DIGIKAM_DIMG_LOG) << "IM QuantumRange :" << QuantumRange;
qCDebug(DIGIKAM_DIMG_LOG) << "IM Depth :" << image.depth();
qCDebug(DIGIKAM_DIMG_LOG) << "IM Format :" << image.format().c_str();
m_sixteenBit = (image.depth() == 16);
StorageType type = m_sixteenBit ? ShortPixel : CharPixel;
uchar* data = new_failureTolerant(image.columns(), image.rows(), m_sixteenBit ? 8 : 4);
if (!data)
{
qCWarning(DIGIKAM_DIMG_LOG_QIMAGE) << "Failed to allocate memory for loading" << filePath;
loadingFailed();
return false;
}
image.write(0, 0, image.columns(), image.rows(), "BGRA", type, (void*)data);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
if (observer)
{
observer->progressInfo(0.9F);
}
if (m_loadFlags & LoadICCData)
{
Blob iccBlob(image.iccColorProfile());
QByteArray iccRawProfile((char*)iccBlob.data(), iccBlob.length());<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
if (!iccRawProfile.isEmpty())
{
imageSetIccProfile(IccProfile(iccRawProfile));
}
}
if (observer)
{
observer->progressInfo(1.0F);
}
imageWidth() = image.columns();
imageHeight() = image.rows();
imageData() = data;
#if MagickLibVersion < 0x700
m_hasAlpha = image.matte();
#else
m_hasAlpha = image.alpha();
#endif
// We considering that PNG is the most representative format of an image loaded by ImageMagick
imageSetAttribute(QLatin1String("format"), QLatin1String("PNG"));
imageSetAttribute(QLatin1String("originalColorModel"), DImg::RGB);
imageSetAttribute(QLatin1String("originalBitDepth"), m_sixteenBit ? 16 : 8);
imageSetAttribute(QLatin1String("originalSize"), QSize(image.columns(), image.rows()));
}
else
{
try
{
image.ping(filePath.toUtf8().constData());
}
catch (Warning& warning)
{
qCWarning(DIGIKAM_DIMG_LOG) << "ImageMagick warning [" << filePath << "]" << warning.what();
}
imageWidth() = image.columns();
imageHeight() = image.rows();
#if MagickLibVersion < 0x700
m_hasAlpha = image.matte();
#else
m_hasAlpha = image.alpha();
#endif
m_sixteenBit = (image.depth() == 16);
imageSetAttribute(QLatin1String("format"), QLatin1String("PNG"));
imageSetAttribute(QLatin1String("originalColorModel"), DImg::RGB);
imageSetAttribute(QLatin1String("originalBitDepth"), m_sixteenBit ? 16 : 8);
imageSetAttribute(QLatin1String("originalSize"), QSize(image.columns(), image.rows()));
}
}
catch (Exception& error)
{
qCWarning(DIGIKAM_DIMG_LOG) << "ImageMagick exception [" << filePath << "]" << error.what();
loadingFailed();
return false;
}
return true;
}
} // namespace DigikamImageMagickDImgPlugin
|