reading & writing bmp code refactoring 05/171705/5
authorJiyong Min <jiyong.min@samsung.com>
Wed, 7 Mar 2018 00:22:03 +0000 (09:22 +0900)
committerhj kim <backto.kim@samsung.com>
Wed, 7 Mar 2018 01:57:31 +0000 (01:57 +0000)
Change-Id: I0078b61fe5e8cc42220231e78153f1f4bc8be4b4
Signed-off-by: Jiyong Min <jiyong.min@samsung.com>
bmp/include/mm_util_bmp.h
bmp/mm_util_bmp.c
bmp/test/mm_util_bmp_testsuite.c
packaging/libmm-utility.spec

index 5be6d36..506a8d5 100755 (executable)
@@ -43,10 +43,10 @@ typedef struct {
 } mm_util_bmp_data;
 
 /**
- * This function extracts raw data from bmp file
+ * This function decodes bmp image(file) to color image(raw data)
  *
- * @param filename [in]     input file name, encoded stream file
- * @param decoded  [out]    pointer of mm_util_bmp_data.
+ * @param filename [in]     the input file, encoded bmp image
+ * @param decoded  [out]    the pointer of mm_util_bmp_data.
  *                          After using it, please free the allocated memory.
  * @return                  This function returns zero on success, or negative value with error code.
  * @remark
@@ -56,11 +56,11 @@ typedef struct {
 int mm_util_decode_from_bmp_file(const char *filename, mm_util_bmp_data * decoded);
 
 /**
- * This function extracts raw data from bmp memory
+ * This function decodes bmp image(memory) to color image(raw data)
  *
- * @param memory   [in]     input memory, encoded stream file
- * @param src_size [in]     input src size, encoded stream file
- * @param decoded  [out]    pointer of mm_util_bmp_data.
+ * @param memory   [in]     the input memory, encoded bmp image
+ * @param src_size [in]     the size of input memory
+ * @param decoded  [out]    the pointer of mm_util_bmp_data.
  *                          After using it, please free the allocated memory.
  * @return                  This function returns zero on success, or negative value with error code.
  * @remark
@@ -70,11 +70,11 @@ int mm_util_decode_from_bmp_file(const char *filename, mm_util_bmp_data * decode
 int mm_util_decode_from_bmp_memory(void *memory, const size_t src_size, mm_util_bmp_data * decoded);
 
 /**
- * This function encodes raw data to bmp file
+ * This function encodes color image(raw data) to bmp file
  *
- * @param decoded  [in]     pointer of mm_util_bmp_data.
+ * @param decoded  [in]     the pointer of mm_util_bmp_data.
  *                          After using it, please free the allocated memory.
- * @param filename [out]    output file name on to which the encoded stream will be written.
+ * @param filename [out]    the output file name on to which the encoded bmp stream will be written.
  * @return                  This function returns zero on success, or negative value with error code.
  * @remark
  * @see                     mm_util_bmp_data
@@ -83,11 +83,11 @@ int mm_util_decode_from_bmp_memory(void *memory, const size_t src_size, mm_util_
 int mm_util_encode_bmp_to_file(mm_util_bmp_data * decoded, const char *filename);
 
 /**
- * This function encodes raw data to buffer
+ * This function encodes color image(raw data) to buffer
  *
- * @param decoded [in]     pointer of mm_util_bmp_data.
+ * @param decoded [in]     the pointer of mm_util_bmp_data.
  *                         After using it, please free the allocated memory.
- * @param buffer  [out]    output buffer on to which the encoded stream will be written.
+ * @param buffer  [out]    the output memory on to which the encoded bmp stream will be written.
  * @param size    [out]    The size of output buffer on to which the encoded stream will be written.
  * @return                  This function returns zero on success, or negative value with error code.
  * @remark
index a20eeb6..940f3c1 100755 (executable)
 
 #define BYTES_PER_PIXEL 4
 
+/* for bmp_bitmap_callback_vt of nsbmp */
 void *__bitmap_create(int width, int height, unsigned int state);
 unsigned char *__bitmap_get_buffer(void *bitmap);
 size_t __bitmap_get_bpp(void *bitmap);
 void __bitmap_destroy(void *bitmap);
 
+void *__bitmap_create(int width, int height, unsigned int state)
+{
+       return calloc(width * height, BYTES_PER_PIXEL);
+}
+
+unsigned char *__bitmap_get_buffer(void *bitmap)
+{
+       return bitmap;
+}
+
+size_t __bitmap_get_bpp(void *bitmap)
+{
+       return BYTES_PER_PIXEL;
+}
+
+void __bitmap_destroy(void *bitmap)
+{
+       MMUTIL_SAFE_FREE(bitmap);
+}
+
 static unsigned char *__load_file(const char *path, size_t * data_size)
 {
        FILE *fd;
@@ -94,27 +115,8 @@ static void __print_error(const char *context, bmp_result code)
        }
 }
 
