Пример #1
0
        static unsafe SoftwareBitmap ColorizeMelspectrogram(SoftwareBitmap bwSpectrogram)
        {
            using (BitmapBuffer buffer = bwSpectrogram.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using var reference = buffer.CreateReference();
                IMemoryBufferByteAccess memoryBuffer = reference.As <IMemoryBufferByteAccess>();
                memoryBuffer.GetBuffer(out byte *dataInBytes, out uint capacity);

                // Edit the BGRA Plane
                BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                for (int i = 0; i < bufferLayout.Height; i++)
                {
                    for (int j = 0; j < bufferLayout.Width; j++)
                    {
                        int pixel = bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j;
                        //Lines below can be tweaked for different custom color filters
                        //Blue
                        dataInBytes[pixel + 0] = (byte)((255 - dataInBytes[pixel + 0]) / 2);
                        //Green
                        dataInBytes[pixel + 1] = (byte)(dataInBytes[pixel + 1] / 2);
                        //Red
                        //dataInBytes[pixel + 2] = (byte)(dataInBytes[pixel + 2]);
                        //Alpha - must leave each pixel at max
                        dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255;
                    }
                }
            }
            return(bwSpectrogram);
        }
Пример #2
0
        private static unsafe SoftwareBitmap MatToSoftwareBitmap(this Mat input)
        {
            try
            {
                SoftwareBitmap output = new SoftwareBitmap(BitmapPixelFormat.Bgra8, input.Width, input.Height, BitmapAlphaMode.Premultiplied);

                using (BitmapBuffer Buffer = output.LockBuffer(BitmapBufferAccessMode.ReadWrite))
                    using (Windows.Foundation.IMemoryBufferReference Reference = Buffer.CreateReference())
                    {
                        ((IMemoryBufferByteAccess)Reference).GetBuffer(out byte *DataInBytes, out uint capacity);
                        BitmapPlaneDescription BufferLayout = Buffer.GetPlaneDescription(0);

                        for (int i = 0; i < BufferLayout.Height; i++)
                        {
                            for (int j = 0; j < BufferLayout.Width; j++)
                            {
                                int Index = BufferLayout.StartIndex + (BufferLayout.Stride * i) + (4 * j);

                                DataInBytes[Index]     = input.DataPointer[Index];
                                DataInBytes[Index + 1] = input.DataPointer[Index + 1];
                                DataInBytes[Index + 2] = input.DataPointer[Index + 2];
                                DataInBytes[Index + 3] = input.DataPointer[Index + 3];
                            }
                        }
                    }

                return(output);
            }
            catch
            {
                return(new SoftwareBitmap(BitmapPixelFormat.Bgra8, input.Width, input.Height, BitmapAlphaMode.Premultiplied));
            }
        }
Пример #3
0
        public static unsafe SoftwareBitmap GetImage_()
        {
            var softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, 100, 100, BitmapAlphaMode.Premultiplied);

            using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                    for (int i = 0; i < bufferLayout.Height; i++)
                    {
                        for (int j = 0; j < bufferLayout.Width; j++)
                        {
                            byte value = (byte)((float)j / bufferLayout.Width * 255);
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = value; // Blue
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = value; // Green
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = value; // Red
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = 255;   // Alpha
                        }
                    }
                }
            }

            return(softwareBitmap);
        }
Пример #4
0
        public static unsafe void Mat2SoftwareBitmap(Mat input, SoftwareBitmap output)
        {
            using (BitmapBuffer buffer = output.LockBuffer(BitmapBufferAccessMode.ReadWrite))
            {
                using (var reference = buffer.CreateReference())
                {
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out var dataInBytes, out var capacity);
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);

                    for (int i = 0; i < bufferLayout.Height; i++)
                    {
                        for (int j = 0; j < bufferLayout.Width; j++)
                        {
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] =
                                input.DataPointer[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0];
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] =
                                input.DataPointer[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1];
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] =
                                input.DataPointer[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2];
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] =
                                input.DataPointer[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3];
                        }
                    }
                }
            }
        }
Пример #5
0
        LockedBufferBgra8(
            SoftwareBitmap bitmap,
            BitmapBufferAccessMode access)
        {
            if (bitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8)
            {
                throw new ArgumentException("Invalid BitmapPixelFormat");
            }

            m_buffer     = bitmap.LockBuffer(access);
            m_byteAccess = (IClosableByteAccess)(Object)m_buffer;

            byte *data;
            uint  capacity;

            m_byteAccess.Lock(out data, out capacity);
            m_locked = true;

            Description = m_buffer.GetPlaneDescription(0);
            PixelWidth  = Description.Width;
            PixelHeight = Description.Height;
            Data        = data + Description.StartIndex;

            m_rowIndex             = 0;
            m_rowData              = Data;
            m_rowReadableCapacity  = (access == BitmapBufferAccessMode.Write ? 0 : (uint)Description.Stride);
            m_rowWriteableCapacity = (access == BitmapBufferAccessMode.Read ? 0 : (uint)Description.Stride);
        }
