EG_IMAGE * egDecodePNG(IN UINT8 *FileData, IN UINTN FileDataLength, IN UINTN IconSize, IN BOOLEAN WantAlpha) { EG_IMAGE *NewImage = NULL; unsigned Error, Width, Height; EG_PIXEL *PixelData; lode_color *LodeData; UINTN i; Error = lodepng_decode_memory((unsigned char **) &PixelData, &Width, &Height, (unsigned char*) FileData, (size_t) FileDataLength, LCT_RGBA, 8); if (Error) { return NULL; } // allocate image structure and buffer NewImage = egCreateImage(Width, Height, WantAlpha); if ((NewImage == NULL) || (NewImage->Width != Width) || (NewImage->Height != Height)) return NULL; LodeData = (lode_color *) PixelData; // Annoyingly, EFI and LodePNG use different ordering of RGB values in // their pixel data representations, so we've got to adjust them.... for (i = 0; i < (NewImage->Height * NewImage->Width); i++) { NewImage->PixelData[i].r = LodeData[i].red; NewImage->PixelData[i].g = LodeData[i].green; NewImage->PixelData[i].b = LodeData[i].blue; if (WantAlpha) NewImage->PixelData[i].a = LodeData[i].alpha; } lodepng_free(PixelData); return NewImage; } // EG_IMAGE * egDecodePNG()
//Test LodePNG encoding and decoding the encoded result, using the C interface void doCodecTest(Image& image) { unsigned char* encoded = 0; size_t encoded_size = 0; unsigned char* decoded = 0; unsigned decoded_w; unsigned decoded_h; double t_enc0 = getTime(); unsigned error_enc = lodepng_encode_memory(&encoded, &encoded_size, &image.data[0], image.width, image.height, image.colorType, image.bitDepth); double t_enc1 = getTime(); assertEquals(0, error_enc, "encoder error C"); double t_dec0 = getTime(); for(int i = 0; i < NUM_DECODE; i++) { unsigned error_dec = lodepng_decode_memory(&decoded, &decoded_w, &decoded_h, encoded, encoded_size, image.colorType, image.bitDepth); assertEquals(0, error_dec, "decoder error C"); } double t_dec1 = getTime(); assertEquals(image.width, decoded_w); assertEquals(image.height, decoded_h); printValue("encoding time", t_enc1 - t_enc0, "s"); std::cout << "compression: " << ((double)(encoded_size) / (double)(image.data.size())) * 100 << "%" << " ratio: " << ((double)(image.data.size()) / (double)(encoded_size)) << " size: " << encoded_size << std::endl; total_enc_size += encoded_size; total_enc_time += (t_enc1 - t_enc0); if(NUM_DECODE> 0) printValue("decoding time", t_dec1 - t_dec0, "/", NUM_DECODE, " s"); total_dec_time += (t_dec1 - t_dec0); std::cout << std::endl; //LodePNG_saveFile(encoded, encoded_size, "test.png"); free(encoded); free(decoded); }
DeletablePixels loadPng(const Path &path, int &w, int &h, int &channels) { uint64 size = FileUtils::fileSize(path); InputStreamHandle in = FileUtils::openInputStream(path); if (size == 0 || !in) return makeVoidPixels(); std::unique_ptr<uint8[]> file(new uint8[size_t(size)]); FileUtils::streamRead(in, file.get(), size_t(size)); uint8 *dst = nullptr; uint32 uw, uh; lodepng_decode_memory(&dst, &uw, &uh, file.get(), size_t(size), LCT_RGBA, 8); if (!dst) return makeVoidPixels(); w = uw; h = uh; channels = 4; return DeletablePixels(dst, free); }
void parg_asset_preload(parg_token id) { if (!_pngsuffix) { _pngsuffix = sdsnew(".png"); } sds filename = parg_token_to_sds(id); parg_buffer* buf = parg_buffer_from_path(filename); parg_assert(buf, "Unable to load asset"); if (sdslen(filename) > 4) { sds suffix = sdsdup(filename); sdsrange(suffix, -4, -1); if (!sdscmp(suffix, _pngsuffix)) { unsigned char* decoded; unsigned dims[3] = {0, 0, 4}; unsigned char* filedata = parg_buffer_lock(buf, PARG_READ); unsigned err = lodepng_decode_memory(&decoded, &dims[0], &dims[1], filedata, parg_buffer_length(buf), LCT_RGBA, 8); parg_assert(err == 0, "PNG decoding error"); parg_buffer_free(buf); int nbytes = dims[0] * dims[1] * dims[2]; buf = parg_buffer_alloc(nbytes + 12, PARG_CPU); int* ptr = parg_buffer_lock(buf, PARG_WRITE); *ptr++ = dims[0]; *ptr++ = dims[1]; *ptr++ = dims[2]; memcpy(ptr, decoded, nbytes); free(decoded); parg_buffer_unlock(buf); } sdsfree(suffix); } if (!_asset_registry) { _asset_registry = kh_init(assmap); } int ret; int iter = kh_put(assmap, _asset_registry, id, &ret); kh_value(_asset_registry, iter) = buf; }
void CPicture::LoadPNG( CDataStream& dataStream ) { lodepng_decode_memory( (unsigned char**)&m_pPixelData, &m_width, &m_height, (unsigned char*)dataStream.GetCursorData(), dataStream.GetSize(), LodePNGColorType::LCT_RGBA, 8 ); m_format = ePF_R8G8B8A8; m_mipMapCount = 0; }