void GetImagaDataFromPath(string path)
        {
            int        width, height;
            NSImage    src;
            CGImage    image;
            CGContext  context = null;
            RectangleF rect    = RectangleF.Empty;

            data = new byte[TEXTURE_WIDTH * TEXTURE_HEIGHT * 4];

            src = new NSImage(path);

            image  = src.AsCGImage(ref rect, null, null);
            width  = image.Width;
            height = image.Height;

            CGImageAlphaInfo ai = CGImageAlphaInfo.PremultipliedLast;

            context = new CGBitmapContext(data, width, height, 8, 4 * width, image.ColorSpace, ai);

            // Core Graphics referential is upside-down compared to OpenGL referential
            // Flip the Core Graphics context here
            // An alternative is to use flipped OpenGL texture coordinates when drawing textures
            context.TranslateCTM(0, height);
            context.ScaleCTM(1, -1);

            // Set the blend mode to copy before drawing since the previous contents of memory aren't used.
            // This avoids unnecessary blending.
            context.SetBlendMode(CGBlendMode.Copy);

            context.DrawImage(new RectangleF(0, 0, width, height), image);
        }
Пример #2
0
        public CGImage(int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow,
                       CGColorSpace colorSpace, CGImageAlphaInfo alphaInfo, CGDataProvider provider,
                       nfloat [] decode, bool shouldInterpolate, CGColorRenderingIntent intent)
        {
            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");
            }

            handle = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow,
                                   colorSpace == null ? IntPtr.Zero : colorSpace.Handle, (CGBitmapFlags)alphaInfo, provider == null ? IntPtr.Zero : provider.Handle,
                                   decode,
                                   shouldInterpolate, intent);
        }
Пример #3
0
        private CGImage CreateRGBImageFromBufferData(int mByteWidth, int mWidth, int mHeight)
        {
            CGColorSpace cgColorSpace = CGColorSpace.CreateDeviceRGB();

            CGImageAlphaInfo alphaInfo = (CGImageAlphaInfo)((int)CGImageAlphaInfo.PremultipliedLast | (int)CGBitmapFlags.ByteOrderDefault);

            CGBitmapContext bitmap;

            byte[] mData = GetImageData(0);

            try
            {
                unsafe
                {
                    fixed(byte *ptr = mData)
                    {
                        bitmap = new CGBitmapContext((IntPtr)ptr, mWidth, mHeight, 8, mByteWidth, cgColorSpace, alphaInfo);
                    }
                }
            }
            catch
            {
            }

            CGImage image = bitmap.ToImage();

            return(image);
        }
Пример #4
0
        public static UIImage Scale(UIImage image, int maxSize)
        {
            UIImage res = null;

            using (CGImage imageRef = image.CGImage)
            {
                CGImageAlphaInfo alphaInfo      = imageRef.AlphaInfo;
                CGColorSpace     colorSpaceInfo = CGColorSpace.CreateDeviceRGB();
                if (alphaInfo == CGImageAlphaInfo.None)
                {
                    alphaInfo = CGImageAlphaInfo.NoneSkipLast;
                }

                nfloat width  = imageRef.Width;
                nfloat height = imageRef.Height;
                if (height >= width)
                {
                    width  = (nfloat)Math.Floor((double)width * ((double)maxSize / (double)height));
                    height = maxSize;
                }
                else
                {
                    height = (nfloat)Math.Floor((double)height * ((double)maxSize / (double)width));
                    width  = maxSize;
                }

                CGBitmapContext bitmap = new CGBitmapContext(IntPtr.Zero, (nint)width, (nint)height, imageRef.BitsPerComponent, 0, colorSpaceInfo, alphaInfo);
                bitmap.DrawImage(new CGRect(0, 0, width, height), imageRef);

                res    = UIImage.FromImage(bitmap.ToImage());
                bitmap = null;
            }

            return(res);
        }
Пример #5
0
        //========================================================================

        #endregion
        //========================================================================

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // no data
            IntPtr data = IntPtr.Zero;
            // size
            SizeF bitmapSize = new SizeF(200, 300);
            //this.View.Frame.Size;
            // 32bit RGB (8bits * 4components (aRGB) = 32bit)
            int bitsPerComponent = 8;
            // 4bytes for each pixel (32 bits = 4bytes)
            int bytesPerRow = (int)(4 * bitmapSize.Width);
            // no special color space
            CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
            // aRGB
            CGImageAlphaInfo alphaType = CGImageAlphaInfo.PremultipliedFirst;


            using (CGBitmapContext context = new CGBitmapContext(data
                                                                 , (int)bitmapSize.Width, (int)bitmapSize.Height, bitsPerComponent
                                                                 , bytesPerRow, colorSpace, alphaType))
            {
                //---- draw
            }
        }
Пример #6
0
        void GetImagaDataFromPath(string path)
        {
            NSImage   src;
            CGImage   image;
            CGContext context = null;

            src = new NSImage(path);

            image  = src.AsCGImage(RectangleF.Empty, null, null);
            width  = image.Width;
            height = image.Height;

            int bytesPerRow      = image.BytesPerRow;
            int bitsPerPixel     = image.BitsPerPixel;
            int bitsPerComponent = image.BitsPerComponent;

            data = new byte[bytesPerRow * height * 4];

            CGImageAlphaInfo ai         = CGImageAlphaInfo.PremultipliedLast;
            CGColorSpace     colorSpace = CGColorSpace.CreateDeviceRGB();

            context = new CGBitmapContext(data, width, height, 8, 4 * width, colorSpace, ai);

            // Core Graphics referential is upside-down compared to OpenGL referential
            // Flip the Core Graphics context here
            // An alternative is to use flipped OpenGL texture coordinates when drawing textures
            context.TranslateCTM(0, height);
            context.ScaleCTM(1, -1);

            // Set the blend mode to copy before drawing since the previous contents of memory aren't used.
            // This avoids unnecessary blending.
            context.SetBlendMode(CGBlendMode.Copy);

            context.DrawImage(new RectangleF(0, 0, width, height), image);
        }
Пример #7
0
        int[] GetBitmapPixels(NSData data, CGImageAlphaInfo alphaInfo)
        {
            var adjustOrder = 2;

            if (alphaInfo == CGImageAlphaInfo.First ||
                alphaInfo == CGImageAlphaInfo.PremultipliedFirst)
            {
                adjustOrder = 0;
            }

            var bytesPerPixel = _image.CGImage.BitsPerPixel / 8;

            // Order by ARGB
            var pixels = new int[Width * Height];
            var idx    = 0;

            for (var i = 0; i < Height; i++)
            {
                for (var j = 0; j < Width; j++)
                {
                    var addr = (i * Width + j) * bytesPerPixel;
                    pixels[idx++] =
                        (data[addr + 3] << 24) |
                        (data[addr + 2 - adjustOrder] << 16) |
                        (data[addr + 1] << 8) |
                        (data[addr + adjustOrder]);
                }
            }


            return(pixels);
        }
Пример #8
0
        public static CGBitmapContext CreateContext(CGSize size, out byte[] data, CGImageAlphaInfo bitmapInfo)
        {
            var width       = (int)size.Width;
            var height      = (int)size.Height;
            var bytesPerRow = width * 4;
            var byteCount   = bytesPerRow * height;

            var colorSpace = CGColorSpace.CreateDeviceRGB();

            data = new byte[byteCount];
            return(new CGBitmapContext(data, width, height, 8,
                                       bytesPerRow, colorSpace, bitmapInfo));
        }
