* Image.cs: Save should use encoder.FormatID, not Clsid
[mono.git] / mcs / class / System.Drawing / System.Drawing / Image.cs
1 //
2 // System.Drawing.Image.cs
3 //
4 // (C) 2002 Ximian, Inc.  http://www.ximian.com
5 // Author:      Christian Meyer (Christian.Meyer@cs.tum.edu)
6 //              Alexandre Pigolkine (pigolkine@gmx.de)
7 //              Jordi Mas i Hernandez (jordi@ximian.com)
8 //
9 namespace System.Drawing {
10
11 using System;
12 using System.Runtime.Remoting;
13 using System.Runtime.Serialization;
14 using System.Runtime.InteropServices;
15 using System.ComponentModel;
16 using System.Drawing.Imaging;
17 using System.IO;
18
19 [Serializable]
20 [ComVisible (true)]
21 [Editor ("System.Drawing.Design.ImageEditor, " + Consts.AssemblySystem_Drawing_Design, typeof (System.Drawing.Design.UITypeEditor))]
22 [TypeConverter (typeof(ImageConverter))]
23 [ImmutableObject (true)]
24 public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISerializable 
25 {
26         public delegate bool GetThumbnailImageAbort();
27         
28         internal IntPtr nativeObject = IntPtr.Zero;     
29         protected ColorPalette colorPalette;
30         protected ImageFormat raw_format;
31         
32         // constructor
33         internal  Image()
34         {               
35                 colorPalette = new ColorPalette();
36         }
37         
38         [MonoTODO]      
39         private Image (SerializationInfo info, StreamingContext context)
40         {
41                 
42         }
43         
44         [MonoTODO]      
45         void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
46         {
47         }
48     
49         // public methods
50         // static
51         public static Image FromFile(string filename)
52         {
53                 return new Bitmap (filename);
54         }
55         
56         public static Image FromFile(string filename, bool useEmbeddedColorManagement)
57         {
58                 return new Bitmap (filename, useEmbeddedColorManagement);
59         }
60
61         [MonoTODO]      
62         public static Bitmap FromHbitmap(IntPtr hbitmap)
63         {               
64                 throw new NotImplementedException ();
65         }
66
67         [MonoTODO]      
68         public static Bitmap FromHbitmap(IntPtr hbitmap, IntPtr hpalette)
69         {               
70                 throw new NotImplementedException ();
71         }
72
73         public static Image FromStream (Stream stream)
74         {
75                 return new Bitmap (stream);
76         }
77         
78         public static Image FromStream (Stream stream, bool useECM)
79         {
80                 return new Bitmap (stream, useECM);
81         }
82
83         public static int GetPixelFormatSize(PixelFormat pixfmt)
84         {
85                 int result = 0;
86                 switch (pixfmt) {
87                         case PixelFormat.Format16bppArgb1555:
88                         case PixelFormat.Format16bppGrayScale:
89                         case PixelFormat.Format16bppRgb555:
90                         case PixelFormat.Format16bppRgb565:
91                                 result = 16;
92                                 break;
93                         case PixelFormat.Format1bppIndexed:
94                                 result = 1;
95                                 break;
96                         case PixelFormat.Format24bppRgb:
97                                 result = 24;
98                                 break;
99                         case PixelFormat.Format32bppArgb:
100                         case PixelFormat.Format32bppPArgb:
101                         case PixelFormat.Format32bppRgb:
102                                 result = 32;
103                                 break;
104                         case PixelFormat.Format48bppRgb:
105                                 result = 48;
106                                 break;
107                         case PixelFormat.Format4bppIndexed:
108                                 result = 4;
109                                 break;
110                         case PixelFormat.Format64bppArgb:
111                         case PixelFormat.Format64bppPArgb:
112                                 result = 64;
113                                 break;
114                         case PixelFormat.Format8bppIndexed:
115                                 result = 8;
116                                 break;
117                 }
118                 return result;
119         }
120
121         public static bool IsAlphaPixelFormat(PixelFormat pixfmt)
122         {
123                 bool result = false;
124                 switch (pixfmt) {
125                         case PixelFormat.Format16bppArgb1555:
126                         case PixelFormat.Format32bppArgb:
127                         case PixelFormat.Format32bppPArgb:
128                         case PixelFormat.Format64bppArgb:
129                         case PixelFormat.Format64bppPArgb:
130                                 result = true;
131                                 break;
132                         case PixelFormat.Format16bppGrayScale:
133                         case PixelFormat.Format16bppRgb555:
134                         case PixelFormat.Format16bppRgb565:
135                         case PixelFormat.Format1bppIndexed:
136                         case PixelFormat.Format24bppRgb:
137                         case PixelFormat.Format32bppRgb:
138                         case PixelFormat.Format48bppRgb:
139                         case PixelFormat.Format4bppIndexed:
140                         case PixelFormat.Format8bppIndexed:
141                                 result = false;
142                                 break;
143                 }
144                 return result;
145         }
146         
147         public static bool IsCanonicalPixelFormat (PixelFormat pixfmt)
148         {
149                 return ((pixfmt & PixelFormat.Canonical) != 0);
150         }
151         
152         public static bool IsExtendedPixelFormat (PixelFormat pixfmt)
153         {
154                 return ((pixfmt & PixelFormat.Extended) != 0);
155         }
156
157         // non-static   
158         public RectangleF GetBounds (ref GraphicsUnit pageUnit)
159         {       
160                 RectangleF source;                      
161                 
162                 Status status = GDIPlus.GdipGetImageBounds (nativeObject, out source, ref pageUnit);
163                 GDIPlus.CheckStatus (status);           
164                 
165                 return source;
166         }
167         
168         [MonoTODO]      
169         public EncoderParameters GetEncoderParameterList(Guid encoder)
170         {
171                 throw new NotImplementedException ();
172         }
173         
174         public int GetFrameCount(FrameDimension dimension)
175         {
176                 int count;
177                 Guid guid = dimension.Guid;
178                 
179                 Status status = GDIPlus.GdipImageGetFrameCount (nativeObject, ref guid, out  count);
180                 GDIPlus.CheckStatus (status);           
181                 
182                 return count;
183                 
184         }
185         
186         [MonoTODO]      
187         public PropertyItem GetPropertyItem(int propid)
188         {
189                 throw new NotImplementedException ();
190         }
191         
192         [MonoTODO]      
193         public Image GetThumbnailImage(int thumbWidth, int thumbHeight, Image.GetThumbnailImageAbort callback, IntPtr callbackData)
194         {
195                 throw new NotImplementedException ();                           
196         }
197         
198         [MonoTODO]      
199         public void RemovePropertyItem (int propid)
200         {               
201                 throw new NotImplementedException ();
202         }
203         
204         [MonoTODO]      
205         public void RotateFlip (RotateFlipType rotateFlipType)
206         {               
207                 throw new NotImplementedException ();
208         }
209
210         public void Save (string filename)
211         {
212                 Save (filename, RawFormat);
213         }
214
215         public void Save (Stream stream, ImageFormat format)
216         {
217                 if (Environment.OSVersion.Platform == (PlatformID) 128) {
218                         byte[] g = format.Guid.ToByteArray();
219                         GDIPlus.GdiPlusStreamHelper sh = new GDIPlus.GdiPlusStreamHelper (stream);
220                         Status st = GDIPlus.GdipSaveImageToDelegate_linux (nativeObject, sh.PutBytesDelegate, g, IntPtr.Zero);
221                 } else {
222                         throw new NotImplementedException ("Image.Save(Stream) (win32)");
223                 }
224         }
225
226         public void Save(string filename, ImageFormat format) 
227         {
228                 byte[] g = format.Guid.ToByteArray();
229                 Status st = GDIPlus.GdipSaveImageToFile (nativeObject, filename, g, IntPtr.Zero);
230                 GDIPlus.CheckStatus (st);
231         }
232         
233         internal void setGDIPalette() 
234         {
235                 IntPtr gdipalette;
236
237                 gdipalette = colorPalette.getGDIPalette ();
238                 Status st = GDIPlus.GdipSetImagePalette (NativeObject, gdipalette);
239                 Marshal.FreeHGlobal (gdipalette);
240
241                 GDIPlus.CheckStatus (st);
242         }
243
244         [MonoTODO ("Ignoring EncoderParameters")]
245         public void Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
246         {
247                 Save (stream, new ImageFormat (encoder.FormatID));
248         }
249         
250         [MonoTODO ("Ignoring EncoderParameters")]       
251         public void Save(string filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
252         {
253                 Save (filename, new ImageFormat (encoder.FormatID));
254         }
255         
256         [MonoTODO]      
257         public void SaveAdd(EncoderParameters encoderParams)
258         {
259                 throw new NotImplementedException ();
260         }
261         
262         [MonoTODO]      
263         public void SaveAdd(Image image, EncoderParameters encoderParams)
264         {
265                 throw new NotImplementedException ();
266         }
267         
268         
269         public int SelectActiveFrame(FrameDimension dimension, int frameIndex)
270         {
271                 Guid guid = dimension.Guid;             
272                                 
273                 Status status = GDIPlus.GdipImageSelectActiveFrame (nativeObject, ref guid, frameIndex);                        
274                 GDIPlus.CheckStatus (status);                   
275                 
276                 return frameIndex;              
277         }
278         
279         [MonoTODO]      
280         public void SetPropertyItem(PropertyItem propitem)
281         {
282                 throw new NotImplementedException ();
283         }
284
285         // properties   
286         public int Flags {
287                 get {
288                         int flags;
289                         
290                         Status status = GDIPlus.GdipGetImageFlags (nativeObject, out flags);                    
291                         GDIPlus.CheckStatus (status);                                           
292                         return flags;                   
293                 }
294         }
295         
296         [MonoTODO]      
297         public Guid[] FrameDimensionsList {
298                 get {
299                         throw new NotImplementedException ();
300                 }
301         }
302         
303         public int Height {
304                 get {
305                         int height;                     
306                         Status status = GDIPlus.GdipGetImageHeight (nativeObject, out height);          
307                         GDIPlus.CheckStatus (status);                   
308                         
309                         return height;
310                 }
311         }
312         
313         public float HorizontalResolution {
314                 get {
315                         float resolution;
316                         
317                         Status status = GDIPlus.GdipGetImageHorizontalResolution (nativeObject, out resolution);                        
318                         GDIPlus.CheckStatus (status);                   
319                         
320                         return resolution;
321                 }
322         }
323         
324         public ColorPalette Palette {
325                 get {                                                   
326                         
327                         return colorPalette;
328                 }
329                 set {
330                         colorPalette = value;                                   
331                 }
332         }
333         
334         
335         public SizeF PhysicalDimension {
336                 get {
337                         float width,  height;
338                         
339                         Status status = GDIPlus.GdipGetImageDimension (nativeObject, out width, out height);            
340                         GDIPlus.CheckStatus (status);                   
341                         
342                         return new SizeF (width, height);
343                 }
344         }
345         
346         public PixelFormat PixelFormat {
347                 get {
348                         
349                         PixelFormat value;                              
350                         Status status = GDIPlus.GdipGetImagePixelFormat (nativeObject, out value);              
351                         GDIPlus.CheckStatus (status);                   
352                         
353                         return value;
354                 }               
355         }
356         
357         
358         [MonoTODO]      
359         public int[] PropertyIdList {
360                 get {
361                         throw new NotImplementedException ();
362                 }
363         }
364         
365         [MonoTODO]      
366         public PropertyItem[] PropertyItems {
367                 get {
368                         throw new NotImplementedException ();
369                 }
370         }
371
372         public ImageFormat RawFormat {
373                 get {
374                         return raw_format;
375                 }
376         }
377
378         internal void SetRawFormat (ImageFormat format)
379         {
380                 raw_format = format;
381         }
382
383         public Size Size {
384                 get {
385                         return new Size(Width, Height);
386                 }
387         }
388         
389         public float VerticalResolution {
390                 get {
391                         float resolution;
392                         
393                         GDIPlus.GdipGetImageVerticalResolution (nativeObject, out resolution);                  
394                         return resolution;
395                 }
396         }
397         
398         public int Width {
399                 get {
400                         int width;                      
401                         Status status = GDIPlus.GdipGetImageWidth (nativeObject, out width);            
402                         GDIPlus.CheckStatus (status);                   
403                         
404                         return width;
405                 }
406         }
407         
408         internal IntPtr NativeObject{
409                 get{
410                         return nativeObject;
411                 }
412                 set     {
413                         nativeObject = value;
414                 }
415         }
416         
417         public void Dispose ()
418         {
419                 Dispose (true);
420         }
421
422         ~Image ()
423         {
424                 Dispose (false);
425         }
426
427         protected virtual void DisposeResources ()
428         {
429                 GDIPlus.GdipDisposeImage (nativeObject);
430         }
431         
432         protected virtual void Dispose (bool disposing)
433         {
434                 if (nativeObject != (IntPtr) 0){
435                         DisposeResources ();
436                         nativeObject=IntPtr.Zero;
437                 }
438         }
439         
440         
441         public virtual object Clone()
442         {                               
443                 IntPtr newimage = IntPtr.Zero;
444                 
445                 if (!(this is Bitmap)) 
446                         throw new NotImplementedException (); 
447                 
448                 Status status = GDIPlus.GdipCloneImage (NativeObject, out newimage);                    
449                 
450                 GDIPlus.CheckStatus (status);                   
451                 
452                 if (this is Bitmap)
453                         return new Bitmap (newimage);
454                 
455                 throw new NotImplementedException (); 
456         }
457
458 }
459
460 }