Pergunta

I am trying to load a TGA file in Android NDK.

I open the file using AssetManager, read in the entire contents of the TGA file into a memory buffer, and then I try to extract the pixel data from it.

I can read the TGA header part of the file without any problems, but when I try to advance the memory pointer past the TGA header, the app crashes. If I don't try to advance the memory pointer, it does not crash.

Is there some sort of limitation in Android NDK for pointer arithmetic?

Here is the code:

This function opens the asset file:

char* GEAndroid::OpenAssetFile( const char* pFileName )
{
char* pBuffer = NULL;

AAssetManager* assetManager = m_pState->activity->assetManager;
AAsset* assetFile = AAssetManager_open(assetManager, pFileName, AASSET_MODE_UNKNOWN);
if (!assetFile) {
    // Log error as 'error in opening the input file from apk'
    LOGD( "Error opening file %s", pFileName );
}
else
{
    LOGD( "File opened successfully %s", pFileName );
    const void* pData = AAsset_getBuffer(assetFile);
    off_t fileLength = AAsset_getLength(assetFile);
    LOGD("fileLength=%d", fileLength);

    pBuffer = new char[fileLength];
    memcpy( pBuffer, pData, fileLength * sizeof( char ) );
}

return pBuffer;
}

And down here in my texture class I try to load it:

char* pBuffer = g_pGEAndroid->OpenAssetFile( fileNameWithPath );

TGA_HEADER textureHeader;
char    *pImageData = NULL;
unsigned int bytesPerPixel = 4;

    textureHeader = *reinterpret_cast<TGA_HEADER*>(pBuffer);
    // I double check that the textureHeader is valid and it is.

    bytesPerPixel   = textureHeader.bits/8;             // Divide By 8 To Get The Bytes Per Pixel
    m_imageSize     = textureHeader.width*textureHeader.height*bytesPerPixel;   // Calculate The Memory Required For The TGA Data


    pImageData = new char[m_imageSize];
    // the line below causes the crash
    pImageData = reinterpret_cast<char*>(pBuffer + sizeof( TGA_HEADER)); // <-- causes a crash

If I replace the line above with the following line (even though it is incorrect), the app runs, although obviously the texture is messed up.

    pImageData = reinterpret_cast<char*>(pBuffer); // <-- does not crash, but obviously texture is messed up.

Anyone have any ideas?

Thanks.

Foi útil?

Solução 2

Sigh, I just realized the mistake. I allocate memory for pImageData, then set the point to the buffer. This does not sit well when I try to create an OpenGL texture with the pixel data. Modifying it so I memcpy the pixel data from (pBuffer + sizeof( TGA_HEADER) ) to pImageData fixes the problem.

Outras dicas

Why reinterpret_cast? You're adding an integer to a char*; that operation produces a char*. No typecast necessary.

One caveat for pointer juggling on Android (and on ARM devices in general): ARM cannot read/write unaligned data from memory. If you read/write an int-sized variable, it needs to be at an address that's a multiple of 4; for short, a multiple of 2. Bytes can be at any address. This does not, as far as I can see, apply to the presented snippet. But do keep in mind. It does throw off binary format parsing occasionally, especially when ported from Intel PCs.

Simply assigning an unaligned value to a pointer does not crash. Dereferencing it might.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top