Recent

Author Topic: Make a Border Or Do a Crop: TBGRABitmap might be the solution  (Read 1256 times)

Boleeman

  • Hero Member
  • *****
  • Posts: 686
Make a Border Or Do a Crop: TBGRABitmap might be the solution
« on: August 05, 2024, 03:54:25 pm »
Made a program to Make a Border Or Do a Crop   (Do a Crop and Not Do a Crap, Ha Ha.)

Set the Border size with a TSpinedit  or Set the Crop decrease in size with that same TSpinedit
Clicking on the filename in the TFilelistbox loads the picture file.

Works for Bmp, png, jpg BUT NOT GiF saved back to GIF, unless I convert the gif to png.


Problem: Using FPWriteGIF  from https://www.gocher.me/FPWriteGIF  gives an Access Violation Error when Adding a Border or Cropping a Gif file.

Not sure why FPWriteGIF is making the Access Violation Error and how do I get rid of that error.

I played around with some FPWriteGIF code by KodeZerg and successfully converted from bmp to gif in another sample code shown below without any Access Violation Error, but I was unable to get rid of that error in my own Border/Cropping program. I wonder if anyone can help out?

Not sure if Bgrabmp might work better in opening and saving to different picture formats?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   fpImage, fpWriteGif;
  10.  
  11. type
  12.   TMyGifImage = class(TGifImage)
  13.   protected
  14.     class function GetWriterClass: TFPCustomImageWriterClass; override;
  15.   end;
  16.  
  17.   TForm1 = class(TForm)
  18.     Button1: TButton;
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. class function TMyGifImage.GetWriterClass: TFPCustomImageWriterClass;
  34. begin
  35.   Result := TFPWriterGIF;
  36. end;
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.Button1Click(Sender: TObject);
  41. var
  42.   bmp: TBitmap;
  43.   gif: TMyGifImage;
  44. begin
  45.   bmp := TBitmap.Create;
  46.   try
  47.     bmp.LoadfromFile('test.bmp');
  48.     gif := TMyGifImage.Create;
  49.     try
  50.       gif.Assign(bmp);
  51.       gif.SaveToFile('test.gif');
  52.     finally
  53.       gif.Free;
  54.     end;
  55.   finally
  56.     bmp.Free;
  57.   end;
  58. end;
  59.  
  60. initialization
  61.   TPicture.RegisterFileFormat('.gif', 'gif', TMyGifImage);
  62.  
  63. end.
  64.  
« Last Edit: August 07, 2024, 03:31:07 pm by Boleeman »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11782
  • FPC developer.
Re: Make a Border Or Do a Crop: FPWriteGIF Help needed
« Reply #1 on: August 05, 2024, 04:53:23 pm »
Do you pass it a 256 color paletted image  ?

Boleeman

  • Hero Member
  • *****
  • Posts: 686
Re: Make a Border Or Do a Crop: FPWriteGIF Help needed
« Reply #2 on: August 06, 2024, 12:17:59 pm »
I made a simple test gif in MSPaint.
Not sure of the palette size, but that gave Access Violation error as well.

The only difference I can see in KodeZerg's modifed example that works is that the BMP is freed.
Perhaps something like that needs to be done in my program for the FPWriteGIF part of the code ?


Boleeman

  • Hero Member
  • *****
  • Posts: 686
Re: Make a Border Or Do a Crop: FPWriteGIF Help needed
« Reply #3 on: August 06, 2024, 01:40:04 pm »
I might just remove that FPWriteGIF code, as it seems it might be a bit buggy to use.

circular

  • Hero Member
  • *****
  • Posts: 4350
    • Personal webpage
Re: Make a Border Or Do a Crop: FPWriteGIF Help needed
« Reply #4 on: August 06, 2024, 01:52:55 pm »
BGRABitmap has its own implementation of the GIF format. So you may be more lucky.

Note that if editing increases the color count over 256, you will need to set up the color quantizer as follows:
Code: Pascal  [Select][+][-]
  1. uses BGRAPalette, BGRAColorQuantization, ...;
  2. begin
  3.   ...
  4.   BGRAPalette.BGRAColorQuantizerFactory:= TBGRAColorQuantizer;
Conscience is the debugger of the mind

Boleeman

  • Hero Member
  • *****
  • Posts: 686
Re: Make a Border Or Do a Crop: FPWriteGIF Help needed
« Reply #5 on: August 06, 2024, 01:53:31 pm »
Actually found a comment by WP here https://forum.lazarus.freepascal.org/index.php?topic=55818.0


The gif writer unit seems to have an issue with memory management because the demo crashes in the local ClearMappings procedure - knowing that this could create a memory leak


Also found a comment by Jurassic Pork:

to save bitmap in gif format you can use  vampyreImaging library (available on package manager).


Thanks for that reply circular

Looks like you were posting at the same time as I was replying.
Would it be possible for you to provide a very simple example of how to save say a bmp or a png to simple gif using BgraBmp ?
« Last Edit: August 06, 2024, 01:57:07 pm by Boleeman »

circular

  • Hero Member
  • *****
  • Posts: 4350
    • Personal webpage
Re: Make a Border Or Do a Crop: FPWriteGIF Help needed
« Reply #6 on: August 06, 2024, 02:51:57 pm »
Hi Boleeman,

Sure, here is how to load a PNG and save it as GIF:
Code: Pascal  [Select][+][-]
  1. uses BGRABitmap, BGRAAnimatedGif, BGRAPalette, BGRAColorQuantization;
  2. var bmp: TBGRABitmap;
  3. begin
  4.   // global setup for quantization
  5.   BGRAPalette.BGRAColorQuantizerFactory:= TBGRAColorQuantizer;
  6.  
  7.   bmp := TBGRABitmap.Create('test.png');
  8.   bmp.SaveToFile('test.gif'); // normally this will use the writer of BGRAAnimatedGif
  9.   bmp.Free;
  10. end.
« Last Edit: August 07, 2024, 03:23:05 pm by circular »
Conscience is the debugger of the mind

Boleeman

  • Hero Member
  • *****
  • Posts: 686
Re: Make a Border Or Do a Crop: FPWriteGIF Help needed
« Reply #7 on: August 06, 2024, 03:17:01 pm »
Thank you Circular for the test code.

I seem to get an error.

The Image Writer is not registered for this Image Format

Also I could not find TBGRABitmap.RegisterFileFormat  like for the FPWriteGIF

initialization
  TPicture.RegisterFileFormat('.gif', 'gif', TMyGifImage);



« Last Edit: August 06, 2024, 03:21:32 pm by Boleeman »

circular

  • Hero Member
  • *****
  • Posts: 4350
    • Personal webpage
Re: Make a Border Or Do a Crop: TBGRABitmap Save Gif Not Working
« Reply #8 on: August 07, 2024, 03:22:20 pm »
Ah ok. Maybe you need to add BGRAAnimatedGif in the unit clause. This is the unit that registers the format.
Conscience is the debugger of the mind

Boleeman

  • Hero Member
  • *****
  • Posts: 686
Re: Make a Border Or Do a Crop: TBGRABitmap Save Gif Not Working
« Reply #9 on: August 07, 2024, 03:30:24 pm »
Yes that was it.

Thanks Circular.

circular

  • Hero Member
  • *****
  • Posts: 4350
    • Personal webpage
Re: Make a Border Or Do a Crop: TBGRABitmap might be the solution
« Reply #10 on: August 07, 2024, 08:45:51 pm »
You're welcome.  :) Glad you got what you're looking for.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018