/////////////////////////////////////////////////////////////////////////// Memory IO /////////////////////////////////////////////////////////////////////////// Load image from memory.
| 722 | //////////////////////////////////////////////////////////////////////////////// |
| 723 | /// Load image from memory. |
| 724 | af_err af_load_image_memory(af_array* out, const void* ptr) { |
| 725 | using arrayfire::readImage; |
| 726 | try { |
| 727 | ARG_ASSERT(1, ptr != NULL); |
| 728 | |
| 729 | FreeImage_Module& _ = getFreeImagePlugin(); |
| 730 | |
| 731 | // set your own FreeImage error handler |
| 732 | _.FreeImage_SetOutputMessage(FreeImageErrorHandler); |
| 733 | |
| 734 | auto* stream = static_cast<FIMEMORY*>(const_cast<void*>(ptr)); |
| 735 | _.FreeImage_SeekMemory(stream, 0L, SEEK_SET); |
| 736 | |
| 737 | // try to guess the file format from the file extension |
| 738 | FREE_IMAGE_FORMAT fif = _.FreeImage_GetFileTypeFromMemory(stream, 0); |
| 739 | // if (fif == FIF_UNKNOWN) { |
| 740 | // fif = FreeImage_GetFIFFromFilenameFromMemory(filename); |
| 741 | //} |
| 742 | |
| 743 | if (fif == FIF_UNKNOWN) { |
| 744 | AF_ERROR("FreeImage Error: Unknown File or Filetype", |
| 745 | AF_ERR_NOT_SUPPORTED); |
| 746 | } |
| 747 | |
| 748 | unsigned flags = 0; |
| 749 | if (fif == FIF_JPEG) { |
| 750 | flags = flags | static_cast<unsigned>(JPEG_ACCURATE); |
| 751 | } |
| 752 | |
| 753 | // check that the plugin has reading capabilities ... |
| 754 | bitmap_ptr pBitmap = make_bitmap_ptr(NULL); |
| 755 | if (_.FreeImage_FIFSupportsReading(fif)) { |
| 756 | pBitmap.reset(_.FreeImage_LoadFromMemory(fif, stream, |
| 757 | static_cast<int>(flags))); |
| 758 | } |
| 759 | |
| 760 | if (pBitmap == NULL) { |
| 761 | AF_ERROR( |
| 762 | "FreeImage Error: Error reading image or file does not exist", |
| 763 | AF_ERR_RUNTIME); |
| 764 | } |
| 765 | |
| 766 | // check image color type |
| 767 | uint color_type = _.FreeImage_GetColorType(pBitmap.get()); |
| 768 | const uint fi_bpp = _.FreeImage_GetBPP(pBitmap.get()); |
| 769 | // int fi_color = (int)((fi_bpp / 8.0) + 0.5); //ceil |
| 770 | int fi_color; |
| 771 | switch (color_type) { |
| 772 | case 0: // FIC_MINISBLACK |
| 773 | case 1: // FIC_MINISWHITE |
| 774 | fi_color = 1; |
| 775 | break; |
| 776 | case 2: // FIC_PALETTE |
| 777 | case 3: // FIC_RGB |
| 778 | fi_color = 3; |
| 779 | break; |
| 780 | case 4: // FIC_RGBALPHA |
| 781 | case 5: // FIC_CMYK |
no test coverage detected