| 289 | } |
| 290 | |
| 291 | Bitmap::UniqueConstPtr Bitmap::createFromFile(const std::filesystem::path& path, bool isTopDown, ImportFlags importFlags) |
| 292 | { |
| 293 | if (!std::filesystem::exists(path)) |
| 294 | { |
| 295 | logWarning("Error when loading image file. File '{}' does not exist.", path); |
| 296 | return nullptr; |
| 297 | } |
| 298 | |
| 299 | FREE_IMAGE_FORMAT fifFormat = FIF_UNKNOWN; |
| 300 | |
| 301 | fifFormat = FreeImage_GetFileType(path.string().c_str(), 0); |
| 302 | if (fifFormat == FIF_UNKNOWN) |
| 303 | { |
| 304 | // Can't get the format from the file. Use file extension |
| 305 | fifFormat = FreeImage_GetFIFFromFilename(path.string().c_str()); |
| 306 | |
| 307 | if (fifFormat == FIF_UNKNOWN) |
| 308 | { |
| 309 | genWarning("Image type unknown", path); |
| 310 | return nullptr; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Check the library supports loading this image type |
| 315 | if (FreeImage_FIFSupportsReading(fifFormat) == false) |
| 316 | { |
| 317 | genWarning("Library doesn't support the file format", path); |
| 318 | return nullptr; |
| 319 | } |
| 320 | |
| 321 | // Read file using memory mapped access which is much faster than regular file IO. |
| 322 | MemoryMappedFile file(path, MemoryMappedFile::kWholeFile, MemoryMappedFile::AccessHint::SequentialScan); |
| 323 | if (!file.isOpen()) |
| 324 | { |
| 325 | genWarning("Can't open image file {}", path); |
| 326 | return nullptr; |
| 327 | } |
| 328 | |
| 329 | if (fifFormat == FIF_EXR) |
| 330 | { |
| 331 | if (isFloat16Exr(file)) |
| 332 | importFlags |= ImportFlags::ConvertToFloat16; |
| 333 | } |
| 334 | |
| 335 | FIMEMORY* memory = FreeImage_OpenMemory((BYTE*)file.getData(), file.getSize()); |
| 336 | FIBITMAP* pDib = FreeImage_LoadFromMemory(fifFormat, memory); |
| 337 | FreeImage_CloseMemory(memory); |
| 338 | file.close(); |
| 339 | |
| 340 | if (pDib == nullptr) |
| 341 | { |
| 342 | genWarning("Can't read image file", path); |
| 343 | return nullptr; |
| 344 | } |
| 345 | |
| 346 | // Create the bitmap |
| 347 | const uint32_t height = FreeImage_GetHeight(pDib); |
| 348 | const uint32_t width = FreeImage_GetWidth(pDib); |
nothing calls this directly
no test coverage detected