Exemplo n.º 1
0
        private static byte[] CreateRGBByteArray(int width, int height)
        {
            var dv = new DrawingVisual();

            TextOptions.SetTextRenderingMode(dv, TextRenderingMode.Aliased);
            TextOptions.SetTextFormattingMode(dv, TextFormattingMode.Ideal);

            using (var dc = dv.RenderOpen())
            {
                dc.DrawRectangle(new SolidColorBrush(Colors.White), null, new Rect(0, 0, width, height));
                var radius = width / 2;
                dc.DrawEllipse(
                    new SolidColorBrush(Colors.Crimson),
                    null,
                    new System.Windows.Point(0.5 * width, 0.5 * height),
                    radius,
                    radius);
            }
            RenderTargetBitmap rtb = new RenderTargetBitmap(
                width,
                height,
                96,
                96,
                PixelFormats.Pbgra32);

            rtb.Render(dv);

            var converted = new FormatConvertedBitmap(rtb, PixelFormats.Bgr24, null, 0);

            var imageBytes = new byte[rtb.PixelHeight * rtb.PixelWidth * 3];

            converted.CopyPixels(imageBytes, rtb.PixelWidth * 3, 0);

            return(imageBytes);
        }
Exemplo n.º 2
0
        public ImageData ReadBmp(IBinaryStream file, BmpMetaData info, byte[] alpha, int alp_stride)
        {
            var decoder = new BmpBitmapDecoder(file.AsStream,
                                               BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
            BitmapSource bitmap = decoder.Frames[0];

            bitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Bgr32, null, 0);
            int dst_stride = (int)info.Width * 4;
            var pixels     = new byte[(int)info.Height * dst_stride];

            bitmap.CopyPixels(pixels, dst_stride, 0);
            int dst = 0;
            int src = 0;

            for (int y = (int)info.Height; y > 0; --y)
            {
                int a_src = src;
                for (int x = 3; x < dst_stride; x += 4)
                {
                    pixels[dst + x] = alpha[a_src++];
                }
                dst += dst_stride;
                src += alp_stride;
            }
            return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels, dst_stride));
        }
Exemplo n.º 3
0
        public override Stream LoadPixelDataStream(Image source)
        {
            using (Bitmap imageBitmap = new Bitmap(source))
            {
                var bitmap = ImageLibraryImageFormat.ConvertBitmap(imageBitmap);
                FormatConvertedBitmap formattedBitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Bgra32, null, 100);
                byte[] bytes = new byte[bitmap.PixelWidth * bitmap.PixelHeight * BytesPerPixel];
                formattedBitmap.CopyPixels(new Int32Rect(0, 0, formattedBitmap.PixelWidth, formattedBitmap.PixelHeight), bytes, formattedBitmap.PixelWidth * BytesPerPixel, 0);

                for (int i = 0; i < bytes.Length; i += 4)
                {
                    byte b = bytes[i];
                    byte g = bytes[i + 1];
                    byte r = bytes[i + 2];
                    byte a = bytes[i + 3];

                    bytes[i]     = r;
                    bytes[i + 1] = g;
                    bytes[i + 2] = b;
                    bytes[i + 3] = a;
                }

                return(new MemoryStream(bytes));
            }
        }
