コード例 #1
0
        private void ReplaceBlock(Block findedBlock, int i, int j)
        {
            Color randomColor = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));

            for (int k = 0; k < findedBlock.Width; k++)
            {
                for (int l = 0; l < findedBlock.Height; l++)
                {
                    int color = workImage.GetPixel(findedBlock.Position.X + k, findedBlock.Position.Y + l);

                    if (i + k < workImage.Width && j + l < workImage.Height)
                    {
                        if (imageBitmap[findedBlock.Position.X + k, findedBlock.Position.Y + l] == false)
                        {
                            color = emptyPixel;
                            numberErrorPX++;
                        }

                        if (originalImageValid)
                        {
                            bitmapBlocks.SetPixel(i + k, j + l, randomColor);
                        }
                        workImage.SetPixel(i + k, j + l, color);
                        imageBitmap[i + k, j + l] = true;
                    }
                }
            }
        }
コード例 #2
0
        private FastImage newBlackImage(Bitmap imageBitmap)
        {
            FastImage newImg = new FastImage(imageBitmap);

            newImg.Lock();
            for (int i = 0; i < newImg.Width; i++)
            {
                for (int j = 0; j < newImg.Height; j++)
                {
                    newImg.SetPixel(i, j, emptyPixel);
                }
            }

            newImg.Unlock();

            return(newImg);
        }
コード例 #3
0
        private void DrawBlockBorder(int i, int j, Block block)
        {
            int r = random.Next(0, 255);
            int g = random.Next(0, 255);
            int b = random.Next(0, 255);

            for (int k = 0; k < block.Width; k++)
            {
                workImage.SetPixel(i + k, j, r, g, b);
                workImage.SetPixel(i + k, j + block.Height - 1, r, g, b);
            }
            for (int l = 0; l < block.Height; l++)
            {
                workImage.SetPixel(i, j + l, r, g, b);
                workImage.SetPixel(i + block.Width - 1, j + l, r, g, b);
            }
        }
コード例 #4
0
        public FastImage StartDeCompression(string inputFile)
        {
            ReadFromFile(inputFile);

            Bitmap blackImageBitmap = new Bitmap(imageWidth, imageHeight, PixelFormat.Format24bppRgb);

            workImage = new FastImage(blackImageBitmap);

            workImage.Lock();
            buildGP();
            while (poolGrowingPoints.Count != 0)
            {
                int widthStep, heightStep;
                //Position growingPoint = UpdateGPPool();
                Position growingPoint = poolGrowingPoints[0];

                int i = growingPoint.X;
                int j = growingPoint.Y;

                int index = indexesList[0];
                indexesList.RemoveAt(0);

                if (index < 256)
                {
                    workImage.SetPixel(i, j, index);
                    imageBitmap[i, j] = true;
                    widthStep         = 1;
                    heightStep        = 1;
                }
                else
                {
                    Block findedBlock = dictionary.LastOrDefault(x => x.Value == index).Key;

                    if (findedBlock == null)
                    {
                        Console.WriteLine("**");
                        foreach (Block block in dictionaryBackup)
                        {
                            if (block.Index == index)
                            {
                                findedBlock = block;
                            }
                        }
                    }

                    ReplaceBlock(findedBlock, i, j);

                    widthStep  = findedBlock.Width;
                    heightStep = findedBlock.Height;

                    numberBlocksFinded++;
                }

                //TryAddGrowingPoint(growingPoint, widthStep, heightStep);
                //TryAddGrowingPoint(new Position(i + widthStep, j));

                if (i != 0 && j != 0)
                {
                    UpdateDictionary(growingPoint, index);
                }

                UpdateGPPool();
            }

            Console.WriteLine("NumberBlocksFinded = {0} numberErrorPX = {1}", numberBlocksFinded, numberErrorPX);

            workImage.Unlock();

            return(workImage);
        }
