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 
15 namespace KExiv2Iface
16 {
17 
18 class KExiv2PreviewsPrivate
19 {
20 public:
21 
22  KExiv2PreviewsPrivate()
23  {
24  manager = nullptr;
25  }
26 
27  ~KExiv2PreviewsPrivate()
28  {
29  delete manager;
30  }
31 
32  void load(Exiv2::Image::AutoPtr image_)
33  {
34  image = image_;
35 
36  image->readMetadata();
37 
38  manager = new Exiv2::PreviewManager(*image);
39  Exiv2::PreviewPropertiesList props = manager->getPreviewProperties();
40 
41  // reverse order of list, which is smallest-first
42  Exiv2::PreviewPropertiesList::reverse_iterator it;
43 
44  for (it = props.rbegin() ; it != props.rend() ; ++it)
45  {
46  properties << *it;
47  }
48  }
49 
50 public:
51 
52  Exiv2::Image::AutoPtr image;
53  Exiv2::PreviewManager* manager;
55 };
56 
58  : d(new KExiv2PreviewsPrivate)
59 {
60  try
61  {
62  Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((const char*)(QFile::encodeName(filePath).constData()));
63  d->load(image);
64  }
65  catch( Exiv2::Error& e )
66  {
67  KExiv2Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
68  }
69  catch(...)
70  {
71  qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
72  }
73 }
74 
76  : d(new KExiv2PreviewsPrivate)
77 {
78  try
79  {
80  Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((Exiv2::byte*)imgData.data(), imgData.size());
81  d->load(image);
82  }
83  catch( Exiv2::Error& e )
84  {
85  KExiv2Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
86  }
87  catch(...)
88  {
89  qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
90  }
91 }
92 
93 KExiv2Previews::~KExiv2Previews() = default;
94 
96 {
97  return d->properties.isEmpty();
98 }
99 
101 {
102  if (d->image.get())
103  return QSize(d->image->pixelWidth(), d->image->pixelHeight());
104 
105  return QSize();
106 }
107 
109 {
110  if (d->image.get())
111  return QString::fromLatin1(d->image->mimeType().c_str());
112 
113  return QString();
114 }
115 
117 {
118  return d->properties.size();
119 }
120 
122 {
123  if (index < 0 || index >= size()) return 0;
124 
125  return d->properties[index].size_;
126 }
127 
128 int KExiv2Previews::width(int index)
129 {
130  if (index < 0 || index >= size()) return 0;
131 
132  return d->properties[index].width_;
133 }
134 
135 int KExiv2Previews::height(int index)
136 {
137  if (index < 0 || index >= size()) return 0;
138 
139  return d->properties[index].height_;
140 }
141 
142 QString KExiv2Previews::mimeType(int index)
143 {
144  if (index < 0 || index >= size()) return QString();
145 
146  return QString::fromLatin1(d->properties[index].mimeType_.c_str());
147 }
148 
149 QString KExiv2Previews::fileExtension(int index)
150 {
151  if (index < 0 || index >= size()) return QString();
152 
153  return QString::fromLatin1(d->properties[index].extension_.c_str());
154 }
155 
157 {
158  if (index < 0 || index >= size()) return QByteArray();
159 
160  qCDebug(LIBKEXIV2_LOG) << "index: " << index;
161  qCDebug(LIBKEXIV2_LOG) << "d->properties: " << count();
162 
163  try
164  {
165  Exiv2::PreviewImage image = d->manager->getPreviewImage(d->properties[index]);
166  return QByteArray((const char*)image.pData(), image.size());
167  }
168  catch( Exiv2::Error& e )
169  {
170  KExiv2Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
171  return QByteArray();
172  }
173  catch(...)
174  {
175  qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
176  return QByteArray();
177  }
178 }
179 
181 {
182  QByteArray previewData = data(index);
183  QImage image;
184 
185  if (!image.loadFromData(previewData))
186  return QImage();
187 
188  return image;
189 }
190 
191 } // namespace KExiv2Iface
QByteArray data(int index=0)
Retrieve the image data for the specified embedded preview image.
QByteArray encodeName(const QString &fileName)
KExiv2Iface - Exiv2 library interface.
Definition: kexiv2.cpp:16
QString originalMimeType() const
Returns the mimeType of the original image, detected from the file's content.
QAction * load(const QObject *recvr, const char *slot, QObject *parent)
KGuiItem properties()
KExiv2Previews(const QString &filePath)
Open the given file and scan for embedded preview images.
bool loadFromData(const uchar *data, int len, const char *format)
bool isEmpty()
Returns if there are any preview images available.
QSize originalSize() const
Returns the pixel size of the original image, as read from the file (not the metadata).
QSize size() const const
int dataSize(int index=0)
For each contained preview image, return the size of the image data in bytes, width and height of the...
QImage image(int index=0)
Loads the data of the specified preview and creates a QImage from this data.
QString fromLatin1(const char *str, int size)
int count()
Returns how many embedded previews are available.
int size() const const
char * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 15 2022 04:02:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.