//根据现有CCSprite,变亮和变灰 CCSprite* PixelGraySprite::graylightWithCCSprite(CCSprite* oldSprite,bool isLight) { //CCSprite转成CCimage CCPoint p = oldSprite->getAnchorPoint(); oldSprite->setAnchorPoint(ccp(0,0)); CCRenderTexture *outTexture = CCRenderTexture::create((int)oldSprite->getContentSize().width,(int)oldSprite->getContentSize().height); outTexture->begin(); oldSprite->visit(); outTexture->end(); oldSprite->setAnchorPoint(p); CCImage* finalImage = outTexture->newCCImage(); unsigned char *pData = finalImage->getData(); int iIndex = 0; if(isLight) { for (int i = 0; i < finalImage->getHeight(); i ++) { for (int j = 0; j < finalImage->getWidth(); j ++) { // highlight int iHightlightPlus = 50; int iBPos = iIndex; unsigned int iB = pData[iIndex]; iIndex ++; unsigned int iG = pData[iIndex]; iIndex ++; unsigned int iR = pData[iIndex]; iIndex ++; //unsigned int o = pData[iIndex]; iIndex ++; //原来的示例缺少 iB = (iB + iHightlightPlus > 255 ? 255 : iB + iHightlightPlus); iG = (iG + iHightlightPlus > 255 ? 255 : iG + iHightlightPlus); iR = (iR + iHightlightPlus > 255 ? 255 : iR + iHightlightPlus); // iR = (iR < 0 ? 0 : iR); // iG = (iG < 0 ? 0 : iG); // iB = (iB < 0 ? 0 : iB); pData[iBPos] = (unsigned char)iB; pData[iBPos + 1] = (unsigned char)iG; pData[iBPos + 2] = (unsigned char)iR; } } }else{ for (int i = 0; i < finalImage->getHeight(); i ++) { for (int j = 0; j < finalImage->getWidth(); j ++) { // gray int iBPos = iIndex; unsigned int iB = pData[iIndex]; iIndex ++; unsigned int iG = pData[iIndex]; iIndex ++; unsigned int iR = pData[iIndex]; iIndex ++; //unsigned int o = pData[iIndex]; iIndex ++; //原来的示例缺少 unsigned int iGray = 0.3 * iR + 0.4 * iG + 0.2 * iB; pData[iBPos] = pData[iBPos + 1] = pData[iBPos + 2] = (unsigned char)iGray; } } } CCTexture2D *texture = new CCTexture2D; texture->initWithImage(finalImage); CCSprite* newSprite = CCSprite::createWithTexture(texture); delete finalImage; texture->release(); return newSprite; }
static void* loadImage(void* data) { AsyncStruct *pAsyncStruct = NULL; while (true) { // create autorelease pool for iOS CCThread thread; thread.createAutoreleasePool(); // wait for rendering thread to ask for loading if s_pAsyncStructQueue is empty int semWaitRet = sem_wait(s_pSem); if( semWaitRet < 0 ) { CCLOG( "CCTextureCache async thread semaphore error: %s\n", strerror( errno ) ); break; } std::queue<AsyncStruct*>* pQueue = s_pAsyncStructQueue; pthread_mutex_lock(&s_asyncStructQueueMutex);// get async struct from queue if (pQueue->empty()) { pthread_mutex_unlock(&s_asyncStructQueueMutex); if (need_quit) break; else continue; } else { pAsyncStruct = pQueue->front(); pQueue->pop(); pthread_mutex_unlock(&s_asyncStructQueueMutex); } const char* filename = pAsyncStruct->filename.c_str(); // compute image type CCImage::EImageFormat imageType = CCImage::computeImageFormatType(pAsyncStruct->filename); if (imageType == CCImage::kFmtUnKnown) { CCLOG("unsupported format %s",filename); delete pAsyncStruct; continue; } // generate image CCImage* pImage = pAsyncStruct->image; if (pImage && !pImage->initWithImageFileThreadSafe(filename, imageType)) { pAsyncStruct->image = NULL; CC_SAFE_RELEASE(pImage); CCLOG("can not load %s", filename); continue; } unsigned char* tempData = CCImage::convertToRequiredFormat(pImage); if (tempData != pImage->getData()) { CCSize size = CCSizeMake(pImage->getWidth(), pImage->getHeight()); size_t bpp = pImage->getBitsPerComponent(); CCTexture2DPixelFormat pixelFormat = pImage->hasAlpha() ? CCTexture2D::getDefaultAlphaPixelFormat() : (bpp >= 8 ? kCCTexture2DPixelFormat_RGB888 : kCCTexture2DPixelFormat_RGB565); pAsyncStruct->image = NULL; CC_SAFE_RELEASE(pImage); pAsyncStruct->data = tempData; pAsyncStruct->size = size; pAsyncStruct->pixelFormat = pixelFormat; } // generate image info ImageInfo* pImageInfo = new ImageInfo(); pImageInfo->asyncStruct = pAsyncStruct; pImageInfo->imageType = imageType; // put the image info into the queue pthread_mutex_lock(&s_ImageInfoMutex); s_pImageQueue->push(pImageInfo); pthread_mutex_unlock(&s_ImageInfoMutex); } if( s_pSem != NULL ) { #if CC_ASYNC_TEXTURE_CACHE_USE_NAMED_SEMAPHORE sem_unlink(CC_ASYNC_TEXTURE_CACHE_SEMAPHORE); sem_close(s_pSem); #else sem_destroy(s_pSem); #endif s_pSem = NULL; delete s_pAsyncStructQueue; delete s_pImageQueue; } return 0; }