1 // Pixbuf.cs - Gdk Pixbuf class customizations
2 //
3 // Authors:
4 // 	Vladimir Vukicevic <vladimir@pobox.com>
5 // 	Miguel de Icaza <miguel@ximian.com>
6 // 	Mike Kestner <mkestner@ximian.com>
7 // 	Duncan Mak <duncan@ximian.com>
8 //	Gonzalo Paniagua Javier <gonzalo@ximian.com>
9 //	Martin Willemoes Hansen <mwh@sysrq.dk>
10 //	Jose Faria   <spigaz@gmail.com>
11 //
12 // Copyright (c) 2002 Vladimir Vukicevic
13 // Copyright (c) 2003 Ximian, Inc. (Miguel de Icaza)
14 // Copyright (c) 2003 Ximian, Inc. (Duncan Mak)
15 // Copyright (c) 2003 Ximian, Inc. (Gonzalo Paniagua Javier)
16 // Copyright (c) 2003 Martin Willemoes Hansen
17 // Copyright (c) 2004-2005 Novell, Inc.
18 //
19 // This code is inserted after the automatically generated code.
20 //
21 // This program is free software; you can redistribute it and/or
22 // modify it under the terms of version 2 of the Lesser GNU General
23 // Public License as published by the Free Software Foundation.
24 //
25 // This program is distributed in the hope that it will be useful,
26 // but WITHOUT ANY WARRANTY; without even the implied warranty of
27 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28 // Lesser General Public License for more details.
29 //
30 // You should have received a copy of the GNU Lesser General Public
31 // License along with this program; if not, write to the
32 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
33 // Boston, MA 02111-1307, USA.
34 
35 namespace Gdk {
36 
37 	using System;
38 	using System.Runtime.InteropServices;
39 
40 	public partial class Pixbuf {
41 
Pixbuf(System.IO.Stream stream)42 		public Pixbuf (System.IO.Stream stream) : base (IntPtr.Zero)
43 		{
44 			using (PixbufLoader pl = new PixbufLoader (stream)) {
45 				Raw = pl.PixbufHandle;
46 			}
47 		}
48 
Pixbuf(System.IO.Stream stream, int width, int height)49 		public Pixbuf (System.IO.Stream stream, int width, int height) : base (IntPtr.Zero)
50 		{
51 			using (PixbufLoader pl = new PixbufLoader (stream, width, height)) {
52 				Raw = pl.PixbufHandle;
53 			}
54 		}
55 
Pixbuf(System.Reflection.Assembly assembly, string resource)56 		public Pixbuf (System.Reflection.Assembly assembly, string resource) : base (IntPtr.Zero)
57 		{
58 			using (PixbufLoader pl = new PixbufLoader (assembly == null ? System.Reflection.Assembly.GetCallingAssembly () : assembly, resource)) {
59 				Raw = pl.PixbufHandle;
60 			}
61 		}
62 
Pixbuf(System.Reflection.Assembly assembly, string resource, int width, int height)63 		public Pixbuf (System.Reflection.Assembly assembly, string resource, int width, int height) : base (IntPtr.Zero)
64 		{
65 			using (PixbufLoader pl = new PixbufLoader (assembly == null ? System.Reflection.Assembly.GetCallingAssembly () : assembly, resource, width, height)) {
66 				Raw = pl.PixbufHandle;
67 			}
68 		}
69 
Pixbuf(byte[] buffer)70 		public Pixbuf (byte[] buffer) : base (IntPtr.Zero)
71 		{
72 			using (PixbufLoader pl = new PixbufLoader (buffer)) {
73 				Raw = pl.PixbufHandle;
74 			}
75 		}
76 
Pixbuf(byte[] buffer, int width, int height)77 		public Pixbuf (byte[] buffer, int width, int height) : base (IntPtr.Zero)
78 		{
79 			using (PixbufLoader pl = new PixbufLoader (buffer, width, height)) {
80 				Raw = pl.PixbufHandle;
81 			}
82 		}
83 
LoadFromResource(string resource)84 		static public Pixbuf LoadFromResource (string resource)
85 		{
86 			return new Pixbuf (System.Reflection.Assembly.GetCallingAssembly (), resource);
87 		}
88 
89 		[DllImport ("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
gdk_pixbuf_scale_simple(IntPtr raw, int dest_width, int dest_height, int interp_type)90 		static extern IntPtr gdk_pixbuf_scale_simple(IntPtr raw, int dest_width, int dest_height, int interp_type);
91 
ScaleSimple(int dest_width, int dest_height, Gdk.InterpType interp_type)92 		public Gdk.Pixbuf ScaleSimple(int dest_width, int dest_height, Gdk.InterpType interp_type) {
93 			IntPtr raw_ret = gdk_pixbuf_scale_simple(Handle, dest_width, dest_height, (int) interp_type);
94 			Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret, true);
95 			return ret;
96 		}
97 
98 		[DllImport ("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
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)99 		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);
100 
CompositeColorSimple(int dest_width, int dest_height, Gdk.InterpType interp_type, int overall_alpha, int check_size, uint color1, uint color2)101 		public Gdk.Pixbuf CompositeColorSimple(int dest_width, int dest_height, Gdk.InterpType interp_type, int overall_alpha, int check_size, uint color1, uint color2) {
102 			IntPtr raw_ret = gdk_pixbuf_composite_color_simple(Handle, dest_width, dest_height, (int) interp_type, overall_alpha, check_size, color1, color2);
103 			Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret, true);
104 			return ret;
105 		}
106 
107 		[DllImport ("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
gdk_pixbuf_add_alpha(IntPtr raw, bool substitute_color, byte r, byte g, byte b)108 		static extern IntPtr gdk_pixbuf_add_alpha(IntPtr raw, bool substitute_color, byte r, byte g, byte b);
109 
AddAlpha(bool substitute_color, byte r, byte g, byte b)110 		public Gdk.Pixbuf AddAlpha(bool substitute_color, byte r, byte g, byte b) {
111 			IntPtr raw_ret = gdk_pixbuf_add_alpha(Handle, substitute_color, r, g, b);
112 			Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(raw_ret, true);
113 			return ret;
114 		}
115 
116 		class DestroyHelper {
117 
118 			GCHandle gch;
119 			GCHandle data_handle;
120 			PixbufDestroyNotify notify;
121 
DestroyHelper(byte[] data, PixbufDestroyNotify notify)122 			public DestroyHelper (byte[] data, PixbufDestroyNotify notify)
123 			{
124 				gch = GCHandle.Alloc (this);
125 				data_handle = GCHandle.Alloc (data, GCHandleType.Pinned);
126 				this.notify = notify;
127 			}
128 
129 			[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
NativeDelegate(IntPtr buf, IntPtr data)130 			public delegate void NativeDelegate (IntPtr buf, IntPtr data);
131 
ReleaseHandles(IntPtr buf, IntPtr data)132 			void ReleaseHandles (IntPtr buf, IntPtr data)
133 			{
134 				if (notify != null)
135 					notify ((byte[])data_handle.Target);
136 				data_handle.Free ();
137 				gch.Free ();
138 			}
139 
140 			NativeDelegate handler;
141 			public NativeDelegate Handler {
142 				get {
143 					handler = new NativeDelegate (ReleaseHandles);
144 					return handler;
145 				}
146 			}
147 		}
148 
149 		[DllImport ("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
gdk_pixbuf_new_from_data(byte[] data, int colorspace, bool has_alpha, int bits_per_sample, int width, int height, int rowstride, DestroyHelper.NativeDelegate destroy_fn, IntPtr destroy_fn_data)150 		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, DestroyHelper.NativeDelegate destroy_fn, IntPtr destroy_fn_data);
151 
Pixbuf(byte[] data, Gdk.Colorspace colorspace, bool has_alpha, int bits_per_sample, int width, int height, int rowstride, Gdk.PixbufDestroyNotify destroy_fn)152 		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)
153 		{
154 			if (GetType () != typeof (Pixbuf)) {
155 				throw new InvalidOperationException ("Can't override this constructor.");
156 			}
157 			DestroyHelper helper = new DestroyHelper (data, destroy_fn);
158 			Raw = gdk_pixbuf_new_from_data(data, (int) colorspace, has_alpha, bits_per_sample, width, height, rowstride, helper.Handler, IntPtr.Zero);
159 		}
160 
161 		// overload to default the colorspace
Pixbuf(byte [] data, bool has_alpha, int bits_per_sample, int width, int height, int rowstride, Gdk.PixbufDestroyNotify destroy_fn)162 		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) {}
163 
Pixbuf(byte [] data, bool has_alpha, int bits_per_sample, int width, int height, int rowstride)164 		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) {}
165 
Pixbuf(byte [] data, Gdk.Colorspace colorspace, bool has_alpha, int bits_per_sample, int width, int height, int rowstride)166 		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) {}
167 
Pixbuf(byte[] data, bool copy_pixels)168 		public unsafe Pixbuf(byte[] data, bool copy_pixels) : base (IntPtr.Zero)
169 		{
170 			IntPtr error = IntPtr.Zero;
171 			Raw = gdk_pixbuf_new_from_inline(data.Length, data, copy_pixels, out error);
172 			if (error != IntPtr.Zero) throw new GLib.GException (error);
173 		}
174 
175 		[DllImport ("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
gdk_pixbuf_new_from_inline(int len, IntPtr data, bool copy_pixels, out IntPtr error)176 		static extern unsafe IntPtr gdk_pixbuf_new_from_inline(int len, IntPtr data, bool copy_pixels, out IntPtr error);
177 
Pixbuf(int data_length, void *data, bool copy_pixels)178 		public unsafe Pixbuf(int data_length, void *data, bool copy_pixels) : base (IntPtr.Zero)
179 		{
180 			IntPtr error = IntPtr.Zero;
181 			Raw = gdk_pixbuf_new_from_inline(data_length, (IntPtr) data, copy_pixels, out error);
182 			if (error != IntPtr.Zero) throw new GLib.GException (error);
183 		}
184 
185 //
186 // ICloneable interface
187 //
188 
Clone()189 		public object Clone ()
190 		{
191 			return Copy ();
192 		}
193 
194 
195 //
196 // the 'Pixels' property
197 //
198 		[DllImport ("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
gdk_pixbuf_get_pixels(IntPtr raw)199 		static extern IntPtr gdk_pixbuf_get_pixels(IntPtr raw);
200 
201                 public IntPtr Pixels {
202                 	get {
203                 		IntPtr ret = gdk_pixbuf_get_pixels (Handle);
204                 		return ret;
205                 	}
206                 }
207 
208 		[DllImport ("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
gdk_pixbuf_get_formats()209 		static extern IntPtr gdk_pixbuf_get_formats();
210 
211 		public static PixbufFormat[] Formats {
212 			get {
213 				IntPtr list_ptr = gdk_pixbuf_get_formats ();
214 				if (list_ptr == IntPtr.Zero)
215 					return new PixbufFormat [0];
216 				GLib.SList list = new GLib.SList (list_ptr, typeof (PixbufFormat));
217 				PixbufFormat[] result = new PixbufFormat [list.Count];
218 				for (int i = 0; i < list.Count; i++)
219 					result [i] = (PixbufFormat) list [i];
220 				return result;
221 			}
222 		}
223 
224 		[DllImport ("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
gdk_pixbuf_save(IntPtr raw, IntPtr filename, IntPtr type, out IntPtr error, IntPtr dummy)225 		static extern unsafe bool gdk_pixbuf_save(IntPtr raw, IntPtr filename, IntPtr type, out IntPtr error, IntPtr dummy);
226 
Save(string filename, string type)227 		public unsafe bool Save(string filename, string type) {
228 			IntPtr error = IntPtr.Zero;
229 			IntPtr nfilename = GLib.Marshaller.StringToPtrGStrdup (filename);
230 			IntPtr ntype = GLib.Marshaller.StringToPtrGStrdup (type);
231 			bool ret = gdk_pixbuf_save(Handle, nfilename, ntype, out error, IntPtr.Zero);
232 			GLib.Marshaller.Free (nfilename);
233 			GLib.Marshaller.Free (ntype);
234 			if (error != IntPtr.Zero) throw new GLib.GException (error);
235 			return ret;
236 		}
237 
238 		[DllImport ("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
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)239 		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);
240 
NullTerm(string[] src)241 		IntPtr[] NullTerm (string[] src)
242 		{
243 			if (src.Length == 0)
244 				return null;
245 			IntPtr[] result = new IntPtr [src.Length + 1];
246 			for (int i = 0; i < src.Length; i++)
247 				result [i] = GLib.Marshaller.StringToPtrGStrdup (src [i]);
248 			result [src.Length] = IntPtr.Zero;
249 			return result;
250 		}
251 
ReleaseArray(IntPtr[] ptrs)252 		void ReleaseArray (IntPtr[] ptrs)
253 		{
254 			if (ptrs == null)
255 				return;
256 			foreach (IntPtr p in ptrs)
257 				GLib.Marshaller.Free (p);
258 		}
259 
SaveToBuffer(string type)260 		public unsafe byte[] SaveToBuffer (string type)
261 		{
262 			return SaveToBuffer (type, new string [0], new string [0]);
263 		}
264 
SaveToBuffer(string type, string[] option_keys, string[] option_values)265 		public unsafe byte[] SaveToBuffer (string type, string[] option_keys, string[] option_values)
266 		{
267 			IntPtr error = IntPtr.Zero;
268 			IntPtr buffer;
269 			IntPtr buffer_size;
270 			IntPtr ntype = GLib.Marshaller.StringToPtrGStrdup (type);
271 			IntPtr[] nkeys = NullTerm (option_keys);
272 			IntPtr[] nvals = NullTerm (option_values);
273 			bool saved = gdk_pixbuf_save_to_bufferv (Handle, out buffer, out buffer_size, ntype, nkeys, nvals, out error);
274 			GLib.Marshaller.Free (ntype);
275 			ReleaseArray (nkeys);
276 			ReleaseArray (nvals);
277 
278 			if (!saved)
279 				throw new GLib.GException (error);
280 			byte[] result = new byte [(int)buffer_size];
281 			Marshal.Copy (buffer, result, 0, (int) buffer_size);
282 			GLib.Marshaller.Free (buffer);
283 			return result;
284 		}
285 
286 		[DllImport ("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
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)287 		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);
288 
SaveToCallback(PixbufSaveFunc save_func, string type)289 		public unsafe void SaveToCallback (PixbufSaveFunc save_func, string type)
290 		{
291 			SaveToCallback (save_func, type, new string [0], new string [0]);
292 		}
293 
SaveToCallback(PixbufSaveFunc save_func, string type, string[] option_keys, string[] option_values)294 		public unsafe void SaveToCallback (PixbufSaveFunc save_func, string type, string[] option_keys, string[] option_values)
295 		{
296 			GdkSharp.PixbufSaveFuncWrapper save_func_wrapper = new GdkSharp.PixbufSaveFuncWrapper (save_func);
297 			IntPtr error = IntPtr.Zero;
298 			IntPtr ntype = GLib.Marshaller.StringToPtrGStrdup (type);
299 			IntPtr[] nkeys = NullTerm (option_keys);
300 			IntPtr[] nvals = NullTerm (option_values);
301 			bool saved = gdk_pixbuf_save_to_callbackv (Handle, save_func_wrapper.NativeDelegate, IntPtr.Zero, ntype, nkeys, nvals, out error);
302 			GLib.Marshaller.Free (ntype);
303 			ReleaseArray (nkeys);
304 			ReleaseArray (nvals);
305 
306 			if (!saved)
307 				throw new GLib.GException (error);
308 		}
309 
310 		[DllImport("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
gdk_pixbuf_savev(IntPtr raw, IntPtr filename, IntPtr type, IntPtr[] option_keys, IntPtr[] option_values, out IntPtr error)311 		static extern unsafe bool gdk_pixbuf_savev(IntPtr raw, IntPtr filename, IntPtr type, IntPtr[] option_keys, IntPtr[] option_values, out IntPtr error);
312 
Savev(string filename, string type, string[] option_keys, string[] option_values)313 		public unsafe bool Savev(string filename, string type, string[] option_keys, string[] option_values) {
314 			IntPtr error = IntPtr.Zero;
315 			IntPtr native_filename = GLib.Marshaller.StringToPtrGStrdup (filename);
316 			IntPtr native_type = GLib.Marshaller.StringToPtrGStrdup (type);
317 			IntPtr[] native_option_keys = NullTerm (option_keys);
318 			IntPtr[] native_option_values = NullTerm (option_values);
319 			bool saved = gdk_pixbuf_savev(Handle, native_filename, native_type, native_option_keys, native_option_values, out error);
320 			GLib.Marshaller.Free (native_filename);
321 			GLib.Marshaller.Free (native_type);
322 			ReleaseArray (native_option_keys);
323 			ReleaseArray (native_option_values);
324 
325 			if (!saved)
326 				throw new GLib.GException (error);
327 			return saved;
328 		}
329 	}
330 }
331 
332