| 1149 | } |
| 1150 | |
| 1151 | void XRInterface::asyncLoadAssetsImage(const std::string &imagePath) { |
| 1152 | auto *assetsImage = new Image(); |
| 1153 | assetsImage->addRef(); |
| 1154 | bool res = assetsImage->initWithImageFile(imagePath); |
| 1155 | if (!res) { |
| 1156 | CC_LOG_ERROR("[XRInterface] async load assets image init failed, %s!!!", imagePath.c_str()); |
| 1157 | assetsImage->release(); |
| 1158 | return; |
| 1159 | } |
| 1160 | uint32_t imageWidth = assetsImage->getWidth(); |
| 1161 | uint32_t imageHeight = assetsImage->getHeight(); |
| 1162 | uint32_t bufferSize = assetsImage->getDataLen(); |
| 1163 | uint8_t *buffer = nullptr; |
| 1164 | if (assetsImage->getRenderFormat() == gfx::Format::RGB8) { |
| 1165 | // convert to rgba8 |
| 1166 | bufferSize = imageWidth * imageHeight * 4; |
| 1167 | buffer = new uint8_t[bufferSize]; |
| 1168 | for (uint32_t y = 0; y < imageHeight; y++) { |
| 1169 | for (uint32_t x = 0; x < imageWidth; x++) { |
| 1170 | const unsigned int pixel = x + y * imageWidth; |
| 1171 | const unsigned int pixelFlip = x + (imageHeight - y - 1) * imageWidth; |
| 1172 | const uint8_t *originalPixel = &assetsImage->getData()[static_cast<size_t>(pixel * 3)]; |
| 1173 | uint8_t *convertedPixel = nullptr; |
| 1174 | if (_isFlipPixelY) { |
| 1175 | convertedPixel = &buffer[static_cast<size_t>(pixelFlip * 4)]; |
| 1176 | } else { |
| 1177 | convertedPixel = &buffer[static_cast<size_t>(pixel * 4)]; |
| 1178 | } |
| 1179 | convertedPixel[0] = originalPixel[0]; |
| 1180 | convertedPixel[1] = originalPixel[1]; |
| 1181 | convertedPixel[2] = originalPixel[2]; |
| 1182 | convertedPixel[3] = 255.0F; |
| 1183 | } |
| 1184 | } |
| 1185 | } else { |
| 1186 | buffer = new uint8_t[bufferSize]; |
| 1187 | if (_isFlipPixelY) { |
| 1188 | for (uint32_t y = 0; y < imageHeight; y++) { |
| 1189 | for (uint32_t x = 0; x < imageWidth; x++) { |
| 1190 | const unsigned int pixel = x + y * imageWidth; |
| 1191 | const unsigned int pixelFlip = x + (imageHeight - y - 1) * imageWidth; |
| 1192 | const uint8_t *originalPixel = &assetsImage->getData()[static_cast<size_t>(pixel * 4)]; |
| 1193 | uint8_t *convertedPixel = &buffer[static_cast<size_t>(pixelFlip * 4)]; |
| 1194 | convertedPixel[0] = originalPixel[0]; |
| 1195 | convertedPixel[1] = originalPixel[1]; |
| 1196 | convertedPixel[2] = originalPixel[2]; |
| 1197 | convertedPixel[3] = originalPixel[3]; |
| 1198 | } |
| 1199 | } |
| 1200 | } else { |
| 1201 | memcpy(buffer, assetsImage->getData(), bufferSize); |
| 1202 | } |
| 1203 | } |
| 1204 | auto app = CC_CURRENT_APPLICATION(); |
| 1205 | if (!app) { |
| 1206 | CC_LOG_ERROR("[XRInterface] loadAssetsImage callback failed, application not exist!!!"); |
| 1207 | return; |
| 1208 | } |
no test coverage detected