Exemplo n.º 4
0
        //returns bitmap snapshot of selected area
        //this code takes a BitmapImage and converts it to a Bitmap so it can be put on the clipboard
        internal override object GetBitmapFromBitmapSource(object source)
        {
            BitmapSource contentImage = (BitmapSource)source;
            int          imageWidth   = (int)contentImage.Width;
            int          imageHeight  = (int)contentImage.Height;

            Bitmap bitmapFinal = new Bitmap(
                imageWidth,
                imageHeight,
                System.Drawing.Imaging.PixelFormat.Format32bppRgb);

            BitmapData bmData = bitmapFinal.LockBits(
                new Rectangle(0, 0, imageWidth, imageHeight),
                ImageLockMode.WriteOnly,
                System.Drawing.Imaging.PixelFormat.Format32bppRgb);

            FormatConvertedBitmap formatConverter = new FormatConvertedBitmap();

            formatConverter.BeginInit();
            formatConverter.Source            = contentImage;
            formatConverter.DestinationFormat = System.Windows.Media.PixelFormats.Bgr32;
            formatConverter.EndInit();

            formatConverter.CopyPixels(
                new Int32Rect(0, 0, imageWidth, imageHeight),
                bmData.Scan0,
                bmData.Stride * (bmData.Height - 1) + (bmData.Width * 4),
                bmData.Stride);

            bitmapFinal.UnlockBits(bmData);

            return(bitmapFinal);
        }
        private byte[] DecodeImage(BitmapImage image)
        {
            if (_lastDecodedImage == image)
            {
                return(_bgrRawPixelCache);
            }

            FormatConvertedBitmap convertedBitmap = new FormatConvertedBitmap(image, PixelFormats.Bgr24, null, 0);
            int bytesPerPixel     = (convertedBitmap.Format.BitsPerPixel + 7) / 8;
            int requiredCacheSize = bytesPerPixel * convertedBitmap.PixelWidth * convertedBitmap.PixelHeight;

            if (_bgrRawPixelCache == null || _bgrRawPixelCacheSize != requiredCacheSize)
            {
                _bgrRawPixelCache     = new byte[bytesPerPixel * convertedBitmap.PixelWidth * convertedBitmap.PixelHeight];
                _bgrRawPixelCacheSize = requiredCacheSize;
            }

            Int32Rect rect = new Int32Rect(0, 0, convertedBitmap.PixelWidth, convertedBitmap.PixelHeight);

            convertedBitmap.CopyPixels(rect, _bgrRawPixelCache, bytesPerPixel * convertedBitmap.PixelWidth, 0);

            _lastDecodedImage = image;

            return(_bgrRawPixelCache);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="bitmap">Source image.</param>
        public unsafe RGBImage(BitmapSource bitmap)
        {
            int rows    = bitmap.PixelHeight;
            int columns = bitmap.PixelWidth;

            _handler = new ByteArrayHandler(rows, columns, 3);

            FormatConvertedBitmap bmp = new FormatConvertedBitmap();

            bmp.BeginInit();
            bmp.Source            = bitmap;
            bmp.DestinationFormat = PixelFormats.Rgb24;
            bmp.EndInit();
            byte[] pixels = new byte[rows * columns * 3];
            bmp.CopyPixels(pixels, columns * 3, 0);
            fixed(byte *src = pixels, dst = _handler.RawArray)
            {
                byte *srcPtr = src;
                byte *dstPtr = dst;
                int   count  = pixels.Length;

                while (count-- > 0)
                {
                    *dstPtr++ = *srcPtr++;
                }
            }

            pixels = null;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Converts a <see cref="BitmapSource"/> object into a GDI+ <see cref="Bitmap"/> object.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static unsafe Bitmap ToBitmap(this BitmapSource source)
        {
            FormatConvertedBitmap bmp = new FormatConvertedBitmap();

            bmp.BeginInit();
            bmp.Source            = source;
            bmp.DestinationFormat = PixelFormats.Bgra32;
            bmp.EndInit();
            int width  = bmp.PixelWidth;
            int height = bmp.PixelHeight;
            int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);

            byte[] bits = new byte[height * stride];

            bmp.CopyPixels(bits, stride, 0);

            Bitmap bitmap = null;

            fixed(byte *pBits = bits)
            {
                IntPtr ptr = new IntPtr(pBits);

                bitmap = new Bitmap(
                    width,
                    height,
                    stride,
                    System.Drawing.Imaging.PixelFormat.Format32bppArgb,
                    ptr);
            }

            bits = null;
            bmp  = null;
            return(bitmap);
        }
Exemplo n.º 8
0
        private void Button_Click_9(object sender, RoutedEventArgs e)
        {
            //クリップボードから画像を貼り付け
            var source = Clipboard.GetImage();

            if (source == null)
            {
                MessageBox.Show("(クリップボードに画像は)ないです");
            }
            else
            {
                //クリップボードに画像があったらピクセルフォーマットをGray8に変換して取り込む
                int    w      = source.PixelWidth;
                int    h      = source.PixelHeight;
                int    stride = w;
                var    gray   = new FormatConvertedBitmap(source, PixelFormats.Gray8, null, 0);
                byte[] pixels = new byte[h * stride];
                gray.CopyPixels(pixels, stride, 0);
                MyPixels             = pixels;
                MyPixelsOrigin       = pixels;
                MyBitmapOrigin       = gray;
                MyImage.Source       = gray;
                MyImageOrigin.Source = gray;
            }
        }
Exemplo n.º 9
0
        public Task <IExposureData> DownloadLiveView(CancellationToken token)
        {
            return(Task.Run <IExposureData>(() => {
                byte[] buffer = _camera.GetLiveViewImage().JpegBuffer;
                using (var memStream = new MemoryStream(buffer)) {
                    memStream.Position = 0;

                    JpegBitmapDecoder decoder = new JpegBitmapDecoder(memStream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.OnLoad);

                    FormatConvertedBitmap bitmap = new FormatConvertedBitmap();
                    bitmap.BeginInit();
                    bitmap.Source = decoder.Frames[0];
                    bitmap.DestinationFormat = System.Windows.Media.PixelFormats.Gray16;
                    bitmap.EndInit();

                    ushort[] outArray = new ushort[bitmap.PixelWidth * bitmap.PixelHeight];
                    bitmap.CopyPixels(outArray, 2 * bitmap.PixelWidth, 0);

                    return new ImageArrayExposureData(
                        input: outArray,
                        width: bitmap.PixelWidth,
                        height: bitmap.PixelHeight,
                        bitDepth: 16,
                        isBayered: false,
                        metaData: new ImageMetaData());
                }
            }));
        }
Exemplo n.º 10
0
        //クリップボードの画像を表示
        private void Button_Click_11(object sender, RoutedEventArgs e)
        {
            BitmapSource source = Clipboard.GetImage();

            if (source == null)
            {
                MessageBox.Show("クリップボードに画像はありませんでした");
                return;
            }
            else
            {
                //ピクセルフォーマットをRgb24に変換
                var    rgb24  = new FormatConvertedBitmap(source, PixelFormats.Rgb24, null, 0);
                int    w      = rgb24.PixelWidth;
                int    h      = rgb24.PixelHeight;
                int    stride = w * 3;
                byte[] pixels = new byte[h * stride];
                rgb24.CopyPixels(pixels, stride, 0);
                MyPixels             = pixels;
                MyPixelsOrigin       = pixels;
                MyBitmapOrigin       = rgb24;
                MyImage.Source       = rgb24;
                MyImageOrigin.Source = rgb24;
            }
        }
Exemplo n.º 11
0
        private BitmapSource GetBitmapSourceWithChangePixelFormat2(
            string filePath, PixelFormat pixelFormat, double dpiX = 0, double dpiY = 0)
        {
            BitmapSource source = null;

            try
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    var    bf = BitmapFrame.Create(fs);
                    var    convertedBitmap = new FormatConvertedBitmap(bf, pixelFormat, null, 0);
                    int    w      = convertedBitmap.PixelWidth;
                    int    h      = convertedBitmap.PixelHeight;
                    int    stride = (w * pixelFormat.BitsPerPixel + 7) / 8;
                    byte[] pixels = new byte[h * stride];
                    convertedBitmap.CopyPixels(pixels, stride, 0);
                    //dpi指定がなければ元の画像と同じdpiにする
                    if (dpiX == 0)
                    {
                        dpiX = bf.DpiX;
                    }
                    if (dpiY == 0)
                    {
                        dpiY = bf.DpiY;
                    }
                    //dpiを指定してBitmapSource作成
                    source = BitmapSource.Create(
                        w, h, dpiX, dpiY,
                        convertedBitmap.Format,
                        convertedBitmap.Palette, pixels, stride);
                };
            }
            catch (Exception) { }
            return(source);
        }
