diff options
author | Matthias Clasen <maclas@gmx.de> | 2003-07-23 22:55:34 +0000 |
---|---|---|
committer | Matthias Clasen <matthiasc@src.gnome.org> | 2003-07-23 22:55:34 +0000 |
commit | b8118e6507ca3fd850e20df1ed791def0b3583bd (patch) | |
tree | 1e30b48b29074651c20dc18b1889e0eaf653ecd1 /gdk-pixbuf | |
parent | 3264c0b679b9cf0709a049025ae7e8758a14d83f (diff) | |
download | gdk-pixbuf-b8118e6507ca3fd850e20df1ed791def0b3583bd.tar.gz |
Preserve the aspect ratio. (#118145, Owen Taylor)
2003-07-24 Matthias Clasen <maclas@gmx.de>
* gdk-pixbuf-io.c (gdk_pixbuf_new_from_file_at_size): Preserve the aspect ratio. (#118145,
Owen Taylor)
Diffstat (limited to 'gdk-pixbuf')
-rw-r--r-- | gdk-pixbuf/ChangeLog | 5 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf-io.c | 37 |
2 files changed, 40 insertions, 2 deletions
diff --git a/gdk-pixbuf/ChangeLog b/gdk-pixbuf/ChangeLog index d7a08be9d..48fe53ffb 100644 --- a/gdk-pixbuf/ChangeLog +++ b/gdk-pixbuf/ChangeLog @@ -1,3 +1,8 @@ +2003-07-24 Matthias Clasen <maclas@gmx.de> + + * gdk-pixbuf-io.c (gdk_pixbuf_new_from_file_at_size): Preserve the aspect ratio. (#118145, + Owen Taylor) + 2003-07-23 Matthias Clasen <maclas@gmx.de> * gdk-pixbuf-io.c (gdk_pixbuf_new_from_file_at_size): Close loader before unref'ing diff --git a/gdk-pixbuf/gdk-pixbuf-io.c b/gdk-pixbuf/gdk-pixbuf-io.c index 00550e1cb..f77c2bebc 100644 --- a/gdk-pixbuf/gdk-pixbuf-io.c +++ b/gdk-pixbuf/gdk-pixbuf-io.c @@ -773,6 +773,31 @@ gdk_pixbuf_new_from_file (const char *filename, return pixbuf; } +static void +size_prepared_cb (GdkPixbufLoader *loader, + int width, + int height, + gpointer data) +{ + struct { + int width; + int height; + } *info = data; + + g_return_if_fail (width > 0 && height > 0); + + if ((double)height * (double)info->width > + (double)width * (double)info->height) { + width = 0.5 + (double)width * (double)info->height / (double)height; + height = info->height; + } else { + height = 0.5 + (double)height * (double)info->width / (double)width; + width = info->width; + } + + gdk_pixbuf_loader_set_size (loader, width, height); +} + /** * gdk_pixbuf_new_from_file_at_size: * @filename: Name of file to load. @@ -783,7 +808,7 @@ gdk_pixbuf_new_from_file (const char *filename, * 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. + * The image will be scaled to fit in the requested size, preserving its aspect ratio. * * 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, @@ -804,6 +829,10 @@ gdk_pixbuf_new_from_file_at_size (const char *filename, guchar buffer [4096]; int length; FILE *f; + struct { + gint width; + gint height; + } info; g_return_val_if_fail (filename != NULL, NULL); g_return_val_if_fail (width > 0 && height > 0, NULL); @@ -819,7 +848,11 @@ gdk_pixbuf_new_from_file_at_size (const char *filename, } loader = gdk_pixbuf_loader_new (); - gdk_pixbuf_loader_set_size (loader, width, height); + + info.width = width; + info.height = height; + + g_signal_connect (loader, "size-prepared", G_CALLBACK (size_prepared_cb), &info); while (!feof (f)) { length = fread (buffer, 1, sizeof (buffer), f); |