Example #1
0
        public ImageSource GetImage(EditorContext context)
        {
            Graphics g = Graphics.FromHwnd(IntPtr.Zero);

            PixelFormat   format;
            BitmapPalette palette = null;

            if (Indexed)
            {
                palette = new BitmapPalette(context.ColorPalette);
                format  = PixelFormats.Indexed8;
            }
            else
            {
                format = PixelFormats.Bgra32;
            }

            int bpp    = format.BitsPerPixel;
            int bypp   = (bpp + 7) / 8;
            int stride = 4 * ((Width * bypp + 3) / 4);

            WriteableBitmap bmp = new WriteableBitmap(Width, Height, g.DpiX, g.DpiY, format, palette);

            bmp.Lock();
            bmp.WritePixels(new Int32Rect(0, 0, Width, Height), Pixels, stride, 0);
            bmp.Unlock();

            return(bmp);
        }
Example #2
0
        ImageData GetImage()
        {
            var bitmap = Unpack();

            m_info = new ImageMetaData {
                Width  = bitmap.ToUInt32(4),
                Height = bitmap.ToUInt32(8),
                BPP    = bitmap.ToUInt16(0xE),
            };
            int           header_size = bitmap.ToInt32(0);
            BitmapPalette palette     = null;
            int           stride      = m_info.iWidth * m_info.BPP / 8;
            var           pixels      = new byte[stride * m_info.iHeight];

            using (var input = new MemoryStream(bitmap, header_size, bitmap.Length - header_size))
            {
                if (8 == m_info.BPP)
                {
                    palette = ImageFormat.ReadPalette(input);
                }
                input.Read(pixels, 0, pixels.Length);
            }
            PixelFormat format = 8 == m_info.BPP ? PixelFormats.Indexed8
                               : 24 == m_info.BPP ? PixelFormats.Bgr24
                               : PixelFormats.Bgr32;

            return(ImageData.Create(m_info, format, palette, pixels, stride));
        }
Example #3
0
        private static BitmapSource CreateBitmapSource(System.Windows.Media.Color color)
        {
            int width  = 5;
            int height = 5;
            int stride = width / 8;

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

            List <System.Windows.Media.Color> colors = new List <System.Windows.Media.Color>();

            colors.Add(color);
            BitmapPalette myPalette = new BitmapPalette(colors);

            BitmapSource image = BitmapSource.Create(
                width,
                height,
                96,
                96,
                PixelFormats.Indexed1,
                myPalette,
                pixels,
                stride);

            return(image);
        }
Example #4
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var image = value as Bitmap;

            if (!cachedBitmaps.ContainsKey(image))
            {
                BitmapPalette palette = null;
                if (image.Palette != null)
                {
                    List <System.Windows.Media.Color> colorList = new List <System.Windows.Media.Color> ();
                    foreach (var color in image.Palette.Entries)
                    {
                        colorList.Add(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
                    }
                    palette = new BitmapPalette(colorList);
                }
                cachedBitmaps.Add(image,
                                  new WriteableBitmap(image.Width, image.Height, image.HorizontalResolution, image.VerticalResolution, ConvertPixelFormat(image.PixelFormat), palette));
            }
            var cachedBitmap = cachedBitmaps[image];

            BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);

            cachedBitmap.Lock();
            CopyMemory(cachedBitmap.BackBuffer, data.Scan0, cachedBitmap.BackBufferStride * data.Height);
            cachedBitmap.AddDirtyRect(new Int32Rect(0, 0, image.Width, image.Height));
            cachedBitmap.Unlock();
            image.UnlockBits(data);

            return(cachedBitmap);
        }
