static GdkPixbuf *
pixbuf_from_buddy_icon (PurpleBuddyIcon *buddy_icon)
{
	GdkPixbuf *icon;
	const guchar *data;
	size_t len;
	GdkPixbufLoader *loader;

	data = purple_buddy_icon_get_data (buddy_icon, &len);

	loader = gdk_pixbuf_loader_new ();
	gdk_pixbuf_loader_set_size (loader, 48, 48);
	gdk_pixbuf_loader_write (loader, data, len, NULL);
	gdk_pixbuf_loader_close (loader, NULL);

	icon = gdk_pixbuf_loader_get_pixbuf (loader);

	if (icon) {
		g_object_ref (icon);
	}

	g_object_unref (loader);

	return icon;
}
Esempio n. 2
0
static void
pixbuf_loader_size_prepared_cb (GdkPixbufLoader *loader,
				int              width,
				int              height,
				gpointer         user_data)
{
	ScaleData *scale_data = user_data;

	scale_data->original_width = width;
	scale_data->original_height = height;
	scale_data->loader_width = width;
	scale_data->loader_height = height;

	if (scale_data->requested_size == -1)
		return;

	if (scale_keeping_ratio (&scale_data->loader_width,
				 &scale_data->loader_height,
				 scale_data->requested_size,
				 scale_data->requested_size,
				 FALSE))
	{
		gdk_pixbuf_loader_set_size (loader,
					    scale_data->loader_width,
					    scale_data->loader_height);
	}
}
Esempio n. 3
0
/**
 * on_image_size_prepared:
 * @pixbuf_loader: #GdkPixbufLoader loading the image
 * @width: the original width of the image
 * @height: the original height of the image
 * @data: pointer to the #Dimensions sructure containing available width and height for the image,
 *        available width or height can be -1 if the dimension is not limited
 *
 * Private function.
 *
 * Sets the size of the image being loaded to fit the available width and height dimensions,
 * but never scales up the image beyond its actual size.
 * Intended to be used as a callback for #GdkPixbufLoader "size-prepared" signal.
 */
static void
on_image_size_prepared (GdkPixbufLoader *pixbuf_loader,
                        gint             width,
                        gint             height,
                        gpointer         data)
{
  Dimensions *available_dimensions = data;
  int available_width = available_dimensions->width;
  int available_height = available_dimensions->height;
  int scaled_width;
  int scaled_height;
  int final_width = width;
  int final_height = height;


  if (compute_pixbuf_scale (width, height, available_width, available_height,
                            &scaled_width, &scaled_height)) {
    final_width = scaled_width;
    final_height = scaled_height;
  }

  final_width = (int)((double) final_width * active_scale);
  final_height = (int)((double) final_height * active_scale);
  gdk_pixbuf_loader_set_size (pixbuf_loader, final_width, final_height);
}
static void
size_prepared_exact_cb (GdkPixbufLoader *loader,
                        gint             width,
                        gint             height,
                        HDImageSize     *destination_size)
{
  if (width == destination_size->width &&
      height == destination_size->height)
    {
      gdk_pixbuf_loader_set_size (loader, width, height);
    }
  else
    {
      gdk_pixbuf_loader_set_size (loader,
                                  MIN (width, destination_size->width - 1),
                                  MIN (height, destination_size->height - 1));
    }
}
Esempio n. 5
0
static void
contact_photo_size (GdkPixbufLoader *loader, gint width, gint height,
                    gpointer user_data)
{
  /* Max height of GTK_ICON_SIZE_DIALOG */
  gint iconwidth, iconheight;
  gtk_icon_size_lookup (GTK_ICON_SIZE_DIALOG, &iconwidth, &iconheight);

  gdk_pixbuf_loader_set_size (loader, width / ((gdouble) height / iconheight),
      iconheight);
}
Esempio n. 6
0
static void
on_loader_size_prepared (GdkPixbufLoader *loader,
                         gint width,
                         gint height,
                         gpointer user_data)
{
  AsyncImageData *data = user_data;
  gdk_pixbuf_loader_set_size (loader,
                              width * data->scale_factor,
                              height * data->scale_factor);
}
Esempio n. 7
0
/**
 * on_image_size_prepared:
 * @pixbuf_loader: #GdkPixbufLoader loading the image
 * @width: the original width of the image
 * @height: the original height of the image
 * @data: pointer to the #Dimensions sructure containing available width and height for the image,
 *        available width or height can be -1 if the dimension is not limited
 *
 * Private function.
 *
 * Sets the size of the image being loaded to fit the available width and height dimensions,
 * but never scales up the image beyond its actual size.
 * Intended to be used as a callback for #GdkPixbufLoader "size-prepared" signal.
 */
