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 8159065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:47:58+00:00 2026-06-06T17:47:58+00:00

I have problems trying to load png file into my application. It seems to

  • 0

I have problems trying to load png file into my application. It seems to load headers fine, and recognize that image is png, but for some reason the output has the same values, though image is really not one color. Another thing that bugs me is that 16 bytes are left unread.

My code:

AAsset* pngAsset_ = 0;
AAssetManager* pngAassetManager_ = 0;

void png_asset_read(png_structp png, png_bytep data, png_size_t size) {
    //AAsset_seek(pngAsset_, 0, 0);
    AAsset_read(pngAsset_, data, size);
    int numBytesRemaining = AAsset_getRemainingLength(pngAsset_);
    LOGI("Read size: %d, remaining: %d", size, numBytesRemaining);
}

Image* loadPngFile(AAssetManager* assetManager) {
    pngAassetManager_ = assetManager;

    LOGI("Trying to load image...");
    int HEADER_SIZE = 8;
    string filename = "skybox.png";
    pngAsset_ = AAssetManager_open(pngAassetManager_, filename.c_str(),
            AASSET_MODE_UNKNOWN);
    if (pngAsset_ == 0) {
        LOGW("Asset \"%s\" not found.", filename.c_str());
        return 0;
    }

    off_t bufferSize = AAsset_getLength(pngAsset_);
    png_byte* buffer = new png_byte[HEADER_SIZE];

    int numBytesRead = AAsset_read(pngAsset_, buffer, HEADER_SIZE);
    int numBytesRemaining = AAsset_getRemainingLength(pngAsset_);

    int is_png = !png_sig_cmp(buffer, 0, 8);
    if (!is_png) {
        LOGE("File %s format is not PNG.", filename.c_str());
        return 0;
    }
    LOGI("Size of the file: %d, bytes read: %d, bytes remain: %d",
            bufferSize, numBytesRead, numBytesRemaining);

    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
            NULL, NULL);
    if (!png_ptr) {
        LOGE("Unable to create PNG structure: %s", filename.c_str());
        return 0;
    }

    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr) {
        png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
        LOGE("Unable to create png info : %s", filename.c_str());
        return 0;
    }

    png_infop end_info = png_create_info_struct(png_ptr);
    if (!end_info) {
        png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
        LOGE("Unable to create png end info : %s", filename.c_str());
        return 0;
    }

    if (setjmp(png_jmpbuf(png_ptr))) {
        LOGE("Error during setjmp : %s", filename.c_str());
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        return 0;
    }
    png_set_read_fn(png_ptr, NULL, png_asset_read);
    png_set_sig_bytes(png_ptr, 8);
    png_read_info(png_ptr, info_ptr);

    int bit_depth, color_type;
    png_uint_32 twidth, theight;
    png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
            NULL, NULL, NULL);
    LOGI("Width: %d, height: %d.", twidth, theight);

    // Update the png info struct.
    png_read_update_info(png_ptr, info_ptr);

    // Row size in bytes.
    int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
    LOGI("Row size: %d bytes.", rowbytes);

    // Allocate the image_data as a big block, to be given to opengl
    png_byte *image_data = new png_byte[rowbytes * theight];
    if (!image_data) {
        //clean up memory and close stuff
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        LOGE(
                "Unable to allocate image_data while loading %s ", filename.c_str());
    }

    //row_pointers is for pointing to image_data for reading the png with libpng
    png_bytep *row_pointers = new png_bytep[theight];
    if (!row_pointers) {
        //clean up memory and close stuff
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        delete[] image_data;
        LOGE(
                "Unable to allocate row_pointer while loading %s ", filename.c_str());
    }
    // set the individual row_pointers to point at the correct offsets of image_data
    for (int i = 0; i < theight; ++i)
        row_pointers[theight - 1 - i] = image_data + i * rowbytes;

    //read the png into image_data through row_pointers
    png_read_image(png_ptr, row_pointers);

    for (int i = 0; i < 10; i ++) {
        LOGI("Pixel %d: %f %f %f %f",i, image_data[i * 4], image_data[i * 4 + 1],
                image_data[i * 4 + 2], image_data[i * 4 + 3]);
    }

    //Now generate the OpenGL texture object
    Image* image = new Image((unsigned char*) image_data, twidth, theight,
            twidth * theight * 4, 0);

    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    delete[] row_pointers;

    AAsset_close(pngAsset_);

    return image;
}

Output:

06-30 23:49:00.651: I/Ghost Engine(21279): Trying to load image...
06-30 23:49:00.651: I/Ghost Engine(21279): Size of the file: 529452, bytes read: 8, bytes remain: 529444
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 8, remaining: 529436
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 13, remaining: 529423
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 4, remaining: 529419
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 8, remaining: 529411
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 9, remaining: 529402
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 4, remaining: 529398
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 8, remaining: 529390
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 32, remaining: 529358
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 4, remaining: 529354
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 8, remaining: 529346
06-30 23:49:00.651: I/Ghost Engine(21279): Width: 1024, height: 1024.
06-30 23:49:00.651: I/Ghost Engine(21279): Row size: 4096 bytes.
06-30 23:49:00.651: I/Ghost Engine(21279): Read size: 8192, remaining: 521154
06-30 23:49:00.681: I/Ghost Engine(21279): Read size: 8192, remaining: 512962
06-30 23:49:00.681: I/Ghost Engine(21279): Read size: 8192, remaining: 504770
06-30 23:49:00.691: I/Ghost Engine(21279): Read size: 8192, remaining: 496578
06-30 23:49:00.701: I/Ghost Engine(21279): Read size: 8192, remaining: 488386
06-30 23:49:00.721: I/Ghost Engine(21279): Read size: 8192, remaining: 480194
06-30 23:49:00.721: I/Ghost Engine(21279): Read size: 8192, remaining: 472002
06-30 23:49:00.731: I/Ghost Engine(21279): Read size: 8192, remaining: 463810
06-30 23:49:00.731: I/Ghost Engine(21279): Read size: 8192, remaining: 455618
06-30 23:49:00.731: I/Ghost Engine(21279): Read size: 8192, remaining: 447426
06-30 23:49:00.741: I/Ghost Engine(21279): Read size: 8192, remaining: 439234
06-30 23:49:00.741: I/Ghost Engine(21279): Read size: 8192, remaining: 431042
06-30 23:49:00.741: I/Ghost Engine(21279): Read size: 8192, remaining: 422850
06-30 23:49:00.741: I/Ghost Engine(21279): Read size: 8192, remaining: 414658
06-30 23:49:00.741: I/Ghost Engine(21279): Read size: 8192, remaining: 406466
06-30 23:49:00.741: I/Ghost Engine(21279): Read size: 8192, remaining: 398274
06-30 23:49:00.751: I/Ghost Engine(21279): Read size: 8192, remaining: 390082
06-30 23:49:00.751: I/Ghost Engine(21279): Read size: 8192, remaining: 381890
06-30 23:49:00.751: I/Ghost Engine(21279): Read size: 8192, remaining: 373698
06-30 23:49:00.751: I/Ghost Engine(21279): Read size: 8192, remaining: 365506
06-30 23:49:00.751: I/Ghost Engine(21279): Read size: 8192, remaining: 357314
06-30 23:49:00.751: I/Ghost Engine(21279): Read size: 8192, remaining: 349122
06-30 23:49:00.761: I/Ghost Engine(21279): Read size: 8192, remaining: 340930
06-30 23:49:00.761: I/Ghost Engine(21279): Read size: 8192, remaining: 332738
06-30 23:49:00.761: I/Ghost Engine(21279): Read size: 8192, remaining: 324546
06-30 23:49:00.761: I/Ghost Engine(21279): Read size: 8192, remaining: 316354
06-30 23:49:00.761: I/Ghost Engine(21279): Read size: 8192, remaining: 308162
06-30 23:49:00.761: I/Ghost Engine(21279): Read size: 8192, remaining: 299970
06-30 23:49:00.761: I/Ghost Engine(21279): Read size: 8192, remaining: 291778
06-30 23:49:00.771: I/Ghost Engine(21279): Read size: 8192, remaining: 283586
06-30 23:49:00.771: I/Ghost Engine(21279): Read size: 8192, remaining: 275394
06-30 23:49:00.771: I/Ghost Engine(21279): Read size: 8192, remaining: 267202
06-30 23:49:00.771: I/Ghost Engine(21279): Read size: 8192, remaining: 259010
06-30 23:49:00.771: I/Ghost Engine(21279): Read size: 8192, remaining: 250818
06-30 23:49:00.771: I/Ghost Engine(21279): Read size: 8192, remaining: 242626
06-30 23:49:00.781: I/Ghost Engine(21279): Read size: 8192, remaining: 234434
06-30 23:49:00.781: I/Ghost Engine(21279): Read size: 8192, remaining: 226242
06-30 23:49:00.781: I/Ghost Engine(21279): Read size: 8192, remaining: 218050
06-30 23:49:00.781: I/Ghost Engine(21279): Read size: 8192, remaining: 209858
06-30 23:49:00.781: I/Ghost Engine(21279): Read size: 8192, remaining: 201666
06-30 23:49:00.781: I/Ghost Engine(21279): Read size: 8192, remaining: 193474
06-30 23:49:00.781: I/Ghost Engine(21279): Read size: 8192, remaining: 185282
06-30 23:49:00.791: I/Ghost Engine(21279): Read size: 8192, remaining: 177090
06-30 23:49:00.791: I/Ghost Engine(21279): Read size: 8192, remaining: 168898
06-30 23:49:00.791: I/Ghost Engine(21279): Read size: 8192, remaining: 160706
06-30 23:49:00.791: I/Ghost Engine(21279): Read size: 8192, remaining: 152514
06-30 23:49:00.791: I/Ghost Engine(21279): Read size: 8192, remaining: 144322
06-30 23:49:00.791: I/Ghost Engine(21279): Read size: 8192, remaining: 136130
06-30 23:49:00.801: I/Ghost Engine(21279): Read size: 8192, remaining: 127938
06-30 23:49:00.801: I/Ghost Engine(21279): Read size: 8192, remaining: 119746
06-30 23:49:00.801: I/Ghost Engine(21279): Read size: 8192, remaining: 111554
06-30 23:49:00.801: I/Ghost Engine(21279): Read size: 8192, remaining: 103362
06-30 23:49:00.801: I/Ghost Engine(21279): Read size: 8192, remaining: 95170
06-30 23:49:00.811: I/Ghost Engine(21279): Read size: 8192, remaining: 86978
06-30 23:49:00.811: I/Ghost Engine(21279): Read size: 8192, remaining: 78786
06-30 23:49:00.811: I/Ghost Engine(21279): Read size: 8192, remaining: 70594
06-30 23:49:00.821: I/Ghost Engine(21279): Read size: 8192, remaining: 62402
06-30 23:49:00.821: I/Ghost Engine(21279): Read size: 8192, remaining: 54210
06-30 23:49:00.831: I/Ghost Engine(21279): Read size: 8192, remaining: 46018
06-30 23:49:00.831: I/Ghost Engine(21279): Read size: 8192, remaining: 37826
06-30 23:49:00.831: I/Ghost Engine(21279): Read size: 8192, remaining: 29634
06-30 23:49:00.841: I/Ghost Engine(21279): Read size: 8192, remaining: 21442
06-30 23:49:00.841: I/Ghost Engine(21279): Read size: 8192, remaining: 13250
06-30 23:49:00.841: I/Ghost Engine(21279): Read size: 8192, remaining: 5058
06-30 23:49:00.851: I/Ghost Engine(21279): Read size: 5042, remaining: 16
06-30 23:49:00.861: I/Ghost Engine(21279): Pixel 0: 0.000000 0.000000 1197.166992 1420.242434
06-30 23:49:00.861: I/Ghost Engine(21279): Pixel 1: 0.000000 0.000000 1197.166992 1420.242434
06-30 23:49:00.861: I/Ghost Engine(21279): Pixel 2: 0.000000 0.000000 1197.166992 1420.242434
06-30 23:49:00.861: I/Ghost Engine(21279): Pixel 3: 0.000000 0.000000 1197.166992 1420.242434
06-30 23:49:00.861: I/Ghost Engine(21279): Pixel 4: 0.000000 0.000000 1197.166992 1420.242434
06-30 23:49:00.861: I/Ghost Engine(21279): Pixel 5: 0.000000 0.000000 1197.166992 1420.242434
06-30 23:49:00.861: I/Ghost Engine(21279): Pixel 6: 0.000000 0.000000 1197.166992 1420.242434
06-30 23:49:00.861: I/Ghost Engine(21279): Pixel 7: 0.000000 0.000000 1197.166992 1420.242434
06-30 23:49:00.861: I/Ghost Engine(21279): Pixel 8: 0.000000 0.000000 1197.166992 1420.242434
06-30 23:49:00.861: I/Ghost Engine(21279): Pixel 9: 0.000000 0.000000 1197.166992 1420.242434
06-30 23:49:00.871: I/Ghost Engine(21279): Image id: 1.
06-30 23:49:00.871: I/Ghost Engine(21279): Image dimensions: 1024 x 1024
06-30 23:49:00.871: I/Ghost Engine(21279): Image size: 4194304

Any ideas where everything might start to go wrong?
Thanks, Martin.

  • 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-06T17:48:00+00:00Added an answer on June 6, 2026 at 5:48 pm

    Found the problem:

    "Pixel %d: %f %f %f %f"
    

    Should have been:

    "Pixel %d: %d %d %d %d"
    

    Apparently the image is loaded fine.

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

Sidebar

Related Questions

Trying to load a file into python. It's a very big file (1.5Gb), but
i have a .png image that i am trying to display as a UIBarButtonItem
I have problems when I'm trying to do numerical differentiation in Matlab. But my
I'm trying out KO but have problems getting observableArray to work, the code is
I have been trying to learn Erlang and have been running into some problems
I'm trying to build firefox but I'm having some problems. I currently have Visual
I have an image file , as C:/44637landscapes-2007.jpg I want to load this file
I trying to load BitmapImage from bmp file. When i load png or jpg
I have a problem with out of memory when I'm trying load a few
Ok I have this problem I'm trying to use Jquery to load a partial

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.