1 /*
2  * Copyright (C) 2023 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 #ifndef EXIF_MAKER_NOTE_H
16 #define EXIF_MAKER_NOTE_H
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include <libexif/exif-data.h>
23 
24 namespace OHOS {
25 namespace ImagePlugin {
26 class ExifMakerNote {
27 public:
28     ExifMakerNote();
29     ~ExifMakerNote();
30 
31     std::string hwCaptureMode;
32     std::string hwPhysicalAperture;
33     std::string hwMnoteRollAngle;
34     std::string hwMnotePitchAngle;
35     std::string hwMnoteSceneFoodConf;
36     std::string hwMnoteSceneStageConf;
37     std::string hwMnoteSceneBlueSkyConf;
38     std::string hwMnoteSceneGreenPlantConf;
39     std::string hwMnoteSceneBeachConf;
40     std::string hwMnoteSceneSnowConf;
41     std::string hwMnoteSceneSunsetConf;
42     std::string hwMnoteSceneFlowersConf;
43     std::string hwMnoteSceneNightConf;
44     std::string hwMnoteSceneTextConf;
45     std::string hwMnoteFaceCount;
46     std::string hwMnoteFocusMode;
47     static const uint16_t HW_MNOTE_TAG_SCENE_INFO_OFFSET = 0x0000;
48     static const uint16_t HW_MNOTE_TAG_FACE_INFO_OFFSET = 0x0100;
49     static const uint16_t HW_MNOTE_TAG_CAPTURE_MODE = 0x0200;
50     static const uint16_t HW_MNOTE_TAG_BURST_CAPTURE_NUMBER = 0x0201;
51     static const uint16_t HW_MNOTE_TAG_FRONT_CAMERA = 0x0202;
52     static const uint16_t HW_MNOTE_TAG_PHYSICAL_APERTURE = 0x0205;
53     static const uint16_t HW_MNOTE_IFD_SCENE_INFO_OFFSET = HW_MNOTE_TAG_SCENE_INFO_OFFSET;
54     static const uint16_t HW_MNOTE_IFD_FACE_INFO_OFFSET = HW_MNOTE_TAG_FACE_INFO_OFFSET;
55     static const uint16_t HW_MNOTE_IFD_DEFAULT = 0xffff;
56     static const uint16_t HW_MNOTE_TAG_ROLL_ANGLE = 0x0203;
57     static const uint16_t HW_MNOTE_TAG_PITCH_ANGLE = 0x0204;
58     static const uint16_t HW_MNOTE_TAG_SCENE_FOOD_CONF = 0x0002;
59     static const uint16_t HW_MNOTE_TAG_SCENE_STAGE_CONF = 0x0003;
60     static const uint16_t HW_MNOTE_TAG_SCENE_BLUE_SKY_CONF = 0x0004 ;
61     static const uint16_t HW_MNOTE_TAG_SCENE_GREEN_PLANT_CONF = 0x0005;
62     static const uint16_t HW_MNOTE_TAG_SCENE_BEACH_CONF = 0x0006;
63     static const uint16_t HW_MNOTE_TAG_SCENE_SNOW_CONF = 0x0007;
64     static const uint16_t HW_MNOTE_TAG_SCENE_SUNSET_CONF = 0x0008;
65     static const uint16_t HW_MNOTE_TAG_SCENE_FLOWERS_CONF = 0x0009;
66     static const uint16_t HW_MNOTE_TAG_SCENE_NIGHT_CONF = 0x000A;
67     static const uint16_t HW_MNOTE_TAG_SCENE_TEXT_CONF = 0x000B;
68     static const uint16_t HW_MNOTE_TAG_FACE_COUNT = 0x0102;
69     static const uint16_t HW_MNOTE_TAG_FOCUS_MODE = 0x020D;
70     const std::string DEFAULT_EXIF_VALUE = "default_exif_value";
71     std::map<std::string, std::string> makerTagValueMap = {
72         {"HwMnoteCaptureMode", DEFAULT_EXIF_VALUE},
73         {"HwMnotePhysicalAperture", DEFAULT_EXIF_VALUE},
74         {"HwMnoteRollAngle", DEFAULT_EXIF_VALUE},
75         {"HwMnotePitchAngle", DEFAULT_EXIF_VALUE},
76         {"HwMnoteSceneFoodConf", DEFAULT_EXIF_VALUE},
77         {"HwMnoteSceneStageConf", DEFAULT_EXIF_VALUE},
78         {"HwMnoteSceneBlueSkyConf", DEFAULT_EXIF_VALUE},
79         {"HwMnoteSceneGreenPlantConf", DEFAULT_EXIF_VALUE},
80         {"HwMnoteSceneBeachConf", DEFAULT_EXIF_VALUE},
81         {"HwMnoteSceneSnowConf", DEFAULT_EXIF_VALUE},
82         {"HwMnoteSceneSunsetConf", DEFAULT_EXIF_VALUE},
83         {"HwMnoteSceneFlowersConf", DEFAULT_EXIF_VALUE},
84         {"HwMnoteSceneNightConf", DEFAULT_EXIF_VALUE},
85         {"HwMnoteSceneTextConf", DEFAULT_EXIF_VALUE},
86         {"HwMnoteFaceCount", DEFAULT_EXIF_VALUE},
87         {"HwMnoteFocusMode", DEFAULT_EXIF_VALUE},
88     };
89 
90     uint32_t Parser(ExifData *exif, const unsigned char *data, uint32_t size);
91     [[nodiscard]] bool IsParsed() const;
92 
93 private:
94     static bool FindExifLocation(const unsigned char *data, uint32_t size, const unsigned char *&newData,
95         uint32_t &newSize);
96     static bool FindJpegAPP1(const unsigned char *data, uint32_t size, const unsigned char *&newData,
97         uint32_t &newSize);
98     uint32_t ParserMakerNote(ExifData* exif, bool &moreCheck);
99     uint32_t ParserMakerNote(const unsigned char *data, uint32_t size);
100 
101 private:
102     struct ExifItem {
103         uint16_t ifd {0};
104         uint16_t tag {0};
105         uint16_t format {0};
106         uint32_t count {0};
107         std::vector<unsigned char> data;
108 
109     public:
110         ExifItem();
111         explicit ExifItem(const ExifItem& item);
112         ~ExifItem();
113         ExifItem& operator=(const ExifItem& item);
114 
115         bool GetValue(std::string &value, const ExifByteOrder &order, bool mock = false);
116         bool GetValue(std::string &value, ExifData *exifData, bool mock = false);
117         void Dump(const std::string &info, const ExifByteOrder &order);
118 
119         static bool GetValue(std::string &value, const ExifByteOrder &order, ExifItem &item, bool mock = false);
120         static bool GetValue(std::string &value, ExifData *exifData, ExifItem &item, bool mock = false);
121         static void Dump(const std::string &info, const ExifItem &item, const ExifByteOrder &order);
122 
123     private:
124         void CopyItem(const ExifItem& item);
125 
126         static bool GetValue(std::string &value, ExifContent *exifContent, ExifItem &item, bool &mock);
127         static bool GetValue(std::string &value, ExifEntry *exifEntry, ExifItem &item, bool &mock);
128     };
129 
130     bool IsHwMakerNote(const unsigned char *data, uint32_t size);
131     bool ParserHwMakerNote();
132     bool ParserIFD(uint32_t offset, uint32_t ifd, uint32_t deep = 0);
133     bool ParserItem(uint32_t offset, uint32_t ifd, uint32_t deep = 0);
134     bool SetValue(const ExifItem &entry, const std::string &value);
135 
136 private:
137     bool GetUInt16AndMove(uint32_t &offset, uint16_t &value);
138     bool GetUInt32AndMove(uint32_t &offset, uint32_t &value);
139     bool GetDataAndMove(size_t &offset, size_t count, std::vector<unsigned char> &value);
140     bool GetUInt16(uint32_t offset, uint16_t &value);
141     bool GetUInt32(uint32_t offset, uint32_t &value);
142     bool GetData(size_t offset, size_t count, std::vector<unsigned char> &value);
143 
144     static bool GetUInt16(const std::vector<unsigned char> &buffer, ExifByteOrder order,
145         size_t offset, uint16_t &value);
146     static bool GetUInt32(const std::vector<unsigned char> &buffer, ExifByteOrder order,
147         size_t offset, uint32_t &value);
148     static bool GetData(const std::vector<unsigned char> &buffer, size_t offset, size_t count,
149         std::vector<unsigned char> &value);
150 
151     static std::string Dump(const std::vector<unsigned char> &data, uint32_t offset = 0, uint32_t sum = UINT32_MAX);
152 
153     std::vector<unsigned char> makerNote_;
154     ExifByteOrder order_ {ExifByteOrder::EXIF_BYTE_ORDER_INTEL};
155     uint32_t tiff_offset_ {0};
156     uint32_t ifd0_offset_ {0};
157 
158     std::vector<ExifItem> items_;
159 };
160 } // namespace ImagePlugin
161 } // namespace OHOS
162 #endif // EXIF_MAKER_NOTE_H