[arrayfire] 293/408: Changed saveImageMem API. Added image format enum

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Sep 21 19:12:16 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/sid
in repository arrayfire.

commit c43af47edf53ba9b79d1f38f458fc7e04dd3b0f4
Author: Shehzan Mohammed <shehzan at arrayfire.com>
Date:   Wed Aug 19 16:55:33 2015 -0400

    Changed saveImageMem API. Added image format enum
---
 include/af/defines.h    | 16 +++++++++
 include/af/image.h      | 86 +++++++++++++++++++++++++------------------------
 src/api/c/imageio.cpp   | 48 +++++++++++++++++----------
 src/api/cpp/imageio.cpp |  4 +--
 test/imageio.cpp        |  8 ++---
 5 files changed, 97 insertions(+), 65 deletions(-)

diff --git a/include/af/defines.h b/include/af/defines.h
index 28f7c96..a84a07a 100644
--- a/include/af/defines.h
+++ b/include/af/defines.h
@@ -285,6 +285,22 @@ typedef enum {
     AF_COLORMAP_BLUE    = 6     ///< Blue hue map
 } af_colormap;
 
+typedef enum {
+    AF_FIF_BMP          = 0,    ///< FreeImage Enum for Bitmap File
+    AF_FIF_ICO          = 1,    ///< FreeImage Enum for Windows Icon File
+    AF_FIF_JPEG         = 2,    ///< FreeImage Enum for JPEG File
+    AF_FIF_JNG          = 3,    ///< FreeImage Enum for JPEG Network Graphics File
+    AF_FIF_PNG          = 13,   ///< FreeImage Enum for Portable Network Graphics File
+    AF_FIF_PPM          = 14,   ///< FreeImage Enum for Portable Pixelmap (ASCII) File
+    AF_FIF_PPMRAW       = 15,   ///< FreeImage Enum for Portable Pixelmap (Binary) File
+    AF_FIF_TIFF         = 18,   ///< FreeImage Enum for Tagged Image File Format File
+    AF_FIF_PSD          = 20,   ///< FreeImage Enum for Adobe Photoshop File
+    AF_FIF_HDR          = 26,   ///< FreeImage Enum for High Dynamic Range File
+    AF_FIF_EXR          = 29,   ///< FreeImage Enum for ILM OpenEXR File
+    AF_FIF_JP2          = 31,   ///< FreeImage Enum for JPEG-2000 File
+    AF_FIF_RAW          = 34    ///< FreeImage Enum for RAW Camera Image File
+} af_image_format;
+
 // Below enum is purely added for example purposes
 // it doesn't and shoudn't be used anywhere in the
 // code. No Guarantee's provided if it is used.
diff --git a/include/af/image.h b/include/af/image.h
index 2f8c85a..9c7ff01 100644
--- a/include/af/image.h
+++ b/include/af/image.h
@@ -65,9 +65,10 @@ AFAPI array loadImageMem(const void *ptr);
 /**
     C++ Interface for saving an image to memory
 
-    \param[in] filename is a dummy string that contains the image type. This is
-    used to determine the filetype to be used. No file is created.
     \param[in] in is the arrayfire array to be saved as an image
+    \param[in] format is the type of image to create in memory. The enum borrows from
+    the FREE_IMAGE_FORMAT enum of FreeImage. Other values not included in af_image_format
+    but included in FREE_IMAGE_FORMAT can also be passed to this function.
 
     \return a void* pointer which is a type cast of the FreeImage type FIMEMORY* pointer.
 
@@ -76,7 +77,7 @@ AFAPI array loadImageMem(const void *ptr);
 
     \ingroup imagemem_func_save
 */
