Inheritance: INativeObject, IDisposable
Esempio n. 1
0
 public Bitmap(string filename)
 {
     // Use Image IO
     CGDataProvider prov = new CGDataProvider(filename);
     var cg = CGImageSource.FromDataProvider(prov).CreateImage(0, null);
     InitWithCGImage(cg);
 }
Esempio n. 2
0
        public static CGImage FromPNG(CGDataProvider provider, nfloat [] decode, bool shouldInterpolate, CGColorRenderingIntent intent)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }

            var handle = CGImageCreateWithPNGDataProvider(provider.Handle, decode, shouldInterpolate, intent);

            if (handle == IntPtr.Zero)
            {
                return(null);
            }

            return(new CGImage(handle, true));
        }
Esempio n. 3
0
        internal void RotateFlip(RotateFlipType rotateFlipType)
        {
            CGAffineTransform rotateFlip = CGAffineTransform.MakeIdentity();

            int width, height;
            width = NativeCGImage.Width;
            height = NativeCGImage.Height;

            switch (rotateFlipType)
            {
                //			case RotateFlipType.RotateNoneFlipNone:
                //			//case RotateFlipType.Rotate180FlipXY:
                //				rotateFlip = GeomUtilities.CreateRotateFlipTransform (b.Width, b.Height, 0, false, false);
                //				break;
                case RotateFlipType.Rotate90FlipNone:
                //case RotateFlipType.Rotate270FlipXY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform (ref width, ref height, 90, false, false);
                break;
                case RotateFlipType.Rotate180FlipNone:
                //case RotateFlipType.RotateNoneFlipXY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform (ref width, ref height, 0, true, true);
                break;
                case RotateFlipType.Rotate270FlipNone:
                //case RotateFlipType.Rotate90FlipXY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform (ref width, ref height, 270, false, false);
                break;
                case RotateFlipType.RotateNoneFlipX:
                //case RotateFlipType.Rotate180FlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform (ref width, ref height, 0, true, false);
                break;
                case RotateFlipType.Rotate90FlipX:
                //case RotateFlipType.Rotate270FlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform (ref width, ref height, 90, true, false);
                break;
                case RotateFlipType.Rotate180FlipX:
                //case RotateFlipType.RotateNoneFlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform (ref width, ref height, 0, false, true);
                break;
                case RotateFlipType.Rotate270FlipX:
                //case RotateFlipType.Rotate90FlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform (ref width, ref height, 270, true, false);
                break;
            }

            var format = GetBestSupportedFormat (pixelFormat);
            var bitmapContext = CreateCompatibleBitmapContext (width, height, format);

            bitmapContext.ConcatCTM (rotateFlip);

            bitmapContext.DrawImage (new RectangleF (0, 0, NativeCGImage.Width, NativeCGImage.Height), NativeCGImage);

            int size = bitmapContext.BytesPerRow * bitmapContext.Height;
            var provider = new CGDataProvider (bitmapContext.Data, size, true);

            // If the width or height is not the seme we need to switch the dpiHeight and dpiWidth
            // We should be able to get around this with set resolution later.
            if (NativeCGImage.Width != width || NativeCGImage.Height != height)
            {
                var temp = dpiWidth;
                dpiHeight = dpiWidth;
                dpiWidth = temp;
            }

            NativeCGImage = new CGImage (bitmapContext.Width, bitmapContext.Height, bitmapContext.BitsPerComponent,
                                         bitmapContext.BitsPerPixel, bitmapContext.BytesPerRow,
                                         bitmapContext.ColorSpace,
                                         bitmapContext.AlphaInfo,
                                         provider, null, true, CGColorRenderingIntent.Default);

            physicalDimension.Width = (float)width;
            physicalDimension.Height = (float)height;

            physicalSize = new SizeF (physicalDimension.Width, physicalDimension.Height);
            physicalSize.Width *= ConversionHelpers.MS_DPI / dpiWidth;
            physicalSize.Height *= ConversionHelpers.MS_DPI / dpiHeight;

            // In windows the RawFormat is changed to MemoryBmp to show that the image has changed.
            rawFormat = ImageFormat.MemoryBmp;

            // Set our transform for this image for the new height
            imageTransform = new CGAffineTransform(1, 0, 0, -1, 0, height);
        }
