コード例 #1
0
        public void Present()
        {
            _bitmap.LockPixels();
            IntPtr length;
            var    pixels = _bitmap.GetPixels(out length);

#if WIN32
            UnmanagedMethods.BITMAPINFO bmi = new UnmanagedMethods.BITMAPINFO();
            bmi.biSize        = UnmanagedMethods.SizeOf_BITMAPINFOHEADER;
            bmi.biWidth       = _bitmap.Width;
            bmi.biHeight      = -_bitmap.Height; // top-down image
            bmi.biPlanes      = 1;
            bmi.biBitCount    = 32;
            bmi.biCompression = (uint)UnmanagedMethods.BitmapCompressionMode.BI_RGB;
            bmi.biSizeImage   = 0;

            IntPtr hdc = UnmanagedMethods.GetDC(_hwnd.Handle);

            int ret = UnmanagedMethods.SetDIBitsToDevice(hdc,
                                                         0, 0,
                                                         (uint)_bitmap.Width, (uint)_bitmap.Height,
                                                         0, 0,
                                                         0, (uint)_bitmap.Height,
                                                         pixels,
                                                         ref bmi,
                                                         (uint)UnmanagedMethods.DIBColorTable.DIB_RGB_COLORS);

            UnmanagedMethods.ReleaseDC(_hwnd.Handle, hdc);
#else
            throw new NotImplementedException();
#endif

            _bitmap.UnlockPixels();
        }
コード例 #2
0
ファイル: ATest01.cs プロジェクト: cs-phillips/PixelFarm
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            if (!buildVxs)
            {
                destImg  = new ActualImage(400, 300, PixelFarm.Agg.PixelFormat.ARGB32);
                imgGfx2d = new ImageGraphics2D(destImg); //no platform
                p        = new AggCanvasPainter(imgGfx2d);

                buildVxs = true;
                ReadAndRender("tahoma.ttf", "A");
            }

            canvas.Clear(SKColors.White);

            using (SKBitmap bitmap = new SKBitmap(destImg.Width, destImg.Height, false))
            {
                //IntPtr pixelHeader = bitmap.GetPixels();
                //byte[] srcBuffer = ActualImage.GetBuffer(destImg);
                bitmap.LockPixels();
                //System.Runtime.InteropServices.Marshal.Copy(
                //    srcBuffer, 0,
                //    pixelHeader, srcBuffer.Length);
                //bitmap.UnlockPixels();
                PixelFarm.Agg.Imaging.BitmapHelper.CopyToGdiPlusBitmapSameSize(
                    destImg, destImg.Width, destImg.Height,
                    destImg.Stride, bitmap.GetPixels());
                bitmap.UnlockPixels();
                canvas.DrawBitmap(bitmap, 10, 10);
            }
        }
コード例 #3
0
ファイル: OpenSlide.cs プロジェクト: patkalie/OpenslideCs
            private SKBitmap ReadRegion(SizeL location, int level, SizeL size)
            {
                Stopwatch sw = new Stopwatch();

                sw.Start();
                OpenSlide.TraceMsg("start ReadRegion " + level + "/" + location.Height + "_" + location.Width + ": " + GetBytesReadable(size.Width * size.Height * 3));
                SKBitmap bmp = new SKBitmap((int)size.Width, (int)size.Height);

                bmp.SetPixel(0, 0, SKColors.AliceBlue);
                bmp.LockPixels();
                var bmpdata = bmp.GetPixels();

                OpenSlide.TraceMsg("bmp locked " + level + "/" + location.Height + "_" + location.Width);
                unsafe
                {
                    void *p = bmpdata.ToPointer();
                    Import.openslide_read_region(handle, p, location.Width, location.Height, level, size.Width, size.Height);
                }
                OpenSlide.TraceMsg("read finished " + level + "/" + location.Height + "_" + location.Width + ": " + GetBytesReadable(size.Width * size.Height * 3 / Math.Max(sw.ElapsedMilliseconds, 1)) + "/ms");
                bmp.UnlockPixels();
                OpenSlide.TraceMsg("unlock bits " + level + "/" + location.Height + "_" + location.Width);
                if (bmp.GetPixel(0, 0) == SKColors.Black)
                {
                    var error = CheckForLastError();
                    if (error != null)
                    {
                        throw new ArgumentException($"error reading region loc:{location}, level:{level}, size:{size}" + error);
                    }
                    //else just a black image?
                }
                OpenSlide.TraceMsg("end ReadRegion " + level + "/" + location.Height + "_" + location.Width);
                return(bmp);
            }
