Fix MakeTransparent
[mono.git] / mcs / class / System.Drawing / System.Drawing / Bitmap.jvm.cs
1 using System;
2 using System.IO;
3 using System.Drawing.Imaging;
4 using System.Runtime.Serialization;
5 using Mainsoft.Drawing.Imaging;
6
7 using io = java.io;
8 using imageio = javax.imageio;
9 using stream = javax.imageio.stream;
10 using spi = javax.imageio.spi;
11 using BufferedImage = java.awt.image.BufferedImage;
12 using JavaImage = java.awt.Image;
13 using awt = java.awt;
14 using image = java.awt.image;
15
16 namespace System.Drawing 
17 {
18         public sealed class Bitmap : Image {
19
20                 # region Static fields
21
22                 static readonly image.ColorModel _jpegColorModel = new image.DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
23
24                 #endregion
25
26                 #region constructors
27
28                 Bitmap (PlainImage orig) {
29                         base.Initialize( orig, false );
30                 }
31
32                 private Bitmap (SerializationInfo info, StreamingContext context) {
33                         throw new NotImplementedException ();
34                 }
35
36                 public Bitmap (int width, int height, Graphics g) 
37                         :this (width, height, PixelFormat.Format32bppArgb) {
38                         CurrentImage.HorizontalResolution = g.DpiX;
39                         CurrentImage.VerticalResolution = g.DpiY;
40                 }
41
42                 public Bitmap (Image original) 
43                         :this (original, original.Size) {}
44
45                 public Bitmap (Image orig, Size newSize)
46                         :this (orig, newSize.Width, newSize.Height) {}
47
48                 public Bitmap (Image orig, int width, int height)
49                         :base (CreateScaledImage (orig, width, height), ImageFormat.MemoryBmp) {}
50
51                 internal Bitmap (java.awt.Image nativeObject, ImageFormat format)
52                         :base (nativeObject, format) {}
53
54                 private Bitmap (java.awt.Image nativeObject, ImageFormat format, PixelFormat pixFormat)
55                         :this (nativeObject, format) {
56                         if (pixFormat != this.PixelFormat)
57                                 throw new NotImplementedException ("Converting PixelFormat is not implemented yet.");
58                 }
59
60                 public Bitmap (int width, int height) 
61                         :this (width, height, PixelFormat.Format32bppArgb) {}
62
63                 public Bitmap (int width, int height, PixelFormat format)
64                         :base (
65                         new java.awt.image.BufferedImage (width, height,
66                         ToBufferedImageFormat (format)),
67                         ImageFormat.Bmp) {
68                 }
69
70                 public Bitmap (Stream stream)
71                         :this (stream, false) {}
72
73                 public Bitmap (string filename) 
74                         :this (filename, false) {}
75
76                 public Bitmap (Stream stream, bool useIcm)
77                         :this (stream, useIcm, null) {}
78
79                 public Bitmap (string filename, bool useIcm)
80                         :this (filename, useIcm, null) {}
81
82                 internal Bitmap (Stream stream, bool useIcm, ImageFormat format) {
83                         // TBD: useIcm param
84                         io.InputStream jis = vmw.common.IOUtils.ToInputStream (stream);
85             Initialize (new stream.MemoryCacheImageInputStream (jis), format);
86                 }
87
88                 internal Bitmap (string filename, bool useIcm, ImageFormat format) {
89                         using(FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
90                                 // TBD: useIcm param
91                                 io.InputStream jis = vmw.common.IOUtils.ToInputStream (stream);
92                                 Initialize (new stream.MemoryCacheImageInputStream (jis), format);
93                         }
94                 }
95
96                 public Bitmap (Type type, string resource) {
97                         using (Stream s = type.Assembly.GetManifestResourceStream (resource)) {
98                                 if (s == null)
99                                         throw new ArgumentException("Resource '" + resource + "' could not be found in class '" + type.ToString() + "'");
100
101                                 io.InputStream jis = vmw.common.IOUtils.ToInputStream (s);
102                                 Initialize (new stream.MemoryCacheImageInputStream (jis), null);
103                         }
104                 }
105 #if INTPTR_SUPPORT
106                 public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
107                 {                                               
108                         throw new NotImplementedException();                    
109                 }
110 #endif
111                 #endregion
112
113                 #region Internal Initialization
114
115                 private void Initialize (stream.ImageInputStream input, ImageFormat format) {
116                         ImageCodec ic = null;
117
118                         if (format == null)
119                                 ic = ImageCodec.CreateReader(input);
120                         else
121                                 ic = ImageCodec.CreateReader(format);
122
123                         try {
124                                 ic.NativeStream = input;
125                                 PlainImage pi = ic.ReadPlainImage();
126                                 base.Initialize( pi, false );
127
128                                 pi = ic.ReadNextPlainImage();
129                                 while ( pi != null) {
130                                         base.Initialize( pi, true );
131                                         pi = ic.ReadNextPlainImage();
132                                 }
133
134                                 _flags |= (int)(ImageFlags.ImageFlagsReadOnly | ImageFlags.ImageFlagsHasRealPixelSize);
135                         }
136                         catch (IOException ex) {
137                                 throw ex;
138                         }
139                         catch (Exception) {
140                                 throw new OutOfMemoryException ("Out of memory");
141                         }
142                         finally {
143                                 ic.Dispose();
144                         }
145                 }
146
147                 #endregion
148
149                 #region InternalSave
150                 protected override void InternalSave (stream.ImageOutputStream output, Guid clsid) {
151
152                         ImageCodec ic = ImageCodec.CreateWriter( clsid );
153                         using (ic) {
154
155                                 PlainImage plainImage = CurrentImage;
156                                 plainImage.NativeImage.flush();
157
158                                 if ( ImageCodec.ClsidToImageFormat( clsid ).Equals( ImageFormat.Jpeg ) ) {
159                                         image.ColorModel cm = ((image.BufferedImage)CurrentImage.NativeImage).getColorModel();
160                                         if (cm.hasAlpha()) {
161                                                 if (cm is image.DirectColorModel) {
162                                                         image.Raster raster = ((image.BufferedImage)CurrentImage.NativeImage).getRaster();
163                                                         image.DataBuffer db = raster.getDataBuffer();
164                                                         image.DirectColorModel dcm = (image.DirectColorModel)cm;
165                                                         image.SinglePixelPackedSampleModel jpegSampleModel = new image.SinglePixelPackedSampleModel( 
166                                                                 db.getDataType(), Width, Height, 
167                                                                 new int[] {dcm.getRedMask(), dcm.getGreenMask(), dcm.getBlueMask()}     );
168                 
169                                                         image.BufferedImage tb = new image.BufferedImage( 
170                                                                 _jpegColorModel, 
171                                                                 image.Raster.createWritableRaster( jpegSampleModel, db, null ),
172                                                                 false, null );
173
174                                                         plainImage = new PlainImage( tb, plainImage.Thumbnails, ImageFormat.Jpeg, plainImage.HorizontalResolution, plainImage.VerticalResolution, plainImage.Dimension );
175                                                         plainImage.NativeMetadata = plainImage.NativeMetadata;
176                                                 }
177                                         }
178                                 }
179
180                                 ic.NativeStream = output;
181                                 ic.WritePlainImage( plainImage );
182                         }
183                 }
184
185                 #endregion
186
187                 #region private statics: ToBufferedImageFormat, CreateScaledImage
188
189                 private static int ToBufferedImageFormat (PixelFormat format) {
190                         switch(format) {
191                                 case PixelFormat.Format16bppGrayScale:
192                                         return BufferedImage.TYPE_USHORT_GRAY;
193                                 case PixelFormat.Format1bppIndexed:
194                                         return BufferedImage.TYPE_BYTE_GRAY;
195                                 case PixelFormat.Format32bppArgb:
196                                         return BufferedImage.TYPE_INT_ARGB;
197                                 case PixelFormat.Format32bppRgb:
198                                         return BufferedImage.TYPE_INT_RGB;
199                                 case PixelFormat.Format32bppPArgb:
200                                         return BufferedImage.TYPE_INT_ARGB_PRE;
201                                 case PixelFormat.Format16bppRgb555:
202                                         return BufferedImage.TYPE_USHORT_555_RGB;
203                                 case PixelFormat.Format16bppRgb565:
204                                         return BufferedImage.TYPE_USHORT_565_RGB;
205                                 case PixelFormat.Indexed:
206                                         return BufferedImage.TYPE_BYTE_INDEXED;
207                                 default:
208                                         return BufferedImage.TYPE_INT_ARGB;
209                         }                       
210                 }
211
212                 private static java.awt.Image CreateScaledImage(Image original, int width, int height) {
213                         JavaImage oldscaled = original.CurrentImage.NativeImage.getScaledInstance(width, height,
214                                 JavaImage.SCALE_DEFAULT);
215                         BufferedImage newimage = new BufferedImage(oldscaled.getWidth(null), 
216                                 oldscaled.getHeight(null),
217                                 BufferedImage.TYPE_INT_ARGB);
218                         java.awt.Graphics2D graphics2d = newimage.createGraphics();
219                         graphics2d.drawImage(oldscaled, 0, 0, null);
220                         graphics2d.dispose();
221                         return newimage;                                
222                 }
223                 #endregion
224
225                 #region Get-SetPixel
226                 public Color GetPixel (int x, int y) 
227                 {
228
229                         int argb = NativeObject.getRGB(x,y);                            
230                         return Color.FromArgb(argb); 
231                 }
232
233                 public void SetPixel (int x, int y, Color color)
234                 {                               
235                         int rgb = color.ToArgb();
236                         NativeObject.setRGB(x,y,rgb);
237                 }
238                 #endregion
239
240                 #region Clone
241                 public override object Clone () {
242                         return new Bitmap ( (PlainImage)CurrentImage.Clone() );
243                 }
244
245                 public Bitmap Clone (Rectangle rect, PixelFormat pixFormat)
246                 {
247                         return Clone(new RectangleF( rect.X, rect.Y, rect.Width, rect.Height ), pixFormat);
248         }
249                 
250                 public Bitmap Clone (RectangleF rect, PixelFormat pixFormat)
251                 {
252                         PlainImage plainImage = CurrentImage.Clone(false);
253                         BufferedImage clone = new BufferedImage( (int)rect.Width, (int)rect.Height, ToBufferedImageFormat( pixFormat ) );
254                         awt.Graphics2D g = clone.createGraphics();
255                         try {
256                                 g.drawImage( NativeObject, -(int)rect.X, -(int)rect.Y, null );
257                         }
258                         finally {
259                                 g.dispose();
260                         }
261
262                         plainImage.NativeImage = clone;
263                         return new Bitmap(plainImage);
264                 }
265                 #endregion
266
267                 #region LockBits
268                 // TBD: implement this
269                 public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format) {
270                         throw new NotImplementedException();
271                 }
272                 #endregion
273
274                 #region MakeTransparent
275                 public void MakeTransparent ()
276                 {
277                         Color clr = Color.FromArgb(0,0,0);                      
278                         MakeTransparent (clr);
279                 }
280
281                 public void MakeTransparent (Color transparentColor)
282                 {
283                         image.WritableRaster raster = NativeObject.getRaster();
284                         int numBands  = raster.getNumBands();
285                         if (numBands != 4)
286                                 return;
287
288                         int maxWidth  = raster.getWidth() + raster.getMinX();
289                         int maxHeight = raster.getHeight() + raster.getMinY();
290                         int[] srcPix  = new int[numBands];
291
292                         for (int y = raster.getMinY(); y < maxHeight; y++) {
293                                 for (int x = raster.getMinX(); x < maxWidth; x++) {
294                                         /*srcPix =*/ raster.getPixel(x, y, srcPix);
295                                         if (srcPix[0] == transparentColor.R &&
296                                                 srcPix[1] == transparentColor.G &&
297                                                 srcPix[2] == transparentColor.B) {
298                                                 srcPix[3] = 0;
299                                                 raster.setPixel(x, y, srcPix);
300                                         }
301                                 }
302                         }
303                 }
304                 #endregion
305
306                 #region SetResolution
307                 public void SetResolution (float xDpi, float yDpi)
308                 {
309                         CurrentImage.HorizontalResolution = xDpi;
310                         CurrentImage.VerticalResolution = yDpi;
311                 }
312                 #endregion 
313
314                 #region UnlockBits
315                 // TBD: implement this
316                 public void UnlockBits (BitmapData bitmap_data)
317                 {
318                         throw new NotImplementedException();
319                 }
320                 #endregion 
321
322                 #region NativeObject
323                 internal new BufferedImage NativeObject {
324                         get {
325                                 return (BufferedImage)base.NativeObject.CurrentImage.NativeImage;
326                         }
327                 }
328
329                 protected override java.awt.Image[] CloneNativeObjects(java.awt.Image[] src) {
330                         if (src == null)
331                                 return null;
332
333                         awt.Image[] dst = new awt.Image[src.Length];
334                         for (int i = 0; i < dst.Length; i++) {
335                                 BufferedImage image = src[i] as BufferedImage;
336                                 if (image == null)
337                                         throw new ArgumentException(String.Format("Unsupported image type '{0}'", src[i].ToString()), "src");
338
339                                 dst[i] = new BufferedImage(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);
340                         }
341
342                         return dst;
343                 }
344
345                 #endregion
346
347                 #region InternalPixelFormat
348                 protected override PixelFormat InternalPixelFormat {
349                         get {
350                                 int t = NativeObject.getType();
351                                 switch(t) {
352                                         case 11://JavaImage.TYPE_USHORT_GRAY:
353                                                 return PixelFormat.Format16bppGrayScale;
354                                         case 10://JavaImage.TYPE_BYTE_GRAY:
355                                                 return PixelFormat.Format1bppIndexed;                           
356                                         case 1: //JavaImage.TYPE_INT_RGB
357                                                 return PixelFormat.Format32bppRgb;
358                                         case 2: //JavaImage.TYPE_INT_ARGB:                      
359                                                 return PixelFormat.Format32bppArgb;
360                                         case 3://JavaImage.TYPE_INT_ARGB_PRE:
361                                                 return PixelFormat.Format32bppPArgb;
362                                         case 9://JavaImage.TYPE_USHORT_555_RGB:
363                                                 return PixelFormat.Format16bppRgb555;
364                                         case 8://JavaImage.TYPE_USHORT_565_RGB:
365                                                 return PixelFormat.Format16bppRgb565;
366                                         case 13://JavaImage.TYPE_BYTE_INDEXED:
367                                                 return PixelFormat.Indexed;
368                                                 //TBD: support this
369                                         case 12://JavaImage.TYPE_BYTE_BINARY:
370                                         case 0://JavaImage.TYPE_CUSTOM:
371                                         case 4://JavaImage.TYPE_INT_BGR:
372                                         case 5://JavaImage.TYPE_3BYTE_BGR:                                      
373                                         case 6://JavaImage.TYPE_4BYTE_ABGR:
374                                         case 7://JavaImage.TYPE_4BYTE_ABGR_PRE:
375                                         default:
376                                                 return PixelFormat.Undefined;
377                                 }                       
378                         }               
379                 }
380                 #endregion
381
382 #if INTPTR_SUPPORT
383                 public static Bitmap FromHicon (IntPtr hicon)
384                 {       
385                         throw new NotImplementedException();
386                 }
387
388                 public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TBD: Untested
389                 {
390                         throw new NotImplementedException();
391                 }
392
393                 public IntPtr GetHbitmap ()
394                 {
395                         throw new NotImplementedException();
396                 }
397
398                 public IntPtr GetHbitmap (Color background)
399                 {
400                         throw new NotImplementedException();
401                 }
402
403                 public IntPtr GetHicon ()
404                 {
405                         throw new NotImplementedException();
406                 }
407 #endif
408
409         }
410 }