Пример #6
0
        private unsafe void CreateNewSoftwareBitmap()
        {
            // <SnippetCreateNewSoftwareBitmap>
            softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, 100, 100, BitmapAlphaMode.Premultiplied);

            using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                    for (int i = 0; i < bufferLayout.Height; i++)
                    {
                        for (int j = 0; j < bufferLayout.Width; j++)
                        {
                            byte value = (byte)((float)j / bufferLayout.Width * 255);
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = value;
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = value;
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = value;
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255;
                        }
                    }
                }
            }
            // </SnippetCreateNewSoftwareBitmap>
        }
Пример #7
0
        private unsafe void AssignPixelValues(SoftwareBitmap newSoftwareBitmap, SoftwareBitmap softwareBitmap)
        {
            using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Read))
                using (BitmapBuffer newBuffer = newSoftwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
                    using (var reference = buffer.CreateReference())
                        using (var newReference = newBuffer.CreateReference())
                        {
                            byte *dataInBytes;
                            byte *newDataInBytes;
                            uint  capacity;
                            ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);
                            ((IMemoryBufferByteAccess)newReference).GetBuffer(out newDataInBytes, out capacity);

                            // Fill-in the BGRA plane
                            BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);

                            for (int i = 0; i < bufferLayout.Height; i++)
                            {
                                for (int j = 0; j < bufferLayout.Width; j++)
                                {
                                    int pixelIndex = bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j;
                                    if (newDataInBytes[pixelIndex + 3] != 0)
                                    {
                                        newDataInBytes[pixelIndex + 0] = dataInBytes[pixelIndex + 0];
                                        newDataInBytes[pixelIndex + 1] = dataInBytes[pixelIndex + 1];
                                        newDataInBytes[pixelIndex + 2] = dataInBytes[pixelIndex + 2];
                                    }
                                }
                            }
                        }
        }
Пример #8
0
        private unsafe void ChangeRandomBgColorPixel()
        {
            using (BitmapBuffer buffer = randomBgSoftwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                    double imgWidth  = bufferLayout.Width;
                    double imgHeight = bufferLayout.Height;

                    for (int row = 0; row < imgHeight; row++)
                    {
                        for (int col = 0; col < imgWidth; col++)
                        {
                            double   hue   = col * (360 / imgWidth);
                            HSVColor hsv   = new HSVColor(hue, 1.0, 1.0);
                            Color    color = hsv.GetRGB();

                            int pixelIndex = bufferLayout.Stride * row + 4 * col;
                            if (dataInBytes[pixelIndex + 3] != 0)
                            {
                                dataInBytes[pixelIndex + 0] = (byte)color.B;
                                dataInBytes[pixelIndex + 1] = (byte)color.G;
                                dataInBytes[pixelIndex + 2] = (byte)color.R;
                            }
                        }
                    }
                }
            }
        }
Пример #9
0
        public unsafe object Detect(SoftwareBitmap image)
        {
            using (BitmapBuffer buffer = image.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte* dataInBytes;
                    uint capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                    for (int i = 0; i < bufferLayout.Height; i++)
                    {
                        for (int j = 0; j < bufferLayout.Width; j++)
                        {
                            int b = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] - Color.B;
                            int g = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] - Color.G;
                            int r = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] - Color.R;

                            if (b * b + g * g + r * r < Delta * Delta)
                            {
                                dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = 0;
                                dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = 0;
                                dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = 255;
                                dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255;
                            }
                        }
                    }
                }
            }

            return null;
        }
Пример #10
0
        public unsafe byte[] GenerateSolidColor(float size, Color color, string cacheName)
        {
            var existing = GetExisting(cacheName);

            if (existing != null)
            {
                return(existing);
            }

            var bmp = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)size, (int)size);

            using (var buffer = bmp.LockBuffer(BitmapBufferAccessMode.Write)) {
                using (var reference = buffer.CreateReference()) {
                    byte *dataInBytes;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                    for (int i = 0; i < bufferLayout.Height; i++)
                    {
                        for (int j = 0; j < bufferLayout.Width; j++)
                        {
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = color.R;
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = color.G;
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = color.B;
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = color.A;
                        }
                    }
                }
            }

            return(Put(cacheName, bmp).Result);
        }
Пример #11
0
        public unsafe void CreateGraphicFile()
        {
            softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, ImageWidth, ImageHeight, BitmapAlphaMode.Ignore);

            using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                    for (int i = 0; i < bufferLayout.Width; i++)
                    {
                        for (int j = 0; j < bufferLayout.Height; j++)
                        {
                            Color tempColor = waveSamples[i].CheckArea(j, ImageHeight) ? ForegroundColor : BackgroundColor;
                            //Blue
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * j + 4 * i + 0] = (byte)tempColor.B;
                            //Green
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * j + 4 * i + 1] = (byte)tempColor.G;
                            //Red
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * j + 4 * i + 2] = (byte)tempColor.R;
                            //Alpha
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * j + 4 * i + 3] = (byte)tempColor.A;
                        }
                    }
                }
            }
        }
Пример #12
0
        private unsafe void SoftwareBitmapChangeColorRed(SoftwareBitmap softwareBitmap)
        {
            using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);

                    for (int i = 0; i < bufferLayout.Height; i++)
                    {
                        for (int j = 0; j < bufferLayout.Width; j++)
                        {
                            int pixelIndex = bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j;
                            if (dataInBytes[pixelIndex + 3] != 0)
                            {
                                dataInBytes[pixelIndex + 0] = 0;
                                dataInBytes[pixelIndex + 1] = 0;
                                dataInBytes[pixelIndex + 2] = 255;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Copies data from an RGB buffer to a software bitmap.
        /// </summary>
        /// <param name="rgb24Buffer">The RGB buffer to copy from.</param>
        /// <param name="sbmp">The software bitmap to copy the data to.</param>
        private void SetBitmapData(byte[] buffer, SoftwareBitmap sbmp, VideoPixelFormatsEnum pixelFormat)
        {
            using (BitmapBuffer bmpBuffer = sbmp.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = bmpBuffer.CreateReference())
                {
                    unsafe
                    {
                        byte *dataInBytes;
                        uint  capacity;
                        ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);
                        int posn = 0;

                        // Fill-in the RGBA plane
                        BitmapPlaneDescription bufferLayout = bmpBuffer.GetPlaneDescription(0);
                        for (int i = 0; i < bufferLayout.Height; i++)
                        {
                            for (int j = 0; j < bufferLayout.Width; j++)
                            {
                                // NOTE: Same as for System.Drawing.Bitmap pixel formats that have "rgb" in their name, such as
                                // BitmapPixelFormat.Rgba8, use a buffer format of BGR. Many issues on StackOverflow regarding this,
                                // e.g. https://stackoverflow.com/questions/5106505/converting-gdi-pixelformat-to-wpf-pixelformat.
                                // Notice the switch of the Blue and Red pixels below.
                                if (pixelFormat == VideoPixelFormatsEnum.Rgb)
                                {
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = buffer[posn++];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = buffer[posn++];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = buffer[posn++];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255;
                                }
                                else if (pixelFormat == VideoPixelFormatsEnum.Bgr)
                                {
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = buffer[posn++];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = buffer[posn++];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = buffer[posn++];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255;
                                }
                                //if (pixelFormat == VideoPixelFormatsEnum.Rgba)
                                //{
                                //    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = buffer[posn++];
                                //    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = buffer[posn++];
                                //    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = buffer[posn++];
                                //    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = buffer[posn++];
                                //}
                                else if (pixelFormat == VideoPixelFormatsEnum.Bgra)
                                {
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = buffer[posn++];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = buffer[posn++];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = buffer[posn++];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = buffer[posn++];
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #14
0
        public static unsafe SoftwareBitmap getImageAs8bitsBitmap(ref ushort[] data, uint height, uint width, int colorDepth, object[] curve, ref int[] value, bool histo, bool bgr)
        {
            SoftwareBitmap image = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)width, (int)height, BitmapAlphaMode.Ignore);

            using (BitmapBuffer buffer = image.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte *tempByteArray;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out tempByteArray, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                    //calculte diff between colordepth and 8
                    int diff = (colorDepth) - 8;

                    for (int i = 0; i < bufferLayout.Width * bufferLayout.Height; i++)
                    {
                        //get the pixel
                        ushort red   = (ushort)(data[(i * 3)] >> diff),
                               green = (ushort)(data[(i * 3) + 1] >> diff),
                               blue  = (ushort)(data[(i * 3) + 2] >> (diff));
                        if (blue > 255)
                        {
                            blue = 255;
                        }
                        if (red > 255)
                        {
                            red = 255;
                        }
                        if (green > 255)
                        {
                            green = 255;
                        }
                        if (histo)
                        {
                            value[(ushort)((red + green + blue) / 3)]++;
                        }
                        if (bgr)
                        {
                            tempByteArray[bufferLayout.StartIndex + (i * 4)]     = (byte)blue;
                            tempByteArray[bufferLayout.StartIndex + (i * 4) + 1] = (byte)green;
                            tempByteArray[bufferLayout.StartIndex + (i * 4) + 2] = (byte)red;
                        }
                        else
                        {
                            tempByteArray[bufferLayout.StartIndex + (i * 4)]     = (byte)red;
                            tempByteArray[bufferLayout.StartIndex + (i * 4) + 1] = (byte)green;
                            tempByteArray[bufferLayout.StartIndex + (i * 4) + 2] = (byte)blue;
                        }
                        tempByteArray[bufferLayout.StartIndex + (i * 4) + 3] = 255;
                    }
                }
            }
            return(image);
        }
Пример #15
0
        private unsafe SoftwareBitmap Blur_image(SoftwareBitmap softwareBitmape, int blurRatio)
        {
            var softwareBitmap = SoftwareBitmap.Convert(softwareBitmape, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

            using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);



                    for (int i = 0; i < bufferLayout.Height; i++)
                    {
                        for (int j = 0; j < bufferLayout.Width; j++)
                        {
                            float avg1 = 0;
                            float avg2 = 0;
                            float avg3 = 0;
                            float avg4 = 0;
                            float l    = 0;

                            for (int k = j - blurRatio; k <= j + blurRatio; k++)
                            {
                                for (int h = i - blurRatio; h <= i + blurRatio; h++)
                                {
                                    if (k >= 0 && h >= 0 && k < bufferLayout.Width && h < bufferLayout.Height)
                                    {
                                        avg1 += dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 0];
                                        avg2 += dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 1];
                                        avg3 += dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 2];
                                        avg4 += dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 3];
                                        var test = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 0];
                                        l++;
                                    }
                                }
                            }



                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = (byte)(avg1 / l);
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = (byte)(avg2 / l);
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = (byte)(avg3 / l);
                            dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)(avg4 / l);
                        }
                    }
                }
            }

            return(softwareBitmap);
        }
Пример #16
0
        // Graphics / Buffer helper functions

        public unsafe int[,] GetColorDistribution(SoftwareBitmap haystack, Color needle, int[] tolerance, int sparcity = 2)
        {
            int w = haystack.PixelWidth;
            int h = haystack.PixelHeight;
            int r = needle.R;
            int g = needle.G;
            int b = needle.B;

            int[,] dA = new int[w * h, 2];
            int dAPointer = 0;

            BitmapBuffer           buffer       = haystack.LockBuffer(BitmapBufferAccessMode.Read);
            BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
            IMemoryBufferReference reference    = buffer.CreateReference();

            ((IMemoryBufferByteAccess)reference).GetBuffer(out byte *bufferData, out uint capacity);



            int pos;

            for (int x = 0; x < w; x += sparcity)
            {
                for (int y = 0; y < h; y += sparcity)
                {
                    pos = bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x;
                    int rC = bufferData[pos + 2];
                    int gC = bufferData[pos + 1];
                    int bC = bufferData[pos];
                    if (Math.Abs(rC - r) <= tolerance[0] && Math.Abs(gC - g) <= tolerance[1] && Math.Abs(bC - b) <= tolerance[2])
                    {
                        dA[dAPointer, 0] = x;
                        dA[dAPointer, 1] = y;
                        dAPointer++;
                    }
                }
            }
            dAPointer--;

            reference.Dispose();
            buffer.Dispose();

            int[,] distributionArray = new int[dAPointer + 1, 2];

            for (int p = dAPointer; p >= 0; p--)
            {
                distributionArray[p, 0] = dA[p, 0];
                distributionArray[p, 1] = dA[p, 1];
            }

            return(distributionArray);
        }
Пример #17
0
        private unsafe SoftwareBitmap Edit_image_Middle(SoftwareBitmap softwareBitmape, int PixelationRatio)
        {
            var softwareBitmap = SoftwareBitmap.Convert(softwareBitmape, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

            using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);



                    for (int i = 0; i < bufferLayout.Height + PixelationRatio; i = i + PixelationRatio * 2)
                    {
                        for (int j = 0; j < bufferLayout.Width + PixelationRatio; j = j + PixelationRatio * 2)
                        {
                            for (int k = j - PixelationRatio; k <= j + PixelationRatio; k++)
                            {
                                for (int h = i - PixelationRatio; h <= i + PixelationRatio; h++)
                                {
                                    if (k >= 0 && h >= 0 && k < bufferLayout.Width && h < bufferLayout.Height)
                                    {
                                        var  iIndex = i >= bufferLayout.Height ? bufferLayout.Height - 1 : i;
                                        var  jIndex = j >= bufferLayout.Width ? bufferLayout.Width - 1 : j;
                                        byte value1 = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * iIndex + 4 * jIndex + 0];
                                        byte value2 = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * iIndex + 4 * jIndex + 1];
                                        byte value3 = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * iIndex + 4 * jIndex + 2];
                                        byte value4 = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * iIndex + 4 * jIndex + 3];

                                        dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 0] = value1;
                                        dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 1] = value2;
                                        dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 2] = value3;
                                        dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * h + 4 * k + 3] = value4;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(softwareBitmap);
        }
