l.html / png_read_pngpng_ptr info_ptr PNG_TRANSFORM_EXPAND 0 width info_ptr-gtwidth height info_ptr-gtheight unsigned char rgba new unsigned charwidth height 4 //each pixelRGBA has 4bytes png_bytep row_pointers png_get_rowspng_ptr info_ptr //Original PNG pixel data stored from top-left corner BUT OGLES Texture drawing isfrom bottom-left corner //int pos 0 //forint row 0 row lt height row // //forint col 0 col lt 4 width col 4 // //rgbapos row_pointersrowcol // red //rgbapos row_pointersrowcol 1 // green //rgbapos row_pointersrowcol 2 // blue //rgbapos row_pointersrowcol 3 // alpha // // //unlike store the pixel data from top-left corner store them from bottom-left corner forOGLES Texture drawing... int pos width height 4 - 4 width forint row 0 row lt height row forint col 0 col lt 4 width col 4 rgbapos row_pointersrowcol // red rgbapos row_pointersrowcol 1 // green rgbapos row_pointersrowcol 2 // blue rgbapos row_pointersrowcol 3 // alpha pospos - width 42 //move the pointer back two rows ImageInfo imageInfo ImageInfokdMallocsizeofImageInfo imageInfo-gtpixelData rgba imageInfo-gtimageHeight height imageInfo-gtimageWidth width //clean up after the read and free any memory allocated png_destroy_read_structamppng_ptr ampinfo_ptr NULL fclosefile return imageInfo//----------------------------------------------------------------------------------------------------------------int createPNGTextureFromStreamconst u8 pixelData const u32amp dataSize GLuint textureID glEnableGL_TEXTURE_2D glGenTextures1amptextureID glBindTextureGL_TEXTURE_2DtextureID ImageInfo imageInfo decodePNGFromStreampixelData dataSize; glTexImage2DGL_TEXTURE_2D0GL_RGBAimageInfo-gtimageWidthimageInfo-gtimageHeight0 GL_RGBAGL_UNSIGNED_BYTEimageInfo-gtpixelData glTexParameteriGL_TEXTURE_2DGL_TEXTURE_MAG_FILTERGL_NEAREST glTexParameteriGL_TEXTURE_2DGL_TEXTURE_MIN_FILTERGL_NEAREST delete imageInfo-gtpixelData delete imageInfo return textureID//----------------------------------------------------------------------------------------------------------------void main //Testcase1: decoding png data from a raw png bufferstream and create thecorresponding Texture //假设我们从某个地方可以拿到一个 unsigned char 的 PNG 数据源 pixelData. int texId createPNGTextureFromStreampixelData dataSize //Testcase2: decoding png data from a given png file and and create the correspondingTexture char fileName quotexample.pngquot int texId createPNGTextureFromFilefileName //现在我们就可以用创建出来的 textureID 来绘制纹理了。
。
。
。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------附录 让我们打开 pnglib 下面的一个 example.c/ Read a PNG file. You may want to return an error code if the read fails depending upon the failure. There are two quotprototypesquot given here - one where we are given the filename and we need to open the file and the other where we are given an open file possibly with some or all of the magic bytes read - see comments above./ifdef open_file / prototype 1 /void read_pngchar file_name / We need to open the file /定义两个很重要的 pngwen 文件指针。
png_info 里面放的是 pngchuck 和 png 的数据。
png_structp png_ptrpng_infop info_ptr这些是读出图像需要的参数,图像高。
宽、深度、图像类型、交错类型。
unsigned int sig_read 0png_uint_32 width heightint bit_depth color_type interlace_typeFILE fp打开文件,把文件指针给 png_prtif fp fopenfile_name quotrbquot NULLreturn ERRORelse no_open_file / prototype 2 /void read_pngFILE fp unsigned int sig_read / file is already open /png_structp png_ptrpng_infop info_ptrpng_uint_32 width heightint bit_depth color_type interlace_typeendif no_open_file / only use one prototype // Create and initialize the png_struct with the desired error handler functions. If you want to use the default stderr and longjump method you can supply NULL for the last three parameters. We also supply the the compiler header file version so that we know if the application was compiled with a compatible version of the library. REQUIRED/png_ptr png_create_read_structPNG_LIBPNG_VER_STRINGpng_voidp user_error_ptr user_error_fn user_warning_fnif png_ptr NULLfclosefpreturn ERROR/ Allocate/initialize the memory for image information. REQUIRED. /info_ptr png_create_info_structpng_ptrif info_ptr NULLfclosefppng_destroy_read_structamppng_ptr png_infopp_NULL png_infopp_NULLreturn ERROR/ Set error handling if you are using the setjmp/longjmp method this is the normal method of doing things with libpng. REQUIRED unless you set up your own error handlers in the png_create_read_struct earlier./if setjmppng_jmpbufpng_ptr/ Free all of the memory associated with the png_ptr and info_ptr /png_destroy_read_structamppng_ptr ampinfo_ptr png_infopp_NULLfclosefp/ If we get here we had a problem reading the file /return ERROR/ One of the following I/O initialization methods is REQUIRED /ifdef streams / PNG file I/O method 1 // Set up the input control if you are using standard C streams /png_init_iopng_ptr fpelse no_streams / PNG file I/O method 2 // If you are using replacement read functions instead of calling png_init_io here you would call:/png_set_read_fnpng_ptr void user_io_ptr user_read_fn/ where user_io_ptr is a structure you want available to the callbacks /endif no_streams / Use only one I/O method // If we have already read some of the signature /png_set_sig_bytespng_ptr sig_readifdef hilevel/ If you have enough memory to read in the entire image at once and you need to specify only transforms that can be controlled with one of the PNG_TRANSFORM_ bits this presently excludes dithering filling
上一篇:
【精品】B语言程序报告21啊撒
下一篇:
村落共同体的核心架构研究