Exemplo n.º 12
0
        private Document OnLoadImpl(Stream input)
        {
            WmpBitmapDecoder wbd    = new WmpBitmapDecoder(input, BitmapCreateOptions.None, BitmapCacheOption.None);
            BitmapFrame      frame0 = wbd.Frames[0];

            Document output = new Document(frame0.PixelWidth, frame0.PixelHeight);

            output.DpuUnit = MeasurementUnit.Inch;
            output.DpuX    = frame0.DpiX;
            output.DpuY    = frame0.DpiY;

            BitmapLayer layer       = Layer.CreateBackgroundLayer(output.Width, output.Height);
            MemoryBlock memoryBlock = layer.Surface.Scan0;
            IntPtr      scan0       = memoryBlock.Pointer;

            FormatConvertedBitmap fcb = new FormatConvertedBitmap(frame0, System.Windows.Media.PixelFormats.Bgra32, null, 0);

            fcb.CopyPixels(Int32Rect.Empty, scan0, (int)memoryBlock.Length, layer.Surface.Stride);
            output.Layers.Add(layer);

            BitmapMetadata hdMetadata = (BitmapMetadata)frame0.Metadata;

            CopyMetadataTo(output.Metadata, hdMetadata);

            // WPF doesn't give us an IDisposable implementation on its types
            Utility.GCFullCollect();

            return(output);
        }
        private static BitmapSource ColorizeByAlpha(BitmapSource bitmap, Color tint)
        {
            var src    = new FormatConvertedBitmap(bitmap, PixelFormats.Bgra32, null, 0);
            int width  = src.PixelWidth;
            int height = src.PixelHeight;
            int stride = ((width * src.Format.BitsPerPixel) + 7) / 8;

            var pixels = new byte[height * stride];

            src.CopyPixels(pixels, stride, 0);
            for (int i = 0; i < pixels.Length; i += 4)
            {
                var srcColor = Color.FromArgb(pixels[i + 3], pixels[i + 2], pixels[i + 1], pixels[i]);
                var dstColor = Colorize(srcColor, tint);

                if (dstColor != srcColor)
                {
                    pixels[i]     = dstColor.B;
                    pixels[i + 1] = dstColor.G;
                    pixels[i + 2] = dstColor.R;
                    pixels[i + 3] = dstColor.A;
                }
            }

            var colorized = BitmapSource.Create(
                width, height, src.DpiX, src.DpiY, src.Format, null, pixels, stride);

            if (colorized.CanFreeze)
            {
                colorized.Freeze();
            }
            return(colorized);
        }
