Recent

Author Topic: Unable to use Scale method in TJPEGImage  (Read 13777 times)

jrmit

  • New Member
  • *
  • Posts: 10
  • Obj Pascal Sings!
Unable to use Scale method in TJPEGImage
« on: January 27, 2010, 05:24:59 pm »
TJPEGImage.Scale will not compile

I am fairly new to Lazarus and was trying to port an application from Delphi for resizing Jpeg images to various screen resolutions (such as 1400x900), but ran into a problem with TJPEGImage.Scale.  It appears Scale is not supported in Lazarus' implementation of TJPEGImage.  Any suggestions.

procedure TForm1.btnResizeImageClick(Sender: TObject);
var
  Jpeg1, Jpeg2: TJPEGImage;
  TempBitmap : TBitMap;
  CanvasRect : TRect;
  x, nscale : integer;
  aspect, maxaspect : single;
  picdisp : String;
begin
  x := lbxFileListBox.ItemIndex;
  picdisp := lbxFileListBox.Items
  • ;

  TempBitmap := TBitmap.Create;
  Jpeg1 := TJPEGImage.Create;
  Jpeg2 := TJPEGImage.Create;
  try
    Jpeg1.LoadFromFile(picdisp);
    //Find the width and height which fits inside the maximum rectangle
    //while preserving the aspect ratio
    aspect := Jpeg1.Width /Jpeg1.Height;
    maxaspect := sw / sh;
    if (aspect > maxaspect) then
    begin
      TempBitmap.Width := sw;
      TempBitmap.Height := round(sw / aspect);
    end
    else
    begin
      TempBitmap.Width := round(sh * aspect);
      TempBitmap.Height := sh;
    end;
    //Find the scale that causes the jpg to be as small as possible while
    //still larger than the destination width and height.  This ensures that
    //when the image is resampled it doesn't increase any dimention, only downsize
    nscale :=floor(min(Jpeg1.Width / TempBitmap.Width, Jpeg1.Height /TempBitmap.Height));
    case nscale of
      0..1: begin
        Jpeg1.Scale := jsFullSize;  Error: identifier indents no member "Scale"
        nscale := 1;
      end;
      ... more code                         


theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1947

jrmit

  • New Member
  • *
  • Posts: 10
  • Obj Pascal Sings!
Re: Unable to use Scale method in TJPEGImage
« Reply #2 on: January 28, 2010, 04:35:58 pm »
Thanks, Theo.  Actually, I am already using canvas.stretchdraw further down in the code (not shown).  I will revise the code not to use TJPEGImage.Scale.

jrmit

  • New Member
  • *
  • Posts: 10
  • Obj Pascal Sings!
Re: Unable to use Scale method in TJPEGImage
« Reply #3 on: March 25, 2010, 04:34:45 pm »
Since JPEGImage.Scale and Scanline were not available in Lazarus I modified the resize (resample) procedure using some code derived from (www.efg2.com/Lab ImageProcessing/AspectRatio.htm).

    Jpeg1.LoadFromFile(picdisp);
    .
    . 
    if   Jpeg1.Width / Jpeg1.Height  <  sw / sh then
    begin
      CanvasRect.Top    := 0;
      CanvasRect.Bottom := NBitmap.Height;
      wt := MathRound((NBitmap.Height * Jpeg1.Width) / Jpeg1.Height);
      Half := (NBitmap.Width - wt) div 2;
      CanvasRect.Left  := Half;
      CanvasRect.Right := CanvasRect.Left + wt;
    end
    else
    begin
      CanvasRect.Left    := 0;
      CanvasRect.Right   := NBitmap.Width;
      ht := MathRound((NBitmap.Width * Jpeg1.Height) / Jpeg1.Width);
      Half := (NBitmap.Height - ht) div 2;
      CanvasRect.Top    := Half;
      CanvasRect.Bottom := CanvasRect.Top + ht;
    end;
    .
    .
  end;

Read More: microguyscube.com/index.php/image-modify-procedure-for-lazarus

John
www.microguyscube.com




derek.john.evans

  • Guest
Re: Unable to use Scale method in TJPEGImage
« Reply #4 on: February 02, 2013, 05:20:09 am »
I also wanted the Scale property in Lazarus. I tried using TFPMemoryImage and TFPReaderJpeg, but the images came out with pixels missing. But, I found this solution:

Code: Pascal  [Select][+][-]
  1. uses FPReadJPEG, IntfGraphics; // May require other units
  2.  
  3. type
  4.  
  5.   TJPEGImagePlus = class(TJPEGImage)
  6.   private
  7.     FScale: TJPEGScale;
  8.   protected
  9.     procedure InitializeReader(AImage: TLazIntfImage;
  10.       AReader: TFPCustomImageReader); override;
  11.   public
  12.     property Scale: TJPEGScale read FScale write FScale;
  13.   end;
  14.  
  15. procedure TJPEGImagePlus.InitializeReader(AImage: TLazIntfImage;
  16.   AReader: TFPCustomImageReader);
  17. begin
  18.   (AReader as TFPReaderJpeg).Scale := FScale;
  19.   inherited;
  20. end;  
  21.  

Then just load the JPEG's as you normally do via TJpegImage:
Code: Pascal  [Select][+][-]
  1.   LJPEGImage := TJPEGImagePlus.Create;
  2.   LJPEGImage.Scale := jsEighth;
  3.   LJPEGImage.LoadFromFile(AFileName);
  4.   FPicture.Assign(LJPEGImage);
  5.   FreeAndNil(LJPEGImage);
  6.  
Hope that helps people.
« Last Edit: October 02, 2015, 03:45:31 am by Geepster »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Unable to use Scale method in TJPEGImage
« Reply #5 on: February 02, 2013, 08:55:21 am »
Guys,

Great that you have improvements for Lazarus.

If they're good enough, could you please post them as patches to bugtracker issues? That way, they won't be forgotten or overlooked.
See e.g.
http://wiki.lazarus.freepascal.org/Creating_A_Patch
and
http://wiki.lazarus.freepascal.org/How_do_I_create_a_bug_report

Thanks a lot!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018