コード例 #4
0
        public override void DrawImage(ActualImage actualImage, double x, double y)
        {
            //create Gdi bitmap from actual image
            int w = actualImage.Width;
            int h = actualImage.Height;

            switch (actualImage.PixelFormat)
            {
            case Agg.PixelFormat.ARGB32:
            {
                using (SKBitmap newBmp = new SKBitmap(actualImage.Width, actualImage.Height))
                {
                    newBmp.LockPixels();
                    byte[] actualImgBuffer = ActualImage.GetBuffer(actualImage);
                    System.Runtime.InteropServices.Marshal.Copy(
                        actualImgBuffer,
                        0,
                        newBmp.GetPixels(),
                        actualImgBuffer.Length);
                    newBmp.UnlockPixels();
                }
                //newBmp.internalBmp.LockPixels();
                //byte[] actualImgBuffer = ActualImage.GetBuffer(actualImage);

                //System.Runtime.InteropServices.Marshal.Copy(
                //     actualImgBuffer,
                //     0,
                //      newBmp.internalBmp.GetPixels(),
                //      actualImgBuffer.Length);

                //newBmp.internalBmp.UnlockPixels();
                //return newBmp;

                //copy data from acutal buffer to internal representation bitmap
                //using (MySkBmp bmp = MySkBmp.CopyFrom(actualImage))
                //{
                //    _skCanvas.DrawBitmap(bmp.internalBmp, (float)x, (float)y);
                //}
            }
            break;

            case Agg.PixelFormat.RGB24:
            {
            }
            break;

            case Agg.PixelFormat.GrayScale8:
            {
            }
            break;

            default:
                throw new NotSupportedException();
            }
        }
コード例 #5
0
ファイル: RenderTarget.cs プロジェクト: nagyistge/Avalonia
        public void Present()
        {
            _bitmap.LockPixels();
            IntPtr length;
            var    pixels = _bitmap.GetPixels(out length);

#if __IOS__
            const int bitmapInfo      = ((int)CGBitmapFlags.ByteOrder32Big) | ((int)CGImageAlphaInfo.PremultipliedLast);
            var       bounds          = GetApplicationFrame();
            var       statusBarOffset = UIScreen.MainScreen.Bounds.Height - bounds.Height;

            using (var colorSpace = CGColorSpace.CreateDeviceRGB())
                using (var bContext = new CGBitmapContext(pixels, _bitmap.Width, _bitmap.Height, 8, _bitmap.Width * 4, colorSpace, (CGImageAlphaInfo)bitmapInfo))
                    using (var image = bContext.ToImage())
                        using (var context = UIGraphics.GetCurrentContext())
                        {
                            // flip the image for CGContext.DrawImage
                            context.TranslateCTM(0, bounds.Height + statusBarOffset);
                            context.ScaleCTM(1, -1);
                            context.DrawImage(bounds, image);
                        }
#elif WIN32
            UnmanagedMethods.BITMAPINFO bmi = new UnmanagedMethods.BITMAPINFO();
            bmi.biSize        = UnmanagedMethods.SizeOf_BITMAPINFOHEADER;
            bmi.biWidth       = _bitmap.Width;
            bmi.biHeight      = -_bitmap.Height; // top-down image
            bmi.biPlanes      = 1;
            bmi.biBitCount    = 32;
            bmi.biCompression = (uint)UnmanagedMethods.BitmapCompressionMode.BI_RGB;
            bmi.biSizeImage   = 0;

            IntPtr hdc = UnmanagedMethods.GetDC(_hwnd);

            int ret = UnmanagedMethods.SetDIBitsToDevice(hdc,
                                                         0, 0,
                                                         (uint)_bitmap.Width, (uint)_bitmap.Height,
                                                         0, 0,
                                                         0, (uint)_bitmap.Height,
                                                         pixels,
                                                         ref bmi,
                                                         (uint)UnmanagedMethods.DIBColorTable.DIB_RGB_COLORS);

            UnmanagedMethods.ReleaseDC(_hwnd, hdc);
#endif

            _bitmap.UnlockPixels();
        }
コード例 #6
0
ファイル: RenderTarget.cs プロジェクト: vcnwafor/Avalonia
        public void Present()
        {
            _bitmap.LockPixels();
            IntPtr length;
            var    pixels = _bitmap.GetPixels(out length);

            const int bitmapInfo      = ((int)CGBitmapFlags.ByteOrder32Big) | ((int)CGImageAlphaInfo.PremultipliedLast);
            var       bounds          = GetApplicationFrame();
            var       statusBarOffset = UIScreen.MainScreen.Bounds.Height - bounds.Height;

            using (var colorSpace = CGColorSpace.CreateDeviceRGB())
                using (var bContext = new CGBitmapContext(pixels, _bitmap.Width, _bitmap.Height, 8, _bitmap.Width * 4, colorSpace, (CGImageAlphaInfo)bitmapInfo))
                    using (var image = bContext.ToImage())
                        using (var context = UIGraphics.GetCurrentContext())
                        {
                            // flip the image for CGContext.DrawImage
                            context.TranslateCTM(0, bounds.Height + statusBarOffset);
                            context.ScaleCTM(1, -1);
                            context.DrawImage(bounds, image);
                        }

            _bitmap.UnlockPixels();
        }
