示例#1
0
        public void CreateIndexed()
        {
            // from: http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGColorSpace/Reference/reference.html
            //  m is the number of color components in the base color space
            nint      m         = 3; // RGB
            const int lastIndex = 2;

            // An array of m*(lastIndex+1) bytes
            byte[] table = new byte [3 * (lastIndex + 1)] {
                1, 2, 3, 4, 5, 6, 255, 255, 255
            };
            using (var base_cs = CGColorSpace.CreateDeviceRGB())
                using (var cs = CGColorSpace.CreateIndexed(base_cs, lastIndex, table)) {
                    Assert.That(cs.Components, Is.EqualTo((nint)1), "1");
                    Assert.That(cs.Model, Is.EqualTo(CGColorSpaceModel.Indexed), "Indexed");
                    var bcs = cs.GetBaseColorSpace();
                    Assert.That(bcs.Components, Is.EqualTo(m), "Components");
                    Assert.That(base_cs.Model, Is.EqualTo(bcs.Model), "GetBaseColorSpace");
                    var new_table = cs.GetColorTable();
                    Assert.That(table, Is.EqualTo(new_table), "GetColorTable");

                    if (TestRuntime.CheckXcodeVersion(5, 0))
                    {
                        Assert.Null(cs.GetICCProfile(), "GetICCProfile");
                    }

                    if (TestRuntime.CheckXcodeVersion(8, 0))
                    {
                        Assert.Null(cs.Name, "Name");
                        Assert.False(cs.IsWideGamutRgb, "IsWideGamutRgb");
                        Assert.False(cs.SupportsOutput, "SupportsOutput");
                        Assert.Null(cs.GetIccData(), "GetIccData");
                    }
                }
        }
示例#2
0
        public static CALayer CreateLayerFromPalettedData(byte[] data, byte[] palette, ushort width, ushort height)
        {
            CALayer layer = CALayer.Create();

            layer.Bounds = new RectangleF(0, 0, width, height);

            layer.Contents = new CGImage(width, height, 8, 8, width,
                                         CGColorSpace.CreateIndexed(CGColorSpace.CreateDeviceRGB(), 255, palette),
                                         CGImageAlphaInfo.None,
                                         new CGDataProvider(data, 0, data.Length), null, false, CGColorRenderingIntent.Default);
            return(layer);
        }
示例#3
0
        public Bitmap(int width, int height, PixelFormat format)
        {
            imageTransform = new CGAffineTransform(1, 0, 0, -1, 0, height);

            int           bitsPerComponent, bytesPerRow;
            CGColorSpace  colorSpace;
            CGBitmapFlags bitmapInfo;
            bool          premultiplied = false;
            int           bitsPerPixel  = 0;

            pixelFormat = format;

            // Don't forget to set the Image width and height for size.
            imageSize.Width  = width;
            imageSize.Height = height;

            switch (format)
            {
            case PixelFormat.Format32bppPArgb:
            case PixelFormat.DontCare:
                premultiplied    = true;
                colorSpace       = CGColorSpace.CreateDeviceRGB();
                bitsPerComponent = 8;
                bitsPerPixel     = 32;
                bitmapInfo       = CGBitmapFlags.PremultipliedLast;
                break;

            case PixelFormat.Format32bppArgb:
                colorSpace       = CGColorSpace.CreateDeviceRGB();
                bitsPerComponent = 8;
                bitsPerPixel     = 32;
                bitmapInfo       = CGBitmapFlags.PremultipliedLast;
                break;

            case PixelFormat.Format32bppRgb:
                colorSpace       = CGColorSpace.CreateDeviceRGB();
                bitsPerComponent = 8;
                bitsPerPixel     = 32;
                bitmapInfo       = CGBitmapFlags.NoneSkipLast;
                break;

            case PixelFormat.Format24bppRgb:
                colorSpace       = CGColorSpace.CreateDeviceRGB();
                bitsPerComponent = 8;
                bitsPerPixel     = 24;
                bitmapInfo       = CGBitmapFlags.None;
                break;

            case PixelFormat.Format8bppIndexed:
                // FIXME: Default palette
                colorSpace       = CGColorSpace.CreateIndexed(CGColorSpace.CreateDeviceRGB(), 255, new byte[3 * 256]);
                bitsPerComponent = 8;
                bitsPerPixel     = 8;
                bitmapInfo       = CGBitmapFlags.None;
                palette          = new ColorPalette(0, new Color[256]);
                break;

            case PixelFormat.Format4bppIndexed:
                // FIXME: Default palette
                colorSpace       = CGColorSpace.CreateIndexed(CGColorSpace.CreateDeviceRGB(), 15, new byte[3 * 16]);
                bitsPerComponent = 4;
                bitsPerPixel     = 4;
                bitmapInfo       = CGBitmapFlags.None;
                palette          = new ColorPalette(0, new Color[16]);
                break;

            case PixelFormat.Format1bppIndexed:
                // FIXME: Default palette
                colorSpace       = CGColorSpace.CreateIndexed(CGColorSpace.CreateDeviceRGB(), 1, new byte[3 * 2]);
                bitsPerComponent = 1;
                bitsPerPixel     = 1;
                bitmapInfo       = CGBitmapFlags.None;
                palette          = new ColorPalette(0, new Color[2] {
                    Color.Black, Color.White
                });
                break;

            default:
                throw new Exception("Format not supported: " + format);
            }

            bytesPerRow = (int)(((long)width * bitsPerPixel + 7) / 8);
            int size = bytesPerRow * height;

            bitmapBlock = Marshal.AllocHGlobal(size);
            unsafe {
                byte *b = (byte *)bitmapBlock;
                for (int i = 0; i < size; i++)
                {
                    b [i] = 0;
                }
            }
            dataProvider  = new CGDataProvider(bitmapBlock, size, true);
            NativeCGImage = new CGImage(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, dataProvider, null, false, CGColorRenderingIntent.Default);

            dpiWidth = dpiHeight = ConversionHelpers.MS_DPI;
            physicalDimension.Width  = width;
            physicalDimension.Height = height;


            // The physical size may be off on certain implementations.  For instance the dpiWidth and dpiHeight
            // are read using integers in core graphics but in windows it is a float.
            // For example:
            // coregraphics dpiWidth = 24 as integer
            // windows dpiWidth = 24.999935 as float
            // this gives a few pixels difference when calculating the physical size.
            // 256 * 96 / 24 = 1024
            // 256 * 96 / 24.999935 = 983.04
            //
            // https://bugzilla.xamarin.com/show_bug.cgi?id=14365
            // PR: https://github.com/mono/maccore/pull/57
            //

            physicalSize         = new SizeF(physicalDimension.Width, physicalDimension.Height);
            physicalSize.Width  *= ConversionHelpers.MS_DPI / dpiWidth;
            physicalSize.Height *= ConversionHelpers.MS_DPI / dpiHeight;

            rawFormat   = ImageFormat.MemoryBmp;
            pixelFormat = format;

            if (premultiplied)
            {
            }                                  // make compiler happy
        }