Пример #18
0
        public static unsafe SoftwareBitmap GetImage(Document doc)
        {
            var softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Gray8, 100, 100);

            using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    // Fill-in the Gray plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                    //doc.test(reference, 100, 100, bufferLayout.StartIndex, bufferLayout.Stride);
                }
            }

            return(SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied));
        }
Пример #19
0
        public unsafe void ProcessFrame(ProcessVideoFrameContext context)
        {
            frameCount++;
            Debug.WriteLine("Frame count: " + frameCount);
            using (BitmapBuffer buffer = context.InputFrame.SoftwareBitmap.LockBuffer(BitmapBufferAccessMode.Read))
                using (BitmapBuffer targetBuffer = context.OutputFrame.SoftwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
                {
                    using (var reference = buffer.CreateReference())
                        using (var targetReference = targetBuffer.CreateReference())
                        {
                            byte *dataInBytes;
                            uint  capacity;
                            ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                            byte *targetDataInBytes;
                            uint  targetCapacity;
                            ((IMemoryBufferByteAccess)targetReference).GetBuffer(out targetDataInBytes, out targetCapacity);

                            var fadeValue = FadeValue;

                            // Fill-in the BGRA plane
                            BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                            for (int i = 0; i < bufferLayout.Height; i++)
                            {
                                for (int j = 0; j < bufferLayout.Width; j++)
                                {
                                    byte value = (byte)((float)j / bufferLayout.Width * 255);

                                    int bytesPerPixel = 4;
                                    if (encodingProperties.Subtype != "ARGB32")
                                    {
                                        // If you support other encodings, adjust index into the buffer accordingly
                                    }


                                    int idx = bufferLayout.StartIndex + bufferLayout.Stride * i + bytesPerPixel * j;

                                    targetDataInBytes[idx + 0] = (byte)(fadeValue * (float)dataInBytes[idx + 0]);

                                    //targetDataInBytes[idx + 1] = (byte)(fadeValue * (float)dataInBytes[idx + 1]);
                                    //targetDataInBytes[idx + 2] = (byte)(fadeValue * (float)dataInBytes[idx + 2]);
                                    //targetDataInBytes[idx + 3] = dataInBytes[idx + 3];
                                }
                            }
                        }
                }
        }
Пример #20
0
        private unsafe bool[,] GetBluePixelArray(SoftwareBitmap softwareBitmap)
        {
            int widthPixels;
            int heightPixels;

            bool[,] pixelBoolArray;

            using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);

                    widthPixels    = bufferLayout.Width;
                    heightPixels   = bufferLayout.Height;
                    pixelBoolArray = new bool[heightPixels, widthPixels];

                    for (int y = 0; y < heightPixels; y++)
                    {
                        for (int x = 0; x < widthPixels; x++)
                        {
                            int pixelIndex = bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x;

                            if (dataInBytes[pixelIndex + 0] == 255 &&
                                dataInBytes[pixelIndex + 1] == 0 &&
                                dataInBytes[pixelIndex + 2] == 0)
                            {
                                pixelBoolArray[y, x] = true;
                            }
                            else
                            {
                                pixelBoolArray[y, x] = false;
                            }
                        }
                    }
                }
            }

            return(pixelBoolArray);
        }
