1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FRAMEWORKS_SERVICES_THUMBNAIL_SERVICE_INCLUDE_THUMBNAIL_CONST_H_
17 #define FRAMEWORKS_SERVICES_THUMBNAIL_SERVICE_INCLUDE_THUMBNAIL_CONST_H_
18 
19 #include "medialibrary_db_const.h"
20 
21 #include <unordered_map>
22 
23 namespace OHOS {
24 namespace Media {
25 constexpr int32_t THUMBNAIL_VERSION = 0;
26 constexpr int32_t DEFAULT_YEAR_SIZE = 64;
27 constexpr int32_t DEFAULT_MTH_SIZE = 128;
28 constexpr int32_t DEFAULT_THUMB_SIZE = 256;
29 constexpr int32_t MAX_DEFAULT_THUMB_SIZE = 768;
30 constexpr int32_t DEFAULT_LCD_SIZE = 1080;
31 constexpr uint32_t INT32_MAX_VALUE_LENGTH = 10;
32 constexpr int32_t VERTICAL_ANGLE = 90;
33 constexpr int32_t STRAIGHT_ANGLE = 180;
34 constexpr int32_t STAMP_PARAM = 4;
35 constexpr int32_t STAMP_PARAM_ZERO = 0;
36 constexpr int32_t STAMP_PARAM_ONE = 1;
37 constexpr int32_t STAMP_PARAM_TWO = 2;
38 constexpr int32_t STAMP_PARAM_THREE = 3;
39 enum class ThumbnailType : int32_t {
40     LCD,
41     THUMB,
42     MTH,
43     YEAR,
44     THUMB_ASTC,
45     MTH_ASTC,
46     YEAR_ASTC,
47     LCD_EX,
48     THUMB_EX,
49 };
50 
51 enum class GenerateScene : int32_t {
52     LOCAL = 0,
53     CLOUD,
54     BACKGROUND,
55     FOREGROUND,
56     RESTORE,
57     UPGRADE,
58 };
59 
60 enum class LoadSourceType : int32_t {
61     LOCAL_PHOTO = 0,
62     CLOUD_THUMB,
63     CLOUD_LCD,
64     CLOUD_PHOTO,
65 };
66 
67 enum class ThumbnailReady : int32_t {
68     GENERATE_THUMB_LATER,
69     GENERATE_THUMB_NOW,
70     GENERATE_THUMB_RETRY,
71     GENERATE_THUMB_COMPLETED,
72     THUMB_TO_UPLOAD,
73     THUMB_UPLOAD_COMPLETED,
74     THUMB_UPGRADE,
75 };
76 
77 enum class LcdReady : int64_t {
78     GENERATE_LCD_LATER,
79     GENERATE_LCD_FAILED,
80     GENERATE_LCD_COMPLETED,
81 };
82 
83 const std::unordered_map<ThumbnailType, std::string> TYPE_NAME_MAP = {
84     { ThumbnailType::LCD, "LCD" },
85     { ThumbnailType::THUMB, "THUMB" },
86     { ThumbnailType::MTH, "MTH" },
87     { ThumbnailType::YEAR, "YEAR" },
88     { ThumbnailType::THUMB_ASTC, "THUMB_ASTC" },
89     { ThumbnailType::MTH_ASTC, "MTH_ASTC" },
90     { ThumbnailType::YEAR_ASTC, "YEAR_ASTC" },
91 };
92 
93 constexpr uint32_t DEVICE_UDID_LENGTH = 65;
94 
95 constexpr int32_t THUMBNAIL_LCD_GENERATE_THRESHOLD = 5000;
96 constexpr int32_t THUMBNAIL_LCD_AGING_THRESHOLD = 10000;
97 constexpr int32_t WAIT_FOR_MS = 1000;
98 constexpr int32_t WAIT_FOR_SECOND = 3;
99 
100 constexpr float EPSILON = 1e-6;
101 constexpr int32_t SHORT_SIDE_THRESHOLD = 350;
102 constexpr int32_t MAXIMUM_SHORT_SIDE_THRESHOLD = 1050;
103 constexpr int32_t LCD_SHORT_SIDE_THRESHOLD = 512;
104 constexpr int32_t LCD_LONG_SIDE_THRESHOLD = 1920;
105 constexpr int32_t MAXIMUM_LCD_LONG_SIDE = 4096;
106 constexpr int32_t ASPECT_RATIO_THRESHOLD = 3;
107 constexpr int32_t MIN_COMPRESS_BUF_SIZE = 8192;
108 constexpr int32_t MAX_FIELD_LENGTH = 10;
109 constexpr int32_t MAX_TIMEID_LENGTH_OLD_VERSION = 10;
110 constexpr int32_t MAX_DATE_ADDED_LENGTH = 13;
111 constexpr int32_t DECODE_SCALE_BASE = 2;
112 constexpr int32_t FLAT_ANGLE = 180;
113 constexpr int32_t THUMBNAIL_GENERATE_BATCH_COUNT = 200;
114 constexpr int32_t ASTC_GENERATE_COUNT_AFTER_RESTORE = 500;
115 const std::string KVSTORE_FIELD_ID_TEMPLATE = "0000000000";
116 const std::string KVSTORE_DATE_ADDED_TEMPLATE = "0000000000000";
117 const std::string DEFAULT_EXIF_ORIENTATION = "1";
118 
119 const std::string THUMBNAIL_LCD_SUFFIX = "LCD";     // The size fit to screen
120 const std::string THUMBNAIL_THUMB_SUFFIX = "THM";   // The size which height is 256 and width is 256
121 const std::string THUMBNAIL_THUMBASTC_SUFFIX = "THM_ASTC";
122 const std::string THUMBNAIL_MTH_SUFFIX = "MTH";     // The size which height is 128 and width is 128
123 const std::string THUMBNAIL_YEAR_SUFFIX = "YEAR";   // The size which height is 64 and width is 64
124 const std::string THUMBNAIL_LCD_EX_SUFFIX = "THM_EX/LCD";
125 const std::string THUMBNAIL_THUMB_EX_SUFFIX = "THM_EX/THM";
126 
127 const std::string FILE_URI_PREX = "file://";
128 
129 const std::string PHOTO_URI_PREFIX = "file://media/Photo/";
130 
131 const std::string THUMBNAIL_FORMAT = "image/jpeg";
132 const std::string THUMBASTC_FORMAT = "image/astc/4*4";
133 constexpr uint8_t THUMBNAIL_MID = 90;
134 constexpr uint8_t THUMBNAIL_HIGH = 100;
135 constexpr uint8_t ASTC_LOW_QUALITY = 20;
136 
137 constexpr uint32_t THUMBNAIL_QUERY_MAX = 2000;
138 constexpr int64_t AV_FRAME_TIME = 0;
139 constexpr int64_t MS_TRANSFER_US = 1000;
140 
141 constexpr uint8_t NUMBER_HINT_1 = 1;
142 
143 constexpr int32_t DEFAULT_ORIGINAL = -1;
144 
145 const std::string THUMBNAIL_OPERN_KEYWORD = "operation";
146 const std::string THUMBNAIL_OPER = "oper";
147 const std::string THUMBNAIL_HEIGHT = "height";
148 const std::string THUMBNAIL_WIDTH = "width";
149 const std::string THUMBNAIL_PATH = "path";
150 const std::string THUMBNAIL_BEGIN_STAMP = "begin_stamp";
151 const std::string THUMBNAIL_TYPE = "type";
152 
153 // create thumbnail in close operation
154 const std::string CLOSE_CREATE_THUMB_STATUS = "create_thumbnail_sync_status";
155 const int32_t CREATE_THUMB_SYNC_STATUS = 1;
156 const int32_t CREATE_THUMB_ASYNC_STATUS = 0;
157 
158 constexpr float FLOAT_EPSILON = 1e-6;
159 
160 // request photo type
161 const std::string REQUEST_PHOTO_TYPE = "requestPhotoType";
162 
163 const int32_t CLOUD_PHOTO_POSITION = 2;
164 const int32_t CLOUD_THUMB_STATUS_DOWNLOAD = 0;
165 const int32_t CLOUD_THUMBNAIL_DOWNLOAD_FINISH_NUMBER = 1000;
166 const std::string RDB_QUERY_COUNT = "count";
167 
168 const int32_t THUMBNAIL_READY_FAILED = 2;
169 
GetThumbnailPath(const std::string &path, const std::string &key)170 static inline std::string GetThumbnailPath(const std::string &path, const std::string &key)
171 {
172     if (path.length() < ROOT_MEDIA_DIR.length()) {
173         return "";
174     }
175     std::string suffix = (key == "THM_ASTC") ? ".astc" : ".jpg";
176     return ROOT_MEDIA_DIR + ".thumbs/" + path.substr(ROOT_MEDIA_DIR.length()) + "/" + key + suffix;
177 }
178 
GetThumbnailPathHighlight(const std::string &path, const std::string &key, const std::string &timeStamp)179 static inline std::string GetThumbnailPathHighlight(const std::string &path, const std::string &key,
180     const std::string &timeStamp)
181 {
182     if (path.length() < ROOT_MEDIA_DIR.length()) {
183         return "";
184     }
185     std::string suffix = (key == "THM_ASTC") ? ".astc" : ".jpg";
186     return ROOT_MEDIA_DIR + ".thumbs/" + path.substr(ROOT_MEDIA_DIR.length()) +
187         "/beginTimeStamp" + timeStamp + "/" + key + suffix;
188 }
189 
GetThumbSuffix(ThumbnailType type)190 static std::string GetThumbSuffix(ThumbnailType type)
191 {
192     switch (type) {
193         case ThumbnailType::MTH:
194             return THUMBNAIL_MTH_SUFFIX;
195         case ThumbnailType::YEAR:
196             return THUMBNAIL_YEAR_SUFFIX;
197         case ThumbnailType::THUMB:
198             return THUMBNAIL_THUMB_SUFFIX;
199         case ThumbnailType::THUMB_ASTC:
200             return THUMBNAIL_THUMBASTC_SUFFIX;
201         case ThumbnailType::LCD:
202             return THUMBNAIL_LCD_SUFFIX;
203         default:
204             return "";
205     }
206 }
207 
GetKeyFrameThumbSuffix(int32_t type)208 static std::string GetKeyFrameThumbSuffix(int32_t type)
209 {
210     switch (type) {
211         case KEY_FRAME_LCD:
212             return THUMBNAIL_LCD_SUFFIX;
213         case KEY_FRAME_THM:
214             return THUMBNAIL_THUMB_SUFFIX;
215         case KEY_FRAME_THM_ASTC:
216             return THUMBNAIL_THUMBASTC_SUFFIX;
217         default:
218             return "";
219     }
220 }
221 
GetThumbType(const int32_t width, const int32_t height, bool isAstc = false)222 static inline ThumbnailType GetThumbType(const int32_t width, const int32_t height, bool isAstc = false)
223 {
224     if (width == DEFAULT_ORIGINAL && height == DEFAULT_ORIGINAL) {
225         return ThumbnailType::LCD;
226     }
227 
228     if (std::min(width, height) <= DEFAULT_THUMB_SIZE &&
229         std::max(width, height) <= MAX_DEFAULT_THUMB_SIZE) {
230         return isAstc ? ThumbnailType::THUMB_ASTC : ThumbnailType::THUMB;
231     }
232 
233     return ThumbnailType::LCD;
234 }
235 
GetSandboxPath(const std::string &path, ThumbnailType type)236 static inline std::string GetSandboxPath(const std::string &path, ThumbnailType type)
237 {
238     if (path.length() < ROOT_MEDIA_DIR.length()) {
239         return "";
240     }
241     std::string suffix = (type == ThumbnailType::THUMB_ASTC) ? ".astc" : ".jpg";
242     std::string suffixStr = path.substr(ROOT_MEDIA_DIR.length()) + "/" + GetThumbSuffix(type) + suffix;
243     return ROOT_SANDBOX_DIR + ".thumbs/" + suffixStr;
244 }
245 
GetKeyFrameSandboxPath(const std::string &path, const int32_t &beginStamp, const int32_t &type)246 static inline std::string GetKeyFrameSandboxPath(const std::string &path, const int32_t &beginStamp,
247     const int32_t &type)
248 {
249     if (path.length() < ROOT_MEDIA_DIR.length()) {
250         return "";
251     }
252     std::string suffix = (type == KEY_FRAME_THM_ASTC) ? ".astc" : ".jpg";
253     std::string suffixStr = path.substr(ROOT_MEDIA_DIR.length()) + "/beginTimeStamp" + std::to_string(beginStamp) +
254         "/" + GetKeyFrameThumbSuffix(type) + suffix;
255     return ROOT_MEDIA_DIR + ".thumbs/" + suffixStr;
256 }
257 
IsThumbnail(const int32_t width, const int32_t height)258 static inline bool IsThumbnail(const int32_t width, const int32_t height)
259 {
260     if (width == DEFAULT_ORIGINAL && height == DEFAULT_ORIGINAL) {
261         return false;
262     }
263     return std::min(width, height) <= DEFAULT_THUMB_SIZE &&
264            std::max(width, height) <= MAX_DEFAULT_THUMB_SIZE;
265 }
266 
267 } // namespace Media
268 } // namespace OHOS
269 
270 #endif  // FRAMEWORKS_SERVICES_THUMBNAIL_SERVICE_INCLUDE_THUMBNAIL_CONST_H_
271