Archived

This topic is now archived and is closed to further replies.

Having problem with a JpegLoad function again!!!

This topic is 5630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey folks... everything fine? good. Well, i´m having problem with my JPeg Load function and wanna some help. Just dont know what to do anymore... my code follows below: IDirectDrawSurface7 JpegLoad(char *FileName, DDSURFACEDESC2 ddsd){ IJLERR jerr; JPEG_CORE_PROPERTIES jcprops; LPDIRECTDRAWSURFACE7 surface = NULL; ZeroMemory( &jcprops, sizeof( JPEG_CORE_PROPERTIES ) ); BYTE* jcpropsdata; jerr = ijlInit(&jcprops); if (jerr != IJL_OK) erro((char *)ijlErrorStr(jerr)); jcprops.JPGFile = const_cast(FileName); jerr = ijlRead(&jcprops, IJL_JFILE_READPARAMS); if (jerr != IJL_OK) erro((char *)ijlErrorStr(jerr)); jcpropsdata = new BYTE[jcprops.JPGWidth * jcprops.JPGHeight * jcprops.JPGChannels]; switch(jcprops.JPGChannels) { case 1: jcprops.JPGColor = IJL_BGR; break; case 3: jcprops.JPGColor = IJL_BGR; break; default: jcprops.DIBColor = (IJL_COLOR)IJL_OTHER; jcprops.JPGColor = (IJL_COLOR)IJL_OTHER; jcprops.DIBChannels = jcprops.JPGChannels; break; } jcprops.DIBWidth = jcprops.JPGWidth; jcprops.DIBHeight = jcprops.JPGHeight; jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.DIBWidth,jcprops.DIBChannels); int jcpropssize = (jcprops.DIBWidth * jcprops.DIBChannels + jcprops.DIBPadBytes) * jcprops.DIBHeight; jcpropsdata = new BYTE[ jcpropssize ]; if(jcpropsdata == NULL) erro("Erro de alocação de memória para JCPROPSDATA!"); jcprops.DIBBytes = reinterpret_cast(jcpropsdata); jerr = ijlRead(&jcprops, IJL_JFILE_READWHOLEIMAGE); if (jerr != IJL_OK){ erro((char *)ijlErrorStr(jerr)); delete[] jcpropsdata; } HBITMAP hbm; hbm = CreateBitmap (jcprops.JPGWidth, jcprops.JPGHeight, 1, 24, jcpropsdata); if(hbm == NULL) erro("Erro ao criar estrutura BitMap"); ZeroMemory(&ddsd, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwWidth = jcprops.JPGWidth; ddsd.dwHeight = jcprops.JPGHeight; G.ddrval = G.lpdd->CreateSurface(&ddsd, &surface, NULL); if (G.ddrval != DD_OK) erro("Erro ao criar superfície de textura para JPEG"); DDCopyBitmap(surface, hbm, 0, 0, 0, 0); delete[] jcpropsdata; ijlFree(&jcprops); return surface; } maybe the problem is in other part of the code, but it compiels and links well without any error nor warning... It just shows a black screen with white lines and some colored pixels around... Please, any help will be useful... I´m beginner to advanced programmer but have never programmed very much with graphics file formats, and i have little experience in this area. Bye! To code is to make things come to life

Share this post


Link to post
Share on other sites
You don''t say what the problem actually is.

Is the file being read okay, but not being copied to the surface?

Have you tried creating a test image saved as JPEG with (maybe) alternating white black pixels to see if the ''jcpropsdata'' structure is being filled correctly?

Looks like you are using the Intel libraries. Is this correct?
If so, have you tried to get the example loader working?
Can''t help much until you say what you''ve tried!

Share this post


Link to post
Share on other sites
Thanks for replying to my post. Actually, i trried just code implementatios to see what is happening. I teste the loader from Intel Library(Yes, i´m using Intel Library) and it worked fine.
I will try to catch the data inside the jcpropsdata to see if it is filled correctly... And the problem is that, if im correct, it is loading wrong to surface, because i made some error codes as you can see, to see if the function return properly results! It compiles correct, runs, but dont show anything but a black screen with white banners and some pixels lost in the surface, its like garbage of the memory you know!

Havy any other clue of what can be happening? If you need some other information, please ask and i will be happy to post anything else i can...


To code is to make things come to life

Share this post


Link to post
Share on other sites
Thanks for replying to my post. Actually, i trried just code implementatios to see what is happening. I teste the loader from Intel Library(Yes, i´m using Intel Library) and it worked fine.
I will try to catch the data inside the jcpropsdata to see if it is filled correctly... And the problem is that, if im correct, it is loading wrong to surface, because i made some error codes as you can see, to see if the function return properly results! It compiles correct, runs, but dont show anything but a black screen with white banners and some pixels lost in the surface, its like garbage of the memory you know!

Have any other clue of what can be happening? If you need some other information, please ask and i will be happy to post anything else i can...


To code is to make things come to life

Share this post


Link to post
Share on other sites
IDirectDrawSurface7 JpegLoad(char *FileName, DDSURFACEDESC2 ddsd);

Why does your function return an IDirectDrawSurface7 instead of an IDirectDrawSurface7* ? Is this a typo?


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
No, it was wrong and i made it to retur IDirectDrawSurface7 * now... but still having the same problem...

I rewrote all the code by a different implementation this morning and it does not work, as i can see the problem is that it is not reading the image right.. Because i alocate the buffer correctly in the DX surface. I wonder what is wrong!

this is my new code: (its long...)

