• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #include "media_image_framework_utils.h"
17 
18 #include "exif_metadata.h"
19 #include "post_proc.h"
20 
21 #include "medialibrary_errno.h"
22 #include "media_exif.h"
23 #include "media_log.h"
24 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace Media {
29 
GetExifRotate(const std::unique_ptr<ImageSource> & imageSource,int32_t & exifRotate)30 int32_t MediaImageFrameWorkUtils::GetExifRotate(
31     const std::unique_ptr<ImageSource> &imageSource, int32_t &exifRotate)
32 {
33     std::string orientationKey;
34     int32_t err = GetOrientationKey(imageSource, orientationKey);
35     CHECK_AND_RETURN_RET(err == E_OK, err);
36     CHECK_AND_RETURN_RET_LOG(ExifRotateUtils::ConvertOrientationKeyToExifRotate(orientationKey, exifRotate),
37         E_ERR, "Convert orientation to exif rotate failed, orientation value:%{public}s", orientationKey.c_str());
38     return E_OK;
39 }
40 
GetOrientationKey(const std::unique_ptr<ImageSource> & imageSource,std::string & orientationKey)41 int32_t MediaImageFrameWorkUtils::GetOrientationKey(const std::unique_ptr<ImageSource> &imageSource,
42     std::string &orientationKey)
43 {
44     CHECK_AND_RETURN_RET_LOG(imageSource != nullptr, E_ERR, "ImageSource is nullptr");
45     auto exifMetadata = imageSource->GetExifMetadata();
46     CHECK_AND_RETURN_RET_LOG(exifMetadata != nullptr, E_ERR, "ExifMetadata is nullptr");
47 
48     int32_t err = exifMetadata->GetValue(PHOTO_DATA_IMAGE_ORIENTATION, orientationKey);
49     CHECK_AND_RETURN_RET_LOG(err == E_OK, err, "Get exif rotate value failed, err:%{public}d", err);
50     return E_OK;
51 }
52 
GetExifRotate(const std::shared_ptr<Picture> picture,int32_t & exifRotate)53 int32_t MediaImageFrameWorkUtils::GetExifRotate(
54     const std::shared_ptr<Picture> picture, int32_t &exifRotate)
55 {
56     std::string orientationKey;
57     int32_t err = GetOrientationKey(picture, orientationKey);
58     CHECK_AND_RETURN_RET(err == E_OK, err);
59     CHECK_AND_RETURN_RET_LOG(ExifRotateUtils::ConvertOrientationKeyToExifRotate(orientationKey, exifRotate),
60         E_ERR, "Convert orientation to exif rotate failed, orientation value:%{public}s", orientationKey.c_str());
61     return E_OK;
62 }
63 
GetOrientationKey(const std::shared_ptr<Picture> picture,std::string & orientationKey)64 int32_t MediaImageFrameWorkUtils::GetOrientationKey(const std::shared_ptr<Picture> picture, std::string &orientationKey)
65 {
66     CHECK_AND_RETURN_RET_LOG(picture != nullptr, E_ERR, "Picture is nullptr");
67     std::shared_ptr<ExifMetadata> exifMetadata = picture->GetExifMetadata();
68     CHECK_AND_RETURN_RET_LOG(exifMetadata != nullptr, E_ERR, "ExifMetadata is nullptr");
69 
70     int32_t err = exifMetadata->GetValue(PHOTO_DATA_IMAGE_ORIENTATION, orientationKey);
71     CHECK_AND_RETURN_RET_LOG(err == E_OK, err, "Get exif rotate value failed, err:%{public}d", err);
72     return E_OK;
73 }
74 
GetExifRotate(const std::string & path,int32_t & exifRotate)75 int32_t MediaImageFrameWorkUtils::GetExifRotate(const std::string &path, int32_t &exifRotate)
76 {
77     uint32_t err = 0;
78     SourceOptions opts;
79     unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(path, opts, err);
80     CHECK_AND_RETURN_RET_LOG(err == 0 && imageSource != nullptr,
81         E_ERR, "CreateImageSource failed, error:%{public}u", err);
82     return GetExifRotate(imageSource, exifRotate);
83 }
84 
FlipAndRotatePixelMap(PixelMap & pixelMap,int32_t exifRotate)85 bool MediaImageFrameWorkUtils::FlipAndRotatePixelMap(PixelMap &pixelMap, int32_t exifRotate)
86 {
87     FlipAndRotateInfo info;
88     CHECK_AND_RETURN_RET_LOG(ExifRotateUtils::GetFlipAndRotateInfo(exifRotate, info),
89         false, "GetFlipAndRotateInfo failed, exifRotate:%{public}d", exifRotate);
90     return FlipAndRotatePixelMap(pixelMap, info);
91 }
92 
FlipAndRotatePixelMap(PixelMap & pixelMap,const FlipAndRotateInfo & info)93 bool MediaImageFrameWorkUtils::FlipAndRotatePixelMap(PixelMap &pixelMap, const FlipAndRotateInfo &info)
94 {
95     if (info.isLeftAndRightFlip || info.isUpAndDownFlip) {
96         pixelMap.flip(info.isLeftAndRightFlip, info.isUpAndDownFlip);
97     }
98 
99     if (info.orientation != 0) {
100         PostProc::RotateInRectangularSteps(pixelMap, static_cast<float>(info.orientation), true);
101     }
102     return true;
103 }
104 } // namespace Media
105 } // namespace OHOS