aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/thumbnailqt_extractor.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/thumbnailqt_extractor.cc')
-rw-r--r--src/plugins/thumbnailqt_extractor.cc174
1 files changed, 174 insertions, 0 deletions
diff --git a/src/plugins/thumbnailqt_extractor.cc b/src/plugins/thumbnailqt_extractor.cc
new file mode 100644
index 0000000..5f4ad9c
--- /dev/null
+++ b/src/plugins/thumbnailqt_extractor.cc
@@ -0,0 +1,174 @@
1/*
2 This file is part of libextractor.
3 (C) 2006, 2009 Vidyut Samanta and Christian Grothoff
4
5 libextractor is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 libextractor is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libextractor; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21/**
22 * @file thumbnailqt_extractor.cc
23 * @author Nils Durner
24 * @brief this extractor produces a binary (!) encoded
25 * thumbnail of images (using Qt).
26 */
27
28#include "platform.h"
29#include "extractor.h"
30#include <Qt/qpixmap.h>
31#include <Qt/qbytearray.h>
32#include <Qt/qbuffer.h>
33#include <Qt/qapplication.h>
34#include <pthread.h>
35
36#ifdef HAVE_QT_SVG
37 #include <Qt/qsvgrenderer.h>
38 #include <Qt/qpainter.h>
39#endif
40
41#define THUMBSIZE 128
42
43extern "C"
44{
45
46int
47EXTRACTOR_thumbnailqt_extract (const char *data,
48 size_t size,
49 EXTRACTOR_MetaDataProcessor proc,
50 void *proc_cls,
51 const char *options)
52{
53 QImage *img;
54 QByteArray bytes;
55 QBuffer buffer;
56 unsigned long width;
57 unsigned long height;
58 char format[64];
59 QImage::Format colors;
60
61 /* Determine image format to use */
62 if (options == NULL)
63 colors = QImage::Format_Indexed8;
64 else
65 switch (atoi(options))
66 {
67 case 1:
68 colors = QImage::Format_Mono;
69 break;
70 case 8:
71 colors = QImage::Format_Indexed8;
72 break;
73 case 16:
74 case 24:
75 colors = QImage::Format_RGB32;
76 break;
77 default:
78 colors = QImage::Format_ARGB32;
79 break;
80 }
81 QByteArray din (data, (int) size);
82
83#ifdef HAVE_QT_SVG
84 /* Render SVG image */
85 QSvgRenderer svg;
86 QSize qsize;
87 if (svg.load(din))
88 {
89 qsize = svg.defaultSize();
90 img = new QImage(qsize, QImage::Format_ARGB32);
91 QPainter painter(img);
92 painter.setViewport(0, 0, qsize.width(), qsize.height());
93 painter.eraseRect(0, 0, qsize.width(), qsize.height());
94 svg.render(&painter);
95 }
96 else
97#endif
98 {
99 /* Load image */
100 img = new QImage();
101 img->loadFromData(din);
102 }
103
104 height = img->height();
105 width = img->width();
106 snprintf(format,
107 sizeof(format),
108 "%ux%u",
109 (unsigned int) width,
110 (unsigned int) height);
111 if (0 != proc (proc_cls,
112 "thumbnailqt",
113 EXTRACTOR_METATYPE_IMAGE_DIMENSIONS,
114 EXTRACTOR_METAFORMAT_UTF8,
115 "text/plain",
116 format,
117 strlen(format)+1))
118 {
119 delete img;
120 return 1;
121 }
122 if (height == 0)
123 height = 1;
124 if (width == 0)
125 width = 1;
126
127 /* Change color depth */
128 QImage thumb = img->convertToFormat(colors);
129 delete img;
130
131 /* Resize image
132 *
133 * Qt's scaled() produces poor quality if the image is resized to less than
134 * half the size. Therefore, we resize the image in multiple steps.
135 * http://lists.trolltech.com/qt-interest/2006-04/msg00376.html */
136 while ( (width > THUMBSIZE) || (height > THUMBSIZE) )
137 {
138 width /= 2;
139 if (width < THUMBSIZE)
140 width = THUMBSIZE;
141 height /= 2;
142 if (height < THUMBSIZE)
143 height = THUMBSIZE;
144 thumb = thumb.scaled(width,
145 height,
146 Qt::KeepAspectRatio,
147 Qt::SmoothTransformation);
148 }
149 buffer.setBuffer(&bytes);
150 buffer.open(QIODevice::WriteOnly);
151 thumb.save(&buffer, "PNG");
152 return proc (proc_cls,
153 "thumbnailqt",
154 EXTRACTOR_METATYPE_THUMBNAIL,
155 EXTRACTOR_METAFORMAT_BINARY,
156 "image/png",
157 bytes.data(),
158 bytes.length());
159}
160
161
162int
163EXTRACTOR_thumbnail_extract (const unsigned char *data,
164 size_t size,
165 EXTRACTOR_MetaDataProcessor proc,
166 void *proc_cls,
167 const char *options)
168{
169 return EXTRACTOR_thumbnail_extract (data, size, proc, proc_cls, options);
170}
171
172} // extern "C"
173
174/* end of thumbnailqt_extractor.cc */