How to read a D3DFMT_A16B16G16R16 texture?

Hi,

I had a a D3DFMT_A16B16G16R16 format texture I want to read. I did sth like this:

float *dest = new float[size*size*4];

D3DLOCKED_RECT locked;

HRESULT hr = ptex->LockRect(0, &locked, NULL, 0);

assert(hr == S_OK);

WORD *fPointer=(WORD *)locked.pBits;

for (DWORD y=0; y<size; y++)

{

for (DWORD x=0; x<size; x++)

{

DWORD index=( x*4 + (y*(locked.Pitch/2)));

dest[y*size*4+x*4] = fPointer[index] ;

dest[y*size*4+x*4+1] = fPointer[index+1] ;

dest[y*size*4+x*4+2] = fPointer[index+2] ;

dest[y*size*4+x*4+3] = fPointer[index+3] ;

}

}

ptex->UnlockRect(0);

But it seems not working, does anyone know how to do it Thanks a lot,

Jianfeng



Answer this question

How to read a D3DFMT_A16B16G16R16 texture?

  • Dd365

    dividing by 65535.0f is not right, but D3DXFloat16To32Array() works. Thanks Ross.
  • Tamiri

    Jianfeng Yin wrote:

    float *dest = new float[size*size*4];
    WORD *fPointer=(WORD *)locked.pBits
    // ...
    dest[y*size*4+x*4] = fPointer[index] ;

    This should work, but looks unusual. Each component in a D3DFMT_A16B16G16R16 pixel is a 16-bit unsigned value ranging from 0 to 65535. When you convert this value to a float, the value remains the same. If you're expecting your float array to have values in the range of 0.0 to 1.0 then you need to divide by 65535.0:

    dest[y*size*4+x*4] = fPointer[index] / 65535.0f;

    Alternatively, your problem might instead be that you're trying to read a D3DFMT_A16B16G16R16F format texture, where each component is a 16-bit floating point value. In that case you'ld need to use D3DXFloat16To32Array() to convert the values.


  • How to read a D3DFMT_A16B16G16R16 texture?