예제 #1
0
        void AddFrameAroundFace(FaceRectangle faceRect)
        {
            NSImage image = ThePhoto.Image;
            // The API returns based on # of pixels, but NSImage scales, so we need to have scaled versions based on size;

            // The actual size of the image
            double imagePixelWidth  = (double)image.Representations()[0].PixelsWide;
            double imagePixelHeight = (double)image.Representations()[0].PixelsHigh;

            // The photo frame itself is always square, but not every image is square
            CGRect photoFrame = ThePhoto.Subviews [0].Frame;

            photoFrame.Offset(ThePhoto.Frame.Location);

            // The % scaling needed in each axis
            double percentageX = photoFrame.Width / imagePixelWidth;
            double percentageY = photoFrame.Height / imagePixelHeight;

            // Scaled position - API gives top left, but Cocoa wants bottom left.
            double faceRectTopConverted = imagePixelHeight - faceRect.Top;
            double picX = (int)Math.Round(faceRect.Left * percentageX);
            double picY = (int)Math.Round(faceRectTopConverted * percentageY);

            // Scaled size
            double picWidth  = (photoFrame.Width / imagePixelWidth) * faceRect.Width;
            double picHeight = (photoFrame.Height / imagePixelHeight) * faceRect.Height;

            BorderView borderView = new BorderView(new CGRect(photoFrame.X + picX, photoFrame.Y + picY - picHeight, picWidth, picHeight));

            Borders.Add(borderView);
            View.AddSubview(borderView);
        }
예제 #2
0
        private static NSImage InitImage(NSData data)
        {
            var image = new NSImage(data);

            if ((image != null) && !image.IsValid)
            {
                image = null;
            }
            else
            {
                var representations = image.Representations();
                if ((representations != null) && (representations.Length > 0))
                {
                    var representation  = representations[0];
                    var verticalScale   = representation.Size.Height / representation.PixelsHigh;
                    var horizontalScale = representation.Size.Width / representation.PixelsWide;

                    // On Mac, assumption is 72 DPI. If the scales computed here are <> 1, that means we're
                    // loading something at a different DPI, so let's "scale" the NSImage. Ugh.
                    if ((verticalScale < 1) || (horizontalScale < 1) || (verticalScale > 1) || (horizontalScale > 1))
                    {
                        var scaledHeight = (float)representation.PixelsHigh;
                        var scaledWidth  = (float)representation.PixelsWide;
                        image.Size = new CGSize(scaledWidth, scaledHeight);
                    }
                }
            }
            return(image);
        }
예제 #3
0
        public static NSImage Tint(this NSImage image, NSColor tint)
        {
            var colorGenerator = new CIConstantColorGenerator
            {
                Color = CIColor.FromCGColor(tint.CGColor)
            };

            var colorFilter = new CIColorControls
            {
                Image      = (CIImage)colorGenerator.ValueForKey(CIFilterOutputKey.Image),
                Saturation = 3f,
                Brightness = 0.35f,
                Contrast   = 1f
            };

            var monochromeFilter = new CIColorMonochrome
            {
                Image     = CIImage.FromCGImage(image.CGImage),
                Color     = CIColor.FromRgb(0.75f, 0.75f, 0.75f),
                Intensity = 1f
            };

            var compositingFilter = new CIMultiplyCompositing
            {
                Image           = (CIImage)colorFilter.ValueForKey(CIFilterOutputKey.Image),
                BackgroundImage = (CIImage)monochromeFilter.ValueForKey(CIFilterOutputKey.Image)
            };

            var outputImage = (CIImage)compositingFilter.ValueForKey(CIFilterOutputKey.Image);
            var extent      = outputImage.Extent;

            var newsize = Size.Truncate(extent.Size.ToEto());

            if (newsize.IsEmpty)
            {
                return(image);
            }

            var tintedImage = new NSImage(newsize.ToNS());

            tintedImage.LockFocus();
            try
            {
                var graphics  = NSGraphicsContext.CurrentContext.GraphicsPort;
                var ciContext = CIContext.FromContext(graphics, new CIContextOptions {
                    UseSoftwareRenderer = true
                });
                ciContext.DrawImage(outputImage, extent, extent);
            }
            finally
            {
                tintedImage.UnlockFocus();
            }

            var newrep = tintedImage.Representations()[0];

            newrep.Size      = image.Size;
            tintedImage.Size = image.Size;
            return(tintedImage);
        }
예제 #4
0
        private void GetFilesAndCountPixels(string path)
        {
            pixelCount.TextColor = NSColor.Black;
            int pixelCounter = 0;

            string[] containingFiles;

            containingFiles = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
            if (containingFiles.Length > 0)
            {
                foreach (string str in containingFiles)
                {
                    if (str.EndsWith("jpg") || str.EndsWith("png"))
                    {
                        NSImage      img       = new NSImage(str);
                        NSImageRep[] imgReps   = img.Representations();
                        int          imgWidth  = 0;
                        int          imgHeight = 0;
                        if (imgReps != null && imgReps.Length > 0)
                        {
                            long lastSquare = 0, curSquare;
                            //NSImageRep imageRep = new NSImageRep();
                            foreach (NSImageRep rep in imgReps)
                            {
                                curSquare = rep.PixelsWide * rep.PixelsHigh;
                                if (curSquare > lastSquare)
                                {
                                    imgWidth   = (int)rep.PixelsWide;
                                    imgHeight  = (int)rep.PixelsHigh;
                                    lastSquare = curSquare;
                                }
                            }
                        }


                        var imgPixels = imgWidth * imgHeight;

                        Console.WriteLine(imgWidth + "x" + imgHeight);

                        pixelCounter += imgPixels;
                    }
                }

                if (pixelCounter == 0)
                {
                    pixelCount.StringValue = "There are no images in the folder!";
                    pixelCount.TextColor   = NSColor.Red;
                    return;
                }

                float newPixelCounter = (float)pixelCounter / (1024 * 1024);
                pixelCount.StringValue = initialLabeltext + " " + newPixelCounter.ToString("n2") + "MP";
            }
            else
            {
                pixelCount.StringValue = "There are no files in this folder!";
                pixelCount.TextColor   = NSColor.Red;
            }
        }
예제 #5
0
        public override void Set(IImageLoaderTask task, NSImage image, bool animated)
        {
            if (task == null || task.IsCancelled)
            {
                return;
            }

            var control = Control;

            if (control == null)
            {
                return;
            }
            if (control.Layer.Contents == image.CGImage)
            {
                return;
            }

            var parameters      = task.Parameters;
            var representations = image.Representations();

            if (representations.Length > 1)
            {
                control.Layer.Contents = null;
                control.Image          = image;
                control.Animates       = true;
                control.SetNeedsDisplay();
                control.CanDrawSubviewsIntoLayer = true;
                if (IsLayoutNeeded(task))
                {
                    control.NeedsLayout = true;
                }
            }
            else
            {
                if (animated)
                {
                    //TODO fade animation
                    control.Layer.Contents = image.CGImage;
                    control.SetNeedsDisplay();
                    if (IsLayoutNeeded(task))
                    {
                        control.NeedsLayout = true;
                    }
                }
                else
                {
                    control.Layer.Contents = image.CGImage;
                    control.SetNeedsDisplay();
                    if (IsLayoutNeeded(task))
                    {
                        control.NeedsLayout = true;
                    }
                }
            }
        }
예제 #6
0
        internal static NSBitmapImageRep GetBitmapRepresentation(NSImage image)
        {
            foreach (NSImageRep representation in image.Representations())
            {
                if (representation is NSBitmapImageRep)
                {
                    return((NSBitmapImageRep)representation);
                }
            }

            return(null);
        }
예제 #7
0
        public void CreateFromImage(Bitmap image)
        {
            NSImage nsimage = (NSImage)image.ControlObject;

            var rep = nsimage.Representations().OfType <NSBitmapImageRep> ().FirstOrDefault();

            Control      = NSGraphicsContext.FromBitmap(rep);
            context      = Control.GraphicsPort;
            this.Flipped = false;
            this.height  = image.Size.Height;
            context.InterpolationQuality = CGInterpolationQuality.High;
            context.SetAllowsSubpixelPositioning(false);
        }
예제 #8
0
        public override void SetBitmapPixel(object handle, int x, int y, Xwt.Drawing.Color color)
        {
            NSImage          img    = (NSImage)handle;
            NSBitmapImageRep bitmap = img.Representations().OfType <NSBitmapImageRep> ().FirstOrDefault();

            if (bitmap != null)
            {
                bitmap.SetColorAt(color.ToNSColor(), x, y);
            }
            else
            {
                throw new InvalidOperationException("Not a bitmnap image");
            }
        }
예제 #9
0
        public override void Set(IImageLoaderTask task, NSImage image, bool animated)
        {
            if (task == null || task.IsCancelled)
            {
                return;
            }

            var control = Control;

            if (control == null)
            {
                return;
            }

            var representations = image.Representations();

            if (representations.Length > 1)
            {
                control.Image    = image;
                control.Animates = true;
                control.SetNeedsDisplay();

                if (IsLayoutNeeded(task))
                {
                    control.NeedsLayout = true;
                }
            }
            else
            {
                if (animated)
                {
                    //TODO fade animation
                    control.Image = image;
                    control.SetNeedsDisplay();
                    if (IsLayoutNeeded(task))
                    {
                        control.NeedsLayout = true;
                    }
                }
                else
                {
                    control.Image = image;
                    control.SetNeedsDisplay();
                    if (IsLayoutNeeded(task))
                    {
                        control.NeedsLayout = true;
                    }
                }
            }
        }
예제 #10
0
        public override Size GetSize(object handle)
        {
            NSImage          img    = (NSImage)handle;
            NSBitmapImageRep bitmap = img.Representations().OfType <NSBitmapImageRep> ().FirstOrDefault();

            if (bitmap != null)
            {
                return(new Size(bitmap.PixelsWide, bitmap.PixelsHigh));
            }
            else
            {
                return(new Size((int)img.Size.Width, (int)img.Size.Height));
            }
        }
예제 #11
0
        public override Xwt.Drawing.Color GetBitmapPixel(object handle, int x, int y)
        {
            NSImage          img    = (NSImage)handle;
            NSBitmapImageRep bitmap = img.Representations().OfType <NSBitmapImageRep> ().FirstOrDefault();

            if (bitmap != null)
            {
                return(bitmap.ColorAt(x, y).ToXwtColor());
            }
            else
            {
                throw new InvalidOperationException("Not a bitmnap image");
            }
        }
        public static NSData AsBmp(this NSImage target)
        {
            var representations = target.Representations();

            if (representations[0] is NSBitmapImageRep rep)
            {
                return(rep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Bmp, new NSMutableDictionary()));
            }

            var rect    = new CGRect();
            var cgImage = target.AsCGImage(ref rect, null, null);
            var bitmap  = new NSBitmapImageRep(cgImage);

            return(bitmap.RepresentationUsingTypeProperties(NSBitmapImageFileType.Bmp, new NSMutableDictionary()));
        }
예제 #13
0
        public override object CropBitmap(object backend, int srcX, int srcY, int width, int height)
        {
            NSImage          img    = (NSImage)backend;
            NSBitmapImageRep bitmap = img.Representations().OfType <NSBitmapImageRep> ().FirstOrDefault();

            if (bitmap != null)
            {
                var     empty = CGRect.Empty;
                var     cgi   = bitmap.AsCGImage(ref empty, null, null).WithImageInRect(new CGRect(srcX, srcY, width, height));
                NSImage res   = new NSImage(cgi, new CGSize(width, height));
                cgi.Dispose();
                return(res);
            }
            else
            {
                throw new InvalidOperationException("Not a bitmap image");
            }
        }
예제 #14
0
        public void Badge()
        {
            using (NSAutoreleasePool a = new NSAutoreleasePool()) {
                foreach (string path in this.paths)
                {
                    string  extension = Path.GetExtension(path.ToLower());
                    NSImage new_icon  = new NSImage();

                    if (!this.icons.ContainsKey(extension))
                    {
                        foreach (int size in this.sizes)
                        {
                            NSImage file_icon = NSWorkspace.SharedWorkspace.IconForFileType(extension);
                            file_icon.Size = new SizeF(size, size);

                            // TODO: replace this with the sync icon
                            NSImage overlay_icon = NSWorkspace.SharedWorkspace.IconForFileType("sln");
                            overlay_icon.Size = new SizeF(size / 2, size / 2);

                            file_icon.LockFocus();
                            NSGraphicsContext.CurrentContext.ImageInterpolation = NSImageInterpolation.High;
                            overlay_icon.Draw(
                                new RectangleF(0, 0, file_icon.Size.Width / 3, file_icon.Size.Width / 3),
                                new RectangleF(), NSCompositingOperation.SourceOver, 1.0f);
                            file_icon.UnlockFocus();

                            new_icon.AddRepresentation(file_icon.Representations() [0]);
                        }


                        this.icons.Add(extension, new_icon);
                    }
                    else
                    {
                        new_icon = this.icons [extension];
                    }

                    NSWorkspace.SharedWorkspace.SetIconforFile(new_icon, path, 0);
                }
            }
        }
예제 #15
0
        public override void Set(IImageLoaderTask task, NSImage image, bool animated)
        {
            if (task == null || task.IsCancelled)
            {
                return;
            }

            var control = Control;

            if (control == null)
            {
                return;
            }
            if (control.Layer.Contents == image.CGImage)
            {
                return;
            }

            var parameters      = task.Parameters;
            var representations = image.Representations();

            if (representations.Length > 1)
            {
                control.Layer.Contents = null;
                control.Image          = image;
                control.Animates       = true;
            }
            else
            {
                if (animated)
                {
                    //TODO fade animation
                    control.Layer.Contents = image.CGImage;
                }
                else
                {
                    control.Layer.Contents = image.CGImage;
                }
            }
        }
예제 #16
0
 void IImageVisualElementRenderer.SetImage(NSImage image)
 {
     Control.Image    = image;
     Control.Animates = image != null && image.Representations().Length > 1;
 }
예제 #17
0
        public override bool IsBitmap(object handle)
        {
            NSImage img = handle as NSImage;

            return(img != null && img.Representations().OfType <NSBitmapImageRep> ().Any());
        }
예제 #18
0
        public override object ConvertToBitmap(ImageDescription idesc, double scaleFactor, ImageFormat format)
        {
            double width       = idesc.Size.Width;
            double height      = idesc.Size.Height;
            int    pixelWidth  = (int)(width * scaleFactor);
            int    pixelHeight = (int)(height * scaleFactor);

            if (idesc.Backend is CustomImage)
            {
                var flags = CGBitmapFlags.ByteOrderDefault;
                int bytesPerRow;
                switch (format)
                {
                case ImageFormat.ARGB32:
                    bytesPerRow = pixelWidth * 4;
                    flags      |= CGBitmapFlags.PremultipliedFirst;
                    break;

                case ImageFormat.RGB24:
                    bytesPerRow = pixelWidth * 3;
                    flags      |= CGBitmapFlags.None;
                    break;

                default:
                    throw new NotImplementedException("ImageFormat: " + format.ToString());
                }

                var bmp = new CGBitmapContext(IntPtr.Zero, pixelWidth, pixelHeight, 8, bytesPerRow, Util.DeviceRGBColorSpace, flags);
                bmp.TranslateCTM(0, pixelHeight);
                bmp.ScaleCTM((float)scaleFactor, (float)-scaleFactor);

                var ctx = new CGContextBackend {
                    Context = bmp,
                    Size    = new CGSize((nfloat)width, (nfloat)height),
                    InverseViewTransform = bmp.GetCTM().Invert(),
                    ScaleFactor          = scaleFactor
                };

                var ci = (CustomImage)idesc.Backend;
                ci.DrawInContext(ctx, idesc);

                var img       = new NSImage(((CGBitmapContext)bmp).ToImage(), new CGSize(pixelWidth, pixelHeight));
                var imageData = img.AsTiff();
                var imageRep  = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData(imageData);
                var im        = new NSImage();
                im.AddRepresentation(imageRep);
                im.Size = new CGSize((nfloat)width, (nfloat)height);
                bmp.Dispose();
                return(im);
            }
            else
            {
                NSImage          img    = (NSImage)idesc.Backend;
                NSBitmapImageRep bitmap = img.Representations().OfType <NSBitmapImageRep> ().FirstOrDefault();
                if (bitmap == null)
                {
                    var imageData = img.AsTiff();
                    var imageRep  = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData(imageData);
                    var im        = new NSImage();
                    im.AddRepresentation(imageRep);
                    im.Size = new CGSize((nfloat)width, (nfloat)height);
                    return(im);
                }
                return(idesc.Backend);
            }
        }