Пример #21
0
        private unsafe SoftwareBitmap CropImageByRect(SoftwareBitmap inputBitmap, FaceRectangle rect)
        {
            var outputBitmap = new SoftwareBitmap(inputBitmap.BitmapPixelFormat, rect.Width, rect.Height, inputBitmap.BitmapAlphaMode);

            using (BitmapBuffer inputBuffer = inputBitmap.LockBuffer(BitmapBufferAccessMode.Read))
            {
                using (BitmapBuffer outputBuffer = outputBitmap.LockBuffer(BitmapBufferAccessMode.Write))
                {
                    using (var inputReference = inputBuffer.CreateReference())
                    {
                        byte *dataInBytes;
                        uint  dataInCapacity;
                        ((IMemoryBufferByteAccess)inputReference).GetBuffer(out dataInBytes, out dataInCapacity);
                        BitmapPlaneDescription inputBufferLayout = inputBuffer.GetPlaneDescription(0);

                        using (var outputReference = outputBuffer.CreateReference())
                        {
                            byte *dataOutBytes;
                            uint  dataOutCapacity;
                            ((IMemoryBufferByteAccess)outputReference).GetBuffer(out dataOutBytes, out dataOutCapacity);

                            // Fill-in the BGRA plane
                            BitmapPlaneDescription outputBufferLayout = outputBuffer.GetPlaneDescription(0);
                            for (int i = 0; i < outputBufferLayout.Height; i++)
                            {
                                for (int j = 0; j < outputBufferLayout.Width; j++)
                                {
                                    dataOutBytes[outputBufferLayout.StartIndex + outputBufferLayout.Stride * i + 4 * j + 0] =
                                        dataInBytes[inputBufferLayout.StartIndex + inputBufferLayout.Stride * (rect.Top + i) + 4 * (rect.Left + j) + 0];
                                    dataOutBytes[outputBufferLayout.StartIndex + outputBufferLayout.Stride * i + 4 * j + 1] =
                                        dataInBytes[inputBufferLayout.StartIndex + inputBufferLayout.Stride * (rect.Top + i) + 4 * (rect.Left + j) + 1];
                                    dataOutBytes[outputBufferLayout.StartIndex + outputBufferLayout.Stride * i + 4 * j + 2] =
                                        dataInBytes[inputBufferLayout.StartIndex + inputBufferLayout.Stride * (rect.Top + i) + 4 * (rect.Left + j) + 2];
                                    dataOutBytes[outputBufferLayout.StartIndex + outputBufferLayout.Stride * i + 4 * j + 3] =
                                        dataInBytes[inputBufferLayout.StartIndex + inputBufferLayout.Stride * (rect.Top + i) + 4 * (rect.Left + j) + 3];
                                }
                            }
                        }
                    }
                }
            }

            return(outputBitmap);
        }
Пример #22
0
        private unsafe void GetHasPixelArray()
        {
            SoftwareBitmap softwareBitmap = g_CurrentImageInfo.SoftwareBitmap;
            int            widthPixels;
            int            heightPixels;

            bool[,] hasPixelArray;

            using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacity;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);

                    widthPixels   = bufferLayout.Width;
                    heightPixels  = bufferLayout.Height;
                    hasPixelArray = new bool[heightPixels, widthPixels];

                    for (int y = 0; y < heightPixels; y++)
                    {
                        for (int x = 0; x < widthPixels; x++)
                        {
                            int pixelIndex = bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x;

                            if (dataInBytes[pixelIndex + 3] != 0)
                            {
                                hasPixelArray[y, x] = true;
                            }
                            else
                            {
                                hasPixelArray[y, x] = false;
                            }
                        }
                    }
                }
            }

            g_CurrentImageInfo.HasPixelArray = hasPixelArray;
        }
Пример #23
0
        public unsafe void SetPixelColor(SoftwareBitmap swBmp, int x, int y, Color color)
        {
            BitmapBuffer           buffer       = swBmp.LockBuffer(BitmapBufferAccessMode.Write);
            BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);

            if (x >= 0 && x < bufferLayout.Width && y >= 0 && y < bufferLayout.Height)
            {
                using (IMemoryBufferReference reference = buffer.CreateReference())
                {
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out byte *bufferData, out uint capacity);

                    bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 0] = color.B;
                    bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 1] = color.G;
                    bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 2] = color.R;
                    bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 3] = color.A;
                }
            }
            buffer.Dispose();
        }
Пример #24
0
        static unsafe public SoftwareBitmap Crop(SoftwareBitmap src, int left, int top, int width, int height)
        {
            if (left < 0 || left + width > src.PixelWidth || width < 1)
            {
                return(null);
            }
            if (top < 0 || top + height > src.PixelHeight || height < 1)
            {
                return(null);
            }
            var dst = new SoftwareBitmap(BitmapPixelFormat.Bgra8, width, height);

            {
                uint  capacity;
                byte *dataInBytes;
                byte *dstDataInBytes;
                using (var buffer = src.LockBuffer(BitmapBufferAccessMode.Read))
                    using (var dstBuffer = dst.LockBuffer(BitmapBufferAccessMode.Write))
                        using (var read = buffer.CreateReference())
                            using (var write = dstBuffer.CreateReference())
                            {
                                {
                                    ((IMemoryBufferByteAccess)read).GetBuffer(out dataInBytes, out capacity);
                                    ((IMemoryBufferByteAccess)write).GetBuffer(out dstDataInBytes, out capacity);
                                    BitmapPlaneDescription rBufferLayout = buffer.GetPlaneDescription(0);
                                    BitmapPlaneDescription wBufferLayout = dstBuffer.GetPlaneDescription(0);

                                    for (int y = 0; y < height; y++)
                                    {
                                        int   srcOffset = rBufferLayout.StartIndex + rBufferLayout.Stride * (top + y) + 4 * left;
                                        int   dstOffset = wBufferLayout.StartIndex + wBufferLayout.Stride * y;
                                        byte *_src      = (byte *)dataInBytes + srcOffset;
                                        byte *_dst      = (byte *)dstDataInBytes + dstOffset;
                                        for (int i = 0; i < width * 4; i++)
                                        {
                                            _dst[i] = _src[i];
                                        }
                                    }
                                }
                            }
            }
            return(dst);
        }
