Recent

Author Topic: PV_Bitmap - a new library to load/save images, TBitmap compatible  (Read 12944 times)

domasz

  • Sr. Member
  • ****
  • Posts: 450
Here's the first version of my small library for Lazarus to load/save various image formats.
I ported, cleaned and improved my old Delphi code here.

This it not a replacement for the great BGRABitmap. This is not an image processing library, just converter.
The goal of the library: make it easy to add new formats, not speed. The speed is fine.
Designed for Windows, should be fairy easy to support Linux. I will just need some info from Linux guys.
It uses a few FPRead*** and FPWrite*** units.

Supported formats, reading:
hrz, nol, nlm, 411, aai, 92i, pat, cel, gr9, ghg, bg9, avs, kro, pic, sgi, ras, wbmp, bmp, 2bp, pcx, jpg, tga, tif, png, pnm, pgm, pbm, pfm, gif, psd

Writing:
hrz, ras, bmp, pcx, jpg, tga, tig, png, ppm, pgm, pbm, pfm, gif, psd

The goal: support for 100 formats.

Usage:
Code: Pascal  [Select][+][-]
  1. uses PV_Bitmap, PV_BitmapFormats
  2.  
  3. var Bmp: TPV_Bitmap;
  4. begin
  5.   Bmp := TPV_Bitmap.Create;
  6.   Bmp.LoadFromFile('1.hrz');
  7.   Bmp.SaveToFile('2.pfm');
  8.   Bmp.Free;

Using TBitmap:

Code: Pascal  [Select][+][-]
  1. Bmp := TPV_Bitmap.Create;
  2. Bmp.FBitmap.Canvas //

Accessing pixels directly:
Code: Pascal  [Select][+][-]
  1. Pix: TPix;
  2. R,G,B: Byte;
  3.  
  4. Pix := Bmp.Pixel[x,y];
  5. Pix.R := 255;
  6. Bmp.Pixel[x,y] := Pix;
  7.  
  8. R := Bmp.Pixel[x,y].R;
  9.  
  10. Bmp.SetRGB(x,y, 128, 0, 0);
  11. Bmp.SetRGBA(x,y, 128,0,0, 128);
  12. Bmp.SetMono(x,y, 1);
  13.  

Included are also 2 classes TPV_Writer and TPV_Reader for a nice reading of data from TStreams.
Both are buffered.
TPV_Reader reads whole stream into memory for faster processing. Images usually are under 50 MB so that should never be a problem.

Usage:
Code: Pascal  [Select][+][-]
  1.   Str: TFileStream;
  2.   Reader: TPV_Reader;
  3. begin
  4.   Reader := TPV_Reader.Create(Str);
  5.  
  6. //all reading is counted in Bytes
  7.   Reader.GetU;         //read Unsigned Integer, 1 Byte long (Uint8)
  8.   Reader.GetU4;       //read Unsigned Integer, 4 Byte long (Uint32)
  9.  
  10.   Reader.GetI;         //read Integer, 1 Byte long (Int8)
  11.   Reader.GetI4;       //read Integer, 4 Byte long (Int32)
  12.  
  13.   Reader.GetMU;     //read Unsigned Integer, 1 Byte long, Motorola byte order (Big endian)
  14.   Reader.GetMU4;   //read Unsigned Integer, 4 Byte long, Motorola byte order (Big endian)
  15.  
  16.   Reader.GetMIU;     //read Integer, 1 Byte long, Motorola byte order (Big endian)
  17.   Reader.GetMIU4;   //read Integer, 4 Byte long, Motorola byte order (Big endian)
  18.  
  19.   Reader.GetS(4);   //read String, 4 Byte long
  20.  
  21.   Reader.GetWhite; //read whitespaces, as many as there are
  22.   Reader.GetNum;  //read numeric string. Currently only positive Integers are read, in decimal notation.
  23.  
  24.   Reader.GetF;      //read Float, 4 Byte long (aka Single)
  25.  
  26.   Reader.AtLeast := 100; //make sure there are at least 100 bytes to read. Let's say your image should be 2 MB long but is truncated. Normally you would need to detect premature end of data and cancel reading. Here instead you virtually add bytes to the data. It won't recover missing data but will make it easy to display partially decoded images.
  27.  
« Last Edit: April 09, 2024, 10:00:50 am by domasz »

AlexTP

  • Hero Member
  • *****
  • Posts: 2417
    • UVviewsoft
Re: PV_Bitmap - a new library to load/save images, TBitmap compatible
« Reply #1 on: April 03, 2024, 11:26:34 am »
Nice effort.

If you host the code on GitHub, it will be easier to update. No need to re-upload zips.

