Beispiel #1
0
        public string decode(Bitmap bmp, LetterFont compareFont)
        {
            string decodeString = "";

            using (UnsafeBitmap ubm = new UnsafeBitmap(bmp))
            {
                ubm.LockBitmap();

                for (int i = 0; i < ubm.Bitmap.Height - compareFont.charHeight + 1; i++)
                {
                    int lastCol = 0;
                    for (int j = 0; j < ubm.Bitmap.Width; j++)
                    {
                        Letter letter = compareFont.findLetter(ubm, j, i, lastCol > 0);
                        if (letter != null)
                        {
                            int padding = (j - lastCol) / compareFont.spaceWidth;
                            for (int k = 0; k < padding; k++)
                            {
                                decodeString += " ";
                            }
                            decodeString += letter.convertString;
                            j            += letter.ubm.Bitmap.Width - 1;
                            lastCol       = j + 1;
                        }
                    }
                    if (lastCol > 0)
                    {
                        decodeString += "\r\n";
                        i            += compareFont.charHeight - 1;
                    }
                }
            }
            return(decodeString.Trim());
        }
Beispiel #2
0
        public bool compare(UnsafeBitmap test, int x, int y, bool lineHasChar)
        {
            string excludeChars = ".-:_";

            if (excludeChars.Contains(convertString) &&
                (!lineHasChar))
            {
                return(false);
            }
            if ((y + ubm.Bitmap.Height) > test.Bitmap.Height)
            {
                return(false);
            }
            if ((x + ubm.Bitmap.Width) > test.Bitmap.Width)
            {
                return(false);
            }
            for (int j = 0; j < ubm.Bitmap.Width; j++)
            {
                for (int i = 0; i < ubm.Bitmap.Height; i++)
                {
                    // we are comparing binary bitmaps, so comparing just the green bit is ok
                    if (test.GetPixel(j + x, i + y).green != ubm.GetPixel(j, i).green)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Beispiel #3
0
        int calcCheckSum(UnsafeBitmap ubmp, int x, int y)
        {
            int checksum = 0;

            for (int i = 0; i < charHeight; i++)
            {
                checksum = checksum << 1;
                if (ubmp.GetPixel(x, y + i).green == 255)
                {
                    checksum += 1;
                }
            }
            return(checksum);
        }
Beispiel #4
0
        public Letter findLetter(UnsafeBitmap ubmp, int x, int y, bool lineHasChar)
        {
            int checksum = calcCheckSum(ubmp, x, y);

            foreach (Letter letter in letters)
            {
                if (letter.checksum != checksum)
                {
                    continue;
                }
                if (letter.compare(ubmp, x, y, lineHasChar))
                {
                    return(letter);
                }
            }
            return(null);
        }
Beispiel #5
0
        public Letter(string filename)
        {
            bmp = new Bitmap(filename);
            ubm = new UnsafeBitmap(bmp);
            ubm.LockBitmap();
            string baseString = Path.GetFileNameWithoutExtension(filename);

            if (baseString.Length == 1)
            {
                convertString = baseString;
            }
            else if (baseString.EndsWith("_cap"))
            {
                convertString = baseString[0].ToString().ToUpper();
            }
            else
            {
                switch (baseString)
                {
                case "dash":
                    convertString = "-";
                    break;

                case "dot":
                    convertString = ".";
                    break;

                case "bracket_open":
                    convertString = "<";
                    break;

                case "bracket_close":
                    convertString = ">";
                    break;

                case "paren_open":
                    convertString = "(";
                    break;

                case "paren_close":
                    convertString = ")";
                    break;

                case "plus":
                    convertString = "+";
                    break;

                case "slash":
                    convertString = "/";
                    break;

                case "colon":
                    convertString = ":";
                    break;

                case "underscore":
                    convertString = "_";
                    break;

                case "square_bracket_open":
                    convertString = "[";
                    break;

                case "square_bracket_close":
                    convertString = "]";
                    break;

                case "comma":
                    convertString = ",";
                    break;

                case "percent":
                    convertString = "%";
                    break;

                case "pound_sign":
                    convertString = "#";
                    break;

                case "star":
                    convertString = "*";
                    break;

                default:
                    throw new Exception(filename);
                }
            }
        }
Beispiel #6
0
 public bool compare(UnsafeBitmap test, int x, int y, bool lineHasChar)
 {
     string excludeChars = ".-:_";
     if (excludeChars.Contains(convertString)
         && (!lineHasChar)) return false;
     if ((y + ubm.Bitmap.Height) > test.Bitmap.Height) return false;
     if ((x + ubm.Bitmap.Width) > test.Bitmap.Width) return false;
     for (int j = 0; j < ubm.Bitmap.Width; j++)
     {
         for (int i = 0; i < ubm.Bitmap.Height; i++)
         {
             // we are comparing binary bitmaps, so comparing just the green bit is ok
             if (test.GetPixel(j + x, i + y).green != ubm.GetPixel(j, i).green)
                 return false;
         }
     }
     return true;
 }
Beispiel #7
0
 public Letter(string filename)
 {
     bmp = new Bitmap(filename);
     ubm = new UnsafeBitmap(bmp);
     ubm.LockBitmap();
     string baseString = Path.GetFileNameWithoutExtension(filename);
     if (baseString.Length == 1)
     {
         convertString = baseString;
     }
     else if (baseString.EndsWith("_cap"))
     {
         convertString = baseString[0].ToString().ToUpper();
     }
     else switch (baseString)
         {
             case "dash":
                 convertString = "-";
                 break;
             case "dot":
                 convertString = ".";
                 break;
             case "bracket_open":
                 convertString = "<";
                 break;
             case "bracket_close":
                 convertString = ">";
                 break;
             case "paren_open":
                 convertString = "(";
                 break;
             case "paren_close":
                 convertString = ")";
                 break;
             case "plus":
                 convertString = "+";
                 break;
             case "slash":
                 convertString = "/";
                 break;
             case "colon":
                 convertString = ":";
                 break;
             case "underscore":
                 convertString = "_";
                 break;
             case "square_bracket_open":
                 convertString = "[";
                 break;
             case "square_bracket_close":
                 convertString = "]";
                 break;
             case "comma":
                 convertString = ",";
                 break;
             case "percent":
                 convertString = "%";
                 break;
             case "pound_sign":
                 convertString = "#";
                 break;
             case "star":
                 convertString = "*";
                 break;
             default:
                 throw new Exception(filename);
         }
 }
Beispiel #8
0
        public string decode(Bitmap bmp, LetterFont compareFont)
        {
            string decodeString = "";
            using (UnsafeBitmap ubm = new UnsafeBitmap(bmp))
            {
                ubm.LockBitmap();

                for (int i = 0; i < ubm.Bitmap.Height - compareFont.charHeight + 1; i++)
                {
                    int lastCol = 0;
                    for (int j = 0; j < ubm.Bitmap.Width; j++)
                    {
                        Letter letter = compareFont.findLetter(ubm, j, i, lastCol > 0);
                        if (letter != null)
                        {
                            int padding = (j - lastCol) / compareFont.spaceWidth;
                            for (int k = 0; k < padding; k++)
                            {
                                decodeString += " ";
                            }
                            decodeString += letter.convertString;
                            j += letter.ubm.Bitmap.Width - 1;
                            lastCol = j + 1;
                        }

                    }
                    if (lastCol > 0)
                    {
                        decodeString += "\r\n";
                        i += compareFont.charHeight - 1;
                    }
                }
            }
            return decodeString.Trim();
        }
Beispiel #9
0
 int calcCheckSum(UnsafeBitmap ubmp, int x, int y)
 {
     int checksum = 0;
     for (int i = 0; i < charHeight; i++)
     {
         checksum = checksum << 1;
         if (ubmp.GetPixel(x, y + i).green == 255) checksum += 1;
     }
     return checksum;
 }
Beispiel #10
0
 public Letter findLetter(UnsafeBitmap ubmp, int x, int y, bool lineHasChar)
 {
     int checksum = calcCheckSum(ubmp, x, y);
     foreach (Letter letter in letters)
     {
         if (letter.checksum != checksum) continue;
         if (letter.compare(ubmp, x, y, lineHasChar)) return letter;
     }
     return null;
 }