Recent

Author Topic: Broken example "Creating and drawing a transparent bitmap for a TImage" in Linux  (Read 3878 times)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Many years ago, I've written a little program to look inside the bitmaps header, where the data are ok
This is how I would fetch amazing fast the needed info:
This demonstration does not do any kind of checking, it just try to do its job done by assuming all is cool.
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$IfDef Windows}
  4. {$AppType Console}
  5. {$EndIf Windows}
  6.  
  7. uses
  8.   SysUtils, Classes, BMPcomn;
  9.  
  10. type
  11.   TFullBitmapHeader = packed record
  12.     BitMapFileHeader: TBitMapFileHeader;
  13.     BitMapInfoHeader: TBitMapInfoHeader;
  14.   end;
  15.  
  16. function GetBitmapHeader(const AFilename: string): TFullBitmapHeader;
  17. var
  18.   FileStream: TFileStream;
  19. begin
  20.   FileStream := TFileStream.Create(AFilename, fmOpenRead);
  21.   try
  22.     FillChar(Result, SizeOf(Result), 0);
  23.     FileStream.Position := 0;
  24.     FileStream.Read(Result.BitMapFileHeader.bfType, SizeOf(Result.BitMapFileHeader.bfType));
  25.     FileStream.Read(Result.BitMapFileHeader.bfSize, SizeOf(Result.BitMapFileHeader.bfSize));
  26.     FileStream.Read(Result.BitMapFileHeader.bfReserved, SizeOf(Result.BitMapFileHeader.bfReserved));
  27.     FileStream.Read(Result.BitMapFileHeader.bfOffset, SizeOf(Result.BitMapFileHeader.bfOffset));
  28.     FileStream.Read(Result.BitMapInfoHeader.Size, SizeOf(Result.BitMapInfoHeader.Size));
  29.     FileStream.Read(Result.BitMapInfoHeader.Width, SizeOf(Result.BitMapInfoHeader.Width));
  30.     FileStream.Read(Result.BitMapInfoHeader.Height, SizeOf(Result.BitMapInfoHeader.Height));
  31.     FileStream.Read(Result.BitMapInfoHeader.Planes, SizeOf(Result.BitMapInfoHeader.Planes));
  32.     FileStream.Read(Result.BitMapInfoHeader.BitCount, SizeOf(Result.BitMapInfoHeader.BitCount));
  33.     FileStream.Read(Result.BitMapInfoHeader.Compression, SizeOf(Result.BitMapInfoHeader.Compression));
  34.     FileStream.Read(Result.BitMapInfoHeader.SizeImage, SizeOf(Result.BitMapInfoHeader.SizeImage));
  35.     FileStream.Read(Result.BitMapInfoHeader.XPelsPerMeter, SizeOf(Result.BitMapInfoHeader.XPelsPerMeter));
  36.     FileStream.Read(Result.BitMapInfoHeader.YPelsPerMeter, SizeOf(Result.BitMapInfoHeader.YPelsPerMeter));
  37.     FileStream.Read(Result.BitMapInfoHeader.ClrUsed, SizeOf(Result.BitMapInfoHeader.ClrUsed));
  38.     FileStream.Read(Result.BitMapInfoHeader.ClrImportant, SizeOf(Result.BitMapInfoHeader.ClrImportant));
  39.   finally
  40.     FileStream.Free;
  41.   end;
  42. end;
  43.  
  44. var
  45.   BitmapHeader: TFullBitmapHeader;
  46. begin
  47.   WriteLn('Quick Bitmap Info by KodeZwerg.');
  48.   if FileExists(ParamStr(1)) then
  49.     begin
  50.       BitmapHeader := GetBitmapHeader(ParamStr(1));
  51.       WriteLn('Type: ' + IntToHex(Swap(BitmapHeader.BitMapFileHeader.bfType), SizeOf(BitmapHeader.BitMapFileHeader.bfType)));
  52.       WriteLn('Filesize: ' + IntToStr(BitmapHeader.BitMapFileHeader.bfSize));
  53.       WriteLn('Reserved: ' + IntToHex(Swap(BitmapHeader.BitMapFileHeader.bfReserved), SizeOf(BitmapHeader.BitMapFileHeader.bfReserved)));
  54.       WriteLn('Offset: ' + IntToStr(BitmapHeader.BitMapFileHeader.bfOffset));
  55.       WriteLn('Structure Size: ' + IntToStr(BitmapHeader.BitMapInfoHeader.Size));
  56.       WriteLn('Width: ' + IntToStr(BitmapHeader.BitMapInfoHeader.Width));
  57.       WriteLn('Height: ' + IntToStr(BitmapHeader.BitMapInfoHeader.Height));
  58.       WriteLn('Planes: ' + IntToStr(BitmapHeader.BitMapInfoHeader.Planes));
  59.       WriteLn('Bits Per Pixel: ' + IntToStr(BitmapHeader.BitMapInfoHeader.BitCount));
  60.       WriteLn('Compression Method: ' + IntToStr(BitmapHeader.BitMapInfoHeader.Compression));
  61.       WriteLn('Image Size: ' + IntToStr(BitmapHeader.BitMapInfoHeader.SizeImage));
  62.       WriteLn('Horizontal Resolution: ' + IntToStr(BitmapHeader.BitMapInfoHeader.XPelsPerMeter));
  63.       WriteLn('Vertical Resolution: ' + IntToStr(BitmapHeader.BitMapInfoHeader.YPelsPerMeter));
  64.       WriteLn('Colors Used: ' + IntToStr(BitmapHeader.BitMapInfoHeader.ClrUsed));
  65.       WriteLn('Colors Important: ' + IntToStr(BitmapHeader.BitMapInfoHeader.ClrImportant));
  66.     end
  67.   else
  68.     WriteLn('Usage: ' + ExtractFileName(ParamStr(0)) + ' "image.bmp"');
  69.   WriteLn('');
  70.   WriteLn('Done.');
  71.   {$IfDef Windows}
  72.   ReadLn;
  73.   {$EndIf Windows}
  74. end.