Example #5
0
        //public void CallTimerCallback(Object stateInfo)
        //{
        //    if (_stream != 0)
        //    {

        //        System.Drawing.Bitmap bmp = vis.CreateSpectrumLine(_stream, _imgWidth, _imgHeight, System.Drawing.Color.Green, System.Drawing.Color.Red, System.Drawing.Color.Transparent, 2, 3, false, false, false);

        //        this.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(delegate()
        //        {
        //            ImageSource imgSrc = IMPUtils.ImageToBitmapImage(bmp);

        //            this.Source = imgSrc;
        //        }));
        //    }
        //    else
        //        this.Source = null;
        //}

        #endregion Events

        #region Methods

        private void CheckOnStateChanged()
        {
            DISPLAY display         = Display;
            int     width           = (int)Width;
            int     height          = (int)Height;
            bool    isStreamPlaying = IsPlaying;

            if (isStreamPlaying && IsVisible && display != DISPLAY.NONE && _timer.IsEnabled == false)
            {
                _timer.Start();
            }
            else if ((isStreamPlaying == false || IsVisible == false || display == DISPLAY.NONE) && _timer.IsEnabled == true)
            {
                _timer.Stop();
                int    stride = width * 3;
                byte[] pixels = new byte[height * stride];

                List <System.Windows.Media.Color> colors = new List <System.Windows.Media.Color>();
                colors.Add(System.Windows.Media.Colors.Transparent);
                BitmapPalette myPalette = new BitmapPalette(colors);

                // make an empty image to make it clickable
                this.Source = BitmapSource.Create(
                    width, height,
                    96, 96,
                    PixelFormats.Indexed1,
                    myPalette,
                    pixels,
                    stride);
            }
        }
Example #6
0
 protected override ImageData GetImageData()
 {
     m_input.Position = m_data_pos;
     using (var input = new ZLibStream(m_input.AsStream, CompressionMode.Decompress, true))
     {
         BitmapPalette palette = null;
         if (8 == Info.BPP)
         {
             var pal_format = HasAlpha ? PaletteFormat.RgbA : PaletteFormat.RgbX;
             palette = ImageFormat.ReadPalette(input, m_colors, pal_format);
         }
         var pixels = new byte[(int)Info.Width * (int)Info.Height * (Info.BPP / 8)];
         input.Read(pixels, 0, pixels.Length);
         if (32 == Info.BPP)
         {
             for (int i = 0; i < pixels.Length; i += 4)
             {
                 byte a = pixels[i];
                 byte r = pixels[i + 1];
                 byte g = pixels[i + 2];
                 byte b = pixels[i + 3];
                 pixels[i]     = b;
                 pixels[i + 1] = g;
                 pixels[i + 2] = r;
                 pixels[i + 3] = a;
             }
         }
         return(ImageData.Create(Info, Format, palette, pixels));
     }
 }
Example #7
0
 public void GetGifPalette(Bitmap bit)
 {
     using (MemoryStream imageStreamSource
                = new MemoryStream())
     {
         System.Drawing.ImageConverter ic
             = new System.Drawing.ImageConverter();
         byte[] btImage
             = (byte[])ic.ConvertTo(bit, typeof(byte[]));
         imageStreamSource.Write(btImage, 0, btImage.Length);
         GifBitmapDecoder decoder
             = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
         BitmapPalette pal
             = decoder.Palette;
         int i;
         for (i
                  = 0; i < 0x100; i++)
         {
             this.Palette[i]
                 = 0;
         }
         try
         {
             i
                 = 0;
             while (i < 0x100)                    //&& i < pal.Colors.Count)
             {
                 int Red
                     = pal.Colors[i].R / 8;
                 int Green
                     = pal.Colors[i].G / 8;
                 int Blue
                     = pal.Colors[i].B / 8;
                 int contaFinal
                     = (((0x400 * Red) + (0x20 * Green)) + Blue) + 0x8000;
                 if (contaFinal == 0x8000)
                 {
                     contaFinal
                         = 0x8001;
                 }
                 this.Palette[i]
                     = (ushort)contaFinal;
                 i++;
             }
         }
         catch (System.IndexOutOfRangeException)
         { }
         catch (System.ArgumentOutOfRangeException)
         { }
         for (i
                  = 0; i < 0x100; i++)
         {
             if (this.Palette[i] < 0x8000)
             {
                 this.Palette[i]
                     = 0x8000;
             }
         }
     }
 }
 private bool CheckIfGrayScale(PixelFormat pixelFormat, BitmapPalette palette, DeepScanOptions DeepScan)
 {
     if (pixelFormat == PixelFormats.Gray32Float ||
         pixelFormat == PixelFormats.Gray16 ||
         pixelFormat == PixelFormats.Gray8 ||
         pixelFormat == PixelFormats.Gray4 ||
         pixelFormat == PixelFormats.Gray2)
     {
         if (DeepScan != DeepScanOptions.Force)
         {
             return(true);
         }
         else if (pixelFormat == PixelFormats.Indexed8 ||
                  pixelFormat == PixelFormats.Indexed4 ||
                  pixelFormat == PixelFormats.Indexed2)
         {
             DeepScan = (DeepScan != DeepScanOptions.Skip) ? DeepScanOptions.Force : DeepScan;
         }
     }
     if ((DeepScan != DeepScanOptions.Skip) & palette != null)
     {
         List <Color> IndexedColors = palette.Colors.ToList();
         return(IndexedColors.All(rgb => (rgb.R == rgb.G && rgb.G == rgb.B && rgb.B == rgb.R)));
     }
     return(false);
 }
