Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,
i am new to VC++,
i am doing a simple vc++ project in that i had a situation.

scenario is...

i have images in .jpeg,.bmp and .png.
i need to implement an algorithm for image compression.....
Steps are:
• Load image into 24-bit image buffer
• Locate top, left pixel
• Convert Red channel 8-bit value to 6-bit
o 6-bit = (8-bit_RedValue/255) * 63
o Round computed 6-bit value to nearest integer
o Multiply rounded value by 4 (left shift by 2 bits) to shift 6-bit value into upper 6 bits of a byte
o Store resulting 8-bit value into the Red channel of the final output file
• Repeat conversion for Green channel
• Repeat conversion for Blue channel
• Move to pixel located directly to the right. If at the end of the line, move to the left-most pixel of the next line. Repeat until all pixels have been processed

and my code is:

C++
Bitmap^ img=gcnew Bitmap(b1->Size.Width,b1->Size.Height, PixelFormat::Format24bppRgb);       
img->SetResolution(b1->HorizontalResolution, b1->VerticalResolution);
Graphics^ g=Graphics::FromImage(img);
g->DrawImage(b1,0,0);
delete g;
Color c;
for (int i = 0; i < img->Width; i++)
{
for (int j = 0; j < img->Height; j++)
{
c = img->GetPixel(i, j);
double oldr = c.R;
double oldg = c.G;
double oldb = c.B;
int r=Math::Round((oldr/255)*63);
int g=Math::Round((oldg/255)*63);
int b=Math::Round((oldb/255)*63);
int R=System::Convert::ToInt32(r*4);
int G=System::Convert::ToInt32(g*4);
int B=System::Convert::ToInt32(b*4);
img->SetPixel(i,j,Color::FromArgb(R,G,B));
}
}


here after saving the final image..... the final image is taking high memory to save ...
like original image =10kb
final image=100kb...



please help me doing this........
Posted

1 solution

If you're saving raw image data, that's why. Take for example the DXT algorithm. The image is cut up into little blocks of 4 pixels:

1 2
3 4

Normally, this might take 12 bytes to store; 3 * 4 = 12. But through DXT, You'd pick the two most different colors out of the 4, then each pixel might be an index between 0 and 3 telling the LERP value between the first and second color. Eg.

0 = 100% color 1, 0% color 2
1 = 66% color 1, 33% color 2
2 = 33% color 1, 66% color 2
3 = 0% color 1, 100% color 2

As a result, each pixel would only require 2 bits to store and it now only takes 3 * 2 + 2 * 4 / 8 = 7 bytes to store 4 pixels; cutting down image size by approximately 42%.

This is just one example of image compression which can be further expanded, but if you want to take a simpler route, just use a library like libpng that will automatically do it for you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900