« Last Edit: May 28, 2024, 02:49:37 am by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

jipété

  • Full Member
  • ***
  • Posts: 172
Good evening,

One month later, I come back here because nothing is moving forward...

A few minute before, I started with  the two files kindly provided by me on May 27 (src32.bmp) and by KodeZwerg on May 27 also (test.zip), the first one has a BITMAPHEADER version 4 when the second has version 1, and I always used this code from the wiki :
TLazIntfImage.LoadFromFile https://wiki.freepascal.org/Developing_with_Graphics#TLazIntfImage.LoadFromFile

Quote
Here is an example how to load an image directly into a TLazIntfImage. It initializes the TLazIntfImage to a 32bit RGBA format. Keep in mind that this is probably not the native format of your screen.
Code: Pascal  [Select][+][-]
  1. uses LazLogger, Graphics, IntfGraphics, GraphType;
  2. procedure TForm1.FormCreate(Sender: TObject);
  3. var
  4.   AImage: TLazIntfImage;
  5.   lRawImage: TRawImage;
  6. begin
  7.   // create a TLazIntfImage with 32 bits per pixel, alpha 8bit, red 8 bit, green 8bit, blue 8bit,
  8.   // Bits In Order: bit 0 is pixel 0, Top To Bottom: line 0 is top
  9.   lRawImage.Init;
  10.   lRawImage.Description.Init_BPP32_A8R8G8B8_BIO_TTB(0,0);
  11.   lRawImage.CreateData(false);
  12.   AImage := TLazIntfImage.Create(0,0);
  13.   try
  14.     AImage.SetRawImage(lRawImage);
  15.     // Load an image from disk.
  16.     // It uses the file extension to select the right registered image reader.
  17.     // The AImage will be resized to the width, height of the loaded image.
  18. //    AImage.LoadFromFile('lazarus/examples/openglcontrol/data/texture1.png');
  19.     AImage.LoadFromFile(openpicturedialog1.FileName);
  20.     debugln(['TForm1.FormCreate ',AImage.Width,' ',AImage.Height]);
  21.   finally
  22.     AImage.Free;
  23.   end;
  24. end;
and just before the finally line I added the command below after changing the last word :

Quote
Loading a TLazIntfImage into a TImage

The pixel data of a TImage is the TImage.Picture property, which is of type TPicture. TPicture is a multi format container containing one of several common image formats like Bitmap, Icon, Jpeg or PNG . Usually you will use the TPicture.Bitmap to load a TLazIntfImage:
Code: Pascal  [Select][+][-]
  1.     Image1.Picture.Bitmap.LoadFromIntfImage(IntfImg);
becomes
Code: Pascal  [Select][+][-]
  1.     Image1.Picture.Bitmap.LoadFromIntfImage(AImage);

Result, it only works with the v1 file, with v4 nothing is displayed while the preview image in the OpenPictureDialog is fine...  :o
« Last Edit: June 27, 2024, 07:59:08 pm by jipété »

 

TinyPortal © 2005-2018