Esempio n. 4
0
        internal CGBitmapContext GetRenderableContext()
        {
            if (cachedContext != null && cachedContext.Handle != IntPtr.Zero)
                return cachedContext;

            var format = GetBestSupportedFormat (pixelFormat);
            var bitmapContext = CreateCompatibleBitmapContext (NativeCGImage.Width, NativeCGImage.Height, format);

            bitmapContext.DrawImage (new RectangleF (0, 0, NativeCGImage.Width, NativeCGImage.Height), NativeCGImage);

            int size = bitmapContext.BytesPerRow * bitmapContext.Height;
            var provider = new CGDataProvider (bitmapContext.Data, size, true);

            CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
            NativeCGImage = new CGImage (bitmapContext.Width, bitmapContext.Height, bitmapContext.BitsPerComponent,
                                         bitmapContext.BitsPerPixel, bitmapContext.BytesPerRow,
                                         colorSpace,
                                         bitmapContext.AlphaInfo,
                                         provider, null, true, CGColorRenderingIntent.Default);
            colorSpace.Dispose ();
            cachedContext = bitmapContext;

            return cachedContext;
        }
Esempio n. 5
0
		public static CGImage FromPNG (CGDataProvider provider, float [] decode, bool shouldInterpolate, CGColorRenderingIntent intent)
		{
			if (provider == null)
				throw new ArgumentNullException ("provider");
			
			var handle = CGImageCreateWithPNGDataProvider (provider.Handle, decode, shouldInterpolate, intent);
			if (handle == IntPtr.Zero)
				return null;

			return new CGImage (handle, true);
		}
Esempio n. 6
0
		public static CGFont CreateFromProvider (CGDataProvider provider)
		{
			if (provider == null)
				throw new ArgumentNullException ("provider");
			return new CGFont (CGFontCreateWithDataProvider (provider.Handle), true);
		}
		internal static CGImage GetImage (this CGBitmapContext bitmapContext)
        {
            var provider = new CGDataProvider (bitmapContext.Data, (int)(bitmapContext.BytesPerRow * bitmapContext.Height), true);

            var NativeCGImage = new CGImage ((int)bitmapContext.Width, 
                                        (int)bitmapContext.Height, 
                (int)bitmapContext.BitsPerComponent, 
                (int)bitmapContext.BitsPerPixel, 
                (int)bitmapContext.BytesPerRow, 
                                        bitmapContext.ColorSpace,  
                                        (CGBitmapFlags)bitmapContext.BitmapInfo,
                                        provider, 
                                        null, 
                                        false, 
                                        CGColorRenderingIntent.Default);
            return NativeCGImage;
        }
		public static CGImageSource FromDataProvider (CGDataProvider provider, CGImageOptions options)
		{
			if (provider == null)
				throw new ArgumentNullException ("provider");

			using (var dict = options == null ? null : options.ToDictionary ())
				return new CGImageSource (CGImageSourceCreateWithDataProvider (provider.Handle, dict == null ? IntPtr.Zero : dict.Handle), true);
		}
Esempio n. 9
0
        private NSImage LoadStream(System.IO.Stream s)
        {
            using(var ms = new MemoryStream())
            {
                s.CopyTo(ms);
                ms.Flush();
                ms.Close();

                var b = ms.ToArray();

                var dp = new CGDataProvider(b, 0, b.Length);
                var img2 = CGImage.FromPNG(dp, null, false, CGColorRenderingIntent.Default);

                return new NSImage(img2, new System.Drawing.SizeF(18, 18));
            }
        }
        void LoadFontFile(string fileName)
        {
            CTFont nativeFont;
            var dpiSize = 0;
            var ext = Path.GetExtension(fileName);

            if (!String.IsNullOrEmpty(ext))
            {

                if (nativeFontDescriptors == null)
                    nativeFontDescriptors = new Dictionary<string, CTFontDescriptor> ();

                //Try loading from Bundle first
                var fontName = fileName.Substring (0, fileName.Length - ext.Length);
                var pathForResource = NSBundle.MainBundle.PathForResource (fontName, ext.Substring(1));

                NSUrl url;

             				if (!string.IsNullOrEmpty(pathForResource))
                    url = NSUrl.FromFilename (pathForResource);
                else
                    url = NSUrl.FromFilename (fileName);

                // We will not use CTFontManager.RegisterFontsForUrl (url, CTFontManagerScope.Process);
                // here.  The reason is that there is no way we can be sure that the font can be created to
                // to identify the family name afterwards.  So instead we will create a CGFont from a data provider.
                // create CTFont to obtain the CTFontDescriptor, store family name and font descriptor to be accessed
                // later.
                try {
                    var dataProvider = new CGDataProvider (url.Path);
                    var cgFont = CGFont.CreateFromProvider (dataProvider);

                    try {
                        nativeFont = new CTFont(cgFont, dpiSize, null);
                        if (!nativeFontDescriptors.ContainsKey(nativeFont.FamilyName))
                        {
                            nativeFontDescriptors.Add(nativeFont.FamilyName, nativeFont.GetFontDescriptor());
                            NSError error;
                            var registered = CTFontManager.RegisterGraphicsFont(cgFont, out error);
                            if (!registered)
                            {
                                // If the error code is 105 then the font we are trying to register is already registered
                                // We will not report this as an error.
                                if (error.Code != 105)
                                    throw new ArgumentException("Error registering: " + Path.GetFileName(fileName));
                            }
                        }
                    }
                    catch
                    {
                        // note: MS throw the same exception FileNotFoundException if the file exists but isn't a valid font file
                        throw new System.IO.FileNotFoundException (fileName);
                    }
                }
                catch (Exception)
                {
                    // note: MS throw the same exception FileNotFoundException if the file exists but isn't a valid font file
                    throw new System.IO.FileNotFoundException (fileName);
                }
            }
        }
Esempio n. 11
0
 private CGImage ToCGImage(Image img) 
 {
     System.IO.MemoryStream s = new System.IO.MemoryStream();
     img.Save(s, System.Drawing.Imaging.ImageFormat.Png);
     byte[] b = s.ToArray();
     CGDataProvider dp = new CGDataProvider(b,0,(int)s.Length);
     s.Flush();
     s.Close();
     CGImage img2 = CGImage.FromPNG(dp,null,false,CGColorRenderingIntent.Default);
     dp.Dispose ();
     s.Dispose ();
     return img2;
 }
        string LoadFontFile (string fileName)
        {
            CTFont nativeFont;
            var dpiSize = 0;
            var ext = Path.GetExtension(fileName);

            if (!String.IsNullOrEmpty(ext))
            {

                if (nativeFontDescriptors == null)
                    nativeFontDescriptors = new Dictionary<string, string> ();

                string fd = null;
                if (nativeFontDescriptors.TryGetValue(fileName, out fd))
                    return fd;
                        
                // We will not use CTFontManager.RegisterFontsForUrl (url, CTFontManagerScope.Process);
                // here.  The reason is that there is no way we can be sure that the font can be created to
                // to identify the family name afterwards.  So instead we will create a CGFont from a data provider.
                // create CTFont to obtain the CTFontDescriptor, store family name and font descriptor to be accessed
                // later.
                try {

                    var filePath = string.Empty;
                    CCContentManager.SharedContentManager.GetAssetStreamAsBytes(fileName, out filePath);

                    var dataProvider = new CGDataProvider (filePath);
                    var cgFont = CGFont.CreateFromProvider (dataProvider);

                    try 
                    {
                        nativeFont = new CTFont(cgFont, dpiSize, null);
                        if (!nativeFontDescriptors.ContainsKey(fileName))
                        {
                            nativeFontDescriptors.Add(fileName, nativeFont.PostScriptName);
                            NSError error;

                            var registered = CTFontManager.RegisterGraphicsFont(cgFont, out error);
                            if (!registered)
                            {
                                // If the error code is 105 then the font we are trying to register is already registered
                                // We will not report this as an error.
                                if (error.Code != 105)
                                    throw new ArgumentException("Error registering: " + Path.GetFileName(fileName));
                            }
                        }

                        return nativeFont.PostScriptName;
                    }
                    catch
                    {
                        // note: MS throw the same exception FileNotFoundException if the file exists but isn't a valid font file
                        throw new System.IO.FileNotFoundException (fileName);
                    }
                }
                catch (Exception)
                {
                    // note: MS throw the same exception FileNotFoundException if the file exists but isn't a valid font file
                    throw new System.IO.FileNotFoundException (fileName);

                }

            }
            return fileName;
        }
