public List <TestResult> CheckCommodityExtractorMemory() { List <TestResult> results = new List <TestResult>(); Process process = Process.GetCurrentProcess(); long memStart = process.WorkingSet64; for (int i = 0; i < 100; i++) { UnsafeBitmap bm = new UnsafeBitmap("Test\\Market_Island_Full_800x600.tif"); ppocr.ClearErrors(); Rectangle commodArea = ppocr.FindCommodityArea(bm); List <Commodity> commodities = ppocr.ExtractCommodities(bm, commodArea); GC.Collect(); // Force garbage collection GC.WaitForPendingFinalizers(); } process = Process.GetCurrentProcess(); long memStop = process.WorkingSet64; if ((memStop - memStart) > 2000000) { results.Add(new TestResult("CheckCommodityExtractorMemory", "MemoryStart: " + memStart.ToString() + " MemoryStop: " + memStop.ToString())); } if (results.Count <= 0) { results.Add(new TestResult("CheckCommodityExtractorMemory", "PASS")); } return(results); }
public List <TestResult> CheckCommodityExtractor(string infoFile) { List <TestResult> results = new List <TestResult>(); CaptureInfo info = CaptureInfo.Deserialize("Test\\" + infoFile); UnsafeBitmap bm = new UnsafeBitmap("Test\\" + info.BitmapFile); ppocr.ClearErrors(); DateTime dtStart = DateTime.Now; Rectangle commodArea = ppocr.FindCommodityArea(bm); if (commodArea != info.CommodityRect) { results.Add(new TestResult(infoFile, "Commodity Rect Expected " + info.CommodityRect.ToString() + ", got " + commodArea.ToString())); return(results); } string island = ppocr.ExtractIslandName(bm); if (!island.Equals(info.Island)) { results.Add(new TestResult(infoFile, "Expected island name: " + info.Island + ", Got: " + island)); } List <Commodity> commodities = ppocr.ExtractCommodities(bm, commodArea); if (ppocr.Error.Length > 0) { results.Add(new TestResult(infoFile, ppocr.Error)); } List <Commodity> refCommods = info.Commodities; if (refCommods.Count != commodities.Count) { results.Add(new TestResult(infoFile, "Commodities Count is " + commodities.Count.ToString() + ", expected " + refCommods.Count.ToString())); } for (int i = 0; i < refCommods.Count && i < commodities.Count; i++) { if (!commodities[i].ToString().Equals(refCommods[i].ToString())) { results.Add(new TestResult(infoFile, "Expected: " + refCommods[i].ToString() + " Got: " + commodities[i].ToString())); } } DateTime dtStop = DateTime.Now; if (results.Count <= 0) { TestResult result = new TestResult(infoFile, "PASS"); result.Duration = dtStop.Subtract(dtStart).TotalSeconds.ToString(); results.Add(result); } return(results); }
private UnsafeBitmap GetReferenceCharBmp(UnsafeBitmap bmChars, int index) { if (index * 16 >= bmChars.Width) { return(null); } return(bmChars.Clone(new Rectangle(index * 16 + 1, 0, 15, bmChars.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb)); }
private bool IsBlankLine(UnsafeBitmap bmp, int x) { // Called blank if it is not all black. Color cBlack = Color.FromArgb(0, 0, 0); for (int y = 0; y < bmp.Height; y++) { if (bmp.GetPixel(x, y) == cBlack) { return(false); } } return(true); }
public BmpChar(UnsafeBitmap bmChar, char character) { this.character = character; bm = bmChar; if (bm == null) { return; } // Trim empty area from right side of reference bitmap int x = 0; // x = Utils.FindNextVerticalLine(bm, 0, Color.FromArgb(255, 255, 255, 255), true); x = bm.FindNextVerticalLine(0, Color.FromArgb(255, 255, 255), true); if (x > 0) { bm = bm.Clone(new Rectangle(0, 0, x, bm.Height), bm.PixelFormat); } charWidth = bm.Width; // Find character height int y = 0; for (y = 0; y < bm.Height && charTop <= 0; y++) { for (x = 0; x < bm.Width; x++) { if (bm.GetPixel(x, y) != Color.FromArgb(255, 255, 255)) { charTop = y; break; } } } for (y = bm.Height - 1; y >= 0 && charBottom <= 0; y--) { for (x = 0; x < bm.Width; x++) { if (bm.GetPixel(x, y) != Color.FromArgb(255, 255, 255)) { charBottom = y; break; } } } charHeight = charBottom - charTop + 1; }
public BinaryPixelBitmap(UnsafeBitmap bm, int xStart, int yStart, int xWidth, int yHeight, BinPixelConvertType convertType, Color?refColor) { // If refColor==null then sets convertType to ColorIsOne and refColor to black // Otherwise if convertType is ColorIsOne then if color matches refColor it is a 1 and all others are 0 // If convertType is ColorIsZero then if color matches refColor then it is a 0 and all others are 1 height = yHeight; columns = new int[xWidth]; int temp; if (refColor == null) { convertType = BinPixelConvertType.ColorIsOne; refColor = Color.FromArgb(0, 0, 0); // Black } if (convertType == BinPixelConvertType.ColorIsOne) { for (int x = xStart; x < xStart + xWidth; x++) { temp = 0; for (int y = yStart; y < (yStart + yHeight) && y < (yStart + 16); y++) { if (bm.GetPixel(x, y) == refColor) { temp |= 1 << (y - yStart); } } columns[x - xStart] = temp; } } else { for (int x = xStart; x < xStart + xWidth; x++) { temp = 0; for (int y = yStart; y < (yStart + yHeight) && y < (yStart + 16); y++) { if (bm.GetPixel(x, y) != refColor) { temp |= 1 << (y - yStart); } } columns[x - xStart] = temp; } } }
public List <TestResult> CheckIslandNames() { UnsafeBitmap bm = new UnsafeBitmap("Test\\IslandNames.tif"); string[] islandNames = { "Wensleydale", "Ventress", "Terjit", "Squibnocket", "Spaniel", "Rowes", "Penobscot", "Morannon", "Mirage", "Lincoln", "Isle of Kent", "Jack's Last Gift", "Halley", "Greenwich", "Fluke", "Descartes Isle", "Caravanserai", "Blackthorpe", "Barbary", "Frond", "Islay of Luthien", "Epsilon", "Eta", "Alpha", "Namath", "Oyster", "Vernal Equinox", "Xi", "Zeta", "Uxmal", "Quetzal", "Yax Mutal", "Swampfen", "Spectre", "Harmattan", "Kirin", "Typhoon" }; List <TestResult> results = new List <TestResult>(); int rowHeight = 15; int i = 0; int islandIndex = 0; while (i < bm.Height && islandIndex < islandNames.Length) { BinaryPixelBitmap binRow = new BinaryPixelBitmap(bm.CloneAsBin(new Rectangle(0, i, bm.Width, rowHeight), BinPixelConvertType.ColorIsZero, bm.GetPixel(0, i)), rowHeight); string island = ocr.ExtractText(binRow, FontType.Font2All); // Remove the word "Island" or ":" and anything after it from the name and trim // white space from left and right int index = island.IndexOf("Island"); index = index > 0 ? index : island.Length; island = island.Substring(0, index).Trim(); index = island.IndexOf(":"); index = index > 0 ? index : island.Length; island = island.Substring(0, index).Trim(); if (!island.Equals(islandNames[islandIndex])) { results.Add(new TestResult("IslandNames", "Expected '" + islandNames[islandIndex] + "', got '" + island + "'")); } i += rowHeight; islandIndex = i / rowHeight; } if (results.Count <= 0) { results.Add(new TestResult("IslandNames", "PASS")); } return(results); }
// Use white as a zero and any other color as a one. public BinaryPixelBitmapChar(UnsafeBitmap bm, CharInfo info) : base(bm, 0, 0, bm.Width, bm.Height, BinPixelConvertType.ColorIsZero, Color.FromArgb(255, 255, 255)) { this.charInfo = info; // this.character = character; // Hack to avoid "q" being recognized as "c," or "d" being recognized as "cl", etc. // if (info.Character[0].Equals('c') || info.Character[0].Equals(':')) if (info.KerningMin > 0) { canAdjacentRight = false; } // Trim any blank columns from right side of character bitmap representation int blankColumns = 0; for (int i = columns.Length - 1; i >= 0; i--) { if (columns[i] == 0) { blankColumns++; } else { break; } } if (blankColumns > 0) { Array.Resize(ref columns, columns.Length - blankColumns); } // Hack to avoid "i" being recognized instead of "l" // if (info.Character[0].Equals('i') || info.Character[0].Equals('l') || info.Character[0].Equals(',') || info.Character[0].Equals('I') || info.Character[0].Equals(':')) if (this.Width <= 1) { canOverlap = false; } }
private void mnuToolsOCRBitmapFile_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) { CleanupDebugFiles(); PPOcr ppocr = new PPOcr(null); UnsafeBitmap bm = new UnsafeBitmap(openFileDialog.FileName); ppocr.ClearErrors(); Rectangle commodArea = ppocr.FindCommodityArea(bm); string island = ppocr.ExtractIslandName(bm); if (island == null || island.Length <= 0) { MessageBox.Show(this, "Can't find island name", Application.ProductName); } labelTitle.Text = "Island: " + island; List <Commodity> commodities = ppocr.ExtractCommodities(bm, commodArea); if (ppocr.Error.Length > 0) { MessageBox.Show(this, ppocr.Error, Application.ProductName); } if (commodities == null || commodities.Count <= 0) { dataGridView1.DataSource = null; } else { dataGridView1.DataSource = new List <Commodity>(commodities); } } }
public MyFont(string font) { // Load reference bit map of characters for Font #1 UnsafeBitmap bmChars = new UnsafeBitmap(font + "Characters.bmp"); info = FontInfo.Deserialize(font + "Info.xml"); // Create list of reference bitmaps (one for each character) for Font #1 // Used for commodities and island name (when on ship) // Order of characters in FontInfo XML file must match the order in the reference bitmap refBmpChars1.Clear(); refBmpNumChars1.Clear(); int temp; for (int i = 0; i < info.CharInfos.Count; i++) { refBmpChars1.Add(info.CharInfos[i].Character[0], new BmpChar(GetReferenceCharBmp(bmChars, i), info.CharInfos[i].Character[0])); refBinBmpChars.Add(info.CharInfos[i].Character[0], new BinaryPixelBitmapChar(GetReferenceCharBmp(bmChars, i), info.CharInfos[i])); if (int.TryParse(info.CharInfos[i].Character, out temp) || info.CharInfos[i].Character[0].Equals('>') || info.CharInfos[i].Character[0].Equals(',') || info.CharInfos[i].Character[0].Equals('.')) { refBmpNumChars1.Add(info.CharInfos[i].Character[0], refBmpChars1[info.CharInfos[i].Character[0]]); refBinBmpNumChars.Add(info.CharInfos[i].Character[0], refBinBmpChars[info.CharInfos[i].Character[0]]); } } }
private bool IsMatchCharBmp(BmpChar charBmp, UnsafeBitmap unknownBmp, int xStart, bool doOverlap) { Color cBlack = Color.FromArgb(0, 0, 0); // Check if charBmp matches specified locatoin in unknown bitmap if (xStart != lastXStart) { yTop = -1; yBottom = -1; // First time trying to match for this location so find top and bottom for // max width character (15 pixels) to speed up comparisons for (int y = 0; y < unknownBmp.Height && yTop < 0; y++) { for (int x = xStart; x < unknownBmp.Width && x < (xStart + 15); x++) { if (unknownBmp.GetPixel(x, y) == cBlack) { yTop = y; break; } } } if (yTop >= 0) { for (int y = unknownBmp.Height - 1; y >= yTop && yBottom < 0; y--) { for (int x = xStart; x < unknownBmp.Width && x < (xStart + 15); x++) { if (unknownBmp.GetPixel(x, y) == cBlack) { yBottom = y; break; } } } } } lastXStart = xStart; // Check if height is within what will match if (yTop < 0 || yBottom < 0) { return(false); } if (charBmp.CharTop < yTop) { return(false); } if (charBmp.CharBottom > yBottom) { return(false); } // Now check character matches, allow left most column and right most column // to overlap Color cUnknown, cKnown; // Check left column for (int y = yTop; y <= yBottom; y++) { if (doOverlap) { if (charBmp.GetPixel(0, y) == cBlack && !(unknownBmp.GetPixel(xStart, y) == cBlack)) { return(false); } } else { cKnown = charBmp.GetPixel(0, y); cUnknown = unknownBmp.GetPixel(xStart, y); if (cKnown == cBlack && cUnknown != cBlack) { return(false); } if (cUnknown == cBlack && cKnown != cBlack) { return(false); } } } // Check middle part of character for (int x = 1; (x < charBmp.CharWidth - 1) && ((xStart + x) < unknownBmp.Width); x++) { for (int y = yTop; y <= yBottom; y++) { cKnown = charBmp.GetPixel(x, y); cUnknown = unknownBmp.GetPixel(xStart + x, y); if (cKnown == cBlack && cUnknown != cBlack) { return(false); } if (cUnknown == cBlack && cKnown != cBlack) { return(false); } } } // Check right part of character if (charBmp.CharWidth > 1) { for (int y = yTop; y <= yBottom; y++) { if (doOverlap) { if (charBmp.GetPixel(charBmp.CharWidth - 1, y) == cBlack && !(unknownBmp.GetPixel(xStart + charBmp.CharWidth - 1, y) == cBlack)) { return(false); } } else { cKnown = charBmp.GetPixel(charBmp.CharWidth - 1, y); cUnknown = unknownBmp.GetPixel(xStart + charBmp.CharWidth - 1, y); if (cKnown == cBlack && cUnknown != cBlack) { return(false); } if (cUnknown == cBlack && cKnown != cBlack) { return(false); } } } } // Found match return(true); }