KExiv2

kexiv2previews.cpp
Go to the documentation of this file.
1 
28 // Local includes
29 
30 #include "kexiv2previews.h"
31 #include "kexiv2_p.h"
32 #include "kexiv2.h"
33 #include "libkexiv2_debug.h"
34 
35 namespace KExiv2Iface
36 {
37 
38 class Q_DECL_HIDDEN KExiv2Previews::Private
39 {
40 public:
41 
42  Private()
43  {
44  manager = nullptr;
45  }
46 
47  ~Private()
48  {
49  delete manager;
50  }
51 
52  void load(Exiv2::Image::AutoPtr image_)
53  {
54  image = image_;
55 
56  image->readMetadata();
57 
58  manager = new Exiv2::PreviewManager(*image);
59  Exiv2::PreviewPropertiesList props = manager->getPreviewProperties();
60 
61  // reverse order of list, which is smallest-first
62  Exiv2::PreviewPropertiesList::reverse_iterator it;
63 
64  for (it = props.rbegin() ; it != props.rend() ; ++it)
65  {
66  properties << *it;
67  }
68  }
69 
70 public:
71 
72  Exiv2::Image::AutoPtr image;
73  Exiv2::PreviewManager* manager;
75 };
76 
78  : d(new Private)
79 {
80  try
81  {
82  Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((const char*)(QFile::encodeName(filePath).constData()));
83  d->load(image);
84  }
85  catch( Exiv2::Error& e )
86  {
87  KExiv2::Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
88  }
89  catch(...)
90  {
91  qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
92  }
93 }
94 
96  : d(new Private)
97 {
98  try
99  {
100  Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((Exiv2::byte*)imgData.data(), imgData.size());
101  d->load(image);
102  }
103  catch( Exiv2::Error& e )
104  {
105  KExiv2::Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
106  }
107  catch(...)
108  {
109  qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
110  }
111 }
112 
113 KExiv2Previews::~KExiv2Previews()
114 {
115  delete d;
116 }
117 
119 {
120  return d->properties.isEmpty();
121 }
122 
124 {
125  if (d->image.get())
126  return QSize(d->image->pixelWidth(), d->image->pixelHeight());
127 
128  return QSize();
129 }
130 
132 {
133  if (d->image.get())
134  return QString::fromLatin1(d->image->mimeType().c_str());
135 
136  return QString();
137 }
138 
140 {
141  return d->properties.size();
142 }
143 
145 {
146  if (index < 0 || index >= size()) return 0;
147 
148  return d->properties[index].size_;
149 }
150 
151 int KExiv2Previews::width(int index)
152 {
153  if (index < 0 || index >= size()) return 0;
154 
155  return d->properties[index].width_;
156 }
157 
158 int KExiv2Previews::height(int index)
159 {
160  if (index < 0 || index >= size()) return 0;
161 
162  return d->properties[index].height_;
163 }
164 
165 QString KExiv2Previews::mimeType(int index)
166 {
167  if (index < 0 || index >= size()) return QString();
168 
169  return QString::fromLatin1(d->properties[index].mimeType_.c_str());
170 }
171 
172 QString KExiv2Previews::fileExtension(int index)
173 {
174  if (index < 0 || index >= size()) return QString();
175 
176  return QString::fromLatin1(d->properties[index].extension_.c_str());
177 }
178 
180 {
181  if (index < 0 || index >= size()) return QByteArray();
182 
183  qCDebug(LIBKEXIV2_LOG) << "index: " << index;
184  qCDebug(LIBKEXIV2_LOG) << "d->properties: " << count();
185 
186  try
187  {
188  Exiv2::PreviewImage image = d->manager->getPreviewImage(d->properties[index]);
189  return QByteArray((const char*)image.pData(), image.size());
190  }
191  catch( Exiv2::Error& e )
192  {
193  KExiv2::Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
194  return QByteArray();
195  }
196  catch(...)
197  {
198  qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
199  return QByteArray();
200  }
201 }
202 
204 {
205  QByteArray previewData = data(index);
206  QImage image;
207 
208  if (!image.loadFromData(previewData))
209  return QImage();
210 
211  return image;
212 }
213 
214 } // namespace KExiv2Iface
bool loadFromData(const uchar *data, int len, const char *format)
QByteArray data(int index=0)
Retrieve the image data for the specified embedded preview image.
KExiv2Iface.
Definition: kexiv2.cpp:36
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&#39;s content.
KExiv2Previews(const QString &filePath)
Open the given file and scan for embedded preview images.
KDEGAMES_EXPORT QAction * load(const QObject *recvr, const char *slot, QObject *parent)
bool isEmpty()
Returns if there are any preview images available.
===========================================================This file is a part of KDE project ...
KGuiItem properties()
int dataSize(int index=0)
For each contained preview image, return the size of the image data in bytes, width and height of the...
int count()
Returns how many embedded previews are available.
QImage image(int index=0)
Loads the data of the specified preview and creates a QImage from this data.
char * data()
QString fromLatin1(const char *str, int size)
int size() const const
===========================================================This file is a part of KDE project ...
QByteArray encodeName(const QString &fileName)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Nov 26 2020 22:35:13 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.