Пример #9
0
        public static UIImage Rotate(UIImage image)
        {
            UIImage res = null;

            using (CGImage imageRef = image.CGImage)
            {
                CGImageAlphaInfo alphaInfo      = imageRef.AlphaInfo;
                CGColorSpace     colorSpaceInfo = CGColorSpace.CreateDeviceRGB();
                if (alphaInfo == CGImageAlphaInfo.None)
                {
                    alphaInfo = CGImageAlphaInfo.NoneSkipLast;
                }

                CGBitmapContext bitmap = new CGBitmapContext(IntPtr.Zero, imageRef.Height, imageRef.Width, imageRef.BitsPerComponent, 0, colorSpaceInfo, alphaInfo);
                bitmap.RotateCTM((nfloat)Math.PI / 2);
                bitmap.TranslateCTM(0, -imageRef.Height);
                bitmap.DrawImage(new CGRect(0, 0, imageRef.Width, imageRef.Height), imageRef);

                res    = UIImage.FromImage(bitmap.ToImage());
                bitmap = null;
            }

            return(res);
        }
Пример #10
0
        private CGImage createRGBImageFromBufferData(int mByteWidth, int mWidth, int mHeight)
        {
            CGColorSpace cSpace = CGColorSpace.CreateDeviceRGB();

            CGImageAlphaInfo ai = (CGImageAlphaInfo)((int)CGImageAlphaInfo.NoneSkipFirst | (int)CGBitmapFlags.ByteOrder32Little);

            CGBitmapContext bitmap;

            byte[] mData = GetImageData(0);
            try {
                unsafe
                {
                    fixed(byte *ptr = mData)
                    {
                        bitmap = new CGBitmapContext((IntPtr)ptr, mWidth, mHeight, 8, mByteWidth, cSpace, ai);
                    }
                }
            } catch {
            }

            CGImage image = bitmap.ToImage();

            return(image);
        }
