private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == true)
            {
                string OpenedFilePath = openFileDialog1.FileName;
                FilePath.Text = OpenedFilePath;
                ImageMatrix   = ImageOperations.OpenImage(OpenedFilePath);
                ImageOperations.DisplayImage(ImageMatrix, ImageBox);
                WidthTextBox.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
                HeightTextBox.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
                int x = int.Parse(WidthTextBox.Text), y = int.Parse(HeightTextBox.Text);

                SeamCarving.leaveIt = new bool[y, x];      // O(1)
                // O(y * x)
                for (int i = 0; i < y; i++)                // O(y)
                {
                    for (int j = 0; j < x; j++)            // O(x)
                    {
                        SeamCarving.leaveIt[i, j] = false; // O(1)
                    }
                }
            }
        }
Пример #2
0
        //Сгенерировать рельеф
        public void GenerateReliev()
        {
            rand = new Random();

            landscapeBlock = new BlockSource(0, 0, sizeX, sizeZ)
            {
                H = loc.TerrainThickness
            };
            Land     = landscapeBlock.Land;
            Colors   = landscapeBlock.Colors;
            UpColors = new MyColor[sizeX, sizeZ];

            var Relievos = loc.Relievos;

            foreach (var relievo in Relievos)
            {
                relievo.Init(this);
            }

            foreach (var relievo in Relievos)
            {
                relievo.AfterInit(this);
            }

            for (int i = 0; i < sizeX; i++)
            {
                for (int j = 0; j < sizeZ; j++)
                {
                    if (UpColors[i, j] == null)
                    {
                        UpColors[i, j] = Colors[i, j];
                    }
                }
            }
        }
Пример #3
0
        private static Color[,] Convert_MyColor_Color(MyColor[,] Image)
        {
            int Height = Image.GetLength(0);
            int Width  = Image.GetLength(1);

            Color[,] image = new Color[Height, Width];
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    int x = Image[i, j].red;
                    image[i, j].R = (double)(x);
                    x             = Image[i, j].green;
                    image[i, j].G = (double)(x);
                    x             = Image[i, j].blue;
                    image[i, j].B = (double)(x);
                }
            }
            //Color black = new Color(0, 0, 0);
            //for (int i = 0; i < Height + margin; i++)
            //{
            //    for (int j = 0; j <= margin / 2; j++)
            //        image[i, j] = image[i, (Width + margin) - 1 - j] = black;
            //}
            //for (int j = 0; j < Width + margin; j++)
            //{
            //    for (int i = 0; i <= margin / 2; i++)
            //        image[i, j] = image[(Height + margin) - 1 - i,j] = black;
            //}
            return(image);
        }
        /// <summary>
        /// Constructeur de la texture
        /// Permet de lire un fichier se trouvant dans le dossier
        /// textures du projet
        /// </summary>
        /// <param name="filename">Le nom du fichier a charger</param>
        public Texture(string filename)
        {
            string basePath = System.IO.Path.GetFullPath("..\\..");
            string fullPath = System.IO.Path.Combine(basePath, "textures", filename);
            Bitmap bitmap   = new Bitmap(fullPath);

            Height = bitmap.Height;
            Width  = bitmap.Width;
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int        stride     = bitmapData.Stride;

            Color = new MyColor[Width, Height];

            unsafe
            {
                byte *ptr = (byte *)bitmapData.Scan0;
                for (int x = 0; x < Width; x++)
                {
                    for (int y = 0; y < Height; y++)
                    {
                        byte red, green, blue;
                        blue  = ptr[(x * 3) + y * stride];
                        green = ptr[(x * 3) + y * stride + 1];
                        red   = ptr[(x * 3) + y * stride + 2];
                        Color[x, y].From255(red, green, blue);
                    }
                }
            }
            bitmap.UnlockBits(bitmapData);
            bitmap.Dispose();
        }
        private void btnGammaNormal_Click(object sender, EventArgs e)
        {
            GammaCorrectedImageMatrix = ImageOperations.CalculateGammaCorrection_Normal(OriginalImageMatrix, GammaCorruptedImageMatrix);

            textBox3.Text = ImageOperations.gammavalueNormal.ToString();

            ImageOperations.DisplayImage(GammaCorrectedImageMatrix, pictureBox2);
        }
        private void BW_DoWork(object sender, DoWorkEventArgs e)
        {
            SeamCarving.TempMatrix = ImageMatrix;
            BackgroundWorker worker = sender as BackgroundWorker;

            stopWatch.Start();
            dispatcherTimer.Start();
            resizedImage = SeamCarving.Resize(ImageMatrix, newHeight, newWidth, worker);
            ImageMatrix  = resizedImage;
            stopWatch.Stop();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Open the browsed image and display it
                string OpenedFilePath = openFileDialog1.FileName;
                GammaCorruptedImageMatrix = ImageOperations.OpenImage(OpenedFilePath);
                ImageOperations.DisplayImage(GammaCorruptedImageMatrix, pictureBox2);
            }
        }