Exemplo n.º 14
0
        private (byte[] array, BitmapSource source) MakeBitmapSourceAndByteArray(System.IO.Stream stream, PixelFormat pixelFormat, double dpiX = 0, double dpiY = 0)
        {
            byte[]       pixels = null;
            BitmapSource source = null;

            try
            {
                var bf = BitmapFrame.Create(stream);
                var convertedBitmap = new FormatConvertedBitmap(bf, pixelFormat, null, 0);
                int w      = convertedBitmap.PixelWidth;
                int h      = convertedBitmap.PixelHeight;
                int stride = (w * pixelFormat.BitsPerPixel + 7) / 8;
                pixels = new byte[h * stride];
                convertedBitmap.CopyPixels(pixels, stride, 0);
                //dpi指定がなければ元の画像と同じdpiにする
                if (dpiX == 0)
                {
                    dpiX = bf.DpiX;
                }
                if (dpiY == 0)
                {
                    dpiY = bf.DpiY;
                }
                //dpiを指定してBitmapSource作成
                source = BitmapSource.Create(
                    w, h, dpiX, dpiY,
                    convertedBitmap.Format,
                    convertedBitmap.Palette, pixels, stride);
            }
            catch (Exception)
            {
            }
            return(pixels, source);
        }
Exemplo n.º 15
0
        public virtual BitmapSource GetResultImage(BitmapImage aSourceImage)
        {
            // 转换为标准Bgr32格式
            FormatConvertedBitmap aFormatedImage = new FormatConvertedBitmap(aSourceImage, PixelFormats.Bgr32, null, 0);

            // 提取图片数据
            int aStride = (aFormatedImage.Format.BitsPerPixel * aFormatedImage.PixelWidth + 7) / 8;

            byte[] aSourceRawData = new byte[aStride * aFormatedImage.PixelHeight];
            aFormatedImage.CopyPixels(aSourceRawData, aStride, 0);

            // 处理图片数据
            int aPixelWidth  = aFormatedImage.PixelWidth;
            int aPixelHeight = aFormatedImage.PixelHeight;

            byte[] aResultRawData = ProcessImage(aSourceRawData, ref aPixelWidth, ref aPixelHeight, (aFormatedImage.Format.BitsPerPixel + 7) / 8, ref aStride);

            // 生成结果图片
            BitmapSource aImageFromRawData = BitmapImage.Create(
                aPixelWidth, aPixelHeight,
                aFormatedImage.DpiX, aFormatedImage.DpiY,
                aFormatedImage.Format, aFormatedImage.Palette,
                aResultRawData, aStride);

            return(aImageFromRawData);
        }
