aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/thumbnailextractorqt.cc
blob: 7fb93cc6447d39beeffcd5ea971663a8e3d0abf6 (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
     This file is part of libextractor.
     (C) 2006 Vidyut Samanta and Christian Grothoff

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
 */

/**
 * @file thumbnailextractorqt.cc
 * @author Nils Durner
 * @brief this extractor produces a binary (!) encoded
 * thumbnail of images (using Qt).
 */

#include "platform.h"
#include "extractor.h"
#include <Qt/qpixmap.h>
#include <Qt/qbytearray.h>
#include <Qt/qbuffer.h>
#include <Qt/qapplication.h>
#include <pthread.h>

#ifdef HAVE_QT_SVG
  #include <Qt/qsvgrenderer.h>
  #include <Qt/qpainter.h>
#endif

#define THUMBSIZE 128

extern "C"
{

static EXTRACTOR_KeywordList * addKeyword(EXTRACTOR_KeywordType type,
					  char * keyword,
					  EXTRACTOR_KeywordList * next) {
  EXTRACTOR_KeywordList * result;

  if (keyword == NULL)
    return next;
  result = (EXTRACTOR_KeywordList *) malloc(sizeof(EXTRACTOR_KeywordList));
  result->next = next;
  result->keyword = keyword;
  result->keywordType = type;
  return result;
}


/* which mime-types maybe subjected to
   the thumbnail extractor (ImageMagick
   crashes and/or prints errors for bad
   formats, so we need to be rather
   conservative here) */
static char * whitelist[] = {
  "image/x-bmp",
  "image/gif",
  "image/jpeg",
  "image/png",
  "image/x-png",
  "image/x-portable-bitmap",
  "image/x-portable-graymap",
  "image/x-portable-pixmap",
  "image/x-xbitmap",
  "image/x-xpixmap"
  "image/x-xpm",
#ifdef HAVE_QT_SVG
  "image/svg+xml",
#endif
  NULL
};

static struct EXTRACTOR_Keywords *
extract(const unsigned char * data,
	size_t size,
	struct EXTRACTOR_Keywords * prev,
	const char * options) {
  QImage *img;
  QByteArray bytes;
  QBuffer buffer;
  unsigned long width;
  unsigned long height;
  char * binary;
  const char * mime;
  int j;
  char * format;
  QImage::Format colors;

  /* if the mime-type of the file is not whitelisted
     do not run the thumbnail extactor! */
  mime = EXTRACTOR_extractLast(EXTRACTOR_MIMETYPE,
			       prev);
  if (mime == NULL)
    return prev;
  j = 0;
  while (whitelist[j] != NULL) {
    if (0 == strcmp(whitelist[j], mime))
      break;
    j++;
  }

  if (whitelist[j] == NULL)
    return prev;

  /* Determine image format to use */
  if (options == NULL)
    colors = QImage::Format_Indexed8;
  else
    switch(atoi(options))
    {
      case 1:
        colors = QImage::Format_Mono;
        break;
      case 8:
        colors = QImage::Format_Indexed8;
        break;
      case 16:
      case 24:
        colors = QImage::Format_RGB32;
        break;
      default:
        colors = QImage::Format_ARGB32;
        break;
    }

#ifdef HAVE_QT_SVG
  if (strcmp(mime, "image/svg+xml") == 0)
  {
    /* Render SVG image */
    QSvgRenderer svg;
    QSize size;

    if (! svg.load(QByteArray((const char *) data)))
      return prev;

    size = svg.defaultSize();
    img = new QImage(size, QImage::Format_ARGB32);

    QPainter painter(img);
    painter.setViewport(0, 0, size.width(), size.height());
    painter.eraseRect(0, 0, size.width(), size.height());

    svg.render(&painter);
  }
  else
#endif
  {
    /* Load image */
    img = new QImage();
    img->loadFromData(data, size);
  }

  height = img->height();
  width = img->width();
  format = (char *) malloc(64);
  snprintf(format,
	   64,
	   "%ux%u",
	   (unsigned int) width,
	   (unsigned int) height);
  prev = addKeyword(EXTRACTOR_SIZE,
		 format,
		 prev);
  if (height == 0)
    height = 1;
  if (width == 0)
    width = 1;

  /* Change color depth */
  QImage thumb = img->convertToFormat(colors);
  delete img;

  /* Resize image
   *
   * Qt's scaled() produces poor quality if the image is resized to less than
   * half the size. Therefore, we resize the image in multiple steps.
   * http://lists.trolltech.com/qt-interest/2006-04/msg00376.html */
  while(true)
  {
    width /= 2;
    if (width < THUMBSIZE)
      width = THUMBSIZE;

    height /= 2;
    if (height < THUMBSIZE)
      height = THUMBSIZE;

    thumb = thumb.scaled(width, height, Qt::KeepAspectRatio,
      Qt::SmoothTransformation);

    if (width == THUMBSIZE && height == THUMBSIZE)
      break;
  }

  buffer.setBuffer(&bytes);
  buffer.open(QIODevice::WriteOnly);
  thumb.save(&buffer, "PNG");

  binary
    = EXTRACTOR_binaryEncode((const unsigned char*) bytes.data(),
			     bytes.length());

  if (binary == NULL)
    return prev;

  return addKeyword(EXTRACTOR_THUMBNAIL_DATA,
		    binary,
		    prev);
}

/* create new thread for C++ code (see Mantis #905) */

struct X {
  const unsigned char * data;
  size_t size;
  struct EXTRACTOR_Keywords * prev;
  const char * options;
};

static void * run(void * arg) {
  struct X * x = (struct X*) arg;
  return extract(x->data, x->size, x->prev,
		 x->options);
}

struct EXTRACTOR_Keywords *
libextractor_thumbnailqt_extract(const char * filename,
				 const unsigned char * data,
				 size_t size,
				 struct EXTRACTOR_Keywords * prev,
				 const char * options) {
  pthread_t pt;
  struct X cls;

  void * ret;
  cls.data = data;
  cls.size = size;
  cls.prev = prev;
  cls.options = options;
  if (0 == pthread_create(&pt, NULL, &run, &cls))
    if (0 == pthread_join(pt, &ret))
      return (struct EXTRACTOR_Keywords*) ret;
  return prev;
}


struct EXTRACTOR_Keywords *
libextractor_thumbnail_extract(const char * filename,
			       const unsigned char * data,
			       size_t size,
			       struct EXTRACTOR_Keywords * prev,
			       const char * options) {
  return libextractor_thumbnailqt_extract(filename,
					  data,
					  size,
					  prev,
					  options);
}

} // extern "C"

/* end of thumbnailextractorqt.cc */