| 35 | using TLoadingFailure = std::function<void(std::string const &)>; |
| 36 | |
| 37 | bool LoadData(std::string const & textureName, std::string const & skinPathName, uint8_t bytesPerPixel, |
| 38 | TLoadingCompletion const & completionHandler, TLoadingFailure const & failureHandler) |
| 39 | { |
| 40 | ASSERT(completionHandler != nullptr, ()); |
| 41 | ASSERT(failureHandler != nullptr, ()); |
| 42 | |
| 43 | std::vector<unsigned char> rawData; |
| 44 | try |
| 45 | { |
| 46 | ReaderPtr<Reader> reader = !skinPathName.empty() ? skinPathName == StaticTexture::kDefaultResource |
| 47 | ? GetStyleReader().GetDefaultResourceReader(textureName) |
| 48 | : GetStyleReader().GetResourceReader(textureName, skinPathName) |
| 49 | : GetPlatform().GetReader(textureName); |
| 50 | |
| 51 | CHECK_LESS(reader.Size(), static_cast<uint64_t>(std::numeric_limits<size_t>::max()), ()); |
| 52 | size_t const size = static_cast<size_t>(reader.Size()); |
| 53 | rawData.resize(size); |
| 54 | reader.Read(0, &rawData[0], size); |
| 55 | } |
| 56 | catch (RootException & e) |
| 57 | { |
| 58 | failureHandler(e.what()); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | int w, h, bpp; |
| 63 | if (!stbi_info_from_memory(&rawData[0], static_cast<int>(rawData.size()), &w, &h, &bpp)) |
| 64 | { |
| 65 | failureHandler(std::string("Failed to get image file info from ") + textureName); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | ASSERT(glm::isPowerOfTwo(w), (w)); |
| 70 | ASSERT(glm::isPowerOfTwo(h), (h)); |
| 71 | |
| 72 | unsigned char * data = stbi_load_from_memory(&rawData[0], static_cast<int>(rawData.size()), &w, &h, &bpp, 0); |
| 73 | |
| 74 | if (bytesPerPixel != bpp) |
| 75 | { |
| 76 | std::vector<unsigned char> convertedData(static_cast<size_t>(w * h * bytesPerPixel)); |
| 77 | auto const pixelsCount = static_cast<uint32_t>(w * h); |
| 78 | for (uint32_t i = 0; i < pixelsCount; ++i) |
| 79 | { |
| 80 | unsigned char const * p = data + i * bpp; |
| 81 | for (uint8_t b = 0; b < bytesPerPixel; ++b) |
| 82 | convertedData[i * bytesPerPixel + b] = (b < bpp) ? p[b] : 255; |
| 83 | } |
| 84 | stbi_image_free(data); |
| 85 | completionHandler(convertedData.data(), static_cast<uint32_t>(w), static_cast<uint32_t>(h)); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | completionHandler(data, static_cast<uint32_t>(w), static_cast<uint32_t>(h)); |
| 90 | stbi_image_free(data); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | class StaticResourceInfo : public Texture::ResourceInfo |
no test coverage detected