///////////////////////////////////////////////////////
| 422 | |
| 423 | //////////////////////////////////////////////////////////// |
| 424 | bool Image::saveToFile(const std::filesystem::path& filename) const |
| 425 | { |
| 426 | // Make sure the image is not empty |
| 427 | if (!m_pixels.empty() && m_size.x > 0 && m_size.y > 0) |
| 428 | { |
| 429 | // Deduce the image type from its extension |
| 430 | |
| 431 | // Extract the extension |
| 432 | const std::filesystem::path extension = filename.extension(); |
| 433 | const Vector2i convertedSize = Vector2i(m_size); |
| 434 | |
| 435 | // Callback to write to std::ofstream |
| 436 | auto writeStdOfstream = [](void* context, void* data, int size) |
| 437 | { |
| 438 | auto& file = *static_cast<std::ofstream*>(context); |
| 439 | if (file) |
| 440 | file.write(static_cast<const char*>(data), static_cast<std::streamsize>(size)); |
| 441 | }; |
| 442 | |
| 443 | if (extension == ".bmp") |
| 444 | { |
| 445 | // BMP format |
| 446 | std::ofstream file(filename, std::ios::binary); |
| 447 | if (stbi_write_bmp_to_func(writeStdOfstream, &file, convertedSize.x, convertedSize.y, 4, m_pixels.data()) && file) |
| 448 | return true; |
| 449 | } |
| 450 | else if (extension == ".tga") |
| 451 | { |
| 452 | // TGA format |
| 453 | std::ofstream file(filename, std::ios::binary); |
| 454 | if (stbi_write_tga_to_func(writeStdOfstream, &file, convertedSize.x, convertedSize.y, 4, m_pixels.data()) && file) |
| 455 | return true; |
| 456 | } |
| 457 | else if (extension == ".png") |
| 458 | { |
| 459 | // PNG format |
| 460 | std::ofstream file(filename, std::ios::binary); |
| 461 | if (stbi_write_png_to_func(writeStdOfstream, &file, convertedSize.x, convertedSize.y, 4, m_pixels.data(), 0) && |
| 462 | file) |
| 463 | return true; |
| 464 | } |
| 465 | else if (extension == ".jpg" || extension == ".jpeg") |
| 466 | { |
| 467 | // JPG format |
| 468 | std::ofstream file(filename, std::ios::binary); |
| 469 | if (stbi_write_jpg_to_func(writeStdOfstream, &file, convertedSize.x, convertedSize.y, 4, m_pixels.data(), 90) && |
| 470 | file) |
| 471 | return true; |
| 472 | } |
| 473 | else if (extension == ".qoi") |
| 474 | { |
| 475 | qoi_desc desc; |
| 476 | desc.width = m_size.x; |
| 477 | desc.height = m_size.y; |
| 478 | desc.channels = 4; |
| 479 | desc.colorspace = QOI_LINEAR; |
| 480 | |
| 481 | int bufferLen = 0; |
no test coverage detected