diff --git a/cocos/renderer/CCTexture2D.cpp b/cocos/renderer/CCTexture2D.cpp index 49273110e6..57f447c610 100644 --- a/cocos/renderer/CCTexture2D.cpp +++ b/cocos/renderer/CCTexture2D.cpp @@ -322,6 +322,15 @@ void Texture2D::convertRGBA8888ToRGB565(const unsigned char* data, ssize_t dataL } } +// RRRRRRRRGGGGGGGGBBBBBBBB -> AAAAAAAA +void Texture2D::convertRGB888ToA8(const unsigned char* data, ssize_t dataLen, unsigned char* outData) +{ + for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3) + { + *outData++ = (data[i] * 299 + data[i + 1] * 587 + data[i + 2] * 114 + 500) / 1000; //A = (R*299 + G*587 + B*114 + 500) / 1000 + } +} + // RRRRRRRRGGGGGGGGBBBBBBBB -> IIIIIIII void Texture2D::convertRGB888ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData) { @@ -917,6 +926,11 @@ Texture2D::PixelFormat Texture2D::convertRGB888ToFormat(const unsigned char* dat *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGB888ToRGB565(data, dataLen, *outData); break; + case PixelFormat::A8: + *outDataLen = dataLen/3; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); + convertRGB888ToA8(data, dataLen, *outData); + break; case PixelFormat::I8: *outDataLen = dataLen/3; *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); diff --git a/cocos/renderer/CCTexture2D.h b/cocos/renderer/CCTexture2D.h index c88dbe7fe7..6a04e69b99 100644 --- a/cocos/renderer/CCTexture2D.h +++ b/cocos/renderer/CCTexture2D.h @@ -489,6 +489,7 @@ private: //RGB888 to XXX static void convertRGB888ToRGBA8888(const unsigned char* data, ssize_t dataLen, unsigned char* outData); static void convertRGB888ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData); + static void convertRGB888ToA8(const unsigned char* data, ssize_t dataLen, unsigned char* outData); static void convertRGB888ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData); static void convertRGB888ToAI88(const unsigned char* data, ssize_t dataLen, unsigned char* outData); static void convertRGB888ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData);