Inheritance: INativeObject, IDisposable
Esempio n. 1
0
		// Angle For The Quad     ( NEW )
		public Scene () : base()
		{
			GL.GenTextures (1, textures);
			GL.BindTexture (TextureTarget.Texture2D, textures [0]);
			image = CGImage.FromPNG (new CGDataProvider ("/tmp/Mono.png"), null, true, CGColorRenderingIntent.Default);
			Console.WriteLine (image);
		}
Esempio n. 2
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. 3
0
		private void InitWithCGImage (CGImage image, All filter)
		{
			int	width, height, i;
			CGContext context = null;
			IntPtr data;
			CGColorSpace colorSpace;
			IntPtr tempData;
			bool hasAlpha;
			CGImageAlphaInfo info;
			CGAffineTransform transform;
			Size imageSize;
			SurfaceFormat pixelFormat;
			bool sizeToFit = false;

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

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

			if (image.ColorSpace != null) {
				if (hasAlpha) {
					pixelFormat = SurfaceFormat.Rgba32;
				} else {
					pixelFormat = SurfaceFormat.Rgb32;
				}
			} else {	
				pixelFormat = SurfaceFormat.Alpha8;
			}

			imageSize = new Size (image.Width,image.Height);
			transform = CGAffineTransform.MakeIdentity ();
			width = imageSize.Width;

			if ((width != 1) && ((width & (width - 1)) != 0)) {
				i = 1;
				while ((sizeToFit ? 2 * i : i) < width)
					i *= 2;
				width = i;
			}
			height = imageSize.Height;
			if ((height != 1) && ((height & (height - 1)) != 0)) {
				i = 1;
				while ((sizeToFit ? 2 * i : i) < height)
					i *= 2;
				height = i;
			}
			// TODO: kMaxTextureSize = 1024
			while ((width > 1024) || (height > 1024)) {
				width /= 2;
				height /= 2;
				transform = CGAffineTransform.MakeScale (0.5f, 0.5f);
				imageSize.Width /= 2;
				imageSize.Height /= 2;
			}

			switch (pixelFormat) {		
			case SurfaceFormat.Rgba32:
				colorSpace = CGColorSpace.CreateDeviceRGB ();
				data = Marshal.AllocHGlobal (height * width * 4);
				context = new CGBitmapContext (data, width, height, 8, 4 * width, colorSpace,CGImageAlphaInfo.PremultipliedLast);
				colorSpace.Dispose ();
				break;
			case SurfaceFormat.Rgb32:
				colorSpace = CGColorSpace.CreateDeviceRGB ();
				data = Marshal.AllocHGlobal (height * width * 4);
				context = new CGBitmapContext (data, width, height, 8, 4 * width, colorSpace, CGImageAlphaInfo.NoneSkipLast);
				colorSpace.Dispose ();
				break;					
			case SurfaceFormat.Alpha8:
				data = Marshal.AllocHGlobal (height * width);
				context = new CGBitmapContext (data, width, height, 8, width, null, CGImageAlphaInfo.Only);
				break;				
			default:
				throw new NotSupportedException ("Invalid pixel format"); 
			}

			context.ClearRect (new RectangleF (0,0,width,height));
			context.TranslateCTM (0, height - imageSize.Height);

			if (!transform.IsIdentity) {
				context.ConcatCTM (transform);
			}

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

			//Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
			if (pixelFormat == SurfaceFormat.Rgb32) {
				tempData = Marshal.AllocHGlobal (height * width * 2);

				int d32;
				short d16;
				int inPixel32Count = 0, outPixel16Count=0;
				for (i = 0; i < width * height; ++i, inPixel32Count+=sizeof(int)) {
					d32 = Marshal.ReadInt32 (data, inPixel32Count);
					short R = (short)((((d32 >> 0) & 0xFF) >> 3) << 11);
					short G = (short)((((d32 >> 8) & 0xFF) >> 2) << 5);
					short B = (short)((((d32 >> 16) & 0xFF) >> 3) << 0);
					d16 = (short)(R | G | B);
					Marshal.WriteInt16 (tempData, outPixel16Count, d16);
					outPixel16Count += sizeof(short);
				}
				Marshal.FreeHGlobal (data);
				data = tempData;			
			}

			InitWithData (data, pixelFormat, width, height, imageSize, filter);

			context.Dispose ();
			Marshal.FreeHGlobal (data);	
		}
Esempio n. 4
0
		public CGImage WithMask (CGImage mask)
		{
			return new CGImage (CGImageCreateWithMask (handle, mask.handle), true);
		}
