/*********************************************************************** * decode png file * const char *file_path = path to png file e.g. "/dev_hdd0/test.png" ***********************************************************************/ Buffer load_png(const char *file_path) { Buffer tmp; png_dec_info dec_ctx; // decryption handles void *buf_addr = NULL; // buffer for decoded png data // create png decoder create_decoder(&dec_ctx); // open png stream open_png(&dec_ctx, file_path); // set decode parameter set_dec_param(&dec_ctx); // alloc target buffer buf_addr = mem_alloc(png_w * png_h * 4); // decode png stream, into target buffer decode_png_stream(&dec_ctx, buf_addr); // close png stream PngDecClose(dec_ctx.main_h, dec_ctx.sub_h); // destroy png decoder PngDecDestroy(dec_ctx.main_h); // store png values tmp.addr = (uint32_t*)buf_addr; tmp.w = png_w; tmp.h = png_h; return tmp; }
int LoadPNG(PngDatas *png, char *filename) { int ret=-1; int mHandle; int sHandle; PngDecThreadInParam InThdParam; PngDecThreadOutParam OutThdParam; uint64_t build_malloc, build_free; // used to create the fake 32 bits addrs .opd for functions png_malloc() and png_free() PngDecInParam inParam; PngDecOutParam outParam; PngDecSrc src; uint32_t space_allocated; PngDecInfo DecInfo; uint64_t bytes_per_line; PngDecDataInfo DecDataInfo; InThdParam.enable = 0; InThdParam.ppu_prio = 512; InThdParam.spu_prio = 200; InThdParam.addr_malloc_func = build32_func_addr(png_malloc, &build_malloc); // (see sysmodule.h) InThdParam.addr_malloc_arg = 0; // no args: if you want one uses get32_addr() to get the 32 bit address (see sysmodule.h) InThdParam.addr_free_func = build32_func_addr(png_free, &build_free); // (see sysmodule.h) InThdParam.addr_free_arg = 0; // no args if you want one uses get32_addr() to get the 32 bit address (see sysmodule.h) ret= PngDecCreate(&mHandle, &InThdParam, &OutThdParam); png->bmp_out= NULL; if(ret == 0) { memset(&src, 0, sizeof(PngDecSrc)); if(filename) { src.stream_select = PNGDEC_FILE; src.addr_file_name = get32_addr(filename); } else { src.stream_select = PNGDEC_BUFFER; src.addr_stream_ptr = get32_addr((void *) png->png_in); src.stream_size = png->png_size; } src.enable = PNGDEC_DISABLE; ret= PngDecOpen(mHandle, &sHandle, &src, &space_allocated); if(ret == 0) { ret = PngDecReadHeader(mHandle, sHandle, &DecInfo); if(ret == 0) { inParam.addr_cmd_ptr = 0; inParam.mode = PNGDEC_TOP_TO_BOTTOM; inParam.color_space = PNGDEC_ARGB; inParam.bit_depth = 8; inParam.pack_flag = 1; if((DecInfo.color_space == PNGDEC_GRAYSCALE_ALPHA) || (DecInfo.color_space == PNGDEC_RGBA) || (DecInfo.chunk_info & 0x10)) inParam.alpha_select = 0; else inParam.alpha_select = 1; inParam.color_alpha = 0xff; ret = PngDecSetParameter(mHandle, sHandle, &inParam, &outParam); } if(ret == 0) { png->wpitch= outParam.width* 4; bytes_per_line = (uint64_t) png->wpitch; png->bmp_out= malloc(png->wpitch * outParam.height); if(!png->bmp_out) { ret=-1; // out of memory } else { memset(png->bmp_out, 0, png->wpitch * outParam.height); ret = PngDecDecodeData(mHandle, sHandle, png->bmp_out, &bytes_per_line, &DecDataInfo); if((ret == 0) && (DecDataInfo.status == 0)){ png->width = outParam.width; png->height = outParam.height; ret=0; // ok :) } } } PngDecClose(mHandle, sHandle); } if(ret && png->bmp_out) { free(png->bmp_out); png->bmp_out= NULL; } PngDecDestroy(mHandle); } return ret; }