Recent

Author Topic: What's wrong with my code?  (Read 5608 times)

wp

  • Hero Member
  • *****
  • Posts: 8387
Re: What's wrong with my code?
« Reply #30 on: March 06, 2021, 07:43:25 pm »
OK. Here is a version which avoids the Pixels property. It is still based on the units which come with Lazarus/FPC, in this case IntfGraphics. Unlike using ScanLine it does not require knowledge of the color structure of the pixels. In my tests the previous demo runs in a few milliseconds:
Got this error:
Strange, for me it works (what is your Lazarus and FPC version?)

Declare them as HBITMAP, instead of THandle. But you must add LCLType to the uses clause.

Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLType,     // <------------ ADD
  3.   IntfGraphics, fpimage;
  4.  
  5. procedure Blur(ASource, ADest: TCustomBitmap);
  6. var
  7.   srcImg, destImg: TLazIntfImage;
  8.   c, c1, c2, c3, c4, avgC, resC: TFPColor;
  9.   hBmp, hMask: HBITMAP;   // <----------- HBITMAP instead of THandle
  10.   i, j: Integer;
  11. begin
  12.   srcImg := ASource.CreateIntfImage;
  13.   destImg := ASource.CreateIntfImage;
  14.   ....
« Last Edit: March 06, 2021, 07:48:41 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

justnewbie

  • Sr. Member
  • ****
  • Posts: 273
Re: What's wrong with my code?
« Reply #31 on: March 06, 2021, 08:54:34 pm »
Wow!  :o
(My new task: trying to interpret your code.  :))
« Last Edit: March 06, 2021, 08:59:39 pm by justnewbie »

justnewbie

  • Sr. Member
  • ****
  • Posts: 273
Re: What's wrong with my code?
« Reply #32 on: March 06, 2021, 09:22:08 pm »
@wp, let me a question:
why only the c1 has type-casting and for the others not?
Code: Pascal  [Select][+][-]
  1. avgC.Red := (Int64(c1.Red) + c2.Red + c3.Red + c4.Red) div 4;

wp

  • Hero Member
  • *****
  • Posts: 8387
Re: What's wrong with my code?
« Reply #33 on: March 06, 2021, 09:55:52 pm »
TFPColor (avgC, c1, c2, c3, c4) is a record consisting for unsigned 16-bit elements for Red, Green, Blue and Alpha. In the code snippet that you cite, four word values are added for calculating the average Red component. The addition may overflow the range of the word data type. Therefore, I am casting to the next larger integer type (sorry, Int64 is not needed, Int32 or DWord, would be enough). I am casting only the first value because then the sum will be the casted type too, and the same with the other values. The final division by 4 brings the average value back into the word range again.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

justnewbie

  • Sr. Member
  • ****
  • Posts: 273
Re: What's wrong with my code?
« Reply #34 on: March 06, 2021, 10:09:28 pm »
... I am casting only the first value because then the sum will be the casted type too, and the same with the other values...
I didn't know it, thank you!
(Your code is fantastic. An 5120x2880 image is blurred within 3.5 seconds.)
« Last Edit: March 06, 2021, 10:14:45 pm by justnewbie »

wp

  • Hero Member
  • *****
  • Posts: 8387
Re: What's wrong with my code?
« Reply #35 on: March 06, 2021, 10:17:30 pm »
(My new task: trying to interpret your code.  :))
I understand. There is not good documentation of the basic fcl-image routines and lazarus IntfGraphics, and I must say that this topic still has many surprises for myself. In https://wiki.freepascal.org/Developing_with_Graphics#Working_with_TLazIntfImage.2C_TRawImage_and_TLazCanvas, you can find some more examples.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

justnewbie

  • Sr. Member
  • ****
  • Posts: 273
Re: What's wrong with my code?
« Reply #36 on: March 06, 2021, 10:34:58 pm »
(My new task: trying to interpret your code.  :))
I understand. There is not good documentation of the basic fcl-image routines and lazarus IntfGraphics, and I must say that this topic still has many surprises for myself. In https://wiki.freepascal.org/Developing_with_Graphics#Working_with_TLazIntfImage.2C_TRawImage_and_TLazCanvas, you can find some more examples.
Thank you very much!

 

TinyPortal © 2005-2018