-void *__bitmap_create(int width, int height, unsigned int state)
-{
-       return calloc(width * height, BYTES_PER_PIXEL);
-}
-
-unsigned char *__bitmap_get_buffer(void *bitmap)
-{
-       return bitmap;
-}
-
-size_t __bitmap_get_bpp(void *bitmap)
-{
-       return BYTES_PER_PIXEL;
-}
-
-void __bitmap_destroy(void *bitmap)
-{
-       MMUTIL_SAFE_FREE(bitmap);
-}
-
-static int __read_bmp(mm_util_bmp_data *decoded, const char *filename, void *memory, size_t src_size)
+/* decodes bmp image to color image */
+static int _read_bmp(const char *filename, void *memory, size_t src_size, mm_util_bmp_data *decoded)
 {
        bmp_bitmap_callback_vt bitmap_callbacks = {
                __bitmap_create,
@@ -128,16 +130,16 @@ static int __read_bmp(mm_util_bmp_data *decoded, const char *filename, void *mem
        int res = MM_UTIL_ERROR_NONE;
        unsigned char *data = NULL;
 
+       mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+       mm_util_retvm_if(!MMUTIL_STRING_VALID(filename) && (memory == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
+
        if (filename) {
                data = __load_file(filename, &size);
                if (data == NULL)
                        return MM_UTIL_ERROR_INVALID_OPERATION;
-       } else if (memory) {
+       } else {
                data = (unsigned char *)memory;
                size = src_size;
-       } else {
-               mm_util_error("Bmp File wrong decode parameters");
-               return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
        nsbmp_create(&bmp, &bitmap_callbacks);
@@ -178,40 +180,24 @@ static int __read_bmp(mm_util_bmp_data *decoded, const char *filename, void *mem
        return res;
 }
 
-int mm_util_decode_from_bmp_file(const char *filename, mm_util_bmp_data *decoded)
-{
-       int ret;
-
-       mm_util_debug("mm_util_decode_from_gif_file");
-
-       ret = __read_bmp(decoded, filename, NULL, 0);
-
-       return ret;
-}
-
-int mm_util_decode_from_bmp_memory(void *memory, const size_t src_size, mm_util_bmp_data *decoded)
-{
-       int ret;
-
-       mm_util_debug("mm_util_decode_from_gif_file");
-
-       ret = __read_bmp(decoded, NULL, memory, src_size);
-
-       return ret;
-}
-
-int mm_util_encode_bmp_to_file(mm_util_bmp_data *decoded, const char *filename)
+/* encodes color image to bmp image */
+static int _write_bmp(mm_util_bmp_data *decoded, const char *filename, void **memory, size_t *src_size)
 {
        bmpfile_t *bmp;
        rgb_pixel_t pixel = { 0, 0, 0, 0 };
        uint16_t row, col;
        uint8_t *image;
 
+       mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+       mm_util_retvm_if(!MMUTIL_STRING_VALID(filename) && (memory == NULL || src_size == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
+
+       /* header of bmp image is create at 'bmp' */
        if ((bmp = bmp_create(decoded->width, decoded->height, BYTES_PER_PIXEL * 8)) == NULL) {
                mm_util_error("Invalid depth value.");
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
+       /* pixcels of bmp image is stored at 'bmp' */
        image = (uint8_t *) decoded->data;
        for (row = 0; row != decoded->height; row++) {
                for (col = 0; col != decoded->width; col++) {
@@ -223,45 +209,95 @@ int mm_util_encode_bmp_to_file(mm_util_bmp_data *decoded, const char *filename)
                }
        }
 
-       if (bmp_save(bmp, filename) == false) {
-               mm_util_error("Saving bmp was failed.");
-               bmp_destroy(bmp);
-               return MM_UTIL_ERROR_INVALID_OPERATION;
+       /* write 'bmp' to file or memory */
+       if (MMUTIL_STRING_VALID(filename)) {
+               if (bmp_save(bmp, filename) == false) {
+                       mm_util_error("Saving bmp was failed.");
+                       bmp_destroy(bmp);
+                       return MM_UTIL_ERROR_INVALID_OPERATION;
+               }
+       } else {
+               if (bmp_save2(bmp, memory, src_size) == false) {
+                       mm_util_error("Saving bmp was failed.");
+                       bmp_destroy(bmp);
+                       MMUTIL_SAFE_FREE(*memory);
+                       *src_size = 0;
+                       return MM_UTIL_ERROR_INVALID_OPERATION;
+               }
        }
+
        bmp_destroy(bmp);
        return MM_UTIL_ERROR_NONE;
 }
 
+int mm_util_decode_from_bmp_file(const char *filename, mm_util_bmp_data *decoded)
+{
+       int ret = MM_UTIL_ERROR_NONE;
+
+       mm_util_retvm_if(!MMUTIL_STRING_VALID(filename), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
+       mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+
+       mm_util_fenter();
+
+       ret = _read_bmp(filename, NULL, 0, decoded);
+       if (ret != MM_UTIL_ERROR_NONE)
+               mm_util_error("_read_bmp failed (%d)", ret);
+
+       mm_util_fleave();
+
+       return ret;
+}
+
+int mm_util_decode_from_bmp_memory(void *memory, const size_t src_size, mm_util_bmp_data *decoded)
+{
+       int ret = MM_UTIL_ERROR_NONE;
+
+       mm_util_retvm_if((memory == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
+       mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+
+       mm_util_fenter();
+
+       ret = _read_bmp(NULL, memory, src_size, decoded);
+       if (ret != MM_UTIL_ERROR_NONE)
+               mm_util_error("_read_bmp failed (%d)", ret);
+
+       mm_util_fleave();
+
+       return ret;
+}
+
+int mm_util_encode_bmp_to_file(mm_util_bmp_data *decoded, const char *filename)
+{
+       int ret = MM_UTIL_ERROR_NONE;
+
+       mm_util_retvm_if(!MMUTIL_STRING_VALID(filename), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
+       mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+
+       mm_util_fenter();
+
+       ret = _write_bmp(decoded, filename, NULL, NULL);
+       if (ret != MM_UTIL_ERROR_NONE)
+               mm_util_error("_write_bmp failed (%d)", ret);
+
+       mm_util_fleave();
+
+       return ret;
+}
+
 int mm_util_encode_bmp_to_memory(mm_util_bmp_data *decoded, void **buffer, size_t *size)
 {
-       bmpfile_t *bmp;
-       rgb_pixel_t pixel = { 0, 0, 0, 0 };
-       uint16_t row, col;
-       uint8_t *image;
+       int ret = MM_UTIL_ERROR_NONE;
 
-       if ((bmp = bmp_create(decoded->width, decoded->height, BYTES_PER_PIXEL * 8)) == NULL) {
-               mm_util_error("Invalid depth value.");
-               return MM_UTIL_ERROR_INVALID_OPERATION;
-       }
+       mm_util_retvm_if((decoded == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid color image");
+       mm_util_retvm_if((buffer == NULL || size == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid bmp image");
 
-       image = (uint8_t *) decoded->data;
-       for (row = 0; row != decoded->height; row++) {
-               for (col = 0; col != decoded->width; col++) {
-                       size_t z = (row * decoded->width + col) * BYTES_PER_PIXEL;
-                       pixel.red = image[z];
-                       pixel.green = image[z + 1];
-                       pixel.blue = image[z + 2];
-                       bmp_set_pixel(bmp, col, row, pixel);
-               }
-       }
+       mm_util_fenter();
 
-       if (bmp_save2(bmp, buffer, size) == false) {
-               mm_util_error("Saving bmp was failed.");
-               bmp_destroy(bmp);
-               MMUTIL_SAFE_FREE(*buffer);
-               *size = 0;
-               return MM_UTIL_ERROR_INVALID_OPERATION;
-       }
-       bmp_destroy(bmp);
-       return MM_UTIL_ERROR_NONE;
+       ret = _write_bmp(decoded, NULL, buffer, size);
+       if (ret != MM_UTIL_ERROR_NONE)
+               mm_util_error("_write_bmp failed (%d)", ret);
+
+       mm_util_fleave();
+
+       return ret;
 }
index 3b2b3b7..da8a6e3 100755 (executable)
@@ -33,8 +33,8 @@
 
 #define DECODE_FILE_PATH       tzplatform_mkpath(TZ_USER_CONTENT, "bmp_test_dec_file.raw")
 #define DECODE_MEM_PATH                tzplatform_mkpath(TZ_USER_CONTENT, "bmp_test_dec_mem.raw")
-#define ENCODE_FILE_PATH       tzplatform_mkpath(TZ_USER_CONTENT, "bmp_test_enc_file.jpg")
-#define ENCODE_MEM_PATH                tzplatform_mkpath(TZ_USER_CONTENT, "bmp_test_enc_mem.jpg")
+#define ENCODE_FILE_PATH       tzplatform_mkpath(TZ_USER_CONTENT, "bmp_test_enc_file.bmp")
+#define ENCODE_MEM_PATH                tzplatform_mkpath(TZ_USER_CONTENT, "bmp_test_enc_mem.bmp")
 
 typedef enum {
        TEST_AUTO,
index 422dcbc..45a8168 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       libmm-utility
 Summary:    Multimedia Framework Utility Library
-Version:    0.1.18
+Version:    0.1.19
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0