示例#1
0
        private UIImage ConvertToGrayScale(UIImage image)
        {
            CGRect imageRect = new CGRect(0, 0, image.Size.Width, image.Size.Height);

            using (var colorSpace = CGColorSpace.CreateDeviceGray())
                using (var context = new CGBitmapContext(IntPtr.Zero,
                                                         (int)image.Size.Width,
                                                         (int)image.Size.Height, 8, 0, colorSpace, CGImageAlphaInfo.None))
                {
                    context.DrawImage(imageRect, image.CGImage);

                    using (var imageRef = context.ToImage())
                        return(new UIImage(imageRef));
                }
        }
示例#2
0
        public void Nullable()
        {
            // either a null CGColorSpace or a null CGFloat* array will return nil, i.e. not a valid instance
            using (var cs = CGColorSpace.CreateDeviceGray())
                Assert.That(CGGradientCreateWithColorComponents(cs.Handle, null, null, 0), Is.EqualTo(IntPtr.Zero), "CGGradientCreateWithColorComponents-1");
            Assert.That(CGGradientCreateWithColorComponents(IntPtr.Zero, new nfloat [3] {
                0f, 1f, 0.5f
            }, null, 0), Is.EqualTo(IntPtr.Zero), "CGGradientCreateWithColorComponents-2");

            // a null CFArray won't return a valid instance
            using (var cs = CGColorSpace.CreateDeviceGray())
                Assert.That(CGGradientCreateWithColors(cs.Handle, IntPtr.Zero, null), Is.EqualTo(IntPtr.Zero), "CGGradientCreateWithColors-1");

            // a null CGColorSpace can return a valid instance
            using (var a = NSArray.FromNSObjects(array))
                Assert.That(CGGradientCreateWithColors(IntPtr.Zero, a.Handle, null), Is.Not.EqualTo(IntPtr.Zero), "CGGradientCreateWithColors-2");
        }
示例#3
0
			public unsafe override void Draw (RectangleF rect)
			{
				var start = new PointF (rect.Left, rect.Bottom);
				var end = new PointF (rect.Left, rect.Top);

				var domain = new nfloat[] {0f, 1f};
				var range = new nfloat[] {0f, 1f, 0f, 1f};
				using (var context = UIGraphics.GetCurrentContext ())
				using (var rgb = CGColorSpace.CreateDeviceGray())
				using (var shadingFunction = new CGFunction(domain, range, Shading))
				using (var shading = CGShading.CreateAxial (rgb, start, end, shadingFunction, true, false))
				{
					context.DrawShading (shading);
				}

				base.Draw (rect);
			}
示例#4
0
        public CGBitmapContext GetViewContext()
        {
            // our network takes in only grayscale images as input
            var colorSpace = CGColorSpace.CreateDeviceGray();
            // we have 3 channels no alpha value put in the network
            var bitmapInfo = CGImageAlphaInfo.None;

            // this is where our view pixel data will go in once we make the render call
            var context = new CGBitmapContext(null, 28, 28, 8, 28, colorSpace, bitmapInfo);

            // scale and translate so we have the full digit and in MNIST standard size 28x28
            context.TranslateCTM(0, 28);
            context.ScaleCTM(28 / Frame.Size.Width, -28 / Frame.Size.Height);

            // put view pixel data in context
            Layer.RenderInContext(context);

            return(context);
        }
示例#5
0
        /*public unsafe Int32* BaseAddress { get { unsafe { return _buffer_ptr; } } }*/
        #endregion

        #region Constructor
        public RawBitmap(int width, int height)
        {
            _buffer = Marshal.AllocHGlobal(width * height);
            unsafe {
                _buffer_ptr = (byte *)((void *)_buffer);
                //_buffer_ptr = (Int32*)((void*)_buffer);
            }
            var colorSpace = CGColorSpace.CreateDeviceGray();

            _context = new CGBitmapContext(
                _buffer, width, height,
                8, width,
                colorSpace,
                CGImageAlphaInfo.None);
            colorSpace.Dispose();
            // lowest possible quality for speed
            _context.InterpolationQuality = CGInterpolationQuality.None;
            _context.SetAllowsAntialiasing(false);
        }
示例#6
0
        UIImage CreateNonInterpolatedUIImageFormCIImage(CIImage image, nfloat size)
        {
            CGRect extent = image.Extent;
            float  scale  = (float)Math.Min(size / extent.Width, size / extent.Height);
            // 创建bitmap;
            int          width      = (int)(extent.Width * scale);
            int          height     = (int)(extent.Height * scale);
            CGColorSpace colorSpace = CGColorSpace.CreateDeviceGray();
//			var rawData = Marshal.AllocHGlobal(height * width * 4);
            CGBitmapContext bitmapRef   = new CGBitmapContext(null, width, height, 8, 0, colorSpace, CGImageAlphaInfo.None);
            CIContext       context     = CIContext.FromOptions(null);
            CGImage         bitmapImage = context.CreateCGImage(image, extent);

            bitmapRef.InterpolationQuality = CGInterpolationQuality.None;
            bitmapRef.ScaleCTM(scale, scale);
            bitmapRef.DrawImage(extent, bitmapImage);
            // 保存bitmap到图片
            CGImage scaledImage = bitmapRef.ToImage();

            return(new UIImage(scaledImage));
        }