-AFAPI void* saveImageMem(const char* filename, const array& in);
+AFAPI void* saveImageMem(const array& in, const af_image_format format = AF_FIF_PNG);
 
 /**
     C++ Interface for deleting memory created by \ref saveImageMem or
@@ -578,76 +579,77 @@ extern "C" {
 #endif
 
     /**
-       C Interface for calculating the gradients
+        C Interface for calculating the gradients
 
-       \param[out] dx the gradient along first dimension
-       \param[out] dy the gradient along second dimension
-       \param[in]  in is the input array
-       \return     \ref AF_SUCCESS if the color transformation is successful,
-       otherwise an appropriate error code is returned.
+        \param[out] dx the gradient along first dimension
+        \param[out] dy the gradient along second dimension
+        \param[in]  in is the input array
+        \return     \ref AF_SUCCESS if the color transformation is successful,
+        otherwise an appropriate error code is returned.
 
-       \ingroup calc_func_grad
+        \ingroup calc_func_grad
     */
     AFAPI af_err af_gradient(af_array *dx, af_array *dy, const af_array in);
 
     /**
-       C Interface for loading an image
+        C Interface for loading an image
 
-       \param[out] out will contain the image
-       \param[in] filename is name of file to be loaded
-       \param[in] isColor boolean denoting if the image should be loaded as 1 channel or 3 channel
-       \return     \ref AF_SUCCESS if the color transformation is successful,
-       otherwise an appropriate error code is returned.
+        \param[out] out will contain the image
+        \param[in] filename is name of file to be loaded
+        \param[in] isColor boolean denoting if the image should be loaded as 1 channel or 3 channel
+        \return     \ref AF_SUCCESS if the color transformation is successful,
+        otherwise an appropriate error code is returned.
 
-       \ingroup imageio_func_load
+        \ingroup imageio_func_load
     */
     AFAPI af_err af_load_image(af_array *out, const char* filename, const bool isColor);
 
     /**
-       C Interface for saving an image
+        C Interface for saving an image
 
-       \param[in] filename is name of file to be loaded
-       \param[in] in is the arrayfire array to be saved as an image
-       \return     \ref AF_SUCCESS if the color transformation is successful,
-       otherwise an appropriate error code is returned.
+        \param[in] filename is name of file to be loaded
+        \param[in] in is the arrayfire array to be saved as an image
+        \return     \ref AF_SUCCESS if the color transformation is successful,
+        otherwise an appropriate error code is returned.
 
-       \ingroup imageio_func_save
+        \ingroup imageio_func_save
     */
     AFAPI af_err af_save_image(const char* filename, const af_array in);
 
     /**
-       C Interface for loading an image from memory
+        C Interface for loading an image from memory
 
-       \param[out] out is an array that will contain the image
-       \param[in] ptr is the FIMEMORY pointer created by either saveImageMem function, the
-       af_save_image_memory function, or the FreeImage_OpenMemory API.
-       \return     \ref AF_SUCCESS if successful
+        \param[out] out is an array that will contain the image
+        \param[in] ptr is the FIMEMORY pointer created by either saveImageMem function, the
+        af_save_image_memory function, or the FreeImage_OpenMemory API.
+        \return     \ref AF_SUCCESS if successful
 
-       \ingroup imagemem_func_load
+        \ingroup imagemem_func_load
     */
     AFAPI af_err af_load_image_memory(af_array *out, const void* ptr);
 
     /**
-       C Interface for saving an image to memory using FreeImage
+        C Interface for saving an image to memory using FreeImage
 
-       \param[out] ptr is the FIMEMORY pointer created by FreeImage.
-       \param[in] filename is a string that contains the type of image storage to be used.
-       No file is created. This is used to determine the type and encoding to use.
-       \param[in] in is the arrayfire array to be saved as an image
-       \return     \ref AF_SUCCESS if successful.
+        \param[out] ptr is the FIMEMORY pointer created by FreeImage.
+        \param[in] in is the arrayfire array to be saved as an image
+        \param[in] format is the type of image to create in memory. The enum borrows from
+        the FREE_IMAGE_FORMAT enum of FreeImage. Other values not included in af_image_format
+        but included in FREE_IMAGE_FORMAT can also be passed to this function.
+        \return     \ref AF_SUCCESS if successful.
 
-       \ingroup imagemem_func_save
+        \ingroup imagemem_func_save
     */
-    AFAPI af_err af_save_image_memory(void** ptr, const char* filename, const af_array in);
+    AFAPI af_err af_save_image_memory(void** ptr, const af_array in, const af_image_format format);
 
     /**
-       C Interface for deleting an image from memory
+        C Interface for deleting an image from memory
 
-       \param[in] ptr is the FIMEMORY pointer created by either saveImageMem function, the
-       af_save_image_memory function, or the FreeImage_OpenMemory API.
-       \return     \ref AF_SUCCESS if successful
+        \param[in] ptr is the FIMEMORY pointer created by either saveImageMem function, the
+        af_save_image_memory function, or the FreeImage_OpenMemory API.
+        \return     \ref AF_SUCCESS if successful
 
-       \ingroup imagemem_func_delete
+        \ingroup imagemem_func_delete
     */
     AFAPI af_err af_delete_image_memory(void* ptr);
 
diff --git a/src/api/c/imageio.cpp b/src/api/c/imageio.cpp
index 567ff07..98af2e0 100644
--- a/src/api/c/imageio.cpp
+++ b/src/api/c/imageio.cpp
@@ -207,10 +207,20 @@ af_err af_load_image(af_array *out, const char* filename, const bool isColor)
         const uint fi_bpp = FreeImage_GetBPP(pBitmap);
         //int fi_color = (int)((fi_bpp / 8.0) + 0.5);        //ceil
         int fi_color;
