1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127:
| procedure FastPixelInterpolate(Image: TBitmap; Color: TColor; tolerance: integer; Maximum: Integer = 20); function ColorDifference(R1, G1, B1: byte; col2: TColor): integer; var R2, G2, B2, RD, GD, BD: byte; begin R2 := getrvalue(col2); G2 := getgvalue(col2); B2 := getbvalue(col2); RD := round(abs(r1 - r2) / 2.55); GD := round(abs(g1 - g2) / 2.55); BD := round(abs(b1 - b2) / 2.55); result := round(0.3 * rd + 0.59 * gd + 0.11 * bd); end; type PixArray = Array[1..3] of Byte; var X, Y, Pol: Integer; XPol, YPol: Integer; Step, BPol: Integer; R, G, B: Integer; aBitmap: TBitmap; ScanL, ScanQ: ^PixArray; ScanN: ^PixArray; begin Step := 0; BPol := 0;
Image.PixelFormat := pf24bit; aBitmap := TBitmap.Create;
with aBitmap do begin Width := Image.Width; Height := Image.Height; PixelFormat := Image.PixelFormat; Canvas.Brush.Color := Color; end;
for Y := 0 to Image.Height - 1 do begin ScanL := Image.ScanLine[Y];
for X := 0 to Image.Width - 1 do begin if ColorDifference(ScanL^[3], ScanL^[2], ScanL^[1], Color) <= tolerance then inc(BPol);
inc(ScanL); end; end;
while (BPol <> 0) AND (Step < Maximum + 1) do begin with aBitmap do Canvas.Rectangle(0, 0, Width, Height);
for Y := 0 to Image.Height - 1 do begin ScanL := Image.ScanLine[Y]; ScanN := aBitmap.ScanLine[Y];
for X := 0 to Image.Width - 1 do begin if ColorDifference(ScanL^[3], ScanL^[2], ScanL^[1], Color) <= tolerance then begin R := 0; G := 0; B := 0; Pol := 0;
for YPol := (Y - 2) to (Y + 2) do if (YPol > -1) and (YPol < Image.Height) then begin ScanQ := Image.ScanLine[YPol]; inc(ScanQ, X - 2);
for XPol := (X - 2) to (X + 2) do begin if (XPol > -1) AND (XPol < Image.Width) then begin if ColorDifference(ScanQ^[3], ScanQ^[2], ScanQ^[1], Color) > tolerance then begin R := R + ScanQ^[3]; G := G + ScanQ^[2]; B := B + ScanQ^[1];
inc(Pol); end; end;
inc(ScanQ); end; end;
if Pol > 0 then begin ScanN^[3] := R div Pol; ScanN^[2] := G div Pol; ScanN^[1] := B div Pol;
dec(BPol); end; end;
inc(ScanL); inc(ScanN); end; end;
for Y := 0 to Image.Height - 1 do begin ScanL := aBitmap.ScanLine[Y]; ScanQ := Image.ScanLine[Y];
for X := 0 to Image.Width - 1 do begin if ColorDifference(ScanL^[3], ScanL^[2], ScanL^[1], Color) > tolerance then for XPol := 1 to 3 do ScanQ^[XPol] := ScanL^[XPol];
inc(ScanL); inc(ScanQ); end; end;
inc(Step); end; end; |