Recent

Author Topic: Help basic Image Quality when using strech ???  (Read 536 times)

Kriscall

  • Newbie
  • Posts: 1
Help basic Image Quality when using strech ???
« on: January 16, 2025, 12:36:14 pm »
Hello everyone,

I'm new to Lazarus and have a question about handling large images. When I import a high-resolution image, its quality looks great initially. However, when I scale (stretch) it down, the image becomes pixelated and looks terrible. I understand that if the image is already at the desired size, the quality remains perfect.

Is there a way to automatically scale larger images to the required size while maintaining their quality? I haven't been able to find this setting in the basic options. I don't want to leave Lazarus to resize each image I import in to my application form when designing a GUI.

Any assistance would be greatly appreciated!

Thank you!

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Help basic Image Quality when using strech ???
« Reply #1 on: January 16, 2025, 12:46:01 pm »
You need a filter for that. Many different ones are available.
But I am sure they don't want the Trumps back...

cdbc

  • Hero Member
  • *****
  • Posts: 1814
    • http://www.cdbc.dk
Re: Help basic Image Quality when using strech ???
« Reply #2 on: January 16, 2025, 01:36:21 pm »
Hi
The easiest way to go about that, is to download BGRABitmap 'Full' I guess, but maybe 'Core' is enough, I dunno...
Then here's a little unit for you to peruse, maybe you can use some of that down-sampling stuff, done with BGRABitmap, inside of it... :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

wp

  • Hero Member
  • *****
  • Posts: 12591
Re: Help basic Image Quality when using strech ???
« Reply #3 on: January 16, 2025, 01:51:33 pm »
When I import a high-resolution image, its quality looks great initially. However, when I scale (stretch) it down, the image becomes pixelated and looks terrible.
This is a contradiction to me - when I scale an image down I "shrink" it rather than "stretch" it. But the image becomes pixelated when I scale it up (i.e. increase its size).

Anyway, here is some scaling code based on fcl-image classes (https://wiki.freepascal.org/fcl-image). The optional argument "AInterpolation" specifies the class for interpolation of the pixels in the output image. The units FPCanvas, FPInterpolation and LazCanvas provide several of these interpolation methods. The TFPBaseInterpolation used by the following procedure by default results in very decent scaled images, in my opinion (some of the other transformations are buggy).

Code: Pascal  [Select][+][-]
  1. uses
  2.   FPImage, FPCanvas, FPImgCanv, FPReadJpeg, FPWriteJpeg, FPReadPNG, FPWritePNG, FPReadBmp, FPWriteBmp;  // more reader/writer units available
  3.  
  4. type
  5.   TFPCustomInterpolationClass = class of TFPCustomInterpolation;
  6.  
  7. // ASrcFilename - file name of the image to be scaled
  8. // ADestFilename - file name of the scaled image.
  9. // ANewSize - new width or height whichever is larger
  10. // AInterpolation - interpolation class (optional)
  11. procedure ScaleImg(const ASrcFileName, ADestFileName: String; ANewSize: Integer;
  12.   AInterpolation: TFPCustomInterpolationClass = nil);
  13. var
  14.   src, dest: TFPMemoryImage;
  15.   canv: TFPImageCanvas;
  16.   w, h: Integer;
  17. begin
  18.   src := TFPMemoryImage.Create(0, 0);
  19.   dest := TFPMemoryImage.Create(0, 0);
  20.   canv := TFPImageCanvas.Create(dest);
  21.   if AInterpolation = nil then
  22.     AInterpolation := TFPBaseInterpolation;
  23.   canv.Interpolation := AInterpolation.Create;
  24.   try
  25.     // Load source image
  26.     src.LoadFromFile(ASrcFileName);
  27.  
  28.     // Calculate new width and height, keep aspect ratio
  29.     if src.Width > src.Height then
  30.     begin
  31.       w := ANewSize;
  32.       h := round(w * src.Height/src.Width);
  33.     end else
  34.     begin
  35.       h := ANewSize;
  36.       w := round(h * src.Width/src.Height);
  37.     end;
  38.  
  39.     // Set size of destination image
  40.     dest.SetSize(w, h);
  41.  
  42.     // Stretch-draw source image on the canvas of the destination image.
  43.     canv.StretchDraw(0, 0, w, h, src);
  44.  
  45.     // Save destination image
  46.     dest.SaveToFile(ADestFileName);
  47.   finally
  48.     // Clean-up
  49.     canv.Interpolation.Free;
  50.     canv.Free;
  51.     dest.Free;
  52.     src.Free;
  53.   end;
  54. end;

Another possibility is usage of BGRABitmap, but his already has been mentioned by cdbc.
« Last Edit: January 16, 2025, 02:16:09 pm by wp »

jamie

  • Hero Member
  • *****
  • Posts: 6801
Re: Help basic Image Quality when using strech ???
« Reply #4 on: January 16, 2025, 07:17:00 pm »
In windows I first set the bltstretchmde to half tone.
After that a stretch draw call will blend the pixels. Works best when enlarging.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018