static int cb_nbio_generic(nbio_handle_t *nbio, size_t *len) { void *ptr = NULL; if (!nbio->image.handle) { nbio->image.cb = NULL; return -1; } ptr = nbio_get_ptr(nbio->handle, len); if (!ptr) { rpng_nbio_load_image_free(nbio->image.handle); nbio->image.handle = NULL; nbio->image.cb = NULL; return -1; } rpng_set_buf_ptr(nbio->image.handle, (uint8_t*)ptr); nbio->image.pos_increment = (*len / 2) ? (*len / 2) : 1; nbio->image.processing_pos_increment = (*len / 4) ? (*len / 4) : 1; if (!rpng_nbio_load_image_argb_start(nbio->image.handle)) { rpng_nbio_load_image_free(nbio->image.handle); return -1; } nbio->image.is_blocking = false; nbio->image.is_finished = false; nbio->is_finished = true; return 0; }
static bool rpng_load_image_argb(const char *path, uint32_t **data, unsigned *width, unsigned *height) { int retval; size_t file_len; bool ret = true; rpng_t *rpng = NULL; void *ptr = NULL; struct nbio_t* handle = (struct nbio_t*)nbio_open(path, NBIO_READ); if (!handle) goto end; nbio_begin_read(handle); while (!nbio_iterate(handle)); ptr = nbio_get_ptr(handle, &file_len); if (!ptr) { ret = false; goto end; } rpng = rpng_alloc(); if (!rpng) { ret = false; goto end; } if (!rpng_set_buf_ptr(rpng, (uint8_t*)ptr)) { ret = false; goto end; } if (!rpng_start(rpng)) { ret = false; goto end; } while (rpng_iterate_image(rpng)); if (!rpng_is_valid(rpng)) { ret = false; goto end; } do { retval = rpng_process_image(rpng, (void**)data, file_len, width, height); }while(retval == IMAGE_PROCESS_NEXT); if (retval == IMAGE_PROCESS_ERROR || retval == IMAGE_PROCESS_ERROR_END) ret = false; end: if (handle) nbio_free(handle); if (rpng) rpng_free(rpng); rpng = NULL; if (!ret) free(*data); return ret; }