コード例 #5
0
        public SimulationResult StartSimulation(int th, int dictionarySize)
        {
            indexesList         = new List <int>();
            Threshold           = th;
            MaxDictionaryLength = dictionarySize;
            // BlocPainFrequency = int.MaxValue;

            originalImage.Lock();
            workImage.Lock();

            DateTime startTime = DateTime.Now;

            buildGP();


            while (poolGrowingPoints.Count != 0)
            {
                int widthStep, heightStep;
                //Position growingPoint = UpdateGPPool();
                Position growingPoint = poolGrowingPoints[0];
                int      i            = growingPoint.X;
                int      j            = growingPoint.Y;
                int      index;

                if (i == 0 || j == 0)
                {
                    index = originalImage.GetPixel(i, j);
                }
                else
                {
                    index = SearchBlock(growingPoint);
                }

                indexesList.Add(index);

                if (index < 256)
                {
                    workImage.SetPixel(i, j, index);
                    imageBitmap[i, j] = true;
                    widthStep         = 1;
                    heightStep        = 1;
                }
                else
                {
                    Block findedBlock = dictionary.LastOrDefault(x => x.Value == index).Key;
                    ReplaceBlock(findedBlock, i, j);

                    widthStep  = findedBlock.Width;
                    heightStep = findedBlock.Height;


                    numberBlocksFinded++;
                }


                // TryAddGrowingPoint(growingPoint, widthStep, heightStep);
                //TryAddGrowingPoint(new Position(i + widthStep, j));

                if (i != 0 && j != 0)
                {
                    UpdateDictionary(growingPoint, index);
                }
                UpdateGPPool();
            }

            Program.form.updatePanelBlock(bitmapBlocks.GetBitMap());

            DateTime finishTime      = DateTime.Now;
            string   compressionTime = (finishTime - startTime).TotalSeconds.ToString();

            WriteInFile(indexesList);
            Console.WriteLine("NumberBlocksFinded = {0} numberErrorPX = {1}", numberBlocksFinded, numberErrorPX);

            workImage.Unlock();
            originalImage.Unlock();


            //---

            originalImageValid      = false;
            dictionary              = new Dictionary <Block, int>();
            dictionaryBackup        = new List <Block>();
            currentDictionaryLength = 256;
            indexesList             = new List <int>();

            ReadFromFile(CompressedFile);

            Bitmap blackImageBitmap = new Bitmap(imageWidth, imageHeight, PixelFormat.Format24bppRgb);

            workImage = new FastImage(blackImageBitmap);

            workImage.Lock();

            startTime = DateTime.Now;

            buildGP();
            while (poolGrowingPoints.Count != 0)
            {
                int widthStep, heightStep;
                //Position growingPoint = UpdateGPPool();
                Position growingPoint = poolGrowingPoints[0];

                int i = growingPoint.X;
                int j = growingPoint.Y;

                int index = indexesList[0];
                indexesList.RemoveAt(0);

                if (index < 256)
                {
                    workImage.SetPixel(i, j, index);
                    imageBitmap[i, j] = true;
                    widthStep         = 1;
                    heightStep        = 1;
                }
                else
                {
                    Block findedBlock = dictionary.LastOrDefault(x => x.Value == index).Key;

                    if (findedBlock == null)
                    {
                        Console.WriteLine("**");
                        foreach (Block block in dictionaryBackup)
                        {
                            if (block.Index == index)
                            {
                                findedBlock = block;
                            }
                        }
                    }

                    ReplaceBlock(findedBlock, i, j);

                    widthStep  = findedBlock.Width;
                    heightStep = findedBlock.Height;

                    numberBlocksFinded++;
                }


                if (i != 0 && j != 0)
                {
                    UpdateDictionary(growingPoint, index);
                }

                UpdateGPPool();
            }

            //Console.WriteLine("NumberBlocksFinded = {0} numberErrorPX = {1}", numberBlocksFinded, numberErrorPX);

            finishTime = DateTime.Now;
            string decompressionTime = (finishTime - startTime).TotalSeconds.ToString();

            //
            workImage.Unlock();


            //---

            Bitmap bitmap = workImage.GetBitMap();

            string[] output = CompressedFile.Split('.');
            DecompressedFile = output[0] + "-decoded.bmp";
            bitmap.Save(DecompressedFile);

            float psnr = CompareImages(originalImage, workImage);

            long compressedFileSize   = new FileInfo(CompressedFile).Length;
            long decompressedFileSize = new FileInfo(DecompressedFile).Length;


            return(new SimulationResult(MaxDictionaryLength, Threshold, compressionTime, decompressionTime, numberBlocksFinded, compressedFileSize, decompressedFileSize, psnr));
        }