public static Pixbuf ConvertToPixbuf(BitmapEx bmpEx) { int bitspersample; bool HasAlpha; if (bmpEx.BitDepth == ImageType.RGB8) { bitspersample = 8; HasAlpha = false; } else if (bmpEx.BitDepth == ImageType.RGBA8) { bitspersample = 8; HasAlpha = true; } else if (bmpEx.BitDepth == ImageType.RGB16) { bitspersample = 16; HasAlpha = false; } else if (bmpEx.BitDepth == ImageType.RGBA16) { bitspersample = 16; HasAlpha = true; } else { throw new ArgumentException("Bitdepth not supported"); } byte[] data = new byte[bmpEx.Height * bmpEx.Stride]; bmpEx.LockBits(); unsafe { System.Runtime.InteropServices.Marshal.Copy(bmpEx.Scan0, data, 0, data.Length); } bmpEx.UnlockBits(); return(new Pixbuf(data, HasAlpha, bitspersample, (int)bmpEx.Width, (int)bmpEx.Height, (int)bmpEx.Stride)); }
public static NSImage ToNSImage(BitmapEx bmpEx) { bmpEx.LockBits(); long bufferLength = bmpEx.Width * bmpEx.Height; CGDataProvider provider = new CGDataProvider(bmpEx.Scan0, (int)bufferLength); int bitsPerComponent, bitsPerPixel, bytesPerRow = (int)bmpEx.Stride; CGColorSpace colorSpaceRef = CGColorSpace.CreateDeviceRGB(); switch (bmpEx.BitDepth) { case ImageType.RGB16: bitsPerComponent = 16; bitsPerPixel = 48; bufferLength *= 3; break; case ImageType.RGBA16: bitsPerComponent = 16; bitsPerPixel = 64; bufferLength *= 4; break; case ImageType.RGB8: bitsPerComponent = 8; bitsPerPixel = 24; bufferLength *= 3; break; case ImageType.RGBA8: bitsPerComponent = 8; bitsPerPixel = 32; bufferLength *= 4; break; case ImageType.RGB32: bitsPerComponent = 32; bitsPerPixel = 96; bufferLength *= 3; break; case ImageType.RGBA32: bitsPerComponent = 32; bitsPerPixel = 128; bufferLength *= 4; break; case ImageType.RGB64: bitsPerComponent = 64; bitsPerPixel = 192; bufferLength *= 3; break; case ImageType.RGBA64: bitsPerComponent = 64; bitsPerPixel = 256; bufferLength *= 4; break; default: throw new ArgumentException("Bitdepth not supported"); } CGImage img = new CGImage((int)bmpEx.Width, (int)bmpEx.Height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, CGBitmapFlags.ByteOrderDefault, provider, null, true, CGColorRenderingIntent.Default); bmpEx.UnlockBits(); return(new NSImage(img, new SizeF(img.Width, img.Height))); }
public static Bitmap ConvertToBitmap(BitmapEx bmpEx) { uint depth = 0; bool HasAlpha; PixelFormat format; if (bmpEx.BitDepth == ImageType.RGB8) { format = PixelFormat.Format24bppRgb; depth = 3; HasAlpha = false; } else if (bmpEx.BitDepth == ImageType.RGBA8) { format = PixelFormat.Format32bppArgb; depth = 4; HasAlpha = true; } else if (bmpEx.BitDepth == ImageType.RGB16) { format = PixelFormat.Format48bppRgb; depth = 6; HasAlpha = false; } else if (bmpEx.BitDepth == ImageType.RGBA16) { format = PixelFormat.Format64bppArgb; depth = 8; HasAlpha = true; } else { throw new ArgumentException("Bitdepth not supported"); } Bitmap outBmp = new Bitmap((int)bmpEx.Width, (int)bmpEx.Height, format); BitmapData bmd = outBmp.LockBits(new System.Drawing.Rectangle(0, 0, outBmp.Width, outBmp.Height), ImageLockMode.WriteOnly, outBmp.PixelFormat); bmpEx.LockBits(); unsafe { byte *pixIn = (byte *)bmpEx.Scan0; byte *pixOut = (byte *)bmd.Scan0; long idx; uint x, y; int resV = (int)(bmd.Stride - bmpEx.Stride); int res = 0; for (y = 0; y < bmd.Height; y++) { for (x = 0; x < bmd.Stride; x += depth) { idx = y * bmd.Stride + x; pixOut[idx + 2] = pixIn[idx - res]; pixOut[idx + 1] = pixIn[idx + 1 - res]; pixOut[idx] = pixIn[idx + 2 - res]; if (HasAlpha) { pixOut[idx + 3] = pixIn[idx + 3 - res]; } } res += resV; } } outBmp.UnlockBits(bmd); bmpEx.UnlockBits(); return(outBmp); }