androidpointerstexturestgaandroid-ndk

Android NDK Pointer Arithmetic


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.


Solution

  • 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.