コード例 #7
0
        private void CalculateLuminance(SKBitmap src)
        {
            if (src == null)
            {
                throw new ArgumentNullException("src");
            }

            src.LockPixels();
            try
            {
                for (var index = 0; index < src.Width * src.Height; index++)
                {
                    var pixel = src.Pixels[index];
                    // Calculate luminance cheaply, favoring green.
                    var luminance = (byte)((RChannelWeight * pixel.Red + GChannelWeight * pixel.Green + BChannelWeight * pixel.Blue) >> ChannelWeight);
                    luminances[index] = (byte)(((luminance * pixel.Alpha) >> 8) + (255 * (255 - pixel.Alpha) >> 8));
                }
            }
            finally
            {
                src.UnlockPixels();
            }
        }
コード例 #8
0
ファイル: BitmapImpl.cs プロジェクト: kyawkyaw/Avalonia
 public void Dispose()
 {
     _bmp.UnlockPixels();
     _bmp = null;
 }
コード例 #9
0
ファイル: MycoImageSource.cs プロジェクト: roceh/MycoLayout
        public IMycoDrawable SKBitmapFromImageSource(ImageSource source)
        {
            if (source is FileImageSource)
            {
                string fullPath = ((FileImageSource)source).File;

                IMycoDrawable drawable = null;

                if (!_cache.TryGetValue(fullPath, out drawable))
                {
                    SKBitmap bitmap;

                    if (System.IO.File.Exists(fullPath))
                    {
                        bitmap = SKBitmap.Decode(fullPath);
                    }
                    else
                    {
                        string fileId = System.IO.Path.GetFileNameWithoutExtension(fullPath).ToLower();

                        bool isNinePatch = false;

                        if (System.IO.Path.GetExtension(fileId) == ".9p")
                        {
                            fileId      = System.IO.Path.GetFileNameWithoutExtension(fileId).ToLower();
                            isNinePatch = true;
                        }

                        var id = Android.App.Application.Context.Resources.GetIdentifier(fileId, "drawable", Android.App.Application.Context.PackageName);

                        const int bytesPerPixel = 4;

                        BitmapFactory.Options opts = new BitmapFactory.Options {
                            InPreferredConfig = Bitmap.Config.Argb8888, InScaled = !isNinePatch
                        };

                        using (var nativeBitmap = BitmapFactory.DecodeResource(Android.App.Application.Context.Resources, id, opts))
                        {
                            int size      = nativeBitmap.Width * nativeBitmap.Height * bytesPerPixel;
                            var pixelData = new byte[size];
                            using (var byteBuffer = Java.Nio.ByteBuffer.AllocateDirect(size))
                            {
                                nativeBitmap.CopyPixelsToBuffer(byteBuffer);
                                Marshal.Copy(byteBuffer.GetDirectBufferAddress(), pixelData, 0, size);
                            }

                            bitmap = new SKBitmap(nativeBitmap.Width, nativeBitmap.Height, SKColorType.Rgba_8888, SKAlphaType.Premul);

                            IntPtr length;

                            bitmap.LockPixels();
                            try
                            {
                                // wish there was a way to IntPtr to IntPtr copy
                                var pixels = bitmap.GetPixels(out length);
                                Marshal.Copy(pixelData, 0, pixels, size);
                            }
                            finally
                            {
                                bitmap.UnlockPixels();
                            }
                        }

                        if (bitmap != null)
                        {
                            drawable = isNinePatch ? new MycoDrawableNinePatch(bitmap) as IMycoDrawable : new MycoDrawableBitmap(bitmap) as IMycoDrawable;
                            _cache.Add(fullPath, drawable);
                        }
                    }
                }

                return(drawable);
            }

            return(null);
        }
