Exemplo n.º 1
0
        /// <summary>
        /// Clone the complex image
        /// </summary>
        ///
        /// <returns>Returns copy of the image.</returns>
        ///
        public object Clone( )
        {
            // create new complex image
            ComplexImage dstImage = new ComplexImage(width, height);

            Complex[,]      data = dstImage.data;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    data[i, j] = this.data[i, j];
                }
            }

            return(dstImage);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates complex image from bitmap
        /// </summary>
        ///
        /// <param name="srcImage">Source bitmap</param>
        ///
        /// <returns>Returns an instance of complex image.</returns>
        ///
        public static ComplexImage FromBitmap(Bitmap srcImage)
        {
            // get source image size
            int width  = srcImage.Width;
            int height = srcImage.Height;

            // check image size
            if (
                (!Tools.IsPowerOf2(width)) ||
                (!Tools.IsPowerOf2(height))
                )
            {
                throw new ArgumentException("Image width and height should be power of 2");
            }

            // create new complex image
            ComplexImage dstImage = new ComplexImage(width, height);

            Complex[,]              data = dstImage.data;

            // lock source bitmap data
            BitmapData srcData = srcImage.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, srcImage.PixelFormat);

            int offset = srcData.Stride - ((srcImage.PixelFormat == PixelFormat.Format8bppIndexed) ? width : width * 3);

            // do the job
            unsafe
            {
                byte *src = (byte *)srcData.Scan0.ToPointer( );

                if (srcImage.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    // grayscale image

                    // for each line
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++, src++)
                        {
                            data[y, x].Re = (float)*src / 255;
                        }
                        src += offset;
                    }
                }
                else
                {
                    // RGB image

                    // for each line
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++, src += 3)
                        {
                            data[y, x].Re = (0.2125 * src[RGB.R] + 0.7154 * src[RGB.G] + 0.0721 * src[RGB.B]) / 255;
                        }
                        src += offset;
                    }
                }
            }
            // unlock source images
            srcImage.UnlockBits(srcData);

            return(dstImage);
        }