-        if      (color_type == 1) fi_color = 1;
-        else if (color_type == 2) fi_color = 3;
-        else if (color_type == 4) fi_color = 4;
-        else                      fi_color = 3;
+        switch(color_type) {
+            case 0:                     // FIC_MINISBLACK
+            case 1:                     // FIC_MINISWHITE
+                fi_color = 1; break;
+            case 2:                     // FIC_PALETTE
+            case 3:                     // FIC_RGB
+                fi_color = 3; break;
+            case 4:                     // FIC_RGBALPHA
+            case 5:                     // FIC_CMYK
+                fi_color = 4; break;
+            default:                    // Should not come here
+                fi_color = 3; break;
+        }
+
         const int fi_bpc = fi_bpp / fi_color;
         if(fi_bpc != 8 && fi_bpc != 16 && fi_bpc != 32) {
             AF_ERROR("FreeImage Error: Bits per channel not supported", AF_ERR_NOT_SUPPORTED);
@@ -481,10 +491,19 @@ af_err af_load_image_memory(af_array *out, const void* ptr)
         const uint fi_bpp = FreeImage_GetBPP(pBitmap);
         //int fi_color = (int)((fi_bpp / 8.0) + 0.5);        //ceil
         int fi_color;
-        if      (color_type == 1) fi_color = 1;
-        else if (color_type == 2) fi_color = 3;
-        else if (color_type == 4) fi_color = 4;
-        else                      fi_color = 3;
+        switch(color_type) {
+            case 0:                     // FIC_MINISBLACK
+            case 1:                     // FIC_MINISWHITE
+                fi_color = 1; break;
+            case 2:                     // FIC_PALETTE
+            case 3:                     // FIC_RGB
+                fi_color = 3; break;
+            case 4:                     // FIC_RGBALPHA
+            case 5:                     // FIC_CMYK
+                fi_color = 4; break;
+            default:                    // Should not come here
+                fi_color = 3; break;
+        }
         const int fi_bpc = fi_bpp / fi_color;
         if(fi_bpc != 8 && fi_bpc != 16 && fi_bpc != 32) {
             AF_ERROR("FreeImage Error: Bits per channel not supported", AF_ERR_NOT_SUPPORTED);
@@ -530,25 +549,20 @@ af_err af_load_image_memory(af_array *out, const void* ptr)
     return AF_SUCCESS;
 }
 
-// Save an image to disk.
-af_err af_save_image_memory(void **ptr, const char* filename, const af_array in_)
+// Save an image to memory.
+af_err af_save_image_memory(void **ptr, const af_array in_, const af_image_format format)
 {
     try {
 
-        ARG_ASSERT(0, filename != NULL);
-
         FI_Init();
 
         // set your own FreeImage error handler
         FreeImage_SetOutputMessage(FreeImageErrorHandler);
 
         // try to guess the file format from the file extension
-        FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename);
-        if (fif == FIF_UNKNOWN) {
-            fif = FreeImage_GetFIFFromFilename(filename);
-        }
+        FREE_IMAGE_FORMAT fif = (FREE_IMAGE_FORMAT)format;
 
-        if(fif == FIF_UNKNOWN) {
+        if(fif == FIF_UNKNOWN || fif > 34) { // FreeImage FREE_IMAGE_FORMAT has upto 34 enums as of 3.17
             AF_ERROR("FreeImage Error: Unknown Filetype", AF_ERR_NOT_SUPPORTED);
         }
 
diff --git a/src/api/cpp/imageio.cpp b/src/api/cpp/imageio.cpp
index a84861f..f6d1680 100644
--- a/src/api/cpp/imageio.cpp
+++ b/src/api/cpp/imageio.cpp
@@ -39,10 +39,10 @@ void saveImage(const char* filename, const array& in)
     AF_THROW(af_save_image(filename, in.get()));
 }
 
-void* saveImageMem(const char* filename, const array& in)
+void* saveImageMem(const array& in, const af_image_format format)
 {
     void* ptr = NULL;
-    AF_THROW(af_save_image_memory(&ptr, filename, in.get()));
+    AF_THROW(af_save_image_memory(&ptr, in.get(), format));
     return ptr;
 }
 
diff --git a/test/imageio.cpp b/test/imageio.cpp
index f5b6017..20d1b43 100644
--- a/test/imageio.cpp
+++ b/test/imageio.cpp
@@ -173,7 +173,7 @@ TEST(ImageMem, SaveMemPNG)
 
     af::array img = af::loadImage(string(TEST_DIR"/imageio/color_seq.png").c_str(), true);
 
-    void* savedMem = af::saveImageMem("save.png", img);
+    void* savedMem = af::saveImageMem(img, AF_FIF_PNG);
 
     af::array loadMem = af::loadImageMem(savedMem);
 
@@ -189,7 +189,7 @@ TEST(ImageMem, SaveMemJPG1)
     af::array img = af::loadImage(string(TEST_DIR"/imageio/color_seq.png").c_str(), false);
     af::saveImage("color_seq1.jpg", img);
 
-    void* savedMem = af::saveImageMem("color_seq1.jpg", img);
+    void* savedMem = af::saveImageMem(img, AF_FIF_JPEG);
 
     af::array loadMem = af::loadImageMem(savedMem);
     af::array imgJPG = af::loadImage("color_seq1.jpg", false);
@@ -206,7 +206,7 @@ TEST(ImageMem, SaveMemJPG3)
     af::array img = af::loadImage(string(TEST_DIR"/imageio/color_seq.png").c_str(), true);
     af::saveImage("color_seq3.jpg", img);
 
-    void* savedMem = af::saveImageMem("color_seq3.jpg", img);
+    void* savedMem = af::saveImageMem(img, AF_FIF_JPEG);
 
     af::array loadMem = af::loadImageMem(savedMem);
     af::array imgJPG = af::loadImage("color_seq3.jpg", true);
@@ -222,7 +222,7 @@ TEST(ImageMem, SaveMemBMP)
 
     af::array img = af::loadImage(string(TEST_DIR"/imageio/color_rand.png").c_str(), true);
 
-    void* savedMem = af::saveImageMem("save.png", img);
+    void* savedMem = af::saveImageMem(img, AF_FIF_BMP);
 
     af::array loadMem = af::loadImageMem(savedMem);
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list