[Solved] same example but it gives different values lazarus / 39-Graphics ---------------------------------------------------------------------- barakli · 2014-08-06 20:27 i have two same examples. But two examples have different gray scale image. why it is different? first example code is: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); var   IntfImg1, IntfImg2: TLazIntfImage;   px, py: Integer;   Row1, Row2: PRGBTripleArray;   abitmap:TBitmap; begin   abitmap:=TBitmap.Create;   abitmap.LoadFromFile('bb.bmp');   IntfImg1:=abitmap.CreateIntfImage;   IntfImg2:=TLazIntfImage.Create(0,0);   IntfImg2.LoadFromBitmap(aBitmap.Handle,aBitmap.MaskHandle);    for py:=0 to IntfImg1.Height-1 do begin      Row1 := IntfImg1.GetDataLineStart(py); //like Delphi TBitMap.ScanLine      Row2 := IntfImg2.GetDataLineStart(py); //like Delphi TBitMap.ScanLine      for px:=0 to IntfImg1.Width-1 do begin        Row2^[px].rgbtRed    :=  round((Row1^[px].rgbtRed+Row1^[px].rgbtGreen+Row1^[px].rgbtBlue)/3);        Row2^[px].rgbtGreen  :=  round((Row1^[px].rgbtRed+Row1^[px].rgbtGreen+Row1^[px].rgbtBlue)/3);        Row2^[px].rgbtBlue   :=  round((Row1^[px].rgbtRed+Row1^[px].rgbtGreen+Row1^[px].rgbtBlue)/3);      end;    end;    Image1.Stretch:=True;    Image1.Picture.Bitmap.LoadFromIntfImage(IntfImg2); end; second code is: Code: [Select]procedure TForm1.Button2Click(Sender: TObject);   var   IntfImg1, IntfImg2: TLazIntfImage;   px, py: Integer;   Row1, Row2: PRGBTripleArray;   abitmap:TBitmap;   CurColor: TFPColor; begin    abitmap:=TBitmap.Create;    abitmap.LoadFromFile('bb.bmp');    IntfImg1:=abitmap.CreateIntfImage;    IntfImg2:=TLazIntfImage.Create(0,0);    IntfImg2.LoadFromBitmap(aBitmap.Handle,aBitmap.MaskHandle);     for py:=0 to IntfImg1.Height-1 do begin       Row1 := IntfImg1.GetDataLineStart(py); //like Delphi TBitMap.ScanLine       Row2 := IntfImg2.GetDataLineStart(py); //like Delphi TBitMap.ScanLine       for px:=0 to IntfImg1.Width-1 do begin         CurColor:=IntfImg1.Colors[px,py];         CurColor.red    :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);         CurColor.green  :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);         CurColor.blue   :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);         IntfImg2.Colors[px,py]:=CurColor;       end;     end;     Image1.Stretch:=True;     Image1.Picture.Bitmap.LoadFromIntfImage(IntfImg2); end; first example result:       http://www.opencvtr.net/buglikod/1.png second example result:  http://www.opencvtr.net/buglikod/2.png ---------------------------------------------------------------------- engkin · 2014-08-06 21:21 Welcome to FPC/Lazarus. This mistake should teach you two things: 1-Do not repeat the same calculation. 2-Repeating the same calculations is wrong. The second case: Quote        CurColor:=IntfImg1.Colors[px,py];         CurColor.red    :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);         CurColor.green  :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);         CurColor.blue   :=  round((CurColor.red+CurColor.green+CurColor.blue)/3); Legend: Original value, New Value. Do you see the mistake? ---------------------------------------------------------------------- Graeme · 2014-08-06 22:17 Quote from: engkin on August 06, 2014, 11:21:57 pmDo you see the mistake? At least the second one makes for a nice "sepia effect" ---------------------------------------------------------------------- engkin · 2014-08-07 02:56 Quote from: Graeme Geldenhuys on August 07, 2014, 12:17:29 amAt least the second one makes for a nice "sepia effect" That's true, it is nice. ---------------------------------------------------------------------- barakli · 2014-08-07 18:22 Thank you very much to engkin and  i love this mistake. in God, I've done this error.