diff options
author | Matthias Clasen <maclas@gmx.de> | 2003-07-13 19:43:09 +0000 |
---|---|---|
committer | Matthias Clasen <matthiasc@src.gnome.org> | 2003-07-13 19:43:09 +0000 |
commit | 8c44f09a172b8682f7d6bcf9b340987d6e024fcb (patch) | |
tree | 1892fe261a13853b132425c37601278fd9e666c7 | |
parent | d5da9bebd47b04e430879652da7ecab9a0e24474 (diff) | |
download | gdk-pixbuf-8c44f09a172b8682f7d6bcf9b340987d6e024fcb.tar.gz |
New function to load an image from a file at a specified size. (#105326,
2003-07-13 Matthias Clasen <maclas@gmx.de>
* gdk-pixbuf.h:
* gdk-pixbuf-io.c (gdk_pixbuf_new_from_file_at_size): New function to load an image from a file at a
specified size. (#105326, Dom Lachowicz)
-rw-r--r-- | docs/reference/gdk-pixbuf/gdk-pixbuf-sections.txt | 1 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf-io.c | 84 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf.h | 5 |
3 files changed, 90 insertions, 0 deletions
diff --git a/docs/reference/gdk-pixbuf/gdk-pixbuf-sections.txt b/docs/reference/gdk-pixbuf/gdk-pixbuf-sections.txt index 0d5f406f7..5211e7ae1 100644 --- a/docs/reference/gdk-pixbuf/gdk-pixbuf-sections.txt +++ b/docs/reference/gdk-pixbuf/gdk-pixbuf-sections.txt @@ -56,6 +56,7 @@ GdkPixbufDestroyNotify <SECTION> <FILE>file-loading</FILE> gdk_pixbuf_new_from_file +gdk_pixbuf_new_from_file_at_size </SECTION> <SECTION> diff --git a/gdk-pixbuf/gdk-pixbuf-io.c b/gdk-pixbuf/gdk-pixbuf-io.c index 5f831f3e4..42a19e4c5 100644 --- a/gdk-pixbuf/gdk-pixbuf-io.c +++ b/gdk-pixbuf/gdk-pixbuf-io.c @@ -774,6 +774,90 @@ gdk_pixbuf_new_from_file (const char *filename, } /** + * gdk_pixbuf_new_from_file_at_size: + * @filename: Name of file to load. + * @width: The width the image should have + * @height: The height the image should have + * @error: Return location for an error + * + * Creates a new pixbuf by loading an image from a file. The file format is + * detected automatically. If %NULL is returned, then @error will be set. + * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains. + * The image will be scaled to the requested size. + * + * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if + * any of several error conditions occurred: the file could not be opened, + * there was no loader for the file's format, there was not enough memory to + * allocate the image buffer, or the image file contained invalid data. + * + * Since: 2.4 + **/ +GdkPixbuf * +gdk_pixbuf_new_from_file_at_size (const char *filename, + int width, + int height, + GError **error) +{ + GdkPixbufLoader *loader; + GdkPixbuf *pixbuf; + + guchar buffer [4096]; + int length; + FILE *f; + + g_return_val_if_fail (filename != NULL, NULL); + g_return_val_if_fail (width > 0 && height > 0, NULL); + + f = fopen (filename, "rb"); + if (!f) { + g_set_error (error, + G_FILE_ERROR, + g_file_error_from_errno (errno), + _("Failed to open file '%s': %s"), + filename, g_strerror (errno)); + return NULL; + } + + loader = gdk_pixbuf_loader_new (); + gdk_pixbuf_loader_set_size (loader, width, height); + + while (!feof (f)) { + length = fread (buffer, 1, sizeof (buffer), f); + if (length > 0) + if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) { + fclose (f); + g_object_unref (G_OBJECT (loader)); + return NULL; + } + } + + fclose (f); + + if (!gdk_pixbuf_loader_close (loader, error)) { + g_object_unref (G_OBJECT (loader)); + return NULL; + } + + pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); + + if (!pixbuf) { + g_object_unref (G_OBJECT (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"), + filename); + return NULL; + } + + g_object_ref (pixbuf); + + g_object_unref (G_OBJECT (loader)); + + return pixbuf; +} + +/** * gdk_pixbuf_new_from_xpm_data: * @data: Pointer to inline XPM data. * diff --git a/gdk-pixbuf/gdk-pixbuf.h b/gdk-pixbuf/gdk-pixbuf.h index ebace7274..0f5728f33 100644 --- a/gdk-pixbuf/gdk-pixbuf.h +++ b/gdk-pixbuf/gdk-pixbuf.h @@ -131,6 +131,11 @@ GdkPixbuf *gdk_pixbuf_new_subpixbuf (GdkPixbuf *src_pixbuf, GdkPixbuf *gdk_pixbuf_new_from_file (const char *filename, GError **error); +GdkPixbuf *gdk_pixbuf_new_from_file_at_size (const char *filename, + int width, + int height, + GError **error); + GdkPixbuf *gdk_pixbuf_new_from_data (const guchar *data, GdkColorspace colorspace, gboolean has_alpha, |