### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [Solved] same example but it gives different values  (Read 3329 times)

#### barakli

• Newbie
• Posts: 3
##### [Solved] same example but it gives different values
« on: August 06, 2014, 10:27:06 pm »
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
« Last Edit: August 09, 2014, 01:18:52 am by barakli »

#### engkin

• Hero Member
• Posts: 2513
##### Re: same example but it gives different values
« Reply #1 on: August 06, 2014, 11:21:57 pm »
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

• Hero Member
• Posts: 1430
##### Re: same example but it gives different values
« Reply #2 on: August 07, 2014, 12:17:29 am »
Do you see the mistake?
At least the second one makes for a nice "sepia effect"
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

#### engkin

• Hero Member
• Posts: 2513
##### Re: same example but it gives different values
« Reply #3 on: August 07, 2014, 04:56:35 am »
At least the second one makes for a nice "sepia effect"
That's true, it is nice.

#### barakli

• Newbie
• Posts: 3
##### Re: same example but it gives different values
« Reply #4 on: August 07, 2014, 08:22:18 pm »
Thank you very much to engkin and  i love this mistake. in God, I've done this error.