Esempio n. 13
0
        public Bitmap(int width, int height, PixelFormat format)
        {
            int bitsPerComponent, bytesPerRow;
            CGColorSpace colorSpace;
            CGBitmapFlags bitmapInfo;
            bool premultiplied = false;
            int bitsPerPixel = 0;

            // Don't forget to set the Image width and height for size.
            imageSize.Width = width;
            imageSize.Height = height;

            switch (format){
            case PixelFormat.Format32bppPArgb:
                premultiplied = true;
                colorSpace = CGColorSpace.CreateDeviceRGB ();
                bitsPerComponent = 8;
                bitsPerPixel = 32;
                bitmapInfo = CGBitmapFlags.PremultipliedFirst;
                break;
            case PixelFormat.Format32bppArgb:
                colorSpace = CGColorSpace.CreateDeviceRGB ();
                bitsPerComponent = 8;
                bitsPerPixel = 32;
                bitmapInfo = CGBitmapFlags.PremultipliedFirst;
                break;
            case PixelFormat.Format32bppRgb:
                colorSpace = CGColorSpace.CreateDeviceRGB ();
                bitsPerComponent = 8;
                bitsPerPixel = 32;
                bitmapInfo = CGBitmapFlags.None;
                break;
            default:
                throw new Exception ("Format not supported: " + format);
            }
            bytesPerRow = width * bitsPerPixel/bitsPerComponent;
            int size = bytesPerRow * height;

            bitmapBlock = Marshal.AllocHGlobal (size);
            var bitmap = new CGBitmapContext (bitmapBlock,
                                          width, height,
                                          bitsPerComponent,
                                          bytesPerRow,
                                          colorSpace,
                                          CGImageAlphaInfo.PremultipliedLast);
            // This works for now but we need to look into initializing the memory area itself
            // TODO: Look at what we should do if the image does not have alpha channel
            bitmap.ClearRect (new RectangleF (0,0,width,height));

            var provider = new CGDataProvider (bitmapBlock, size, true);
            NativeCGImage = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, null, false, CGColorRenderingIntent.Default);
        }
Esempio n. 14
0
		public static CGImage CreateMask (int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow, CGDataProvider provider, float [] decode, bool shouldInterpolate)
		{
			if (width < 0)
				throw new ArgumentException ("width");
			if (height < 0)
				throw new ArgumentException ("height");
			if (bitsPerPixel < 0)
				throw new ArgumentException ("bitsPerPixel");
			if (bytesPerRow < 0)
				throw new ArgumentException ("bytesPerRow");
			if (provider == null)
				throw new ArgumentNullException ("provider");

			var handle = CGImageMaskCreate (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, provider.Handle, decode, shouldInterpolate);
			if (handle == IntPtr.Zero)
				return null;

			return new CGImage (handle, true);
		}
Esempio n. 15
0
        public Bitmap(string filename)
        {
            if (filename == null)
                throw new ArgumentNullException ("Value can not be null");

            try
            {
                // Use Image IO
                dataProvider = new CGDataProvider(filename);
                if (dataProvider == null)
                    throw new FileNotFoundException ("File {0} not found.", filename);

                InitializeImageFrame (0);
            }
            catch (Exception exc)
            {
                throw new FileNotFoundException ("File {0} not found.", filename);
            }
        }
Esempio n. 16
0
 public CGPDFDocument(CGDataProvider provider)
 {
     if (provider == null)
         throw new ArgumentNullException ("provider");
     handle = CGPDFDocumentCreateWithProvider (provider.Handle);
 }