Example #9
0
 private static void SetColor(ref ConsoleChar c, BitmapPalette palette, int colorIndex)
 {
     if (ConsolePaletteMap.TryGetValue(palette.Colors[colorIndex], out ConsoleColor color))
     {
         c.BackgroundColor = color;
     }
 }
Example #10
0
        public HzcDecoder(IBinaryStream input, HzcMetaData info, bool leave_open = false)
        {
            m_info   = info;
            m_stride = (int)m_info.Width * m_info.BPP / 8;
            switch (m_info.Type)
            {
            default: throw new NotSupportedException();

            case 0: Format = PixelFormats.Bgr24; break;

            case 1:
            case 2: Format = PixelFormats.Bgra32; break;

            case 3: Format = PixelFormats.Gray8; break;

            case 4:
            {
                Format = PixelFormats.Indexed8;
                var colors = new Color[2] {
                    Color.FromRgb(0, 0, 0), Color.FromRgb(0xFF, 0xFF, 0xFF)
                };
                Palette = new BitmapPalette(colors);
                break;
            }
            }
            Source         = new ZLibStream(input.AsStream, CompressionMode.Decompress, leave_open);
            m_frame_offset = 0;
            m_frame_size   = m_stride * (int)Info.Height;
        }
Example #11
0
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            var meta = (ClmMetaData)info;

            stream.Position = meta.DataOffset;
            PixelFormat   format;
            BitmapPalette palette = null;

            if (8 == meta.BPP)
            {
                format  = PixelFormats.Indexed8;
                palette = ReadPalette(stream.AsStream);
            }
            else if (24 == meta.BPP)
            {
                format = PixelFormats.Bgr24;
            }
            else if (32 == meta.BPP)
            {
                format = PixelFormats.Bgr32;
            }
            else
            {
                throw new NotSupportedException("Not supported CLM color depth");
            }
            int packed_size = (int)(stream.Length - stream.Position);

            using (var reader = new MrgLzssReader(stream, packed_size, meta.UnpackedSize))
            {
                reader.Unpack();
                return(ImageData.Create(info, format, palette, reader.Data));
            }
        }
Example #12
0
        public static BitmapSource BuildOptTexture(Texture texture, int paletteIndex = TextureUtils.DefaultPalette, int level = 0)
        {
            if (texture == null || paletteIndex < 0 || paletteIndex > 15 || level < 0 || level >= texture.MipmapsCount)
            {
                return(null);
            }

            int bpp = texture.BitsPerPixel;

            if (bpp == 8)
            {
                var palette = new BitmapPalette(Enumerable.Range(0, 256)
                                                .Select(i =>
                {
                    ushort c = BitConverter.ToUInt16(texture.Palette, paletteIndex * 512 + i * 2);

                    byte r = (byte)((c & 0xF800) >> 11);
                    byte g = (byte)((c & 0x7E0) >> 5);
                    byte b = (byte)(c & 0x1F);

                    r = (byte)((r * (0xffU * 2) + 0x1fU) / (0x1fU * 2));
                    g = (byte)((g * (0xffU * 2) + 0x3fU) / (0x3fU * 2));
                    b = (byte)((b * (0xffU * 2) + 0x1fU) / (0x1fU * 2));

                    return(Color.FromRgb(r, g, b));
                })
                                                .ToList());

                int    texWidth;
                int    texHeight;
                byte[] texImageData = texture.GetMipmapImageData(level, out texWidth, out texHeight);

                int    size      = texWidth * texHeight;
                byte[] imageData = new byte[size * 4];

                for (int i = 0; i < size; i++)
                {
                    int colorIndex = texImageData[i];

                    imageData[i * 4 + 0] = palette.Colors[colorIndex].B;
                    imageData[i * 4 + 1] = palette.Colors[colorIndex].G;
                    imageData[i * 4 + 2] = palette.Colors[colorIndex].R;
                    imageData[i * 4 + 3] = texture.AlphaData == null ? (byte)255 : texture.AlphaData[i];
                }

                return(BitmapSource.Create(texWidth, texHeight, 96, 96, PixelFormats.Bgra32, null, imageData, texWidth * 4));
            }
            else if (bpp == 32)
            {
                int    texWidth;
                int    texHeight;
                byte[] texImageData = texture.GetMipmapImageData(level, out texWidth, out texHeight);

                return(BitmapSource.Create(texWidth, texHeight, 96, 96, PixelFormats.Bgra32, null, texImageData, texWidth * 4));
            }
            else
            {
                return(null);
            }
        }
