1 /*
2  * Copyright (C) 2024 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 "abstract_exif_metadata_accessor.h"
17 
18 #undef LOG_DOMAIN
19 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
20 
21 #undef LOG_TAG
22 #define LOG_TAG "AbstractExifMetadataAccessor"
23 
24 namespace OHOS {
25 namespace Media {
AbstractExifMetadataAccessor(std::shared_ptr<MetadataStream> & stream)26 AbstractExifMetadataAccessor::AbstractExifMetadataAccessor(std::shared_ptr<MetadataStream> &stream)
27     : imageStream_(stream)
28 {}
29 
~AbstractExifMetadataAccessor()30 AbstractExifMetadataAccessor::~AbstractExifMetadataAccessor() {}
31 
Create()32 bool AbstractExifMetadataAccessor::Create()
33 {
34     if (exifMetadata_ == nullptr) {
35         exifMetadata_ = std::make_shared<ExifMetadata>();
36     }
37 
38     return exifMetadata_->CreateExifdata();
39 }
40 
WriteToOutput(SkWStream & output)41 bool AbstractExifMetadataAccessor::WriteToOutput(SkWStream &output)
42 {
43     if (imageStream_ == nullptr) {
44         return false;
45     }
46 
47     return output.write(imageStream_->GetAddr(), imageStream_->GetSize());
48 }
49 
Get()50 std::shared_ptr<ExifMetadata> AbstractExifMetadataAccessor::Get()
51 {
52     return exifMetadata_;
53 }
54 
Set(std::shared_ptr<ExifMetadata> & ptr)55 void AbstractExifMetadataAccessor::Set(std::shared_ptr<ExifMetadata> &ptr)
56 {
57     exifMetadata_ = ptr;
58 }
59 
GetOutputStream()60 std::shared_ptr<MetadataStream> AbstractExifMetadataAccessor::GetOutputStream()
61 {
62     return imageStream_;
63 }
64 
GetFilterArea(const std::vector<std::string> & exifKeys,std::vector<std::pair<uint32_t,uint32_t>> & ranges)65 uint32_t AbstractExifMetadataAccessor::GetFilterArea(const std::vector<std::string> &exifKeys,
66                                                      std::vector<std::pair<uint32_t, uint32_t>> &ranges)
67 {
68     uint32_t ret = Read();
69     if (ret != SUCCESS) {
70         IMAGE_LOGD("Failed to read the exif info.");
71         return E_NO_EXIF_TAG;
72     }
73     exifMetadata_->GetFilterArea(exifKeys, ranges);
74     if (ranges.empty()) {
75         return E_NO_EXIF_TAG;
76     }
77     for (auto& range : ranges) {
78         range.first += static_cast<uint32_t>(GetTiffOffset());
79     }
80     return SUCCESS;
81 }
82 
83 } // namespace Media
84 } // namespace OHOS
85