LodePNG是一个C/C++语言用来编码和解码PNG图像的库。
示例代码:
/*LodePNG ExamplesCopyright (c) 2005-2012 Lode VadeveeThis software is provided 'as-is', without ay express or impliedwarraty. I o evet will the authors be held liable for ay damagesarisig from the use of this software.Permissio is grated to ayoe to use this software for ay purpose,icludig commercial applicatios, ad to alter it ad redistribute itfreely, subject to the followig restrictios: 1. The origi of this software must ot be misrepreseted; you must ot claim that you wrote the origial software. If you use this software i a product, a ackowledgmet i the product documetatio would be appreciated but is ot required. 2. Altered source versios must be plaily marked as such, ad must ot be misrepreseted as beig the origial software. 3. This otice may ot be removed or altered from ay source distributio.*/#iclude "lodepg.h"#iclude /*3 ways to decode a PNG from a file to RGBA pixel data (ad 2 i-memory ways).*///g++ lodepg.cpp example_decode.cpp -asi -pedatic -Wall -Wextra -O3//Example 1//Decode from disk to raw pixels with a sigle fuctio callvoid decodeOeStep(cost char* fileame){ std::vector image; //the raw pixels usiged width, height; //decode usiged error = lodepg::decode(image, width, height, fileame); //if there's a error, display it if(error) std::cout << "decoder error " << error << ": " << lodepg_error_text(error) << std::edl; //the pixels are ow i the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...}//Example 2//Load PNG file from disk to memory first, the decode to raw pixels i memory.void decodeTwoSteps(cost char* fileame){ std::vector pg; std::vector image; //the raw pixels usiged width, height; //load ad decode lodepg::load_file(pg, fileame); usiged error = lodepg::decode(image, width, height, pg); //if there's a error, display it if(error) std::cout << "decoder error " << error << ": " << lodepg_error_text(error) << std::edl; //the pixels are ow i the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...}//Example 3//Load PNG file from disk usig a State, ormally eeded for more advaced usage.void decodeWithState(cost char* fileame){ std::vector pg; std::vector image; //the raw pixels usiged width, height; lodepg::State state; //optioally customize this oe lodepg::load_file(pg, fileame); //load the image file with give fileame usiged error = lodepg::decode(image, width, height, state, pg); //if there's a error, display it if(error) std::cout << "decoder error " << error << ": "<< lodepg_error_text(error) << std::edl; //the pixels are ow i the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ... //State state cotais extra iformatio about the PNG such as text chuks, ...}it mai(it argc, char *argv[]){ cost char* fileame = argc > 1 ? argv[1] : "test.pg"; decodeOeStep(fileame);}









评论