Advertisement
WarPie90

Untitled

May 3rd, 2018
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.83 KB | None | 0 0
  1. program new;
  2.  
  3. const
  4.   IMAGE_PATH    = 'tess.png';
  5.   IMAGE_PADDING = TBox([3, 2, 6, 8]);
  6.  
  7.   FONT_NAME   = 'DejaVu Sans Mono';
  8.   FONT_SIZE   = 10;
  9.  
  10.   FONT_WIDTH  = 9;
  11.   FONT_HEIGHT = 16;
  12.  
  13. var
  14.   Chars: array [0..164] of TMufasaBitmap;
  15.  
  16. function CompareImageAt(Image, Templ: TMufasaBitmap; Pt: TPoint; Tol: Int32): Double;
  17. var x,y,w,h,sum: Int32;
  18. begin
  19.   H := Templ.GetHeight;
  20.   W := Templ.GetWidth;
  21.   if (W = 0) or (H = 0) then Exit(0);
  22.  
  23.   sum := 0;
  24.   for y:=0 to h-1 do
  25.     for x:=0 to w-1 do
  26.       if SimilarColors(Image.GetPixel(x+Pt.x,y+Pt.y), Templ.GetPixel(x,y), Tol) then
  27.         Inc(sum);
  28.   Result := sum / (W*H);
  29. end;
  30.  
  31. procedure LoadChars();
  32. var i: Int32;
  33. begin
  34.   for i:=0 to High(Chars) do
  35.   begin
  36.     Chars[i].Init(client.GetMBitmaps());
  37.     Chars[i].SetSize(FONT_WIDTH, FONT_HEIGHT);
  38.     Chars[i].DrawSystemText(Chr(i), FONT_NAME, FONT_SIZE, Point(0,0), False, $FFFFFF);
  39.   end;
  40. end;
  41.  
  42. procedure FreeChars();
  43. var i: Int32;
  44. begin
  45.   for i:=Low(chars) to High(chars) do
  46.     chars[i].Free();
  47. end;
  48.  
  49. function GetChar(input: TMufasaBitmap; p: TPoint): Char;
  50. var
  51.   i: Int32;
  52.   match, best: Double;
  53. begin
  54.   for i:=32 to High(Chars) do
  55.   begin
  56.     match := CompareImageAt(input, Chars[i], p, 5);
  57.     if match > best then
  58.     begin
  59.       best := match;
  60.       Result := Chr(i);
  61.       if match = 1 then Exit;
  62.     end;
  63.   end;
  64. end;
  65.  
  66. function GetBlocks(Im: TMufasaBitmap; Padding: TBox; FontW, FontH: Int32): T2DPointArray;
  67. var TPA: TPointArray;
  68. begin
  69.   TPA    := TPAFromBox([Padding.X1, Padding.Y1, Im.GetWidth-1-Padding.X2, Im.GetHeight-1-Padding.Y2]);
  70.   Result := PartitionTPA(TPA, FontW,FontH);
  71. end;
  72.  
  73. function ChopImage(Image: TMufasaBitmap): TPointArray;
  74. var
  75.   blocks: T2DPointArray;
  76.   bmp: TMufasaBitmap;
  77.   i: Int32;
  78.   B: TBox;
  79. begin
  80.   blocks := GetBlocks(Image, IMAGE_PADDING, FONT_WIDTH-1,FONT_HEIGHT-1);
  81.  
  82.   bmp := Image.Copy(0,0, Image.GetWidth-1, Image.GetHeight-1);
  83.   client.GetMBitmaps().AddBMP(bmp);
  84.   for i:=0 to High(blocks) do
  85.   begin
  86.     B := GetTPABounds(blocks[i]);
  87.     if B.x1 = B.x2 then continue;
  88.     B.x2 += 1;
  89.     B.y2 += 1;
  90.     BMP.DrawTPA(EdgeFromBox(B), 255);
  91.     Result += Point(B.x1,B.y1);
  92.   end;
  93.   DisplayDebugImgWindow(bmp.GetWidth, bmp.GetHeight);
  94.   DrawBitmapDebugImg(bmp.GetIndex);
  95.   bmp.Free();
  96. end;
  97.  
  98. var
  99.   bmp: TMufasaBitmap;
  100.   Cuts: TPointArray;
  101.   text: String;
  102.   i,y: Int32;
  103. begin
  104.   ClearDebug();
  105.  
  106.   bmp.Init(client.getMBitmaps);
  107.   bmp.LoadFromFile(IMAGE_PATH);
  108.   bmp.ReplaceColor(4333568, 0);
  109.  
  110.   Cuts := ChopImage(bmp);
  111.  
  112.   LoadChars();
  113.   AddOnTerminate('FreeChars');
  114.  
  115.   for i:=0 to High(Cuts) do
  116.   begin
  117.     if Cuts[i].x = 0 then continue;
  118.     if (Cuts[i].y > y) and (i <> 0) then
  119.     begin
  120.       WriteLn(text);
  121.       text := '';
  122.     end;
  123.  
  124.     text += GetChar(bmp, Cuts[i]);
  125.     y := Cuts[i].y;
  126.   end;
  127.   WriteLn(text);
  128.  
  129.   bmp.Free();
  130. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement