示例#1
0
        public static BitmapSource ILArrayToBitmapSource(ILArray <double> surface)
        {
            // Define parameters used to create the BitmapSource.
            PixelFormat pf        = PixelFormats.Bgr32;
            int         width     = surface.Dimensions[0];
            int         height    = surface.Dimensions[1];
            int         bytes     = (pf.BitsPerPixel + 7) / 8;
            int         rawStride = (width * bytes);

            byte[]    rawImage  = new byte[rawStride * height];
            int       index     = 0;
            ColourMap ColourMap = new ColourMap(ColourMapType.Jet, 256);

            byte[,] cmap = ColourMap.ToByteArray();
            double           range     = surface.MaxValue - surface.MinValue;
            double           min       = surface.MinValue;
            int              magnitude = 0;
            ILArray <int>    scaled    = (ILArray <int>)ILMath.convert(NumericType.Int32, ILMath.floor((surface - min) * 256.0 / range));
            ILIterator <int> iterator  = scaled.CreateIterator();
            Stopwatch        sw        = Stopwatch.StartNew();

            sw.Reset();
            sw.Start();
            magnitude = iterator.Value;
            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    if (magnitude == 256)
                    {
                        magnitude = 255;
                    }
                    rawImage[index]     = cmap[magnitude, 3];
                    rawImage[index + 1] = cmap[magnitude, 2];
                    rawImage[index + 2] = cmap[magnitude, 1];
                    rawImage[index + 3] = cmap[magnitude, 0];
                    index    += bytes;
                    magnitude = iterator.Increment();
                }
            }
            sw.Stop();
            string result;

            result = "Elapsed time: " + sw.ElapsedMilliseconds.ToString() + " ms";
            // Create a BitmapSource.
            BitmapSource bitmap = BitmapSource.Create(width, height,
                                                      96, 96, pf, null,
                                                      rawImage, rawStride);

            return(bitmap);
        }
示例#2
0
        public static BitmapSource ILArrayToBitmapSourceReversed(ILArray <double> surface, ColourMap colourMap)
        {
            // Define parameters used to create the BitmapSource.
            PixelFormat pf        = PixelFormats.Bgr32;
            int         width     = surface.Dimensions[0];
            int         height    = surface.Dimensions[1];
            int         bytes     = (pf.BitsPerPixel + 7) / 8;
            int         rawStride = (width * bytes);

            byte[] rawImage = new byte[rawStride * height];
            int    index    = 0;

            byte[,] cmap = colourMap.ToByteArray();
            int              colourMapLength = colourMap.Length;
            double           range           = surface.MaxValue - surface.MinValue;
            double           min             = surface.MinValue;
            int              magnitude       = 0;
            ILArray <int>    scaled          = (ILArray <int>)ILMath.convert(NumericType.Int32, ILMath.floor((surface - min) * (double)(colourMapLength - 1) / range));
            ILIterator <int> iterator        = scaled.CreateIterator();

            magnitude = iterator.Value;
            for (int y = height - 1; y >= 0; --y)
            {
                index = y * rawStride;
                for (int x = 0; x < width; ++x)
                {
                    rawImage[index]     = cmap[magnitude, 3];
                    rawImage[index + 1] = cmap[magnitude, 2];
                    rawImage[index + 2] = cmap[magnitude, 1];
                    rawImage[index + 3] = cmap[magnitude, 0];
                    index    += bytes;
                    magnitude = iterator.Increment();
                }
            }
            // Create a BitmapSource.
            BitmapSource bitmap = BitmapSource.Create(width, height,
                                                      96, 96, pf, null,
                                                      rawImage, rawStride);

            return(bitmap);
        }