Пример #25
0
        protected override RawImage decodeRawInternal()
        {
            mRaw.colorDepth = 8;
            mRaw.cpp        = 3;
            mRaw.bpp        = 8;
            var decoder = BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, mFile.BaseStream.AsRandomAccessStream()).AsTask();

            decoder.Wait();

            var bitmapasync = decoder.Result.GetSoftwareBitmapAsync().AsTask();

            bitmapasync.Wait();
            var image = bitmapasync.Result;

            using (BitmapBuffer buffer = image.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                    mRaw.dim = new iPoint2D(bufferLayout.Width, bufferLayout.Height);
                    mRaw.Init();
                    unsafe
                    {
                        ((IMemoryBufferByteAccess)reference).GetBuffer(out var temp, out uint capacity);

                        for (int y = 0; y < mRaw.dim.y; y++)
                        {
                            int realY   = y * mRaw.dim.x * 3;
                            int bufferY = y * mRaw.dim.x * 4 + +bufferLayout.StartIndex;
                            for (int x = 0; x < mRaw.dim.x; x++)
                            {
                                int realPix   = realY + (3 * x);
                                int bufferPix = bufferY + (4 * x);
                                mRaw.rawData[realPix]     = temp[bufferPix + 2];
                                mRaw.rawData[realPix + 1] = temp[bufferPix + 1];
                                mRaw.rawData[realPix + 2] = temp[bufferPix];
                            }
                        }
                    }
                }
            }
            return(mRaw);
        }
Пример #26
0
        static public SoftwareBitmap Rotate270(SoftwareBitmap src)
        {
            var imageData    = new byte[src.PixelWidth * src.PixelHeight * 4];
            int rotateHeight = src.PixelWidth;
            int rotateWidth  = src.PixelHeight;
            var dst          = new SoftwareBitmap(BitmapPixelFormat.Bgra8, rotateWidth, rotateHeight);

            unsafe
            {
                uint  capacity;
                byte *dataInBytes;
                byte *dstDataInBytes;
                using (var buffer = src.LockBuffer(BitmapBufferAccessMode.Read))
                    using (var dstBuffer = dst.LockBuffer(BitmapBufferAccessMode.Write))
                        using (var read = buffer.CreateReference())
                            using (var write = dstBuffer.CreateReference())
                            {
                                {
                                    ((IMemoryBufferByteAccess)read).GetBuffer(out dataInBytes, out capacity);
                                    ((IMemoryBufferByteAccess)write).GetBuffer(out dstDataInBytes, out capacity);
                                    BitmapPlaneDescription rBufferLayout = buffer.GetPlaneDescription(0);
                                    BitmapPlaneDescription wBufferLayout = dstBuffer.GetPlaneDescription(0);
                                    int hpw = (src.PixelWidth - src.PixelHeight) / 2;
                                    int wdh = (src.PixelWidth + src.PixelHeight) / 2;
                                    for (int i = 0; i < rBufferLayout.Height; i++)
                                    {
                                        for (int j = 0; j < rBufferLayout.Width; j++)
                                        {
                                            int dstOffset = wBufferLayout.StartIndex + wBufferLayout.Stride * (wBufferLayout.Height - 1 - j) + 4 * i;
                                            int srcOffset = rBufferLayout.StartIndex + rBufferLayout.Stride * i + 4 * j;
                                            dstDataInBytes[dstOffset]     = dataInBytes[srcOffset];
                                            dstDataInBytes[dstOffset + 1] = dataInBytes[srcOffset + 1];
                                            dstDataInBytes[dstOffset + 2] = dataInBytes[srcOffset + 2];
                                            dstDataInBytes[dstOffset + 3] = dataInBytes[srcOffset + 3];
                                        }
                                    }
                                }
                            }
            }
            return(dst);
        }
