aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/thumbnailgtk_extractor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/thumbnailgtk_extractor.c')
-rw-r--r--src/plugins/thumbnailgtk_extractor.c155
1 files changed, 78 insertions, 77 deletions
diff --git a/src/plugins/thumbnailgtk_extractor.c b/src/plugins/thumbnailgtk_extractor.c
index 8df7079..8775de3 100644
--- a/src/plugins/thumbnailgtk_extractor.c
+++ b/src/plugins/thumbnailgtk_extractor.c
@@ -75,14 +75,14 @@ EXTRACTOR_thumbnailgtk_extract_method (struct EXTRACTOR_ExtractContext *ec)
75 const char *mime; 75 const char *mime;
76 76
77 if (-1 == (iret = ec->read (ec->cls, 77 if (-1 == (iret = ec->read (ec->cls,
78 &data, 78 &data,
79 16 * 1024))) 79 16 * 1024)))
80 return; 80 return;
81 if (NULL == (mime = magic_buffer (magic, data, iret))) 81 if (NULL == (mime = magic_buffer (magic, data, iret)))
82 return; 82 return;
83 if (0 != strncmp (mime, 83 if (0 != strncmp (mime,
84 "image/", 84 "image/",
85 strlen ("image/"))) 85 strlen ("image/")))
86 return; /* not an image */ 86 return; /* not an image */
87 87
88 /* read entire image into memory */ 88 /* read entire image into memory */
@@ -98,95 +98,95 @@ EXTRACTOR_thumbnailgtk_extract_method (struct EXTRACTOR_ExtractContext *ec)
98 memcpy (buf, data, iret); 98 memcpy (buf, data, iret);
99 off = iret; 99 off = iret;
100 while (off < size) 100 while (off < size)
101 {
102 iret = ec->read (ec->cls, &data, size - off);
103 if (iret <= 0)
101 { 104 {
102 iret = ec->read (ec->cls, &data, size - off); 105 /* io error */
103 if (iret <= 0) 106 free (buf);
104 { 107 return;
105 /* io error */
106 free (buf);
107 return;
108 }
109 memcpy (buf + off, data, iret);
110 off += iret;
111 } 108 }
109 memcpy (buf + off, data, iret);
110 off += iret;
111 }
112 112
113 loader = gdk_pixbuf_loader_new (); 113 loader = gdk_pixbuf_loader_new ();
114 gdk_pixbuf_loader_write (loader, 114 gdk_pixbuf_loader_write (loader,
115 buf, 115 buf,
116 size, NULL); 116 size, NULL);
117 free (buf); 117 free (buf);
118 in = gdk_pixbuf_loader_get_pixbuf (loader); 118 in = gdk_pixbuf_loader_get_pixbuf (loader);
119 gdk_pixbuf_loader_close (loader, NULL); 119 gdk_pixbuf_loader_close (loader, NULL);
120 if (NULL == in) 120 if (NULL == in)
121 { 121 {
122 g_object_unref (loader); 122 g_object_unref (loader);
123 return; 123 return;
124 } 124 }
125 g_object_ref (in); 125 g_object_ref (in);
126 g_object_unref (loader); 126 g_object_unref (loader);
127 height = gdk_pixbuf_get_height (in); 127 height = gdk_pixbuf_get_height (in);
128 width = gdk_pixbuf_get_width (in); 128 width = gdk_pixbuf_get_width (in);
129 snprintf (format, 129 snprintf (format,
130 sizeof (format), 130 sizeof (format),
131 "%ux%u", 131 "%ux%u",
132 (unsigned int) width, 132 (unsigned int) width,
133 (unsigned int) height); 133 (unsigned int) height);
134 if (0 != ec->proc (ec->cls, 134 if (0 != ec->proc (ec->cls,
135 "thumbnailgtk", 135 "thumbnailgtk",
136 EXTRACTOR_METATYPE_IMAGE_DIMENSIONS, 136 EXTRACTOR_METATYPE_IMAGE_DIMENSIONS,
137 EXTRACTOR_METAFORMAT_UTF8, 137 EXTRACTOR_METAFORMAT_UTF8,
138 "text/plain", 138 "text/plain",
139 format, 139 format,
140 strlen (format) + 1)) 140 strlen (format) + 1))
141 { 141 {
142 g_object_unref (in); 142 g_object_unref (in);
143 return; 143 return;
144 } 144 }
145 if ((height <= THUMBSIZE) && (width <= THUMBSIZE)) 145 if ((height <= THUMBSIZE) && (width <= THUMBSIZE))
146 { 146 {
147 g_object_unref (in); 147 g_object_unref (in);
148 return; 148 return;
149 } 149 }
150 if (height > THUMBSIZE) 150 if (height > THUMBSIZE)
151 { 151 {
152 width = width * THUMBSIZE / height; 152 width = width * THUMBSIZE / height;
153 height = THUMBSIZE; 153 height = THUMBSIZE;
154 } 154 }
155 if (width > THUMBSIZE) 155 if (width > THUMBSIZE)
156 { 156 {
157 height = height * THUMBSIZE / width; 157 height = height * THUMBSIZE / width;
158 width = THUMBSIZE; 158 width = THUMBSIZE;
159 } 159 }
160 if ( (0 == height) || (0 == width) ) 160 if ( (0 == height) || (0 == width) )
161 { 161 {
162 g_object_unref (in); 162 g_object_unref (in);
163 return; 163 return;
164 } 164 }
165 out = gdk_pixbuf_scale_simple (in, width, height, 165 out = gdk_pixbuf_scale_simple (in, width, height,
166 GDK_INTERP_BILINEAR); 166 GDK_INTERP_BILINEAR);
167 g_object_unref (in); 167 g_object_unref (in);
168 thumb = NULL; 168 thumb = NULL;
169 length = 0; 169 length = 0;
170 if (NULL == out) 170 if (NULL == out)
171 return; 171 return;
172 if (! gdk_pixbuf_save_to_buffer (out, &thumb, 172 if (! gdk_pixbuf_save_to_buffer (out, &thumb,
173 &length, 173 &length,
174 "png", NULL, 174 "png", NULL,
175 "compression", "9", 175 "compression", "9",
176 NULL)) 176 NULL))
177 { 177 {
178 g_object_unref (out); 178 g_object_unref (out);
179 return; 179 return;
180 } 180 }
181 g_object_unref (out); 181 g_object_unref (out);
182 if (NULL == thumb) 182 if (NULL == thumb)
183 return; 183 return;
184 ec->proc (ec->cls, 184 ec->proc (ec->cls,
185 "thumbnailgtk", 185 "thumbnailgtk",
186 EXTRACTOR_METATYPE_THUMBNAIL, 186 EXTRACTOR_METATYPE_THUMBNAIL,
187 EXTRACTOR_METAFORMAT_BINARY, 187 EXTRACTOR_METAFORMAT_BINARY,
188 "image/png", 188 "image/png",
189 thumb, length); 189 thumb, length);
190 free (thumb); 190 free (thumb);
191} 191}
192 192
@@ -207,31 +207,32 @@ EXTRACTOR_thumbnail_extract_method (struct EXTRACTOR_ExtractContext *ec)
207/** 207/**
208 * Initialize glib and load magic file. 208 * Initialize glib and load magic file.
209 */ 209 */
210void __attribute__ ((constructor)) 210void __attribute__ ((constructor))
211thumbnailgtk_gobject_init () 211thumbnailgtk_gobject_init ()
212{ 212{
213#if !GLIB_CHECK_VERSION(2, 35, 0) 213#if ! GLIB_CHECK_VERSION (2, 35, 0)
214 g_type_init (); 214 g_type_init ();
215#endif 215#endif
216 magic = magic_open (MAGIC_MIME_TYPE); 216 magic = magic_open (MAGIC_MIME_TYPE);
217 if (0 != magic_load (magic, NULL)) 217 if (0 != magic_load (magic, NULL))
218 { 218 {
219 /* FIXME: how to deal with errors? */ 219 /* FIXME: how to deal with errors? */
220 } 220 }
221} 221}
222 222
223 223
224/** 224/**
225 * Destructor for the library, cleans up. 225 * Destructor for the library, cleans up.
226 */ 226 */
227void __attribute__ ((destructor)) 227void __attribute__ ((destructor))
228thumbnailgtk_ltdl_fini () 228thumbnailgtk_ltdl_fini ()
229{ 229{
230 if (NULL != magic) 230 if (NULL != magic)
231 { 231 {
232 magic_close (magic); 232 magic_close (magic);
233 magic = NULL; 233 magic = NULL;
234 } 234 }
235} 235}
236 236
237
237/* end of thumbnailgtk_extractor.c */ 238/* end of thumbnailgtk_extractor.c */