partial void stylizeClicked(NSObject sender) { messageLabel.StringValue = "stylize Clicked"; //mainImageView.Image = null; StylizeGraph stylizeGraph = new StylizeGraph(); Tensor image = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile("surfers.jpg"); Tensor stylizedImage = stylizeGraph.Stylize(image, 0); var dim = stylizedImage.Dim; System.Drawing.Size sz = new System.Drawing.Size(dim[2], dim[1]); byte[] rawPixels = Emgu.TF.Models.ImageIO.TensorToPixel(stylizedImage, 255, 4); CGColorSpace cspace = CGColorSpace.CreateDeviceRGB(); CGBitmapContext context = new CGBitmapContext( rawPixels, sz.Width, sz.Height, 8, sz.Width * 4, cspace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big); CGImage cgImage = context.ToImage(); NSImage newImg = new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height)); mainImageView.Image = newImg; NSData dta = newImg.AsTiff(); NSBitmapImageRep imgRep = new NSBitmapImageRep(dta); //var datra = imgRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.NSBitmapImageFileType.Jpeg); }
void stylizeGraph_OnDownloadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { Tensor image = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile("surfers.jpg"); Tensor stylizedImage = _stylizeGraph.Stylize(image, 0); var dim = stylizedImage.Dim; System.Drawing.Size sz = new System.Drawing.Size(dim[2], dim[1]); byte[] rawPixels = Emgu.TF.Models.ImageIO.TensorToPixel(stylizedImage, 255, 4); CGColorSpace cspace = CGColorSpace.CreateDeviceRGB(); CGBitmapContext context = new CGBitmapContext( rawPixels, sz.Width, sz.Height, 8, sz.Width * 4, cspace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big); CGImage cgImage = context.ToImage(); NSImage newImg = new NSImage(cgImage, new CGSize(cgImage.Width, cgImage.Height)); SetImage(newImg); NSData dta = newImg.AsTiff(); NSBitmapImageRep imgRep = new NSBitmapImageRep(dta); //var datra = imgRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.NSBitmapImageFileType.Jpeg); }
public byte[] DrawResultsToJpeg(String fileName, MultiboxGraph.Result detectResult, float scoreThreshold = 0.2f) { #if __ANDROID__ BitmapFactory.Options options = new BitmapFactory.Options(); options.InMutable = true; Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName, options); MultiboxGraph.DrawResults(bmp, detectResult, scoreThreshold); using (MemoryStream ms = new MemoryStream()) { bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms); return(ms.ToArray()); } #elif __MACOS__ NSImage img = NSImage.ImageNamed(fileName); MultiboxGraph.DrawResults(img, detectResult, scoreThreshold); var imageData = img.AsTiff(); var imageRep = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep; var jpegData = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null); byte[] jpeg = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length); return(jpeg); #elif __IOS__ UIImage uiimage = new UIImage(fileName); UIImage newImg = MultiboxGraph.DrawResults(uiimage, detectResult, scoreThreshold); var jpegData = newImg.AsJPEG(); byte[] jpeg = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length); return(jpeg); #else throw new Exception("Not implemented"); #endif }
NSData ImageAsJPEG(NSImage i) { NSData d = i.AsTiff(); NSBitmapImageRep rep = new NSBitmapImageRep(d); return(rep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, NSDictionary.FromObjectAndKey(NSNumber.FromInt32(1), NSBitmapImageRep.CompressionFactor))); }
private void ShowImageInfo(string filename) { var sb = new System.Text.StringBuilder(); sb.AppendLine($"Filename: {filename}"); var nsImage = new NSImage(filename); // 画像サイズ sb.AppendLine($"NSImage.Size: {nsImage.Size.Width} {nsImage.Size.Height}"); //var nsImageRep = NSImageRep.ImageRepFromFile(filename); var nsImageRep = NSBitmapImageRep.ImageRepsWithData(nsImage.AsTiff())[0]; // 画像サイズ sb.AppendLine($"NSImageRep.Size: {nsImageRep.Size.Width} {nsImageRep.Size.Height}"); // ピクセル数 sb.AppendLine($"NSImageRep.Pixels: {nsImageRep.PixelsWide} {nsImageRep.PixelsHigh}"); // DPI var dpi = Math.Ceiling((72.0 * nsImageRep.PixelsWide) / nsImageRep.Size.Width); sb.AppendLine($"DPI = {dpi:F4}"); Label.StringValue = sb.ToString(); }
public static Image ToImage(this NSImage img) { using (var imageData = img.AsTiff()) { var imgRep = NSBitmapImageRep.ImageRepFromData(imageData) as NSBitmapImageRep; var imageProps = new NSDictionary(); var data = imgRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png, imageProps); return(Image.FromStream(data.AsStream())); } }
public override object CreateImage(object backend) { var gc = (CGContextBackend)backend; var img = new NSImage(((CGBitmapContext)gc.Context).ToImage(), gc.Size); var imageData = img.AsTiff(); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData(imageData); var im = new NSImage(); im.AddRepresentation(imageRep); return(im); }
/// <summary> /// will merge a group of images into one /// </summary> /// <param name="imageBytes"></param> /// <param name="imageNames"></param> /// <param name="start"></param> /// <param name="end"></param> /// <param name="index"></param> public KeyValuePair <PageImageIndex, AppKit.NSImage> MergeGroup(Dictionary <PageImageIndex, AppKit.NSImage> groupImages, string outputPath) { double maxWidth = 0, maxHeight = 0; int position = 0; foreach (var imageInfo in groupImages) { maxWidth = Math.Max(imageInfo.Value.Size.Width, maxWidth); maxHeight += imageInfo.Value.Size.Height; } // Create new image with max width and sum of all height var newImage = new NSImage(new CoreGraphics.CGSize(maxWidth, maxHeight)); newImage.LockFocus(); // merge all images foreach (var img in groupImages) { if (position == 0) { img.Value.Draw(new CGPoint(), new CGRect(), NSCompositingOperation.DestinationAtop, 1); position++; maxHeight = img.Value.Size.Height; } else { img.Value.Draw(new CGPoint(0, maxHeight), new CGRect(), NSCompositingOperation.DestinationAtop, 1); maxHeight += img.Value.Size.Height; } img.Value.Dispose(); var imagePath = string.Format(@"{0}\{1}", outputPath, img.Key.ImageName); File.Delete(imagePath); } var pageDetails = new PageImageIndex { PageIndex = groupImages.First().Key.PageIndex, ImageIndex = 1, ImageName = string.Format("{0:0000}_{1:0000}.jpg", groupImages.First().Key.PageIndex, 1) }; var newImagePath = string.Format(@"{0}\{1}", outputPath, pageDetails.ImageName); var newImageData2 = newImage.AsTiff(); newImageData2.Save(newImagePath, false); newImage.Dispose(); return(new KeyValuePair <PageImageIndex, NSImage>(pageDetails, null)); }
public override object RenderWidget(Widget w) { var view = ((ViewBackend)w.GetBackend()).Widget; view.LockFocus(); var img = new NSImage(view.DataWithPdfInsideRect(view.Bounds)); var imageData = img.AsTiff(); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData(imageData); var im = new NSImage(); im.AddRepresentation(imageRep); im.Size = new CGSize((nfloat)view.Bounds.Width, (nfloat)view.Bounds.Height); return(im); }
public static void Colourize(NSView control, Color color, Action drawAction) { var size = control.Frame.Size; var image = new NSImage(size); image.LockFocusFlipped(control.IsFlipped); drawAction(); image.UnlockFocus(); var ciImage = CIImage.FromData(image.AsTiff()); if (control.IsFlipped) { var realSize = control.ConvertSizeToBase(size); var affineTransform = new NSAffineTransform(); affineTransform.Translate(0, realSize.Height); affineTransform.Scale(1, -1); var filter1 = CIFilter.FromName("CIAffineTransform"); filter1.SetValueForKey(ciImage, CIInputImage); filter1.SetValueForKey(affineTransform, CIInputTransform); ciImage = filter1.ValueForKey(CIOutputImage) as CIImage; } var filter2 = CIFilter.FromName("CIColorControls"); filter2.SetDefaults(); filter2.SetValueForKey(ciImage, CIInputImage); filter2.SetValueForKey(new NSNumber(0.0f), CIInputSaturation); ciImage = filter2.ValueForKey(CIOutputImage) as CIImage; var filter3 = CIFilter.FromName("CIColorMatrix"); filter3.SetDefaults(); filter3.SetValueForKey(ciImage, CIInputImage); filter3.SetValueForKey(new CIVector(0, color.R, 0), CIInputRVector); filter3.SetValueForKey(new CIVector(color.G, 0, 0), CIInputGVector); filter3.SetValueForKey(new CIVector(0, 0, color.B), CIInputBVector); ciImage = filter3.ValueForKey(CIOutputImage) as CIImage; image = new NSImage(size); var rep = NSCIImageRep.FromCIImage(ciImage); image.AddRepresentation(rep); image.Draw(SD.PointF.Empty, new SD.RectangleF(SD.PointF.Empty, size), NSCompositingOperation.SourceOver, 1); /* Use this when implemented in maccore: * ciImage.Draw (SD.PointF.Empty, new SD.RectangleF (SD.PointF.Empty, size), NSCompositingOperation.SourceOver, 1); */ }
public override object ConvertToBitmap(object handle, int pixelWidth, int pixelHeight, ImageFormat format) { if (handle 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(1, -1); var ctx = new CGContextBackend { Context = bmp, Size = new SizeF(pixelWidth, pixelHeight), InverseViewTransform = bmp.GetCTM().Invert() }; var ci = (CustomImage)handle; ci.DrawInContext(ctx); var img = new NSImage(((CGBitmapContext)bmp).ToImage(), new SizeF(pixelWidth, pixelHeight)); var imageData = img.AsTiff(); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData(imageData); var im = new NSImage(); im.AddRepresentation(imageRep); return(im); } else { return(handle); } }
public static bool SaveImageToFile(NSImage image, string filename, string folderPath) { string jpgFilename = System.IO.Path.Combine(folderPath, filename); // hardcoded filename, overwritten each time NSData imgData = image.AsTiff(); NSError err = null; if (imgData.Save(jpgFilename, false, out err)) { Console.WriteLine("saved as " + jpgFilename); return(true); } else { Console.WriteLine("NOT saved as " + jpgFilename + " because" + err.LocalizedDescription); return(false); } }
public void PlayNewImageInScale(NSImage image, Scale scale) { ForceStop = false; if (image.Size.Width > 150 || image.Size.Height > 150) { image = ImageHelpers.ResizeImage(image, new CGSize(150, 150)); } PlayerNode.Play(); using (var imageRepresentation = new NSBitmapImageRep(image.AsTiff())) { var imageSize = image.Size; for (var x = 0; x < imageSize.Width; x++) { for (var y = 0; y < imageSize.Height; y++) { if (ForceStop) { return; } var color = imageRepresentation.ColorAt(x, y); var carrierFrequency = GetCarrierFrequencyForColor(color); var modulatorFrequency = GetModulatorFrequencyForColor(color); var sampleLength = GetNoteLengthForColor(color); var octave = GetOctaveForColor(color); var pan = GetPanForColor(color); var closestOctave = FrequenciesByOctave.Keys.Aggregate((a, b) => Math.Abs(a - octave) < Math.Abs(b - octave) ? a : b); var closestCarrier = ScaleHelper.GetNotesForScale(scale, closestOctave).Aggregate( (a, b) => Math.Abs(a - carrierFrequency) < Math.Abs(b - carrierFrequency) ? a : b); var closestModulator = ScaleHelper.GetNotesForScale(scale, closestOctave).Aggregate( (a, b) => Math.Abs(a - modulatorFrequency) < Math.Abs(b - modulatorFrequency) ? a : b); PlayToneForColor(closestCarrier, closestModulator, sampleLength, pan, color); } } } }
public override void SaveToStream(object backend, System.IO.Stream stream, ImageFileType fileType) { NSImage img = backend as NSImage; if (img == null) { throw new NotSupportedException(); } var imageData = img.AsTiff(); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData(imageData); var props = new NSDictionary(); imageData = imageRep.RepresentationUsingTypeProperties(fileType.ToMacFileType(), props); using (var s = imageData.AsStream()) { s.CopyTo(stream); } }
/// <summary> /// Saves an image as a jpeg image, with the given quality /// </summary> /// <param name="path">Path to which the image would be saved.</param> /// <param name="quality">An integer from 0 to 100, with 100 being the highest quality</param> public static void SaveJpeg(string path, NSImage img, int quality) { if (quality < 0 || quality > 100) { throw new ArgumentOutOfRangeException(nameof(quality), quality, "quality must be between 0 and 100."); } var tiffData = img.AsTiff(); var imgRep = new NSBitmapImageRep(tiffData); double doubleQual = quality; var settings = new NSMutableDictionary { { NSBitmapImageRep.FallbackBackgroundColor, NSColor.White }, { NSBitmapImageRep.CompressionFactor, new NSNumber(doubleQual / 100) } }; var imgData = imgRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, settings); File.WriteAllBytes(path, imgData.ToArray()); }
public static void SaveToFile(this NSImage image, string path) { //NSBitmapImageRep *imgRep = [[image representations] objectAtIndex: 0]; //NSData *data = [imgRep representationUsingType: NSPNGFileType properties: nil]; //[data writeToFile: @"/path/to/file.png" atomically: NO]; var imageData = image.AsTiff(); var imgRep = NSBitmapImageRep.ImageRepFromData(imageData) as NSBitmapImageRep; var imageProps = new NSDictionary(); var data = imgRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png, imageProps); NSError error; data.Save(path, false, out error); data.Dispose(); imgRep.Dispose(); }
/// <summary> /// Read the file and draw rectangles on it. /// </summary> /// <param name="fileName">The name of the file.</param> /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and labels</param> /// <returns>The image in Jpeg stream format</returns> public static JpegData ImageFileToJpeg(String fileName, Annotation[] annotations = null) { NSImage img = NSImage.ImageNamed(fileName); if (annotations != null && annotations.Length > 0) { DrawAnnotations(img, annotations); } var imageData = img.AsTiff(); var imageRep = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep; var jpegData = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null); byte[] jpeg = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length); JpegData result = new JpegData(); result.Raw = jpeg; result.Width = (int)img.Size.Width; result.Height = (int)img.Size.Height; return(result); }
/// <summary> /// Read the file and draw rectangles on it. /// </summary> /// <param name="fileName">The name of the file.</param> /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and lables</param> /// <returns>The image in Jpeg stream format</returns> public static byte[] ImageFileToJpeg(String fileName, Annotation[] annotations = null) { #if __ANDROID__ BitmapFactory.Options options = new BitmapFactory.Options(); options.InMutable = true; Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName, options); Android.Graphics.Paint p = new Android.Graphics.Paint(); p.SetStyle(Paint.Style.Stroke); p.AntiAlias = true; p.Color = Android.Graphics.Color.Red; Canvas c = new Canvas(bmp); for (int i = 0; i < annotations.Length; i++) { float[] rects = ScaleLocation(annotations[i].Rectangle, bmp.Width, bmp.Height); Android.Graphics.Rect r = new Rect((int)rects[0], (int)rects[1], (int)rects[2], (int)rects[3]); c.DrawRect(r, p); } using (MemoryStream ms = new MemoryStream()) { bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms); return(ms.ToArray()); } #elif __MACOS__ NSImage img = NSImage.ImageNamed(fileName); DrawAnnotations(img, annotations); /* * img.LockFocus(); * * NSColor redColor = NSColor.Red; * redColor.Set(); * var context = NSGraphicsContext.CurrentContext; * var cgcontext = context.CGContext; * cgcontext.ScaleCTM(1, -1); * cgcontext.TranslateCTM(0, -img.Size.Height); * //context.IsFlipped = !context.IsFlipped; * for (int i = 0; i < annotations.Length; i++) * { * float[] rects = ScaleLocation(annotations[i].Rectangle, (int)img.Size.Width, (int) img.Size.Height); * CGRect cgRect = new CGRect( * rects[0], * rects[1], * rects[2] - rects[0], * rects[3] - rects[1]); * NSBezierPath.StrokeRect(cgRect); * } * img.UnlockFocus(); */ var imageData = img.AsTiff(); var imageRep = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep; var jpegData = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null); byte[] jpeg = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length); return(jpeg); #elif __IOS__ UIImage uiimage = new UIImage(fileName); UIGraphics.BeginImageContextWithOptions(uiimage.Size, false, 0); var context = UIGraphics.GetCurrentContext(); uiimage.Draw(new CGPoint()); context.SetStrokeColor(UIColor.Red.CGColor); context.SetLineWidth(2); for (int i = 0; i < annotations.Length; i++) { float[] rects = ScaleLocation( annotations[i].Rectangle, (int)uiimage.Size.Width, (int)uiimage.Size.Height); CGRect cgRect = new CGRect( (nfloat)rects[0], (nfloat)rects[1], (nfloat)(rects[2] - rects[0]), (nfloat)(rects[3] - rects[1])); context.AddRect(cgRect); context.DrawPath(CGPathDrawingMode.Stroke); } UIImage imgWithRect = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); var jpegData = imgWithRect.AsJPEG(); byte[] jpeg = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length); return(jpeg); #else if (Emgu.TF.Util.Platform.OperationSystem == OS.Windows) { Bitmap img = new Bitmap(fileName); if (annotations != null) { using (Graphics g = Graphics.FromImage(img)) { for (int i = 0; i < annotations.Length; i++) { if (annotations[i].Rectangle != null) { float[] rects = ScaleLocation(annotations[i].Rectangle, img.Width, img.Height); PointF origin = new PointF(rects[0], rects[1]); RectangleF rect = new RectangleF(rects[0], rects[1], rects[2] - rects[0], rects[3] - rects[1]); Pen redPen = new Pen(Color.Red, 3); g.DrawRectangle(redPen, Rectangle.Round(rect)); String label = annotations[i].Label; if (label != null) { g.DrawString(label, new Font(FontFamily.GenericSansSerif, 20f), Brushes.Red, origin); } } } g.Save(); } } using (MemoryStream ms = new MemoryStream()) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return(ms.ToArray()); } } else { throw new Exception("DrawResultsToJpeg Not implemented for this platform"); } #endif }
// Create one display list based on the given image. This assumes the image // uses 8-bit chunks to represent a sample bool MakeDisplayList(int listNum, NSImage theImage) { NSBitmapImageRep bitmap; int bytesPerRow, pixelsHigh, pixelsWide, samplesPerPixel; byte currentBit, byteValue; byte[] newBuffer; int rowIndex, colIndex; bitmap = new NSBitmapImageRep(theImage.AsTiff(NSTiffCompression.None, 0)); pixelsHigh = bitmap.PixelsHigh; pixelsWide = bitmap.PixelsWide; bytesPerRow = bitmap.BytesPerRow; samplesPerPixel = bitmap.SamplesPerPixel; newBuffer = new byte[(int)Math.Ceiling((float)bytesPerRow / 8.0) * pixelsHigh]; byte[] bitmapBytesArray = new byte[(pixelsWide * pixelsHigh) * samplesPerPixel]; System.Runtime.InteropServices.Marshal.Copy(bitmap.BitmapData, bitmapBytesArray, 0, (pixelsWide * pixelsHigh) * samplesPerPixel); int curIdx = 0; /* * Convert the color bitmap into a true bitmap, ie, one bit per pixel. We * read at last row, write to first row as Cocoa and OpenGL have opposite * y origins */ for (rowIndex = pixelsHigh - 1; rowIndex >= 0; rowIndex--) { currentBit = 0x80; byteValue = 0; for (colIndex = 0; colIndex < pixelsWide; colIndex++) { if (bitmapBytesArray [rowIndex * bytesPerRow + colIndex * samplesPerPixel] > 0) { byteValue |= currentBit; } currentBit >>= 1; if (currentBit == 0) { newBuffer [curIdx++] = byteValue; currentBit = 0x80; byteValue = 0; } } /* * Fill out the last byte; extra is ignored by OpenGL, but each row * must start on a new byte */ if (currentBit != 0x80) { newBuffer[curIdx++] = byteValue; } } GL.NewList(listNum, ListMode.Compile); GL.Bitmap(pixelsWide, pixelsHigh, 0, 0, pixelsWide, 0, newBuffer); GL.EndList(); return(true); }
public MultiboxDetectionPage() : base() { var button = this.GetButton(); button.Text = "Perform People Detection"; button.Clicked += OnButtonClicked; OnImagesLoaded += async(sender, image) => { GetLabel().Text = "Please wait..."; SetImage(); Task <Tuple <byte[], long> > t = new Task <Tuple <byte[], long> >( () => { //MultiboxGrapho.Download(); MultiboxGraph graph = new MultiboxGraph(); Tensor imageTensor = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile(image[0], 224, 224, 128.0f, 1.0f / 128.0f); MultiboxGraph.Result detectResult = graph.Detect(imageTensor); #if __ANDROID__ BitmapFactory.Options options = new BitmapFactory.Options(); options.InMutable = true; Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(image[0], options); MultiboxGraph.DrawResults(bmp, detectResult, 0.2f); using (MemoryStream ms = new MemoryStream()) { bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms); return(new Tuple <byte[], long>(ms.ToArray(), 0)); } #elif __UNIFIED__ && !__IOS__ NSImage img = NSImage.ImageNamed(image[0]); Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { MultiboxGraph.DrawResults(img, detectResult, 0.1f); var imageData = img.AsTiff(); var imageRep = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep; var jpegData = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null); byte[] raw = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, raw, 0, (int)jpegData.Length); SetImage(raw); GetLabel().Text = String.Format("Detected with in {0} milliseconds.", 0); }); return(new Tuple <byte[], long>(null, 0)); #elif __IOS__ UIImage uiimage = new UIImage(image[0]); Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { UIImage newImg = MultiboxGraph.DrawResults(uiimage, detectResult, 0.1f); var jpegData = newImg.AsJPEG(); byte[] raw = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, raw, 0, (int)jpegData.Length); SetImage(raw); GetLabel().Text = String.Format("Detected with in {0} milliseconds.", 0); }); return(new Tuple <byte[], long>(null, 0)); #else return(new Tuple <byte[], long>(new byte[10], 0)); #endif }); t.Start(); #if !(__UNIFIED__) var result = await t; SetImage(t.Result.Item1); GetLabel().Text = String.Format("Detected with in {0} milliseconds.", t.Result.Item2); #endif }; }
/// <summary> /// Read the file and draw rectangles on it. /// </summary> /// <param name="fileName">The name of the file.</param> /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and lables</param> /// <returns>The image in Jpeg stream format</returns> public static JpegData ImageFileToJpeg(String fileName, Annotation[] annotations = null) { #if __ANDROID__ BitmapFactory.Options options = new BitmapFactory.Options(); options.InMutable = true; Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName, options); Android.Graphics.Paint p = new Android.Graphics.Paint(); p.SetStyle(Paint.Style.Stroke); p.AntiAlias = true; p.Color = Android.Graphics.Color.Red; Canvas c = new Canvas(bmp); for (int i = 0; i < annotations.Length; i++) { float[] rects = ScaleLocation(annotations[i].Rectangle, bmp.Width, bmp.Height); Android.Graphics.Rect r = new Rect((int)rects[0], (int)rects[1], (int)rects[2], (int)rects[3]); c.DrawRect(r, p); } using (MemoryStream ms = new MemoryStream()) { bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms); JpegData result = new JpegData(); result.Raw = ms.ToArray(); result.Width = bmp.Width; result.Height = bmp.Height; return(result); } #elif __MACOS__ NSImage img = NSImage.ImageNamed(fileName); if (annotations != null && annotations.Length > 0) { DrawAnnotations(img, annotations); } var imageData = img.AsTiff(); var imageRep = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep; var jpegData = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null); byte[] jpeg = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length); JpegData result = new JpegData(); result.Raw = jpeg; result.Width = (int)img.Size.Width; result.Height = (int)img.Size.Height; return(result); #elif __IOS__ UIImage uiimage = new UIImage(fileName); UIGraphics.BeginImageContextWithOptions(uiimage.Size, false, 0); var context = UIGraphics.GetCurrentContext(); uiimage.Draw(new CGPoint()); context.SetStrokeColor(UIColor.Red.CGColor); context.SetLineWidth(2); for (int i = 0; i < annotations.Length; i++) { float[] rects = ScaleLocation( annotations[i].Rectangle, (int)uiimage.Size.Width, (int)uiimage.Size.Height); CGRect cgRect = new CGRect( (nfloat)rects[0], (nfloat)rects[1], (nfloat)(rects[2] - rects[0]), (nfloat)(rects[3] - rects[1])); context.AddRect(cgRect); context.DrawPath(CGPathDrawingMode.Stroke); } context.ScaleCTM(1, -1); context.TranslateCTM(0, -uiimage.Size.Height); for (int i = 0; i < annotations.Length; i++) { float[] rects = ScaleLocation( annotations[i].Rectangle, (int)uiimage.Size.Width, (int)uiimage.Size.Height); context.SelectFont("Helvetica", 18, CGTextEncoding.MacRoman); context.SetFillColor((nfloat)1.0, (nfloat)0.0, (nfloat)0.0, (nfloat)1.0); context.SetTextDrawingMode(CGTextDrawingMode.Fill); context.ShowTextAtPoint(rects[0], uiimage.Size.Height - rects[1], annotations[i].Label); } UIImage imgWithRect = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); var jpegData = imgWithRect.AsJPEG(); byte[] jpeg = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length); JpegData result = new JpegData(); result.Raw = jpeg; result.Width = (int)uiimage.Size.Width; result.Height = (int)uiimage.Size.Height; return(result); #else Bitmap img = new Bitmap(fileName); if (annotations != null) { using (Graphics g = Graphics.FromImage(img)) { for (int i = 0; i < annotations.Length; i++) { if (annotations[i].Rectangle != null) { float[] rects = ScaleLocation(annotations[i].Rectangle, img.Width, img.Height); PointF origin = new PointF(rects[0], rects[1]); RectangleF rect = new RectangleF(origin, new SizeF(rects[2] - rects[0], rects[3] - rects[1])); Pen redPen = new Pen(Color.Red, 3); g.DrawRectangle(redPen, Rectangle.Round(rect)); String label = annotations[i].Label; if (label != null) { g.DrawString(label, new Font(FontFamily.GenericSansSerif, 20f), Brushes.Red, origin); } } } g.Save(); } } using (MemoryStream ms = new MemoryStream()) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); JpegData result = new JpegData(); result.Raw = ms.ToArray(); result.Width = img.Size.Width; result.Height = img.Size.Height; return(result); } #endif }
NSData ImageAsJPEG (NSImage i) { NSData d = i.AsTiff (); NSBitmapImageRep rep = new NSBitmapImageRep (d); return rep.RepresentationUsingTypeProperties (NSBitmapImageFileType.Jpeg, NSDictionary.FromObjectAndKey (NSNumber.FromInt32 (1), NSBitmapImageRep.CompressionFactor)); }
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); } }
// Create one display list based on the given image. This assumes the image // uses 8-bit chunks to represent a sample bool MakeDisplayList (int listNum, NSImage theImage) { NSBitmapImageRep bitmap; int bytesPerRow, pixelsHigh, pixelsWide, samplesPerPixel; byte currentBit, byteValue; byte[] newBuffer; int rowIndex, colIndex; bitmap = new NSBitmapImageRep ( theImage.AsTiff (NSTiffCompression.None, 0) ); pixelsHigh = (int)bitmap.PixelsHigh; pixelsWide = (int)bitmap.PixelsWide; bytesPerRow = (int)bitmap.BytesPerRow; samplesPerPixel = (int) bitmap.SamplesPerPixel; newBuffer = new byte[(int)Math.Ceiling ((float)bytesPerRow / 8.0) * pixelsHigh]; byte[] bitmapBytesArray = new byte[(pixelsWide * pixelsHigh) * samplesPerPixel]; System.Runtime.InteropServices.Marshal.Copy (bitmap.BitmapData, bitmapBytesArray, 0, (pixelsWide * pixelsHigh) * samplesPerPixel); int curIdx = 0; /* * Convert the color bitmap into a true bitmap, ie, one bit per pixel. We * read at last row, write to first row as Cocoa and OpenGL have opposite * y origins */ for (rowIndex = pixelsHigh - 1; rowIndex >= 0; rowIndex--) { currentBit = 0x80; byteValue = 0; for (colIndex = 0; colIndex < pixelsWide; colIndex++) { if (bitmapBytesArray [rowIndex * bytesPerRow + colIndex * samplesPerPixel] > 0) byteValue |= currentBit; currentBit >>= 1; if (currentBit == 0) { newBuffer [curIdx++] = byteValue; currentBit = 0x80; byteValue = 0; } } /* * Fill out the last byte; extra is ignored by OpenGL, but each row * must start on a new byte */ if (currentBit != 0x80) newBuffer[curIdx++] = byteValue; } GL.NewList( listNum, ListMode.Compile); GL.Bitmap(pixelsWide, pixelsHigh, 0, 0, pixelsWide, 0, newBuffer); GL.EndList(); return true; }
public virtual async Task <Tuple <Stream, LoadingResult, ImageInformation> > Resolve(string identifier, TaskParameter parameters, CancellationToken token) { NSBundle bundle = null; var filename = Path.GetFileNameWithoutExtension(identifier); var tmpPath = Path.GetDirectoryName(identifier).Trim('/'); var filenamePath = string.IsNullOrWhiteSpace(tmpPath) ? null : tmpPath + "/"; foreach (var fileType in fileTypes) { string file = null; var extension = Path.HasExtension(identifier) ? Path.GetExtension(identifier) : string.IsNullOrWhiteSpace(fileType) ? string.Empty : "." + fileType; token.ThrowIfCancellationRequested(); int scale = (int)ScaleHelper.Scale; if (scale > 1) { while (scale > 1) { token.ThrowIfCancellationRequested(); var tmpFile = string.Format("{0}@{1}x{2}", filename, scale, extension); bundle = NSBundle._AllBundles.FirstOrDefault(bu => { var path = string.IsNullOrWhiteSpace(filenamePath) ? bu.PathForResource(tmpFile, null) : bu.PathForResource(tmpFile, null, filenamePath); return(!string.IsNullOrWhiteSpace(path)); }); if (bundle != null) { file = tmpFile; break; } scale--; } } token.ThrowIfCancellationRequested(); if (file == null) { var tmpFile = string.Format(filename + extension); file = tmpFile; bundle = NSBundle._AllBundles.FirstOrDefault(bu => { var path = string.IsNullOrWhiteSpace(filenamePath) ? bu.PathForResource(tmpFile, null) : bu.PathForResource(tmpFile, null, filenamePath); return(!string.IsNullOrWhiteSpace(path)); }); } token.ThrowIfCancellationRequested(); if (bundle != null) { string path = !string.IsNullOrEmpty(filenamePath) ? bundle.PathForResource(file, null, filenamePath) : bundle.PathForResource(file, null); var stream = FileStore.GetInputStream(path, true); var imageInformation = new ImageInformation(); imageInformation.SetPath(identifier); imageInformation.SetFilePath(path); return(new Tuple <Stream, LoadingResult, ImageInformation>( stream, LoadingResult.CompiledResource, imageInformation)); } token.ThrowIfCancellationRequested(); if (string.IsNullOrEmpty(fileType)) { //Asset catalog NSDataAsset asset = null; try { await MainThreadDispatcher.Instance.PostAsync(() => asset = new NSDataAsset(filename)).ConfigureAwait(false); } catch (Exception) { } if (asset != null) { token.ThrowIfCancellationRequested(); var stream = asset.Data?.AsStream(); var imageInformation = new ImageInformation(); imageInformation.SetPath(identifier); imageInformation.SetFilePath(null); return(new Tuple <Stream, LoadingResult, ImageInformation>( stream, LoadingResult.CompiledResource, imageInformation)); } NSImage image = null; try { await MainThreadDispatcher.Instance.PostAsync(() => image = NSImage.ImageNamed(filename)).ConfigureAwait(false); } catch (Exception) { } if (image != null) { token.ThrowIfCancellationRequested(); var imageRep = new NSBitmapImageRep(image.AsTiff()); var stream = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png) .AsStream(); //var stream = image.AsPNG()?.AsStream(); var imageInformation = new ImageInformation(); imageInformation.SetPath(identifier); imageInformation.SetFilePath(null); return(new Tuple <Stream, LoadingResult, ImageInformation>( stream, LoadingResult.CompiledResource, imageInformation)); } } } throw new FileNotFoundException(identifier); }
public PlotHandler() { Control = new DWSIM.UI.Desktop.Mac.PlotView(); { }; ContextMenu cmenu = new ContextMenu(); var b1 = new ButtonMenuItem() { Text = "Copy" }; cmenu.Items.Add(b1); b1.Click += (sender, e) => { Console.WriteLine(sender.ToString()); // Get the standard pasteboard var pasteboard = NSPasteboard.GeneralPasteboard; // Empty the current contents pasteboard.ClearContents(); NSImage image = new NSImage(new CoreGraphics.CGSize(Control.Bounds.Width, Control.Bounds.Height)); image.LockFocus(); var ctx = NSGraphicsContext.CurrentContext.GraphicsPort; Control.Layer.RenderInContext(ctx); image.UnlockFocus(); // Add the current image to the pasteboard pasteboard.WriteObjects(new NSImage[] { image }); }; var b4 = new ButtonMenuItem() { Text = "Save to File" }; cmenu.Items.Add(b4); b4.Click += (sender, e) => { Console.WriteLine(sender.ToString()); var sfd = new SaveFileDialog(); sfd.Title = "Save Chart to PNG"; sfd.Filters.Add(new FileFilter("PNG File", new string[] { ".png" })); sfd.CurrentFilterIndex = 0; if (sfd.ShowDialog(this.Widget) == DialogResult.Ok) { NSImage image = new NSImage(new CoreGraphics.CGSize(Control.Bounds.Width, Control.Bounds.Height)); image.LockFocus(); var ctx = NSGraphicsContext.CurrentContext.GraphicsPort; Control.Layer.RenderInContext(ctx); image.UnlockFocus(); var imageRep = new NSBitmapImageRep(image.AsTiff()); var pngData = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png); pngData.Save(sfd.FileName, false); } }; var b7 = new ButtonMenuItem() { Text = "Reset to Default View" }; cmenu.Items.Add(b7); b7.Click += (sender, e) => { Console.WriteLine(sender.ToString()); Control.Model.ResetAllAxes(); Control.Model.InvalidatePlot(false); }; Control.RightMouseAction = () => { cmenu.Show(this.Widget); }; }