Пример #27
0
        public unsafe IResult Detect(SoftwareBitmap image)
        {
            CounterResult result = new CounterResult()
            {
                Count = 0,
                Image = new SoftwareBitmap(image.BitmapPixelFormat, image.PixelWidth, image.PixelHeight)
            };

            image.CopyTo(result.Image);

            using (BitmapBuffer buffer = result.Image.LockBuffer(BitmapBufferAccessMode.Write))
            {
                using (var reference = buffer.CreateReference())
                {
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out byte *dataInBytes, out uint capacity);

                    // Fill-in the BGRA plane
                    BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                    for (int i = 0; i < bufferLayout.Height; i++)
                    {
                        for (int j = 0; j < bufferLayout.Width; j++)
                        {
                            int b = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] - Color.B;
                            int g = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] - Color.G;
                            int r = dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] - Color.R;

                            if (b * b + g * g + r * r < Delta * Delta)
                            {
                                result.Count++;
                                dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = 0;
                                dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = 0;
                                dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = 255;
                                dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255;
                            }
                        }
                    }
                }
            }

            return(result);
        }
Пример #28
0
        public unsafe Color GetPixelColor(SoftwareBitmap swBmp, int x, int y)
        {
            BitmapBuffer           buffer       = swBmp.LockBuffer(BitmapBufferAccessMode.Read);
            BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
            Color color = new Color();

            if (x >= 0 && x < bufferLayout.Width && y >= 0 && y < bufferLayout.Height)
            {
                using (IMemoryBufferReference reference = buffer.CreateReference())
                {
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out byte *bufferData, out uint capacity);

                    color.B = bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 0];
                    color.G = bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 1];
                    color.R = bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 2];
                    color.A = bufferData[bufferLayout.StartIndex + bufferLayout.Stride * y + 4 * x + 3];
                }
            }
            buffer.Dispose();
            return(color);
        }
Пример #29
0
        private void SubscriptionHandler(sensor_msgs.Image message)
        {
            var ignore = CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                var softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, message.width, message.height, BitmapAlphaMode.Ignore);

                unsafe
                {
                    using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
                    {
                        using (var reference = buffer.CreateReference())
                        {
                            byte *dataInBytes;
                            uint capacity;
                            ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);

                            // Fill-in the BGRA plane
                            BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
                            for (int y = 0; y < bufferLayout.Height; y++)
                            {
                                uint xoffset = 0;
                                for (int x = 0; x < bufferLayout.Stride; x += 4)
                                {
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * y + x + 0] = message.data[message.step * y + xoffset + 0];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * y + x + 1] = message.data[message.step * y + xoffset + 1];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * y + x + 2] = message.data[message.step * y + xoffset + 2];
                                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * y + x + 3] = (byte)255;

                                    xoffset += 3;
                                }
                            }
                        }
                    }
                }

                await MLView.SetBitmapAsync(softwareBitmap);

                NotifyPropertyChanged("MLView");
            });
        }
Пример #30
0
        private unsafe void Tile()
        {
            using (BitmapBuffer sourceBuffer = g_BaseSB.LockBuffer(BitmapBufferAccessMode.Read))
                using (BitmapBuffer targetBuffer = g_TileSB.LockBuffer(BitmapBufferAccessMode.Write))
                    using (var reference = sourceBuffer.CreateReference())
                        using (var newReference = targetBuffer.CreateReference())
                        {
                            byte *dataInBytes;
                            byte *newDataInBytes;
                            uint  capacity;
                            ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);
                            ((IMemoryBufferByteAccess)newReference).GetBuffer(out newDataInBytes, out capacity);

                            // Fill-in the BGRA plane
                            BitmapPlaneDescription sourceBufferLayout = sourceBuffer.GetPlaneDescription(0);
                            BitmapPlaneDescription targetBufferLayout = targetBuffer.GetPlaneDescription(0);
                            int baseWidth  = g_BaseSB.PixelWidth;
                            int baseHeight = g_BaseSB.PixelHeight;
                            int tileWidth  = targetBufferLayout.Width;
                            int tileHeight = targetBufferLayout.Height;

                            for (int i = 0; i < tileHeight; i++)
                            {
                                for (int j = 0; j < tileWidth; j++)
                                {
                                    int wSourceIndex = i % baseWidth;
                                    int hSourceIndex = j % baseHeight;

                                    int sourcePixelIndex = sourceBufferLayout.StartIndex + sourceBufferLayout.Stride * wSourceIndex + 4 * hSourceIndex;
                                    int targetPixelIndex = targetBufferLayout.StartIndex + targetBufferLayout.Stride * i + 4 * j;

                                    newDataInBytes[targetPixelIndex + 0] = dataInBytes[sourcePixelIndex + 0];
                                    newDataInBytes[targetPixelIndex + 1] = dataInBytes[sourcePixelIndex + 1];
                                    newDataInBytes[targetPixelIndex + 2] = dataInBytes[sourcePixelIndex + 2];
                                    newDataInBytes[targetPixelIndex + 3] = dataInBytes[sourcePixelIndex + 3];
                                }
                            }
                        }
        }