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; }
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 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 System.Drawing.SizeF ((float)view.Bounds.Width, (float)view.Bounds.Height); return im; }
public override object ConvertToBitmap(object handle, double width, double height, double scaleFactor, ImageFormat format) { int pixelWidth = (int)(width * scaleFactor); int pixelHeight = (int)(height * scaleFactor); 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 ((float)scaleFactor, (float)-scaleFactor); var ctx = new CGContextBackend { Context = bmp, Size = new SizeF ((float)width, (float)height), InverseViewTransform = bmp.GetCTM ().Invert (), ScaleFactor = scaleFactor }; 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); im.Size = new SizeF ((float)width, (float)height); bmp.Dispose (); return im; } else { NSImage img = (NSImage)handle; 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 SizeF ((float)width, (float)height); return im; } return handle; } }
// 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 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; }