Esempio n. 5
0
        private void InitWithCGImage(CGImage image, All filter)
        {
            int	width, height, i;
            CGContext context = null;
            IntPtr data;
            CGColorSpace colorSpace;
            IntPtr tempData;
            bool hasAlpha;
            CGImageAlphaInfo info;
            CGAffineTransform transform;
            Size imageSize;
            SurfaceFormat pixelFormat;
            //bool sizeToFit = false;

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

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

            if (image.ColorSpace != null) {
                if (hasAlpha) {
                    pixelFormat = SurfaceFormat.Color;
                } else {
                    pixelFormat = SurfaceFormat.Color;
                }
            } else {
                pixelFormat = SurfaceFormat.Alpha8;
            }

            imageSize = new Size (image.Width,image.Height);
            transform = CGAffineTransform.MakeIdentity ();
            width = imageSize.Width;

            // Take out the width and height adjustments for power of 2
            //  If not then GetData and SetData is messed up.

            // The Mac opengl version supports non power of 2 textures
            // so we do not have to make them so
            //			if ((width != 1) && ((width & (width - 1)) != 0)) {
            //				i = 1;
            //				while ((sizeToFit ? 2 * i : i) < width)
            //					i *= 2;
            //				width = i;
            //			}

            height = imageSize.Height;

            // The Mac opengl version supports non power of 2 textures
            // so we do not have to make them so
            //			if ((height != 1) && ((height & (height - 1)) != 0)) {
            //				i = 1;
            //				while ((sizeToFit ? 2 * i : i) < height)
            //					i *= 2;
            //				height = i;
            //			}
            // TODO: kMaxTextureSize = 1024
            //			while ((width > 1024) || (height > 1024)) {
            //				width /= 2;
            //				height /= 2;
            //				transform = CGAffineTransform.MakeScale (0.5f, 0.5f);
            //				imageSize.Width /= 2;
            //				imageSize.Height /= 2;
            //			}

            float size = Math.Max(width,height);
            if(size > 1024)
            {
                float ratio = 1024 / size;
                width = (int)(width * ratio);
                height = (int)(height * ratio);
                transform = CGAffineTransform.MakeScale(ratio, ratio);
                imageSize.Width = (int)(imageSize.Width * ratio);
                imageSize.Height = (int)(imageSize.Height * ratio);;
            }

            switch (pixelFormat) {
            case SurfaceFormat.Color:
                colorSpace = CGColorSpace.CreateDeviceRGB ();
                data = Marshal.AllocHGlobal (height * width * 4);
                context = new CGBitmapContext (data, width, height, 8, 4 * width, colorSpace,CGImageAlphaInfo.PremultipliedLast);
                colorSpace.Dispose ();
                break;
            case SurfaceFormat.Alpha8:
                data = Marshal.AllocHGlobal (height * width);
                context = new CGBitmapContext (data, width, height, 8, width, null, CGImageAlphaInfo.Only);
                break;
            default:
                throw new NotSupportedException ("Invalid pixel format");
            }

            context.ClearRect (new RectangleF (0,0,width,height));
            context.TranslateCTM (0, height - imageSize.Height);

            if (!transform.IsIdentity) {
                context.ConcatCTM (transform);
            }

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

            //Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
            /*
            if(pixelFormat == SurfaceFormat.Rgb32) {
                tempData = Marshal.AllocHGlobal(height * width * 2);

                int d32;
                short d16;
                int inPixel32Count=0,outPixel16Count=0;
                for(i = 0; i < width * height; ++i, inPixel32Count+=sizeof(int))
                {
                    d32 = Marshal.ReadInt32(data,inPixel32Count);
                    short R = (short)((((d32 >> 0) & 0xFF) >> 3) << 11);
                    short G = (short)((((d32 >> 8) & 0xFF) >> 2) << 5);
                    short B = (short)((((d32 >> 16) & 0xFF) >> 3) << 0);
                    d16 = (short)  (R | G | B);
                    Marshal.WriteInt16(tempData,outPixel16Count,d16);
                    outPixel16Count += sizeof(short);
                }
                Marshal.FreeHGlobal(data);
                data = tempData;
            }
            */

            InitWithData (data, pixelFormat, width, height, imageSize, filter);

            context.Dispose ();
            Marshal.FreeHGlobal (data);
        }
Esempio n. 6
0
 public void DrawTiledImage(RectangleF rect, CGImage image)
 {
     CGContextDrawTiledImage(handle, rect, image.handle);
 }
 public UIKitImage(CGImage image)
 {
     Image = image;
 }
Esempio n. 8
0
		public void ClipToMask (RectangleF rect, CGImage mask)
		{
			CGContextClipToMask (handle, rect, mask.handle);
		}
Esempio n. 9
0
 public void DrawImage(CGRect rect, CGImage image)
 {
     CGContextDrawImage(handle, rect, image.handle);
 }
