Пример #1
0
        private static Pixel[,] YUVBlocksToGrayscalePixelMatrix(PPMImage Image)
        {
            Pixel[,] final = new Pixel[Image.Height, Image.Width];

            int step = 0;
            int rank = Communicator.world.Rank - 1;
            int size = Communicator.world.Size - 1;

            for (int i = 0; i < Image.Blocks.Count(); i = i + 3)
            {
                if (rank == step % size)
                {
                    double[,] yMatrix = Image.Blocks[i].Matrix;
                    int starti = Image.Blocks[i].PositionI;
                    int startj = Image.Blocks[i].PositionJ;

                    for (int mi = 0; mi < yMatrix.GetLength(0); mi++)
                    {
                        for (int mj = 0; mj < yMatrix.GetLength(1); mj++)
                        {
                            double c = yMatrix[mi, mj] - 16;
                            int    r = Clamp((int)(298 * c + 128) >> 8, 0, 255);
                            int    g = Clamp((int)(298 * c + 128) >> 8, 0, 255);
                            int    b = Clamp((int)(298 * c + 128) >> 8, 0, 255);

                            final[starti + mi, startj + mj] = new Pixel(r, g, b);
                        }
                    }
                }

                step++;
            }

            return(final);
        }
Пример #2
0
        private static void Master()
        {
            bool choosePictureAlive = true;

            while (choosePictureAlive)
            {
                bool chooseFilterAlive = true;

                Console.Write("Path to picture: ");
                string path = Console.ReadLine();

                PPMImage image = PPMOperations.ReadPPMImageFromFile(path);
                PPMOperations.Broadcast(image);

                while (chooseFilterAlive)
                {
                    //add filter in menu

                    Console.Write("Choose filter: \n\t'1'-Regular\n\t'2'-Grayscale" +
                                  "\n\t'3'-Blur\n\t'4'-Find Edges" +
                                  "\n\t'5'-Choose different picture\n\t'0'-Exit\nFilter: ");
                    string filter = Console.ReadLine();
                    switch (filter)
                    {
                    case "1":
                    case "2":
                        PPMOperations.Broadcast(filter);
                        GenerateFullImage(image);
                        break;

                    case "3":
                    case "4":
                        PPMOperations.Broadcast(filter);
                        GenerateFilteredImage(image, filter);
                        break;

                    case "5":
                        PPMOperations.Broadcast(filter);
                        chooseFilterAlive = false;
                        break;

                    case "0":
                        PPMOperations.Broadcast(filter);
                        chooseFilterAlive  = false;
                        choosePictureAlive = false;
                        break;

                    default:
                        Console.WriteLine("Invalid command.\n");
                        break;
                    }
                }
            }
        }
Пример #3
0
        public static PPMImage ReadPPMImageFromFile(String path)
        {
            List <String> before = File.ReadAllLines(path).ToList();

            string[] thirdLine = before[2].Split(' ');
            int      width     = Convert.ToInt16(thirdLine[0]);
            int      height    = Convert.ToInt16(thirdLine[1]);
            int      maxValue  = Convert.ToInt16(before[3].Trim());

            Block[] blocks = RGBListToYUVBlocks(before.Skip(4).ToList(), width, height, 8).ToArray();

            PPMImage image = new PPMImage(before[0], before[1], width, height, maxValue, blocks);

            return(image);
        }
Пример #4
0
        public static void GenerateImage(PPMImage image, string outputPath)
        {
            Pixel[,] matrix = MergePixelMatrices();
            StringBuilder sb = new StringBuilder();

            sb.Append(image.FirstComment);
            sb.Append('\n');
            sb.Append(image.SecondComment);
            sb.Append('\n');
            sb.Append(image.Width + " " + image.Height);
            sb.Append('\n');
            sb.Append(image.MaxValue);
            sb.Append(PixelMatrixToString(matrix));
            WriteToFile(outputPath, sb.ToString());
        }
Пример #5
0
        public static void GenerateRegularImage(PPMImage Image, string path)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(Image.FirstComment);
            sb.Append('\n');
            sb.Append(Image.SecondComment);
            sb.Append('\n');
            sb.Append(Image.Width + " " + Image.Height);
            sb.Append('\n');
            sb.Append(Image.MaxValue);
            sb.Append('\n');
            sb.Append(YUVBlocksToRGBString(Image));

            File.WriteAllText(path, sb.ToString());
        }
Пример #6
0
        public static void GenerateFilteredImage(PPMImage image, double[,] filterMatrix, double filterFactor, double filterBias, string outputPath)
        {
            Pixel[,] matrix = MergePixelMatrices();

            Pixel[,] finalMatrix = ApplyFilter(matrix, filterMatrix, filterFactor, filterBias);
            StringBuilder sb = new StringBuilder();

            sb.Append(image.FirstComment);
            sb.Append('\n');
            sb.Append(image.SecondComment);
            sb.Append('\n');
            sb.Append(image.Width + " " + image.Height);
            sb.Append('\n');
            sb.Append(image.MaxValue);
            sb.Append(PixelMatrixToString(finalMatrix));
            WriteToFile(outputPath, sb.ToString());
        }
