summaryrefslogtreecommitdiff
path: root/src/media-thumb-internal.c
blob: bd6ff2d3f492de98fe264f1ede86b6a9e9ed4098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * libmedia-thumbnail
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "media-thumb-debug.h"
#include "media-thumb-util.h"
#include "media-thumb-internal.h"
#include "media-thumb-ipc.h"
#include "media-thumb-db.h"

#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <mm_util_magick.h>

static void __media_thumb_get_proper_thumb_size(unsigned int origin_width, unsigned int origin_height, unsigned int *thumb_width, unsigned int *thumb_height)
{
	bool portrait = false;
	double ratio = 0.0;

	thumb_retm_if(origin_width == 0, "Invalid origin_width");
	thumb_retm_if(origin_height == 0, "Invalid origin_height");
	thumb_retm_if(!thumb_width, "Invalid thumb_width");
	thumb_retm_if(!thumb_height, "Invalid thumb_height");

	if (origin_width < origin_height)
		portrait = true;

	/* Set smaller length to default size */
	if (portrait) {
		if (origin_width < *thumb_width)
			*thumb_width = origin_width;
		ratio = (double)origin_height / (double)origin_width;
		*thumb_height = *thumb_width * ratio;
	} else {
		if (origin_height < *thumb_height)
			*thumb_height = origin_height;
		ratio = (double)origin_width / (double)origin_height;
		*thumb_width = *thumb_height * ratio;
	}

	thumb_dbg("proper thumb w: %d h: %d", *thumb_width, *thumb_height);

	return;
}

int _media_thumb_general(const char *origin_path, const char *thumb_path, int thumb_width, int thumb_height, media_thumb_info *thumb_info)
{
	int err = MS_MEDIA_ERR_NONE;
	mm_util_image_h img = NULL;
	unsigned char *buf = NULL;
	unsigned int width = 0;
	unsigned int height = 0;
	size_t size = 0;
	mm_util_color_format_e format = MM_UTIL_COLOR_NUM;

	if (thumb_path != NULL) {
		err = mm_util_resize_P_P(origin_path, thumb_width, thumb_height, thumb_path);
	} else {
		err = mm_util_resize_P_B(origin_path, thumb_width, thumb_height, MM_UTIL_COLOR_BGRA, &img);
		if (err != MM_UTIL_ERROR_NONE) {
			thumb_err("mm_util_resize_P_B failed : %d", err);
			return MS_MEDIA_ERR_INTERNAL;
		}

		mm_image_get_image(img, &width, &height, &format, &buf, &size);
		thumb_info->data = buf;
		thumb_info->size = size;
		thumb_info->width = width;
		thumb_info->height = height;
		mm_image_destroy_image(img);
	}

	return err;
}

int _media_thumb_image(const char *origin_path, char *thumb_path, unsigned int thumb_width, unsigned int thumb_height, media_thumb_info *thumb_info)
{
	int err = MS_MEDIA_ERR_NONE;
	mm_util_img_codec_type image_type = 0;
	unsigned int origin_w = 0;
	unsigned int origin_h = 0;

	err = mm_util_extract_image_info(origin_path, &image_type, &origin_w, &origin_h);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_warn("Getting image info is failed err: %d", err);
		return MS_MEDIA_ERR_INTERNAL;
	}

	if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM_FOR_THUMB)) {
		thumb_warn("This original image is too big");
		return MS_MEDIA_ERR_THUMB_TOO_BIG;
	}

	__media_thumb_get_proper_thumb_size(origin_w, origin_h, &thumb_width, &thumb_height);

	if (image_type != IMG_CODEC_UNKNOWN_TYPE) {
		err = _media_thumb_general(origin_path, thumb_path, thumb_width, thumb_height, thumb_info);
	} else {
		thumb_warn("Unsupported image type");
		err = MS_MEDIA_ERR_THUMB_UNSUPPORTED;
	}

	return err;
}

int _media_thumb_get_hash_name(const char *file_full_path, char *thumb_hash_path, size_t max_thumb_path, uid_t uid)
{
	char *hash_name = NULL;
	char file_ext[255] = { 0 };
	char *get_path = NULL;
	int ret_len = 0;
	ms_user_storage_type_e storage_type = -1;
	int ret = MS_MEDIA_ERR_NONE;

	if (file_full_path == NULL || thumb_hash_path == NULL || max_thumb_path <= 0) {
		thumb_err("file_full_path==NULL || thumb_hash_path == NULL || max_thumb_path <= 0");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	_media_thumb_get_file_ext(file_full_path, file_ext, sizeof(file_ext));

	ret = ms_user_get_storage_type(uid, file_full_path, &storage_type);
	if ((ret != MS_MEDIA_ERR_NONE) || ((storage_type != MS_USER_STORAGE_INTERNAL) && (storage_type != MS_USER_STORAGE_EXTERNAL))) {
		thumb_err_slog("origin path(%s) is invalid. err : [%d] storage_type [%d]", file_full_path, ret, storage_type);
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	hash_name = _media_thumb_generate_hash_name(file_full_path);
	if (hash_name == NULL) {
		thumb_err("_media_thumb_generate_hash_name fail");
		return MS_MEDIA_ERR_INTERNAL;
	}

	ret = ms_user_get_root_thumb_store_path(uid, &get_path);
	if (get_path != NULL)
		ret_len = snprintf(thumb_hash_path, max_thumb_path - 1, "%s/.%s-%s.png", get_path, file_ext, hash_name);

	SAFE_FREE(get_path);

	if ((ret_len < 0) || (ret_len > (int)max_thumb_path)) {
		thumb_err("invalid hash path ret_len[%d]", ret_len);
		return MS_MEDIA_ERR_INTERNAL;
	}

	return MS_MEDIA_ERR_NONE;
}