converting JPEG Grayscale to DIB using libjpeg in C++

Problem in converting Jpeg to DIB, Iam using the same code for Jpeg RGB(Jpeg_color_space=JCS_RGB) and Grayscale Images(JCS_GRAYSCALE). this code works correctly for Jpeg RGB Images when i create DIB for 24 bit. But for Jpeg Grayscale(JCS_GRAYSCALE) images, when i am converting to DIB I get a Black color space next to image,but the original image does'nt contain any black space.

Please help me to solve this problem using the same code. I dont Know where the problem occurs.

following is the code i use for converting jpeg to DIB.

//Function to convert jpeg to dib.

int convert_jpeg_to_dib(const char * filename)

{

struct jpeg_decompress_struct cinfo;

unsigned char * buffer;

int result = 0;

struct my_error_mgr jerr;

FILE * infile; /* source file */

int row_stride; /* physical row width in output buffer */


if ((infile = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "can't open %s\n", filename);
return result;
}

cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;

if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return result;
}

jpeg_create_decompress(&cinfo);

jpeg_stdio_src(&cinfo, infile);

jpeg_read_header(&cinfo, TRUE);

jpeg_start_decompress(&cinfo);

DIBSection dib;

if (cinfo.num_components == 1 || cinfo.num_components == 3)
{

dib.Create(cinfo.image_width, cinfo.image_height, 24);

if (dib.IsCreated())
{

row_stride = cinfo.output_width * cinfo.output_components;

buffer = (unsigned char*)malloc(sizeof(cinfo) * row_stride);

int linesize = (cinfo.image_width * cinfo.num_components);

unsigned char * dibBits = (unsigned char *)dib.GetBits();

UINT32 off;
UINT32 tw = dib.GetTotalWidth();
UINT32 height = dib.Height();

UINT32 row = 0;

while (cinfo.output_scanline < cinfo.output_height)
{


jpeg_read_scanlines(&cinfo, &buffer, 1);


off = (height - ++row) * 3 * tw;

memcpy(&dibBits[off],&buffer[0],row_stride);


}

free(buffer);

}

result++;
}

jpeg_finish_decompress(&cinfo);

jpeg_destroy_decompress(&cinfo);
jpeg_destroy((j_common_ptr)&cinfo);

dib.Close();

fclose(infile);

}


//code for creating DIB

void Create(int cx, int cy, int nbits)
{

// Initialize the bitmapinfo header

const DWORD dwcBihSize = sizeof(BITMAPINFOHEADER);
memset(&m_bih, 0, dwcBihSize);

// Calculate the memory required for the DIB
DWORD dwSize = dwcBihSize +
(2>>nbits) * sizeof(RGBQUAD) +
((nbits * cx) * cy);


// Populate bitmapinfo header
BITMAPINFOHEADER bih;
bih.bisize = dwcbihsize;
bih.biwidth = ((((int) cx * 8) + 31) & ~31) >> 3;
bih.biheight = cy;
bih.biplanes = 1;
bih.bibitcount = nbits;
bih.bicompression = bi_rgb;
bih.bisizeimage = dwsize;
bih.biclrused = (nbits <= 8) ? 1<<nbits : 0;

// Create a new DC.
HDC hDC = GetDC(NULL);
memHdc = CreateCompatibleDC(hDC);

// Create the DIB section.
m_hbmp = CreateDIBSection( memHdc,
(BITMAPINFO*)&bih,
DIB_RGB_COLORS,
&m_pBits,
NULL,
0);


DeleteDC(memHdc);
}

}

Last edited on
some one please help me to find where the issue occurs.
Thanks.
Topic archived. No new replies allowed.