KExiv2

kexiv2previews.cpp
1/*
2 SPDX-FileCopyrightText: 2009-2015 Gilles Caulier <caulier dot gilles at gmail dot com>
3 SPDX-FileCopyrightText: 2009-2012 Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8// Local includes
9
10#include "kexiv2previews.h"
11#include "kexiv2_p.h"
12#include "kexiv2.h"
13#include "libkexiv2_debug.h"
14
15namespace KExiv2Iface
16{
17
18class KExiv2PreviewsPrivate
19{
20public:
21
22 KExiv2PreviewsPrivate()
23 {
24 manager = nullptr;
25 }
26
27 ~KExiv2PreviewsPrivate()
28 {
29 delete manager;
30 }
31
32#if EXIV2_TEST_VERSION(0,28,0)
33 void load(Exiv2::Image::UniquePtr image_)
34#else
35 void load(Exiv2::Image::AutoPtr image_)
36#endif
37 {
38#if EXIV2_TEST_VERSION(0,28,0)
39 image = std::move(image_);
40#else
41 image = image_;
42#endif
43
44 image->readMetadata();
45
46 manager = new Exiv2::PreviewManager(*image);
47 Exiv2::PreviewPropertiesList props = manager->getPreviewProperties();
48
49 // reverse order of list, which is smallest-first
50 Exiv2::PreviewPropertiesList::reverse_iterator it;
51
52 for (it = props.rbegin() ; it != props.rend() ; ++it)
53 {
54 properties << *it;
55 }
56 }
57
58public:
59
60#if EXIV2_TEST_VERSION(0,28,0)
61 Exiv2::Image::UniquePtr image;
62#else
63 Exiv2::Image::AutoPtr image;
64#endif
65 Exiv2::PreviewManager* manager;
67};
68
70 : d(new KExiv2PreviewsPrivate)
71{
72 try
73 {
74#if EXIV2_TEST_VERSION(0,28,0)
75 Exiv2::Image::UniquePtr image = Exiv2::ImageFactory::open((const char*)(QFile::encodeName(filePath).constData()));
76 d->load(std::move(image));
77#else
78 Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((const char*)(QFile::encodeName(filePath).constData()));
79 d->load(image);
80#endif
81 }
82 catch( Exiv2::Error& e )
83 {
84 KExiv2Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
85 }
86 catch(...)
87 {
88 qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
89 }
90}
91
93 : d(new KExiv2PreviewsPrivate)
94{
95 try
96 {
97#if EXIV2_TEST_VERSION(0,28,0)
98 Exiv2::Image::UniquePtr image = Exiv2::ImageFactory::open((Exiv2::byte*)imgData.data(), imgData.size());
99 d->load(std::move(image));
100#else
101 Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((Exiv2::byte*)imgData.data(), imgData.size());
102 d->load(image);
103#endif
104 }
105 catch( Exiv2::Error& e )
106 {
107 KExiv2Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
108 }
109 catch(...)
110 {
111 qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
112 }
113}
114
115KExiv2Previews::~KExiv2Previews() = default;
116
118{
119 return d->properties.isEmpty();
120}
121
123{
124 if (d->image.get())
125 return QSize(d->image->pixelWidth(), d->image->pixelHeight());
126
127 return QSize();
128}
129
131{
132 if (d->image.get())
133 return QString::fromLatin1(d->image->mimeType().c_str());
134
135 return QString();
136}
137
139{
140 return d->properties.size();
141}
142
144{
145 if (index < 0 || index >= size()) return 0;
146
147 return d->properties[index].size_;
148}
149
150int KExiv2Previews::width(int index)
151{
152 if (index < 0 || index >= size()) return 0;
153
154 return d->properties[index].width_;
155}
156
157int KExiv2Previews::height(int index)
158{
159 if (index < 0 || index >= size()) return 0;
160
161 return d->properties[index].height_;
162}
163
164QString KExiv2Previews::mimeType(int index)
165{
166 if (index < 0 || index >= size()) return QString();
167
168 return QString::fromLatin1(d->properties[index].mimeType_.c_str());
169}
170
171QString KExiv2Previews::fileExtension(int index)
172{
173 if (index < 0 || index >= size()) return QString();
174
175 return QString::fromLatin1(d->properties[index].extension_.c_str());
176}
177
179{
180 if (index < 0 || index >= size()) return QByteArray();
181
182 qCDebug(LIBKEXIV2_LOG) << "index: " << index;
183 qCDebug(LIBKEXIV2_LOG) << "d->properties: " << count();
184
185 try
186 {
187 Exiv2::PreviewImage image = d->manager->getPreviewImage(d->properties[index]);
188 return QByteArray((const char*)image.pData(), image.size());
189 }
190 catch( Exiv2::Error& e )
191 {
192 KExiv2Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
193 return QByteArray();
194 }
195 catch(...)
196 {
197 qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
198 return QByteArray();
199 }
200}
201
203{
204 QByteArray previewData = data(index);
206
207 if (!image.loadFromData(previewData))
208 return QImage();
209
210 return image;
211}
212
213} // namespace KExiv2Iface
bool isEmpty()
Returns if there are any preview images available.
int dataSize(int index=0)
For each contained preview image, return the size of the image data in bytes, width and height of the...
QSize originalSize() const
Returns the pixel size of the original image, as read from the file (not the metadata).
QString originalMimeType() const
Returns the mimeType of the original image, detected from the file's content.
QByteArray data(int index=0)
Retrieve the image data for the specified embedded preview image.
QImage image(int index=0)
Loads the data of the specified preview and creates a QImage from this data.
KExiv2Previews(const QString &filePath)
Open the given file and scan for embedded preview images.
int count()
Returns how many embedded previews are available.
KExiv2Iface - Exiv2 library interface.
Definition kexiv2.cpp:17
char * data()
qsizetype size() const const
QByteArray encodeName(const QString &fileName)
bool loadFromData(QByteArrayView data, const char *format)
QSize size() const const
QString fromLatin1(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:02 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.