Recent

Author Topic: TImage colors incorrect  (Read 3341 times)

Jonny

  • Full Member
  • ***
  • Posts: 104
TImage colors incorrect
« on: January 14, 2025, 04:48:10 pm »
Create an empty form with this code in FormCreate:

Code: Pascal  [Select][+][-]
  1. var
  2.   aImage: TImage;
  3. procedure TfrmMain.FormCreate(Sender: TObject);
  4. var
  5.    aBitmap: TBitmap;
  6. begin
  7.   aImage := TImage.Create(frmMain);
  8.   aImage.Parent := frmMain;
  9.   aImage.Width := 500;
  10.   aImage.Height := 500;
  11.   aBitmap := TBitmap.Create;
  12.   aBitmap.Width := 500;
  13.   aBitmap.Height := 500;
  14.   aBitmap.Canvas.Brush.Color := clRed;
  15.   aBitmap.Canvas.FillRect(0,0,500,500);
  16.   aBitmap.Canvas.Brush.Color := clBlue;
  17.   aBitmap.Canvas.FillRect(150,150,350,350);
  18.   aImage.Canvas.Draw(0,0,aBitmap);
  19.   aBitmap.SaveToFile('/tmp/test.bmp');
  20. end;
  21.  

Expected result:
  TImage on form = red background with blue box
  Saved bitmap file  = red background with blue box

Actual result:
  TImage on form = blue background with red box ** !!! **
  Saved bitmap file  = red background with blue box

See attachments.

Why is the image on the form incorrect?

wp

  • Hero Member
  • *****
  • Posts: 12597
Re: TImage colors incorrect
« Reply #1 on: January 14, 2025, 05:58:45 pm »
Your Lazarus/FPC version? ("Help" > "About Lazarus")? IIRC, there were old versions in which red and blue were exchanged.

Jonny

  • Full Member
  • ***
  • Posts: 104
Re: TImage colors incorrect
« Reply #2 on: January 14, 2025, 06:46:27 pm »
Code: [Select]
$ ./fpc -iW
3.3.1-17259-g904c25745c-dirty

Lazarus 4.99 (rev main_4_99-813-g9eb9286e45) FPC 3.3.1 x86_64-linux-qt5

Is there a way to check affected versions?

wp

  • Hero Member
  • *****
  • Posts: 12597
Re: TImage colors incorrect
« Reply #3 on: January 14, 2025, 07:43:27 pm »
And which Linux distribution is this?

Jonny

  • Full Member
  • ***
  • Posts: 104
Re: TImage colors incorrect
« Reply #4 on: January 14, 2025, 11:27:29 pm »
MX Linux 23 - based on Debian 12 - built GTK2 and QT5 x86-64 executables and both have the same issue.
« Last Edit: January 14, 2025, 11:29:05 pm by Jonny »

Jonny

  • Full Member
  • ***
  • Posts: 104
Re: TImage colors incorrect
« Reply #5 on: January 15, 2025, 11:45:34 am »
Just updated to latest trunk. The issue persists. Is it my code or an ongoing error in trunk? Should I drop down to last stable or fixes branch?

jamie

  • Hero Member
  • *****
  • Posts: 6801
Re: TImage colors incorrect
« Reply #6 on: January 15, 2025, 01:58:08 pm »
Try setting the pixel format of the bitmap to 32bits before setting the width and height of it.
The only true wisdom is knowing you know nothing

bytebites

  • Hero Member
  • *****
  • Posts: 698
Re: TImage colors incorrect
« Reply #7 on: January 15, 2025, 02:07:39 pm »
Unable to reproduce. (Linux Mint).
« Last Edit: January 15, 2025, 02:34:16 pm by bytebites »

Jonny

  • Full Member
  • ***
  • Posts: 104
Re: TImage colors incorrect
« Reply #8 on: January 15, 2025, 02:21:13 pm »
Quote from: jamie
Try setting the pixel format of the bitmap to 32bits before setting the width and height of it.

Code: Pascal  [Select][+][-]
  1. var
  2.   aImage: TImage;
  3. procedure TfrmMain.btnDrawClick(Sender: TObject);
  4. var
  5.    aBitmap: TBitmap;
  6. begin
  7.   aImage := TImage.Create(frmMain);
  8.   aImage.Parent := frmMain;
  9.   aImage.Width := 500;
  10.   aImage.Height := 500;
  11.   aBitmap := TBitmap.Create;
  12.   aBitmap.PixelFormat := pf32bit;
  13.   aBitmap.Width := 500;
  14.   aBitmap.Height := 500;
  15.   aBitmap.Canvas.Brush.Color := clRed;
  16.   aBitmap.Canvas.FillRect(0,0,500,500);
  17.   aBitmap.Canvas.Brush.Color := clBlue;
  18.   aBitmap.Canvas.FillRect(150,150,350,350);
  19.   aImage.Canvas.Draw(0,0,aBitmap);
  20.   aBitmap.SaveToFile('/tmp/test.bmp');
  21. end;
  22.  

Thanks, but no change, the TBitmap is always correct, it is the colors on TImage that are still inverted.

Khrys

  • Full Member
  • ***
  • Posts: 148
Re: TImage colors incorrect
« Reply #9 on: January 15, 2025, 02:30:02 pm »
Does the output of the following two lines (the raw image descriptions) differ?

Code: Pascal  [Select][+][-]
  1. WriteLn(aImage.Picture.Bitmap.RawImage.Description.AsString());
  2. WriteLn(aBitmap.RawImage.Description.AsString());

From the screenshots it seems like the channel order is being swapped somewhere (RGB / BGR - red is interpreted as blue and vice versa). Try drawing a green  (clFuchsia)  square, it should stay green in both versions if this is the culprit.

Jonny

  • Full Member
  • ***
  • Posts: 104
Re: TImage colors incorrect
« Reply #10 on: January 15, 2025, 02:40:43 pm »
Code: [Select]
Format=ricfRGBA HasPalette->False HasMask->False Depth=24 Width=500 Height=500 BitOrder=riboReversedBits ByteOrder=riboLSBFirst LineOrder=riloTopToBottom LineEnd=rileDWordBoundary BitsPerPixel=32 BytesPerLine->2000 RedPrec=8 RedShift=16 GreenPrec=8 GreenShift=8 BluePrec=8 BlueShift=0 AlphaPrec=0 AlphaShift=0 ~~~mask~~~ MaskBitsPerPixel=1 MaskShift=0 MaskLineEnd=rileDWordBoundary MaskBitOrder=riboReversedBits MaskBytesPerLine->64 ~~~palette~~~ PaletteColorCount=0 PaletteBitsPerIndex=0 PaletteShift=0 PaletteLineEnd=rileTight PaletteBitOrder=riboBitsInOrder PaletteByteOrder=riboLSBFirst PaletteBytesPerLine->0

Format=ricfRGBA HasPalette->False HasMask->False Depth=32 Width=500 Height=500 BitOrder=riboReversedBits ByteOrder=riboLSBFirst LineOrder=riloTopToBottom LineEnd=rileDWordBoundary BitsPerPixel=32 BytesPerLine->2000 RedPrec=8 RedShift=16 GreenPrec=8 GreenShift=8 BluePrec=8 BlueShift=0 AlphaPrec=8 AlphaShift=24 ~~~mask~~~ MaskBitsPerPixel=1 MaskShift=0 MaskLineEnd=rileDWordBoundary MaskBitOrder=riboReversedBits MaskBytesPerLine->64 ~~~palette~~~ PaletteColorCount=0 PaletteBitsPerIndex=0 PaletteShift=0 PaletteLineEnd=rileTight PaletteBitOrder=riboBitsInOrder PaletteByteOrder=riboLSBFirst PaletteBytesPerLine->0

The differences are:

Code: [Select]
Depth=24
Depth=32

AlphaPrec=0 AlphaShift=0
AlphaPrec=8 AlphaShift=24

All green drawing is fine, it is indeed RGB vs BGR. Very strange, everything else on my display is fine. Just files that I compile with FPC.

lainz

  • Hero Member
  • *****
  • Posts: 4686
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: TImage colors incorrect
« Reply #11 on: January 15, 2025, 03:13:20 pm »
Please try with BGRABitmap, and BGRAControls if you can...

To know if that bug affect these packages as well...

Thanks

Jonny

  • Full Member
  • ***
  • Posts: 104
Re: TImage colors incorrect
« Reply #12 on: January 15, 2025, 03:26:29 pm »
Quote from: lainz
Please try with BGRABitmap, and BGRAControls if you can...

Hmm, are these external packages? I cannot find them in the normal FPC/Lazarus installation.

lainz

  • Hero Member
  • *****
  • Posts: 4686
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: TImage colors incorrect
« Reply #13 on: January 15, 2025, 03:40:08 pm »
Quote from: lainz
Please try with BGRABitmap, and BGRAControls if you can...

Hmm, are these external packages? I cannot find them in the normal FPC/Lazarus installation.

Yes these are external. Try with OPM (online package manager).

Basically to test install both packages and drop a TBCButton for example and change the color of normal state In the object inspector..
« Last Edit: January 15, 2025, 03:42:09 pm by lainz »

Jonny

  • Full Member
  • ***
  • Posts: 104
Re: TImage colors incorrect
« Reply #14 on: January 15, 2025, 04:16:44 pm »
Same issue with TBGRABitmap - blue and red reversed on screen, exporting bitmap image is fine.

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.btnDrawClick(Sender: TObject);
  2. var
  3.    aBitmap: TBGRABitmap;
  4. begin
  5.   aImage := TImage.Create(frmMain);
  6.   aImage.Parent := frmMain;
  7.   aImage.Width := 500;
  8.   aImage.Height := 500;
  9.   aBitmap := TBGRABitmap.Create;
  10.   aBitmap.SetSize(500,500);
  11.   aBitmap.Canvas.Brush.Color := clRed;
  12.   aBitmap.Canvas.FillRect(0,0,500,500);
  13.   aBitmap.Canvas.Brush.Color := clBlue;
  14.   aBitmap.Canvas.FillRect(150,150,350,350);
  15.   aImage.Picture.Bitmap.Assign(aBitmap);
  16.   aBitmap.SaveToFile('/tmp/test.bmp');
  17. end;
  18.  

 

TinyPortal © 2005-2018