コード例 #10
0
ファイル: SkiaPainter.cs プロジェクト: dhilip89/PixelFarm
        public override void DrawImage(Image img, double left, double top)
        {
            if (img is ActualImage)
            {
                ActualImage actualImage = (ActualImage)img;
                //create Gdi bitmap from actual image
                int w = actualImage.Width;
                int h = actualImage.Height;
                switch (actualImage.PixelFormat)
                {
                case Agg.PixelFormat.ARGB32:
                {
                    using (SKBitmap newBmp = new SKBitmap(actualImage.Width, actualImage.Height))
                    {
                        newBmp.LockPixels();
                        //byte[] actualImgBuffer = ActualImage.GetBuffer(actualImage);
                        TempMemPtr bufferPtr = ActualImage.GetBufferPtr(actualImage);
                        unsafe
                        {
                            byte *actualImgH = (byte *)bufferPtr.Ptr;
                            AggMemMx.memcpy((byte *)newBmp.GetPixels(), actualImgH, actualImage.Stride * actualImage.Height);
                            //System.Runtime.InteropServices.Marshal.Copy(
                            //    actualImgBuffer,
                            //    0,
                            //    newBmp.GetPixels(),
                            //    actualImgBuffer.Length);
                        }
                        bufferPtr.Release();
                        newBmp.UnlockPixels();
                    }
                    //newBmp.internalBmp.LockPixels();
                    //byte[] actualImgBuffer = ActualImage.GetBuffer(actualImage);

                    //System.Runtime.InteropServices.Marshal.Copy(
                    //     actualImgBuffer,
                    //     0,
                    //      newBmp.internalBmp.GetPixels(),
                    //      actualImgBuffer.Length);

                    //newBmp.internalBmp.UnlockPixels();
                    //return newBmp;

                    //copy data from acutal buffer to internal representation bitmap
                    //using (MySkBmp bmp = MySkBmp.CopyFrom(actualImage))
                    //{
                    //    _skCanvas.DrawBitmap(bmp.internalBmp, (float)x, (float)y);
                    //}
                }
                break;

                case Agg.PixelFormat.RGB24:
                {
                }
                break;

                case Agg.PixelFormat.GrayScale8:
                {
                }
                break;

                default:
                    throw new NotSupportedException();
                }
            }
        }
コード例 #11
0
        private void PrepareDirect()
        {
            GL.ClearColor(0f, 0f, 0f, 1f);
            CheckErrors();
            GL.Enable(EnableCap.Blend);
            CheckErrors();
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            CheckErrors();

            GL.Enable(EnableCap.Texture2D);
            CheckErrors();
            GL.EnableClientState(ArrayCap.VertexArray);
            CheckErrors();
            GL.EnableClientState(ArrayCap.TextureCoordArray);
            CheckErrors();
            GL.EnableClientState(ArrayCap.ColorArray);
            CheckErrors();
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
            CheckErrors();

            textureIdDirect = GL.GenTexture();
            CheckErrors();
            GL.BindTexture(TextureTarget.Texture2D, textureIdDirect);
            CheckErrors();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            CheckErrors();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            CheckErrors();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            CheckErrors();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
            CheckErrors();

            SKBitmap bitmap = new SKBitmap(100, 100);

            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    bitmap.SetPixel(x, y, SKColors.White);
                }
            }

            bitmap.LockPixels();

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height,
                          0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bitmap.GetAddr(0, 0));
            CheckErrors();
            bitmap.UnlockPixels();

            shaderProgram = GL.CreateProgram();
            CheckErrors();

            int vertexShader = GL.CreateShader(ShaderType.VertexShader);

            CheckErrors();
            GL.ShaderSource(vertexShader, GetStandardVertexShader());
            CheckErrors();
            GL.CompileShader(vertexShader);
            CheckErrors();
            GL.AttachShader(shaderProgram, vertexShader);
            CheckErrors();
            GL.DeleteShader(vertexShader);
            CheckErrors();

            int fragmentShader = GL.CreateShader(ShaderType.FragmentShader);

            CheckErrors();
            GL.ShaderSource(fragmentShader, GetStandardFragmentShader());
            CheckErrors();
            GL.CompileShader(fragmentShader);
            CheckErrors();
            GL.AttachShader(shaderProgram, fragmentShader);
            CheckErrors();
            GL.DeleteShader(fragmentShader);
            CheckErrors();

            GL.LinkProgram(shaderProgram);
            CheckErrors();

            var buf1 = GL.GenBuffer();

            CheckErrors();
            GL.BindBuffer(BufferTarget.ArrayBuffer, buf1);
            CheckErrors();
            GL.VertexPointer(2, VertexPointerType.Float, GLVertex.Size, 0);
            CheckErrors();
            GL.TexCoordPointer(2, TexCoordPointerType.Float, GLVertex.Size, Vector2.SizeInBytes);
            CheckErrors();
            GL.ColorPointer(4, ColorPointerType.Float, GLVertex.Size, Vector2.SizeInBytes * 2);
            CheckErrors();
            var buf2 = GL.GenBuffer();

            CheckErrors();
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, buf2);
            CheckErrors();
        }
コード例 #12
0
 public void UnlockBits(BitmapData bitmapData)
 {
     _image.UnlockPixels();
 }