IDirectDrawSurface7 *JpegLoad(char *FileName, DDSURFACEDESC2 ddsd){

IJLERR jerr;
JPEG_CORE_PROPERTIES jcprops;
LPDIRECTDRAWSURFACE7 surface = NULL;
bool bres;

ZeroMemory( &jcprops, sizeof( JPEG_CORE_PROPERTIES ) );

BYTE* jcpropsdata;

DWORD x = 0;
DWORD y = 0;
DWORD c = 0;
DWORD wholeimage;
DWORD dib_line_width;
DWORD dib_pad_bytes;
BITMAPINFOHEADER* bmih;

bres = TRUE;

__try
{
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}

jcprops.JPGFile = const_cast(FileName);
jerr = ijlRead(&jcprops, IJL_JFILE_READPARAMS);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}
x = jcprops.JPGWidth;
y = jcprops.JPGHeight;
c = 3;

dib_line_width = x * c;
dib_pad_bytes = IJL_DIB_PAD_BYTES(x,c);

wholeimage = (dib_line_width + dib_pad_bytes) * y;

jcpropsdata = new BYTE[sizeof(BITMAPINFOHEADER) + wholeimage];
if(NULL == jcpropsdata)
{
bres = FALSE;
__leave;
}

bmih = reinterpret_cast(jcpropsdata);

bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = x;
bmih->biHeight = y;
bmih->biPlanes = 1;
bmih->biBitCount = 24;
bmih->biCompression = BI_RGB;
bmih->biSizeImage = 0;
bmih->biXPelsPerMeter = 0;
bmih->biYPelsPerMeter = 0;
bmih->biClrUsed = 0;
bmih->biClrImportant = 0;



jcprops.DIBWidth = x;
jcprops.DIBHeight = y;
jcprops.DIBChannels = c;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBPadBytes = dib_pad_bytes;
jcprops.DIBBytes = reinterpret_cast(jcpropsdata + sizeof(BITMAPINFOHEADER));


switch(jcprops.JPGChannels)
{
case 1:
{
jcprops.JPGColor = IJL_G;
break;
}
case 3:
{
jcprops.JPGColor = IJL_YCBCR;
break;
}
default:
{
jcprops.DIBColor = (IJL_COLOR)IJL_OTHER;
jcprops.JPGColor = (IJL_COLOR)IJL_OTHER;
break;
}
}

jerr = ijlRead(&jcprops, IJL_JFILE_READWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = FALSE;
__leave;
}

HBITMAP hbm;

hbm = CreateBitmap (bmih->biWidth, bmih->biHeight, 1, 24, jcpropsdata);
if(hbm == NULL)

erro("Erro ao criar estrutura BitMap");


ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = bmih->biWidth;
ddsd.dwHeight = bmih->biHeight;

G.ddrval = G.lpdd->CreateSurface(&ddsd, &surface, NULL);
if (G.ddrval != DD_OK)

erro("Erro ao criar superfície de textura para JPEG");


DDCopyBitmap(surface, hbm, 0, 0, 0, 0);
}

__finally
{
if(FALSE == bres)
{
if(NULL != jcpropsdata)
{
delete [] jcpropsdata;
jcpropsdata = NULL;
}
}
ijlFree(&jcprops);
}

return surface;

}


To code is to make things come to life

Share this post


Link to post
Share on other sites
well, i just happened to code a jpeg loading function using libjpeg, so if it''s of any help to you, here it is:

  
HBITMAP LoadJpeg(LPCTSTR FileName)
{
jpeg_decompress_struct cinfo;
jpeg_error_mgr jerr;

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);

FILE * infile;
if ((infile = _tfopen(FileName, _T("rb"))) == NULL)
return NULL;

jpeg_stdio_src(&cinfo, infile);

jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);

BITMAPINFO bmi = { { sizeof (BITMAPINFOHEADER), cinfo.output_width, cinfo.output_height, 1, WORD(cinfo.output_components*8), BI_RGB } };
PVOID pBitmapBits;
HBITMAP hBitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, &pBitmapBits, 0, 0);
if (hBitmap)
{
BITMAP bm;
GetObject(hBitmap, sizeof bm, &bm);
PBYTE CurrentScanLine = PBYTE(bm.bmBits)+bm.bmWidthBytes*bm.bmHeight;
for (UINT i = 0; i < cinfo.output_height; ++i)
{
CurrentScanLine -= bm.bmWidthBytes;
UINT read = jpeg_read_scanlines(&cinfo, &CurrentScanLine, 1);
ATLASSERT(read == 1);
}

PBYTE CurrentLine = PBYTE(pBitmapBits);
for (UINT line = 0; line < cinfo.output_height; ++line)
{
PBYTE CurrentPixel = CurrentLine;
CurrentLine += bm.bmWidthBytes;
for (UINT pixel = 0; pixel < cinfo.output_width; ++pixel)
{
std::swap(CurrentPixel[0], CurrentPixel[2]);
CurrentPixel += cinfo.output_components;
}
}
}

jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);

return hBitmap;
}


---
visit #directxdev on afternet

Share this post


Link to post
Share on other sites
Thank´s, but i dont know the lib that you used and where can i find it!!! Can you sen me the adress to the file? because im using the Intel Jpeg Lib.. Thanks

To code is to make things come to life

Share this post


Link to post
Share on other sites
Since I have a feeling that you wouldn't want to build libjpeg by yourself, I zipped up only required headers and the lib file that I built for myself here. If you do want to build libjpeg yourself, here is the full source (including my prebuilt lib).

---
Come to #directxdev IRC channel on AfterNET

[edited by - niyaw on July 15, 2002 1:22:21 AM]

Share this post


Link to post
Share on other sites