Count used colors

Share your advanced PureBasic knowledge/code with the community.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Count used colors

Post by wilbert »

Two simple procedures that maybe someone could use.

CountColors(image.i) - Counts the amount of colors the image contains (alpha channel is ignored).

HasOverMaxColors(image.i, max = 256) - Returns #True when the image contains more than max colors.

Code: Select all

Procedure CountColors(image.i); count colors (24 bit)
  Protected.i x, y, max_x, max_y, c, count
  Dim m.a(2097151)
  StartDrawing(ImageOutput(image))
  max_x = ImageWidth(image) - 1
  max_y = ImageHeight(image) - 1
  For y = 0 To max_y
    For x = 0 To max_x
      c = Point(x, y) & $ffffff
      If m(c >> 3) & 1 << (c & 7) = 0
       m(c >> 3) | 1 << (c & 7)
       count + 1
      EndIf
    Next
  Next
  StopDrawing()
  ProcedureReturn count
EndProcedure

Procedure.i HasOverMaxColors(image.i, max = 256); checks color limit (24 bit)
  Protected.i x, y, max_x, max_y, c, count
  Dim m.a(2097151)
  StartDrawing(ImageOutput(image))
  max_x = ImageWidth(image) - 1
  max_y = ImageHeight(image) - 1
  For y = 0 To max_y
    For x = 0 To max_x
      c = Point(x, y) & $ffffff
      If m(c >> 3) & 1 << (c & 7) = 0
       m(c >> 3) | 1 << (c & 7)
       count + 1
       If count > max
         StopDrawing()
         ProcedureReturn #True
       EndIf
      EndIf
    Next
  Next
  StopDrawing()
  ProcedureReturn #False
EndProcedure
Last edited by wilbert on Thu Jan 16, 2014 11:27 am, edited 2 times in total.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Count used colors

Post by BasicallyPure »

Thanks Wilbert,
I tested your count colors procedure and it works great.
It is useful to me for something I am doing now.
I never imagined it could be done with so little code.

BP
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Count used colors

Post by Little John »

Hi wilbert!
wilbert wrote:

Code: Select all

max_x = ImageWidth(0) - 1
max_y = ImageHeight(0) - 1
It should be

Code: Select all

max_x = ImageWidth(image) - 1
max_y = ImageHeight(image) - 1
instead, no? :)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Count used colors

Post by wilbert »

BasicallyPure wrote:I never imagined it could be done with so little code.
Me neither initially :shock: :)
I came across a post on stackoverflow that suggested to use a bit array and that worked great.
Indeed only a few lines of code and pretty fast.
Little John wrote:It should be

Code: Select all

max_x = ImageWidth(image) - 1
max_y = ImageHeight(image) - 1
instead, no? :)
You are absolutely right.
I corrected my first post. Thanks for mentioning the problem.
Windows (x64)
Raspberry Pi OS (Arm64)
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: Count used colors

Post by Poshu »

frediuncle wrote:Until you know everything you know nothing, all you have is what you believe.
This might have been the weirdest first post ever °_°;

Pretty nice code here Wilbert, thanks
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Count used colors

Post by netmaestro »

This might have been the weirdest first post ever °_°;
Not so weird, just a bot :wink:

Wilbert, you keep right on searching stackoverflow for ideas, it's working out very well for you! (and us too) Thanks for all your valuable contributions here, they're much appreciated.
BERESHEIT
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Count used colors

Post by davido »

Another impressive demonstration!
Thank you for sharing. :D
DE AA EB
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Count used colors

Post by Kwai chang caine »

Works great 8)
Perhaps if i can allow me, add the StopDrawing ?? :oops:
Because when i have used two time your splendid procedure, i have an error

Code: Select all

If count > max
 StopDrawing()
 ProcedureReturn #True
EndIf
Thanks a lot for sharing 8)
ImageThe happiness is a road...
Not a destination
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Count used colors

Post by wilbert »

Kwaï chang caïne wrote:Works great 8)
Perhaps if i can allow me, add the StopDrawing ?? :oops:
Because when i have used two time your splendid procedure, i have an error
Thanks for mentioning the problem.
I updated the code in the first post :)
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Count used colors

Post by Kwai chang caine »

It's me to thanks again you 8)

Is it possible to know the number of Red, blue, Green of the picture ?
Because i have using your code on two differents pictures and the result is nearly the same :cry:
ImageThe happiness is a road...
Not a destination
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Count used colors

Post by wilbert »

Kwaï chang caïne wrote:Is it possible to know the number of Red, blue, Green of the picture ?
Wouldn't that be a histogram like BasicallyPure posted ?
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Count used colors

Post by Kwai chang caine »

Yes perhaps...in fact i have not understand all in this subject (in numerous other two) .. :oops:
I have see this nice works...it's very nice effect...like with audio sound 8)
But like i'm a donkey...i have not understand what is the goal to make nice graphical like that :oops: :oops:
Because for comparing two picture for example, i think it's better to have numbers no ???
But again one time..the advice of KCC about programming is like a fart in a toilet...it's not really useful :oops:
ImageThe happiness is a road...
Not a destination
Post Reply