Allow -1 for width/height and interpret them as "not constrained".

2005-07-01  Matthias Clasen  <mclasen@redhat.com>

	* gdk-pixbuf-io.c (gdk_pixbuf_new_from_file_at_scale): Allow
	-1 for width/height and interpret them as "not constrained".
	(#309258, Mark McLoughlin)
This commit is contained in:
Matthias Clasen
2005-07-01 16:52:33 +00:00
committed by Matthias Clasen
parent 7dc4d68410
commit e1e07ce4f1
2 changed files with 38 additions and 11 deletions

View File

@ -1,3 +1,9 @@
2005-07-01 Matthias Clasen <mclasen@redhat.com>
* gdk-pixbuf-io.c (gdk_pixbuf_new_from_file_at_scale): Allow
-1 for width/height and interpret them as "not constrained".
(#309258, Mark McLoughlin)
2005-06-26 Tor Lillqvist <tml@novell.com> 2005-06-26 Tor Lillqvist <tml@novell.com>
* Makefile.am: Current GNU tools do understand the PRIVATE * Makefile.am: Current GNU tools do understand the PRIVATE

View File

@ -939,8 +939,19 @@ size_prepared_cb (GdkPixbufLoader *loader,
g_return_if_fail (width > 0 && height > 0); g_return_if_fail (width > 0 && height > 0);
if(info->preserve_aspect_ratio) { if (info->preserve_aspect_ratio &&
if ((double)height * (double)info->width > (info->width > 0 || info->height > 0)) {
if (info->width < 0)
{
width = width * (double)info->height/(double)height;
height = info->height;
}
else if (info->height < 0)
{
height = height * (double)info->width/(double)width;
width = info->width;
}
else if ((double)height * (double)info->width >
(double)width * (double)info->height) { (double)width * (double)info->height) {
width = 0.5 + (double)width * (double)info->height / (double)height; width = 0.5 + (double)width * (double)info->height / (double)height;
height = info->height; height = info->height;
@ -949,7 +960,9 @@ size_prepared_cb (GdkPixbufLoader *loader,
width = info->width; width = info->width;
} }
} else { } else {
if (info->width > 0)
width = info->width; width = info->width;
if (info->height > 0)
height = info->height; height = info->height;
} }
@ -959,8 +972,8 @@ size_prepared_cb (GdkPixbufLoader *loader,
/** /**
* gdk_pixbuf_new_from_file_at_size: * gdk_pixbuf_new_from_file_at_size:
* @filename: Name of file to load, in the GLib file name encoding * @filename: Name of file to load, in the GLib file name encoding
* @width: The width the image should have * @width: The width the image should have or -1 to not constrain the width
* @height: The height the image should have * @height: The height the image should have or -1 to not constrain the height
* @error: Return location for an error * @error: Return location for an error
* *
* Creates a new pixbuf by loading an image from a file. The file format is * Creates a new pixbuf by loading an image from a file. The file format is
@ -1018,8 +1031,8 @@ gdk_pixbuf_new_from_file_at_size (const char *filename,
/** /**
* gdk_pixbuf_new_from_file_at_scale: * gdk_pixbuf_new_from_file_at_scale:
* @filename: Name of file to load, in the GLib file name encoding * @filename: Name of file to load, in the GLib file name encoding
* @width: The width the image should have * @width: The width the image should have or -1 to not constrain the width
* @height: The height the image should have * @height: The height the image should have or -1 to not constrain the height
* @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
* @error: Return location for an error * @error: Return location for an error
* *
@ -1029,6 +1042,13 @@ gdk_pixbuf_new_from_file_at_size (const char *filename,
* The image will be scaled to fit in the requested size, optionally preserving * The image will be scaled to fit in the requested size, optionally preserving
* the image's aspect ratio. * the image's aspect ratio.
* *
* When preserving the aspect ratio, a @width of -1 will cause the image
* to be scaled to the exact given height, and a @height of -1 will cause
* the image to be scaled to the exact given width. When not preserving
* aspect ratio, a @width or @height of -1 means to not scale the image
* at all in that dimension. Negative values for @width and @height are
* allowed since 2.8.
*
* Return value: A newly-created pixbuf with a reference count of 1, or %NULL * 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, * 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 * there was no loader for the file's format, there was not enough memory to
@ -1057,7 +1077,8 @@ gdk_pixbuf_new_from_file_at_scale (const char *filename,
} info; } info;
g_return_val_if_fail (filename != NULL, NULL); g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (width > 0 && height > 0, NULL); g_return_val_if_fail (width > 0 || width == -1, NULL);
g_return_val_if_fail (height > 0 || height == -1, NULL);
f = g_fopen (filename, "rb"); f = g_fopen (filename, "rb");
if (!f) { if (!f) {