revert Sanjay's patch. It broke the TestBimap test case and also saving on existing...
[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
6 // eMail: Christian.Meyer@cs.tum.edu
7 //
8 // Alexandre Pigolkine (pigolkine@gmx.de)
9 // 
10 //
11 namespace System.Drawing {
12
13 using System;
14 using System.Runtime.Remoting;
15 using System.Runtime.Serialization;
16 using System.Runtime.InteropServices;
17 using System.ComponentModel;
18 using System.Drawing.Imaging;
19 using System.IO;
20
21 [Serializable]
22 [ComVisible (true)]
23 [Editor ("System.Drawing.Design.ImageEditor, " + Consts.AssemblySystem_Drawing_Design, typeof (System.Drawing.Design.UITypeEditor))]
24 [TypeConverter (typeof(ImageConverter))]
25 [ImmutableObject (true)]
26 public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISerializable 
27 {
28         public delegate bool GetThumbnailImageAbort ();
29         
30         internal IntPtr nativeObject = IntPtr.Zero;
31         protected Size image_size;
32         protected PixelFormat pixel_format;
33         protected ColorPalette colorPalette;
34
35         protected ImageFormat raw_format;
36         
37         // constructor
38         public Image ()
39         {
40                 pixel_format = PixelFormat.Format32bppArgb;
41                 colorPalette = new ColorPalette();
42         }
43
44         private Image (SerializationInfo info, StreamingContext context)
45         {
46         }
47         
48         
49
50         void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
51         {
52         }
53     
54         // public methods
55         // static
56         public static Image FromFile (string filename)
57         {
58                 return new Bitmap (filename);
59         }
60         
61         public static Image FromFile (string filename, bool useEmbeddedColorManagement)
62         {
63                 return new Bitmap (filename, useEmbeddedColorManagement);
64         }
65         
66         public static Bitmap FromHbitmap (IntPtr hbitmap)
67         {
68                 // Fixme: implement me
69                 throw new NotImplementedException ();
70         }
71
72         public static Bitmap FromHbitmap (IntPtr hbitmap, IntPtr hpalette)
73         {
74                 // Fixme: implement me
75                 throw new NotImplementedException ();
76         }
77
78         public static Image FromStream (Stream stream)
79         {
80                 return new Bitmap (stream);
81         }
82         
83         public static Image FromStream (Stream stream, bool useECM)
84         {
85                 return new Bitmap (stream, useECM);
86         }
87         
88         internal BitmapData Decode (Stream streamIn) 
89         {
90                 Stream stream = streamIn;
91                 BitmapData result = new BitmapData ();
92                 if (!stream.CanSeek) {
93                         // FIXME: if stream.CanSeek == false, copy to a MemoryStream and read nicely 
94                 }
95                 ImageCodecInfo[] availableDecoders = ImageCodecInfo.GetImageDecoders();
96                 long pos = stream.Position;
97                 ImageCodecInfo codecToUse = null;
98                 foreach (ImageCodecInfo info in availableDecoders) {
99                         for (int i = 0; i < info.SignaturePatterns.Length; i++) {
100                                 stream.Seek(pos, SeekOrigin.Begin);
101                                 bool codecFound = true;
102                                 for (int iPattern = 0; iPattern < info.SignaturePatterns[i].Length; iPattern++) {
103                                         byte pattern = (byte)stream.ReadByte();
104                                         pattern &= info.SignatureMasks[i][iPattern];
105                                         if (pattern != info.SignaturePatterns[i][iPattern]) {
106                                                 codecFound = false;
107                                                 break;
108                                         }
109                                 }
110                                 if (codecFound) {
111                                         codecToUse = info;
112                                         break;
113                                 }
114                         }
115                 }
116                 stream.Seek (pos, SeekOrigin.Begin);
117                 if (codecToUse != null && codecToUse.decode != null) {
118                         codecToUse.decode (this, stream, result);
119                 }
120                 return result;
121         }
122         
123         public static int GetPixelFormatSize (PixelFormat pixfmt)
124         {
125                 int result = 0;
126                 switch (pixfmt) {
127                         case PixelFormat.Format16bppArgb1555:
128                         case PixelFormat.Format16bppGrayScale:
129                         case PixelFormat.Format16bppRgb555:
130                         case PixelFormat.Format16bppRgb565:
131                                 result = 16;
132                                 break;
133                         case PixelFormat.Format1bppIndexed:
134                                 result = 1;
135                                 break;
136                         case PixelFormat.Format24bppRgb:
137                                 result = 24;
138                                 break;
139                         case PixelFormat.Format32bppArgb:
140                         case PixelFormat.Format32bppPArgb:
141                         case PixelFormat.Format32bppRgb:
142                                 result = 32;
143                                 break;
144                         case PixelFormat.Format48bppRgb:
145                                 result = 48;
146                                 break;
147                         case PixelFormat.Format4bppIndexed:
148                                 result = 4;
149                                 break;
150                         case PixelFormat.Format64bppArgb:
151                         case PixelFormat.Format64bppPArgb:
152                                 result = 64;
153                                 break;
154                         case PixelFormat.Format8bppIndexed:
155                                 result = 8;
156                                 break;
157                 }
158                 return result;
159         }
160
161         public static bool IsAlphaPixelFormat (PixelFormat pixfmt)
162         {
163                 bool result = false;
164                 switch (pixfmt) {
165                         case PixelFormat.Format16bppArgb1555:
166                         case PixelFormat.Format32bppArgb:
167                         case PixelFormat.Format32bppPArgb:
168                         case PixelFormat.Format64bppArgb:
169                         case PixelFormat.Format64bppPArgb:
170                                 result = true;
171                                 break;
172                         case PixelFormat.Format16bppGrayScale:
173                         case PixelFormat.Format16bppRgb555:
174                         case PixelFormat.Format16bppRgb565:
175                         case PixelFormat.Format1bppIndexed:
176                         case PixelFormat.Format24bppRgb:
177                         case PixelFormat.Format32bppRgb:
178                         case PixelFormat.Format48bppRgb:
179                         case PixelFormat.Format4bppIndexed:
180                         case PixelFormat.Format8bppIndexed:
181                                 result = false;
182                                 break;
183                 }
184                 return result;
185         }
186         
187 //      public static bool IsCanonicalPixelFormat (PixelFormat pixfmt)
188 //      {
189 //              // Fixme: implement me
190 //              throw new NotImplementedException ();
191 //      }
192 //      
193 //      public static bool IsExtendedPixelFormat (PixelFormat pixfmt)
194 //      {
195 //              // Fixme: implement me
196 //              throw new NotImplementedException ();
197 //      }
198
199         // non-static
200         public RectangleF GetBounds (ref GraphicsUnit pageUnit)
201         {
202                 // Fixme: implement me
203                 throw new NotImplementedException ();
204         }
205         
206         //public EncoderParameters GetEncoderParameterList(Guid encoder);
207         //public int GetFrameCount(FrameDimension dimension);
208         //public PropertyItem GetPropertyItem(int propid);
209         /*
210           public Image GetThumbnailImage(int thumbWidth, int thumbHeight,
211           Image.GetThumbnailImageAbort callback,
212           IntPtr callbackData);
213         */
214         
215         public void RemovePropertyItem (int propid)
216         {
217                 // Fixme: implement me
218                 throw new NotImplementedException ();
219         }
220         
221         public void RotateFlip (RotateFlipType rotateFlipType)
222         {
223                 // Fixme: implement me
224                 throw new NotImplementedException ();
225         }
226
227         public void Save (string filename)
228         {
229                 Save (filename, RawFormat);
230         }
231
232         public void Save (Stream stream, ImageFormat format)
233         {
234                 ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
235
236                 foreach (ImageCodecInfo encoder in encoders) {
237                         if (encoder.FormatID != format.Guid)
238                                 continue;
239                         
240                         if (encoder.encode == null)
241                                 continue;
242                         if (!(this is Bitmap))
243                                 continue;
244                         encoder.encode(this, stream);
245                         break;
246                 }
247         }
248
249         public void Save(string filename, ImageFormat format) 
250         {
251                 FileStream fs = new FileStream (filename, FileMode.Create);
252                 Save(fs, format);
253                 fs.Flush();
254                 fs.Close();
255         }
256
257         //public void Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams);
258         //public void Save(string filename, ImageCodecInfo encoder, EncoderParameters encoderParams);
259         //public void SaveAdd(EncoderParameters_ encoderParams);
260         //public void SaveAdd(Image image, EncoderParameters_ encoderParams);
261         //public int SelectActiveFrame(FrameDimension dimension, int frameIndex);
262         //public void SetPropertyItem(PropertyItem propitem);
263
264         // properties
265         public int Flags {
266                 get {
267                         throw new NotImplementedException ();
268                 }
269         }
270         
271         public Guid[] FrameDimensionsList {
272                 get {
273                         throw new NotImplementedException ();
274                 }
275         }
276         
277         public int Height {
278                 get {
279                         return image_size.Height;
280                 }
281         }
282         
283         public float HorizontalResolution {
284                 get {
285                         throw new NotImplementedException ();
286                 }
287         }
288         
289         public ColorPalette Palette {
290                 get {
291                         return colorPalette;
292                 }
293                 set {
294                         colorPalette = value;
295                 }
296         }
297         
298         public SizeF PhysicalDimension {
299                 get {
300                         throw new NotImplementedException ();
301                 }
302         }
303         
304         public PixelFormat PixelFormat {
305                 get {
306                         return pixel_format;
307                 }
308         }
309         
310         public int[] PropertyIdList {
311                 get {
312                         throw new NotImplementedException ();
313                 }
314         }
315         
316         public PropertyItem[] PropertyItems {
317                 get {
318                         throw new NotImplementedException ();
319                 }
320         }
321
322         public ImageFormat RawFormat {
323                 get {
324                         return raw_format;
325                 }
326         }
327
328         internal void SetRawFormat (ImageFormat format)
329         {
330                 raw_format = format;
331         }
332
333         public Size Size {
334                 get {
335                         return image_size;
336                 }
337         }
338         
339         public float VerticalResolution {
340                 get {
341                         throw new NotImplementedException ();
342                 }
343         }
344         
345         public int Width {
346                 get {
347                         return image_size.Width;
348                 }
349         }
350         
351         internal IntPtr NativeObject{
352                 get{
353                         return nativeObject;
354                 }
355                 set     {
356                         nativeObject = value;
357                 }
358         }
359         
360         public void Dispose ()
361         {
362                 Dispose (true);
363         }
364
365         ~Image ()
366         {
367                 Dispose (false);
368         }
369
370         protected virtual void DisposeResources ()
371         {
372                 GDIPlus.GdipDisposeImage (nativeObject);
373         }
374         
375         protected virtual void Dispose (bool disposing)
376         {
377                 if (nativeObject != (IntPtr) 0){
378                         DisposeResources ();
379                         nativeObject=IntPtr.Zero;
380                 }
381         }
382         
383         [MonoTODO]
384         object ICloneable.Clone()
385         {
386                 throw new NotImplementedException ();
387         }
388
389 }
390
391 }