Question : How do I save an 8 bit grayscale jpeg with CImage

Hello experts,

I have an 8 bit CImage, when I save it into a bitmap or png and check the properties it is an 8 bit bitmap but when I save it as a jpeg it is still 24 bit.  Following is code where I create the image and then save a test JPEG BMP and PNG, only the JPEG is 24 bit depth and I need it to be 8.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
if (isEightBit)
	{
		imagePtr->Create(pdib->biWidth,pdib->biHeight, 8);
		unsigned char * pImgBits= (unsigned char*)imagePtr->GetBits();
		int iPitch = imagePtr->GetPitch();
		char *pbitThat; 
		int iCount=0;
		int iMultBitCountFactor = pdib->biBitCount/8;
		int iMultBitCountFactor8 = pdib->biBitCount/32;
 
		////TO OPTIMIZE
		for ( int y=0; ybiHeight; y++ )
		{
			for ( int x=0; xbiWidth; x++ )
			{
				pbitThat = (char *) (LPBYTE(pImgBits) + (y * iPitch ) + x*iMultBitCountFactor8+0);
				*pbitThat = imgBits [(pdib->biHeight-y-1)*pdib->biWidth*iMultBitCountFactor+x*iMultBitCountFactor+0];
 
				pbitThat = (char *) (LPBYTE(pImgBits) + (y * iPitch ) + x*iMultBitCountFactor8+1);
				*pbitThat = imgBits [(pdib->biHeight-y-1)*pdib->biWidth*iMultBitCountFactor+x*iMultBitCountFactor+1];
 
				pbitThat = (char *) (LPBYTE(pImgBits) + (y * iPitch ) + x*iMultBitCountFactor8+2);
				*pbitThat = imgBits [(pdib->biHeight-y-1)*pdib->biWidth*iMultBitCountFactor+x*iMultBitCountFactor+2];
			}
		}
 
		RGBQUAD clrs[256];
 
		//set up a gray-scale palette
		for(int i=0;i<256;i++){
			clrs[i].rgbRed=i;
			clrs[i].rgbGreen=i;
			clrs[i].rgbBlue=i;
		}
		imagePtr->SetColorTable(0,256,clrs);
	}
///////////////////////////////////this is the save code
 
result = imgPtr->Save("C:\\test_JPEG.jpg", Gdiplus::ImageFormatJPEG); //this is 24 bit depth
 
result = imgPtr->Save("C:\\test_BMP.jpg", Gdiplus::ImageFormatBMP); //this is 8 bit depth
 
result = imgPtr->Save("C:\\test_PNG.jpg", Gdiplus::ImageFormatPNG); //this is 8 bit depth

Answer : How do I save an 8 bit grayscale jpeg with CImage

Just a closure note (not a objection):

      How do I save an 8 bit grayscale jpeg with CImage.

You limited your question in such a way that it was impossible for any Expert to post an answer other than "It can't be done."

Random Solutions  
 
programming4us programming4us