CImage : copying 8bit JPEGs gives a black image

507 Views Asked by At

The following code extract I am loading an 300DPI 8-bit JPEG and then trying to write it out again in a Fresh instance of a CImage also as a JPEG.

But I end up with a black image with the correct dimensions.

Can someone explain why that is?

Ignore the commented out brush lines I'll get over that mental hurdle later.

If I hard code the bppGraphic to 24 it does copy the picture (to a DPI of 96) resulting in a smaller file size. I can live with this, I guess I am just curious.

Update 07-Nov-2018

So I added the indendented 'if' statement and it still came out black. The colorCountIMAGE comes out at 20. (The IsIndexed lines were to help me with an ASSERT issue I found in the SetColorTable - but it went away)

I think I may just force in all 24 bit.

Thanks

4GLGuy PS This is all being done in VS2017.

char filePath[256] = "C:\\temp\\b64-one.jpg";
CImage imageGRAPHIC, imageJPG;

HRESULT retval;
bool result;

retval = imageGRAPHIC.Load(filePath);
if (retval != S_OK) {
    throw FALSE;
}

int xGRAPHIC, yGRAPHIC, bppGRAPHIC = 0;
xGRAPHIC = imageGRAPHIC.GetWidth();
yGRAPHIC = imageGRAPHIC.GetHeight();
bppGRAPHIC = imageGRAPHIC.GetBPP();

//Create my target JPG same size and bit depth specifying 
//that there is no alpha channel (dwflag last param)
result = imageJPG.Create(xGRAPHIC, yGRAPHIC, bppGRAPHIC, 0);
auto dcJPEG = imageJPG.GetDC();


    if (bppGRAPHIC <= 8)
    {

        result = imageJPG.IsIndexed();
        result = imageGRAPHIC.IsIndexed();
        auto dcIMAGE = imageGRAPHIC.GetDC();

        int colorCountIMAGE = GetDeviceCaps(dcIMAGE, NUMCOLORS);

        RGBQUAD* coltblIMAGE = new RGBQUAD[colorCountIMAGE];
        imageGRAPHIC.GetColorTable(0, colorCountIMAGE, &coltblIMAGE[0]);
        imageJPG.SetColorTable(0, colorCountIMAGE, &coltblIMAGE[0]);

    }


//Let there be white - 8 bit depth with 24 bit brush - no worky
//CRect rect{ 0, 0, xGRAPHIC, yGRAPHIC };
//HBRUSH white = CreateSolidBrush(RGB(255, 255, 255));
//FillRect(dcJPEG, &rect, white);

result = imageGRAPHIC.Draw(dcJPEG, 0, 0);

retval = imageJPG.Save(filePath, Gdiplus::ImageFormatJPEG);
if (retval != S_OK) {
    throw FALSE;
}
0

There are 0 best solutions below