static void
on_image_size_prepared (GdkPixbufLoader *pixbuf_loader,
                        gint             width,
                        gint             height,
                        gpointer         data)
{
  Dimensions *available_dimensions = data;
  int available_width = available_dimensions->width;
  int available_height = available_dimensions->height;
  int scaled_width;
  int scaled_height;

  if (compute_pixbuf_scale (width, height, available_width, available_height,
                            &scaled_width, &scaled_height))
    gdk_pixbuf_loader_set_size (pixbuf_loader, scaled_width, scaled_height);
}
static void
thumbnail_loader_size_prepared (GdkPixbufLoader *loader,
				int width,
				int height,
				ThumbnailLoadArgs *args)
{
	int size = MAX (width, height);

	args->original_width = width;
	args->original_height = height;

	if (args->force_nominal) {
		args->base_size = size;                        
	} else if (args->base_size == 0) {
		if (args->is_thumbnail) {
			args->base_size = 128 * NAUTILUS_ICON_SIZE_STANDARD / thumbnail_icon_size;
		} else {
			if (size > args->nominal_size * thumbnail_icon_size / NAUTILUS_ICON_SIZE_STANDARD) {
				args->base_size = size * NAUTILUS_ICON_SIZE_STANDARD / thumbnail_icon_size;
			} else if (size > NAUTILUS_ICON_SIZE_STANDARD) {
				args->base_size = args->nominal_size;
			} else {
				/* Don't scale up small icons */
				args->base_size = NAUTILUS_ICON_SIZE_STANDARD;
			}
		}
	}

	if (args->base_size != args->nominal_size) {
		double scale;

		scale = (double) args->nominal_size / args->base_size;

		if ((int) (width * scale) > NAUTILUS_ICON_MAXIMUM_SIZE ||
		    (int) (height * scale) > NAUTILUS_ICON_MAXIMUM_SIZE) {
			scale = MIN ((double) NAUTILUS_ICON_MAXIMUM_SIZE / width,
				     (double) NAUTILUS_ICON_MAXIMUM_SIZE / height);
		}

		width = MAX (1, floor (width * scale + 0.5));
		height = MAX (1, floor (height * scale + 0.5));

		gdk_pixbuf_loader_set_size (loader, width, height);
	}

}
Esempio n. 9
0
static void
size_prepared_cb (GdkPixbufLoader *loader,
                  gint             width,
                  gint             height,
                  gpointer         user_data)
{
	gfloat scale;

	if (max_width_in_bytes < 1 || width <= max_width_in_bytes) {
		return;
	}

	g_debug ("Resizing media art to %d width", max_width_in_bytes);

	scale = width / (gfloat) max_width_in_bytes;

	gdk_pixbuf_loader_set_size (loader, (gint) (width / scale), (gint) (height / scale));
}
Esempio n. 10
0
static void
size_prepared_cb (GdkPixbufLoader *loader, 
		  int              width,
		  int              height,
		  gpointer         data)
{
	SizePrepareContext *info = data;

	g_return_if_fail (width > 0 && height > 0);

	info->input_width = width;
	info->input_height = height;
	
	if (width < info->width && height < info->height) return;
	
	if (info->preserve_aspect_ratio && 
	    (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) {
			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;
		}
	} else {
		if (info->width > 0)
			width = info->width;
		if (info->height > 0)
			height = info->height;
	}
	
	gdk_pixbuf_loader_set_size (loader, width, height);
}
Esempio n. 11
0
static GdkPixbuf*
load_background (gchar *image_name, gint width, gint height)
{
	GdkPixbufLoader *loader;
	gchar *data;
	gsize length;
	GdkPixbuf *pixbuf = NULL;
	GError *error = NULL;

	if (g_file_get_contents (image_name, &data, &length, NULL))
	{
		loader = gdk_pixbuf_loader_new ();
		gdk_pixbuf_loader_set_size (loader, width, height);
		gdk_pixbuf_loader_write (loader, data, length, &error);
		pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
	}

	return pixbuf;
}
Esempio n. 12
0
static void
pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
    int width,
    int height,
    struct SizeData *data)
{
  g_return_if_fail (width > 0 && height > 0);

