Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# how to grayscale the24 bit bitmap

Posted on 2003-03-13
Medium Priority
308 Views
Hello experts,
I have 1 question.
I have displayed 24 bit bitmap on client area.
now I have to convert it grayscale image how I do?
because there is no palette for 24 bit as know
how to do it.
please anybody can give me some solution for this.
thank you
0
Question by:image_nation

LVL 12

Accepted Solution

williamcampbell earned 100 total points
ID: 8129500

image

http://codeguru.earthweb.com/bitmap/grayscale.shtml
0

Expert Comment

ID: 8135985

Get buffer with image data.
For example:

void *ImageData;

In this data are pixels stored in BGR format. Memory can see as "BGRBGRBGRBGRBGRBGR....", all you need to do
is read every byte and calculate their average value:

unsigned char r,g,b, back;
__int16 temp;

for (i=0;i<end of data;i+=3)
{
// first block
memcpy(&b, (void *)((DWORD)ImageData),1);
memcpy(&g, (void *)((DWORD)ImageData + 1),1);
memcpy(&r, (void *)((DWORD)ImageData + 1),1);

// second block
temp = (r + g + b) / 3;
back = (unsigned char) temp;

// third block
memcpy((void *)((DWORD)ImageData),&b,1);
memcpy((void *)((DWORD)ImageData + 1),&g,1);
memcpy((void *)((DWORD)ImageData + 1),&r,1);
}

First block read RGB of a pixel. (see BGR direction)
Second is for calculate average value and store again
into back ( unsigned int )
Third is for putting pixel back to memory

BUT: !!!
Now I'm tired even I don't know if there are "&r" or "r" in memory functions

Don't forget, that one scanline must be flush for 4 byte blocks.
0

Author Comment

ID: 8141288
No friends..
You haven't tell me Grayscaling of 24 bit BMP.
You told @ 8bit BMP thats easy.
plz send ur views @ it
0

Expert Comment

ID: 8143815
Yes he have, because one char has 8bit and 3*8 = 24bit !!
0

LVL 1

Expert Comment

ID: 8162620
You can't do a 24 bit grayscale. There is only an 8-bit DAC on the output. Plus, gray, by definition, has the red, green, and blue values the same. each component can only range from 0 to 255. By my calculations, that's only 255 different shades of gray.
0

LVL 1

Expert Comment

ID: 8162625
I mean 256.
0

Expert Comment

ID: 8164938
That's nice, but in 24 bit TrueColor you can do,
I think, 768 types of gray. I've write that program.

I've got on your mind this:
1.  0  0  0
2.  1  1  1
3.  2  2  2
...
255. 255 255 255

, but another choice is:

1. 0 0 0
2. 0 0 1
3. 0 1 1
4. 1 1 1
5. 1 1 2
6. 1 2 2

.. and so on
0

LVL 1

Expert Comment

ID: 8167078
Well, if you find a way to get more than 256 values out of 8 bits please share it with the world. By definition, gray has each color component set to the same value and each component is only 8 bits.

R,G,B
0,0,0
1,1,1
2,2,2
...
192,192,192
...
254,254,254
255,255,255

no more

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
###### Suggested Courses
Course of the Month14 days, 1 hour left to enroll