Cctexturecache Type source Analysis (2)

Source: Internet
Author: User
Tags emscripten

Cctexturecache class Source Analysis (2): In the Cctexturecache class source Analysis (1), we analyzed the Cctexturecache how to implement the texture cache, but in the process of analysis, we ignore a lot of things, such as Ccimage class such as him load texture picture, this one we analyze Ccimage: Source analysis: 1, ccimage inherit from Ccobject 2, member variables, these variables need us to parse the picture file to get unsigned char *m_pdata; Picture Data cc_synthesize_readonly (unsigned short, m_nwidth, Width); Wide height cc_synthesize_readonly (unsigned short, m_nheight, Height); cc_synthesize_readonly (int, m_nbitspercomponent, bitspercomponent); The number of bits per color component bool M_bhasalpha; Whether there is Alpha component 3,//For. PVR and. PKM Picture format files require special handling, followed by parsing if (Std::string::npos! = Lowercase.find (". PVR")) {texture =    This->addpvrimage (Fullpath.c_str ()); } else if (Std::string::npos! = Lowercase.find (". PKM")) {//ETC1 file format, only supportted on androidtexture = th    Is->addetcimage (Fullpath.c_str ()); } else {//for the following picture formats, we need to use the Ccimage class to read//Picture file data into memory and analyze it, because each image format//file stores picture information differently, such as the size information of the few bytes that hold the picture,//Those bytes To store the image color information, we need to do different analysis processing according to each file format,//So as to get the information we need. From here you can also see the COCOS2DX supported image formats: CCIMAGE::EIMAGEFOrmat Eimageformat = ccimage::kfmtunknown;if (Std::string::npos! = Lowercase.find (". png")) {Eimageformat = CCImage::kFm Tpng;} else if (std::string::npos! = Lowercase.find (". jpg") | | Std::string::npos! = lowercase.find (". jpeg")) {Eimageformat = C Cimage::kfmtjpg;} else if (std::string::npos! = Lowercase.find (". tif") | | Std::string::npos! = lowercase.find (". Tiff")) {Eimageformat = C Cimage::kfmttiff;} else if (std::string::npos! = Lowercase.find (". Webp")) {Eimageformat = CCIMAGE::KFMTWEBP;} Pimage = new Ccimage (); cc_break_if (NULL = = pimage); bool BRet = Pimage->initwithimagefile (Fullpath.c_str (), Eimageformat); Cc_break_if (!bret);----initwithimagefile---->>strpath: File path eimgfmt: Picture format bool Ccimage::initwithimagefile ( const char * strpath, Eimageformat eimgfmt/* = efmtpng*/) {bool BRet = false; #ifdef Emscripten ....    This part doesn't matter. #else//Read the picture file data unsigned long nSize = 0;    std::string FullPath = Ccfileutils::sharedfileutils ()->fullpathforfilename (strpath); Unsigned char* pbuffer = Ccfileutils::sharedfileutils ()->getfiledata (Fullpath.c_str (), "RB", &nsize);    if (pbuffer! = NULL && nSize > 0) {bRet = Initwithimagedata (pbuffer, NSize, eimgfmt); } cc_safe_delete_array (pbuffer); #endif//Emscripten return bRet;} ------initwithimagedata------>>bool ccimage::initwithimagedata (void * pData, int nDa TaLen, Eimageformat efmt/* = esrcfmtpng*/, int nwidth/* =    0*/, int nheight/* = 0*/, int nbitspercomponent/* = 8*/) {    BOOL BRet = false;            do {cc_break_if (! PData | | ndatalen <= 0);//Depending on the image file format, call the different analytic functions IF (kfmtpng = = efmt) { The PNG image is parsed, the _initwithpngdata function calls the PNG parsing library//to the PNG file parsing, and then get all the information we need bRet = _initwithpngdata (PData, NDa            TaLen);        Break    } else if (kfmtjpg = = efmt) {        BRet = _initwithjpgdata (PData, Ndatalen);        Break            } else if (Kfmttiff = = efmt) {BRet = _initwithtiffdata (PData, Ndatalen);        Break } #if (Cc_target_platform! = CC_PLATFORM_WINRT) && (cc_target_platform! = CC_PLATFORM_WP8) Else if (KFMTWEBP            = = efmt) {BRet = _initwithwebpdata (PData, Ndatalen);        Break  } #endif Else if (kfmtrawdata = = efmt) {BRet = _initwithrawdata (PData, Ndatalen, nwidth, nheight,            Nbitspercomponent, false);        Break            } else {//If it is a PNG file buffer.                if (Ndatalen > 8) {unsigned char* Phead = (unsigned char*) pData;                    if (phead[0] = = 0x89 && phead[1] = = 0x50 && phead[2] = = 0x4E          && phead[3] = = 0x47 && phead[4] = = 0x0D          && phead[5] = = 0x0A && phead[6] = = 0x1A && phead[7                    ] = = 0x0A) {BRet = _initwithpngdata (PData, Ndatalen);                Break            }}//If it is a TIFF file buffer.                if (Ndatalen > 2) {unsigned char* Phead = (unsigned char*) pData; if ((phead[0] = = 0x49 && phead[1] = = 0x49) | |                (Phead[0] = = 0x4d && phead[1] = = 0x4d))                    {BRet = _initwithtiffdata (PData, Ndatalen);                Break            }}//If it is a JPEG file buffer.                if (Ndatalen > 2) {unsigned char* Phead = (unsigned char*) pData; if (phead[0] = = 0xFF && phead[1] = = 0xd8) {BRet = _initwi ThjpgData (PData, Ndatalen);                Break    }}}} while (0); return bRet;}


Summarize:
Ccimage's job is to invoke different underlying libraries based on different image formats.
such as the Png,jpg Analytic library, through these libraries to the image file analysis,
So that we can get all the information we need, here's a question, through the underlying library
What format of data is stored after parsing a picture file?
Because I am in the Cctexturecache class source analysis (1) through the following line
Convert "Rrrrrrrrrggggggggbbbbbbbbaaaaaaaa" to "RRRRGGGGBBBBAAAA"
Guessing that the storage of the data is 8 bits per color component, but because I am unfamiliar with the parsing of PNG images,
So there is no good evidence to prove the above speculation, so put this doubt on this.

Cctexturecache Type source Analysis (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.