Page 1 of 1

Count used colors

Posted: Thu Jan 09, 2014 6:34 pm
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

Re: Count used colors

Posted: Fri Jan 10, 2014 6:25 pm
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

Re: Count used colors

Posted: Fri Jan 10, 2014 7:59 pm
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? :)

Re: Count used colors

Posted: Fri Jan 10, 2014 8:03 pm
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.

Re: Count used colors

Posted: Wed Jan 15, 2014 5:45 pm
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

Re: Count used colors

Posted: Wed Jan 15, 2014 6:24 pm
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.

Re: Count used colors

Posted: Wed Jan 15, 2014 9:45 pm
by davido
Another impressive demonstration!
Thank you for sharing. :D

Re: Count used colors

Posted: Thu Jan 16, 2014 10:37 am
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)

Re: Count used colors

Posted: Thu Jan 16, 2014 11:28 am
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 :)

Re: Count used colors

Posted: Thu Jan 16, 2014 11:53 am
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:

Re: Count used colors

Posted: Thu Jan 16, 2014 12:10 pm
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 ?

Re: Count used colors

Posted: Thu Jan 16, 2014 12:30 pm
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: