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; }