示例#7
0
        public void ColorSpace()
        {
            TestRuntime.AssertSystemVersion(ApplePlatform.MacOSX, 10, 9, throwIfOtherPlatform: false);
            TestRuntime.AssertSystemVersion(ApplePlatform.iOS, 7, 0, throwIfOtherPlatform: false);

            using (var f = new CIColorCubeWithColorSpace()) {
                Assert.Null(f.ColorSpace, "ColorSpace/default");
                using (var cs = CGColorSpace.CreateDeviceGray()) {
                    f.ColorSpace = cs;
                    var rc = CFGetRetainCount(cs.Handle);
                    for (int i = 0; i < 5; i++)
                    {
                        using (var fcs = f.ColorSpace)
                            Assert.NotNull(fcs, i.ToString());
                    }
                    Assert.That(CFGetRetainCount(cs.Handle), Is.EqualTo(rc), "RetainCount");
                    f.ColorSpace = null;
                }
                Assert.Null(f.ColorSpace, "ColorSpace/reset-null");
            }
        }
示例#8
0
        public void ColorSpace()
        {
            if (!TestRuntime.CheckSystemAndSDKVersion(7, 0))
            {
                Assert.Ignore("Ignoring ColorSpace test: CIColorCubeWithColorSpace requires iOS7+");
            }

            using (var f = new CIColorCubeWithColorSpace()) {
                Assert.Null(f.ColorSpace, "ColorSpace/default");
                using (var cs = CGColorSpace.CreateDeviceGray()) {
                    f.ColorSpace = cs;
                    var rc = CFGetRetainCount(cs.Handle);
                    for (int i = 0; i < 5; i++)
                    {
                        Assert.NotNull(f.ColorSpace, i.ToString());
                    }
                    Assert.That(CFGetRetainCount(cs.Handle), Is.EqualTo(rc), "RetainCount");
                    f.ColorSpace = null;
                }
                Assert.Null(f.ColorSpace, "ColorSpace/reset-null");
            }
        }
示例#9
0
        public void CreateDeviceGray()
        {
            using (var cs = CGColorSpace.CreateDeviceGray()) {
                Assert.That(cs.Components, Is.EqualTo((nint)1), "1");
                Assert.That(cs.Model, Is.EqualTo(CGColorSpaceModel.Monochrome), "Monochrome");
                Assert.Null(cs.GetBaseColorSpace(), "GetBaseColorSpace");
                // not indexed so no color table
                Assert.That(cs.GetColorTable().Length, Is.EqualTo(0), "GetColorTable");
#if NET
                Assert.Null(cs.GetIccProfile(), "GetIccProfile");
#else
                Assert.Null(cs.GetICCProfile(), "GetICCProfile");
#endif
                if (TestRuntime.CheckXcodeVersion(8, 0))
                {
                    // kCGColorSpaceDeviceGray is not a public constant, e.g. from CGColorSpaceNames.*
                    Assert.That(cs.Name, Is.EqualTo("kCGColorSpaceDeviceGray"), "Name");
                    Assert.False(cs.IsWideGamutRgb, "IsWideGamutRgb");
                    Assert.True(cs.SupportsOutput, "SupportsOutput");
                    Assert.Null(cs.GetIccData(), "GetIccData");
                }
            }
        }
        internal override pTexture CreateText(string text, float size, OpenTK.Vector2 restrictBounds, OpenTK.Graphics.Color4 Color4, bool shadow, bool bold, bool underline, TextAlignment alignment, bool forceAa, out OpenTK.Vector2 measured, OpenTK.Graphics.Color4 background, OpenTK.Graphics.Color4 border, int borderWidth, bool measureOnly, string fontFace)
        {
            //UIFont font = bold ? UIFont.FromName("GillSans-Bold", size) : UIFont.FromName("GillSans",size);
            //UIFont font = bold ? UIFont.BoldSystemFontOfSize(size) : UIFont.SystemFontOfSize(size);
            UIFont font = UIFont.FromName(bold ? "Futura-CondensedExtraBold" : "Futura-Medium",size);

            CGSize actualSize = CGSize.Empty;

            // Render the text to a UILabel to calculate sizing
            // and line-wrapping, and then copy the pixels to our texture buffer.
            UILabel textLabel = new UILabel();
            textLabel.Font = font;
            textLabel.BackgroundColor = UIColor.Clear;
            textLabel.TextColor = UIColor.White;
            textLabel.LineBreakMode = UILineBreakMode.WordWrap;
            textLabel.Lines = 0; // Needed for multiple lines
            textLabel.Text = text;

            textLabel.TextAlignment = UITextAlignment.Left;
            switch (alignment) {
            case TextAlignment.Centre:
                textLabel.TextAlignment = UITextAlignment.Center;
                break;
            case TextAlignment.Right:
                textLabel.TextAlignment = UITextAlignment.Right;
                break;
            }

            if (restrictBounds == Vector2.Zero)
            {
                textLabel.SizeToFit();
                actualSize = textLabel.Frame.Size;

                restrictBounds = new Vector2((float)actualSize.Width, (float)actualSize.Height);
            }
            else if (restrictBounds.Y == 0)
            {
                SizeF boundsSize = new SizeF (restrictBounds.X, GameBase.NativeSize.Height);
                actualSize = textLabel.SizeThatFits(boundsSize);
                textLabel.Frame = new CGRect(CGPoint.Empty, actualSize);

                restrictBounds = new Vector2((float)actualSize.Width, (float)actualSize.Height);
            }

            int width = TextureGl.GetPotDimension((int)restrictBounds.X);
            int height = TextureGl.GetPotDimension((int)restrictBounds.Y);

            IntPtr data = Marshal.AllocHGlobal(width * height);
            unsafe {
                byte* bytes = (byte*)data;
                for (int i = width * height - 1; i >= 0; i--) bytes[i] = 0;
            }

            using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceGray())
            using (CGBitmapContext context = new CGBitmapContext(data, width, height, 8, width, colorSpace,CGImageAlphaInfo.None))
            {
                context.TranslateCTM(0, height);
                context.ScaleCTM(1, -1);

                UIGraphics.PushContext(context);

                textLabel.SetNeedsDisplay();
                textLabel.Layer.DrawInContext (context);
                
                UIGraphics.PopContext();

                measured = new Vector2((float)actualSize.Width, (float)actualSize.Height);

    			SpriteManager.TexturesEnabled = true;

                TextureGl gl = new TextureGl(width, height);
                gl.SetData(data, 0, All.Alpha);

                Marshal.FreeHGlobal(data);

                return new pTexture(gl, (int)actualSize.Width, (int)actualSize.Height);
            }
        }
