static GdkPixbuf* decode_image (const char *val) { int i; GError *error = NULL; GdkPixbuf *res = NULL; struct { const char *prefix; const char *mime_type; } formats[] = { { "data:image/x-icon;base64,", "image/x-icon" }, { "data:image/png;base64,", "image/png" } }; g_return_val_if_fail (val, NULL); for (i = 0; i < G_N_ELEMENTS (formats); i++) { if (g_str_has_prefix (val, formats[i].prefix)) { gsize len; guchar *data = NULL; char *unescaped; unescaped = g_uri_unescape_string (val + strlen (formats[i].prefix), NULL); if (unescaped) { data = g_base64_decode (unescaped, &len); g_free (unescaped); } if (data) { GdkPixbufLoader *loader; loader = gdk_pixbuf_loader_new_with_mime_type (formats[i].mime_type, &error); if (loader && gdk_pixbuf_loader_write (loader, data, len, &error) && gdk_pixbuf_loader_close (loader, &error)) { res = gdk_pixbuf_loader_get_pixbuf (loader); g_object_ref (res); } g_object_unref (loader); g_free (data); } } } if (!res) { if (error) { g_warning ("%s\n", error->message); g_error_free (error); } else g_warning ("incorrect data uri"); } return res; }
static GdkPixbufLoader * create_loader (GFile *file, const guchar *data, gsize size) { GdkPixbufLoader *loader; GError *error = NULL; char *mime_type; char *filename; loader = NULL; /* need to specify the type here because the gdk_pixbuf_loader_write doesn't have access to the filename in order to correct detect the image type. */ filename = g_file_get_basename (file); mime_type = g_content_type_guess (filename, data, size, NULL); g_free (filename); if (mime_type != NULL) { loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, &error); } if (loader == NULL) { g_debug ("Unable to create loader for mime type %s: %s", mime_type, (error != NULL) ? error->message : "(null)"); g_clear_error (&error); loader = gdk_pixbuf_loader_new (); } g_free (mime_type); return loader; }
static GdkPixbuf * get_icon_from_message (SoupMessage *msg, GetIconURLData *data, GError **error) { GdkPixbufLoader *loader; GdkPixbuf *pixbuf; loader = gdk_pixbuf_loader_new_with_mime_type (data->mime_type, error); if (loader == NULL) return NULL; gdk_pixbuf_loader_write (loader, (guchar *) msg->response_body->data, msg->response_body->length, error); pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); if (pixbuf) { gfloat aspect_ratio; gint height; /* Preserve the aspect-ratio of the original image */ aspect_ratio = (gfloat) data->width / data->height; height = (gint) (PREFERED_WIDTH / aspect_ratio); pixbuf = gdk_pixbuf_scale_simple (pixbuf, PREFERED_WIDTH, height, GDK_INTERP_HYPER); } gdk_pixbuf_loader_close (loader, NULL); g_object_unref (loader); return pixbuf; }
/* Image Data in Memory */ static VALUE initialize_loader(int argc, VALUE *argv, VALUE self) { GdkPixbufLoader* loader; GError* error = NULL; VALUE arg1, is_mime_type; rb_scan_args(argc, argv, "02", &arg1, &is_mime_type); if (NIL_P(arg1)) { loader = gdk_pixbuf_loader_new(); } else { if (is_mime_type == Qtrue) { #if RBGDK_PIXBUF_CHECK_VERSION(2,4,0) loader = gdk_pixbuf_loader_new_with_mime_type(RVAL2CSTR(arg1), &error); #else rb_warning("Not supported GTK+-2.0/2.2."); loader = gdk_pixbuf_loader_new(); #endif } else { /* Default behavior */ loader = gdk_pixbuf_loader_new_with_type(RVAL2CSTR(arg1), &error); } if(error) RAISE_GERROR(error); } G_INITIALIZE(self, loader); return Qnil; }
// FIXME: More refactor this function. static GdkPixbuf* pixbuf_from_url_impl(const char* ctype, const MEMFILE* raw, GError** error) { GdkPixbuf* pixbuf = NULL; #ifdef _WIN32 if (ctype && (!strcmp(ctype, "image/jpeg") || !strcmp(ctype, "image/gif"))) { char temp_path[MAX_PATH]; char temp_filename[MAX_PATH]; GetTempPath(sizeof(temp_path), temp_path); GetTempFileName(temp_path, "growl-for-linux-", 0, temp_filename); FILE* const fp = fopen(temp_filename, "wb"); if (fp) { fwrite(memfdata(raw), memfsize(raw), 1, fp); fclose(fp); } pixbuf = gdk_pixbuf_new_from_file(temp_filename, NULL); DeleteFile(temp_filename); } else #endif { GError* _error = NULL; GdkPixbufLoader* const loader = ctype ? gdk_pixbuf_loader_new_with_mime_type(ctype, &_error) : gdk_pixbuf_loader_new(); if (!gerror_set_or_free(error, _error)) { if (gdk_pixbuf_loader_write(loader, (const guchar*) memfcdata(raw), memfsize(raw), &_error)) pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); else gerror_set_or_free(error, _error); gdk_pixbuf_loader_close(loader, NULL); } } return pixbuf; }
static void file_open_callback (GObject *object, GAsyncResult *res, gpointer user_data) { FileOpenData *data = user_data; NemoImagePropertiesPage *page = data->page; GFile *file; GFileInputStream *stream; GError *error; char *uri; file = G_FILE (object); uri = g_file_get_uri (file); error = NULL; stream = g_file_read_finish (file, res, &error); if (stream) { char *mime_type; mime_type = nemo_file_info_get_mime_type (data->info); page->details->loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, &error); if (error != NULL) { g_warning ("Error creating loader for %s: %s", uri, error->message); g_clear_error (&error); } page->details->pixbuf_still_loading = TRUE; page->details->width = 0; page->details->height = 0; #ifdef HAVE_EXIF page->details->exifldr = exif_loader_new (); #endif /*HAVE_EXIF*/ g_free (mime_type); g_signal_connect (page->details->loader, "size_prepared", G_CALLBACK (size_prepared_callback), page); g_input_stream_read_async (G_INPUT_STREAM (stream), page->details->buffer, sizeof (page->details->buffer), 0, page->details->cancellable, file_read_callback, page); g_object_unref (stream); } else { g_warning ("Error reading %s: %s", uri, error->message); g_clear_error (&error); load_finished (page); } g_free (uri); g_free (data); }
/** * tny_gtk_pixbuf_stream_new: * @mime_type: the MIME type, for example image/jpeg * * Create an adaptor instance between #TnyStream and #GdkPixbuf * * returns: (caller-owns): a new #TnyStream instance * since: 1.0 * audience: application-developer **/ TnyStream* tny_gtk_pixbuf_stream_new (const gchar *mime_type) { TnyGtkPixbufStream *self = g_object_new (TNY_TYPE_GTK_PIXBUF_STREAM, NULL); TnyGtkPixbufStreamPriv *priv = TNY_GTK_PIXBUF_STREAM_GET_PRIVATE (self); /* TODO: Handle errors */ priv->loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, NULL); return TNY_STREAM (self); }
static gboolean internal_load_image(CustomCellRendererFlexi *cr, const char *src, const char *mime_type) { GdkPixbufLoader *loader; bytestring_t *bs = bytestring_new_from_string(src); GError *err = NULL; if (bs==NULL) { internal_render_error(cr,"No image data."); return FALSE; } if (bs->width!=8) { internal_render_error(cr,"Image data must be octets."); bytestring_free(bs); return FALSE; } loader = gdk_pixbuf_loader_new_with_mime_type(mime_type,&err); if (!loader) goto internal_load_image_fail; if (gdk_pixbuf_loader_write(loader,bs->data,bs->len,&err)==FALSE) goto internal_load_image_fail; err=NULL; if (gdk_pixbuf_loader_close(loader,&err)==FALSE) goto internal_load_image_fail; cr->rendered_type = RENDER_PIXBUF; cr->rendered_value = (GdkPixbuf*) gdk_pixbuf_loader_get_pixbuf(loader); g_object_ref(cr->rendered_value); g_object_unref(loader); bytestring_free(bs); return TRUE; internal_load_image_fail: if (err!=NULL) { internal_render_error(cr,err->message); g_error_free(err); } if (bs) bytestring_free(bs); return FALSE; }
cairo_surface_t * rsvg_cairo_surface_new_from_href (RsvgHandle *handle, const char *href, GError **error) { char *data; gsize data_len; char *mime_type = NULL; GdkPixbufLoader *loader = NULL; GdkPixbuf *pixbuf = NULL; cairo_surface_t *surface = NULL; data = _rsvg_handle_acquire_data (handle, href, &mime_type, &data_len, error); if (data == NULL) return NULL; if (mime_type) { loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, error); } else { loader = gdk_pixbuf_loader_new (); } if (loader == NULL) goto out; if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_len, error)) { gdk_pixbuf_loader_close (loader, NULL); goto out; } if (!gdk_pixbuf_loader_close (loader, error)) goto out; pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); if (!pixbuf) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED, _("Failed to load image '%s': reason not known, probably a corrupt image file"), href); goto out; } surface = rsvg_cairo_surface_from_pixbuf (pixbuf); if (mime_type == NULL) { /* Try to get the information from the loader */ GdkPixbufFormat *format; char **mime_types; if ((format = gdk_pixbuf_loader_get_format (loader)) != NULL) { mime_types = gdk_pixbuf_format_get_mime_types (format); if (mime_types != NULL) mime_type = g_strdup (mime_types[0]); g_strfreev (mime_types); } } if ((handle->priv->flags & RSVG_HANDLE_FLAG_KEEP_IMAGE_DATA) != 0 && mime_type != NULL && cairo_surface_set_mime_data (surface, mime_type, (guchar *) data, data_len, g_free, data) == CAIRO_STATUS_SUCCESS) { data = NULL; /* transferred to the surface */ } out: if (loader) g_object_unref (loader); g_free (mime_type); g_free (data); return surface; }
cairo_surface_t * rsvg_cairo_surface_new_from_href (RsvgHandle *handle, const char *href, GError **error) { guint8 *data; gsize data_len; char *mime_type = NULL; GdkPixbufLoader *loader; GdkPixbuf *pixbuf = NULL; int res; cairo_surface_t *surface; data = _rsvg_handle_acquire_data (handle, href, &mime_type, &data_len, error); if (data == NULL) return NULL; if (mime_type) { loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, error); g_free (mime_type); } else { loader = gdk_pixbuf_loader_new (); } if (loader == NULL) { g_free (data); return NULL; } res = gdk_pixbuf_loader_write (loader, data, data_len, error); g_free (data); if (!res) { gdk_pixbuf_loader_close (loader, NULL); g_object_unref (loader); return NULL; } if (!gdk_pixbuf_loader_close (loader, error)) { g_object_unref (loader); return NULL; } pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); if (!pixbuf) { g_object_unref (loader); g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED, _("Failed to load image '%s': reason not known, probably a corrupt image file"), href); return NULL; } surface = rsvg_cairo_surface_from_pixbuf (pixbuf); g_object_unref (loader); return surface; }
GdkPixbuf * eventd_nd_pixbuf_from_data(GVariant *var, gint width, gint height) { GdkPixbuf *pixbuf = NULL; GError *error = NULL; const gchar *mime_type; GVariant *invar; g_variant_get(var, "(m&sm&sv)", &mime_type, NULL, &invar); if ( g_strcmp0(mime_type, "image/x.eventd.pixbuf") == 0 ) { gboolean a; gint b, w, h, s, n; GVariant *data; g_variant_get(invar, "(iiibii@ay)", &w, &h, &s, &a, &b, &n, &data); /* This is the only format gdk-pixbuf can read */ if ( ( b == 8 ) && ( n == ( a ? 4 : 3 ) ) && ( h * s == (gint) g_variant_get_size(data) ) ) return gdk_pixbuf_new_from_data(g_variant_get_data(data), GDK_COLORSPACE_RGB, a, b, w, h, s, _eventd_nd_pixbuf_free_data, data); g_variant_unref(data); goto end; } if ( ! g_variant_is_of_type(invar, G_VARIANT_TYPE_BYTESTRING) ) goto end; GdkPixbufLoader *loader; const guchar *data; gsize length; data = g_variant_get_data(invar); length = g_variant_get_size(invar); if ( mime_type != NULL ) { loader = gdk_pixbuf_loader_new_with_mime_type(mime_type, &error); if ( loader == NULL ) { g_warning("Couldn't create loader for MIME type '%s': %s", mime_type, error->message); goto end; } GdkPixbufFormat *format; if ( ( ( width > 0 ) || ( height > 0 ) ) && ( ( format = gdk_pixbuf_loader_get_format(loader) ) != NULL ) && gdk_pixbuf_format_is_scalable(format) ) gdk_pixbuf_loader_set_size(loader, width, height); } else loader = gdk_pixbuf_loader_new(); if ( ! gdk_pixbuf_loader_write(loader, data, length, &error) ) { g_warning("Couldn't write image data: %s", error->message); goto error; } if ( ! gdk_pixbuf_loader_close(loader, &error) ) { g_warning("Couldn't load image data: %s", error->message); goto error; } pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader)); error: g_object_unref(loader); end: g_variant_unref(invar); g_variant_unref(var); g_clear_error(&error); return pixbuf; }
gboolean e_composer_selection_is_image_uris (EMsgComposer *composer, GtkSelectionData *selection) { gboolean all_image_uris = TRUE; gchar **uris; guint ii; g_return_val_if_fail (E_IS_MSG_COMPOSER (composer), FALSE); g_return_val_if_fail (selection != NULL, FALSE); uris = gtk_selection_data_get_uris (selection); if (!uris) return FALSE; for (ii = 0; uris[ii] != NULL; ii++) { GFile *file; GFileInfo *file_info; GdkPixbufLoader *loader; const gchar *attribute; const gchar *content_type; gchar *mime_type = NULL; file = g_file_new_for_uri (uris[ii]); attribute = G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE; /* XXX This blocks, but we're requesting the fast content * type (which only inspects filenames), so hopefully * it won't be noticeable. Also, this is best effort * so we don't really care if it fails. */ file_info = g_file_query_info ( file, attribute, G_FILE_QUERY_INFO_NONE, NULL, NULL); if (file_info == NULL) { g_object_unref (file); all_image_uris = FALSE; break; } content_type = g_file_info_get_attribute_string ( file_info, attribute); mime_type = g_content_type_get_mime_type (content_type); g_object_unref (file_info); g_object_unref (file); if (mime_type == NULL) { all_image_uris = FALSE; break; } /* Easy way to determine if a MIME type is a supported * image format: try creating a GdkPixbufLoader for it. */ loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, NULL); g_free (mime_type); if (loader == NULL) { all_image_uris = FALSE; break; } gdk_pixbuf_loader_close (loader, NULL); g_object_unref (loader); } g_strfreev (uris); return all_image_uris; }
static GdkPixbuf* url2pixbuf(const char* url, GError** error) { GdkPixbuf* pixbuf = NULL; GdkPixbufLoader* loader = NULL; GError* _error = NULL; CURL* curl = NULL; MEMFILE* mbody; MEMFILE* mhead; char* head; char* body; unsigned long size; CURLcode res = CURLE_FAILED_INIT; curl = curl_easy_init(); if (!curl) return NULL; mbody = memfopen(); mhead = memfopen(); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, REQUEST_TIMEOUT); curl_easy_setopt(curl, CURLOPT_TIMEOUT, REQUEST_TIMEOUT); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite); curl_easy_setopt(curl, CURLOPT_WRITEDATA, mbody); curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, memfwrite); curl_easy_setopt(curl, CURLOPT_HEADERDATA, mhead); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); res = curl_easy_perform(curl); curl_easy_cleanup(curl); head = memfstrdup(mhead); memfclose(mhead); body = memfstrdup(mbody); size = mbody->size; memfclose(mbody); if (res == CURLE_OK) { char* ctype; char* csize; ctype = get_http_header_alloc(head, "Content-Type"); csize = get_http_header_alloc(head, "Content-Length"); #ifdef _WIN32 if (ctype && (!strcmp(ctype, "image/jpeg") || !strcmp(ctype, "image/gif"))) { char temp_path[MAX_PATH]; char temp_filename[MAX_PATH]; FILE* fp; GetTempPath(sizeof(temp_path), temp_path); GetTempFileName(temp_path, "growl-for-linux-", 0, temp_filename); fp = fopen(temp_filename, "wb"); if (fp) { fwrite(body, size, 1, fp); fclose(fp); } pixbuf = gdk_pixbuf_new_from_file(temp_filename, NULL); DeleteFile(temp_filename); } else #endif { if (ctype) loader = (GdkPixbufLoader*) gdk_pixbuf_loader_new_with_mime_type(ctype, error); if (csize) size = atol(csize); if (!loader) loader = gdk_pixbuf_loader_new(); if (body && gdk_pixbuf_loader_write(loader, (const guchar*) body, size, &_error)) { pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); } } if (ctype) free(ctype); if (csize) free(csize); if (loader) gdk_pixbuf_loader_close(loader, NULL); } else { _error = g_error_new_literal(G_FILE_ERROR, res, curl_easy_strerror(res)); } free(head); free(body); /* cleanup callback data */ if (error && _error) *error = _error; return pixbuf; }