#include <stdlib.h>
#include <image_util.h>
+#include <unistd.h> /* access */
#include <maps_view_plugin.h>
#include <maps_condition.h>
return IMAGE_UTIL_COLORSPACE_RGBA8888;
}
+static int __encode_to_jpeg(unsigned char *image_buffer, int w, int h,
+ maps_view_colorspace_type_e cs, int quality, const char *file_path)
+{
+ if (!image_buffer || !file_path || (file_path && *file_path == '\0'))
+ return MAPS_ERROR_INVALID_OPERATION;
+#if (TIZEN_VER >= VERSION(3, 0, 0))
+ image_util_encode_h image_handle = NULL;
+ unsigned long long image_size = 0;
+ int error = IMAGE_UTIL_ERROR_NONE;
+
+ do {
+ error = image_util_encode_create(IMAGE_UTIL_JPEG, &image_handle);
+ if (error != IMAGE_UTIL_ERROR_NONE) {
+ MAPS_LOGE("Failed to create the image handle with JPEG format. error=%d", error);
+ break;
+ }
+ error = image_util_encode_set_input_buffer(image_handle, image_buffer);
+ if (error != IMAGE_UTIL_ERROR_NONE) {
+ MAPS_LOGE("Failed to set image_buffer to image handle. error=%d", error);
+ break;
+ }
+ error = image_util_encode_set_resolution(image_handle, w, h);
+ if (error != IMAGE_UTIL_ERROR_NONE) {
+ MAPS_LOGE("Failed to set resolution to image handle. error=%d", error);
+ break;
+ }
+ error = image_util_encode_set_colorspace(image_handle, __convert_colorspace(cs));
+ if (error != IMAGE_UTIL_ERROR_NONE) {
+ MAPS_LOGE("Failed to set colorspace to image handle. error=%d", error);
+ break;
+ }
+ error = image_util_encode_set_quality(image_handle, quality);
+ if (error != IMAGE_UTIL_ERROR_NONE) {
+ MAPS_LOGE("Failed to set quality to image handle. error=%d", error);
+ break;
+ }
+ error = image_util_encode_set_output_path(image_handle, file_path);
+ if (error != IMAGE_UTIL_ERROR_NONE) {
+ MAPS_LOGE("Failed to set path to image handle. error=%d", error);
+ break;
+ }
+ error = image_util_encode_run(image_handle, &image_size);
+ if (error != IMAGE_UTIL_ERROR_NONE) {
+ MAPS_LOGE("Failed to encode with image handle. error=%d", error);
+ break;
+ }
+ } while (0);
+
+ image_util_encode_destroy(image_handle);
+ image_handle = NULL;
+#else
+ int error = image_util_encode_jpeg(image_buffer, w, h,
+ __convert_colorspace(cs), quality, file_path);
+ if (error != IMAGE_UTIL_ERROR_NONE) {
+ MAPS_LOGD("image_buffer=%p, w=%d, h=%d, cs=%d, quality=%d, file_path=%s",
+ image_buffer, w, h, cs, quality, file_path);
+ MAPS_LOGE("Failed to encode it with JPEG format. error=%d", error);
+ }
+#endif
+
+ return (error == IMAGE_UTIL_ERROR_NONE ? MAPS_ERROR_NONE : MAPS_ERROR_INVALID_OPERATION);
+}
+
+static int __encode_to_bmp(unsigned char *image_buffer, int w, int h,
+ maps_view_colorspace_type_e cs, const char *file_path)
+{
+ if (!image_buffer || !file_path || (file_path && *file_path == '\0'))
+ return MAPS_ERROR_INVALID_OPERATION;
+
+ int error = MAPS_ERROR_NONE;
+
+ if (cs == MAPS_VIEW_COLORSPACE_RGBA8888) {
+ __convert_rgba_to_bgra(image_buffer, w, h);
+ cs = MAPS_VIEW_COLORSPACE_BGRA8888;
+ }
+
+ if (!__encode_bitmap_file(image_buffer, w, h, file_path)) {
+ MAPS_LOGE("Failed to store it to a file.");
+ error = MAPS_ERROR_INVALID_OPERATION;
+ }
+ return error;
+}
+
+static int __build_tmp_path(const char *path, char **tmp_path)
+{
+ if (!path || !tmp_path)
+ return MAPS_ERROR_INVALID_PARAMETER;
+
+ int error = MAPS_ERROR_NONE;
+ int tmp_path_len = strlen(path);
+ *tmp_path = (char*)malloc(tmp_path_len + 10);
+ if (!*tmp_path)
+ return MAPS_ERROR_INVALID_OPERATION;
+
+ int retry = 0;
+ const int retry_max = 20;
+ sprintf(*tmp_path, "%s.tmp", path);
+ while(access(*tmp_path, F_OK) != -1 && ++retry < retry_max) {
+ sprintf(*tmp_path, "%s.tmp%d", path, retry);
+ }
+ if (retry >= retry_max) {
+ MAPS_LOGD("There are already too many temporary files.");
+ error = MAPS_ERROR_INVALID_OPERATION;
+ free(*tmp_path);
+ *tmp_path = NULL;
+ }
+ return error;
+}
+
+
EXPORT_API int maps_view_capture_snapshot(maps_view_h view,
maps_view_snapshot_format_type_e type,
int quality,
int w, h;
unsigned char *image_buffer = NULL;
maps_view_colorspace_type_e cs = MAPS_VIEW_COLORSPACE_RGBA8888;
- char *fname = NULL;
+ char *tmp_path = NULL;
do {
- if (!__get_plugin_interface(view)->maps_plugin_capture_snapshot)
- return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
+ if (!__get_plugin_interface(view)->maps_plugin_capture_snapshot) {
+ error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
+ break;
+ }
error = __get_plugin_interface(view)->maps_plugin_capture_snapshot(view,
(void**)&image_buffer, &w, &h, &cs);
if (error != MAPS_ERROR_NONE) break;
+
if (cs != MAPS_VIEW_COLORSPACE_RGBA8888 && cs != MAPS_VIEW_COLORSPACE_BGRA8888) {
MAPS_LOGE("The color space is not supported yet. (%d)", cs);
error = MAPS_ERROR_INVALID_OPERATION;
break;
}
- int fname_len = strlen(path) + 4;
- fname = (char*)malloc(fname_len + 1);
- if (!fname)
- return MAPS_ERROR_INVALID_OPERATION;
-
- memset(fname, 0, fname_len + 1);
- sprintf(fname, "%s.tmp", path);
+ error = __build_tmp_path(path, &tmp_path);
+ if (error != MAPS_ERROR_NONE) break;
if (type == MAPS_VIEW_SNAPSHOT_JPEG) {
- error = image_util_encode_jpeg(image_buffer, w, h,
- __convert_colorspace(cs), quality, fname);
- if (error != IMAGE_UTIL_ERROR_NONE) {
- MAPS_LOGD("image_buffer=%p, w=%d, h=%d, cs=%d, quality=%d, fname=%s",
- image_buffer, w, h, cs, quality, fname);
- MAPS_LOGE("Failed to encode it with JPEG format. error=%d", error);
- error = MAPS_ERROR_INVALID_OPERATION;
- }
+ error = __encode_to_jpeg(image_buffer, w, h, cs, quality, tmp_path);
} else if (type == MAPS_VIEW_SNAPSHOT_BMP) {
- if (cs == MAPS_VIEW_COLORSPACE_RGBA8888) {
- MAPS_LOGD("RGBA -> BGRA");
- __convert_rgba_to_bgra(image_buffer, w, h);
- cs = MAPS_VIEW_COLORSPACE_BGRA8888;
- }
- if (!__encode_bitmap_file(image_buffer, w, h, fname)) {
- MAPS_LOGE("Failed to store it to a file.");
- error = MAPS_ERROR_INVALID_OPERATION;
- }
+ error = __encode_to_bmp(image_buffer, w, h, cs, tmp_path);
} else {
error = MAPS_ERROR_INVALID_PARAMETER;
}
} while (0);
- if (fname) {
+ if (tmp_path) {
if (error == MAPS_ERROR_NONE) {
+ MAPS_LOGD("The snapshot is saved to %s", path);
remove(path);
- rename(fname, path);
+ rename(tmp_path, path);
} else {
- remove(fname);
+ remove(tmp_path);
}
- free(fname);
+ free(tmp_path);
}
g_free(image_buffer);
return error;