-1

I know Delphi for almost a month,

I have found a function code that change the color of the gray-scale by changing the color weights, I would like to ask, is there a faster way than this code for changing color or weighting the color?

        function tform1.changecolorweighting(coloredbmp:tbitmap):tbitmap;
    Var
    X, Y: Integer;
    P   : TColor;
    r,g,b: byte;
    RP,GP,BP:single;
        changegray:tbitmap;
    changecolor:tbitmap;
    begin
    x:=RedWeight.value+GreenWeight.value+BlueWeight.value;
    RP:=RedWeight.value/x;
    GP:=Greenweight.value/x;
    BP:=BlueWeight.value/x;
    changegray := tbitmap.Create;
    changegray.Width := coloredbmp.Width;
    changegray.Height := coloredbmp.Height;
    changecolor.Assign(coloredbmp);
    For X := 0 to changecolor.Width do
    begin
    For y := 0 to changecolor.Height do
    begin
    P := changecolor.Canvas.Pixels[X, Y];
    r := (P and $00000FF);
    g := (P and $00FF00) shr 8;
    b := (P and $FF0000) shr 16;
    changegray.Canvas.Pixels[X, Y] :=  round(r * RP + g * GP + b*BP) * $010101;
    end;
    end;
    result := changegray;
    end;

if there is someone of you has a faster way of changing the color weights, please correct the code that I have found in the internet, or if you have something to offer faster than that code, please help.

The code above, it takes 1 second before the gray-scale applied with the color weighting.

thank you

claBau
  • 9
  • 3

1 Answers1

0

This is the answer that I'm looking for, it's from Embarcadero: https://community.embarcadero.com/blogs/entry/converting-to-grayscale-with-tbitmapscanline-property-39051

    procedure ToGray(aBitmap: Graphics.TBitmap; redweightvalue,greenweightvalue,blueweightvalue:integer);
    var w, h: integer; CurrRow, OffSet: integer;
    x: byte; pRed, pGreen, pBlue: PByte;
    function RGBToGray(R, G, B: byte): byte;
    var x:integer;
    RP,GP,BP:single;
    begin
    x:=redweightvalue+greenweightvalue+blueweightvalue;
    RP:=redweightvalue/x;
    GP:=greenweightvalue/x;
    BP:=blueweightvalue/x;
    //Result := round(0.2989*R + 0.5870*G + 0.1141*B); // coeffs from Matlab
    Result := round(rp*R + gp*G + bp*B);
    end;
    begin
    if aBitmap.PixelFormat <> pf24bit then exit;
    CurrRow := Integer(aBitmap.ScanLine[0]);
    OffSet := Integer(aBitmap.ScanLine[1]) - CurrRow;
    for h := 0 to aBitmap.Height - 1 do
    begin
    for w := 0 to aBitmap.Width - 1 do
    begin
    pBlue  := pByte(CurrRow + w*3);
    pGreen := pByte(CurrRow + w*3 + 1);
    pRed   := pByte(CurrRow + w*3 + 2);
    x := RGBToGray(pRed^, pGreen^, pBlue^);
    pBlue^  := x;
    pGreen^ := x;
    pRed^   := x;
    end;
    inc(CurrRow, OffSet);
    end;
    end;
whosrdaddy
  • 11,720
  • 4
  • 50
  • 99
claBau
  • 9
  • 3