a09c4d533f3078418b6d038b921ed52e0c8f5da0
[platform/core/multimedia/libmm-utility.git] / common / mm_util_image.c
1 /*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18
19 #include "mm_util_private.h"
20
21 gboolean mm_image_is_valid_image(mm_util_image_h image)
22 {
23         mm_image_info_s *_image = (mm_image_info_s *)image;
24
25         mm_util_retvm_if(image == NULL, FALSE, "Invalid image");
26         mm_util_retvm_if(_image->width == 0, FALSE, "Invalid width");
27         mm_util_retvm_if(_image->height == 0, FALSE, "Invalid height");
28         mm_util_retvm_if(!IS_VALID_COLOR(_image->color), FALSE, "Invalid color [%d]", _image->color);
29         mm_util_retvm_if((_image->data == NULL) || (_image->size == 0), FALSE, "Invalid data [%zu, %p]", _image->size, _image->data);
30
31         return TRUE;
32 }
33
34 void mm_image_debug_image(mm_util_image_h image, const char *message)
35 {
36         mm_image_info_s *_image = (mm_image_info_s *)image;
37
38         mm_util_retm_if(image == NULL, "Invalid image");
39
40         if (message)
41                 mm_util_sec_debug("[%s] w [%u], h [%u], color [%u], data [%p], size [%zu]", message,
42                         _image->width, _image->height, _image->color, _image->data, _image->size);
43         else
44                 mm_util_sec_debug("w [%u], h [%u], color [%u], data [%p], size [%zu]",
45                         _image->width, _image->height, _image->color, _image->data, _image->size);
46 }
47
48 int mm_image_create_image(unsigned int width, unsigned int height,
49                 mm_util_color_format_e color, const unsigned char *data, size_t size,
50                 mm_util_image_h *image)
51 {
52         mm_image_info_s *_image = NULL;
53
54         mm_util_retvm_if(image == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid image");
55         mm_util_retvm_if(width == 0, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid width");
56         mm_util_retvm_if(height == 0, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid height");
57         mm_util_retvm_if(!IS_VALID_COLOR(color), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid color [%d]", color);
58         mm_util_retvm_if(data == NULL || (size == 0), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid data");
59
60         mm_util_sec_debug("w [%u], h [%u], color [%u], data [%p], size [%zu]", width, height, color, data, size);
61
62         _image = g_new0(mm_image_info_s, 1);
63
64         /* Just TEMP_DATA_SIZE has been used internally.
65          * It will be removed after removing deprecated CAPIs. */
66         if (size == TEMP_DATA_SIZE)
67                 _image->data = (unsigned char *)data;
68         else
69                 _image->data = g_memdup(data, size);
70
71         _image->size = size;
72         _image->width = width;
73         _image->height = height;
74         _image->color = color;
75
76         *image = (mm_util_image_h)_image;
77
78         mm_image_debug_image(*image, "Create Image");
79
80         return MM_UTIL_ERROR_NONE;
81 }
82
83 int mm_image_clone_image(mm_util_image_h src, mm_util_image_h *dst)
84 {
85         mm_image_info_s *_src = (mm_image_info_s *)src;
86         mm_image_info_s *_dst = NULL;
87
88         mm_util_retvm_if(!src, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid src");
89         mm_util_retvm_if(!dst, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid dst");
90
91         _dst = g_new0(mm_image_info_s, 1);
92
93         _dst->width = _src->width;
94         _dst->height = _src->height;
95         _dst->color = _src->color;
96         _dst->size = _src->size;
97
98         if (_src->size == TEMP_DATA_SIZE)
99                 _dst->data = _src->data;
100         else
101                 _dst->data = g_memdup(_src->data, _dst->size);
102
103         *dst = (mm_util_image_h)_dst;
104
105         return MM_UTIL_ERROR_NONE;
106 }
107
108 int mm_image_set_delay_time(mm_util_image_h image, unsigned int delay_time)
109 {
110         mm_image_info_s *_image = (mm_image_info_s *)image;
111
112         mm_util_retvm_if(image == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid image");
113
114         _image->delay_time = delay_time;
115
116         return MM_UTIL_ERROR_NONE;
117 }
118
119 int mm_image_get_delay_time(mm_util_image_h image, unsigned int *delay_time)
120 {
121         mm_image_info_s *_image = (mm_image_info_s *)image;
122
123         mm_util_retvm_if(image == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid image");
124         mm_util_retvm_if(delay_time == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid delay_time");
125
126         *delay_time = _image->delay_time;
127
128         return MM_UTIL_ERROR_NONE;
129 }
130
131 int mm_image_get_image(mm_util_image_h image, unsigned int *width,
132                 unsigned int *height, mm_util_color_format_e *color,
133                 unsigned char **data, size_t *size)
134 {
135         mm_image_info_s *_image = (mm_image_info_s *)image;
136
137         mm_util_retvm_if(!IS_VALID_IMAGE(image), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid image");
138         /* Just TEMP_DATA_SIZE has been used internally.
139          * It will be removed after removing deprecated CAPIs. */
140         mm_util_retvm_if(_image->size == TEMP_DATA_SIZE, MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid size");
141
142         if (width)
143                 *width = _image->width;
144
145         if (height)
146                 *height = _image->height;
147
148         if (color)
149                 *color = _image->color;
150
151         if (data && size) {
152                 *data = g_memdup(_image->data, _image->size);
153                 *size = _image->size;
154         }
155
156         return MM_UTIL_ERROR_NONE;
157 }
158
159 void mm_image_destroy_image(mm_util_image_h image)
160 {
161         mm_image_info_s *_image = (mm_image_info_s *)image;
162
163         if (!image)
164                 return;
165
166         /* Just TEMP_DATA_SIZE has been used internally.
167          * It will be removed after removing deprecated CAPIs. */
168         if (_image->size != TEMP_DATA_SIZE)
169                 g_free(_image->data);
170         g_free(_image);
171 }