Save an image to disk.
| 531 | |
| 532 | // Save an image to disk. |
| 533 | af_err af_save_image(const char* filename, const af_array in_) { |
| 534 | try { |
| 535 | ARG_ASSERT(0, filename != NULL); |
| 536 | |
| 537 | FreeImage_Module& _ = getFreeImagePlugin(); |
| 538 | |
| 539 | // set your own FreeImage error handler |
| 540 | _.FreeImage_SetOutputMessage(FreeImageErrorHandler); |
| 541 | |
| 542 | // try to guess the file format from the file extension |
| 543 | FREE_IMAGE_FORMAT fif = _.FreeImage_GetFileType(filename, 0); |
| 544 | if (fif == FIF_UNKNOWN) { |
| 545 | fif = _.FreeImage_GetFIFFromFilename(filename); |
| 546 | } |
| 547 | |
| 548 | if (fif == FIF_UNKNOWN) { |
| 549 | AF_ERROR("FreeImage Error: Unknown Filetype", AF_ERR_NOT_SUPPORTED); |
| 550 | } |
| 551 | |
| 552 | const ArrayInfo& info = getInfo(in_); |
| 553 | // check image color type |
| 554 | uint channels = info.dims()[2]; |
| 555 | DIM_ASSERT(1, channels <= 4); |
| 556 | DIM_ASSERT(1, channels != 2); |
| 557 | |
| 558 | uint fi_bpp = channels * 8; |
| 559 | |
| 560 | // sizes |
| 561 | uint fi_w = info.dims()[1]; |
| 562 | uint fi_h = info.dims()[0]; |
| 563 | |
| 564 | // create the result image storage using FreeImage |
| 565 | bitmap_ptr pResultBitmap = make_bitmap_ptr(_.FreeImage_Allocate( |
| 566 | fi_w, fi_h, static_cast<int>(fi_bpp), 0, 0, 0)); |
| 567 | if (pResultBitmap == NULL) { |
| 568 | AF_ERROR("FreeImage Error: Error creating image or file", |
| 569 | AF_ERR_RUNTIME); |
| 570 | } |
| 571 | |
| 572 | // FI assumes [0-255] |
| 573 | // If array is in 0-1 range, multiply by 255 |
| 574 | af_array in; |
| 575 | double max_real, max_imag; |
| 576 | bool free_in = false; |
| 577 | AF_CHECK(af_max_all(&max_real, &max_imag, in_)); |
| 578 | if (max_real <= 1) { |
| 579 | af_array c255 = 0; |
| 580 | AF_CHECK(af_constant(&c255, 255.0, info.ndims(), info.dims().get(), |
| 581 | f32)); |
| 582 | AF_CHECK(af_mul(&in, in_, c255, false)); |
| 583 | AF_CHECK(af_release_array(c255)); |
| 584 | free_in = true; |
| 585 | } else if (max_real < 256) { // NOLINT(bugprone-branch-clone) |
| 586 | in = in_; |
| 587 | } else if (max_real < 65536) { |
| 588 | af_array c255 = 0; |
| 589 | AF_CHECK(af_constant(&c255, 257.0, info.ndims(), info.dims().get(), |
| 590 | f32)); |
no test coverage detected