//------------------------------------------------------------------------------------------------
        //Создание изображения из матриц R G B
        public static WriteableBitmap CreateWriteableBitmapFromMatricesRGB(
            IntegerMatrix redMatrix, IntegerMatrix greenMatrix, IntegerMatrix blueMatrix,
            int dpiX, int dpiY
            )
        {
            int             width       = redMatrix.ColumnCount;
            int             height      = redMatrix.RowCount;
            PixelFormat     pixelFormat = PixelFormats.Bgra32;
            WriteableBitmap newImage    = WriteableBitmapCreator.CreateWriteableBitmap
                                              (width, height, dpiX, dpiY, pixelFormat);
            WriteableBitmapWrapper wrapper = WriteableBitmapWrapper.Create(newImage);

            byte[] pixelBytes     = new byte[4 * width * height]; //BGRA
            int    pixelByteIndex = 0;

            for (int y = 0; y < newImage.PixelHeight; y++)
            {
                for (int x = 0; x < newImage.PixelWidth; x++)
                {
                    pixelBytes[pixelByteIndex++] = Convert.ToByte(blueMatrix[y, x]);
                    pixelBytes[pixelByteIndex++] = Convert.ToByte(greenMatrix[y, x]);
                    pixelBytes[pixelByteIndex++] = Convert.ToByte(redMatrix[y, x]);
                    pixelBytes[pixelByteIndex++] = byte.MaxValue;
                }
            }

            wrapper.WritePixels(pixelBytes);

            return(newImage);
        }
        //------------------------------------------------------------------------------------------------
        //Создание изображения из матрицы интенсивностей серого по шаблону
        public static WriteableBitmap CreateGrayScaleWriteableBitmapFromMatrix(
            RealMatrix grayScaleMatrix,
            int dpiX, int dpiY, BitMask2D bitMask
            )
        {
            int             pixelWidth      = grayScaleMatrix.ColumnCount;
            int             pixelHeight     = grayScaleMatrix.RowCount;
            WriteableBitmap writeableBitmap = WriteableBitmapCreator.CreateWriteableBitmap
                                                  (pixelWidth, pixelHeight, dpiX, dpiY, PixelFormats.Bgra32);
            WriteableBitmapWrapper bitmapWrapper = WriteableBitmapWrapper.Create(writeableBitmap);

            for (int x = 0; x < pixelWidth; x++)
            {
                for (int y = 0; y < pixelHeight; y++)
                {
                    if (bitMask[y, x] == true)
                    {
                        int  grayIntensity = ( int )Math.Round(grayScaleMatrix[y, x]);
                        byte red, green, blue;
                        red = green = blue = Convert.ToByte(grayIntensity);
                        Color color = Color.FromRgb(red, green, blue);
                        bitmapWrapper.SetPixelColor(x, y, color);
                    }
                }
            }
            return(writeableBitmap);
        }
 //------------------------------------------------------------------------------------------------
 //Создание изображений из файлов
 public static WriteableBitmap[] CreateWriteableBitmapsFromFiles(params string[] fileNames)
 {
     WriteableBitmap[] writeableBitmaps = new WriteableBitmap[fileNames.Length];
     for (int index = 0; index < fileNames.Length; index++)
     {
         string          fileName        = fileNames[index];
         WriteableBitmap writeableBitmap =
             WriteableBitmapCreator.CreateWriteableBitmapFromFile(fileName);
         writeableBitmaps[index] = writeableBitmap;
     }
     return(writeableBitmaps);
 }
 //-----------------------------------------------------------------------------------------------
 //-----------------------------------------------------------------------------------------------
 //Создание изображений из матриц интенсивностей серого
 public static WriteableBitmap[] CreateGrayScaleWriteableBitmapsFromMatrices(
     int dpiX, int dpiY, params RealMatrix[] matrices
     )
 {
     WriteableBitmap[] writeableBitmaps = new WriteableBitmap[matrices.Length];
     for (int index = 0; index < matrices.Length; index++)
     {
         RealMatrix      matrix          = matrices[index];
         WriteableBitmap writeableBitmap =
             WriteableBitmapCreator.CreateGrayScaleWriteableBitmapFromMatrix(matrix, dpiX, dpiY);
         writeableBitmaps[index] = writeableBitmap;
     }
     return(writeableBitmaps);
 }
        //-----------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------
        //Новое изображение c заданными dpiX, dpiY
        public WriteableBitmap GetNewWriteableBitmap(int dpiX, int dpiY)
        {
            int pixelWidth  = this.Image.PixelWidth;
            int pixelHeight = this.Image.PixelHeight;
            int stride      = this.GetStride();

            Int32Rect rect = new Int32Rect(0, 0, pixelWidth, pixelHeight);

            byte[] pixelsBytes = new byte[this.SizeInBytes];
            this.Image.CopyPixels(rect, pixelsBytes, stride, 0);

            media.PixelFormat pixelFormat = this.Image.Format;
            WriteableBitmap   newImage    = WriteableBitmapCreator.CreateWriteableBitmap
                                                (pixelWidth, pixelHeight, dpiX, dpiY, pixelFormat);

            newImage.WritePixels(rect, pixelsBytes, stride, 0);
            return(newImage);
        }
        //-------------------------------------------------------------------------------------------------
        //Создание изображений из матриц R G B
        public static WriteableBitmap[] CreateWriteableBitmapsFromMatricesRGB(
            RealMatrix[] redMatrices, RealMatrix[] greenMatrices, RealMatrix[] blueMatrices,
            int dpiX, int dpiY
            )
        {
            int count = redMatrices.Length;

            WriteableBitmap[] bitmaps = new WriteableBitmap[count];
            for (int index = 0; index < count; index++)
            {
                RealMatrix      redMatrix   = redMatrices[index];
                RealMatrix      greenMatrix = greenMatrices[index];
                RealMatrix      blueMatrix  = blueMatrices[index];
                WriteableBitmap bitmap      =
                    WriteableBitmapCreator.CreateWriteableBitmapFromMatricesRGB
                        (redMatrix, greenMatrix, blueMatrix, dpiX, dpiY);
                bitmaps[index] = bitmap;
            }
            return(bitmaps);
        }
        //-----------------------------------------------------------------------------------------------------


        //-----------------------------------------------------------------------------------------------------
        //Обрезка изображения
        public WriteableBitmap GetSubBitmap(System.Drawing.Point leftTop, System.Drawing.Point rightBottom)
        {
            int newImageWidth  = Convert.ToInt32(Math.Abs(rightBottom.X - leftTop.X)) + 1;
            int newImageHeight = Convert.ToInt32(Math.Abs(rightBottom.Y - leftTop.Y)) + 1;

            media.PixelFormat pixelFormat = this.Image.Format;
            int             dpiX          = Convert.ToInt32(OS.OS.SystemDpiX);
            int             dpiY          = Convert.ToInt32(OS.OS.SystemDpiY);
            WriteableBitmap newBitmap     = WriteableBitmapCreator.CreateWriteableBitmap
                                                (newImageWidth, newImageHeight, dpiX, dpiY, pixelFormat);
            WriteableBitmapWrapper newImageWrapper = WriteableBitmapWrapper.Create(newBitmap);

            for (int y = leftTop.Y, newY = 0; y <= rightBottom.Y; y++, newY++)
            {
                for (int x = leftTop.X, newX = 0; x <= rightBottom.X; x++, newX++)
                {
                    media.Color color = this.GetPixelColor(x, y);
                    newImageWrapper.SetPixelColor(newX, newY, color);
                }
            }

            return(newBitmap);
        }
        //-----------------------------------------------------------------------------------------------
        public WriteableBitmap GetExtendedWriteableBitmapByOnePixelToBorder()
        {
            int pixelWidth  = this.Image.PixelWidth;
            int pixelHeight = this.Image.PixelHeight;
            int stride      = this.GetStride();

            int dpiX = OS.OS.IntegerSystemDpiX;
            int dpiY = OS.OS.IntegerSystemDpiY;

            Int32Rect rect = new Int32Rect(0, 0, pixelWidth, pixelHeight);

            byte[] pixelsBytes = new byte[this.SizeInBytes];
            this.Image.CopyPixels(rect, pixelsBytes, stride, 0);

            media.PixelFormat pixelFormat = this.Image.Format;

            WriteableBitmap newImage = WriteableBitmapCreator.CreateWriteableBitmap
                                           (pixelWidth + 2, pixelHeight + 2, dpiX, dpiY, pixelFormat);

            Int32Rect newRect = new Int32Rect(1, 1, pixelWidth, pixelHeight);

            newImage.WritePixels(newRect, pixelsBytes, stride, 0);
            return(newImage);
        }
        //------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------

        /*
         * //Создание изображения из файла
         * public static WriteableBitmap CreateWriteableBitmapFromFile( string fileName, double dpiX, double dpiY ) {
         *  System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( fileName );
         *
         *  //System.Windows.Media.PixelFormat targetPixelFormat = PixelFormats.Rgba64;
         *  System.Windows.Media.PixelFormat targetPixelFormat = PixelFormats.Pbgra32;
         *
         *  WriteableBitmap writeableBitmap =
         *      new WriteableBitmap( bitmap.Width, bitmap.Height, dpiX, dpiY, targetPixelFormat, null );
         *
         *  //WriteableBitmap writeableBitmap =
         *  //    new WriteableBitmap( bitmap.Width, bitmap.Height, dpiX, dpiY, PixelFormats.Rgba64, null );
         *
         *  System.Drawing.Rectangle rect = new System.Drawing.Rectangle( 0, 0, bitmap.Width, bitmap.Height );
         *  System.Drawing.Imaging.ImageLockMode lockMode = System.Drawing.Imaging.ImageLockMode.ReadOnly;
         *
         *  System.Drawing.Imaging.PixelFormat pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
         *  //System.Drawing.Imaging.PixelFormat pixelFormat = System.Drawing.Imaging.PixelFormat.Format64bppArgb;
         *  //System.Drawing.Imaging.PixelFormat pixelFormat = System.Drawing.Imaging.PixelFormat.Format16bppGrayScale;
         *
         *  System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits( rect, lockMode, pixelFormat );
         *  Int32Rect int32Rect = new System.Windows.Int32Rect( 0, 0, bitmapData.Width, bitmapData.Height );
         *
         *  writeableBitmap.WritePixels
         *      ( int32Rect, bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride );
         *  bitmap.UnlockBits( bitmapData );
         *  bitmap.Dispose();
         *
         *  return writeableBitmap;
         * }
         */
        //------------------------------------------------------------------------------------------------
        public static ExtraImageInfo CreateWriteableBitmapFromFile(string fileName, double dpiX, double dpiY)
        {
            //public static WriteableBitmap CreateWriteableBitmapFromFile( string fileName, double dpiX, double dpiY ) {

            /*
             * if ( fileName.ToUpper().EndsWith( "CR2" ) ) {
             *
             *  MagickImage magicImage = new MagickImage( fileName );
             *
             *  int width = magicImage.Width;
             *  int height = magicImage.Height;
             *  PixelFormat pixelFormat = PixelFormats.Bgra32;
             *
             *  WriteableBitmap resultWriteableBitmap =
             *      WriteableBitmapCreator.CreateWriteableBitmap( width, height, (int)dpiX, (int)dpiY, pixelFormat );
             *  WriteableBitmapWrapper imageWrapper = WriteableBitmapWrapper.Create( resultWriteableBitmap );
             *
             *  PixelCollection pixelCollection = magicImage.GetPixels();
             *
             *  Interval<double> interval1 = new Interval<double>( 0, ushort.MaxValue );
             *  Interval<double> interval2 = new Interval<double>( 0, byte.MaxValue );
             *  RealIntervalTransform intervalTransform = new RealIntervalTransform( interval1, interval2 );
             *
             *  RealMatrix redMatrix = new RealMatrix( height, width );
             *
             *  for ( int x = 0; x < width; x++ ) {
             *      for ( int y = 0; y < height; y++ ) {
             *          Pixel pixel = pixelCollection.GetPixel(x, y);
             *          MagickColor magicColor = pixel.ToColor();
             *
             *          redMatrix[ y, x ] = intervalTransform.TransformToFinishIntervalValue(magicColor.R);
             *
             *          byte a = Convert.ToByte( intervalTransform.TransformToFinishIntervalValue( magicColor.A ) );
             *          byte r = Convert.ToByte( intervalTransform.TransformToFinishIntervalValue( magicColor.R ) );
             *          byte g = Convert.ToByte( intervalTransform.TransformToFinishIntervalValue( magicColor.G ) );
             *          byte b = Convert.ToByte( intervalTransform.TransformToFinishIntervalValue( magicColor.B ) );
             *
             *          Color color = Color.FromArgb( a, r, g, b );
             *
             *          imageWrapper.SetPixelColor( x, y, color );
             *      }
             *  }
             *
             *  ExtraImageInfo extraImageInfo = new ExtraImageInfo();
             *  extraImageInfo.RedMatrix = redMatrix;
             *  extraImageInfo.Image = resultWriteableBitmap;
             *
             *  //return resultWriteableBitmap;
             *  return extraImageInfo;
             *
             * }
             */

            /*
             * if ( fileName.ToUpper().EndsWith( "CR2" ) ) {
             *
             *
             *  //System.GC.Collect();
             *
             *  //BitmapDecoder bitmapDecoder =
             *  //    BitmapDecoder.Create( new Uri( fileName ),
             *  //    BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None );
             *
             *  //BitmapFrame frame = bitmapDecoder.Frames[ 0 ];
             *
             *  //WriteableBitmap bitmap = new WriteableBitmap( frame );
             *  //WriteableBitmapWrapper wrapper = WriteableBitmapWrapper.Create( bitmap );
             *
             *  //Color c = wrapper.GetPixelColor( 300, 300 );
             *
             *
             *  System.GC.Collect();
             *
             *  uint error;
             *
             *  IntPtr imgRef;
             *  //Open image
             *  IntPtr inStream;
             *  error = EDSDK.EdsCreateFileStream(fileName, EDSDK.EdsFileCreateDisposition.OpenExisting, EDSDK.EdsAccess.Read, out inStream);
             *  EDSDK.EdsCreateImageRef(inStream, out imgRef);
             *  EDSDK.EdsRelease(inStream);
             *
             *  string convertedFileName = Path.ChangeExtension(fileName, "TIFF");
             *
             *  //Save image
             *  IntPtr outStream;
             *  var settings = new EDSDK.EdsSaveImageSetting();
             *  error = EDSDK.EdsCreateFileStream(convertedFileName, EDSDK.EdsFileCreateDisposition.CreateAlways, EDSDK.EdsAccess.Write, out outStream);
             *  error = EDSDK.EdsSaveImage(imgRef, EDSDK.EdsTargetImageType.TIFF16, settings, outStream);
             *  EDSDK.EdsRelease(outStream);
             *
             *  ExtraImageInfo extraImageInfo = new ExtraImageInfo();
             *  return extraImageInfo;
             *
             * }
             */
            if (fileName.ToUpper().EndsWith("CR2"))
            {
                Interval <double> finishInterval = new Interval <double>(0, 255);

                IntegerMatrix matrix = RawReader.ReadImageFromFile(fileName);
                //matrix = matrix.GetSubMatrix(0, 2640 - 1, matrix.RowCount - 1, matrix.ColumnCount - 1);


                int min = matrix.GetMinValue();
                int max = matrix.GetMaxValue();

                IntegerMatrix redMatrix = InterpolationHelper.GetRedMatrix(matrix);
                int           minRed    = redMatrix.GetMinValue();
                int           maxRed    = redMatrix.GetMaxValue();

                IntegerMatrix greenMatrix = InterpolationHelper.GetGreenMatrix(matrix);
                int           minGreen    = greenMatrix.GetMinValue();
                int           maxGreen    = greenMatrix.GetMaxValue();

                IntegerMatrix blueMatrix = InterpolationHelper.GetBlueMatrix(matrix);
                int           minBlue    = blueMatrix.GetMinValue();
                int           maxBlue    = blueMatrix.GetMaxValue();

                int minValue = (new int[] { minRed, minGreen, minBlue }).Min();
                int maxValue = (new int[] { maxRed, maxGreen, maxBlue }).Max();

                RealIntervalTransform redIntervalTransform =
                    new RealIntervalTransform(new Interval <double>(minValue, maxValue), finishInterval);
                IntegerMatrix resRedMatrix =
                    IntegerMatrixValuesTransform.TransformMatrixValues(redMatrix, redIntervalTransform);

                RealIntervalTransform greenIntervalTransform =
                    new RealIntervalTransform(new Interval <double>(minValue, maxValue), finishInterval);
                IntegerMatrix resGreenMatrix =
                    IntegerMatrixValuesTransform.TransformMatrixValues(greenMatrix, greenIntervalTransform);

                RealIntervalTransform blueIntervalTransform =
                    new RealIntervalTransform(new Interval <double>(minValue, maxValue), finishInterval);
                IntegerMatrix resBlueMatrix =
                    IntegerMatrixValuesTransform.TransformMatrixValues(blueMatrix, blueIntervalTransform);

                WriteableBitmap resImage =
                    WriteableBitmapCreator.CreateWriteableBitmapFromMatricesRGB
                        (resRedMatrix, resGreenMatrix, resBlueMatrix, OS.OS.IntegerSystemDpiX, OS.OS.IntegerSystemDpiY);

                /*
                 * IntegerMatrix resMatrix =
                 *  IntegerMatrixValuesTransform.TransformMatrixValuesToFinishIntervalValues(matrix, finishInterval);
                 *
                 * WriteableBitmap resImage =
                 *  WriteableBitmapCreator.CreateGrayScaleWriteableBitmapFromMatrix
                 *  (resMatrix, OS.OS.IntegerSystemDpiX, OS.OS.IntegerSystemDpiY);
                 */

                ExtraImageInfo extraImageInfo = new ExtraImageInfo();
                extraImageInfo.Image = resImage;

                return(extraImageInfo);
            }

            else
            {
                Stream imageStreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

                BitmapDecoder decoder      = BitmapDecoder.Create(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource  bitmapSource = decoder.Frames[0];

                WriteableBitmap resultWriteableBitmap = new WriteableBitmap(bitmapSource);

                ExtraImageInfo extraImageInfo = new ExtraImageInfo();
                extraImageInfo.Image = resultWriteableBitmap;

                return(extraImageInfo);
            }
        }