Exemplo n.º 16
0
        protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
        {
            var source = new FormatConvertedBitmap((BitmapSource)Source, PixelFormats.Bgra32, null, 0);

            // Get the pixel of the source that was hit
            var x = (int)(hitTestParameters.HitPoint.X / ActualWidth * source.PixelWidth);
            var y = (int)(hitTestParameters.HitPoint.Y / ActualHeight * source.PixelHeight);

            if (x == source.PixelWidth)
            {
                x--;
            }
            if (y == source.PixelHeight)
            {
                y--;
            }

            var pixelxy   = new Int32Rect(x, y, 1, 1);
            var pixelbgra = new byte[4];

            source.CopyPixels(pixelxy, pixelbgra, source.PixelWidth * 4, 0);

            if (pixelbgra[3] < 5)
            {
                return(null);
            }

            return(new PointHitTestResult(this, hitTestParameters.HitPoint));
        }
Exemplo n.º 17
0
        /// <summary>
        /// 从图片中获取背景颜色
        /// </summary>
        /// <param name="bitmap">图片</param>
        public static Color GetImageColorForBackground(FormatConvertedBitmap bitmap)
        {
            const int bytesPerPixel = 3;

            if (bitmap.CanFreeze)
            {
                bitmap.Freeze();
            }
            var pixels = new byte[bitmap.PixelHeight * bitmap.PixelWidth * bytesPerPixel];

            bitmap.CopyPixels(pixels, bitmap.PixelWidth * bytesPerPixel, 0);
            var width  = bitmap.PixelWidth;
            var height = bitmap.PixelHeight;

            //计算颜色的均值
            Color color = GetColorOfRegion(pixels, width, height, 0, width, 0, height);

            var hsl = new HslColor(color);

            if (IsNotSaturateEnough(hsl) && !IsAlmostZeroSaturation(hsl))
            {
                hsl.Saturation += 0.2;
            }

            return(Revise(hsl).ToRgb());
        }
Exemplo n.º 18
0
        public Sprite(BitmapSource source) : this((uint)source.PixelWidth, (uint)source.PixelHeight)
        {
            var bmp    = new FormatConvertedBitmap(source, PixelFormats.Bgr32, null, 0);
            var pixels = new uint[_width * _height];

            bmp.CopyPixels(pixels, (int)(_width * 4), 0);
            SetRawData(pixels);
        }
Exemplo n.º 19
0
        private void CopyPixelsToByteArray(Bitmap source, byte[] bytes, int offset)
        {
            var bitmap = ConvertBitmap(source);
            FormatConvertedBitmap formattedBitmap = new FormatConvertedBitmap(bitmap, PixelFormat, Palette, 0);

            formattedBitmap.CopyPixels(new Int32Rect(0, 0, formattedBitmap.PixelWidth, formattedBitmap.PixelHeight), bytes,
                                       formattedBitmap.PixelWidth * BytesPerPixel, offset);
        }
Exemplo n.º 20
0
        private static void InitializeOriginal(ProjectStats stats, MosaicProject project)
        {
            BitmapImage           image          = new BitmapImage(new Uri(project.OriginalPath));
            FormatConvertedBitmap convertedImage = new FormatConvertedBitmap(image, PixelFormats.Rgb24, null, 0);

            stats.OriginalWidth = convertedImage.PixelWidth;
            byte[] pixels = new byte[convertedImage.PixelHeight * stats.OriginalStride];
            convertedImage.CopyPixels(pixels, stats.OriginalStride, 0);
        }