Пример #8
0
 public void Mirror()
 {
     MyColor[,] pixels = new MyColor[this.biWidth, this.biHeight];
     for (int i = 0; i < this.biWidth; i++)
     {
         for (int j = 0; j < this.biHeight; j++)
         {
             pixels[i, j] = this.picture[this.biWidth - i - 1, j];
         }
     }
     this.picture = pixels;
 }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Open the browsed image and display it
                string OpenedFilePath = openFileDialog1.FileName;
                OriginalImageMatrix = ImageOperations.OpenImage(OpenedFilePath);
                ImageOperations.DisplayImage(OriginalImageMatrix, pictureBox1);
            }
            textBox1.Text = ImageOperations.GetWidth(OriginalImageMatrix).ToString();
            textBox2.Text = ImageOperations.GetHeight(OriginalImageMatrix).ToString();
        }
Пример #10
0
        public void Transpose()
        {
            MyColor[,] pixels = new MyColor[this.biHeight, this.biWidth];

            for (int i = 0; i < this.biHeight; i++)
            {
                for (int j = 0; j < this.biWidth; j++)
                {
                    pixels[i, j] = this.picture[this.biWidth - j - 1, this.biHeight - i - 1];
                }
            }
            this.picture = pixels;
            int x = this.biWidth;

            this.biWidth  = this.biHeight;
            this.biHeight = x;
        }
Пример #11
0
        //Размыть цвет вокселей
        public void BloorVoxels()
        {
            var newColors = new MyColor[sizeX, sizeZ];

            for (int x = 0; x < sizeX; x++)
            {
                for (int z = 0; z < sizeZ; z++)
                {
                    bool    haveOther = false;
                    MyColor m         = UpColors[x, z];
                    if (m == null)
                    {
                        continue;
                    }

                    int   c = 2;
                    float r = m.R * c, g = m.G * c, b = m.B * c;

                    for (int x2 = Math.Max(0, x - 1); x2 <= x + 1 && x2 < sizeX; x2++)
                    {
                        for (int z2 = Math.Max(0, z - 1); z2 <= z + 1 && z2 < sizeZ; z2++)
                        {
                            if (x2 != x && z2 != z && Math.Abs(Land[x, z] - Land[x2, z2]) < 3)
                            {
                                c++;
                                MyColor m2 = UpColors[x2, z2];
                                r += m2.R;
                                g += m2.G;
                                b += m2.B;
                            }
                        }
                    }

                    if (haveOther)
                    {
                        newColors[x, z] = MyColor.FromArgb((byte)(r / c), (byte)(g / c), (byte)(b / c));
                    }
                    else
                    {
                        newColors[x, z] = m;
                    }
                }
            }

            UpColors = newColors;
        }
Пример #12
0
        public MyColor[,] Colors;    //цвет вокселей

        public BlockSource(int offsetX, int offsetZ, int sizex, int sizez)
        {
            OffsetX = offsetX;
            OffsetZ = offsetZ;
            SizeX   = sizex;
            SizeZ   = sizez;

            Land = new int[SizeX + 1, SizeZ + 1];

            for (int i = 0; i < SizeX; i++)
            {
                for (int j = 0; j < SizeZ; j++)
                {
                    Land[i, j] = int.MinValue;
                }
            }

            Colors = new MyColor[SizeX + 1, SizeZ + 1];
        }