Example #13
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            file.Position = 12;
            BitmapPalette palette = null;

            if (8 == info.BPP)
            {
                palette = ReadPalette(file.AsStream);
            }
            int         stride = (int)info.Width * (info.BPP / 8);
            var         pixels = new byte[stride * (int)info.Height];
            PixelFormat format;

            if (8 == info.BPP)
            {
                Unpack8BPP(file, pixels);
                format = PixelFormats.Indexed8;
            }
            else if (24 == info.BPP)
            {
                Unpack24BPP(file, pixels);
                format = PixelFormats.Bgr24;
            }
            else
            {
                throw new InvalidFormatException();
            }
            return(ImageData.Create(info, format, palette, pixels, stride));
        }
Example #14
0
        public override ImageData Read(Stream stream, ImageMetaData info)
        {
            var meta = info as IsgMetaData;

            if (null == meta)
            {
                throw new ArgumentException("IsgFormat.Read should be supplied with IsgMetaData", "info");
            }
            if (0x21 != meta.Type && 0x10 != meta.Type)
            {
                throw new InvalidFormatException("Unsupported ISM image type");
            }

            stream.Position = 0x30;
            using (var input = new Reader(stream, meta))
            {
                if (0x21 == meta.Type)
                {
                    input.Unpack21();
                }
                else
                {
                    input.Unpack10();
                }
                var palette = new BitmapPalette(input.Palette);
                return(ImageData.CreateFlipped(info, PixelFormats.Indexed8, palette, input.Data, (int)info.Width));
            }
        }
Example #15
0
        private (byte[] pixels, BitmapSource source) Gray8To4Colors2(byte[] pixels, int width, int height)
        {
            byte[] rPixels = new byte[pixels.Length];
            for (int i = 0; i < pixels.Length; i++)
            {
                switch (pixels[i])
                {
                case byte b when b < 64:
                    rPixels[i] = 0;
                    break;

                case byte b when b < 128:
                    rPixels[i] = 85;
                    break;

                case byte b when b < 192:
                    rPixels[i] = 170;
                    break;

                default:
                    rPixels[i] = 255;
                    break;
                }
            }
            var temp = BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray8, null, rPixels, width);
            //パレットを減色したGray8から作成、これはnullを指定して自動作成と全く同じ結果になった
            //このまま保存するとなぜかIndexd4になる、どうせならindexed2にして欲しい
            var palette   = new BitmapPalette(temp, 4);
            var converted = new FormatConvertedBitmap(temp, PixelFormats.Indexed8, palette, 0);

            return(rPixels, converted);
        }
Example #16
0
        internal static int HasAlpha(BitmapSource bitmap)
        {
            if (bitmap.Format.HasAlpha)
            {
                return(1);
            }

            if (bitmap.Format.Palettized)
            {
                BitmapPalette palette = bitmap.Palette;

                if (palette != null)
                {
                    IList <System.Windows.Media.Color> palColor = palette.Colors;

                    if (palColor != null)
                    {
                        foreach (Color c in palColor)
                        {
                            if (!Utility.IsOpaque(c.ScA))
                            {
                                return(2);
                            }
                        }
                    }
                }
            }

            return(0);
        }
