Add internal APIs of encoding animated-gif frame by frame
[platform/core/api/image-util.git] / src / image_util_internal.c
1 /*
2 * Copyright (c) 2011 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 <image_util.h>
18 #include <image_util_internal.h>
19 #include <image_util_private.h>
20
21 int image_util_frame_create(void *decode_encode_h, image_util_frame_h *frame_h)
22 {
23         int ret = IMAGE_UTIL_ERROR_NONE;
24
25         image_util_retvm_if((decode_encode_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
26         image_util_retvm_if((frame_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
27
28         decode_encode_s *image = (decode_encode_s *)decode_encode_h;
29         image_util_retvm_if((image->image_h == NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The image handle is wrong");
30         image_util_retvm_if((image->width == 0) || (image->height == 0), MM_UTIL_ERROR_INVALID_OPERATION, "The resolution is wrong");
31
32         frame_s *frame = calloc(1, sizeof(frame_s));
33         if (frame == NULL) {
34                 image_util_error("Memory allocation is failed.");
35                 return MM_UTIL_ERROR_OUT_OF_MEMORY;
36         }
37
38         if (image->image_type == IMAGE_UTIL_GIF) {
39                 mm_gif_file_h gif_data = (mm_gif_file_h)image->image_h;
40                 mm_gif_image_h gif_frame = NULL;
41
42                 ret = mm_util_gif_image_create(gif_data, &gif_frame);
43                 if (ret != IMAGE_UTIL_ERROR_NONE) {
44                         image_util_error("mm_util_gif_image_create is failed(%d).", ret);
45                         IMAGE_UTIL_SAFE_FREE(frame);
46                         return MM_UTIL_ERROR_INVALID_OPERATION;
47                 }
48                 frame->frame_h = gif_frame;
49         } else {
50                 image_util_error("The image type(%d) is not supported.", image->image_type);
51                 IMAGE_UTIL_SAFE_FREE(frame);
52                 return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
53         }
54
55         *frame_h = frame;
56
57         return IMAGE_UTIL_ERROR_NONE;
58 }
59
60 int image_util_frame_set_resolution(image_util_frame_h frame_h, const int width, const int height)
61 {
62         int ret = IMAGE_UTIL_ERROR_NONE;
63
64         image_util_retvm_if((frame_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
65         image_util_retvm_if((width <= 0) || (height <= 0), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Width or Height");
66
67         frame_s *frame = (frame_s *)frame_h;
68
69         ret = mm_util_gif_image_set_position(frame->frame_h, 0, 0, width, height);
70         if (ret != IMAGE_UTIL_ERROR_NONE) {
71                 image_util_error("mm_util_gif_image_set_position is failed(%d).", ret);
72                 return MM_UTIL_ERROR_INVALID_OPERATION;
73         }
74
75         return IMAGE_UTIL_ERROR_NONE;
76 }
77
78 int image_util_frame_set_gif_delay(image_util_frame_h frame_h, const int delay_time)
79 {
80         int ret = IMAGE_UTIL_ERROR_NONE;
81
82         image_util_retvm_if((frame_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
83         image_util_retvm_if((delay_time <= 0), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Delay Time");
84
85         frame_s *frame = (frame_s *)frame_h;
86
87         ret = mm_util_gif_image_set_delay_time(frame->frame_h, delay_time);
88         if (ret != IMAGE_UTIL_ERROR_NONE) {
89                 image_util_error("mm_util_gif_image_set_delay_time is failed(%d).", ret);
90                 return MM_UTIL_ERROR_INVALID_OPERATION;
91         }
92
93         return IMAGE_UTIL_ERROR_NONE;
94 }
95
96 int image_util_frame_set_frame(image_util_frame_h frame_h, unsigned char *buffer, const unsigned long size)
97 {
98         int ret = IMAGE_UTIL_ERROR_NONE;
99
100         image_util_retvm_if((frame_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
101         image_util_retvm_if((buffer == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Buffer");
102         image_util_retvm_if((size == 0), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Buffer Size");
103
104         frame_s *frame = (frame_s *)frame_h;
105
106         ret = mm_util_gif_image_set_image(frame->frame_h, buffer, size);
107         if (ret != IMAGE_UTIL_ERROR_NONE) {
108                 image_util_error("mm_util_gif_image_set_image is failed(%d).", ret);
109                 return MM_UTIL_ERROR_INVALID_OPERATION;
110         }
111
112         return IMAGE_UTIL_ERROR_NONE;
113 }
114
115 void image_util_frame_destroy(image_util_frame_h frame_h)
116 {
117         image_util_retm_if((frame_h == NULL), "Invalid Handle");
118
119         frame_s *frame = (frame_s *)frame_h;
120         mm_util_gif_image_destory(frame->frame_h);
121 }
122
123 int image_util_encode_add_frame(image_util_encode_h encode_h, image_util_frame_h frame_h)
124 {
125         int ret = IMAGE_UTIL_ERROR_NONE;
126
127         image_util_retvm_if((encode_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
128         image_util_retvm_if((frame_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
129
130         decode_encode_s *encode = (decode_encode_s *)encode_h;
131         frame_s *frame = (frame_s *)frame_h;
132         image_util_retvm_if((encode->image_h == NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The image handle is wrong");
133         image_util_retvm_if((frame->frame_h == NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The frame handle is wrong");
134
135         if (encode->image_type == IMAGE_UTIL_GIF) {
136                 mm_gif_file_h gif_data = (mm_gif_file_h)encode->image_h;
137
138                 ret = mm_util_gif_encode_add_image(gif_data, (mm_gif_image_h)frame->frame_h);
139                 if (ret != IMAGE_UTIL_ERROR_NONE) {
140                         image_util_error("mm_util_gif_encode_add_image is failed(%d).", ret);
141                         mm_util_gif_encode_destroy(gif_data);
142                         return MM_UTIL_ERROR_INVALID_OPERATION;
143                 }
144         } else {
145                 image_util_error("The image type(%d) is not supported.", encode->image_type);
146                 return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
147         }
148
149         return IMAGE_UTIL_ERROR_NONE;
150 }
151
152 int image_util_encode_save(image_util_encode_h encode_h)
153 {
154         int ret = IMAGE_UTIL_ERROR_NONE;
155
156         image_util_retvm_if((encode_h == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
157
158         decode_encode_s *encode = (decode_encode_s *)encode_h;
159         image_util_retvm_if((encode->image_h == NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The image handle is wrong");
160
161         if (encode->image_type == IMAGE_UTIL_GIF) {
162                 mm_gif_file_h gif_data = (mm_gif_file_h)encode->image_h;
163
164                 ret = mm_util_gif_encode_save(gif_data);
165                 if (ret != IMAGE_UTIL_ERROR_NONE) {
166                         image_util_error("mm_util_gif_encode_save is failed(%d).", ret);
167                         mm_util_gif_encode_destroy(gif_data);
168                         return MM_UTIL_ERROR_INVALID_OPERATION;
169                 }
170         } else {
171                 image_util_error("The image type(%d) is not supported.", encode->image_type);
172                 return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
173         }
174
175         return IMAGE_UTIL_ERROR_NONE;
176 }