Add APIs for encoding options to use Magick APIs. 49/238249/15
authorhj kim <backto.kim@samsung.com>
Fri, 10 Jul 2020 03:48:04 +0000 (12:48 +0900)
committerhj kim <backto.kim@samsung.com>
Wed, 15 Jul 2020 07:52:54 +0000 (16:52 +0900)
Now only BMP format uses MagicK for encoding.
Other formats such as PNG, WEBP, JPEG uses their own library. (eg. libjpeg, libpng etc)
But MagicK supports all those kind of formats. so we will cover all format with MagicK.
Each format has specific options when encoding but current functions didn't consider that.
So new APIs for encoding options has been added.

Change-Id: I1b5908d6176c5d55f7abcc68f88b3bebf07b9eb3

magick/include/mm_util_magick.h
magick/mm_util_magick.c
packaging/libmm-utility.spec

index 110a9f1..d66b42a 100644 (file)
@@ -29,12 +29,14 @@ extern "C" {
 #include "mm_util_type.h"
 #include "mm_util_image.h"
 
+typedef void *mm_util_enc_opt_h;
+
 typedef enum {
-       IMG_CODEC_GIF = 0,
+       IMG_CODEC_JPEG = 0,
        IMG_CODEC_PNG,
-       IMG_CODEC_WBMP,
-       IMG_CODEC_JPEG,
+       IMG_CODEC_GIF,
        IMG_CODEC_BMP,
+       IMG_CODEC_WBMP = 100,           // used by only media-content
        IMG_CODEC_UNKNOWN_TYPE,
 } mm_util_img_codec_type;
 
@@ -56,8 +58,13 @@ int mm_util_extract_image_info(const char *path, mm_util_img_codec_type *type, u
 
 int mm_util_decode_image_from_file(const char *path, mm_util_color_format_e format, mm_util_image_h *decoded_image);
 int mm_util_decode_image_from_buffer(const void *buf, size_t buf_size, mm_util_color_format_e format, mm_util_image_h *decoded_image);
-int mm_util_encode_image_to_file(mm_util_image_h decoded_image, const char *path);
-int mm_util_encode_image_to_buffer(mm_util_image_h decoded_image, mm_util_img_codec_type type, void **buf, size_t *buf_size);
+int mm_util_encode_image_to_file(mm_util_image_h decoded_image, mm_util_enc_opt_h opt, const char *path);
+int mm_util_encode_image_to_buffer(mm_util_image_h decoded_image, mm_util_enc_opt_h opt, void **buf, size_t *buf_size);
+
+int mm_util_enc_opt_create(mm_util_enc_opt_h *enc_opt);
+int mm_util_enc_opt_set_codec(mm_util_enc_opt_h enc_opt, mm_util_img_codec_type codec);
+int mm_util_enc_opt_set_png_compression(mm_util_enc_opt_h enc_opt, unsigned int compression);
+void mm_util_enc_opt_destroy(mm_util_enc_opt_h enc_opt);
 
 #ifdef __cplusplus
 }
index a9f5b83..93ad0cd 100644 (file)
 #include "mm_util_private.h"
 #include "mm_util_magick.h"
 
+#define DEFAULT_PNG_COMPRESSION 6
+
+typedef struct {
+       mm_util_img_codec_type codec;
+       unsigned int compression;
+} mm_util_enc_opt_t;
+
 static bool __mm_util_check_rotation(mm_util_rotate_type_e rotation);
 
 static void __mm_util_magick_log_method(const ExceptionType excep, const char *message)
@@ -301,7 +308,7 @@ static Image * __mm_util_read_image_from_buffer(const void *buf, size_t buf_size
        return _image;
 }
 
-static int __mm_util_write_image_to_file(Image *image, const char *out_path)
+static int __mm_util_write_image_to_file(Image *image, mm_util_enc_opt_t *option, const char *out_path)
 {
        int ret = MM_UTIL_ERROR_NONE;
        ImageInfo *_image_info = NULL;
@@ -320,14 +327,41 @@ static int __mm_util_write_image_to_file(Image *image, const char *out_path)
        SAFE_STRLCPY(image->filename, out_path, sizeof(image->filename));
        image->filename[MaxTextExtent-1] = '\0';
 
-       AddDefinition(_image_info, "jpeg", "dct-method", "FASTEST", &exception);
-       AddDefinition(_image_info, "jpeg", "optimize-coding", "FALSE", &exception);
        DeleteImageProfile(image, "EXIF");
        DeleteImageProfile(image, "8BIM");
        DeleteImageProfile(image, "ICM");
        DeleteImageProfile(image, "IPTC");
        DeleteImageProfile(image, "XMP");
 
+       if (option) {
+               switch(option->codec) {
+               case IMG_CODEC_JPEG:
+                       AddDefinition(_image_info, "jpeg", "dct-method", "FASTEST", &exception);
+                       AddDefinition(_image_info, "jpeg", "optimize-coding", "FALSE", &exception);
+                       break;
+
+               case IMG_CODEC_PNG:
+                       mm_util_sec_debug("PNG compression: %d", option->compression);
+                       _image_info->quality = option->compression * 10;
+                       break;
+
+               case IMG_CODEC_GIF:
+                       /* fall through */
+               case IMG_CODEC_BMP:
+                       /* fall through */
+               case IMG_CODEC_WBMP:
+                       break;
+
+               default:
+                       mm_util_error("invalid codec [%d]", option->codec);
+                       break;
+               }
+       } else {
+               /* used by only media-content */
+               AddDefinition(_image_info, "jpeg", "dct-method", "FASTEST", &exception);
+               AddDefinition(_image_info, "jpeg", "optimize-coding", "FALSE", &exception);
+       }
+
        if (WriteImage (_image_info, image) == MagickFalse) {
                mm_util_error("Error: Writing Image failed.");
                if (exception.severity != UndefinedException)
@@ -577,7 +611,7 @@ int mm_util_rotate_B_P(mm_util_image_h src_handle, mm_util_rotate_type_e rotatio
                goto ERROR;
        }
 
-       ret = __mm_util_write_image_to_file(_processed_image, dst_path);
+       ret = __mm_util_write_image_to_file(_processed_image, NULL, dst_path);
 
 ERROR:
 
@@ -658,7 +692,7 @@ int mm_util_rotate_P_P(const char *src_path, mm_util_rotate_type_e rotation, con
                goto ERROR;
        }
 
-       ret = __mm_util_write_image_to_file(_processed_image, dst_path);
+       ret = __mm_util_write_image_to_file(_processed_image, NULL, dst_path);
 
 ERROR:
 
@@ -751,7 +785,7 @@ int mm_util_resize_B_P(mm_util_image_h src_handle, unsigned int req_width, unsig
                goto ERROR;
        }
 
-       ret = __mm_util_write_image_to_file(_processed_image, dst_path);
+       ret = __mm_util_write_image_to_file(_processed_image, NULL, dst_path);
 
 ERROR:
 
@@ -843,7 +877,7 @@ int mm_util_resize_P_P(const char *src_path, unsigned int req_width, unsigned in
                goto ERROR;
        }
 
-       ret = __mm_util_write_image_to_file(_processed_image, dst_path);
+       ret = __mm_util_write_image_to_file(_processed_image, NULL, dst_path);
 
 ERROR:
 
@@ -947,22 +981,20 @@ ERROR:
        return ret;
 }
 
-int mm_util_encode_image_to_file(mm_util_image_h decoded_image, const char *path)
+int mm_util_encode_image_to_file(mm_util_image_h decoded_image, mm_util_enc_opt_h opt, const char *path)
 {
        int ret = MM_UTIL_ERROR_NONE;
-       char *extension = NULL;
        Image *_image = NULL;
        mm_util_image_h converted_image = NULL, source = NULL;
+       mm_util_enc_opt_t *_opt = (mm_util_enc_opt_t *)opt;
 
        mm_util_retvm_if(!decoded_image, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid decoded_image");
+       mm_util_retvm_if(!_opt, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid opt");
        mm_util_retvm_if(!MMUTIL_STRING_VALID(path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid path");
 
        mm_util_sec_debug("path [%s]", path);
 
-       extension = g_strrstr(path, ".");
-       mm_util_retvm_if(!MMUTIL_STRING_VALID(extension), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid path");
-
-       if (g_ascii_strcasecmp(extension, ".bmp") == 0) {
+       if (_opt->codec == IMG_CODEC_BMP) {
                /*
                  GraphicsMagick doesn't support alpha overlay(compression method:BI_ALPHABITFIELDS) for bmp.
                  Officialy BMP format supports alpha overlay since BMP4(version 4), but GraphicsMagick
@@ -992,7 +1024,7 @@ int mm_util_encode_image_to_file(mm_util_image_h decoded_image, const char *path
                goto ERROR;
        }
 
-       ret = __mm_util_write_image_to_file(_image, path);
+       ret = __mm_util_write_image_to_file(_image, _opt, path);
 
 ERROR:
 
@@ -1005,21 +1037,20 @@ ERROR:
        return ret;
 }
 
-int mm_util_encode_image_to_buffer(mm_util_image_h decoded_image, mm_util_img_codec_type type, void **buf, size_t *buf_size)
+int mm_util_encode_image_to_buffer(mm_util_image_h decoded_image, mm_util_enc_opt_h opt, void **buf, size_t *buf_size)
 {
        int ret = MM_UTIL_ERROR_NONE;
        char *tmp_file = NULL;
 
        mm_util_retvm_if(!decoded_image, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid decoded_image");
+       mm_util_retvm_if(!opt, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid opt");
        mm_util_retvm_if(!buf, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid buf");
        mm_util_retvm_if(buf_size == 0, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid buf_size");
 
-       mm_util_sec_debug("type [%d]", type);
-
-       ret = __mm_util_make_tmp_file(type, &tmp_file);
+       ret = __mm_util_make_tmp_file(((mm_util_enc_opt_t *)opt)->codec, &tmp_file);
        mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "Error: __mm_util_make_tmp_file failed.");
 
-       ret = mm_util_encode_image_to_file(decoded_image, tmp_file);
+       ret = mm_util_encode_image_to_file(decoded_image, opt, tmp_file);
        if (ret != MM_UTIL_ERROR_NONE) {
                mm_util_error("Error: mm_util_encode_image_P failed.");
                goto ERROR;
@@ -1119,7 +1150,7 @@ int mm_util_resize_and_rotate_P_P(const char *src_path, unsigned int req_width,
                _write_image->is_grayscale = MagickFalse;
        }
 
-       ret = __mm_util_write_image_to_file(_write_image, dst_path);
+       ret = __mm_util_write_image_to_file(_write_image, NULL, dst_path);
 
 ERROR:
 
@@ -1130,3 +1161,47 @@ ERROR:
 
        return ret;
 }
+
+int mm_util_enc_opt_create(mm_util_enc_opt_h *enc_opt)
+{
+       mm_util_enc_opt_t *enc_option = NULL;
+
+       mm_util_retvm_if(!enc_opt, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid enc_opt");
+
+       enc_option = g_new0(mm_util_enc_opt_t, 1);
+       enc_option->codec = IMG_CODEC_UNKNOWN_TYPE;
+       enc_option->compression = DEFAULT_PNG_COMPRESSION;
+
+       *enc_opt = enc_option;
+
+       return MM_UTIL_ERROR_NONE;
+}
+
+int mm_util_enc_opt_set_codec(mm_util_enc_opt_h enc_opt, mm_util_img_codec_type codec)
+{
+       mm_util_enc_opt_t *enc_option = (mm_util_enc_opt_t *)enc_opt;
+
+       mm_util_retvm_if(!enc_opt, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid enc_opt");
+
+       enc_option->codec = codec;
+
+       return MM_UTIL_ERROR_NONE;
+}
+
+int mm_util_enc_opt_set_png_compression(mm_util_enc_opt_h enc_opt, unsigned int compression)
+{
+       mm_util_enc_opt_t *enc_option = (mm_util_enc_opt_t *)enc_opt;
+
+       mm_util_retvm_if(!enc_opt, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid enc_opt");
+       mm_util_retvm_if(enc_option->codec != IMG_CODEC_PNG, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid codec [%d]", enc_option->codec);
+       mm_util_retvm_if(compression > 9, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid compression [%d]", compression);
+
+       enc_option->compression = compression;
+
+       return MM_UTIL_ERROR_NONE;
+}
+
+void mm_util_enc_opt_destroy(mm_util_enc_opt_h enc_opt)
+{
+       g_free(enc_opt);
+}
\ No newline at end of file
index a531776..65524e5 100644 (file)
@@ -1,6 +1,6 @@
 Name:       libmm-utility
 Summary:    Multimedia Framework Utility Library
-Version:    0.1.47
+Version:    0.1.48
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0