Пример #13
0
        //------纹理部分------
        public void LoadTexture()
        {
            Image  img = Image.FromFile(TexturePath);
            Bitmap bm  = new Bitmap(img);

            this.TextureHeight = bm.Height;
            this.TextureWidth  = bm.Width;
            this.TextureImage  = new MyColor[TextureHeight, TextureWidth];
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    this.TextureImage[y, x]   = new MyColor();
                    this.TextureImage[y, x].r = bm.GetPixel(x, y).R;
                    this.TextureImage[y, x].g = bm.GetPixel(x, y).G;
                    this.TextureImage[y, x].b = bm.GetPixel(x, y).B;
                    //getpixel是从左下角开始算先X后Y 纹理图是从右上角开始算,先行后列
                }
            }
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            newWidth  = int.Parse(WidthTextBox.Text);   // O(1)
            newHeight = int.Parse(HeightTextBox.Text);  // O(1)

            if (TypesComboBox.SelectedIndex == 0)
            {
                resizedImage = ImageOperations.NormalResize(ImageMatrix, newHeight, newWidth);
                ImageOperations.DisplayImage(resizedImage, ImageBox);
            }
            else if (TypesComboBox.SelectedIndex == 1)
            {
                BW.DoWork               += BW_DoWork;
                ProgressBar.Minimum      = 1;
                ProgressBar.Maximum      = 100;
                BW.ProgressChanged      += BW_ProgressChanged;
                BW.WorkerReportsProgress = true;
                BW.RunWorkerCompleted   += BW_RunWorkerCompleted;
                BW.RunWorkerAsync();
            }
        }
