flat assembler
Message board for the users of flat assembler.

Index > Main > Count number of distinct colors in a buffer

Author
Thread Post new topic Reply to topic
Andy



Joined: 17 Oct 2011
Posts: 35
Andy
I want to count how many distinct colors I have in a bitmap. For this I have a pointer to Scan0 where the bitmap pixels are located, the number of pixels, a buffer to store all distinct colors found in the image and a buffer that will store how many times each color have been found. These last two buffers are the same size as the pixel data - assuming the worst case that each pixel in the image have a different color.

Code:
mov esi, [esp + 4]   ; Scan0 - pointer to the bitmap pixels
mov ecx, [esp + 8]   ; Number of pixels in buffer
mov edi, [esp + 12]   ; Pointer to a buffer that will store each distinct color
mov ebx, [esp + 16]   ; Pointer to a buffer that will store how many times each color has been found
xor edx, edx   ; EDX - will be used to keep tracking the offset (how many distinct colors have been found so far)

NextPixel:
lodsd
push ecx
mov ecx, edx

NextCheck:
cmp ecx, 0
je AddColor
cmp eax, [edi + ecx * 4]
je UpdateIndex
loop NextCheck

AddColor:
mov dword[edi + edx * 4], eax
mov dword[ebx + edx * 4], 1
inc edx
pop ecx
loop NextPixel
jmp Exit

UpdateIndex:
add dword[ebx + ecx], 1
pop ecx
jmp NextPixel

Exit:
ret 16    


Well, I wrote this code but it's not working and sometimes even crash. Any thoughts what I am doing wrong?
Post 27 May 2022, 14:55
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 541
Location: Russia
macomics
Copy all the colors from the Bitmap into a linear array, sort it and count the number of colors that do not match the previous one in the array.

You can not copy if bitmap is not a pity.


Last edited by macomics on 27 May 2022, 15:01; edited 1 time in total
Post 27 May 2022, 15:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18592
Location: In your JS exploiting you and your system
revolution
I think a simple sort would work better than building a buffer one by one.

Your algorithm is O(N^2). A sort would be O(N log N).
Post 27 May 2022, 15:01
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 541
Location: Russia
macomics
revolution wrote:
Your algorithm is O(N^2). A sort would be O(N log N).
In fact, he got bubble sorting at its worst
Post 27 May 2022, 15:08
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.