* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Drawing / System.Drawing / Bitmap.jvm.cs
index af0fd07b38e977cb8ea868df69c460556a72a5c6..90db022df0d0692792abcf88ad54ad0601203a19 100644 (file)
@@ -17,12 +17,19 @@ namespace System.Drawing
 {
        public sealed class Bitmap : Image {
 
+               # region Static fields
+
+               static readonly image.ColorModel _jpegColorModel = new image.DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
+
+               #endregion
+
                #region constructors
 
                Bitmap (PlainImage orig) {
                        base.Initialize( orig, false );
                }
 
+               [MonoTODO]
                private Bitmap (SerializationInfo info, StreamingContext context) {
                        throw new NotImplementedException ();
                }
@@ -45,6 +52,7 @@ namespace System.Drawing
                internal Bitmap (java.awt.Image nativeObject, ImageFormat format)
                        :base (nativeObject, format) {}
 
+               [MonoTODO]
                private Bitmap (java.awt.Image nativeObject, ImageFormat format, PixelFormat pixFormat)
                        :this (nativeObject, format) {
                        if (pixFormat != this.PixelFormat)
@@ -67,9 +75,11 @@ namespace System.Drawing
                public Bitmap (string filename) 
                        :this (filename, false) {}
 
+               [MonoTODO]
                public Bitmap (Stream stream, bool useIcm)
                        :this (stream, useIcm, null) {}
 
+               [MonoTODO]
                public Bitmap (string filename, bool useIcm)
                        :this (filename, useIcm, null) {}
 
@@ -80,11 +90,11 @@ namespace System.Drawing
                }
 
                internal Bitmap (string filename, bool useIcm, ImageFormat format) {
-                       // TBD: useIcm param
-                       java.io.File file = vmw.common.IOUtils.getJavaFile (filename);
-                       if (!file.exists ())
-                                throw new System.IO.FileNotFoundException (filename);
-                       Initialize (new stream.FileImageInputStream (file), format);
+                       using(FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
+                               // TBD: useIcm param
+                               io.InputStream jis = vmw.common.IOUtils.ToInputStream (stream);
+                               Initialize (new stream.MemoryCacheImageInputStream (jis), format);
+                       }
                }
 
                public Bitmap (Type type, string resource) {
@@ -97,6 +107,7 @@ namespace System.Drawing
                        }
                }
 #if INTPTR_SUPPORT
+               [MonoTODO]
                public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
                {                                               
                        throw new NotImplementedException();                    
@@ -133,6 +144,9 @@ namespace System.Drawing
                        catch (Exception) {
                                throw new OutOfMemoryException ("Out of memory");
                        }
+                       finally {
+                               ic.Dispose();
+                       }
                }
 
                #endregion
@@ -141,17 +155,35 @@ namespace System.Drawing
                protected override void InternalSave (stream.ImageOutputStream output, Guid clsid) {
 
                        ImageCodec ic = ImageCodec.CreateWriter( clsid );
+                       using (ic) {
+
+                               PlainImage plainImage = CurrentImage;
+                               plainImage.NativeImage.flush();
+
+                               if ( ImageCodec.ClsidToImageFormat( clsid ).Equals( ImageFormat.Jpeg ) ) {
+                                       image.ColorModel cm = ((image.BufferedImage)CurrentImage.NativeImage).getColorModel();
+                                       if (cm.hasAlpha()) {
+                                               if (cm is image.DirectColorModel) {
+                                                       image.Raster raster = ((image.BufferedImage)CurrentImage.NativeImage).getRaster();
+                                                       image.DataBuffer db = raster.getDataBuffer();
+                                                       image.DirectColorModel dcm = (image.DirectColorModel)cm;
+                                                       image.SinglePixelPackedSampleModel jpegSampleModel = new image.SinglePixelPackedSampleModel( 
+                                                               db.getDataType(), Width, Height, 
+                                                               new int[] {dcm.getRedMask(), dcm.getGreenMask(), dcm.getBlueMask()}     );
+               
+                                                       image.BufferedImage tb = new image.BufferedImage( 
+                                                               _jpegColorModel, 
+                                                               image.Raster.createWritableRaster( jpegSampleModel, db, null ),
+                                                               false, null );
 
-                       // .net saves in png if cannot find requested encoder. act id 316563
-                       if (ic == null)
-                               ic = ImageCodec.CreateWriter( ImageFormat.Png );
+                                                       plainImage = new PlainImage( tb, plainImage.Thumbnails, ImageFormat.Jpeg, plainImage.HorizontalResolution, plainImage.VerticalResolution, plainImage.Dimension );
+                                                       plainImage.NativeMetadata = plainImage.NativeMetadata;
+                                               }
+                                       }
+                               }
 
-                       if (ic != null) {
                                ic.NativeStream = output;
-                               ic.WritePlainImage( CurrentImage );
-                       }
-                       else {
-                               throw new NotSupportedException("The requested format encoder is not supported");
+                               ic.WritePlainImage( plainImage );
                        }
                }
 
@@ -178,7 +210,7 @@ namespace System.Drawing
                                case PixelFormat.Indexed:
                                        return BufferedImage.TYPE_BYTE_INDEXED;
                                default:
-                                       return 0;
+                                       return BufferedImage.TYPE_INT_ARGB;
                        }                       
                }
 
@@ -222,18 +254,23 @@ namespace System.Drawing
                
                public Bitmap Clone (RectangleF rect, PixelFormat pixFormat)
                {
-                       PlainImage plainImage = (PlainImage)CurrentImage.Clone();
-                       plainImage.NativeImage = ((BufferedImage)plainImage.NativeImage).getSubimage((int)rect.X,(int)rect.Y,(int)rect.Width,(int)rect.Height);
-                       
-                       if (pixFormat != this.PixelFormat)
-                               throw new NotImplementedException ("Converting PixelFormat is not implemented yet.");
-       
+                       PlainImage plainImage = CurrentImage.Clone(false);
+                       BufferedImage clone = new BufferedImage( (int)rect.Width, (int)rect.Height, ToBufferedImageFormat( pixFormat ) );
+                       awt.Graphics2D g = clone.createGraphics();
+                       try {
+                               g.drawImage( NativeObject, -(int)rect.X, -(int)rect.Y, null );
+                       }
+                       finally {
+                               g.dispose();
+                       }
+
+                       plainImage.NativeImage = clone;
                        return new Bitmap(plainImage);
                }
                #endregion
 
                #region LockBits
-               // TBD: implement this
+               [MonoTODO]
                public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format) {
                        throw new NotImplementedException();
                }
@@ -242,15 +279,17 @@ namespace System.Drawing
                #region MakeTransparent
                public void MakeTransparent ()
                {
-                       Color clr = GetPixel(0,0);                      
+                       Color clr = Color.FromArgb(0,0,0);                      
                        MakeTransparent (clr);
                }
 
                public void MakeTransparent (Color transparentColor)
                {
-                       byte A = transparentColor.A;
                        image.WritableRaster raster = NativeObject.getRaster();
                        int numBands  = raster.getNumBands();
+                       if (numBands != 4)
+                               return;
+
                        int maxWidth  = raster.getWidth() + raster.getMinX();
                        int maxHeight = raster.getHeight() + raster.getMinY();
                        int[] srcPix  = new int[numBands];
@@ -258,12 +297,11 @@ namespace System.Drawing
                        for (int y = raster.getMinY(); y < maxHeight; y++) {
                                for (int x = raster.getMinX(); x < maxWidth; x++) {
                                        /*srcPix =*/ raster.getPixel(x, y, srcPix);
-                                       for (int z = 0; z < numBands; z++) {
-                                               int argb = srcPix[z];
-                                               if ((uint)argb >> 24 == A) {
-                                                       argb &= 0x00FFFFFF;
-                                                       srcPix[z] = argb;
-                                               }
+                                       if (srcPix[0] == transparentColor.R &&
+                                               srcPix[1] == transparentColor.G &&
+                                               srcPix[2] == transparentColor.B) {
+                                               srcPix[3] = 0;
+                                               raster.setPixel(x, y, srcPix);
                                        }
                                }
                        }
@@ -279,7 +317,7 @@ namespace System.Drawing
                #endregion 
 
                #region UnlockBits
-               // TBD: implement this
+               [MonoTODO]
                public void UnlockBits (BitmapData bitmap_data)
                {
                        throw new NotImplementedException();
@@ -347,26 +385,31 @@ namespace System.Drawing
                #endregion
 
 #if INTPTR_SUPPORT
+               [MonoTODO]
                public static Bitmap FromHicon (IntPtr hicon)
                {       
                        throw new NotImplementedException();
                }
 
+               [MonoTODO]
                public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TBD: Untested
                {
                        throw new NotImplementedException();
                }
 
+               [MonoTODO]
                public IntPtr GetHbitmap ()
                {
                        throw new NotImplementedException();
                }
 
+               [MonoTODO]
                public IntPtr GetHbitmap (Color background)
                {
                        throw new NotImplementedException();
                }
 
+               [MonoTODO]
                public IntPtr GetHicon ()
                {
                        throw new NotImplementedException();