Recent

Author Topic: TBgraBitmap.Rectangle/FillRect bug?  (Read 1891 times)

hayanninja

  • New Member
  • *
  • Posts: 45
TBgraBitmap.Rectangle/FillRect bug?
« on: January 27, 2018, 07:56:33 am »
I'm guessing it's probably more I'm doing something wrong, but I'd like to confirm anyway.

In my project, there is a wrapper class around TBgraBitmaps. This is so that I can easily switch between different graphics libraries and/or use different ones on different platforms (for example, I already also have a - fully working - wrapper for Graphics32, too).

The following procedure is meant to give equivalent behaviour to (Graphics32's) TBitmap32.Clear - as far as I can tell TBgraBitmap doesn't have an equivalent procedure built in, so I did something that should give an equivalent result.

Code: [Select]
procedure <wrapper class>.DoClear(aColor: Cardinal);
begin
  <TBgraBitmap>.Rectangle(0, 0, Width, Height, aColor, aColor, dmSet);
  <TBgraBitmap>.SaveToFile(IntToHex(aColor, 8) + '.png');
end;


I've tried a few of the other overloads of Rectangle and FillRect too, but always get the same result. It appears that this works fine when dealing with solid colors, but goes haywire when non-255 alpha values become involved. In the above procedure, the second line is there solely for debugging purposes.

The output images, regardless of the value of aColor, always seem to be one of two results:
(ARGB) 00, 00, 00, 00; every pixel in the output PNG is 255, 0, 0, 0. (IE: Correct color, but wrong alpha value.)
Anything else; every pixel in the output PNG is 255, 200, 200, 200. (IE: Wrong color, wrong alpha value, and always the same color.)

Any pointers on where I might be going wrong here? I have verified that the actual value being passed for aColor is correct, although since the saved PNG filename is based off aColor's value anyway it'd be pretty apparent if the issue was there.
« Last Edit: January 27, 2018, 09:22:57 am by hayanninja »

hayanninja

  • New Member
  • *
  • Posts: 45
Re: TBgraBitmap.Rectangle/FillRect bug?
« Reply #1 on: January 27, 2018, 09:33:08 am »
Found the issue. It seems it needs to specifically have a typecast to TBgraPixel.

circular

  • Hero Member
  • *****
  • Posts: 4220
    • Personal webpage
Re: TBgraBitmap.Rectangle/FillRect bug?
« Reply #2 on: February 04, 2018, 10:49:35 pm »
Hello Hayanninja.

Indeed, if you supply an integer-like value, it will be interpreted as a TColor which does not have an alpha value.

The equivalent of TBitmap32.Clear is TBGRABitmap.Fill.

Note that doing a direct cast TBGRAPixel(aColor) is not garanteed to work because the byte order in TBGRAPixel may change depending on the platform and same thing for TColor.

A cross-platform way could be: RGB(aColor and $ff, (aColor shr 8 ) and $ff, (aColor shr 16) and $ff, aColor shr 24)

« Last Edit: April 28, 2018, 12:10:34 pm by circular »
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018