Exemplo n.º 1
0
        /// <summary>
        /// Converts a list of KeyValuePairs into an image using a ColorMapper.
        /// </summary>
        /// <param name="rg">Specifies a KeyValuePair where the Value is converted to a color.</param>
        /// <param name="sz">Specifies the size of the image.</param>
        /// <param name="clrMap">Specifies a color mapper to use when converting each value into a color.</param>
        /// <returns>The Image of the data is returned.</returns>
        public static Bitmap GetImage(List <Result> rg, Size sz, ColorMapper clrMap)
        {
            Bitmap bmp   = new Bitmap(sz.Width, sz.Height);
            int    nSize = (int)Math.Ceiling(Math.Sqrt(rg.Count));
            float  fX    = 0;
            float  fY    = 0;
            float  fIncX = (float)sz.Width / (float)nSize;
            float  fIncY = (float)sz.Height / (float)nSize;

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.FillRectangle(Brushes.Black, new RectangleF(0, 0, bmp.Width, bmp.Height));

                for (int i = 0; i < rg.Count; i++)
                {
                    Brush br = new SolidBrush(clrMap.GetColor(rg[i].Score));
                    g.FillRectangle(br, fX, fY, fIncX, fIncY);
                    br.Dispose();

                    fX += fIncX;

                    if (fX >= bmp.Width)
                    {
                        fX  = 0;
                        fY += fIncY;
                    }
                }
            }

            return(bmp);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts a SimplDatum (or Datum) into an image, optionally using a ColorMapper.
        /// </summary>
        /// <param name="d">Specifies the Datum to use.</param>
        /// <param name="clrMap">Optionally, specifies a color mapper to use when converting each value into a color (default = null, not used).</param>
        /// <param name="rgClrOrder">Optionally, specifies the color ordering. Note, this list must have the same number of elements as there are channels.</param>
        /// <returns>The Image of the data is returned.</returns>
        public static Bitmap GetImage(SimpleDatum d, ColorMapper clrMap = null, List <int> rgClrOrder = null)
        {
            if (d.Channels != 1 && d.Channels != 3)
            {
                throw new Exception("Standard images only support either 1 or 3 channels.");
            }

            Bitmap bmp = new Bitmap(d.Width, d.Height);

            List <byte>[]   rgrgByteData = new List <byte> [d.Channels];
            List <double>[] rgrgRealData = new List <double> [d.Channels];
            int             nOffset      = 0;
            int             nCount       = d.Height * d.Width;
            bool            bDataIsReal  = d.HasRealData;
            double          dfMin        = 1;
            double          dfMax        = 0;


            for (int i = 0; i < d.Channels; i++)
            {
                List <byte>   rgByteData = new List <byte>();
                List <double> rgRealData = new List <double>();
                int           nChIdx     = i;

                if (rgClrOrder != null)
                {
                    nChIdx = rgClrOrder[i];
                }

                if (bDataIsReal)
                {
                    for (int j = 0; j < nCount; j++)
                    {
                        double dfVal = d.GetDataAtD(nOffset + j);
                        dfMin = Math.Min(dfMin, dfVal);
                        dfMax = Math.Max(dfMax, dfVal);
                        rgRealData.Add(dfVal);
                    }

                    rgrgRealData[nChIdx] = rgRealData;
                }
                else
                {
                    for (int j = 0; j < nCount; j++)
                    {
                        rgByteData.Add(d.ByteData[nOffset + j]);
                    }

                    rgrgByteData[nChIdx] = rgByteData;
                }

                nOffset += nCount;
            }

            LockBitmap bmp1 = new LockBitmap(bmp);

            try
            {
                bmp1.LockBits();

                for (int y = 0; y < bmp1.Height; y++)
                {
                    for (int x = 0; x < bmp1.Width; x++)
                    {
                        Color clr;
                        int   nIdx = (y * bmp1.Width) + x;

                        if (d.Channels == 1)
                        {
                            if (bDataIsReal)
                            {
                                if (dfMin >= 0 && dfMax <= 1.0)
                                {
                                    int nG = clip((int)(rgrgRealData[0][nIdx] * 255.0), 0, 255);
                                    clr = Color.FromArgb(nG, nG, nG);
                                }
                                else
                                {
                                    clr = Color.FromArgb((int)rgrgRealData[0][nIdx]);
                                }

                                if (clrMap != null)
                                {
                                    clr = clrMap.GetColor(clr.ToArgb());
                                }
                            }
                            else
                            {
                                int nR = clip((int)rgrgByteData[0][nIdx], 0, 255);
                                int nG = clip((int)rgrgByteData[0][nIdx], 0, 255);
                                int nB = clip((int)rgrgByteData[0][nIdx], 0, 255);

                                clr = Color.FromArgb(nR, nG, nB);
                            }
                        }
                        else
                        {
                            if (bDataIsReal)
                            {
                                int nR = clip((int)rgrgRealData[0][nIdx], 0, 255);
                                int nG = clip((int)rgrgRealData[1][nIdx], 0, 255);
                                int nB = clip((int)rgrgRealData[2][nIdx], 0, 255);

                                clr = Color.FromArgb(nR, nG, nB);

                                if (clrMap != null)
                                {
                                    clr = clrMap.GetColor(clr.ToArgb());
                                }
                            }
                            else
                            {
                                int nR = clip((int)rgrgByteData[0][nIdx], 0, 255);
                                int nG = clip((int)rgrgByteData[1][nIdx], 0, 255);
                                int nB = clip((int)rgrgByteData[2][nIdx], 0, 255);

                                clr = Color.FromArgb(nR, nG, nB);
                            }
                        }

                        bmp1.SetPixel(x, y, clr);
                    }
                }
            }
            catch (Exception excpt)
            {
                throw excpt;
            }
            finally
            {
                bmp1.UnlockBits();
            }

            return(bmp);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts a Datum into an image, optionally using a ColorMapper.
        /// </summary>
        /// <param name="d">Specifies the Datum to use.</param>
        /// <param name="clrMap">Optionally, specifies a color mapper to use when converting each value into a color (default = null, not used).</param>
        /// <param name="rgClrOrder">Optionally, specifies the color ordering. Note, this list must have the same number of elements as there are channels.</param>
        /// <returns>The Image of the data is returned.</returns>
        public static Image GetImage(Datum d, ColorMapper clrMap = null, List <int> rgClrOrder = null)
        {
            if (d.channels != 1 && d.channels != 3)
            {
                throw new Exception("Standard images only support either 1 or 3 channels.");
            }

            Bitmap bmp = new Bitmap(d.width, d.height);

            List <byte>[]   rgrgByteData = new List <byte> [d.channels];
            List <double>[] rgrgRealData = new List <double> [d.channels];
            int             nOffset      = 0;
            int             nCount       = d.height * d.width;
            bool            bDataIsReal  = (d.float_data != null && d.float_data.Length > 0) ? true : false;


            for (int i = 0; i < d.channels; i++)
            {
                List <byte>   rgByteData = new List <byte>();
                List <double> rgRealData = new List <double>();
                int           nChIdx     = i;

                if (rgClrOrder != null)
                {
                    nChIdx = rgClrOrder[i];
                }

                if (bDataIsReal)
                {
                    for (int j = 0; j < nCount; j++)
                    {
                        rgRealData.Add(d.float_data[nOffset + j]);
                    }

                    rgrgRealData[nChIdx] = rgRealData;
                }
                else
                {
                    for (int j = 0; j < nCount; j++)
                    {
                        rgByteData.Add(d.data[nOffset + j]);
                    }

                    rgrgByteData[nChIdx] = rgByteData;
                }

                nOffset += nCount;
            }

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color clr;
                    int   nIdx = (y * bmp.Width) + x;

                    if (d.channels == 1)
                    {
                        if (bDataIsReal)
                        {
                            clr = Color.FromArgb((int)rgrgRealData[0][nIdx]);

                            if (clrMap != null)
                            {
                                clr = clrMap.GetColor(clr.ToArgb());
                            }
                        }
                        else
                        {
                            clr = Color.FromArgb((int)rgrgByteData[0][nIdx], (int)rgrgByteData[0][nIdx], (int)rgrgByteData[0][nIdx]);
                        }
                    }
                    else
                    {
                        if (bDataIsReal)
                        {
                            clr = Color.FromArgb((int)rgrgRealData[0][nIdx], (int)rgrgRealData[1][nIdx], (int)rgrgRealData[2][nIdx]);

                            if (clrMap != null)
                            {
                                clr = clrMap.GetColor(clr.ToArgb());
                            }
                        }
                        else
                        {
                            clr = Color.FromArgb((int)rgrgByteData[0][nIdx], (int)rgrgByteData[1][nIdx], (int)rgrgByteData[2][nIdx]);
                        }
                    }

                    bmp.SetPixel(x, y, clr);
                }
            }

            return(bmp);
        }