Пример #11
0
 public CGBitmapContext(IntPtr data, int width, int height, int bitsPerComponent, int bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo)
     : base(CGBitmapContextCreate (data, (UIntPtr) width, (UIntPtr) height, (UIntPtr) bitsPerComponent, (UIntPtr) bytesPerRow, colorSpace.handle, (uint) bitmapInfo), true)
 {
 }
        public static UIImage ScaleImage(UIImage image, int maxSize)
        {
            UIImage result;
            nint    width, height;

            using (CGImage imageRef = image.CGImage) {
                CGImageAlphaInfo alphaInfo      = imageRef.AlphaInfo;
                CGColorSpace     colorSpaceInfo = CGColorSpace.CreateDeviceRGB();

                if (alphaInfo == CGImageAlphaInfo.None)
                {
                    alphaInfo = CGImageAlphaInfo.NoneSkipLast;
                }

                width  = imageRef.Width;
                height = imageRef.Height;


                if (height >= width)
                {
                    width  = (int)Math.Floor((double)width * ((double)maxSize / (double)height));
                    height = maxSize;
                }
                else
                {
                    height = (int)Math.Floor((double)height * ((double)maxSize / (double)width));
                    width  = maxSize;
                }

                CGBitmapContext bitmap;

                if (image.Orientation == UIImageOrientation.Up || image.Orientation == UIImageOrientation.Down)
                {
                    bitmap = new CGBitmapContext(IntPtr.Zero, width, height, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
                }
                else
                {
                    bitmap = new CGBitmapContext(IntPtr.Zero, height, width, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
                }

                switch (image.Orientation)
                {
                case UIImageOrientation.Left:
                    bitmap.RotateCTM((float)Math.PI / 2);
                    bitmap.TranslateCTM(0, -height);
                    break;

                case UIImageOrientation.Right:
                    bitmap.RotateCTM(-((float)Math.PI / 2));
                    bitmap.TranslateCTM(-width, 0);
                    break;

                case UIImageOrientation.Up:
                    break;

                case UIImageOrientation.Down:
                    bitmap.TranslateCTM(width, height);
                    bitmap.RotateCTM(-(float)Math.PI);
                    break;
                }

                bitmap.DrawImage(new CGRect(0, 0, width, height), imageRef);

                result = UIImage.FromImage(bitmap.ToImage());
                bitmap = null;
            }
            return(result);
        }
Пример #13
0
        private static UIImage ScaleImage(UIImage image, int maxSize)
        {
            UIImage res = image;

            CGImage imageRef = image.CGImage;

            CGImageAlphaInfo alphaInfo      = imageRef.AlphaInfo;
            CGColorSpace     colorSpaceInfo = CGColorSpace.CreateDeviceRGB();

            if (alphaInfo == CGImageAlphaInfo.None)
            {
                alphaInfo = CGImageAlphaInfo.NoneSkipLast;
            }

            int width  = imageRef.Width;
            int height = imageRef.Height;

            if (maxSize > 0 && maxSize < Math.Max(width, height))
            {
                try
                {
                    if (height >= width)
                    {
                        width  = (int)Math.Floor(width * (maxSize / (double)height));
                        height = maxSize;
                    }
                    else
                    {
                        height = (int)Math.Floor(height * (maxSize / (double)width));
                        width  = maxSize;
                    }

                    int bytesPerRow = (int)image.Size.Width * 4;
                    var buffer      = new byte[(int)(bytesPerRow * image.Size.Height)];

                    CGBitmapContext bitmap;
                    if (image.Orientation == UIImageOrientation.Up || image.Orientation == UIImageOrientation.Down)
                    {
                        bitmap = new CGBitmapContext(buffer, width, height, imageRef.BitsPerComponent,
                                                     imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
                    }
                    else
                    {
                        bitmap = new CGBitmapContext(buffer, height, width, imageRef.BitsPerComponent,
                                                     imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
                    }

                    switch (image.Orientation)
                    {
                    case UIImageOrientation.Left:
                        bitmap.RotateCTM((float)Math.PI / 2);
                        bitmap.TranslateCTM(0, -height);
                        break;

                    case UIImageOrientation.Right:
                        bitmap.RotateCTM(-((float)Math.PI / 2));
                        bitmap.TranslateCTM(-width, 0);
                        break;

                    case UIImageOrientation.Up:
                        break;

                    case UIImageOrientation.Down:
                        bitmap.TranslateCTM(width, height);
                        bitmap.RotateCTM(-(float)Math.PI);
                        break;
                    }

                    bitmap.DrawImage(new RectangleF(0, 0, width, height), imageRef);
                    res = UIImage.FromImage(bitmap.ToImage());
                }
                finally
                {
                    image.Dispose();
                }
            }


            return(res);
        }
Пример #14
0
 public CGBitmapContext(byte [] data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo)
 {
     if (data != null)
         buffer = GCHandle.Alloc (data, GCHandleType.Pinned);
     Handle = CGBitmapContextCreate (data, width, height, bitsPerComponent, bytesPerRow, GetHandle (colorSpace), (uint) bitmapInfo);
 }
Пример #15
0
 public CGBitmapContext(byte []?data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace?colorSpace, CGImageAlphaInfo bitmapInfo)
     : base(Create(data, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo, out var buffer), true)
 {
     this.buffer = buffer;
 }
Пример #16
0
 static IntPtr Create(byte []?data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace?colorSpace, CGImageAlphaInfo bitmapInfo, out GCHandle buffer)
 {
     buffer = default(GCHandle);
     if (data != null)
     {
         buffer = GCHandle.Alloc(data, GCHandleType.Pinned);                  // This requires a pinned GCHandle, because unsafe code is scoped to the current block, and the address of the byte array will be used after this function returns.
     }
     return(CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorSpace.GetHandle(), (uint)bitmapInfo));
 }
Пример #17
0
 public CGBitmapContext(IntPtr data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace?colorSpace, CGImageAlphaInfo bitmapInfo)
     : base(CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorSpace.GetHandle(), (uint)bitmapInfo), true)
 {
 }
Пример #18
0
 public CGBitmapContext(IntPtr data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo)
     : base(CGBitmapContextCreate (data, width, height, bitsPerComponent, bytesPerRow, GetHandle (colorSpace), (uint) bitmapInfo), true)
 {
 }
Пример #19
0
 public CGBitmapContext(byte [] data, int width, int height, int bitsPerComponent, int bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo)
     : base(CGBitmapContextCreate(data, (UIntPtr)width, (UIntPtr)height, (UIntPtr)bitsPerComponent, (UIntPtr)bytesPerRow, colorSpace.handle, (uint)bitmapInfo), true)
 {
 }
Пример #20
0
 public CGBitmapContext(byte [] data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo)
 {
     if (data != null)
     {
         buffer = GCHandle.Alloc(data, GCHandleType.Pinned);                  // This requires a pinned GCHandle, because unsafe code is scoped to the current block, and the address of the byte array will be used after this function returns.
     }
     Handle = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, GetHandle(colorSpace), (uint)bitmapInfo);
 }
Пример #21
0
		public CGImage (int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow,
				CGColorSpace colorSpace, CGImageAlphaInfo alphaInfo, CGDataProvider provider,
				nfloat [] decode, bool shouldInterpolate, CGColorRenderingIntent intent)
		{
			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");

			handle = CGImageCreate (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow,
						colorSpace == null ? IntPtr.Zero : colorSpace.Handle, (CGBitmapFlags) alphaInfo, provider == null ? IntPtr.Zero : provider.Handle,
						decode,
						shouldInterpolate, intent);
		}
Пример #22
0
        private UIImage ScaleAndRotateImage(UIImage imageIn, UIImageOrientation orIn)
        {
            UIImage res;

            using (CGImage imageRef = imageIn.CGImage)
            {
                CGImageAlphaInfo alphaInfo      = imageRef.AlphaInfo;
                CGColorSpace     colorSpaceInfo = CGColorSpace.CreateDeviceRGB();
                if (alphaInfo == CGImageAlphaInfo.None)
                {
                    alphaInfo = CGImageAlphaInfo.NoneSkipLast;
                }

                int    width, height;
                double maxSize = (double)imageIn.Size.Width;
                width  = (int)imageRef.Width;
                height = (int)imageRef.Height;


                if (height >= width)
                {
                    if (orIn == UIImageOrientation.Up || orIn == UIImageOrientation.Down)
                    {
                        maxSize = (double)imageIn.Size.Height;
                    }
                    else
                    {
                        maxSize   = (double)imageIn.Size.Height;
                        alphaInfo = CGImageAlphaInfo.PremultipliedLast;
                    }
                    width  = (int)Math.Floor((double)width * ((double)maxSize / (double)height));
                    height = (int)maxSize;
                }
                else
                {
                    height = (int)Math.Floor((double)height * ((double)maxSize / (double)width));
                    width  = (int)maxSize;
                }


                CGBitmapContext bitmap;

                if (orIn == UIImageOrientation.Up || orIn == UIImageOrientation.Down)
                {
                    bitmap = new CGBitmapContext(IntPtr.Zero, width, height, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
                }
                else
                {
                    bitmap = new CGBitmapContext(IntPtr.Zero, height, width, imageRef.BitsPerComponent, width * imageRef.BitsPerPixel, colorSpaceInfo, alphaInfo);
                }

                switch (orIn)
                {
                case UIImageOrientation.Left:
                    bitmap.RotateCTM((float)Math.PI / 2);
                    bitmap.TranslateCTM(0, -height);
                    break;

                case UIImageOrientation.Right:
                    bitmap.RotateCTM(-((float)Math.PI / 2));
                    bitmap.TranslateCTM(-width, 0);
                    break;

                case UIImageOrientation.Up:
                    break;

                case UIImageOrientation.Down:
                    bitmap.TranslateCTM(width, height);
                    bitmap.RotateCTM(-(float)Math.PI);
                    break;
                }

                bitmap.DrawImage(new Rectangle(0, 0, width, height), imageRef);


                res    = UIImage.FromImage(bitmap.ToImage());
                bitmap = null;


                return(res);
            }
        }
Пример #23
0
 public CGBitmapContext(byte [] data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo)
 {
     if (data != null)
     {
         buffer = GCHandle.Alloc(data, GCHandleType.Pinned);
     }
     Handle = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, GetHandle(colorSpace), (uint)bitmapInfo);
 }