///////////////////////////////////////////////////////
| 318 | |
| 319 | //////////////////////////////////////////////////////////// |
| 320 | bool Image::loadFromMemory(const void* data, std::size_t size) |
| 321 | { |
| 322 | // Check input parameters |
| 323 | if (data && size) |
| 324 | { |
| 325 | // Check if the buffer contains a QOI image |
| 326 | const auto* qoiMagicNumBuffer = static_cast<const char*>(data); |
| 327 | if (isQoiMagicNumber(std::string_view(qoiMagicNumBuffer, size))) |
| 328 | { |
| 329 | qoi_desc formatDesc = {}; |
| 330 | if (const auto ptr = MallocPtr(qoi_decode(data, static_cast<int>(size), &formatDesc, 4))) |
| 331 | { |
| 332 | const Vector2u imageSize = {formatDesc.width, formatDesc.height}; |
| 333 | resize(imageSize, static_cast<const uint8_t*>(ptr.get())); |
| 334 | return true; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // If we can't load it as a QOI file, we fall back to using STBI |
| 339 | // Load the image and get a pointer to the pixels in memory |
| 340 | Vector2i imageSize; |
| 341 | int channels = 0; |
| 342 | const auto* buffer = static_cast<const unsigned char*>(data); |
| 343 | if (const auto ptr = StbPtr( |
| 344 | stbi_load_from_memory(buffer, static_cast<int>(size), &imageSize.x, &imageSize.y, &channels, STBI_rgb_alpha))) |
| 345 | { |
| 346 | resize(Vector2u(imageSize), ptr.get()); |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | // Error, failed to load the image |
| 351 | err() << "Failed to load image from memory. Reason: " << stbi_failure_reason() << std::endl; |
| 352 | |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | err() << "Failed to load image from memory, no data provided" << std::endl; |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | //////////////////////////////////////////////////////////// |
no test coverage detected