2003-08-28 10:40:14 +00:00
// Pixbuf.custom - Gdk Pixbuf class customizations
2003-02-19 06:00:27 +00:00
//
2003-08-28 10:40:14 +00:00
// Authors:
// Vladimir Vukicevic <vladimir@pobox.com>
// Miguel de Icaza <miguel@ximian.com>
2004-02-12 17:31:37 +00:00
// Mike Kestner <mkestner@ximian.com>
2003-08-28 10:40:14 +00:00
// Duncan Mak <duncan@ximian.com>
// Gonzalo Paniagua Javier <gonzalo@ximian.com>
2005-05-06 14:24:44 +00:00
// Martin Willemoes Hansen <mwh@sysrq.dk>
// Jose Faria <spigaz@gmail.com>
2003-02-19 06:00:27 +00:00
//
2004-06-25 18:42:19 +00:00
// Copyright (c) 2002 Vladimir Vukicevic
// Copyright (c) 2003 Ximian, Inc. (Miguel de Icaza)
// Copyright (c) 2003 Ximian, Inc. (Duncan Mak)
// Copyright (c) 2003 Ximian, Inc. (Gonzalo Paniagua Javier)
// Copyright (c) 2003 Martin Willemoes Hansen
2005-03-09 20:32:24 +00:00
// Copyright (c) 2004-2005 Novell, Inc.
2003-02-19 06:00:27 +00:00
//
2003-08-28 10:40:14 +00:00
// This code is inserted after the automatically generated code.
2004-06-25 18:42:19 +00:00
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
2003-02-19 06:00:27 +00:00
2004-10-29 20:33:07 +00:00
[DllImport("libgdk-win32-2.0-0.dll")]
static extern IntPtr gdk_pixbuf_get_from_drawable(IntPtr raw, IntPtr src, IntPtr cmap, int src_x, int src_y, int dest_x, int dest_y, int width, int height);
public Gdk.Pixbuf GetFromDrawable(Gdk.Drawable src, Gdk.Colormap cmap, int src_x, int src_y, int dest_x, int dest_y, int width, int height) {
IntPtr raw_ret = gdk_pixbuf_get_from_drawable(Handle, src.Handle, cmap.Handle, src_x, src_y, dest_x, dest_y, width, height);
Gdk.Pixbuf ret;
if (raw_ret == IntPtr.Zero)
ret = null;
else
ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret);
return ret;
}
[DllImport("libgdk-win32-2.0-0.dll")]
static extern IntPtr gdk_pixbuf_get_from_image(IntPtr raw, IntPtr src, IntPtr cmap, int src_x, int src_y, int dest_x, int dest_y, int width, int height);
public Gdk.Pixbuf GetFromImage(Gdk.Image src, Gdk.Colormap cmap, int src_x, int src_y, int dest_x, int dest_y, int width, int height) {
IntPtr raw_ret = gdk_pixbuf_get_from_image(Handle, src.Handle, cmap.Handle, src_x, src_y, dest_x, dest_y, width, height);
Gdk.Pixbuf ret;
if (raw_ret == IntPtr.Zero)
ret = null;
else
ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret);
return ret;
}
[DllImport("libgdk-win32-2.0-0.dll")]
2005-04-12 03:39:23 +00:00
static extern void gdk_pixbuf_render_pixmap_and_mask (IntPtr raw, out IntPtr pixmap_return, out IntPtr mask_return, int alpha_threshold);
2004-10-29 20:33:07 +00:00
2005-04-12 03:39:23 +00:00
public void RenderPixmapAndMask (out Pixmap pixmap_return, out Pixmap mask_return, int alpha_threshold)
{
IntPtr mask_handle, pixmap_handle;
gdk_pixbuf_render_pixmap_and_mask (Handle, out pixmap_handle, out mask_handle, alpha_threshold);
pixmap_return = GLib.Object.GetObject (pixmap_handle) as Pixmap;
mask_return = GLib.Object.GetObject (mask_handle) as Pixmap;
2004-10-29 20:33:07 +00:00
}
[DllImport("libgdk-win32-2.0-0.dll")]
2005-04-12 03:39:23 +00:00
static extern void gdk_pixbuf_render_pixmap_and_mask_for_colormap (IntPtr raw, IntPtr colormap, out IntPtr pixmap_return, out IntPtr mask_return, int alpha_threshold);
2004-10-29 20:33:07 +00:00
2005-04-12 03:39:23 +00:00
public void RenderPixmapAndMaskForColormap (Colormap colormap, out Pixmap pixmap_return, out Pixmap mask_return, int alpha_threshold)
{
IntPtr mask_handle, pixmap_handle;
gdk_pixbuf_render_pixmap_and_mask_for_colormap (Handle, colormap == null ? IntPtr.Zero : colormap.Handle, out pixmap_handle, out mask_handle, alpha_threshold);
pixmap_return = GLib.Object.GetObject (pixmap_handle) as Pixmap;
mask_return = GLib.Object.GetObject (mask_handle) as Pixmap;
2004-10-29 20:33:07 +00:00
}
[DllImport("libgdk-win32-2.0-0.dll")]
static extern void gdk_pixbuf_render_threshold_alpha(IntPtr raw, IntPtr bitmap, int src_x, int src_y, int dest_x, int dest_y, int width, int height, int alpha_threshold);
2004-12-03 20:42:12 +00:00
public void RenderThresholdAlpha(Gdk.Pixmap bitmap, int src_x, int src_y, int dest_x, int dest_y, int width, int height, int alpha_threshold) {
2004-10-29 20:33:07 +00:00
gdk_pixbuf_render_threshold_alpha(Handle, bitmap.Handle, src_x, src_y, dest_x, dest_y, width, height, alpha_threshold);
}
[DllImport("libgdk-win32-2.0-0.dll")]
static extern void gdk_pixbuf_render_to_drawable(IntPtr raw, IntPtr drawable, IntPtr gc, int src_x, int src_y, int dest_x, int dest_y, int width, int height, int dither, int x_dither, int y_dither);
public void RenderToDrawable(Gdk.Drawable drawable, Gdk.GC gc, int src_x, int src_y, int dest_x, int dest_y, int width, int height, Gdk.RgbDither dither, int x_dither, int y_dither) {
gdk_pixbuf_render_to_drawable(Handle, drawable.Handle, gc.Handle, src_x, src_y, dest_x, dest_y, width, height, (int) dither, x_dither, y_dither);
}
[DllImport("libgdk-win32-2.0-0.dll")]
static extern void gdk_pixbuf_render_to_drawable_alpha(IntPtr raw, IntPtr drawable, int src_x, int src_y, int dest_x, int dest_y, int width, int height, int alpha_mode, int alpha_threshold, int dither, int x_dither, int y_dither);
public void RenderToDrawableAlpha(Gdk.Drawable drawable, int src_x, int src_y, int dest_x, int dest_y, int width, int height, Gdk.PixbufAlphaMode alpha_mode, int alpha_threshold, Gdk.RgbDither dither, int x_dither, int y_dither) {
gdk_pixbuf_render_to_drawable_alpha(Handle, drawable.Handle, src_x, src_y, dest_x, dest_y, width, height, (int) alpha_mode, alpha_threshold, (int) dither, x_dither, y_dither);
}
2007-01-09 17:00:52 +00:00
public Pixbuf (System.IO.Stream stream) : base (IntPtr.Zero)
{
using (PixbufLoader pl = new PixbufLoader (stream)) {
Raw = pl.PixbufHandle;
}
}
2005-05-06 14:24:44 +00:00
2007-01-09 17:00:52 +00:00
public Pixbuf (System.IO.Stream stream, int width, int height) : base (IntPtr.Zero)
{
using (PixbufLoader pl = new PixbufLoader (stream, width, height)) {
Raw = pl.PixbufHandle;
}
}
2005-05-06 14:24:44 +00:00
2004-04-07 19:15:01 +00:00
public Pixbuf (System.Reflection.Assembly assembly, string resource) : base (IntPtr.Zero)
2005-05-06 14:24:44 +00:00
{
2007-01-09 17:00:52 +00:00
using (PixbufLoader pl = new PixbufLoader (assembly == null ? System.Reflection.Assembly.GetCallingAssembly () : assembly, resource)) {
Raw = pl.PixbufHandle;
}
2005-05-06 14:24:44 +00:00
}
public Pixbuf (System.Reflection.Assembly assembly, string resource, int width, int height) : base (IntPtr.Zero)
{
2007-01-09 17:00:52 +00:00
using (PixbufLoader pl = new PixbufLoader (assembly == null ? System.Reflection.Assembly.GetCallingAssembly () : assembly, resource, width, height)) {
Raw = pl.PixbufHandle;
}
2005-05-06 14:24:44 +00:00
}
2007-01-09 17:00:52 +00:00
public Pixbuf (byte[] buffer) : base (IntPtr.Zero)
{
using (PixbufLoader pl = new PixbufLoader (buffer)) {
Raw = pl.PixbufHandle;
}
}
2005-05-06 14:24:44 +00:00
2007-01-09 17:00:52 +00:00
public Pixbuf (byte[] buffer, int width, int height) : base (IntPtr.Zero)
{
using (PixbufLoader pl = new PixbufLoader (buffer, width, height)) {
Raw = pl.PixbufHandle;
}
}
2003-11-17 07:38:40 +00:00
2003-11-18 19:10:36 +00:00
static public Pixbuf LoadFromResource (string resource)
2002-11-10 10:09:05 +00:00
{
2003-11-18 19:10:36 +00:00
return new Pixbuf (System.Reflection.Assembly.GetCallingAssembly (), resource);
2002-11-10 10:09:05 +00:00
}
2002-09-17 05:29:54 +00:00
2003-08-30 00:26:38 +00:00
[DllImport("libgdk_pixbuf-2.0-0.dll")]
2002-11-10 10:09:05 +00:00
static extern IntPtr gdk_pixbuf_scale_simple(IntPtr raw, int dest_width, int dest_height, int interp_type);
2002-09-17 05:29:54 +00:00
2002-11-10 10:09:05 +00:00
public Gdk.Pixbuf ScaleSimple(int dest_width, int dest_height, Gdk.InterpType interp_type) {
IntPtr raw_ret = gdk_pixbuf_scale_simple(Handle, dest_width, dest_height, (int) interp_type);
2003-07-23 17:19:21 +00:00
Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret, true);
2002-11-10 10:09:05 +00:00
return ret;
}
2002-09-17 05:29:54 +00:00
2003-08-30 00:26:38 +00:00
[DllImport("libgdk_pixbuf-2.0-0.dll")]
2002-11-10 10:09:05 +00:00
static extern IntPtr gdk_pixbuf_composite_color_simple(IntPtr raw, int dest_width, int dest_height, int interp_type, int overall_alpha, int check_size, uint color1, uint color2);
2002-09-17 05:29:54 +00:00
2002-11-10 10:09:05 +00:00
public Gdk.Pixbuf CompositeColorSimple(int dest_width, int dest_height, Gdk.InterpType interp_type, int overall_alpha, int check_size, uint color1, uint color2) {
IntPtr raw_ret = gdk_pixbuf_composite_color_simple(Handle, dest_width, dest_height, (int) interp_type, overall_alpha, check_size, color1, color2);
2003-07-23 17:19:21 +00:00
Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret, true);
2002-11-10 10:09:05 +00:00
return ret;
}
2002-09-17 05:29:54 +00:00
2003-08-30 00:26:38 +00:00
[DllImport("libgdk_pixbuf-2.0-0.dll")]
2002-11-10 10:09:05 +00:00
static extern IntPtr gdk_pixbuf_add_alpha(IntPtr raw, bool substitute_color, byte r, byte g, byte b);
public Gdk.Pixbuf AddAlpha(bool substitute_color, byte r, byte g, byte b) {
IntPtr raw_ret = gdk_pixbuf_add_alpha(Handle, substitute_color, r, g, b);
2003-07-23 17:19:21 +00:00
Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret, true);
2002-11-10 10:09:05 +00:00
return ret;
}
2003-02-19 06:00:27 +00:00
2008-02-23 04:20:02 +00:00
class DestroyHelper {
GCHandle gch;
GCHandle data_handle;
PixbufDestroyNotify notify;
public DestroyHelper (byte[] data, PixbufDestroyNotify notify)
{
gch = GCHandle.Alloc (this);
data_handle = GCHandle.Alloc (data, GCHandleType.Pinned);
this.notify = notify;
}
void ReleaseHandles (byte[] data)
{
data_handle.Free ();
if (notify != null)
notify (data);
gch.Free ();
}
PixbufDestroyNotify handler;
public PixbufDestroyNotify Handler {
get {
if (handler == null)
handler = new PixbufDestroyNotify (ReleaseHandles);
return handler;
}
}
}
2005-04-04 16:27:08 +00:00
[DllImport("libgdk_pixbuf-2.0-0.dll")]
static extern IntPtr gdk_pixbuf_new_from_data(byte[] data, int colorspace, bool has_alpha, int bits_per_sample, int width, int height, int rowstride, GdkSharp.PixbufDestroyNotifyNative destroy_fn, IntPtr destroy_fn_data);
public Pixbuf (byte[] data, Gdk.Colorspace colorspace, bool has_alpha, int bits_per_sample, int width, int height, int rowstride, Gdk.PixbufDestroyNotify destroy_fn) : base (IntPtr.Zero)
2003-02-19 06:00:27 +00:00
{
2005-04-04 16:27:08 +00:00
if (GetType () != typeof (Pixbuf)) {
throw new InvalidOperationException ("Can't override this constructor.");
}
2008-02-23 04:20:02 +00:00
DestroyHelper helper = new DestroyHelper (data, destroy_fn);
GdkSharp.PixbufDestroyNotifyWrapper destroy_fn_wrapper = new GdkSharp.PixbufDestroyNotifyWrapper (helper.Handler);
2005-04-04 16:27:08 +00:00
Raw = gdk_pixbuf_new_from_data(data, (int) colorspace, has_alpha, bits_per_sample, width, height, rowstride, destroy_fn_wrapper.NativeDelegate, IntPtr.Zero);
PersistentData ["new_from_data_destroy_fn_wrapper"] = destroy_fn_wrapper;
2003-02-19 06:00:27 +00:00
}
2005-04-04 16:27:08 +00:00
// overload to default the colorspace
public Pixbuf(byte [] data, bool has_alpha, int bits_per_sample, int width, int height, int rowstride, Gdk.PixbufDestroyNotify destroy_fn) : this (data, Gdk.Colorspace.Rgb, has_alpha, bits_per_sample, width, height, rowstride, destroy_fn) {}
2008-02-23 04:20:02 +00:00
public Pixbuf(byte [] data, bool has_alpha, int bits_per_sample, int width, int height, int rowstride) : this (data, Gdk.Colorspace.Rgb, has_alpha, bits_per_sample, width, height, rowstride, null) {}
public Pixbuf(byte [] data, Gdk.Colorspace colorspace, bool has_alpha, int bits_per_sample, int width, int height, int rowstride) : this (data, colorspace, has_alpha, bits_per_sample, width, height, rowstride, null) {}
2004-04-07 19:15:01 +00:00
public unsafe Pixbuf(byte[] data, bool copy_pixels) : base (IntPtr.Zero)
2003-02-19 06:00:27 +00:00
{
IntPtr error = IntPtr.Zero;
Raw = gdk_pixbuf_new_from_inline(data.Length, data, copy_pixels, out error);
if (error != IntPtr.Zero) throw new GLib.GException (error);
}
2003-11-30 03:07:41 +00:00
[DllImport("libgdk_pixbuf-2.0-0.dll")]
2003-02-19 06:00:27 +00:00
static extern unsafe IntPtr gdk_pixbuf_new_from_inline(int len, IntPtr data, bool copy_pixels, out IntPtr error);
2003-11-30 01:00:56 +00:00
2004-04-07 19:15:01 +00:00
public unsafe Pixbuf(int data_length, void *data, bool copy_pixels) : base (IntPtr.Zero)
2003-02-19 06:00:27 +00:00
{
IntPtr error = IntPtr.Zero;
Raw = gdk_pixbuf_new_from_inline(data_length, (IntPtr) data, copy_pixels, out error);
if (error != IntPtr.Zero) throw new GLib.GException (error);
}
//
// ICloneable interface
//
public object Clone ()
{
return Copy ();
}
//
// These are factory versions of a couple of existing methods, but simplify
// the process by reducing the number of steps required. Here a single
// operation will create the Pixbuf instead of two
//
2005-03-28 21:57:19 +00:00
public static Gdk.Pixbuf FromDrawable (Gdk.Drawable src, Gdk.Colormap cmap, int src_x, int src_y, int dest_x, int dest_y, int width, int height) {
IntPtr raw_ret = gdk_pixbuf_get_from_drawable(IntPtr.Zero, src.Handle, cmap.Handle, src_x, src_y, dest_x, dest_y, width, height);
return new Pixbuf (raw_ret);
}
[Obsolete ("Replaced by static FromDrawable method")]
2003-02-19 06:00:27 +00:00
public Gdk.Pixbuf CreateFromDrawable (Gdk.Drawable src, Gdk.Colormap cmap, int src_x, int src_y, int dest_x, int dest_y, int width, int height) {
IntPtr raw_ret = gdk_pixbuf_get_from_drawable((IntPtr) 0, src.Handle, cmap.Handle, src_x, src_y, dest_x, dest_y, width, height);
return new Pixbuf (raw_ret);
}
2003-03-14 03:04:15 +00:00
//
// the 'Pixels' property
//
[DllImport("libgdk_pixbuf-2.0-0.dll")]
2004-05-18 19:03:30 +00:00
static extern IntPtr gdk_pixbuf_get_pixels(IntPtr raw);
2003-03-14 03:04:15 +00:00
2004-06-04 04:13:49 +00:00
public IntPtr Pixels {
2003-03-14 03:04:15 +00:00
get {
2004-05-18 19:03:30 +00:00
IntPtr ret = gdk_pixbuf_get_pixels (Handle);
2003-03-14 03:04:15 +00:00
return ret;
}
}
2004-02-12 17:31:37 +00:00
[DllImport("libgdk_pixbuf-2.0-0.dll")]
static extern IntPtr gdk_pixbuf_get_formats();
public static PixbufFormat[] Formats {
get {
IntPtr list_ptr = gdk_pixbuf_get_formats ();
if (list_ptr == IntPtr.Zero)
return new PixbufFormat [0];
GLib.SList list = new GLib.SList (list_ptr, typeof (PixbufFormat));
PixbufFormat[] result = new PixbufFormat [list.Count];
for (int i = 0; i < list.Count; i++)
result [i] = (PixbufFormat) list [i];
return result;
}
}
2004-06-14 20:25:50 +00:00
[DllImport("libgdk_pixbuf-2.0-0.dll")]
2005-03-09 20:32:24 +00:00
static extern unsafe bool gdk_pixbuf_save(IntPtr raw, IntPtr filename, IntPtr type, out IntPtr error, IntPtr dummy);
2004-06-14 20:25:50 +00:00
public unsafe bool Save(string filename, string type) {
IntPtr error = IntPtr.Zero;
2005-03-09 20:32:24 +00:00
IntPtr nfilename = GLib.Marshaller.StringToPtrGStrdup (filename);
IntPtr ntype = GLib.Marshaller.StringToPtrGStrdup (type);
bool ret = gdk_pixbuf_save(Handle, nfilename, ntype, out error, IntPtr.Zero);
GLib.Marshaller.Free (nfilename);
GLib.Marshaller.Free (ntype);
2004-06-14 20:25:50 +00:00
if (error != IntPtr.Zero) throw new GLib.GException (error);
return ret;
}
2005-01-25 17:20:55 +00:00
[DllImport("libglib-2.0-0.dll")]
static extern void g_free (IntPtr raw);
[DllImport("libgdk_pixbuf-2.0-0.dll")]
2006-02-28 08:45:31 +00:00
static extern unsafe bool gdk_pixbuf_save_to_bufferv (IntPtr raw, out IntPtr buffer, out IntPtr buffer_size, IntPtr type, IntPtr[] option_keys, IntPtr[] option_values, out IntPtr error);
2005-01-25 17:20:55 +00:00
2005-03-09 20:32:24 +00:00
IntPtr[] NullTerm (string[] src)
2005-01-25 17:20:55 +00:00
{
2005-03-09 20:32:24 +00:00
if (src.Length == 0)
return null;
IntPtr[] result = new IntPtr [src.Length + 1];
2005-01-25 17:20:55 +00:00
for (int i = 0; i < src.Length; i++)
2005-03-09 20:32:24 +00:00
result [i] = GLib.Marshaller.StringToPtrGStrdup (src [i]);
result [src.Length] = IntPtr.Zero;
2005-01-25 17:20:55 +00:00
return result;
}
2005-03-09 20:32:24 +00:00
void ReleaseArray (IntPtr[] ptrs)
{
if (ptrs == null)
return;
foreach (IntPtr p in ptrs)
GLib.Marshaller.Free (p);
}
2005-01-25 17:20:55 +00:00
public unsafe byte[] SaveToBuffer (string type)
{
return SaveToBuffer (type, new string [0], new string [0]);
}
public unsafe byte[] SaveToBuffer (string type, string[] option_keys, string[] option_values)
{
IntPtr error = IntPtr.Zero;
IntPtr buffer;
2006-02-28 08:45:31 +00:00
IntPtr buffer_size;
2005-03-09 20:32:24 +00:00
IntPtr ntype = GLib.Marshaller.StringToPtrGStrdup (type);
IntPtr[] nkeys = NullTerm (option_keys);
IntPtr[] nvals = NullTerm (option_values);
bool saved = gdk_pixbuf_save_to_bufferv (Handle, out buffer, out buffer_size, ntype, nkeys, nvals, out error);
GLib.Marshaller.Free (ntype);
ReleaseArray (nkeys);
ReleaseArray (nvals);
if (!saved)
2005-01-25 17:20:55 +00:00
throw new GLib.GException (error);
2006-02-28 08:45:31 +00:00
byte[] result = new byte [(int)buffer_size];
2005-01-25 17:20:55 +00:00
Marshal.Copy (buffer, result, 0, (int) buffer_size);
g_free (buffer);
return result;
}
[DllImport("libgdk_pixbuf-2.0-0.dll")]
2005-03-09 20:32:24 +00:00
static extern unsafe bool gdk_pixbuf_save_to_callbackv (IntPtr raw, GdkSharp.PixbufSaveFuncNative save_func, IntPtr user_data, IntPtr type, IntPtr[] option_keys, IntPtr[] option_values, out IntPtr error);
2005-01-25 17:20:55 +00:00
public unsafe void SaveToCallback (PixbufSaveFunc save_func, string type)
{
SaveToCallback (save_func, type, new string [0], new string [0]);
}
public unsafe void SaveToCallback (PixbufSaveFunc save_func, string type, string[] option_keys, string[] option_values)
{
2005-04-04 16:27:08 +00:00
GdkSharp.PixbufSaveFuncWrapper save_func_wrapper = new GdkSharp.PixbufSaveFuncWrapper (save_func);
2005-01-25 17:20:55 +00:00
IntPtr error = IntPtr.Zero;
2005-03-09 20:32:24 +00:00
IntPtr ntype = GLib.Marshaller.StringToPtrGStrdup (type);
IntPtr[] nkeys = NullTerm (option_keys);
IntPtr[] nvals = NullTerm (option_values);
bool saved = gdk_pixbuf_save_to_callbackv (Handle, save_func_wrapper.NativeDelegate, IntPtr.Zero, ntype, nkeys, nvals, out error);
GLib.Marshaller.Free (ntype);
ReleaseArray (nkeys);
ReleaseArray (nvals);
if (!saved)
2005-01-25 17:20:55 +00:00
throw new GLib.GException (error);
}