Esempio n. 17
0
        public static NSImage ToNSImage(BitmapEx bmpEx)
        {
            bmpEx.LockBits();
            long bufferLength = bmpEx.Width * bmpEx.Height;
            CGDataProvider provider = new CGDataProvider(bmpEx.Scan0, (int)bufferLength);
            int bitsPerComponent, bitsPerPixel, bytesPerRow = (int)bmpEx.Stride;
            CGColorSpace colorSpaceRef = CGColorSpace.CreateDeviceRGB();

            switch (bmpEx.BitDepth)
            {
                case ImageType.RGB16:
                    bitsPerComponent = 16;
                    bitsPerPixel = 48;
                    bufferLength *= 3;
                    break;
                case ImageType.RGBA16:
                    bitsPerComponent = 16;
                    bitsPerPixel = 64;
                    bufferLength *= 4;
                    break;
                case ImageType.RGB8:
                    bitsPerComponent = 8;
                    bitsPerPixel = 24;
                    bufferLength *= 3;
                    break;
                case ImageType.RGBA8:
                    bitsPerComponent = 8;
                    bitsPerPixel = 32;
                    bufferLength *= 4;
                    break;
                case ImageType.RGB32:
                    bitsPerComponent = 32;
                    bitsPerPixel = 96;
                    bufferLength *= 3;
                    break;
                case ImageType.RGBA32:
                    bitsPerComponent = 32;
                    bitsPerPixel = 128;
                    bufferLength *= 4;
                    break;
                case ImageType.RGB64:
                    bitsPerComponent = 64;
                    bitsPerPixel = 192;
                    bufferLength *= 3;
                    break;
                case ImageType.RGBA64:
                    bitsPerComponent = 64;
                    bitsPerPixel = 256;
                    bufferLength *= 4;
                    break;

                default:
                    throw new ArgumentException("Bitdepth not supported");
            }

            CGImage img = new CGImage((int)bmpEx.Width, (int)bmpEx.Height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, CGBitmapFlags.ByteOrderDefault, provider, null, true, CGColorRenderingIntent.Default);

            bmpEx.UnlockBits();

            return new NSImage(img, new SizeF(img.Width, img.Height));
        }
Esempio n. 18
0
        private void InitWithCGImage(CGImage image)
        {
            int	width, height;
            CGBitmapContext bitmap = null;
            bool hasAlpha;
            CGImageAlphaInfo alphaInfo;
            CGColorSpace colorSpace;
            int bitsPerComponent, bytesPerRow;
            CGBitmapFlags bitmapInfo;
            bool premultiplied = false;
            int bitsPerPixel = 0;

            if (image == null) {
                throw new ArgumentException (" image is invalid! " );
            }

            alphaInfo = image.AlphaInfo;
            hasAlpha = ((alphaInfo == CGImageAlphaInfo.PremultipliedLast) || (alphaInfo == CGImageAlphaInfo.PremultipliedFirst) || (alphaInfo == CGImageAlphaInfo.Last) || (alphaInfo == CGImageAlphaInfo.First) ? true : false);

            imageSize.Width = image.Width;
            imageSize.Height = image.Height;

            width = image.Width;
            height = image.Height;

            // Not sure yet if we need to keep the original image information
            // before we change it internally.  TODO look at what windows does
            // and follow that.
            bitmapInfo = image.BitmapInfo;
            bitsPerComponent = image.BitsPerComponent;
            bitsPerPixel = image.BitsPerPixel;
            bytesPerRow = width * bitsPerPixel/bitsPerComponent;
            int size = bytesPerRow * height;

            colorSpace = image.ColorSpace;

            // Right now internally we represent the images all the same
            // I left the call here just in case we find that this is not
            // possible.  Read the comments for non alpha images.
            if(colorSpace != null) {
                if( hasAlpha ) {
                    premultiplied = true;
                    colorSpace = CGColorSpace.CreateDeviceRGB ();
                    bitsPerComponent = 8;
                    bitsPerPixel = 32;
                    bitmapInfo = CGBitmapFlags.PremultipliedLast;
                }
                else
                {
                    // even for images without alpha we will internally
                    // represent them as RGB with alpha.  There were problems
                    // if we do not do it this way and creating a bitmap context.
                    // The images were not drawing correctly and tearing.  Also
                    // creating a Graphics to draw on was a nightmare.  This
                    // should probably be looked into or maybe it is ok and we
                    // can continue representing internally with this representation
                    premultiplied = true;
                    colorSpace = CGColorSpace.CreateDeviceRGB ();
                    bitsPerComponent = 8;
                    bitsPerPixel = 32;
                    bitmapInfo = CGBitmapFlags.NoneSkipLast;
                }
            } else {
                premultiplied = true;
                colorSpace = CGColorSpace.CreateDeviceRGB ();
                bitsPerComponent = 8;
                bitsPerPixel = 32;
                bitmapInfo = CGBitmapFlags.NoneSkipLast;
            }

            bytesPerRow = width * bitsPerPixel/bitsPerComponent;
            size = bytesPerRow * height;

            bitmapBlock = Marshal.AllocHGlobal (size);
            bitmap = new CGBitmapContext (bitmapBlock,
                                          width, height,
                                          bitsPerComponent,
                                          bytesPerRow,
                                          colorSpace,
                                          bitmapInfo);

            bitmap.ClearRect (new RectangleF (0,0,width,height));

            // We need to flip the Y axis to go from right handed to lefted handed coordinate system
            var transform = new CGAffineTransform(1, 0, 0, -1, 0, image.Height);
            bitmap.ConcatCTM(transform);

            bitmap.DrawImage(new RectangleF (0, 0, image.Width, image.Height), image);

            var provider = new CGDataProvider (bitmapBlock, size, true);
            NativeCGImage = new CGImage (width, height, bitsPerComponent,
                                         bitsPerPixel, bytesPerRow,
                                         colorSpace,
                                         bitmapInfo,
                                         provider, null, true, image.RenderingIntent);

            colorSpace.Dispose();
            bitmap.Dispose();
        }
