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 "picture_adapter.h"
17 #include "camera_log.h"
18 #include "image_format.h"
19 #include "image_mime_type.h"
20 #include "image_type.h"
21 #include "ipc_skeleton.h"
22 #include "picture.h"
23 #include "pixel_map.h"
24 #include "surface_buffer.h"
25 namespace OHOS {
26 namespace CameraStandard {
27 std::unordered_map<std::string, float> exifOrientationDegree = {
28 {"Top-left", 0},
29 {"Top-right", 90},
30 {"Bottom-right", 180},
31 {"Right-top", 90},
32 {"Left-bottom", 270},
33 };
34
TransExifOrientationToDegree(const std::string & orientation)35 float TransExifOrientationToDegree(const std::string& orientation)
36 {
37 float degree = .0;
38 if (exifOrientationDegree.count(orientation)) {
39 degree = exifOrientationDegree[orientation];
40 }
41 return degree;
42 }
43
RotatePixelMap(std::shared_ptr<Media::PixelMap> pixelMap,const std::string & exifOrientation)44 inline void RotatePixelMap(std::shared_ptr<Media::PixelMap> pixelMap, const std::string& exifOrientation)
45 {
46 float degree = TransExifOrientationToDegree(exifOrientation);
47 if (pixelMap) {
48 DECORATOR_HILOG(HILOG_INFO, "RotatePicture degree is %{public}f", degree);
49 pixelMap->rotate(degree);
50 } else {
51 DECORATOR_HILOG(HILOG_ERROR, "RotatePicture degree is %{public}f", degree);
52 }
53 }
54
GetAndSetExifOrientation(OHOS::Media::ImageMetadata * exifData)55 std::string GetAndSetExifOrientation(OHOS::Media::ImageMetadata* exifData)
56 {
57 std::string orientation = "";
58 if (exifData != nullptr) {
59 exifData->GetValue("Orientation", orientation);
60 std::string defalutExifOrientation = "1";
61 exifData->SetValue("Orientation", defalutExifOrientation);
62 DECORATOR_HILOG(HILOG_INFO, "GetExifOrientation orientation:%{public}s", orientation.c_str());
63 exifData->RemoveExifThumbnail();
64 MEDIA_INFO_LOG("RemoveExifThumbnail");
65 } else {
66 DECORATOR_HILOG(HILOG_ERROR, "GetExifOrientation exifData is nullptr");
67 }
68 return orientation;
69 }
70
PictureAdapter()71 PictureAdapter::PictureAdapter() : picture_(nullptr)
72 {
73 MEDIA_INFO_LOG("PictureAdapter ctor");
74 }
75
~PictureAdapter()76 PictureAdapter::~PictureAdapter()
77 {
78 MEDIA_INFO_LOG("PictureAdapter dctor");
79 }
80
Create(sptr<SurfaceBuffer> & surfaceBuffer)81 void PictureAdapter::Create(sptr<SurfaceBuffer> &surfaceBuffer)
82 {
83 MEDIA_INFO_LOG("PictureAdapter ctor");
84 picture_ = Media::Picture::Create(surfaceBuffer);
85 }
86
SetAuxiliaryPicture(sptr<SurfaceBuffer> & surfaceBuffer,CameraAuxiliaryPictureType type)87 void PictureAdapter::SetAuxiliaryPicture(sptr<SurfaceBuffer> &surfaceBuffer, CameraAuxiliaryPictureType type)
88 {
89 MEDIA_INFO_LOG("PictureAdapter::SetAuxiliaryPicture enter");
90 std::shared_ptr<Media::Picture> picture = GetPicture();
91 if (!picture) {
92 MEDIA_ERR_LOG("PictureAdapter::SetAuxiliaryPicture picture is nullptr");
93 return;
94 }
95 std::unique_ptr<Media::AuxiliaryPicture> uniptr = Media::AuxiliaryPicture::Create(
96 surfaceBuffer, static_cast<Media::AuxiliaryPictureType>(type));
97 std::shared_ptr<Media::AuxiliaryPicture> picturePtr = std::move(uniptr);
98 picture->SetAuxiliaryPicture(picturePtr);
99 }
100
Marshalling(Parcel & data)101 bool PictureAdapter::Marshalling(Parcel &data)
102 {
103 MEDIA_INFO_LOG("PictureAdapter::Marshalling enter");
104 std::shared_ptr<Media::Picture> picture = GetPicture();
105 if (!picture) {
106 MEDIA_ERR_LOG("PictureAdapter::Marshalling picture is nullptr");
107 return false;
108 }
109 return picture->Marshalling(data);
110 }
111
Unmarshalling(Parcel & data)112 void PictureAdapter::Unmarshalling(Parcel &data)
113 {
114 MEDIA_INFO_LOG("PictureAdapter::Unmarshalling enter");
115 picture_.reset(Media::Picture::Unmarshalling(data));
116 }
117
SetExifMetadata(sptr<SurfaceBuffer> & surfaceBuffer)118 int32_t PictureAdapter::SetExifMetadata(sptr<SurfaceBuffer> &surfaceBuffer)
119 {
120 MEDIA_INFO_LOG("PictureAdapter::SetExifMetadata enter");
121 int32_t retCode = -1;
122 std::shared_ptr<Media::Picture> picture = GetPicture();
123 if (!picture) {
124 MEDIA_ERR_LOG("PictureAdapter::SetExifMetadata picture is nullptr");
125 return retCode;
126 }
127 retCode = picture->SetExifMetadata(surfaceBuffer);
128 return retCode;
129 }
130
SetMaintenanceData(sptr<SurfaceBuffer> & surfaceBuffer)131 bool PictureAdapter::SetMaintenanceData(sptr<SurfaceBuffer> &surfaceBuffer)
132 {
133 bool retCode = false;
134 std::shared_ptr<Media::Picture> picture = GetPicture();
135 if (!picture) {
136 MEDIA_ERR_LOG("PictureAdapter::SetMaintenanceData picture is nullptr");
137 return retCode;
138 }
139 retCode = picture->SetMaintenanceData(surfaceBuffer);
140 return retCode;
141 }
142
RotatePicture()143 void PictureAdapter::RotatePicture()
144 {
145 MEDIA_INFO_LOG("PictureAdapter::RotatePicture E");
146 std::shared_ptr<Media::Picture> picture = GetPicture();
147 if (!picture) {
148 MEDIA_ERR_LOG("PictureAdapter::RotatePicture picture is nullptr");
149 return;
150 }
151 std::string orientation = GetAndSetExifOrientation(
152 reinterpret_cast<OHOS::Media::ImageMetadata*>(picture->GetExifMetadata().get()));
153 RotatePixelMap(picture->GetMainPixel(), orientation);
154 auto gainMap = picture->GetAuxiliaryPicture(Media::AuxiliaryPictureType::GAINMAP);
155 if (gainMap) {
156 RotatePixelMap(gainMap->GetContentPixel(), orientation);
157 }
158 auto depthMap = picture->GetAuxiliaryPicture(Media::AuxiliaryPictureType::DEPTH_MAP);
159 if (depthMap) {
160 RotatePixelMap(depthMap->GetContentPixel(), orientation);
161 }
162 auto unrefocusMap = picture->GetAuxiliaryPicture(Media::AuxiliaryPictureType::UNREFOCUS_MAP);
163 if (unrefocusMap) {
164 RotatePixelMap(unrefocusMap->GetContentPixel(), orientation);
165 }
166 auto linearMap = picture->GetAuxiliaryPicture(Media::AuxiliaryPictureType::LINEAR_MAP);
167 if (linearMap) {
168 RotatePixelMap(linearMap->GetContentPixel(), orientation);
169 }
170 MEDIA_INFO_LOG("PictureAdapter::RotatePicture X");
171 }
172
GetPicture()173 std::shared_ptr<Media::Picture> PictureAdapter::GetPicture()
174 {
175 return picture_;
176 }
177
createPictureAdapterIntf()178 extern "C" PictureIntf *createPictureAdapterIntf()
179 {
180 return new PictureAdapter();
181 }
182
183 } // namespace AVSession
184 } // namespace OHOS