public static Bitmap BoundCore(Bitmap b) { // GDI+ still lies to us - the return format is BGR, NOT RGB. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int stride = bmData.Stride; System.IntPtr Scan0 = bmData.Scan0; int rw, rn, re, rs; rw = rn = re = rs = -1; unsafe { byte *p = (byte *)(void *)Scan0; int nOffset = stride - b.Width * 3; byte red, green, blue; for (int y = 0; y < b.Height; ++y) { for (int x = 0; x < b.Width; ++x) { blue = p[0]; green = p[1]; red = p[2]; if (blue == 255 && green == 255 && red == 255) { if (rw == -1) { rw = x; } rw = Math.Min(rw, x); if (re == -1) { re = x; } re = Math.Max(re, x); if (rn == -1) { rn = y; } rn = Math.Min(rn, y); if (rs == -1) { rs = y; } rs = Math.Max(rs, y); } p += 3; } p += nOffset; } } b.UnlockBits(bmData); int d = Math.Max(re + 1 - rw, rs + 1 - rn); return(FontMethods.CropBitmap(b, new Rectangle(rw, rn, d, d))); }
private void OnImageToBackBone(object sender, System.EventArgs e) { m_Undo = (Bitmap)m_Bitmap.Clone(); FontMethods.ImageToBackBone(m_Bitmap); m_Bitmap = FontMethods.BoundCore(m_Bitmap); this.AutoScroll = true; this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom)); this.Invalidate(); }
private void OnImageFromText(object sender, System.EventArgs e) { KanjiInput dlg = new KanjiInput(); dlg.sValue = "田"; if (DialogResult.OK == dlg.ShowDialog()) { m_Bitmap = (Bitmap)FontMethods.Render(dlg.sValue); BitmapFilter.GrayToBlack(m_Bitmap, BRIGHT); this.AutoScroll = true; this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom)); this.Invalidate(); } }
private void OnRank(object sender, System.EventArgs e) { m_Undo = (Bitmap)m_Bitmap.Clone(); BitmapFilter.GrayToBlack(m_Bitmap, BRIGHT); Bitmap[] arr = FontMethods.ImageToTextR1(m_Bitmap); Bitmap[][] br = new Bitmap[arr.Length][]; Bitmap[] cr = new Bitmap[arr.Length]; for (int i = 0; i < arr.Length; i++) { br[i] = FontMethods.ImageToTextR2(arr[i]); cr[i] = FontMethods.JoinBitmap(br[i]); } m_Bitmap = FontMethods.JoinBitmapH(cr); this.AutoScroll = true; this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom)); this.Invalidate(); Bitmap[][] brcop = new Bitmap[arr.Length][]; List <ABC> listABC = new List <ABC>(); for (int i = 0; i < arr.Length; i++) { Bitmap[] bbcop = new Bitmap[br[i].Length]; for (int j = 0; j < br[i].Length; j++) { bbcop[j] = (Bitmap)br[i][j].Clone(); bbcop[j] = FontMethods.BoundCore(bbcop[j]); int[] fra = FontMethods.Fragment(bbcop[j]); //FontMethods.ImageToBackBone(br[i][j]); br[i][j] = FontMethods.BoundCore(br[i][j]); double[] d = FontMethods.AverageSquare(br[i][j]); double[][] ad = FontMethods.Margin(br[i][j]); ABC strABC = new ABC(); strABC.AverageSquare = d; strABC.ProportionSquare = strABC.AverageSquare[1] == 0 ? 0 : strABC.AverageSquare[0] / strABC.AverageSquare[1]; strABC.Margin = ad; strABC.Fragment = fra; listABC.Add(strABC); } } string mes = String.Join("\n", listABC); Clipboard.SetText(mes); MessageBox.Show(mes); }
private void OnImageToTextR1(object sender, System.EventArgs e) { m_Undo = (Bitmap)m_Bitmap.Clone(); BitmapFilter.GrayToBlack(m_Bitmap, BRIGHT); Bitmap[] arr = FontMethods.ImageToTextR1(m_Bitmap); Bitmap[][] br = new Bitmap[arr.Length][]; Bitmap[] cr = new Bitmap[arr.Length]; for (int i = 0; i < arr.Length; i++) { br[i] = FontMethods.ImageToTextR2(arr[i]); cr[i] = FontMethods.JoinBitmap(br[i]); } m_Bitmap = FontMethods.JoinBitmapH(cr); m_Bitmap = br[0][7]; this.AutoScroll = true; this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom)); this.Invalidate(); }
private void OnAverageSquare(object sender, System.EventArgs e) { m_Undo = FontMethods.BoundCore(m_Undo); int[] fra = FontMethods.Fragment(m_Undo); //already run OnImageToBackBone double[] d = FontMethods.AverageSquare(m_Bitmap); double[][] ad = FontMethods.Margin(m_Bitmap); string mes = d[0].ToString() + ":" + d[1].ToString() + ":" + d[2].ToString() + ":[" + ad[0][0].ToString("0.00000") + ":" + ad[0][1].ToString("0.00000") + "]" + ":[" + ad[1][0].ToString("0.00000") + ":" + ad[1][1].ToString("0.00000") + "]" + ":[" + ad[2][0].ToString("0.00000") + ":" + ad[2][1].ToString("0.00000") + "]" + ":[" + ad[3][0].ToString("0.00000") + ":" + ad[3][1].ToString("0.00000") + "]" + ":[" + ad[4][0].ToString("0.00000") + ":" + ad[4][1].ToString("0.00000") + "]" + ":[" + ad[5][0].ToString("0.00000") + ":" + ad[5][1].ToString("0.00000") + "]" + ":[" + ad[6][0].ToString("0.00000") + ":" + ad[6][1].ToString("0.00000") + "]" + ":[" + ad[7][0].ToString("0.00000") + ":" + ad[7][1].ToString("0.00000") + "]" + ":" + String.Join(",", fra); Clipboard.SetText(mes); MessageBox.Show(mes); }
private void OnDictionary(object sender, System.EventArgs e) { string[] arrdic = new String[] { "田", "字", "奉", "膂", "雪", "こ", "や", "至", "一", "雪", "雪", "だ", "や", "ほ", "’", "ん", "と", "や" }; Bitmap[] br = new Bitmap[arrdic.Length]; Bitmap[] brcop = new Bitmap[arrdic.Length]; List <ABC> listABC = new List <ABC>(); for (int i = 0; i < arrdic.Length; i++) { br[i] = (Bitmap)FontMethods.Render(arrdic[i]); BitmapFilter.GrayToBlack(br[i], BRIGHT); brcop[i] = (Bitmap)br[i].Clone(); brcop[i] = FontMethods.BoundCore(brcop[i]); int[] fra = FontMethods.Fragment(brcop[i]); //FontMethods.ImageToBackBone(br[i]); br[i] = FontMethods.BoundCore(br[i]); double[] d = FontMethods.AverageSquare(br[i]); double[][] ad = FontMethods.Margin(br[i]); ABC strABC = new ABC(); strABC.KEY = arrdic[i]; strABC.AverageSquare = d; strABC.ProportionSquare = strABC.AverageSquare[1] == 0 ? 0 : strABC.AverageSquare[0] / strABC.AverageSquare[1]; strABC.Margin = ad; strABC.Fragment = fra; listABC.Add(strABC); } m_Bitmap = FontMethods.JoinBitmap(brcop); this.AutoScroll = true; this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom)); this.Invalidate(); string mes = String.Join("\n", listABC); Clipboard.SetText(mes); MessageBox.Show(mes); }
public static Bitmap[] ImageToTextR1(Bitmap b) { // GDI+ still lies to us - the return format is BGR, NOT RGB. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int stride = bmData.Stride; System.IntPtr Scan0 = bmData.Scan0; List <Rectangle> brow = new List <Rectangle>(); unsafe { byte *p = (byte *)(void *)Scan0; int nOffset = stride - b.Width * 3; byte red, green, blue; int rx, ry, rw, rh; rx = ry = rw = rh = 0; bool fs = false; int[] fr = new int[b.Height]; for (int y = 0; y < b.Height; ++y) { for (int x = 0; x < b.Width; ++x) { blue = p[0]; green = p[1]; red = p[2]; if (blue == 0 && green == 0 && red == 0) { fr[y]++; } p += 3; } p += nOffset; } for (int y = 0; y < b.Height; ++y) { //first possition has byte -> start row if ((!fs) && fr[y] < b.Width) { rx = 0; ry = y; rw = b.Width; fs = true; } else { //fisrt possition no byte after has byte -> end row if (fs && ((fr[y] == b.Width) || (y + 1 == b.Height))) { rh = y - ry + 1; Rectangle bm = new Rectangle(rx, ry, rw, rh); brow.Add(bm); if (brow.Count > 1) { Rectangle f = brow[brow.Count - 1]; Rectangle l = brow[brow.Count - 2]; //minimum distance between row 4px if (f.Y + 1 - l.Y - l.Height - 4 < 0) { Rectangle n = new Rectangle(l.X, l.Y, f.Width, l.Height + f.Y + 1 - l.Y); brow.RemoveRange(brow.Count - 2, 2); brow.Add(n); } } rx = ry = rw = rh = 0; fs = false; } } } } b.UnlockBits(bmData); Bitmap[] ar = FontMethods.CropBitmap(b, brow.ToArray()); return(ar); }
public static int[] Fragment(Bitmap b) { double[] rw, rn, re, rs; double[][] ad = FontMethods.Margin(b, true); rw = ad[0]; rn = ad[1]; re = ad[2]; rs = ad[3]; // use bitmap before backbone // GDI+ still lies to us - the return format is BGR, NOT RGB. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int stride = bmData.Stride; System.IntPtr Scan0 = bmData.Scan0; List <int> r = new List <int>(); List <int> r1 = new List <int>(); int[][] mr = new int[b.Width][]; int[][] fr = new int[b.Height][]; unsafe { byte *p = (byte *)(void *)Scan0; int nOffset = stride - b.Width * 3; byte red, green, blue; for (int x = 0; x < b.Width; ++x) { mr[x] = new int[b.Height]; } for (int y = 0; y < b.Height; ++y) { fr[y] = new int[b.Width]; for (int x = 0; x < b.Width; ++x) { blue = p[0]; green = p[1]; red = p[2]; if (blue == 255 && green == 255 && red == 255) { mr[x][y] = 1; fr[y][x] = 1; } p += 3; } p += nOffset; } } b.UnlockBits(bmData); for (int x = Math.Max(b.Width / 10, (int)rw[0] + 1); x < Math.Min(9 * b.Width / 10, (int)re[0] - 1); ++x) { if (mr[x].Sum() == 0) { if (b.Width / 10 <= x && x < 2 * b.Width / 10) { r.Add(1); } else if (2 * b.Width / 10 <= x && x < 3 * b.Width / 10) { r.Add(2); } else if (3 * b.Width / 10 <= x && x < 4 * b.Width / 10) { r.Add(3); } else if (4 * b.Width / 10 <= x && x < 5 * b.Width / 10) { r.Add(4); } else if (5 * b.Width / 10 <= x && x < 6 * b.Width / 10) { r.Add(5); } else if (6 * b.Width / 10 <= x && x < 7 * b.Width / 10) { r.Add(6); } else if (7 * b.Width / 10 <= x && x < 8 * b.Width / 10) { r.Add(7); } else if (8 * b.Width / 10 <= x && x < 9 * b.Width / 10) { r.Add(8); } } if (mr[x].Sum() == 1 || mr[x].Sum() == 2) { if (b.Width / 10 <= x && x < 2 * b.Width / 10) { r1.Add(1); } else if (2 * b.Width / 10 <= x && x < 3 * b.Width / 10) { r1.Add(2); } else if (3 * b.Width / 10 <= x && x < 4 * b.Width / 10) { r1.Add(3); } else if (4 * b.Width / 10 <= x && x < 5 * b.Width / 10) { r1.Add(4); } else if (5 * b.Width / 10 <= x && x < 6 * b.Width / 10) { r1.Add(5); } else if (6 * b.Width / 10 <= x && x < 7 * b.Width / 10) { r1.Add(6); } else if (7 * b.Width / 10 <= x && x < 8 * b.Width / 10) { r1.Add(7); } else if (8 * b.Width / 10 <= x && x < 9 * b.Width / 10) { r1.Add(8); } } if (mr[x].Sum() <= 2) { int lx = mr[x].Where((m, i) => i <= 5 * b.Height / 6).Sum(); if (x < b.Width / 3 && lx == 0) { for (int y = 5 * b.Height / 6; y < b.Height; ++y) { int hy = fr[y].Where((m, i) => i > x).Sum(); if (hy == 0) { r.Add(100); } } } } } for (int y = Math.Max(b.Height / 10, (int)rn[1] + 1); y < Math.Min(9 * b.Height / 10, (int)rs[1] - 1); ++y) { if (fr[y].Sum() == 0) { if (b.Height / 10 <= y && y < 2 * b.Height / 10) { r.Add(101); } else if (2 * b.Height / 10 <= y && y < 3 * b.Height / 10) { r.Add(102); } else if (3 * b.Height / 10 <= y && y < 4 * b.Height / 10) { r.Add(103); } else if (4 * b.Height / 10 <= y && y < 5 * b.Height / 10) { r.Add(104); } else if (5 * b.Height / 10 <= y && y < 6 * b.Height / 10) { r.Add(105); } else if (6 * b.Height / 10 <= y && y < 7 * b.Height / 10) { r.Add(106); } else if (7 * b.Height / 10 <= y && y < 8 * b.Height / 10) { r.Add(107); } else if (8 * b.Height / 10 <= y && y < 9 * b.Height / 10) { r.Add(108); } } if (fr[y].Sum() == 1 || fr[y].Sum() == 2) { if (b.Height / 10 <= y && y < 2 * b.Height / 10) { r1.Add(101); } else if (2 * b.Height / 10 <= y && y < 3 * b.Height / 10) { r1.Add(102); } else if (3 * b.Height / 10 <= y && y < 4 * b.Height / 10) { r1.Add(103); } else if (4 * b.Height / 10 <= y && y < 5 * b.Height / 10) { r1.Add(104); } else if (5 * b.Height / 10 <= y && y < 6 * b.Height / 10) { r1.Add(105); } else if (6 * b.Height / 10 <= y && y < 7 * b.Height / 10) { r1.Add(106); } else if (7 * b.Height / 10 <= y && y < 8 * b.Height / 10) { r1.Add(107); } else if (8 * b.Height / 10 <= y && y < 9 * b.Height / 10) { r1.Add(108); } } } if (r.Where(m => m >= 1 && m <= 8).Count() == 0) { if (r1.Where(m => m >= 4 && m <= 5).Count() > 0) { r.Add(r1.Where(m => m >= 4 && m <= 5).First()); } else if (r1.Where(m => m >= 1 && m <= 3).Count() > 0) { r.Add(r1.Where(m => m >= 1 && m <= 3).First()); } else if (r1.Where(m => m >= 6 && m <= 8).Count() > 0) { r.Add(r1.Where(m => m >= 6 && m <= 8).First()); } } if (r.Where(m => m >= 101 && m <= 108).Count() == 0) { if (r1.Where(m => m >= 104 && m <= 105).Count() > 0) { r.Add(r1.Where(m => m >= 104 && m <= 105).First()); } else if (r1.Where(m => m >= 101 && m <= 103).Count() > 0) { r.Add(r1.Where(m => m >= 101 && m <= 103).First()); } else if (r1.Where(m => m >= 106 && m <= 108).Count() > 0) { r.Add(r1.Where(m => m >= 106 && m <= 108).First()); } } return(r.Distinct().ToArray()); }
public static bool ImageToBackBone(Bitmap b) { // GDI+ still lies to us - the return format is BGR, NOT RGB. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int stride = bmData.Stride; System.IntPtr Scan0 = bmData.Scan0; int[][] mr = new int[b.Width][]; int[][] fr = new int[b.Height][]; unsafe { byte *p = (byte *)(void *)Scan0; int nOffset = stride - b.Width * 3; byte red, green, blue; for (int x = 0; x < b.Width; ++x) { mr[x] = new int[b.Height]; } for (int y = 0; y < b.Height; ++y) { fr[y] = new int[b.Width]; for (int x = 0; x < b.Width; ++x) { blue = p[0]; green = p[1]; red = p[2]; if (blue == 255 && green == 255 && red == 255) { mr[x][y] = 1; fr[y][x] = 1; } p += 3; } p += nOffset; } FontMethods.Converge(mr, 5); FontMethods.Converge(fr, 5); } unsafe { byte *p = (byte *)(void *)Scan0; int nOffset = stride - b.Width * 3; byte red, green, blue; for (int y = 0; y < b.Height; ++y) { for (int x = 0; x < b.Width; ++x) { blue = p[0]; green = p[1]; red = p[2]; if (blue == 255 && green == 255 && red == 255) { if (mr[x][y] == 0) { p[0] = p[1] = p[2] = 0; } if (fr[y][x] == 0) { p[0] = p[1] = p[2] = 0; } } p += 3; } p += nOffset; } } b.UnlockBits(bmData); _ImageToBackBone(b, mr, fr); return(true); }
private void OnSynthetic(object sender, System.EventArgs e) { List <ABC> des = new List <ABC>(); List <ABC> predic = new List <ABC>(); int im = 0; if (im == 0) { string[] arrdic = new String[] { "田", "字", "奉", "膂", "雪", "こ", "や", "至", "一", "だ", "ほ", "’", "ん", "と" }; Bitmap[] br = new Bitmap[arrdic.Length]; Bitmap[] brcop = new Bitmap[arrdic.Length]; List <ABC> listABC = new List <ABC>(); for (int i = 0; i < arrdic.Length; i++) { br[i] = (Bitmap)FontMethods.Render(arrdic[i]); BitmapFilter.GrayToBlack(br[i], BRIGHT); brcop[i] = (Bitmap)br[i].Clone(); brcop[i] = FontMethods.BoundCore(brcop[i]); int[] fra = FontMethods.Fragment(brcop[i]); //FontMethods.ImageToBackBone(br[i]); br[i] = FontMethods.BoundCore(br[i]); double[] d = FontMethods.AverageSquare(br[i]); double[][] ad = FontMethods.Margin(br[i]); ABC strABC = new ABC(); strABC.KEY = arrdic[i]; strABC.AverageSquare = d; strABC.ProportionSquare = strABC.AverageSquare[1] == 0 ? 0 : strABC.AverageSquare[0] / strABC.AverageSquare[1]; strABC.Margin = ad; strABC.Fragment = fra; listABC.Add(strABC); } predic = listABC; } im = 1; if (im == 1) { m_Undo = (Bitmap)m_Bitmap.Clone(); BitmapFilter.GrayToBlack(m_Bitmap, BRIGHT); Bitmap[] arr = FontMethods.ImageToTextR1(m_Bitmap); Bitmap[][] br = new Bitmap[arr.Length][]; Bitmap[] cr = new Bitmap[arr.Length]; for (int i = 0; i < arr.Length; i++) { br[i] = FontMethods.ImageToTextR2(arr[i]); cr[i] = FontMethods.JoinBitmap(br[i]); } m_Bitmap = FontMethods.JoinBitmapH(cr); this.AutoScroll = true; this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom)); this.Invalidate(); Bitmap[][] brcop = new Bitmap[arr.Length][]; List <ABC> listABC = new List <ABC>(); for (int i = 0; i < arr.Length; i++) { Bitmap[] bbcop = new Bitmap[br[i].Length]; for (int j = 0; j < br[i].Length; j++) { bbcop[j] = (Bitmap)br[i][j].Clone(); bbcop[j] = FontMethods.BoundCore(bbcop[j]); int[] fra = FontMethods.Fragment(bbcop[j]); //FontMethods.ImageToBackBone(br[i][j]); br[i][j] = FontMethods.BoundCore(br[i][j]); double[] d = FontMethods.AverageSquare(br[i][j]); double[][] ad = FontMethods.Margin(br[i][j]); ABC strABC = new ABC(); strABC.AverageSquare = d; strABC.ProportionSquare = strABC.AverageSquare[1] == 0 ? 0 : strABC.AverageSquare[0] / strABC.AverageSquare[1]; strABC.Margin = ad; strABC.Fragment = fra; listABC.Add(strABC); } } des = listABC; } FncSynthetic(des, predic); }