Esempio n. 19
0
		public static CGImageSource FromDataProvider (CGDataProvider provider)
		{
			return FromDataProvider (provider, null);
		}
Esempio n. 20
0
        public Bitmap(Stream stream, bool useIcm)
        {
            if (stream == null)
                throw new ArgumentNullException ("Value can not be null");

            // false: stream is owned by user code
            //nativeObject = InitFromStream (stream);
            // TODO
            // Use Image IO
            byte[] buffer;
            using(var memoryStream = new MemoryStream())
            {
                stream.CopyTo(memoryStream);
                buffer = memoryStream.ToArray();
            }

            dataProvider = new CGDataProvider(buffer, 0, buffer.Length);

            InitializeImageFrame (0);
        }
Esempio n. 21
0
		public void UpdateDataProvider (CGDataProvider provider)
		{
			if (provider == null)
				throw new ArgumentNullException ("provider");
			CGImageSourceUpdateDataProvider (handle, provider.Handle);
		}
Esempio n. 22
0
        private void MakeSureWeHaveAnAlphaChannel()
        {
            // Initialize our prmultiplied tables.
            if (!ConversionHelpers.sTablesInitialized)
                ConversionHelpers.CalculateTables ();

            var alphaInfo = NativeCGImage.AlphaInfo;
            var hasAlpha = ((alphaInfo == CGImageAlphaInfo.PremultipliedLast)
                            || (alphaInfo == CGImageAlphaInfo.PremultipliedFirst)
                            || (alphaInfo == CGImageAlphaInfo.Last)
                            || (alphaInfo == CGImageAlphaInfo.First) ? true : false);

            if (cachedContext != null && cachedContext.Handle != IntPtr.Zero)
            {
                return;
            }

            // set our pixel format
            pixelFormat = PixelFormat.Format32bppArgb;
            // and mark the rawformat as from memory
            rawFormat = ImageFormat.MemoryBmp;

            //format = GetBestSupportedFormat (pixelFormat);
            cachedContext = CreateCompatibleBitmapContext (NativeCGImage.Width, NativeCGImage.Height, pixelFormat);

            // Fill our pixel data with the actual image information
            cachedContext.DrawImage (new RectangleF (0, 0, NativeCGImage.Width, NativeCGImage.Height), NativeCGImage);

            // Dispose of the prevous image that is allocated.
            NativeCGImage.Dispose ();

            // Get a reference to the pixel data
            bitmapBlock = cachedContext.Data;
            int size = cachedContext.BytesPerRow * cachedContext.Height;
            var provider = new CGDataProvider (cachedContext.Data, size, true);

            // Get the image from the bitmap context.
            //NativeCGImage = bitmapContext.ToImage ();
            CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
            NativeCGImage = new CGImage (cachedContext.Width, cachedContext.Height, cachedContext.BitsPerComponent,
                                         cachedContext.BitsPerPixel, cachedContext.BytesPerRow,
                                         colorSpace,
                                         cachedContext.AlphaInfo,
                                         provider, null, true, CGColorRenderingIntent.Default);
            colorSpace.Dispose ();
        }
		static internal CTFont CreateFont (string familyName, float emSize, FontStyle style,
		                        byte gdiCharSet, bool  gdiVerticalFont )
		{
			if (emSize <= 0)
				throw new ArgumentException("emSize is less than or equal to 0, evaluates to infinity, or is not a valid number.","emSize");

			CTFont nativeFont;
			// convert to 96 Dpi to be consistent with Windows
			var dpiSize = emSize * dpiScale;

			var ext = System.IO.Path.GetExtension(familyName);
			if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf")
			{
				var fontName = familyName.Substring (0, familyName.Length - ext.Length);
				var path = CCApplication.SharedApplication.Game.Content.RootDirectory + Path.DirectorySeparatorChar + fontName;
				var pathForResource = NSBundle.MainBundle.PathForResource (path, ext.Substring(1));

				try {
					var dataProvider = new CGDataProvider (pathForResource);
					var cgFont = CGFont.CreateFromProvider (dataProvider);

					try {
						nativeFont = new CTFont(cgFont, dpiSize, null);
					}
					catch
					{
						nativeFont = new CTFont("Helvetica",dpiSize);
					}
				}
				catch (Exception)
				{
					try {
						nativeFont = new CTFont(Path.GetFileNameWithoutExtension(familyName),dpiSize);
					}
					catch
					{
						nativeFont = new CTFont("Helvetica",dpiSize);
					}	
					CCLog.Log (string.Format ("Could not load font: {0} so will use default {1}.", familyName, nativeFont.DisplayName));

				}
			}
			else
			{
				try {
					nativeFont = new CTFont(familyName,dpiSize);
				}
				catch
				{
					nativeFont = new CTFont("Helvetica",dpiSize);
				}
			}


			CTFontSymbolicTraits tMask = CTFontSymbolicTraits.None;

			if ((style & FontStyle.Bold) == FontStyle.Bold)
				tMask |= CTFontSymbolicTraits.Bold;
			if ((style & FontStyle.Italic) == FontStyle.Italic)
				tMask |= CTFontSymbolicTraits.Italic;
			strikeThrough = (style & FontStyle.Strikeout) == FontStyle.Strikeout;
			underLine = (style & FontStyle.Underline) == FontStyle.Underline;

			var nativeFont2 = nativeFont.WithSymbolicTraits(dpiSize,tMask,tMask);

			if (nativeFont2 != null)
				nativeFont = nativeFont2;

			return nativeFont;
		}
