Save an image to disk.
| 408 | |
| 409 | // Save an image to disk. |
| 410 | af_err af_save_image_native(const char* filename, const af_array in) { |
| 411 | try { |
| 412 | ARG_ASSERT(0, filename != NULL); |
| 413 | |
| 414 | FreeImage_Module& _ = getFreeImagePlugin(); |
| 415 | |
| 416 | // set your own FreeImage error handler |
| 417 | _.FreeImage_SetOutputMessage(FreeImageErrorHandler); |
| 418 | |
| 419 | // try to guess the file format from the file extension |
| 420 | FREE_IMAGE_FORMAT fif = _.FreeImage_GetFileType(filename, 0); |
| 421 | if (fif == FIF_UNKNOWN) { |
| 422 | fif = _.FreeImage_GetFIFFromFilename(filename); |
| 423 | } |
| 424 | |
| 425 | if (fif == FIF_UNKNOWN) { |
| 426 | AF_ERROR("FreeImage Error: Unknown Filetype", AF_ERR_NOT_SUPPORTED); |
| 427 | } |
| 428 | |
| 429 | const ArrayInfo& info = getInfo(in); |
| 430 | // check image color type |
| 431 | auto channels = static_cast<FI_CHANNELS>(info.dims()[2]); |
| 432 | DIM_ASSERT(1, channels <= 4); |
| 433 | DIM_ASSERT(1, channels != 2); |
| 434 | |
| 435 | // sizes |
| 436 | uint fi_w = info.dims()[1]; |
| 437 | uint fi_h = info.dims()[0]; |
| 438 | |
| 439 | af_dtype type = info.getType(); |
| 440 | |
| 441 | // FI assumes [0-255] for u8 |
| 442 | // FI assumes [0-65k] for u16 |
| 443 | // FI assumes [0-1] for f32 |
| 444 | int fi_bpp = 0; |
| 445 | switch (type) { |
| 446 | case u8: fi_bpp = channels * 8; break; |
| 447 | case u16: fi_bpp = channels * 16; break; |
| 448 | case f32: fi_bpp = channels * 32; break; |
| 449 | default: TYPE_ERROR(1, type); |
| 450 | } |
| 451 | |
| 452 | FREE_IMAGE_TYPE fit_type = getFIT(channels, type); |
| 453 | |
| 454 | // create the result image storage using FreeImage |
| 455 | bitmap_ptr pResultBitmap = make_bitmap_ptr(nullptr); |
| 456 | switch (type) { |
| 457 | case u8: |
| 458 | case u16: |
| 459 | case f32: |
| 460 | pResultBitmap.reset(_.FreeImage_AllocateT(fit_type, fi_w, fi_h, |
| 461 | fi_bpp, 0, 0, 0)); |
| 462 | break; |
| 463 | default: TYPE_ERROR(1, type); |
| 464 | } |
| 465 | |
| 466 | if (pResultBitmap == NULL) { |
| 467 | AF_ERROR("FreeImage Error: Error creating image or file", |
no test coverage detected