From b58b27df3b1b705ae4d928c42a83695e78dc4dc4 Mon Sep 17 00:00:00 2001 From: ColinCollins <35832931+ColinCollins@users.noreply.github.com> Date: Tue, 29 Jan 2019 15:54:09 +0800 Subject: [PATCH] add the adaptation of the png special format transform to RGBA format (#1672) Squashed commits: [2c579977] add the adaptation of the png special format transform to RGBA format --- .../js-bindings/manual/jsb_global.cpp | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/native/cocos/scripting/js-bindings/manual/jsb_global.cpp b/native/cocos/scripting/js-bindings/manual/jsb_global.cpp index f6eaa83c7c..255caddf94 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_global.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_global.cpp @@ -723,43 +723,37 @@ namespace bool freeData = false; }; - void cvRGB2RGBA (ImageInfo* imgInfo, const Image* img) { - imgInfo->length = img->getWidth() * img->getHeight() * 4; - uint8_t* dst = new uint8_t[imgInfo->length]; - uint8_t* src = imgInfo->data; - for (uint32_t i = 0, length = imgInfo->length; i < length; i += 4) { + uint8_t* cvRGB2RGBA (uint32_t length, uint8_t* src) { + uint8_t* dst = new uint8_t[length]; + for (uint32_t i = 0; i < length; i += 4) { dst[i] = *src++; dst[i + 1] = *src++; dst[i + 2] = *src++; dst[i + 3] = 255; } - imgInfo->data = dst; + return dst; } - void cvIA2RGBA (ImageInfo* imgInfo, const Image* img) { - imgInfo->length = img->getWidth() * img->getHeight() * 4; - uint8_t* dst = new uint8_t[imgInfo->length]; - uint8_t* src = imgInfo->data; - for (uint32_t i = 0, length = imgInfo->length; i < length; i += 2) { + uint8_t* cvIA2RGBA (uint32_t length, uint8_t* src) { + uint8_t* dst = new uint8_t[length]; + for (uint32_t i = 0; i < length; i += 4) { dst[i] = *src; dst[i + 1] = *src; dst[i + 2] = *src++; dst[i + 3] = *src; } - imgInfo->data = dst; + return dst; } - void cvI2RGBA (ImageInfo* imgInfo, const Image* img) { - imgInfo->length = img->getWidth() * img->getHeight() * 4; - uint8_t* dst = new uint8_t[imgInfo->length]; - uint8_t* src = imgInfo->data; - for (uint32_t i = 0, length = imgInfo->length; i < length; i++) { + uint8_t* cvI2RGBA (uint32_t length, uint8_t* src) { + uint8_t* dst = new uint8_t[length]; + for (uint32_t i = 0; i < length; i += 4) { dst[i] = *src; dst[i + 1] = *src; - dst[i + 2] = *src; + dst[i + 2] = *src++; dst[i + 3] = 255; } - imgInfo->data = dst; + return dst; } struct ImageInfo* createImageInfo(const Image* img) @@ -780,31 +774,40 @@ namespace imgInfo->hasAlpha = img->hasAlpha(); imgInfo->hasPremultipliedAlpha = img->hasPremultipliedAlpha(); imgInfo->compressed = img->isCompressed(); + imgInfo->length = img->getWidth() * img->getHeight() * 4; // Convert to RGBA888 because standard web api will return only RGBA888. // If not, then it may have issue in glTexSubImage. For example, engine // will create a big texture, and update its content with small pictures. // The big texture is RGBA888, then the small picture should be the same // format, or it will cause 0x502 error on OpenGL ES 2. + uint8_t* dst = nullptr; + uint32_t length = imgInfo->length; + uint8_t* src = imgInfo->data; switch(imgInfo->glFormat) { + case GL_RGBA: break; case GL_LUMINANCE_ALPHA: - cvIA2RGBA(imgInfo, img); + dst = cvIA2RGBA(length, src); break; case GL_ALPHA: - case GL_LUMINANCE - cvI2RGBA(imgInfo, img); + case GL_LUMINANCE: + dst = cvI2RGBA(length, src); break; case GL_RGB: - case default: - cvRGB2RGBA(imgInfo, img); + dst = cvRGB2RGBA(length, src); break; + default: + SE_LOGE("unknown image format"); } - imgInfo->hasAlpha = true; - imgInfo->bpp = 32; - imgInfo->glFormat = GL_RGBA; - imgInfo->glInternalFormat = GL_RGBA; - imgInfo->freeData = true; + if (imgInfo->glFormat != GL_RGBA) { + imgInfo->data = dst; + imgInfo->hasAlpha = true; + imgInfo->bpp = 32; + imgInfo->glFormat = GL_RGBA; + imgInfo->glInternalFormat = GL_RGBA; + imgInfo->freeData = true; + } return imgInfo; }