Solved

# Convert 24 bit to 8 bit grayscale

Posted on 2003-03-22
Medium Priority
388 Views
hi to all,
i want to convert 24 bit image to a 8 bit grayscale image
i write these codes but these doesnt work

void __fastcall TForm1::Button2Click(TObject *Sender)
{
Graphics::TBitmap *bmp24 = new Graphics::TBitmap();
Graphics::TBitmap *bmp8  = new Graphics::TBitmap();

try
{
bmp8->Width=bmp24->Width;
bmp8->Height=bmp24->Height;
bmp8->PixelFormat=pf8bit;

for (int y=0; y < bmp24->Height; y++)
{
Byte *ptr24 = new Byte[(bmp24->Width)*3];
ptr24 = (Byte *)bmp24->ScanLine[y];
Byte *ptr8 = new Byte[bmp24->Width];
ptr8 = (Byte *)bmp8->ScanLine[y];
for (int x=0; x< bmp24->Width; x++)
{
ptr8[x] = (Byte) ptr24[x*3];
}
delete [] ptr24;
delete [] ptr8;
delete ptr24;
//..
}
}
catch (...)
{
ShowMessage("Could not load or alter bitmap");
}
bmp8->SaveToFile("asta8.bmp");

delete bmp24;
delete bmp8;
}

also delete commands give error. Why?
0
Question by:mece
• 3
• 2
• 2

LVL 22

Accepted Solution

grg99 earned 80 total points
ID: 8186237
You cant just try to poke a 24-bit packed RGB value into an 8-bit grayscale location.

You have to take the individual RGB values and form a weighed average.   A raw first cut would be to just add the RGB values and divide by three.  That would give you a rough approximation of grayscale.  To do it right you should multiply each value by the right scaling factor.
One commonly used set of factors is:

R = 0.30

G = 0.59

B = 0.11

Out = R * 0.3 + G * 0.59 + B * 0.11

--------------
Now in many computers flaoting-point multiplies are considerably slower than integer ones, so you'd probably recast this into 16-bit integer math, something like:

Out = (int8) ( ( (int16) R ) * (0.3 * 256 )) +
( (int16) G ) * (0.6 * 256 )) +
( (int16) R ) * (0.1 * 256 )) );

Of course you should first pre-compute those scaling factors and store them into int16's.

0

Author Comment

ID: 8187702
i will try this code part

also why delete part does not work?
delete [] ptr24;
delete [] ptr8;
delete ptr24;
it gives error on run
0

Author Comment

ID: 8187742
also i know that all red, green and blue values of my image are same. They are 24 bit images.
i want to convert them into 8 bit image.
0

LVL 5

Expert Comment

ID: 8188220
You don't need to use new and delete here.
Also, if your red,green,blue are the same,
You can just add them all and divide it by three
...
for (int y=0; y < bmp24->Height; y++) {
Byte *ptr24 = (Byte *)bmp24->ScanLine[y];
Byte *ptr8 = (Byte *)bmp8->ScanLine[y];
for (int x=0; x< bmp24->Width; x++)
{
*ptr8 = (ptr24[0]+ptr24[1]+ptr24[2]) / 3;
ptr8 += 1; ptr24 += 3;
}
}
...
0

LVL 22

Expert Comment

ID: 8190022
"All the same" doesnt matter.  Even if the values of RGB are all scaledto go from 0 to 100%, the effective brightness doesnt get equal contributions from R, G, and B.

For example, the eye is very sensitive to green, so green contributes 60% of the apparent brightness.

The eye is very insensitive to blue, it only contributes 11% to brightness.  That is why you never see any bright, pure blue lights.

If you dont use the scaling factors, pictures will look very unusual, with grass looking black, and skies blindingly white.  You probably want better color balance than that!
0

LVL 5

Expert Comment

ID: 8190635
Good point grg.
0

Author Comment

ID: 8191271
thanx grg
good point of view
i will try it
0

## Featured Post

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their waâ€¦
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why tâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month7 days, 21 hours left to enroll