Esempio n. 24
0
        public Bitmap(int width, int height, PixelFormat format)
        {
            imageTransform = new CGAffineTransform(1, 0, 0, -1, 0, height);

            int bitsPerComponent, bytesPerRow;
            CGColorSpace colorSpace;
            CGBitmapFlags bitmapInfo;
            bool premultiplied = false;
            int bitsPerPixel = 0;

            pixelFormat = format;

            // Don't forget to set the Image width and height for size.
            imageSize.Width = width;
            imageSize.Height = height;

            switch (format){
            case PixelFormat.Format32bppPArgb:
            case PixelFormat.DontCare:
                premultiplied = true;
                colorSpace = CGColorSpace.CreateDeviceRGB ();
                bitsPerComponent = 8;
                bitsPerPixel = 32;
                bitmapInfo = CGBitmapFlags.PremultipliedFirst;
                break;
            case PixelFormat.Format32bppArgb:
                colorSpace = CGColorSpace.CreateDeviceRGB ();
                bitsPerComponent = 8;
                bitsPerPixel = 32;
                bitmapInfo = CGBitmapFlags.PremultipliedFirst;
                break;
            case PixelFormat.Format32bppRgb:
                colorSpace = CGColorSpace.CreateDeviceRGB ();
                bitsPerComponent = 8;
                bitsPerPixel = 32;
                bitmapInfo = CGBitmapFlags.NoneSkipLast;
                break;
            case PixelFormat.Format24bppRgb:
                colorSpace = CGColorSpace.CreateDeviceRGB ();
                bitsPerComponent = 8;
                bitsPerPixel = 32;
                bitmapInfo = CGBitmapFlags.NoneSkipLast;
                break;
            default:
                throw new Exception ("Format not supported: " + format);
            }
            bytesPerRow = width * bitsPerPixel/bitsPerComponent;
            int size = bytesPerRow * height;

            bitmapBlock = Marshal.AllocHGlobal (size);
            var bitmap = new CGBitmapContext (bitmapBlock,
                                          width, height,
                                          bitsPerComponent,
                                          bytesPerRow,
                                          colorSpace,
                                          bitmapInfo);
            // This works for now but we need to look into initializing the memory area itself
            // TODO: Look at what we should do if the image does not have alpha channel
            bitmap.ClearRect (new RectangleF (0,0,width,height));

            var provider = new CGDataProvider (bitmapBlock, size, true);
            NativeCGImage = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, null, false, CGColorRenderingIntent.Default);

            dpiWidth = dpiHeight = ConversionHelpers.MS_DPI;
            physicalDimension.Width = width;
            physicalDimension.Height = height;

            // The physical size may be off on certain implementations.  For instance the dpiWidth and dpiHeight
            // are read using integers in core graphics but in windows it is a float.
            // For example:
            // coregraphics dpiWidth = 24 as integer
            // windows dpiWidth = 24.999935 as float
            // this gives a few pixels difference when calculating the physical size.
            // 256 * 96 / 24 = 1024
            // 256 * 96 / 24.999935 = 983.04
            //
            // https://bugzilla.xamarin.com/show_bug.cgi?id=14365
            // PR: https://github.com/mono/maccore/pull/57
            //

            physicalSize = new SizeF (physicalDimension.Width, physicalDimension.Height);
            physicalSize.Width *= ConversionHelpers.MS_DPI / dpiWidth;
            physicalSize.Height *= ConversionHelpers.MS_DPI / dpiHeight;

            rawFormat = ImageFormat.MemoryBmp;
            pixelFormat = format;
        }
Esempio n. 25
0
        public static CGImage CreateMask(nint width, nint height, nint bitsPerComponent, nint bitsPerPixel, nint bytesPerRow, CGDataProvider provider, nfloat [] decode, bool shouldInterpolate)
        {
            if (width < 0)
            {
                throw new ArgumentException("width");
            }
            if (height < 0)
            {
                throw new ArgumentException("height");
            }
            if (bitsPerPixel < 0)
            {
                throw new ArgumentException("bitsPerPixel");
            }
            if (bytesPerRow < 0)
            {
                throw new ArgumentException("bytesPerRow");
            }
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }

            var handle = CGImageMaskCreate(new IntPtr(width), new IntPtr(height), new IntPtr(bitsPerComponent),
                                           new IntPtr(bitsPerPixel), new IntPtr(bytesPerRow), provider.Handle, decode, shouldInterpolate);

            if (handle == IntPtr.Zero)
            {
                return(null);
            }

            return(new CGImage(handle, true));
        }
Esempio n. 26
0
		public CGImage (int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow,
				CGColorSpace colorSpace, CGImageAlphaInfo alphaInfo, CGDataProvider provider,
				float [] decode, bool shouldInterpolate, CGColorRenderingIntent intent)
		{
			if (colorSpace == null)
				throw new ArgumentNullException ("colorSpace");
			if (width < 0)
				throw new ArgumentException ("width");
			if (height < 0)
				throw new ArgumentException ("height");
			if (bitsPerPixel < 0)
				throw new ArgumentException ("bitsPerPixel");
			if (bitsPerComponent < 0)
				throw new ArgumentException ("bitsPerComponent");
			if (bytesPerRow < 0)
				throw new ArgumentException ("bytesPerRow");
			if (provider == null)
				throw new ArgumentNullException ("provider");

			handle = CGImageCreate (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow,
						colorSpace.Handle, (CGBitmapFlags) alphaInfo, provider.Handle,
						decode,
						shouldInterpolate, intent);
		}
Esempio n. 27
-6
        public CGImage(int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow,
                       CGColorSpace colorSpace, CGImageAlphaInfo alphaInfo, CGDataProvider provider,
                       float [] decode, bool shouldInterpolate, CGColorRenderingIntent intent)
        {
            if (colorSpace == null)
            {
                throw new ArgumentNullException("colorSpace");
            }
            if (width < 0)
            {
                throw new ArgumentException("width");
            }
            if (height < 0)
            {
                throw new ArgumentException("height");
            }
            if (bitsPerPixel < 0)
            {
                throw new ArgumentException("bitsPerPixel");
            }
            if (bitsPerComponent < 0)
            {
                throw new ArgumentException("bitsPerComponent");
            }
            if (bytesPerRow < 0)
            {
                throw new ArgumentException("bytesPerRow");
            }
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }

            handle = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow,
                                   colorSpace.Handle, (CGBitmapFlags)alphaInfo, provider.Handle,
                                   decode,
                                   shouldInterpolate, intent);
        }