示例#11
0
 public static CGColor CreateDeviceGrayColor(float w, float a)
 {
     using (var gray = CGColorSpace.CreateDeviceGray()) {
         return(new CGColor(gray, new nfloat[] { w, a }));
     }
 }
示例#12
0
        public static UIImage AddImageReflection(UIImage image, float reflectionFraction)
        {
            int reflectionHeight = (int)(image.Size.Height * reflectionFraction);

            // Create a 2 bit CGImage containing a gradient that will be used for masking the
            // main view content to create the 'fade' of the reflection.  The CGImageCreateWithMask
            // function will stretch the bitmap image as required, so we can create a 1 pixel wide gradient


            // gradient is always black and white and the mask must be in the gray colorspace
            var colorSpace = CGColorSpace.CreateDeviceGray();

            // Creat the bitmap context
            var gradientBitmapContext = new CGBitmapContext(IntPtr.Zero, 1, reflectionHeight, 8, 0, colorSpace, CGImageAlphaInfo.None);

            // define the start and end grayscale values (with the alpha, even though
            // our bitmap context doesn't support alpha the gradien requires it)
            float [] colors = { 0, 1, 1, 1 };

            // Create the CGGradient and then release the gray color space
            var grayScaleGradient = new CGGradient(colorSpace, colors, null);

            colorSpace.Dispose();

            // create the start and end points for the gradient vector (straight down)
            var gradientStartPoint = new PointF(0, reflectionHeight);
            var gradientEndPoint   = PointF.Empty;

            // draw the gradient into the gray bitmap context
            gradientBitmapContext.DrawLinearGradient(grayScaleGradient, gradientStartPoint,
                                                     gradientEndPoint, CGGradientDrawingOptions.DrawsAfterEndLocation);
            grayScaleGradient.Dispose();

            // Add a black fill with 50% opactiy
            gradientBitmapContext.SetGrayFillColor(0, 0.5f);
            gradientBitmapContext.FillRect(new RectangleF(0, 0, 1, reflectionHeight));

            // conver the context into a CGImage and release the context
            var gradientImageMask = gradientBitmapContext.ToImage();

            gradientBitmapContext.Dispose();

            // create an image by masking the bitmap of the mainView content with the gradient view
            // then release the pre-masked content bitmap and the gradient bitmap
            var reflectionImage = image.CGImage.WithMask(gradientImageMask);

            gradientImageMask.Dispose();

            var size = new SizeF(image.Size.Width, image.Size.Height + reflectionHeight);

            UIGraphics.BeginImageContext(size);
            image.Draw(PointF.Empty);
            var context = UIGraphics.GetCurrentContext();

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

            var result = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();
            reflectionImage.Dispose();

            return(result);
        }