Esempio n. 1
0
        /// <summary>
        /// Converts a bitmap into a new Bytemap.
        /// </summary>
        /// <param name="bmp">Specifies the bitmap.</param>
        /// <returns>A new Bytemap is returned.</returns>
        public static Bytemap FromImage(Bitmap bmp)
        {
            Bytemap    data = new Bytemap(3, bmp.Height, bmp.Width);
            LockBitmap bmpA = new LockBitmap(bmp);

            bmpA.LockBits();

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color clr = bmpA.GetPixel(x, y);
                    data.SetPixel(x, y, clr);
                }
            }

            bmpA.UnlockBits();

            return(data);
        }
Esempio n. 2
0
        /// <summary>
        /// Converts the Bytemap into a Bitmap.
        /// </summary>
        /// <returns>A new bitmap is returned.</returns>
        public Bitmap ToImage()
        {
            Bitmap     bmp  = new Bitmap(m_nWidth, m_nHeight);
            LockBitmap bmpA = new LockBitmap(bmp);

            bmpA.LockBits();

            for (int y = 0; y < m_nHeight; y++)
            {
                for (int x = 0; x < m_nWidth; x++)
                {
                    Color clr = GetPixel(x, y);
                    bmpA.SetPixel(x, y, clr);
                }
            }

            bmpA.UnlockBits();

            return(bmp);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        /// <summary>
        /// The GetImageDataD function converts a Bitmap into a Datum using the <i>double</i> type for real data.
        /// </summary>
        /// <param name="bmp">Specifies the Bitmap containing the image.</param>
        /// <param name="nChannels">Specifies the number of channels contained in the Bitmap (e.g. 3 = color, 1 = black and white).</param>
        /// <param name="bDataIsReal">Specifies whether or not to add each color to the List of <i>double</i> or to the list of <i>byte</i>.  Using the <i>byte</i> array is more common for it already separates a 3 color Bitmap into 3 channels of data.</param>
        /// <param name="nLabel">Specifies the known label.</param>
        /// <param name="bUseLockBitmap">Optionally, use the Lock Bitmap which is faster but may produce corrupted images in a few scenarios (default = true).</param>
        /// <returns>The Datum representing the image is returned.</returns>
        public static Datum GetImageDataD(Bitmap bmp, int nChannels, bool bDataIsReal, int nLabel, bool bUseLockBitmap = true)
        {
            if (nChannels != 1 && nChannels != 3)
            {
                throw new Exception("Images only support either 1 or 3 channels.");
            }

            List <byte>[]   rgrgByteData = new List <byte> [nChannels];
            List <double>[] rgrgRealData = new List <double> [nChannels];

            for (int i = 0; i < nChannels; i++)
            {
                rgrgByteData[i] = new List <byte>();
                rgrgRealData[i] = new List <double>();
            }

            if (bmp.Width >= bmp.Height && bUseLockBitmap)
            {
                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 = bmp1.GetPixel(x, y);

                            if (nChannels == 1)
                            {
                                if (bDataIsReal)
                                {
                                    rgrgRealData[0].Add(clr.ToArgb());
                                }
                                else
                                {
                                    rgrgByteData[0].Add((byte)((clr.R * 0.3) + (clr.G * 0.59) + (clr.B * 0.11)));
                                }
                            }
                            else
                            {
                                if (bDataIsReal)
                                {
                                    rgrgRealData[0].Add(clr.R);
                                    rgrgRealData[1].Add(clr.G);
                                    rgrgRealData[2].Add(clr.B);
                                }
                                else
                                {
                                    rgrgByteData[0].Add(clr.R);
                                    rgrgByteData[1].Add(clr.G);
                                    rgrgByteData[2].Add(clr.B);
                                }
                            }
                        }
                    }
                }
                catch (Exception excpt)
                {
                    throw excpt;
                }
                finally
                {
                    bmp1.UnlockBits();
                }
            }
            // LockBitmap currently has a bug with images were bmp.Width < bmp.Height so in this case we use the slower Bitmap.GetPixel.
            else
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    for (int x = 0; x < bmp.Width; x++)
                    {
                        Color clr = bmp.GetPixel(x, y);

                        if (nChannels == 1)
                        {
                            if (bDataIsReal)
                            {
                                rgrgRealData[0].Add(clr.ToArgb());
                            }
                            else
                            {
                                rgrgByteData[0].Add((byte)((clr.R * 0.3) + (clr.G * 0.59) + (clr.B * 0.11)));
                            }
                        }
                        else
                        {
                            if (bDataIsReal)
                            {
                                rgrgRealData[0].Add(clr.R);
                                rgrgRealData[1].Add(clr.G);
                                rgrgRealData[2].Add(clr.B);
                            }
                            else
                            {
                                rgrgByteData[0].Add(clr.R);
                                rgrgByteData[1].Add(clr.G);
                                rgrgByteData[2].Add(clr.B);
                            }
                        }
                    }
                }
            }

            List <byte>   rgByteData = new List <byte>();
            List <double> rgRealData = new List <double>();

            for (int i = 0; i < nChannels; i++)
            {
                rgByteData.AddRange(rgrgByteData[i]);
                rgRealData.AddRange(rgrgRealData[i]);
            }

            if (bDataIsReal)
            {
                return(new Datum(true, nChannels, bmp.Width, bmp.Height, nLabel, DateTime.MinValue, new List <double>(rgRealData), 0, false, -1));
            }
            else
            {
                return(new Datum(false, nChannels, bmp.Width, bmp.Height, nLabel, DateTime.MinValue, new List <byte>(rgByteData), 0, false, -1));
            }
        }