public static GlyphParseResult ParseNextGlyph(FastAccessImage image, GlyphLookup lookup, Rectangle rectangle, int xStart) { foreach (var kvp in lookup.ReferenceLookup.OrderBy(key => - 1 * key.Value.Width)) { if (CheckGlyphMatch(image, lookup, rectangle, xStart, lookup.ReferenceImage, kvp.Value)) { int xEndOfCurrent = xStart + kvp.Value.Width; int xStartOfNext = HorizontalSeek(image, lookup, rectangle, xEndOfCurrent); if (xStartOfNext == -1) { return(new GlyphParseResult(false, kvp.Key, -1)); } else if (xStartOfNext - xEndOfCurrent >= lookup.WhiteSpaceWidth) { return(new GlyphParseResult(true, kvp.Key + " ", xStartOfNext)); } else { return(new GlyphParseResult(true, kvp.Key, xStartOfNext)); } } } return(new GlyphParseResult(false, string.Empty, -1)); }
public static string Read(FastAccessImage image, GlyphLookup lookup, Rectangle location) { if (location.X > image.Width || location.Right > image.Width || location.Y > image.Height || location.Bottom > image.Height) { throw new IndexOutOfRangeException("Rectangle outside of supplied image"); } int x = HorizontalSeek(image, lookup, location, 0); if (x == -1) { return(string.Empty); } StringBuilder parsedString = new StringBuilder(); GlyphParseResult parseResult = new GlyphParseResult(true, string.Empty, x); do { parseResult = ParseNextGlyph(image, lookup, location, x); x = parseResult.X; parsedString.Append(parseResult.ParsedString); }while (parseResult.Continue); return(parsedString.ToString().Trim()); }
public static List <string> Read(FastAccessImage image, GlyphLookup lookup, List <Rectangle> locations) { List <string> results = new List <string>(); foreach (Rectangle location in locations) { results.Add(Read(image, lookup, location)); } return(results); }
public static int HorizontalSeek(FastAccessImage image, GlyphLookup lookup, Rectangle rectangle, int xStart) { if (xStart >= rectangle.Width) { return(-1); } int left = rectangle.Left; int top = rectangle.Top; int width = rectangle.Width; int height = rectangle.Height; int y = 0; for (; xStart < width; xStart++) { for (y = 0; y < height; y++) { FastAccessImage.Pixel pixel = image.GetPixel(left + xStart, top + y); if (pixel.Equals(lookup.ReferencePixel)) { break; } } if (y != height) { break; } } if ((xStart == width) && (y == height)) { return(-1); } else { return(xStart); } }
public static bool CheckGlyphMatch(FastAccessImage image, GlyphLookup lookup, Rectangle location, int xStart, FastAccessImage glyphImageReference, Rectangle glyphRectangle) { if (xStart + glyphRectangle.Width > location.Right || glyphRectangle.Height > location.Height) { return(false); } for (int x = 0; x < glyphRectangle.Width; x++) { for (int y = 0; y < glyphRectangle.Height; y++) { FastAccessImage.Pixel glyphPixel = glyphImageReference.GetPixel(glyphRectangle.Left + x, glyphRectangle.Top + y); FastAccessImage.Pixel imagePixel = image.GetPixel(location.Left + xStart + x, location.Top + y); if ((glyphPixel.Alpha != 0 && !glyphPixel.Equals(imagePixel)) || (glyphPixel.Alpha == 0 && imagePixel.Equals(lookup.ReferencePixel))) { return(false); } } } return(true); }