0

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;
}
4GLGuy
  • 21
  • 3
  • 3
    Is this monochrome or color palellte 8 bpp image? In this case you need to clone also the image palette, see `GetColorTable` and `SetColorTable`. – Alex F Nov 06 '18 at 16:37
  • @AlexF - Not sure how I can answer that - the picture is a good quality greyscale image (Paint shop Pro 5.0 report 225 colors used in the single layer). Is there a test you can recomend to check for a better answer? I'll will look at the Get/SetColorTable documenation and have a play. Thanks – 4GLGuy Nov 06 '18 at 17:42
  • Also you have to call `imageJPG.ReleaseDC()` for cleanup – Barmak Shemirani Nov 06 '18 at 23:12
  • Grayscale images are usually kept as color palette images. For 8 bpp images the color palette is: 0: RGB(0,0,0), 1: RGB(1,1,1)...255:RGB(255,255,255). Bitmap pixels are actually indexes in this palette. – Alex F Nov 07 '18 at 05:31
  • So, call GetColorTable for imageGRAPHIC to investigate this. – Alex F Nov 07 '18 at 05:32
  • @BarmakShemirani - This is just the working part of the code - there is a full tidy up of all objects as part of a try-throw block. Cheers – 4GLGuy Nov 07 '18 at 07:51
  • @AlexF - Thanks Alex I was looking for something to read to help me understand RGB and 8bits - You have nudged me in the right direction now - Thanks – 4GLGuy Nov 07 '18 at 07:52

0 Answers0