Add to support saving bmp to memory 73/133073/3 accepted/tizen/unified/20170612.170907 submit/tizen/20170612.052408
authorJiyong Min <jiyong.min@samsung.com>
Fri, 9 Jun 2017 01:23:07 +0000 (10:23 +0900)
committerJiyong Min <jiyong.min@samsung.com>
Fri, 9 Jun 2017 01:28:13 +0000 (10:28 +0900)
Change-Id: I8dbf68b278547fd962925effc6f85c8425c730d5
Signed-off-by: jiyong min <jiyong.min@samsung.com>
bmp/include/mm_util_bmp.h
bmp/mm_util_bmp.c

index 06fb17a..935ac4c 100755 (executable)
@@ -130,6 +130,20 @@ unsigned long long mm_util_bmp_decode_get_size(mm_util_bmp_data * data);
 int mm_util_encode_bmp_to_file(mm_util_bmp_data * encoded, const char *filename);
 
 /**
+ * This function encodes raw data to buffer
+ *
+ * @param encoded  [in ]    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 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
+ * @see                     mm_util_bmp_data
+ * @since                   R1, 1.0
+ */
+int mm_util_encode_bmp_to_memory(mm_util_bmp_data *encoded, void **buffer, size_t *size);
+
+/**
  * This function sets width of the encoded image.
  *
  * @param data               [in]     pointer of mm_util_bmp_data.
index 7e2beb7..174c6fb 100755 (executable)
@@ -38,6 +38,8 @@
 
 #define BYTES_PER_PIXEL 4
 
+#define BMP_SAFE_FREE(src) { if(src != NULL) {free(src); src = NULL;} }
+
 void *__bitmap_create(int width, int height, unsigned int state);
 unsigned char *__bitmap_get_buffer(void *bitmap);
 size_t __bitmap_get_bpp(void *bitmap);
@@ -228,7 +230,39 @@ int mm_util_encode_bmp_to_file(mm_util_bmp_data *encoded, const char *filename)
        uint8_t *image;
 
        if ((bmp = bmp_create(encoded->width, encoded->height, BYTES_PER_PIXEL * 8)) == NULL) {
-               printf("Invalid depth value.\n");
+               mm_util_error("Invalid depth value.");
+               return MM_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       image = (uint8_t *) encoded->data;
+       for (row = 0; row != encoded->height; row++) {
+               for (col = 0; col != encoded->width; col++) {
+                       size_t z = (row * encoded->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);
+               }
+       }
+
+       if (bmp_save(bmp, filename) == false) {
+               mm_util_error("Saving bmp was failed.");
+               bmp_destroy(bmp);
+               return MM_UTIL_ERROR_INVALID_OPERATION;
+       }
+       bmp_destroy(bmp);
+       return MM_UTIL_ERROR_NONE;
+}
+
+int mm_util_encode_bmp_to_memory(mm_util_bmp_data *encoded, void **buffer, size_t *size)
+{
+       bmpfile_t *bmp;
+       rgb_pixel_t pixel = { 0, 0, 0, 0 };
+       uint16_t row, col;
+       uint8_t *image;
+
+       if ((bmp = bmp_create(encoded->width, encoded->height, BYTES_PER_PIXEL * 8)) == NULL) {
+               mm_util_error("Invalid depth value.");
                return MM_UTIL_ERROR_INVALID_OPERATION;
        }
 
@@ -243,7 +277,13 @@ int mm_util_encode_bmp_to_file(mm_util_bmp_data *encoded, const char *filename)
                }
        }
 
-       bmp_save(bmp, filename);
+       if (bmp_save2(bmp, buffer, size) == false) {
+               mm_util_error("Saving bmp was failed.");
+               bmp_destroy(bmp);
+               BMP_SAFE_FREE(buffer);
+               *size = 0;
+               return MM_UTIL_ERROR_INVALID_OPERATION;
+       }
        bmp_destroy(bmp);
        return MM_UTIL_ERROR_NONE;
 }