Example #17
0
        public static BitmapSource Invert(BitmapSource originalSource)
        {
            int stride = (originalSource.PixelWidth * originalSource.Format.BitsPerPixel + 7) / 8;

            int length = stride * originalSource.PixelHeight;

            byte[] data = new byte[length];

            originalSource.CopyPixels(data, stride, 0);

            int a = 0;

            for (int i = 0; i < length; i += 1)
            {
                if (!InvertCount(ref a))
                {
                    data[i] = (byte)(255 - data[i]);
                }
            }

            List <Color> colors = new List <Color>
            {
                Colors.Black
            };
            BitmapPalette palette = new BitmapPalette(colors);

            return(BitmapSource.Create(originalSource.PixelWidth, originalSource.PixelHeight,
                                       originalSource.DpiX, originalSource.DpiY, originalSource.Format, palette, data, stride));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BitmapDataBuffer" /> class.
        /// </summary>
        /// <param name="scan0">The scan0.</param>
        /// <param name="stride">The stride.</param>
        /// <param name="bytesPerPixel">The bytes per pixel.</param>
        /// <param name="pixelWidth">Width of the pixel.</param>
        /// <param name="pixelHeight">Height of the pixel.</param>
        /// <param name="dpiX">The dpi x.</param>
        /// <param name="dpiY">The dpi y.</param>
        /// <param name="palette">The palette.</param>
        /// <param name="pixelFormat">The pixel format.</param>
        private BitmapDataBuffer(
            IntPtr scan0,
            int stride,
            int bytesPerPixel,
            int pixelWidth,
            int pixelHeight,
            double dpiX,
            double dpiY,
            BitmapPalette palette,
            PixelFormat pixelFormat)
        {
            Scan0         = scan0;
            Stride        = stride;
            BytesPerPixel = bytesPerPixel;
            BitsPerPixel  = bytesPerPixel * 8;
            PixelWidth    = pixelWidth;
            PixelHeight   = pixelHeight;
            DpiX          = dpiX;
            DpiY          = dpiY;

            UpdateRect   = new Int32Rect(0, 0, pixelWidth, pixelHeight);
            BufferLength = (uint)(Stride * PixelHeight);
            Palette      = palette;
            PixelFormat  = pixelFormat;
        }
Example #19
0
        private WriteableBitmap GetBitmap()
        {
            var bitMapPalette = new BitmapPalette(GetColors(_colors));
            var writer        = new WriteableBitmap(RenderWidth, RenderHeight, 96.0, 96.0, PixelFormats.Indexed8, bitMapPalette);

            return(writer);
        }
Example #20
0
        /// <summary>
        /// Converts an IVisualization2d to a BitmapSource for display.  The bitmap will be
        /// the same size as the original content.
        /// </summary>
        public static BitmapSource ConvertToBitmapSource(IVisualization2d vis2d)
        {
            // Create indexed color palette.
            int[]        intPal = vis2d.GetPalette();
            List <Color> colors = new List <Color>(intPal.Length);

            foreach (int argb in intPal)
            {
                Color col = Color.FromArgb((byte)(argb >> 24), (byte)(argb >> 16),
                                           (byte)(argb >> 8), (byte)argb);
                colors.Add(col);
            }
            BitmapPalette palette = new BitmapPalette(colors);

            // indexed-color; see https://stackoverflow.com/a/15272528/294248 for direct color
            BitmapSource image = BitmapSource.Create(
                vis2d.Width,
                vis2d.Height,
                96.0,
                96.0,
                PixelFormats.Indexed8,
                palette,
                vis2d.GetPixels(),
                vis2d.Width);

            image.Freeze();

            return(image);
        }
Example #21
0
        public override ImageData Read(Stream file, ImageMetaData info)
        {
            var meta = (GgdMetaData)info;

            file.Position = meta.HeaderSize + 4;
            var palette_data = new byte[0x400];

            if (palette_data.Length != file.Read(palette_data, 0, palette_data.Length))
            {
                throw new InvalidFormatException();
            }
            var colors = new Color[256];

            for (int i = 0; i < 256; ++i)
            {
                colors[i] = Color.FromRgb(palette_data[i * 4 + 2], palette_data[i * 4 + 1], palette_data[i * 4]);
            }
            file.Seek(4, SeekOrigin.Current);
            int input_size = (int)(file.Length - file.Position);

            using (var reader = new LzssReader(file, input_size, (int)meta.BitmapSize))
            {
                reader.Unpack();
                var palette = new BitmapPalette(colors);
                int stride  = ((int)meta.Width + 3) & ~3;
                return(ImageData.Create(info, PixelFormats.Indexed8, palette, reader.Data, stride));
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            s = new Sphere(250, 50, 50);

            foreach (Vertex v in s.Vertices)
            {
                v.Projected = TransformationMatrix.GetRotationMatrix(new Vector3D(0, 0, 0)) * v.P;
                v.Projected = TransformationMatrix.GetCameraMatrix(
                    new Camera {
                    Position = new Vector3D(0, 50, 300), Target = new Vector3D(0, 0, 0), UpDirection = new Vector3D(0, 1, 0)
                }) * v.Projected;
                v.Projected = TransformationMatrix.GetProjectionMatrix(180, 800, 600) * v.Projected;
                v.Projected = v.Projected / v.Projected.W;
                v.Projected.NormalizeToPixels();
            }
            List <System.Windows.Media.Color> colors = new List <System.Windows.Media.Color>();

            colors.Add(System.Windows.Media.Colors.Red);
            colors.Add(System.Windows.Media.Colors.Blue);
            colors.Add(System.Windows.Media.Colors.Green);
            BitmapPalette   myPalette = new BitmapPalette(colors);
            WriteableBitmap ToProcess = new WriteableBitmap(800, 600, 96, 96, PixelFormats.Bgr32, myPalette);

            byte[] Pixels = new byte[ToProcess.PixelHeight * ToProcess.PixelWidth * 3];

            SceneImage.Source = ToProcess;
            img     = LoadImage("earth.jpg");
            Texture = new byte[img.PixelWidth * img.PixelHeight * img.Format.BitsPerPixel / 8];
            img.CopyPixels(Texture, img.PixelWidth * 4, 0);
        }
        public static BitmapSource Convert(this System.Drawing.Bitmap image)
        {
            var rect       = new System.Drawing.Rectangle(0, 0, image.Width, image.Height);
            var bitmapData = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);

            try
            {
                BitmapPalette palette = null;

                if (image.Palette.Entries.Length > 0)
                {
                    var paletteColors = image.Palette.Entries.Select(entry => Color.FromArgb(entry.A, entry.R, entry.G, entry.B)).ToList();
                    palette = new BitmapPalette(paletteColors);
                }

                return(BitmapSource.Create(
                           image.Width,
                           image.Height,
                           image.HorizontalResolution,
                           image.VerticalResolution,
                           ConvertPixelFormat(image.PixelFormat),
                           palette,
                           bitmapData.Scan0,
                           bitmapData.Stride * image.Height,
                           bitmapData.Stride
                           ));
            }
            finally
            {
                image.UnlockBits(bitmapData);
            }
        }
        /// <summary>
        /// MatをBitmapSourceに変換する.
        /// </summary>
        /// <param name="src">変換するIplImage</param>
        /// <param name="horizontalResolution"></param>
        /// <param name="verticalResolution"></param>
        /// <param name="pixelFormat"></param>
        /// <param name="palette"></param>
        /// <returns>WPFのBitmapSource</returns>
#else
        /// <summary>
        /// Converts Mat to BitmapSource.
        /// </summary>
        /// <param name="src">Input IplImage</param>
        /// <param name="horizontalResolution"></param>
        /// <param name="verticalResolution"></param>
        /// <param name="pixelFormat"></param>
        /// <param name="palette"></param>
        /// <returns>BitmapSource</returns>
#endif
        public static BitmapSource ToBitmapSource(
            this Mat src,
            int horizontalResolution,
            int verticalResolution,
            PixelFormat pixelFormat,
            BitmapPalette palette)
        {
            if (src == null)
            {
                throw new ArgumentNullException("src");
            }
            if (src.IsDisposed)
            {
                throw new ObjectDisposedException(typeof(Mat).ToString());
            }
            if (src.Dims() != 2)
            {
                throw new ArgumentException("src.Dims() != 2");
            }

            long step = src.Step();

            return(BitmapSource.Create(
                       src.Width,
                       src.Height,
                       horizontalResolution,
                       verticalResolution,
                       pixelFormat,
                       palette,
                       src.Data,
                       (int)(step * src.Rows),
                       (int)step));
        }
Example #25
0
        private BitmapSource redImage()
        {
            int width  = 128;
            int height = width;
            int stride = width / 8;

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

            List <System.Windows.Media.Color> colors = new List <System.Windows.Media.Color>();

            colors.Add(System.Windows.Media.Colors.Red);
            BitmapPalette tempPalette = new BitmapPalette(colors);

            BitmapSource image = BitmapSource.Create(
                width,
                height,
                96,
                96,
                PixelFormats.Indexed1,
                tempPalette,
                pixels,
                stride);

            return(image);
        }
        /// <summary>
        /// IplImageをBitmapSourceに変換する.
        /// </summary>
        /// <param name="src">変換するIplImage</param>
        /// <param name="horizontalResolution"></param>
        /// <param name="verticalResolution"></param>
        /// <param name="pixelFormat"></param>
        /// <param name="palette"></param>
        /// <returns>WPFのBitmapSource</returns>
#else
        /// <summary>
        /// Converts IplImage to BitmapSource.
        /// </summary>
        /// <param name="src">Input IplImage</param>
        /// <param name="horizontalResolution"></param>
        /// <param name="verticalResolution"></param>
        /// <param name="pixelFormat"></param>
        /// <param name="palette"></param>
        /// <returns>BitmapSource</returns>
#endif
        public static BitmapSource ToBitmapSource(
            this IplImage src,
            int horizontalResolution,
            int verticalResolution,
            PixelFormat pixelFormat,
            BitmapPalette palette)
        {
            if (src == null)
            {
                throw new ArgumentNullException("src");
            }
            if (src.IsDisposed)
            {
                throw new ObjectDisposedException(typeof(IplImage).ToString());
            }

            return(BitmapSource.Create(
                       src.Width,
                       src.Height,
                       horizontalResolution,
                       verticalResolution,
                       pixelFormat,
                       palette,
                       src.ImageData,
                       src.WidthStep * src.Height,
                       src.WidthStep));
        }
Example #27
0
 public McaArchive(ArcView arc, ArchiveFormat impl, ICollection <Entry> dir, byte key, int bpp = 24, BitmapPalette palette = null)
     : base(arc, impl, dir)
 {
     Key     = key;
     BPP     = bpp;
     Palette = palette;
 }
Example #28
0
        private void MainWindow_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop) == false)
            {
                return;
            }
            string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop);
            var(pixels, bitmap, palette) = MakeBitmapSourceAndByteArrayAndPalette(filePath[0], PixelFormats.Gray8, 96, 96);

            if (bitmap == null)
            {
                MessageBox.Show("画像ファイルじゃないみたい");
            }
            else
            {
                //MyPixels = pixels;
                //MyPixelsOrigin = pixels;

                ImageFileFullPath    = filePath[0];
                MyOriginBitmapSource = bitmap;
                MyImageOrigin.Source = MyOriginBitmapSource;
                MyOriginalPalette    = palette;
                MyOriginPixels       = pixels;
            }
        }
Example #29
0
            private void ReadPalette(int colors)
            {
                int color_size = 0x102 == colors ? 4 : 3;

                if (colors > 0x100)
                {
                    colors = 0x100;
                }
                int palette_size = colors * color_size;
                var palette_data = new byte[palette_size];

                if (palette_size != m_input.Read(palette_data, 0, palette_size))
                {
                    throw new InvalidFormatException();
                }
                var palette   = new Color[colors];
                int color_pos = 0;

                for (int i = 0; i < palette.Length; ++i)
                {
                    byte r = palette_data[color_pos];
                    byte g = palette_data[color_pos + 1];
                    byte b = palette_data[color_pos + 2];
                    if (0xff == b && 0 == g && 0xff == r)
                    {
                        g = 0xff;
                    }
                    palette[i] = Color.FromRgb(r, g, b);
                    color_pos += color_size;
                }
                Palette = new BitmapPalette(palette);
            }
        private static bool CheckAreEqual(BitmapPalette first, BitmapPalette second)
        {
            if (first == null && second == null)
            {
                return(true);
            }

            if ((first == null && second != null) ||
                (first != null && second == null))
            {
                return(false);
            }

            if (first.Colors.Count != second.Colors.Count)
            {
                return(false);
            }

            for (int i = 0; i < first.Colors.Count; ++i)
            {
                if (first.Colors[i] != second.Colors[i])
                {
                    return(false);
                }
            }

            return(true);
        }