aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/thumbnailgtk_extractor.c
blob: 51e3ea409e9b02c43424917c138ca30b3f559e0b (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
/*
     This file is part of libextractor.
     (C) 2005, 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 thumbnailextractor.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.
 */

#include "platform.h"
#include "extractor.h"
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#define THUMBSIZE 128

/* using libgobject, needs init! */
void __attribute__ ((constructor)) ole_gobject_init ()
{
  g_type_init ();
}


const char *
EXTRACTOR_thumbnailgtk_options ()
{
  /* 
     Since the Gnome developers think that being unable to
     unload plugins is an 'acceptable' limitation, we
     require out-of-process execution for plugins depending
     on libgsf and other glib-based plugins.
     See also https://bugzilla.gnome.org/show_bug.cgi?id=374940 
  */
  return "oop-only"; 
}


int 
EXTRACTOR_thumbnailgtk_extract (const char *data,
				size_t size,
				EXTRACTOR_MetaDataProcessor proc,
				void *proc_cls,
				const char *options)
{
  GdkPixbufLoader *loader;
  GdkPixbuf *in;
  GdkPixbuf *out;
  size_t length;
  char *thumb;
  unsigned long width;
  unsigned long height;
  char format[64];
  int ret;

  loader = gdk_pixbuf_loader_new ();
  gdk_pixbuf_loader_write (loader, 
			   (const unsigned char*) data, 
			   size, NULL);
  in = gdk_pixbuf_loader_get_pixbuf (loader);
  gdk_pixbuf_loader_close (loader, NULL);
  if (in == NULL)
    {
      g_object_unref (loader);
      return 0;
    }
  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 != proc (proc_cls,
		 "thumbnailgtk",
		 EXTRACTOR_METATYPE_IMAGE_DIMENSIONS,
		 EXTRACTOR_METAFORMAT_UTF8,
		 "text/plain",
		 format,
		 strlen (format) + 1))
    {
      g_object_unref (in);
      return 1;
    }
  if ((height <= THUMBSIZE) && (width <= THUMBSIZE))
    {
      g_object_unref (in);
      return 0;
    }
  if (height > THUMBSIZE)
    {
      width = width * THUMBSIZE / height;
      height = THUMBSIZE;
    }
  if (width > THUMBSIZE)
    {
      height = height * THUMBSIZE / width;
      width = THUMBSIZE;
    }
  if ( (height == 0) || (width == 0) )
    {
      g_object_unref (in);
      return 0;
    }
  out = gdk_pixbuf_scale_simple (in, width, height, GDK_INTERP_BILINEAR);
  g_object_unref (in);
  thumb = NULL;
  length = 0;
  if (out == NULL)
    return 0;
  if (!gdk_pixbuf_save_to_buffer (out, &thumb, &length, "png", NULL, 
				  "compression", "9", NULL))
    {
      g_object_unref (out);
      return 0;
    }
  g_object_unref (out);
  if (thumb == NULL)
    return 0;
  ret = proc (proc_cls,
	      "thumbnailgtk",
	      EXTRACTOR_METATYPE_THUMBNAIL,
	      EXTRACTOR_METAFORMAT_BINARY,
	      "image/png",
	      thumb, length);  
  free (thumb);
  return ret;
}

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


/* end of thumbnailgtk_extractor.c */