u8 *BAG_Display_LoadPng(const char *filename, int *wd, int *ht){ u8* buffer = NULL; u8* image = NULL; size_t buffersize = 0, imagesize = 0; LodePNG_Decoder pngDecoder; memset(&pngDecoder, 0, sizeof(LodePNG_Decoder)); if(!LodePNG_loadFile ( &buffer, &buffersize, filename )){ LodePNG_Decoder_init (&pngDecoder); LodePNG_decode( &pngDecoder, &image, &imagesize, buffer, buffersize ); /*decode the png*/ *wd = pngDecoder.infoPng.width; *ht = pngDecoder.infoPng.height; //_png_fillBuffer(object, image); } else{ //BAG_DBG_LibMsg(OPEN_FAIL_MSG, filename); printf("Error opening png file: %s\n", filename); if(image) free(image); image = NULL; } end: LodePNG_Decoder_cleanup(&pngDecoder); if(buffer) free(buffer); return image; }
unsigned char *RasterMapManager::DecodePNG(unsigned char *readBuffer, unsigned int length, unsigned int& width, unsigned int& height) { static unsigned char* image = 0; if (image != 0) { free(image); } size_t dummy_size; LodePNG_Decoder decoder; LodePNG_Decoder_init(&decoder); decoder.infoRaw.color.colorType = 2; LodePNG_decode(&decoder, &image, &dummy_size, &readBuffer[0], length); unsigned int error = decoder.error; width = decoder.infoPng.width; height = decoder.infoPng.height; LodePNG_Decoder_cleanup(&decoder); if (error) { printf("RasterMapManager: PNG codec error number %d\n", error); return NULL; } return image; }
static GLuint LoadTexture(const char* filename) { unsigned char* buffer; unsigned char* image; size_t buffersize, imagesize; LodePNG_Decoder decoder; LodePNG_loadFile(&buffer, &buffersize, filename); LodePNG_Decoder_init(&decoder); decoder.infoRaw.color.colorType = 0; decoder.infoRaw.color.bitDepth = 8; LodePNG_Decoder_decode(&decoder, &image, &imagesize, buffer, buffersize); pezCheck(!decoder.error, "error %u: %s\n", decoder.error, LodePNG_error_text(decoder.error)); int bpp = LodePNG_InfoColor_getBpp(&decoder.infoPng.color); int bitDepth = decoder.infoPng.color.bitDepth; int colorChannels = LodePNG_InfoColor_getChannels(&decoder.infoPng.color); pezCheck(bpp == 8 && bitDepth == 8 && colorChannels == 1); int w = decoder.infoPng.width; int h = decoder.infoPng.height; pezPrintString("Loaded %s (%d x %d) bufferSize = %d, imageSize = %d\n", filename, w, h, buffersize, imagesize); GLuint handle; glGenTextures(1, &handle); glBindTexture(GL_TEXTURE_2D, handle); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, image); free(buffer); free(image); LodePNG_Decoder_cleanup(&decoder); pezCheck(OpenGLError); return handle; }
static int load_image(char *filename, struct image_info *info, unsigned char *buf, ssize_t *buf_size) { int fd; long time = 0; /* measured ticks */ int w, h; /* used to center output */ LodePNG_Decoder *p_decoder = &decoder; unsigned char *memory, *memory_max, *image; size_t memory_size, file_size; /* cleanup */ memset(&disp, 0, sizeof(disp)); /* align buffer */ memory = (unsigned char *)((intptr_t)(buf + 3) & ~3); memory_max = (unsigned char *)((intptr_t)(memory + *buf_size) & ~3); memory_size = memory_max - memory; fd = rb->open(filename, O_RDONLY); if (fd < 0) { rb->splashf(HZ, "err opening %s: %d", filename, fd); return PLUGIN_ERROR; } file_size = rb->filesize(fd); DEBUGF("reading file '%s'\n", filename); if (!iv->running_slideshow) { rb->lcd_puts(0, 0, rb->strrchr(filename,'/')+1); rb->lcd_update(); } if (file_size > memory_size) { p_decoder->error = FILE_TOO_LARGE; rb->close(fd); } else { if (!iv->running_slideshow) { rb->lcd_putsf(0, 1, "loading %zu bytes", file_size); rb->lcd_update(); } /* load file to the end of the buffer */ image = memory_max - file_size; rb->read(fd, image, file_size); rb->close(fd); if (!iv->running_slideshow) { rb->lcd_puts(0, 2, "decoding image"); rb->lcd_update(); } #ifdef DISK_SPINDOWN else if (iv->immediate_ata_off) { /* running slideshow and time is long enough: power down disk */ rb->storage_sleep(); } #endif /* Initialize decoder context struct, set buffer decoder is free * to use. * Decoder assumes that raw image file is loaded at the very * end of the allocated buffer */ LodePNG_Decoder_init(p_decoder, memory, memory_size); /* read file header; file is loaded at the end * of the allocated buffer */ LodePNG_inspect(p_decoder, image, file_size); if (!p_decoder->error) { if (!iv->running_slideshow) { rb->lcd_putsf(0, 2, "image %dx%d", p_decoder->infoPng.width, p_decoder->infoPng.height); rb->lcd_putsf(0, 3, "decoding %d*%d", p_decoder->infoPng.width, p_decoder->infoPng.height); rb->lcd_update(); } /* the actual decoding */ time = *rb->current_tick; #ifdef HAVE_ADJUSTABLE_CPU_FREQ rb->cpu_boost(true); LodePNG_decode(p_decoder, image, file_size, iv->cb_progress); rb->cpu_boost(false); #else LodePNG_decode(p_decoder, image, file_size, iv->cb_progress); #endif /*HAVE_ADJUSTABLE_CPU_FREQ*/ time = *rb->current_tick - time; } } if (!iv->running_slideshow && !p_decoder->error) { rb->snprintf(print, sizeof(print), " %ld.%02ld sec ", time/HZ, time%HZ); rb->lcd_getstringsize(print, &w, &h); /* centered in progress bar */ rb->lcd_putsxy((LCD_WIDTH - w)/2, LCD_HEIGHT - h, print); rb->lcd_update(); } if (p_decoder->error) { if (p_decoder->error == FILE_TOO_LARGE || p_decoder->error == OUT_OF_MEMORY) { return PLUGIN_OUTOFMEM; } if (LodePNG_perror(p_decoder) != NULL) { rb->splash(HZ, LodePNG_perror(p_decoder)); } else if (p_decoder->error == TINF_DATA_ERROR) { rb->splash(HZ, "Zlib decompressor error"); } else { rb->splashf(HZ, "other error : %ld", p_decoder->error); } return PLUGIN_ERROR; } info->x_size = p_decoder->infoPng.width; info->y_size = p_decoder->infoPng.height; p_decoder->native_img_size = (p_decoder->native_img_size + 3) & ~3; disp_buf = p_decoder->buf + p_decoder->native_img_size; *buf_size = memory_max - disp_buf; return PLUGIN_OK; }