Пример #7
0
        private static PPMImage MergeImages()
        {
            PPMImage Image = Communicator.world.Receive <PPMImage>(1, 0);

            for (int i = 2; i < Communicator.world.Size; i++)
            {
                PPMImage img = Communicator.world.Receive <PPMImage>(i, 0);
                for (int j = 0; j < img.Blocks.Length; j++)
                {
                    if (img.Blocks[j].Type != Block.Types.None)
                    {
                        Image.Blocks[j] = img.Blocks[j];
                    }
                }
            }
            return(Image);
        }
Пример #8
0
        private static void Worker()
        {
            bool choosePictureAlive = true;

            while (choosePictureAlive)
            {
                bool chooseFilterAlive = true;

                PPMImage image = Communicator.world.Receive <PPMImage>(0, 0);

                //add filter in menu

                while (chooseFilterAlive)
                {
                    string filter = Communicator.world.Receive <string>(0, 0);
                    switch (filter)
                    {
                    case "1":
                        PPMOperations.GenerateRegularImage(image);
                        break;

                    case "2":
                        PPMOperations.GenerateGrayscaleImage(image);
                        break;

                    case "3":
                        PPMOperations.GenerateBlurImage(image);
                        break;

                    case "4":
                        PPMOperations.GenerateEdgeImage(image);
                        break;

                    case "5":
                        chooseFilterAlive = false;
                        break;

                    case "0":
                        chooseFilterAlive  = false;
                        choosePictureAlive = false;
                        break;
                    }
                }
            }
        }
Пример #9
0
        private static void GenerateFilteredImage(PPMImage image, string filter)
        {
            //add if statement for the new filter

            if (filter == "3")
            {
                double[,] filterMatrix =
                {
                    { 1, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 1, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 1, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 1, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 1, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 1, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 1, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                };

                double filterFactor = 1.0 / 9.0;
                double filterBias   = 0.0;

                PPMOperations.GenerateFilteredImage(image, filterMatrix, filterFactor, filterBias, "after.ppm");
            }
            else if (filter == "4")
            {
                double[,] filterMatrix =
                {
                    { 0, 0, -1, 0, 0 },
                    { 0, 0, -1, 0, 0 },
                    { 0, 0,  2, 0, 0 },
                    { 0, 0,  0, 0, 0 },
                    { 0, 0,  0, 0, 0 },
                };

                double filterFactor = 1.0;
                double filterBias   = 0.0;

                PPMOperations.GenerateFilteredImage(image, filterMatrix, filterFactor, filterBias, "after.ppm");
            }
        }
Пример #10
0
        public static void ReadPPMImageFromFile(PPMImage Image, String path)
        {
            List <String> before = File.ReadAllLines(path).ToList();

            string[] thirdLine = before[2].Split(' ');
            Image.Width    = Convert.ToInt16(thirdLine[0]);
            Image.Height   = Convert.ToInt16(thirdLine[1]);
            Image.MaxValue = Convert.ToInt16(before[3].Trim());

            Block[] blocks = RGBListToYUVBlocks(before.Skip(4).ToList(), Image.Width, Image.Height, 8);

            for (int i = 0; i < blocks.Length; i++)
            {
                if (Image.Blocks[i].Type == Block.Types.None)
                {
                    Image.Blocks[i] = blocks[i];
                }
            }

            //Image.Blocks = blocks;
        }
Пример #11
0
        private static Pixel[,] YUVBlocksToPixelMatrix(PPMImage Image)
        {
            Pixel[,] final = new Pixel[Image.Height, Image.Width];

            int size = Communicator.world.Size - 1;
            int rank = Communicator.world.Rank - 1;

            int k = 0;

            for (int i = 0; i < Image.Blocks.Length; i = i + 3)
            {
                if (k % size == rank)
                {
                    double[,] yMatrix = Image.Blocks[i].Matrix;
                    double[,] uMatrix = Image.Blocks[i + 1].Matrix;
                    double[,] vMatrix = Image.Blocks[i + 2].Matrix;
                    int starti = Image.Blocks[i].PositionI;
                    int startj = Image.Blocks[i].PositionJ;

                    for (int mi = 0; mi < yMatrix.GetLength(0); mi++)
                    {
                        for (int mj = 0; mj < yMatrix.GetLength(1); mj++)
                        {
                            double c = yMatrix[mi, mj] - 16;
                            double d = uMatrix[mi, mj] - 128;
                            double e = vMatrix[mi, mj] - 128;
                            int    r = Clamp((int)(298 * c + 409 * e + 128) >> 8, Image.MaxValue);
                            int    g = Clamp((int)(298 * c - 100 * d - 208 * e + 128) >> 8, Image.MaxValue);
                            int    b = Clamp((int)(298 * c + 516 * d + 128) >> 8, Image.MaxValue);

                            final[starti + mi, startj + mj] = new Pixel(r, g, b);
                        }
                    }
                }
                k++;
            }

            return(final);
        }
Пример #12
0
        public static void GenerateEdgeImage(PPMImage image)
        {
            Pixel[,] matrix = YUVBlocksToPixelMatrix(image);

            Communicator.world.Send <Pixel[, ]>(matrix, 0, 0);
        }
Пример #13
0
 private static void GenerateFullImage(PPMImage image)
 {
     PPMOperations.GenerateImage(image, "after.ppm");
 }