Save an image to memory.
| 858 | |
| 859 | // Save an image to memory. |
| 860 | af_err af_save_image_memory(void** ptr, const af_array in_, |
| 861 | const af_image_format format) { |
| 862 | try { |
| 863 | FreeImage_Module& _ = getFreeImagePlugin(); |
| 864 | |
| 865 | // set our own FreeImage error handler |
| 866 | _.FreeImage_SetOutputMessage(FreeImageErrorHandler); |
| 867 | |
| 868 | // try to guess the file format from the file extension |
| 869 | auto fif = static_cast<FREE_IMAGE_FORMAT>(format); |
| 870 | |
| 871 | if (fif == FIF_UNKNOWN || fif > 34) { // FreeImage FREE_IMAGE_FORMAT |
| 872 | // has upto 34 enums as of 3.17 |
| 873 | AF_ERROR("FreeImage Error: Unknown Filetype", AF_ERR_NOT_SUPPORTED); |
| 874 | } |
| 875 | |
| 876 | const ArrayInfo& info = getInfo(in_); |
| 877 | // check image color type |
| 878 | uint channels = info.dims()[2]; |
| 879 | DIM_ASSERT(1, channels <= 4); |
| 880 | DIM_ASSERT(1, channels != 2); |
| 881 | |
| 882 | uint fi_bpp = channels * 8; |
| 883 | |
| 884 | // sizes |
| 885 | uint fi_w = info.dims()[1]; |
| 886 | uint fi_h = info.dims()[0]; |
| 887 | |
| 888 | // create the result image storage using FreeImage |
| 889 | bitmap_ptr pResultBitmap = make_bitmap_ptr(_.FreeImage_Allocate( |
| 890 | fi_w, fi_h, static_cast<int>(fi_bpp), 0, 0, 0)); |
| 891 | if (pResultBitmap == NULL) { |
| 892 | AF_ERROR("FreeImage Error: Error creating image or file", |
| 893 | AF_ERR_RUNTIME); |
| 894 | } |
| 895 | |
| 896 | // FI assumes [0-255] |
| 897 | // If array is in 0-1 range, multiply by 255 |
| 898 | af_array in; |
| 899 | double max_real, max_imag; |
| 900 | bool free_in = false; |
| 901 | AF_CHECK(af_max_all(&max_real, &max_imag, in_)); |
| 902 | if (max_real <= 1) { |
| 903 | af_array c255; |
| 904 | AF_CHECK(af_constant(&c255, 255.0, info.ndims(), info.dims().get(), |
| 905 | f32)); |
| 906 | AF_CHECK(af_mul(&in, in_, c255, false)); |
| 907 | AF_CHECK(af_release_array(c255)); |
| 908 | free_in = true; |
| 909 | } else { |
| 910 | in = in_; |
| 911 | } |
| 912 | |
| 913 | // FI = row major | AF = column major |
| 914 | uint nDstPitch = _.FreeImage_GetPitch(pResultBitmap.get()); |
| 915 | uchar* pDstLine = |
| 916 | _.FreeImage_GetBits(pResultBitmap.get()) + nDstPitch * (fi_h - 1); |
| 917 | af_array rr = 0, gg = 0, bb = 0, aa = 0; |
no test coverage detected