Пример #15
0
        public void DataToArray()
        {
            this.picture = new MyColor[this.biWidth, this.biHeight];
            int lineLen = this.pixelData.Length / this.biHeight;

            for (int i = 0; i < this.biHeight; i++)
            {
                int j   = 0;
                int pos = 0;
                while (j < this.biWidth)
                {
                    MyColor c = new MyColor();
                    c.Blue = this.pixelData[pos + lineLen * i];
                    pos++;
                    c.Green = this.pixelData[pos + lineLen * i];
                    pos++;
                    c.Red = this.pixelData[pos + lineLen * i];
                    pos++;
                    this.picture[j, i] = c;
                    j++;
                }
            }
        }
        /// <summary>
        /// Display the given image on the given PictureBox object
        /// </summary>
        /// <param name="ImageMatrix">2D array that contains the image</param>
        /// <param name="PicBox">PictureBox object to display the image on it</param>
        public static void DisplayImage(MyColor[,] ImageMatrix, System.Windows.Controls.Image ImageBox)
        {
            // Create Image:
            //==============
            int Height = ImageMatrix.GetLength(0);
            int Width  = ImageMatrix.GetLength(1);

            Bitmap ImageBMP = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);

            unsafe
            {
                BitmapData bmd    = ImageBMP.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, ImageBMP.PixelFormat);
                int        nWidth = 0;
                nWidth = Width * 3;
                int   nOffset = bmd.Stride - nWidth;
                byte *p       = (byte *)bmd.Scan0;
                for (int i = 0; i < Height; i++)
                {
                    for (int j = 0; j < Width; j++)
                    {
                        p[0] = ImageMatrix[i, j].red;
                        p[1] = ImageMatrix[i, j].green;
                        p[2] = ImageMatrix[i, j].blue;
                        p   += 3;
                    }

                    p += nOffset;
                }
                ImageBMP.UnlockBits(bmd);
            }
            ImageBox.Height = Height;
            ImageBox.Width  = Width;
            ImageBox.Source = Convert(ImageBMP);
            ImageBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            ImageBox.VerticalAlignment   = System.Windows.VerticalAlignment.Center;
        }
        public static MyColor[,] Resize(MyColor[,] ImageMatrix, int newHeight, int newWidth, BackgroundWorker w)
        {
            ImageList.Clear();                                               // O(N) : where N is the list count
            Height = ImageMatrix.GetLength(0);                               // O(1)
            Width  = ImageMatrix.GetLength(1);                               // O(1)
            int    HeightDifference = Height - newHeight;                    // O(1)
            int    WidthDifference  = Width - newWidth;                      // O(1)
            double total            = Height - newHeight + Width - newWidth; // O(1)
            double done             = 0;                                     // O(1)

            // O(Height * Width)
            for (int i = 0; i < Height; i++)                    // O(Height)
            {
                List <MyColor> currList = new List <MyColor>(); // O(1)
                for (int j = 0; j < Width; j++)                 // O(Width)
                {
                    currList.Add(ImageMatrix[i, j]);            // O(1)
                }
                ImageList.Add(currList);                        // O(1)
            }



            if (WidthDifference > 0)                               // O(1)
            {
                AddedCounter = new List <int> [Height];            // O(1)
                for (int i = 0; i < Height; i++)                   // O(Height)
                {
                    AddedCounter[i] = new List <int>();            // O(1)
                }
                while (WidthDifference > 0)                        // O()
                {
                    DeleteColumn();                                // O(1)
                    done++;                                        // O(1)
                    w.ReportProgress((int)((done / total) * 100)); // O(1)
                    WidthDifference--;                             // O(1)
                }
            }

            if (HeightDifference > 0)                   // O(1)
            {
                AddedCounter = new List <int> [Width];  // O(1)
                for (int i = 0; i < Width; i++)         // O(Width)
                {
                    AddedCounter[i] = new List <int>(); // O(1)
                }
                Transpose();                            // O(Height * Width)

                // O(HeightDifference * Height * Width)
                while (HeightDifference > 0)                       // O(HeightDifference)
                {
                    DeleteRow();                                   // O(Height * Width)
                    done++;                                        // O(1)
                    w.ReportProgress((int)((done / total) * 100)); // O(1)
                    HeightDifference--;                            // O(1)
                }
                Transpose();                                       // O(Height * Width)
            }

            MyColor[,] newImageMatrix = new MyColor[newHeight, newWidth]; // O(1)
            int deltaw = ImageMatrix.GetLength(1) - newWidth;             // O(1)
            int deltah = ImageMatrix.GetLength(0) - newHeight;            // O(1)

            // O(newWidth * new Height)

            for (int i = 0; i < newHeight; i++)                                             // O(newHeight)
            {
                for (int j = 0; j < newWidth; j++)                                          // O(newWidth)
                {
                    newImageMatrix[i, j] = ImageList[i][j];                                 // O(1)

                    if (leaveIt[i, j])                                                      // O(1);
                    {
                        newImageMatrix[i - deltah / 2, j - deltaw / 2] = ImageMatrix[i, j]; // O(1);
                    }
                }
            }

            return(newImageMatrix);  // O(1)
        }
 /// <summary>
 /// Get the width of the image
 /// </summary>
 /// <param name="ImageMatrix">2D array that contains the image</param>
 /// <returns>Image Width</returns>
 public static int GetWidth(MyColor[,] ImageMatrix)
 {
     return(ImageMatrix.GetLength(1));
 }
 /// <summary>
 /// Get the height of the image
 /// </summary>
 /// <param name="ImageMatrix">2D array that contains the image</param>
 /// <returns>Image Height</returns>
 public static int GetHeight(MyColor[,] ImageMatrix)
 {
     return(ImageMatrix.GetLength(0));
 }
        /// <summary>
        /// Normal resize of an image
        /// </summary>
        /// <param name="ImageMatrix">2D array of image values</param>
        /// <param name="NewWidth">desired width</param>
        /// <param name="NewHeight">desired height</param>
        /// <returns>Resized image</returns>
        public static MyColor[,] NormalResize(MyColor[,] ImageMatrix, int NewWidth, int NewHeight)
        {
            int i = 0, j = 0;
            int Height = ImageMatrix.GetLength(0);
            int Width  = ImageMatrix.GetLength(1);

            double WidthRatio  = (double)(Width) / (double)(NewWidth);
            double HeightRatio = (double)(Height) / (double)(NewHeight);

            int OldWidth  = Width;
            int OldHeight = Height;

            MyColor P1, P2, P3, P4;

            MyColor Y1, Y2, X = new MyColor();

            MyColor[,] Data = new MyColor[NewHeight, NewWidth];

            Width  = NewWidth;
            Height = NewHeight;

            int floor_x, ceil_x;
            int floor_y, ceil_y;

            double x, y;
            double fraction_x, one_minus_x;
            double fraction_y, one_minus_y;

            for (j = 0; j < NewHeight; j++)
            {
                for (i = 0; i < NewWidth; i++)
                {
                    x = (double)(i) * WidthRatio;
                    y = (double)(j) * HeightRatio;

                    floor_x = (int)(x);
                    ceil_x  = floor_x + 1;
                    if (ceil_x >= OldWidth)
                    {
                        ceil_x = floor_x;
                    }

                    floor_y = (int)(y);
                    ceil_y  = floor_y + 1;
                    if (ceil_y >= OldHeight)
                    {
                        ceil_y = floor_y;
                    }

                    fraction_x  = x - floor_x;
                    one_minus_x = 1.0 - fraction_x;

                    fraction_y  = y - floor_y;
                    one_minus_y = 1.0 - fraction_y;

                    P1 = ImageMatrix[floor_y, floor_x];
                    P2 = ImageMatrix[ceil_y, floor_x];
                    P3 = ImageMatrix[floor_y, ceil_x];
                    P4 = ImageMatrix[ceil_y, ceil_x];

                    Y1.red   = (byte)(one_minus_y * P1.red + fraction_y * P2.red);
                    Y1.green = (byte)(one_minus_y * P1.green + fraction_y * P2.green);
                    Y1.blue  = (byte)(one_minus_y * P1.blue + fraction_y * P2.blue);

                    Y2.red   = (byte)(one_minus_y * P3.red + fraction_y * P4.red);
                    Y2.green = (byte)(one_minus_y * P3.green + fraction_y * P4.green);
                    Y2.blue  = (byte)(one_minus_y * P3.blue + fraction_y * P4.blue);

                    X.red   = (byte)(one_minus_x * Y1.red + fraction_x * Y2.red);
                    X.green = (byte)(one_minus_x * Y1.green + fraction_x * Y2.green);
                    X.blue  = (byte)(one_minus_x * Y1.blue + fraction_x * Y2.blue);

                    Data[j, i] = X;
                }
            }

            return(Data);
        }
Пример #21
0
        public static MyColor[,] Image_Enlarging(MyColor[,] Image, int height_resize, int width_resize)
        {
            if (height_resize == 0 && width_resize == 0)
            {
                return(Image);
            }


            int Height = Image.GetLength(0);
            int Width  = Image.GetLength(1);

            //   MyColor [,] new_Image = new MyColor [Height+height_resize,Width+width_resize];
            bool[,] empty      = new bool[Height + height_resize, Width + width_resize];
            Color[,] new_Image = new Color[Height + height_resize, Width + width_resize];
            Color[,] image     = Convert_MyColor_Color(Image);

            int[,] energy = new int[Height, Width];
            energy_init(energy, image, Width, Height);

            for (int i = 0; i < Height + height_resize; ++i)
            {
                for (int j = 0; j < Width + width_resize; ++j)
                {
                    new_Image[i, j].R = -1;
                    new_Image[i, j].G = -1;
                    new_Image[i, j].B = -1;
                }
            }

            int[,] C_ver   = new int[Height, Width];
            int[,] map_ver = new int[Height, Width];
            int[,] C_hor   = new int[Height, Width];
            int[,] map_hor = new int[Height, Width];
            if (width_resize > 0)
            {
                //Ver_C(1, image, energy, C_ver, map_ver, Width, Height);
                List <Pair> ver_seam = new List <Pair>();

                for (int j = 0; j < Width; ++j)
                {
                    ver_seam.Add(new Pair(C_ver[Height - 1, j], j));
                }

                ver_seam.Sort();

                List <List <int> > duplicated_pixels = new List <List <int> >();
                for (int i = Height - 1; i >= 0; --i)
                {
                    duplicated_pixels.Insert(0, new List <int>());
                }


                List <int> path;
                for (int k = 0; k < width_resize; ++k)
                {
                    path = get_ver_path(ver_seam[k].Second, map_ver, Height);
                    for (int i = Height - 1; i >= 0; --i)
                    {
                        duplicated_pixels[i].Add(path[i]);
                    }
                }
                for (int i = Height - 1; i >= 0; --i)
                {
                    duplicated_pixels[i].Sort();
                }
                insert_pixels(duplicated_pixels, image, new_Image, width_resize, Width, Height);
            }
            if (height_resize > 0)
            {
                //Hor_C(1, image, energy, C_hor, map_hor, Width, Height);
            }



            return(Convert_Color_MyColor(new_Image));
        }
 private void button_EfficientBlur_Click(object sender, EventArgs e)
 {
     int newValue = blurTrackBar.Value;
     /* Efficient Algorithm */
     BlurredImageMatrix = ImageOperations.BlurImage_Efficient(OriginalImageMatrix, newValue);
     ImageOperations.DisplayImage(BlurredImageMatrix, pictureBox2);
 }
 private void btnNormalContrast_Click(object sender, EventArgs e)
 {
     double newValue = contrastTrackBar.Value;
     ContrastedImageMatrix = ImageOperations.ContrastImage_Normal(OriginalImageMatrix , newValue);
     ImageOperations.DisplayImage(ContrastedImageMatrix, pictureBox2);
 }