Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program new;
- const
- IMAGE_PATH = 'tess.png';
- IMAGE_PADDING = TBox([3, 2, 6, 8]);
- FONT_NAME = 'DejaVu Sans Mono';
- FONT_SIZE = 10;
- FONT_WIDTH = 9;
- FONT_HEIGHT = 16;
- var
- Chars: array [0..164] of TMufasaBitmap;
- function CompareImageAt(Image, Templ: TMufasaBitmap; Pt: TPoint; Tol: Int32): Double;
- var x,y,w,h,sum: Int32;
- begin
- H := Templ.GetHeight;
- W := Templ.GetWidth;
- if (W = 0) or (H = 0) then Exit(0);
- sum := 0;
- for y:=0 to h-1 do
- for x:=0 to w-1 do
- if SimilarColors(Image.GetPixel(x+Pt.x,y+Pt.y), Templ.GetPixel(x,y), Tol) then
- Inc(sum);
- Result := sum / (W*H);
- end;
- procedure LoadChars();
- var i: Int32;
- begin
- for i:=0 to High(Chars) do
- begin
- Chars[i].Init(client.GetMBitmaps());
- Chars[i].SetSize(FONT_WIDTH, FONT_HEIGHT);
- Chars[i].DrawSystemText(Chr(i), FONT_NAME, FONT_SIZE, Point(0,0), False, $FFFFFF);
- end;
- end;
- procedure FreeChars();
- var i: Int32;
- begin
- for i:=Low(chars) to High(chars) do
- chars[i].Free();
- end;
- function GetChar(input: TMufasaBitmap; p: TPoint): Char;
- var
- i: Int32;
- match, best: Double;
- begin
- for i:=32 to High(Chars) do
- begin
- match := CompareImageAt(input, Chars[i], p, 5);
- if match > best then
- begin
- best := match;
- Result := Chr(i);
- if match = 1 then Exit;
- end;
- end;
- end;
- function GetBlocks(Im: TMufasaBitmap; Padding: TBox; FontW, FontH: Int32): T2DPointArray;
- var TPA: TPointArray;
- begin
- TPA := TPAFromBox([Padding.X1, Padding.Y1, Im.GetWidth-1-Padding.X2, Im.GetHeight-1-Padding.Y2]);
- Result := PartitionTPA(TPA, FontW,FontH);
- end;
- function ChopImage(Image: TMufasaBitmap): TPointArray;
- var
- blocks: T2DPointArray;
- bmp: TMufasaBitmap;
- i: Int32;
- B: TBox;
- begin
- blocks := GetBlocks(Image, IMAGE_PADDING, FONT_WIDTH-1,FONT_HEIGHT-1);
- bmp := Image.Copy(0,0, Image.GetWidth-1, Image.GetHeight-1);
- client.GetMBitmaps().AddBMP(bmp);
- for i:=0 to High(blocks) do
- begin
- B := GetTPABounds(blocks[i]);
- if B.x1 = B.x2 then continue;
- B.x2 += 1;
- B.y2 += 1;
- BMP.DrawTPA(EdgeFromBox(B), 255);
- Result += Point(B.x1,B.y1);
- end;
- DisplayDebugImgWindow(bmp.GetWidth, bmp.GetHeight);
- DrawBitmapDebugImg(bmp.GetIndex);
- bmp.Free();
- end;
- var
- bmp: TMufasaBitmap;
- Cuts: TPointArray;
- text: String;
- i,y: Int32;
- begin
- ClearDebug();
- bmp.Init(client.getMBitmaps);
- bmp.LoadFromFile(IMAGE_PATH);
- bmp.ReplaceColor(4333568, 0);
- Cuts := ChopImage(bmp);
- LoadChars();
- AddOnTerminate('FreeChars');
- for i:=0 to High(Cuts) do
- begin
- if Cuts[i].x = 0 then continue;
- if (Cuts[i].y > y) and (i <> 0) then
- begin
- WriteLn(text);
- text := '';
- end;
- text += GetChar(bmp, Cuts[i]);
- y := Cuts[i].y;
- end;
- WriteLn(text);
- bmp.Free();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement