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 "dng_exif_metadata_accessor.h"
17 
18 #include "data_buf.h"
19 #include "image_log.h"
20 #include "media_errors.h"
21 #include "tiff_parser.h"
22 
23 #undef LOG_DOMAIN
24 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
25 
26 #undef LOG_TAG
27 #define LOG_TAG "DngExifMetadataAccessor"
28 
29 namespace OHOS {
30 namespace Media {
31 namespace {
32     using uint_8 = byte;
33 }
34 
DngExifMetadataAccessor(std::shared_ptr<MetadataStream> & stream)35 DngExifMetadataAccessor::DngExifMetadataAccessor(std::shared_ptr<MetadataStream> &stream)
36     : AbstractExifMetadataAccessor(stream)
37 {
38 }
39 
~DngExifMetadataAccessor()40 DngExifMetadataAccessor::~DngExifMetadataAccessor() {}
41 
Read()42 uint32_t DngExifMetadataAccessor::Read()
43 {
44     if (!imageStream_->IsOpen()) {
45         IMAGE_LOGE("Input image stream is not open.");
46         return ERR_IMAGE_SOURCE_DATA;
47     }
48 
49     imageStream_->Seek(0, SeekPos::BEGIN);
50     ssize_t size = imageStream_->GetSize();
51     byte *byteStream = imageStream_->GetAddr();
52     if ((size == 0) || (byteStream == nullptr)) {
53         IMAGE_LOGE("Input image stream is empty.");
54         return ERR_IMAGE_SOURCE_DATA;
55     }
56 
57     size_t tiffHeaderPos = TiffParser::FindTiffPos(byteStream, size);
58     if (tiffHeaderPos == std::numeric_limits<size_t>::max()) {
59         IMAGE_LOGE("Input image stream is not tiff type.");
60         return ERR_IMAGE_SOURCE_DATA;
61     }
62 
63     ExifData *exifData;
64     TiffParser::Decode(reinterpret_cast<const unsigned char *>(byteStream + tiffHeaderPos),
65                        (size - tiffHeaderPos), &exifData);
66     if (exifData == nullptr) {
67         IMAGE_LOGE("Failed to decode TIFF buffer.");
68         return ERR_EXIF_DECODE_FAILED;
69     }
70 
71     tiffOffset_ = static_cast<long>(tiffHeaderPos);
72     exifMetadata_ = std::make_shared<OHOS::Media::ExifMetadata>(exifData);
73     return SUCCESS;
74 }
75 
ReadBlob(DataBuf & blob)76 bool DngExifMetadataAccessor::ReadBlob(DataBuf &blob)
77 {
78     return false;
79 }
80 
Write()81 uint32_t DngExifMetadataAccessor::Write()
82 {
83     return ERROR;
84 }
85 
WriteBlob(DataBuf & blob)86 uint32_t DngExifMetadataAccessor::WriteBlob(DataBuf &blob)
87 {
88     return ERROR;
89 }
90 } // namespace Media
91 } // namespace OHOS
92