initial upload
[apps/native/smart-surveillance-camera.git] / src / controller_image.c
1  /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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 <glib.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <tizen.h>
21 #include <image_util.h>
22 #include "log.h"
23 #include "exif.h"
24
25 static image_util_encode_h encode_h = NULL;
26 static image_util_decode_h decode_h = NULL;
27
28 #define IMAGE_COLORSPACE IMAGE_UTIL_COLORSPACE_I420
29
30 void controller_image_initialize(void)
31 {
32         int error_code = image_util_encode_create(IMAGE_UTIL_JPEG, &encode_h);
33         if (error_code != IMAGE_UTIL_ERROR_NONE) {
34                 _E("image_util_encode_create [%s]", get_error_message(error_code));
35         }
36
37         error_code = image_util_decode_create(&decode_h);
38         if (error_code != IMAGE_UTIL_ERROR_NONE) {
39                 _E("image_util_decode_create [%s]", get_error_message(error_code));
40         }
41 }
42
43 void controller_image_finalize(void)
44 {
45         int error_code = image_util_encode_destroy(encode_h);
46         if (error_code != IMAGE_UTIL_ERROR_NONE) {
47                 _E("image_util_encode_destroy [%s]", get_error_message(error_code));
48         }
49
50         error_code = image_util_decode_destroy(decode_h);
51     if (error_code != IMAGE_UTIL_ERROR_NONE) {
52         _E("image_util_decode_destroy [%s]", get_error_message(error_code));
53     }
54 }
55
56 int controller_image_save_image_file(const char *path,
57         unsigned int width, unsigned int height, const unsigned char *buffer,
58         const char *comment, unsigned int comment_len)
59 {
60         unsigned char *encoded = NULL;
61         unsigned long long size = 0;
62
63         int error_code = image_util_encode_set_resolution(encode_h, width, height);
64         if (error_code != IMAGE_UTIL_ERROR_NONE) {
65                 _E("image_util_encode_set_resolution [%s]", get_error_message(error_code));
66                 return -1;
67         }
68
69         error_code = image_util_encode_set_colorspace(encode_h, IMAGE_COLORSPACE);
70         if (error_code != IMAGE_UTIL_ERROR_NONE) {
71                 _E("image_util_encode_set_colorspace [%s]", get_error_message(error_code));
72                 return -1;
73         }
74
75         error_code = image_util_encode_set_quality(encode_h, 90);
76         if (error_code != IMAGE_UTIL_ERROR_NONE) {
77                 _E("image_util_encode_set_quality [%s]", get_error_message(error_code));
78                 return -1;
79         }
80
81         error_code = image_util_encode_set_input_buffer(encode_h, buffer);
82         if (error_code != IMAGE_UTIL_ERROR_NONE) {
83                 _E("image_util_encode_set_input_buffer [%s]", get_error_message(error_code));
84                 return -1;
85         }
86
87         error_code = image_util_encode_set_output_buffer(encode_h, &encoded);
88         if (error_code != IMAGE_UTIL_ERROR_NONE) {
89                 _E("image_util_encode_set_output_buffer [%s]", get_error_message(error_code));
90                 return -1;
91         }
92
93         error_code = image_util_encode_run(encode_h, &size);
94         if (error_code != IMAGE_UTIL_ERROR_NONE) {
95                 _E("image_util_encode_run [%s]", get_error_message(error_code));
96                 return -1;
97         }
98
99         error_code = exif_write_jpg_file_with_comment(path,
100                         encoded, (unsigned int)size, width, height, comment, comment_len);
101
102         free(encoded);
103
104         return error_code;
105 }
106
107 int controller_image_read_image_file(const char *path,
108         unsigned long *width, unsigned long *height, unsigned char *buffer, unsigned long long *size)
109 {
110         int error_code = image_util_decode_set_input_path(decode_h, path);
111         if (error_code != IMAGE_UTIL_ERROR_NONE) {
112                 _E("image_util_decode_set_input_path [%s] [%s]", path, get_error_message(error_code));
113                 return -1;
114         }
115
116         error_code = image_util_decode_set_output_buffer(decode_h, &buffer);
117         if (error_code != IMAGE_UTIL_ERROR_NONE) {
118                 _E("image_util_decode_set_output_buffer [%s]", get_error_message(error_code));
119                 return -1;
120         }
121
122         error_code = image_util_decode_set_colorspace(decode_h, IMAGE_UTIL_COLORSPACE_RGBA8888);
123         if (error_code != IMAGE_UTIL_ERROR_NONE) {
124                 _E("image_util_decode_set_colorspace [%s]", get_error_message(error_code));
125                 return -1;
126         }
127
128         error_code = image_util_decode_set_jpeg_downscale(decode_h, IMAGE_UTIL_DOWNSCALE_1_1);
129         if (error_code != IMAGE_UTIL_ERROR_NONE) {
130                 _E("image_util_decode_set_jpeg_downscale [%s]", get_error_message(error_code));
131                 return -1;
132         }
133
134         error_code = image_util_decode_run(decode_h, width, height, size);
135         if (error_code != IMAGE_UTIL_ERROR_NONE) {
136                 _E("image_util_decode_run [%s]", get_error_message(error_code));
137                 return -1;
138         }
139
140         return error_code;
141 }