Count unique colors used in an image using GDIPlus [Windows only]

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Count unique colors used in an image using GDIPlus [Windows only]

Post by MrSwiss »

Counting_pine's code is far simpler, than the Bit(var, pos), since he *knows* the definition,
of the variable a (before the evaluation), which Bit() has to *figure out* first:

Code: Select all

#define Bit( value, bit_number ) (((value) and (Cast(TypeOf(value), 1) shl (bit_number))) <> 0)
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Count unique colors used in an image using GDIPlus [Windows only]

Post by fxm »

I do not see the interest of an explicit casting in this kind of macro as Bit, BitSet, BitReset.
The implicit compiler casting is sufficient.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Count unique colors used in an image using GDIPlus [Windows only]

Post by MrSwiss »

fxm wrote:I do not see the interest of an explicit casting in this kind of macro as Bit, BitSet, BitReset.
Value could be a U/Byte / U/Short ... The explicit cast prevents n, being larger than allowed, by type-of-var's size,
(in bit's - 1) and, therefore needed, to work properly with all sizes of integers.
Nothing to do, with below:
fxm wrote:The implicit compiler casting is sufficient.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Count unique colors used in an image using GDIPlus [Windows only]

Post by fxm »

Post an example where explicit casting is required.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Count unique colors used in an image using GDIPlus [Windows only]

Post by MrSwiss »

Take the casting + the And out of the Macro, and see what happens, using Byte's/Short's for tests ...

Then:
with FBC 32-bit, use a U/LongInt (where Integer is bound, to fail, without casting).

Resulting in: warning 33(0): Shift value greater than or equal to number of bits in data type
With original Macro: no problem at all ..., test was:

Code: Select all

#Undef Bit
#Define Bit(value, bit_number)    ( ( (value) and (1 shl (bit_number)) ) <> 0 )

Dim As Byte     b = &h80
Dim As Short    s = &h7FFF
Dim As ULongInt u = &h800FFFFFFFFFF000

? Bit(b, 8), Bit(s, 15), Bit(u, 37)    ' test on u = 0 (wrong result, bit is set)

Sleep
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Count unique colors used in an image using GDIPlus [Windows only]

Post by fxm »

Yes, you are right for Win32 (no problem for Win64).
Post Reply