GdkPixbuf *  XAP_UnixDialog_FileOpenSaveAs::pixbufForByteBuf (UT_ByteBuf * pBB)
{
    if ( !pBB || !pBB->getLength() )
        return NULL;

    GdkPixbuf * pixbuf = NULL;

    bool bIsXPM = false;
    const char * szBuf = reinterpret_cast<const char *>(pBB->getPointer(0));
    if((pBB->getLength() > 9) && (strncmp (szBuf, "/* XPM */", 9) == 0))
    {
        bIsXPM = true;
    }

    if(bIsXPM)
    {
        pixbuf = _loadXPM(pBB);
    }
    else
    {
        GError * err = 0;
        GdkPixbufLoader * ldr = 0;

        ldr = gdk_pixbuf_loader_new ();
        if (!ldr)
        {
            UT_DEBUGMSG (("GdkPixbuf: couldn't create loader! WTF?\n"));
            UT_ASSERT (ldr);
            return NULL ;
        }

        if ( FALSE== gdk_pixbuf_loader_write (ldr, static_cast<const guchar *>(pBB->getPointer (0)),
                                              static_cast<gsize>(pBB->getLength ()), &err) )
        {
            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 NULL ;
        }

        gdk_pixbuf_loader_close (ldr, NULL);
        pixbuf = gdk_pixbuf_loader_get_pixbuf (ldr);

        // ref before closing the loader
        if ( pixbuf )
            g_object_ref (G_OBJECT(pixbuf));

        g_object_unref (G_OBJECT(ldr));
    }

    return pixbuf;
}
GdkPixbuf * IE_ImpGraphic_GdkPixbuf::pixbufForByteBuf (UT_ByteBuf * pBB, 
													   std::string & mimetype)
{
	if ( !pBB || !pBB->getLength() )
		return NULL;

	GdkPixbuf * pixbuf = NULL;

	bool bIsXPM = false;
	const char * szBuf = reinterpret_cast<const char *>(pBB->getPointer(0));
	if((pBB->getLength() > 9) && (strncmp (szBuf, "/* XPM */", 9) == 0))
	{
		bIsXPM = true;
	}

	if(bIsXPM)
	{
		pixbuf = _loadXPM(pBB);
	}
	else
	{
		GError * err = 0;
		GdkPixbufLoader * ldr = 0;

		ldr = gdk_pixbuf_loader_new ();
		if (!ldr)
		{
			UT_DEBUGMSG (("GdkPixbuf: couldn't create loader! WTF?\n"));
			UT_ASSERT (ldr);
			return NULL ;
		}

		if (!gdk_pixbuf_loader_write (ldr, static_cast<const guchar *>(pBB->getPointer (0)),
									  static_cast<gsize>(pBB->getLength ()), &err) )
		{
			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));
			mimetype.clear();
			return NULL ;
		}

		
		gdk_pixbuf_loader_close (ldr, NULL);
		pixbuf = gdk_pixbuf_loader_get_pixbuf (ldr);

		GdkPixbufFormat * format = gdk_pixbuf_loader_get_format(ldr);
		gchar ** mime_types = gdk_pixbuf_format_get_mime_types(format);
		gchar ** current = mime_types;
		while(*current) {
			if((strcmp(*current, "image/jpeg") == 0) 
			   || (strcmp(*current, "image/png") == 0)) {
				mimetype = *current;
				break;
			}
			current++;
		}
		g_strfreev(mime_types);
		

		// ref before closing the loader
		if ( pixbuf )
			g_object_ref (G_OBJECT(pixbuf));

		g_object_unref (G_OBJECT(ldr));
	}

	return pixbuf;
}