I use freeimage to load jpgs, then copy the data to a direct draw surface (dx7). This works fine for 95% of my users, but for 5%, they get white textures. No error messages, just white textures. is this code ok to you?:
Code:
DebugOut("Creating JPG file");
FIBITMAP* fimage = FreeImage_Load(FIF_JPEG,szImage,JPEG_DEFAULT);
if(!fimage)
{
DebugOut("failed loading jpg");
return;
}
DWORD bpp;
if(BFallBack)
{
bpp = (DWORD)AppliedColorDepth;
}
else
{
D3DX->GetNumBits(&bpp,NULL,NULL,NULL);
}
if(bpp == 16)
{
fimage = FreeImage_ConvertTo16Bits565(fimage);
DebugOut("Converting JPG to 16 bits");
}
//create the surface
DDSCAPS2 ddsCaps;
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ZeroMemory(&ddsCaps,sizeof(ddsCaps));
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.dwHeight = FreeImage_GetHeight(fimage);
ddsd.dwWidth = FreeImage_GetWidth(fimage);
ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
// Create Surface
HRESULT hr = GetD3DEngine()->GetDDObject()->CreateSurface( &ddsd, lplpDDS, NULL );
if( hr == DD_OK)
{
DebugOut("Created jpg surface");
}
else
{
char debug[256];
sprintf_s(debug,256,"Failed to create jpg surface");
DebugOut(debug);
D3DEngineError(hr,debug);
return;
}
//copy data to it
LPDIRECTDRAWSURFACE7 psurf = *lplpDDS;
DDSURFACEDESC2 ds;
ds.dwSize = sizeof(ds);//initialize it
hr = psurf->Lock(NULL,&ds,DDLOCK_WAIT,NULL);
if(FAILED(hr))
{
return;
}
BYTE* pdest = (BYTE*)ds.lpSurface;
for(int y = 0; y < ds.dwHeight; y++)
{
BYTE* srcbits = FreeImage_GetScanLine(fimage,ds.dwHeight - y - 1);
DWORD pitch = (ds.lPitch * y);
for(int x = 0; x < ds.dwWidth; x++)
{
if(bpp == 32)
{
//get data at screen pos xy
DWORD offset = pitch + (x * 4);
BYTE* pbyte = &pdest[offset];
*pbyte = srcbits[x * 3];pbyte++;
*pbyte = srcbits[(x * 3) + 1];pbyte++;
*pbyte = srcbits[(x * 3) + 2];pbyte++;
*pbyte = 0;
}
if(bpp == 16)
{
//get data at screen pos xy
DWORD offset = pitch + (x * 2);
BYTE* pbyte = &pdest[offset];
*pbyte = srcbits[x * 2];
pbyte++;
*pbyte = srcbits[(x * 2) + 1];
}
}
}
psurf->Unlock(NULL);
FreeImage_Unload(fimage);
I'm presuming the Lock() prior to copying the data is failing (silly me didn't put error check code there), but why would this happen?
Cheers.