Esempio n. 10
0
 public void ClipToMask(CGRect rect, CGImage mask)
 {
     CGContextClipToMask(handle, rect, mask.handle);
 }
        private void DrawImage(RectangleF rect, CGImage image, CGAffineTransform transform)
        {
            var trans = transform;
            // Do our translation on the image transform
            trans.Translate (rect.X, rect.Height - image.Height + rect.Y);

            // The translation is already taken care of in the transform
            rect.Y = 0;
            rect.X = 0;

            // Apply our transform to the context
            context.ConcatCTM (trans);

            // we are getting an error somewhere and not sure where
            // I think the image bitmapBlock is being corrupted somewhere
            try {
                context.DrawImage(rect, image);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }

            // Now we revert our image transform from the context
            var revert = CGAffineTransform.CGAffineTransformInvert (trans);
            context.ConcatCTM (revert);
        }
Esempio n. 12
0
		void Events_Tick(object sender, TickEventArgs e)
		{
			timeElapsed += e.SecondsElapsed;
			
			while (timeElapsed > 1.0 / file.Header.Fps)
			{
				lock (((ICollection)frameQueue).SyncRoot) {
					if (frameQueue.Count <= 0)
						return;
				}
					
				timeElapsed -= (float)(1.0f / file.Header.Fps);
				byte[] argbData = frameQueue.Dequeue();
								
				currentFrame = GuiUtil.CreateImage (argbData, (ushort)Width, (ushort)Height, 32, Width * 4);
				
				EmitFrameReady ();
				
				if (frameQueue.Count < (buffered_frames / 2) + 1)
					waitEvent.Set ();
			}
		}
Esempio n. 13
0
		public static CIImage FromCGImage (CGImage image, CGColorSpace colorSpace)
		{
			if (colorSpace == null)
				throw new ArgumentNullException ("colorSpace");
			
			using (var arr = NSArray.FromIntPtrs (new IntPtr [] { colorSpace.Handle })){
				using (var keys = NSArray.FromIntPtrs (new IntPtr [] { CIImageColorSpaceKey.Handle } )){
					using (var dict = NSDictionary.FromObjectsAndKeysInternal (arr, keys)){
						return FromCGImage (image, dict);
					}
				}
			}
		}
Esempio n. 14
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);
        }
		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;
        }
Esempio n. 16
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. 17
0
		public void AddImage (CGImage image, NSDictionary properties)
		{
			if (image == null)
				throw new ArgumentNullException ("image");
			
			CGImageDestinationAddImage (Handle, image.Handle, properties == null ? IntPtr.Zero : properties.Handle);
		}
Esempio n. 18
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 ();
        }
Esempio n. 19
0
		public void DrawTiledImage (RectangleF rect, CGImage image)
		{
			CGContextDrawTiledImage (handle, rect, image.handle);
		}
Esempio n. 20
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. 21
0
 public void ClipToMask(RectangleF rect, CGImage mask)
 {
     CGContextClipToMask(handle, rect, mask.handle);
 }
Esempio n. 22
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. 23
0
 public CGImage WithMask(CGImage mask)
 {
     return(new CGImage(CGImageCreateWithMask(handle, mask.handle), true));
 }
Esempio n. 24
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. 25
0
 public ESTexture2D(CGImage cgImage, All filter)
 {
     InitWithCGImage (cgImage, filter);
 }
Esempio n. 26
0
        private void DrawNoise(float opacity)
        {
            if (_noiseImageRef == null)
            {
                const int width = 124;
                const int height = width;
                const int size = width * height;

                var rgba = new byte[size];
                var random = new Random(120);
                for (var i = 0; i < size; i++)
                {
                    rgba[i] = (byte)(random.Next() % 256);
                }
                var colorSpace = CGColorSpace.CreateDeviceGray();
                var bitmapContext = new CGBitmapContext(rgba, width, height, 8, width, colorSpace, CGImageAlphaInfo.None);
                _noiseImageRef = bitmapContext.ToImage();
            }

            var context = NSGraphicsContext.CurrentContext.GraphicsPort;
            NSGraphicsContext.CurrentContext.SaveGraphicsState();
            context.SetAlpha(opacity);
            context.SetBlendMode(CGBlendMode.Screen);
            var scaleFactor = Window.BackingScaleFactor;
            context.ScaleCTM(1f / scaleFactor, 1f / scaleFactor);
            var imageRect = new RectangleF(PointF.Empty, new SizeF(_noiseImageRef.Width, _noiseImageRef.Height));
            context.DrawTiledImage(imageRect, _noiseImageRef);
            NSGraphicsContext.CurrentContext.RestoreGraphicsState();
        }