Code: Select all
Image* loadPngImage(const char* filename, const char* rarfile)
{
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
png_uint_32 width, height, x, y;
int bit_depth, color_type, interlace_type;
u32* line;
FILE *fp;
hardrarfile = (char*)rarfile;
Image* image = (Image*) malloc(sizeof(Image));
if (!image) return NULL;
if ((fp = fopen(filename, "rb")) == NULL) return NULL;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
free(image);
fclose(fp);
return NULL;;
}
png_set_read_fn(png_ptr, NULL, user_read_data_fn);
png_set_error_fn(png_ptr, (png_voidp) NULL, (png_error_ptr) NULL, user_warning_fn);
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
free(image);
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return NULL;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, sig_read);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL);
if (width > 512 || height > 512) {
free(image);
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return NULL;
}
image->imageWidth = width;
image->imageHeight = height;
image->textureWidth = getNextPower2(width);
image->textureHeight = getNextPower2(height);
png_set_strip_16(png_ptr);
png_set_packing(png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_gray_1_2_4_to_8(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
image->data = (Color*) memalign(16, image->textureWidth * image->textureHeight * sizeof(Color));
if (!image->data) {
free(image);
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return NULL;
}
line = (u32*) malloc(width * 4);
if (!line) {
free(image->data);
free(image);
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return NULL;
}
for (y = 0; y < height; y++) {
png_read_row(png_ptr, (u8*) line, png_bytep_NULL);
for (x = 0; x < width; x++) {
u32 color = line[x];
image->data[x + y * image->textureWidth] = color;
}
}
free(line);
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
return image;
}
So, as follows, this is my custom function to read the data from the rar file and right back into libpng.
Code: Select all
char *hardrarfile;
png_structp last_ptr;
char *data_ptr;
void user_read_data_fn(png_structp png_ptr, png_bytep data, png_size_t length) {
unsigned long data_size;
if (last_ptr != png_ptr->io_ptr) {
free(data_ptr);
urarlib_get(&data_ptr, &data_size, hardrarfile, (png_FILE_p)png_ptr->io_ptr, "MaySeptember8891");
last_ptr = (png_struct*)png_ptr->io_ptr;
pspDebugScreenPrintf("^ READ RAR SUCESS\n");
} else { pspDebugScreenPrintf("^ SKIPPING READ RAR DUE TO PREVIOUS ACCESS\n"); }
strncpy((char*)data, (const char*)data_ptr, (int)length);
pspDebugScreenPrintf("^ STRCOPY TO PNG DATA SUCESS\n");
}
It prints out as such:
^ READ RAR SUCESS
^ STRCOPY TO PNG DATA SUCESS
^ SKIPPING READ RAR DUE TO PREVIOUS ACCESS
^ STRCOPY TO PNG DATA SUCESS
However, in my main.cpp file it does not continue after I attempt to load the PNG. It doesn't freeze, it just stops, and doesn't display the image.
Code: Select all
pspDebugScreenInit();
SetupCallbacks();
initGraphics();
Image* testimage;
printf("^ CALLING LOADPNG\n");
testimage = loadPngImage("data.rar", "rro.png");
printf("^ LOADPNG SUCESS\n^ CALLING BLIT TO SCREEN\n");
blitAlphaImageToScreen(0, 0, 480, 272, testimage, 0, 0);
flipScreen();
Hopefully this is enough information so in turn you can help me out. Anyone have any ideas? Thanks.