domasz

  • Sr. Member
  • ****
  • Posts: 450
Re: PV_Bitmap - a new library to load/save images, TBitmap compatible
« Reply #2 on: April 03, 2024, 11:31:09 am »
Thanks. I will at one point. They changed how you log in to Github and I don't like it.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11524
  • FPC developer.
Re: PV_Bitmap - a new library to load/save images, TBitmap compatible
« Reply #3 on: April 03, 2024, 01:39:22 pm »
Note that such bigendian and littleendian helpers are already provided for if you include unit streamex:

https://www.freepascal.org/docs-html/current/fcl/streamex/tstreamhelper.html

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2241
  • Fifty shades of code.
    • Delphi & FreePascal
Re: PV_Bitmap - a new library to load/save images, TBitmap compatible
« Reply #4 on: April 04, 2024, 12:50:26 am »
Here's the first version of my small library for Lazarus to load/save various image formats.
How nice of you sharing it!  :-*

It uses a few FPRead*** and FPWrite*** units.
The linked file only include "fpwritegif.pas", is that correct?

Supported formats, reading:
... gif, psd

Writing:
... gif, psd
I have not downloaded yet your library to test out its features, since GIF and PSD are one of my interests I would like to ask for an easy example.
- Create, save, load an animated GIF  O:-)
- Create, save, load an alpha-layered PSD please  O:-)
And my next question, how-to format convert with your library, eg. input is jpeg and output shall be png
- Load any JPEG file
- Rescale to 100x200 to see how its implemented, maybe you have also some "Keep Aspect Ratio" logic optional included?
- Save as PNG, to see how format specific options are set like for PNG the compression algo/level

Maybe you have included some demos that answer my questions, downloading now and start testing  :-*

I am much interested in your project and saying again a big thank you for sharing your library!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

domasz

  • Sr. Member
  • ****
  • Posts: 450
Re: PV_Bitmap - a new library to load/save images, TBitmap compatible
« Reply #5 on: April 04, 2024, 01:41:59 am »
I have not downloaded yet your library to test out its features, since GIF and PSD are one of my interests I would like to ask for an easy example.
- Create, save, load an animated GIF  O:-)
- Create, save, load an alpha-layered PSD please  O:-)
Sorry to disappoint but right now it supports only static images without layers. So you will get only the main image of the PSD and the first frame of GIF.

And my next question, how-to format convert with your library, eg. input is jpeg and output shall be png
- Load any JPEG file
- Rescale to 100x200 to see how its implemented, maybe you have also some "Keep Aspect Ratio" logic optional included?
- Save as PNG, to see how format specific options are set like for PNG the compression algo/level

Code: Pascal  [Select][+][-]
  1. var Bmp: TPV_Bitmap;
  2. begin
  3. Bmp := TPV_Bitmap.Create;
  4. Bmp.LoadFromFile('test.jpg');
  5. Bmp.Resize(100,200);
  6. Bmp.SaveToFile('test.png', 6); //6 is the compression level
  7. Bmp.Free;
  8.  

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2241
  • Fifty shades of code.
    • Delphi & FreePascal
Re: PV_Bitmap - a new library to load/save images, TBitmap compatible
« Reply #6 on: April 04, 2024, 02:12:13 am »
Thank you for quick reply and small demo about usage, thats very helpful!

No worries about unsupported GIF/PSD features, maybe one day its integrated hehe

Another question I like to ask is about metatags, are they supported, will they be lost, can I modify etc.

And since I've asked earlier how-to convert, does your library support to change BPP, like input has 32bpp while output should be 16bpp.
 - Does library have quantizer integrated to generate a shrinked palette?
 - Does library have a (simplified) color to greyscale mode?
 - Does library support generation of thumbnails/previews? (beside the manual resize that you absolute good explained!)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

domasz

  • Sr. Member
  • ****
  • Posts: 450
Re: PV_Bitmap - a new library to load/save images, TBitmap compatible
« Reply #7 on: April 04, 2024, 10:55:32 pm »
Metatags - not yet, will be added
Quantizer - only when saving to Gif, will be added
Yes, there is normal grayscale and simplified (R+G+B/3)
How would you like it to generate thumbnails?

domasz

  • Sr. Member
  • ****
  • Posts: 450
Re: PV_Bitmap - a new library to load/save images, TBitmap compatible
« Reply #8 on: April 09, 2024, 10:02:07 am »
UPDATE
- ZIP with code in the first post
- more formats - about 85 now
- color reduction with dithering (Floyd-Steinberg, Stucki, Atkinson...)
- grayscale with dithering
- mono with dithering
« Last Edit: April 09, 2024, 11:26:26 am by domasz »

 

TinyPortal © 2005-2018