aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/thumbnailqt_extractor.cc
blob: 5f4ad9c9feef9552710d008d080bc743b8ba44f9 (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
/*
     This file is part of libextractor.
     (C) 2006, 2009 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 thumbnailqt_extractor.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"
{

int 
EXTRACTOR_thumbnailqt_extract (const char *data,
			       size_t size,
			       EXTRACTOR_MetaDataProcessor proc,
			       void *proc_cls,
			       const char *options)
{
  QImage *img;
  QByteArray bytes;
  QBuffer buffer;
  unsigned long width;
  unsigned long height;
  char format[64];
  QImage::Format colors;

  /* 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;
      }
  QByteArray din (data, (int) size);

#ifdef HAVE_QT_SVG
  /* Render SVG image */
  QSvgRenderer svg;
  QSize qsize;
  if (svg.load(din))
    {
      qsize = svg.defaultSize();
      img = new QImage(qsize, QImage::Format_ARGB32);
      QPainter painter(img);
      painter.setViewport(0, 0, qsize.width(), qsize.height());
      painter.eraseRect(0, 0, qsize.width(), qsize.height());      
      svg.render(&painter);
    }
  else
#endif
    {
      /* Load image */
      img = new QImage();
      img->loadFromData(din);
    }

  height = img->height();
  width = img->width();
  snprintf(format,
	   sizeof(format),
	   "%ux%u",
	   (unsigned int) width,
	   (unsigned int) height);
  if (0 != proc (proc_cls,
		 "thumbnailqt",
		 EXTRACTOR_METATYPE_IMAGE_DIMENSIONS,
		 EXTRACTOR_METAFORMAT_UTF8,
		 "text/plain",
		 format,
		 strlen(format)+1))
    {
      delete img;
      return 1;
    }
  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 ( (width > THUMBSIZE) || (height > THUMBSIZE) )
    {
      width /= 2;
      if (width < THUMBSIZE)
	width = THUMBSIZE;      
      height /= 2;
      if (height < THUMBSIZE)
	height = THUMBSIZE;      
      thumb = thumb.scaled(width, 
			   height, 
			   Qt::KeepAspectRatio,
			   Qt::SmoothTransformation);
    }
  buffer.setBuffer(&bytes);
  buffer.open(QIODevice::WriteOnly);
  thumb.save(&buffer, "PNG");
  return proc (proc_cls,
	       "thumbnailqt",
	       EXTRACTOR_METATYPE_THUMBNAIL,
	       EXTRACTOR_METAFORMAT_BINARY,
	       "image/png",
	       bytes.data(),
	       bytes.length());
}


int 
EXTRACTOR_thumbnail_extract (const unsigned char *data,
			     size_t size,
			     EXTRACTOR_MetaDataProcessor proc,
			     void *proc_cls,
			     const char *options)
{
  return EXTRACTOR_thumbnail_extract (data, size, proc, proc_cls, options);
}

} // extern "C"

/* end of thumbnailqt_extractor.cc */