aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/thumbnailgtk_extractor.c
blob: 8df70790f7fa3489185ac6a4d81c3ec4f4a27ca2 (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
/*
     This file is part of libextractor.
     Copyright (C) 2005, 2009, 2012 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 3, 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., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
 */
/**
 * @file plugins/thumbnailgtk_extractor.c
 * @author Christian Grothoff
 * @brief this extractor produces a binary (!) encoded
 * thumbnail of images (using gdk pixbuf).  The bottom
 * of the file includes a decoder method that can be used
 * to reproduce the 128x128 PNG thumbnails.  We use
 * libmagic to test if the input data is actually an
 * image before trying to give it to gdk-pixbuf.
 */
#include "platform.h"
#include "extractor.h"
#include <magic.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

/**
 * Target size for the thumbnails (width and height).
 */
#define THUMBSIZE 128

/**
 * Maximum image size supported (to avoid unreasonable
 * allocations)
 */
#define MAX_IMAGE_SIZE (32 * 1024 * 1024)


/**
 * Global handle to MAGIC data.
 */
static magic_t magic;


/**
 * Main method for the gtk-thumbnailer plugin.
 *
 * @param ec extraction context
 */
void
EXTRACTOR_thumbnailgtk_extract_method (struct EXTRACTOR_ExtractContext *ec)
{
  GdkPixbufLoader *loader;
  GdkPixbuf *in;
  GdkPixbuf *out;
  size_t length;
  char *thumb;
  unsigned long width;
  unsigned long height;
  char format[64];
  void *data;
  uint64_t size;
  size_t off;
  ssize_t iret;
  void *buf;
  const char *mime;

  if (-1 == (iret = ec->read (ec->cls,
			      &data,
			      16 * 1024)))
    return;
  if (NULL == (mime = magic_buffer (magic, data, iret)))
    return;
  if (0 != strncmp (mime,
		    "image/",
		    strlen ("image/")))
    return; /* not an image */

  /* read entire image into memory */
  size = ec->get_size (ec->cls);
  if (UINT64_MAX == size)
    size = MAX_IMAGE_SIZE; /* unknown size, cap at max */
  if (size > MAX_IMAGE_SIZE)
    return; /* FAR too big to be an image */
  if (NULL == (buf = malloc (size)))
    return; /* too big to fit into memory on this system */

  /* start with data already read */
  memcpy (buf, data, iret);
  off = iret;
  while (off < size)
    {
      iret = ec->read (ec->cls, &data, size - off);
      if (iret <= 0)
	{
	  /* io error */
	  free (buf);
	  return;
	}
      memcpy (buf + off, data, iret);
      off += iret;
    }

  loader = gdk_pixbuf_loader_new ();
  gdk_pixbuf_loader_write (loader, 
			   buf, 
			   size, NULL);
  free (buf);
  in = gdk_pixbuf_loader_get_pixbuf (loader);
  gdk_pixbuf_loader_close (loader, NULL);
  if (NULL == in)
    {
      g_object_unref (loader);
      return;
    }
  g_object_ref (in);
  g_object_unref (loader);
  height = gdk_pixbuf_get_height (in);
  width = gdk_pixbuf_get_width (in);
  snprintf (format, 
	    sizeof (format),
	    "%ux%u",
	    (unsigned int) width, 
	    (unsigned int) height);
  if (0 != ec->proc (ec->cls,
		     "thumbnailgtk",
		     EXTRACTOR_METATYPE_IMAGE_DIMENSIONS,
		     EXTRACTOR_METAFORMAT_UTF8,
		     "text/plain",
		     format,
		     strlen (format) + 1))
    {
      g_object_unref (in);
      return;
    }
  if ((height <= THUMBSIZE) && (width <= THUMBSIZE))
    {
      g_object_unref (in);
      return;
    }
  if (height > THUMBSIZE)
    {
      width = width * THUMBSIZE / height;
      height = THUMBSIZE;
    }
  if (width > THUMBSIZE)
    {
      height = height * THUMBSIZE / width;
      width = THUMBSIZE;
    }
  if ( (0 == height) || (0 == width) )
    {
      g_object_unref (in);
      return;
    }
  out = gdk_pixbuf_scale_simple (in, width, height,
				 GDK_INTERP_BILINEAR);
  g_object_unref (in);
  thumb = NULL;
  length = 0;
  if (NULL == out)
    return;
  if (! gdk_pixbuf_save_to_buffer (out, &thumb, 
				   &length, 
				   "png", NULL, 
				   "compression", "9", 
				   NULL))
    {
      g_object_unref (out);
      return;
    }
  g_object_unref (out);
  if (NULL == thumb)
    return;
  ec->proc (ec->cls,
	    "thumbnailgtk",
	    EXTRACTOR_METATYPE_THUMBNAIL,
	    EXTRACTOR_METAFORMAT_BINARY,
	    "image/png",
	    thumb, length);  
  free (thumb);
}


/**
 * This plugin sometimes is installed under the alias 'thumbnail'.
 * So we need to provide a second entry method.
 *
 * @param ec extraction context
 */
void
EXTRACTOR_thumbnail_extract_method (struct EXTRACTOR_ExtractContext *ec)
{
  EXTRACTOR_thumbnailgtk_extract_method (ec);
}


/**
 * Initialize glib and load magic file.
 */
void __attribute__ ((constructor)) 
thumbnailgtk_gobject_init ()
{
#if !GLIB_CHECK_VERSION(2, 35, 0)
  g_type_init ();
#endif
  magic = magic_open (MAGIC_MIME_TYPE);
  if (0 != magic_load (magic, NULL))
    {
      /* FIXME: how to deal with errors? */
    }
}


/**
 * Destructor for the library, cleans up.
 */
void __attribute__ ((destructor)) 
thumbnailgtk_ltdl_fini () 
{
  if (NULL != magic)
    {
      magic_close (magic);
      magic = NULL;
    }
}

/* end of thumbnailgtk_extractor.c */