我在尝试将 png 文件加载到我的应用程序时遇到问题。似乎可以很好地加载标题,并识别出图像是 png,但由于某种原因,输出具有相同的值,尽管图像实际上不是一种颜色。让我烦恼的另一件事是 16 个字节未读。
我的代码:
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;
}
输出:
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
任何事情都可能开始出错的想法?谢谢,马丁。