Exemplo n.º 21
0
        public override ImageData Read(Stream stream, ImageMetaData info)
        {
            var meta = info as BipMetaData;

            if (null == meta)
            {
                throw new ArgumentException("BipFormat.Read should be supplied with BipMetaData", "info");
            }

            var header = new byte[0x7c];
            var bitmap = new WriteableBitmap((int)meta.Width, (int)meta.Height,
                                             ImageData.DefaultDpiX, ImageData.DefaultDpiY, PixelFormats.Bgra32, null);

            foreach (var tile in meta.Tiles)
            {
                stream.Position = tile.Offset;
                if (header.Length != stream.Read(header, 0, header.Length))
                {
                    throw new InvalidFormatException("Invalid tile header");
                }
                if (!Binary.AsciiEqual(header, "PNGFILE2"))
                {
                    throw new InvalidFormatException("Unknown tile format");
                }
                int data_size = LittleEndian.ToInt32(header, 0x18) - header.Length;
                int alpha     = LittleEndian.ToInt32(header, 0x68);
                int x         = LittleEndian.ToInt32(header, 0x6c);
                int y         = LittleEndian.ToInt32(header, 0x70);
                using (var png = new StreamRegion(stream, stream.Position, data_size, true))
                {
                    var decoder = new PngBitmapDecoder(png,
                                                       BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                    BitmapSource frame     = decoder.Frames[0];
                    PixelFormat  format    = 0 == alpha ? PixelFormats.Bgr32 : PixelFormats.Bgra32;
                    var          converted = new FormatConvertedBitmap(frame, format, null, 0);
                    int          stride    = converted.PixelWidth * 4;
                    var          pixels    = new byte[stride * converted.PixelHeight];
                    converted.CopyPixels(pixels, stride, 0);
                    for (int p = 0; p < pixels.Length; p += 4)
                    {
                        byte r = pixels[p];
                        pixels[p]     = pixels[p + 2];
                        pixels[p + 2] = r;
                        int a = 0 == alpha ? 0xff : pixels[p + 3] * 0xff / 0x80;
                        if (a > 0xff)
                        {
                            a = 0xff;
                        }
                        pixels[p + 3] = (byte)a;
                    }
                    var rect = new Int32Rect(tile.Left + x, tile.Top + y, converted.PixelWidth, converted.PixelHeight);
                    bitmap.WritePixels(rect, pixels, stride, 0);
                }
            }
            bitmap.Freeze();
            return(new ImageData(bitmap, meta));
        }
Exemplo n.º 22
0
        /// <summary>
        ///   Converts an image from one representation to another.
        /// </summary>
        ///
        /// <param name="input">The input image to be converted.</param>
        /// <param name="output">The converted image.</param>
        ///
        public void Convert(BitmapSource input, out byte[,,] output)
        {
            int width  = input.PixelWidth;
            int height = input.PixelHeight;

            var c = new FormatConvertedBitmap(input, PixelFormats.Bgra32, null, 1.0);

            output = new byte[width, height, 4];
            c.CopyPixels(output);
        }
Exemplo n.º 23
0
        /// <summary>
        ///   Converts an image from one representation to another.
        /// </summary>
        ///
        /// <param name="input">The input image to be converted.</param>
        /// <param name="output">The converted image.</param>
        ///
        public void Convert(BitmapSource input, out float[,,] output)
        {
            var c      = new FormatConvertedBitmap(input, PixelFormats.Rgba128Float, null, 1.0);
            int width  = c.PixelWidth;
            int height = c.PixelHeight;
            int stride = c.GetStride();

            output = new float[width, height, 4];
            c.CopyPixels(output);
        }
Exemplo n.º 24
0
        /// <summary>
        ///   Converts an image from one representation to another.
        /// </summary>
        ///
        /// <param name="input">The input image to be converted.</param>
        /// <param name="output">The converted image.</param>
        ///
        public void Convert(BitmapSource input, out byte[] output)
        {
            var c      = new FormatConvertedBitmap(input, PixelFormats.Gray8, null, 1.0);
            int width  = c.PixelWidth;
            int height = c.PixelHeight;
            int stride = c.GetStride();

            output = new byte[width * height];
            c.CopyPixels(output, stride, 0);
        }
Exemplo n.º 25
0
        /// <summary>
        ///   Converts an image from one representation to another.
        /// </summary>
        ///
        /// <param name="input">The input image to be converted.</param>
        /// <param name="output">The converted image.</param>
        ///
        public void Convert(BitmapSource input, out float[][] output)
        {
            var c      = new FormatConvertedBitmap(input, PixelFormats.Rgba128Float, null, 1.0);
            int width  = c.PixelWidth;
            int height = c.PixelHeight;
            var buffer = new float[width * height, 4];

            c.CopyPixels(buffer);
            output = buffer.ToJagged();
        }
Exemplo n.º 26
0
        public void SetImage(BitmapSource image)
        {
            PixelFormat pixelFormat;

            if (Header.Glyphs.BitsPerPixel == 4)
            {
                pixelFormat = PixelFormats.Indexed4;
            }
            else if (Header.Glyphs.BitsPerPixel == 8)
            {
                pixelFormat = PixelFormats.Indexed8;
            }
            else
            {
                throw new Exception("FNT: Unknown Pixel Format");
            }

            FormatConvertedBitmap bitmap = new FormatConvertedBitmap();

            bitmap.BeginInit();
            bitmap.Source             = image;
            bitmap.DestinationFormat  = pixelFormat;
            bitmap.DestinationPalette = GetImagePalette(Palette.Pallete);
            bitmap.EndInit();

            int stride = (bitmap.Format.BitsPerPixel * bitmap.PixelWidth + 7) / 8;

            byte[] data = new byte[bitmap.PixelHeight * stride];
            bitmap.CopyPixels(data, stride, 0);

            ImageData     BMP     = new ImageData(data, bitmap.Format, bitmap.PixelWidth, bitmap.PixelHeight);
            List <byte[]> BMPdata = new List <byte[]>();

            int row    = 0;
            int column = 0;

            for (int i = 0; i < Header.Glyphs.Count; i++)
            {
                BMPdata.Add(ImageData.Crop(BMP, new ImageData.Rect(column * Header.Glyphs.Size1,
                                                                   row * Header.Glyphs.Size2, Header.Glyphs.Size1, Header.Glyphs.Size2)).Data);
                column++;
                if (column == 16)
                {
                    row++;
                    column = 0;
                }
            }

            if (Header.Glyphs.BitsPerPixel == 4)
            {
                Util.ReverseByteInList(BMPdata);
            }

            Compressed.CompressData(BMPdata);
        }
Exemplo n.º 27
0
        public MosaicImage(string path, int width, int height)
        {
            BitmapImage           image          = new BitmapImage(new Uri(path));
            FormatConvertedBitmap convertedImage = new FormatConvertedBitmap(image, PixelFormats.Rgb24, null, 0);

            _width  = convertedImage.PixelWidth;
            _height = convertedImage.PixelHeight;
            _stride = _width * 3;
            _pixels = new byte[_height * _stride];
            convertedImage.CopyPixels(_pixels, _stride, 0);
        }
Exemplo n.º 28
0
 private void GetRgbImage()
 {
     if (DipLibImage is RgbImage)
     {
         return;
     }
     Image = new FormatConvertedBitmap(OriginImage, PixelFormats.Bgra32, null, 0);
     byte[] pixels = new byte[Image.PixelWidth * Image.PixelHeight * 4];
     Image.CopyPixels(pixels, Image.PixelWidth * 4, 0);
     DipLibImage = new RgbImage(pixels, Image.PixelWidth, Image.PixelHeight);
 }
Exemplo n.º 29
0
 private void GetGrayscaleImage()
 {
     if (DipLibImage is GrayscaleImage)
     {
         return;
     }
     Image = new FormatConvertedBitmap(OriginImage, PixelFormats.Gray8, BitmapPalettes.Gray256, 0);
     byte[] pixels = new byte[Image.PixelWidth * Image.PixelHeight];
     Image.CopyPixels(pixels, Image.PixelWidth, 0);
     DipLibImage = new GrayscaleImage(pixels, Image.PixelWidth, Image.PixelHeight);
 }
Exemplo n.º 30
0
        private Color GetPixelColor(int x, int y, BitmapSource bmp)
        {
            var cb  = new CroppedBitmap(bmp, new Int32Rect(x, y, 1, 1));
            var fcb = new FormatConvertedBitmap(cb, PixelFormats.Bgra32, null, 0);

            byte[] pixels = new byte[4];
            fcb.CopyPixels(pixels, 4, 0);
            var c = Color.FromArgb(pixels[3], pixels[2], pixels[1], pixels[0]);

            return(c);
        }