1 /*
2 * Copyright (C) 2021-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 #define MLOG_TAG "Thumbnail"
16
17 #include "media_thumbnail_helper.h"
18 #include "hitrace_meter.h"
19 #include "image_packer.h"
20 #include "media_file_utils.h"
21 #include "media_log.h"
22 #include "medialibrary_db_const.h"
23 #include "medialibrary_type_const.h"
24 #include "medialibrary_tracer.h"
25
26 namespace OHOS {
27 namespace Media {
MediaThumbnailHelper()28 MediaThumbnailHelper::MediaThumbnailHelper()
29 {}
30
ResizeImage(const std::vector<uint8_t> & data,const Size & size,std::unique_ptr<PixelMap> & pixelMap)31 bool MediaThumbnailHelper::ResizeImage(const std::vector<uint8_t> &data, const Size &size,
32 std::unique_ptr<PixelMap> &pixelMap)
33 {
34 MediaLibraryTracer tracer;
35 tracer.Start("ResizeImage");
36 if (data.size() == 0) {
37 MEDIA_ERR_LOG("Data is empty");
38 return false;
39 }
40
41 tracer.Start("ImageSource::CreateImageSource");
42 uint32_t errorCode = Media::SUCCESS;
43 SourceOptions opts;
44 std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(data.data(),
45 data.size(), opts, errorCode);
46 if (errorCode != Media::SUCCESS) {
47 MEDIA_ERR_LOG("Failed to create image source %{public}d", errorCode);
48 return false;
49 }
50 tracer.Finish();
51
52 tracer.Start("imageSource->CreatePixelMap");
53 DecodeOptions decodeOpts;
54 decodeOpts.desiredSize.width = size.width;
55 decodeOpts.desiredSize.height = size.height;
56 decodeOpts.allocatorType = AllocatorType::SHARE_MEM_ALLOC;
57 pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
58 if (errorCode != Media::SUCCESS) {
59 MEDIA_ERR_LOG("Failed to create pixelmap %{public}d", errorCode);
60 return false;
61 }
62 tracer.Finish();
63 return true;
64 }
65 } // namespace Media
66 } // namespace OHOS
67