Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8090605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:46:45+00:00 2026-06-05T19:46:45+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:46:46+00:00Added an answer on June 5, 2026 at 7:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to load a csv file into a datatable using oledb. This is
I'm trying to load Gravatars into Flash. Luckily, they provided a crossdomain.xml file at
Im trying to load a xml file into a dataset and generate crystal report
I am trying to load a Silverlight project to read every XAML file by
Trying to load a file into python. It's a very big file (1.5Gb), but
I'm trying to load a CSV into MySQL using the LOAD DATA INFILE technique.
I'm trying to load data from a CSV file into a MySQL database, and
Trying to load a local file into a webbrowser control but this does not
Im trying to load content into a DIV from an XML file when the
Im trying to load an xml file using the jquery ajax function,it works fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.