💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.2 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "resource-loader.hpp"
  20. #include "stb/stb_image.h"
  21. #include "resources/image.hpp"
  22. #include <cstring>
  23. #include <stdexcept>
  24. #include <physfs.h>
  25. #include <tinyexr.h>
  26. template <>
  27. image* resource_loader<image>::load(resource_manager* resource_manager, PHYSFS_File* file, const std::filesystem::path& path)
  28. {
  29. unsigned char* buffer;
  30. int size;
  31. ::image* image = nullptr;
  32. // Read input stream into buffer
  33. size = static_cast<int>(PHYSFS_fileLength(file));
  34. buffer = new unsigned char[size];
  35. PHYSFS_readBytes(file, buffer, size);
  36. // Select loader according to file extension
  37. if (path.extension() == ".exr")
  38. {
  39. // Load OpenEXR with TinyEXR
  40. float* pixels = nullptr;
  41. int width = 0;
  42. int height = 0;
  43. const char* error = nullptr;
  44. int status = LoadEXRFromMemory(&pixels, &width, &height, buffer, size, &error);
  45. if (status != TINYEXR_SUCCESS)
  46. {
  47. delete[] buffer;
  48. std::string error_string(error);
  49. FreeEXRErrorMessage(error);
  50. throw std::runtime_error("TinyEXR error (" + std::to_string(status) + "): " + error_string);
  51. }
  52. // Create image
  53. std::size_t component_size = sizeof(float);
  54. image = new ::image();
  55. image->format(component_size, 4);
  56. image->resize(static_cast<unsigned int>(width), static_cast<unsigned int>(height));
  57. std::memcpy(image->get_pixels(), pixels, image->get_size());
  58. // Free loaded pixels
  59. free(pixels);
  60. }
  61. else
  62. {
  63. // Load all other formats with stb_image
  64. // Determine if image is in an HDR format
  65. bool hdr = (stbi_is_hdr_from_memory(buffer, size) != 0);
  66. // Set vertical flip on load in order to upload pixels correctly to OpenGL
  67. stbi_set_flip_vertically_on_load(true);
  68. // Load image data
  69. void* pixels = nullptr;
  70. int width = 0;
  71. int height = 0;
  72. int channels = 0;
  73. if (hdr)
  74. {
  75. pixels = stbi_loadf_from_memory(buffer, size, &width, &height, &channels, 0);
  76. }
  77. else
  78. {
  79. pixels = stbi_load_from_memory(buffer, size, &width, &height, &channels, 0);
  80. }
  81. // Free file buffer
  82. delete[] buffer;
  83. // Check if image was loaded
  84. if (!pixels)
  85. {
  86. throw std::runtime_error("STBI failed to load image from memory.");
  87. }
  88. // Create image
  89. std::size_t component_size = (hdr) ? sizeof(float) : sizeof(unsigned char);
  90. image = new ::image();
  91. image->format(component_size, channels);
  92. image->resize(static_cast<unsigned int>(width), static_cast<unsigned int>(height));
  93. std::memcpy(image->get_pixels(), pixels, image->get_size());
  94. // Free loaded image data
  95. stbi_image_free(pixels);
  96. }
  97. return image;
  98. }