  if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0))
    {
      if (data->width < 0)
        {
          width = width * (double) data->height / (gdouble) height;
          height = data->height;
        }
      else if (data->height < 0)
        {
          height = height * (double) data->width / (double) width;
          width = data->width;
        }
      else if ((double) height * (double) data->width >
           (double) width * (double) data->height)
        {
          width = 0.5 + (double) width * (double) data->height / (double) height;
          height = data->height;
        }
      else
        {
          height = 0.5 + (double) height * (double) data->width / (double) width;
          width = data->width;
        }
    }
  else
    {
      if (data->width > 0)
        width = data->width;

      if (data->height > 0)
        height = data->height;
    }

  gdk_pixbuf_loader_set_size (loader, width, height);
}
static void
pixbuf_loader_size_prepared (GdkPixbufLoader *loader,
			     int              width,
			     int              height,
			     gpointer         desired_size_ptr)
{
	int size, desired_size;
	float scale;

	size = MAX (width, height);
	desired_size = GPOINTER_TO_INT (desired_size_ptr);

	if (size != desired_size) {
		scale = (float) desired_size / size;
		gdk_pixbuf_loader_set_size (loader,
					    floor (scale * width + 0.5),
					    floor (scale * height + 0.5));
	}
}
Esempio n. 14
0
static void
size_prepared_cb (GdkPixbufLoader *loader, 
		  int              width,
		  int              height,
		  gpointer         data)
{
	struct {
		int width;
		int height;
	} *info = data;

	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);
}
Esempio n. 15
0
static void
cover_thumbnailer_size_prepared (GdkPixbufLoader        *loader,
                                 gint                    source_width,
                                 gint                    source_height,
                                 TumblerThumbnailFlavor *flavor)
{

  gint    dest_width;
  gint    dest_height;
  gdouble hratio;
  gdouble wratio;

  g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
  g_return_if_fail (TUMBLER_IS_THUMBNAIL_FLAVOR (flavor));

  /* get the destination size */
  tumbler_thumbnail_flavor_get_size (flavor, &dest_width, &dest_height);

  if (source_width <= dest_width && source_height <= dest_height)
    {
      /* do not scale the image */
      dest_width = source_width;
      dest_height = source_height;
    }
  else
    {
      /* determine which axis needs to be scaled down more */
      wratio = (gdouble) source_width / (gdouble) dest_width;
      hratio = (gdouble) source_height / (gdouble) dest_height;

      /* adjust the other axis */
      if (hratio > wratio)
        dest_width = rint (source_width / hratio);
     else
        dest_height = rint (source_height / wratio);
    }

  gdk_pixbuf_loader_set_size (loader, MAX (dest_width, 1), MAX (dest_height, 1));
}
Esempio n. 16
0
static void
size_prepared_cb (GdkPixbufLoader *loader,
                  gint             width,
                  gint             height,
                  HDImageSize     *size)
{
  HDImageSize image_size = {width, height};
  HDImageSize minimum_size;
  double scale;

  minimum_size.width = minimum_size.height = MAX (size->width, size->height);

  scale = get_scale_for_aspect_ratio (&image_size, &minimum_size);

  /* Do not load images bigger than neccesary */
  if (scale < 1)
    {
      image_size.width *= scale;
      image_size.height *= scale;
    }

  gdk_pixbuf_loader_set_size (loader, image_size.width, image_size.height);
}
Esempio n. 17
0
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;
}
Esempio n. 18
0
void size_func (GdkPixbufLoader *loader, gint width, gint height, gpointer data)
{
        gdk_pixbuf_loader_set_size (loader, width*2, height*2);
}
static struct graphics_image_priv *
image_new(struct graphics_priv *gr, struct graphics_image_methods *meth, char *name, int *w, int *h, struct point *hot, int rotation)
{
	GdkPixbuf *pixbuf;
	struct graphics_image_priv *ret;
	const char *option;
	
	if (!strcmp(name,"buffer:")) {
		struct graphics_image_buffer *buffer=(struct graphics_image_buffer *)name;
		GdkPixbufLoader *loader=gdk_pixbuf_loader_new();
		if (!loader)
			return NULL;
		if (*w != -1 || *h != -1)
			gdk_pixbuf_loader_set_size(loader, *w, *h);
		gdk_pixbuf_loader_write(loader, buffer->start, buffer->len, NULL);
		gdk_pixbuf_loader_close(loader, NULL);
		pixbuf=gdk_pixbuf_loader_get_pixbuf(loader);
		g_object_ref(pixbuf);
		g_object_unref(loader);
	} else {
		if (*w == -1 && *h == -1)
			pixbuf=gdk_pixbuf_new_from_file(name, NULL);
		else
			pixbuf=gdk_pixbuf_new_from_file_at_size(name, *w, *h, NULL);
	}

	if (!pixbuf)
		return NULL;

	if (rotation) {
		GdkPixbuf *tmp;
		switch (rotation) {
		case 90:
			rotation=270;
			break;
		case 180:
			break;
		case 270:
			rotation=90;
			break;
		default:
			return NULL;
		}

		tmp=gdk_pixbuf_rotate_simple(pixbuf, rotation);

		if (!tmp) {
			g_object_unref(pixbuf);
			return NULL;
		}

		g_object_unref(pixbuf);
		pixbuf=tmp;
	}

	ret=g_new0(struct graphics_image_priv, 1);
	ret->pixbuf=pixbuf;
	ret->w=gdk_pixbuf_get_width(pixbuf);
	ret->h=gdk_pixbuf_get_height(pixbuf);
	*w=ret->w;
	*h=ret->h;
	if (hot) {
		option=gdk_pixbuf_get_option(pixbuf, "x_hot");
		if (option)
			hot->x=atoi(option);
		else
			hot->x=ret->w/2-1;
		option=gdk_pixbuf_get_option(pixbuf, "y_hot");
		if (option)
			hot->y=atoi(option);
		else
			hot->y=ret->h/2-1;
	}
	return ret;
}
Esempio n. 20
0
/*!
Loads an image from from a byte buffer. Note: If the specified width and/or
height does not match the size of the image contained in the byte buffer,
some image information will be lost. WARNING: TODO
@param iDisplayWidth the width to which the image needs to be scaled. Values
are in device units. Setting this to -1 will cause the image not to be scaled.
@param iDisplayheight the height to which the image needs to be scaled. Values
are in device units. Setting this to -1 will cause the image not to be scaled.
@return true if successful, false otherwise
*/
bool GR_UnixImage::convertFromBuffer(const UT_ByteBuf* pBB, 
                                     const std::string & /*mimetype */,
                                     UT_sint32 iDisplayWidth,
                                     UT_sint32 iDisplayHeight)
{
	// assert no image loaded yet

	UT_ASSERT(!m_image);

	bool forceScale = (iDisplayWidth != -1 && iDisplayHeight != -1);
	
	GError * err = 0;
	GdkPixbufLoader * ldr = gdk_pixbuf_loader_new ();	

	if (!ldr)
	{
		UT_DEBUGMSG (("GdkPixbuf: couldn't create loader! WTF?\n"));
		UT_ASSERT (ldr);
		return false;
	}
	
	if (forceScale) {
		setDisplaySize(iDisplayWidth, iDisplayHeight);
		gdk_pixbuf_loader_set_size(ldr, iDisplayWidth, iDisplayHeight);
	}
	
	if ( !gdk_pixbuf_loader_write (ldr, static_cast<const guchar *>(pBB->getPointer (0)),static_cast<gsize>(pBB->getLength ()), &err) )
	{
		if(err != NULL)
		{
			UT_DEBUGMSG(("DOM: couldn't write to loader:%s \n", err->message));
			g_error_free(err);
		}
		gdk_pixbuf_loader_close (ldr, NULL);
		g_object_unref(G_OBJECT(ldr));
		return false;
	}

	if ( !gdk_pixbuf_loader_close (ldr, &err) )
	{
		if(err != NULL)
		{
			UT_DEBUGMSG(("DOM: couldn't close loader:%s \n", err->message));
			g_error_free(err);
		}
		g_object_unref(G_OBJECT(ldr));
		return false;
	}

//
// This is just pointer to the buffer in the loader. This can be deleted
// when we close the loader.
//
	m_image = gdk_pixbuf_loader_get_pixbuf (ldr);
	if (!m_image)
	{
		UT_DEBUGMSG (("GdkPixbuf: couldn't get image from loader!\n"));
		gdk_pixbuf_loader_close (ldr, NULL);
		g_object_unref(G_OBJECT(ldr));
		return false;
	}

	G_IS_OBJECT(G_OBJECT(m_image));
//
// Have to put a reference on it to prevent it being deleted during the close.
//
	g_object_ref(G_OBJECT(m_image));
	if ( FALSE == gdk_pixbuf_loader_close (ldr, &err) )
	{
		UT_DEBUGMSG(("DOM: error closing loader. Corrupt image: %s\n", err->message));
		g_error_free(err);
		g_object_unref(G_OBJECT(m_image));
		return false;
	}
	g_object_unref(G_OBJECT(ldr));
//
// Adjust the reference count so it's always 1. Unfortunately 
// the reference count after the close is undefined.
//
	while(G_OBJECT(m_image)->ref_count > 1)
	{
		g_object_unref(G_OBJECT(m_image));
	}
	UT_ASSERT(G_OBJECT(m_image)->ref_count == 1);
	
	// if gdk_pixbuf_loader_set_size was not able to scale the image, then do it manually
	if (forceScale && (iDisplayWidth != gdk_pixbuf_get_width (m_image) || iDisplayHeight != gdk_pixbuf_get_height(m_image)))
		scale (iDisplayWidth, iDisplayHeight);
	UT_ASSERT(G_OBJECT(m_image)->ref_count == 1);
	
	return true;
}
Esempio n. 21
0
GdkPixbuf *
my_new_from_file_at_size (const char *filename,
			  int         width, 
			  int         height,
			  GError    **error)
{
	GdkPixbufLoader *loader;
	GdkPixbuf       *pixbuf;
	struct {
		int width;
		int height;
	} info;
	struct stat st;

	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);

	if (g_stat (filename, &st) != 0) {
		g_set_error (error,
			     G_FILE_ERROR,
			     g_file_error_from_errno (errno),
			     _("Could not get information for file '%s': %s"),
			     filename, g_strerror (errno));
		return NULL;
	}

	if (!S_ISREG (st.st_mode))
		return NULL;

	f = g_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 ();
#ifdef DONT_PRESERVE_ASPECT
	gdk_pixbuf_loader_set_size (loader, width, height);
#else
	info.width = width;
	info.height = height;
	g_signal_connect (loader, "size-prepared", G_CALLBACK (&size_prepared_cb), &info);
#endif	

	while (!feof (f)) {
		length = fread (buffer, 1, sizeof (buffer), f);
		if (length > 0)
			if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
			        gdk_pixbuf_loader_close (loader, NULL);
				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;
}
Esempio n. 22
0
static VALUE
loader_set_size(VALUE self, VALUE width, VALUE height)
{
    gdk_pixbuf_loader_set_size(_SELF(self), NUM2INT(width), NUM2INT(height));
    return self;
}