Example #1
0
        private void Load(string path)
        {
            //Create temp directory
            if (Directory.Exists("TempFontStorage"))
            {
                Directory.Delete("TempFontStorage", true);
            }

            Directory.CreateDirectory("TempFontStorage");

            //Extract zip
            try
            {
                ZipFile.ExtractToDirectory(path, "TempFontStorage");
            }
            catch (InvalidDataException e)
            {
                throw new FontLoadingException("Couldn't unpack zip", e);
            }

            //Load images
            string[] folders = Directory.GetDirectories("TempFontStorage");

            foreach (string folder in folders)
            {
                FChar         key        = (FChar)Enum.Parse(typeof(FChar), Path.GetFileName(folder));
                List <Bitmap> value      = new List <Bitmap>();
                string[]      imagePaths = Directory.GetFiles(folder);
                foreach (string imagePath in imagePaths)
                {
                    Bitmap loadedBitmap;
                    using (Bitmap bitmap = BitmapUtils.LoadBitmap(imagePath))
                    {
                        loadedBitmap = new Bitmap(bitmap);
                    }
                    value.Add(loadedBitmap);
                }
                images.Add(key, value);
            }

            //Load margins
            XElement file      = XElement.Load("TempFontStorage/margins.xml");
            var      leftPairs = file.Descendants().Where((x) => x.Name == "leftMargins").First().Descendants().Where((elem) => elem.Name == "pair");

            leftMargins = new Dictionary <FChar, List <double> >();
            foreach (XElement pair in leftPairs)
            {
                FChar key = (FChar)Enum.Parse(typeof(FChar), pair.Attribute("key").Value);
                if (!leftMargins.ContainsKey(key))
                {
                    leftMargins.Add(key, new List <double>());
                }
                leftMargins[key].AddRange(pair.Descendants().Select((item) => double.Parse(item.Attribute("value").Value, CultureInfo.InvariantCulture)));
            }

            var rightPairs = file.Descendants().Where((x) => x.Name == "rightMargins").First().Descendants().Where((elem) => elem.Name == "pair");

            rightMargins = new Dictionary <FChar, List <double> >();
            foreach (XElement pair in rightPairs)
            {
                FChar key = (FChar)Enum.Parse(typeof(FChar), pair.Attribute("key").Value);
                if (!rightMargins.ContainsKey(key))
                {
                    rightMargins.Add(key, new List <double>());
                }
                rightMargins[key].AddRange(pair.Descendants().Select((item) => double.Parse(item.Attribute("value").Value, CultureInfo.InvariantCulture)));
            }

            //Delete temp directory
            if (Directory.Exists("TempFontStorage"))
            {
                Directory.Delete("TempFontStorage", true);
            }
        }
Example #2
0
 private void CreateSmallExpandedBW(Bitmap smallForm)
 {
     smallBWForm = BitmapUtils.MakeBlackAndWhite(smallForm, BWThreshold);
     smallBWForm = BitmapUtils.ExpandWhite(smallBWForm);
 }
Example #3
0
        /// <summary>
        /// Fills result list with points belonging to the component
        /// </summary>
        private static void TraverseConnectedComponentDFS(List <Vector> result, Bitmap map, Bitmap visited, int x, int y, int threshold)
        {
            if (x < 0 || x >= map.Width || y < 0 || y >= map.Height || IsVisited(visited, x, y) || !BitmapUtils.IsBelowThreshold(map, x, y, threshold))
            {
                return;
            }
            result.Add(new Vector(x, y));

            if (result.Count > 2000)
            {
                throw new MarkersNotFoundException("Couldn't traverse one of the markers: for some reason it is too large.");
            }

            visited.SetPixel(x, y, Color.Red);

            TraverseConnectedComponentDFS(result, map, visited, x + 1, y, threshold);
            TraverseConnectedComponentDFS(result, map, visited, x, y + 1, threshold);
            TraverseConnectedComponentDFS(result, map, visited, x - 1, y, threshold);
            TraverseConnectedComponentDFS(result, map, visited, x, y - 1, threshold);
        }
        private void FindCoreRegion()
        {
            int[] histogram = new int[Font.imagePixelH];

            foreach (FChar letter in smallLetters)
            {
                for (int i = 0; i < font.images[letter].Count; i++)
                {
                    Bitmap     image = font.images[letter][i];
                    BitmapData data  = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);

                    unsafe
                    {
                        byte *arr          = (byte *)data.Scan0;
                        int   channelCount = Bitmap.GetPixelFormatSize(image.PixelFormat) / 8;

                        for (int j = 0; j < image.Width * image.Height * channelCount; j += channelCount)
                        {
                            if (BitmapUtils.GetGrayscale(arr[j], arr[j + 1], arr[j + 2]) < 128 && arr[j + 3] > 240)
                            {
                                histogram[j / channelCount / image.Width]++;
                            }
                        }
                    }

                    image.UnlockBits(data);
                }
            }

            int[] cumulativeHistogram = new int[Font.imagePixelH];
            cumulativeHistogram[0] = histogram[0];

            for (int i = 1; i < cumulativeHistogram.Length; i++)
            {
                cumulativeHistogram[i] = cumulativeHistogram[i - 1] + histogram[i];
            }

            double cutoffRatio = 0.05;
            int    total       = cumulativeHistogram.Last();

            int leftBorder = 0;

            for (int i = 0; i < cumulativeHistogram.Length; i++)
            {
                if ((double)cumulativeHistogram[i] / total > cutoffRatio)
                {
                    leftBorder = i;
                    break;
                }
            }

            int rightBorder = cumulativeHistogram.Length;

            for (int i = cumulativeHistogram.Length - 1; i >= 0; i--)
            {
                if ((double)cumulativeHistogram[i] / total < (1.0 - cutoffRatio))
                {
                    rightBorder = i;
                    break;
                }
            }

            coreRegionTop    = leftBorder * Font.cmPerPixelV;
            coreRegionBottom = rightBorder * Font.cmPerPixelV;
        }