1 /*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ULTRAHDR_JPEGRUTILS_H
18 #define ULTRAHDR_JPEGRUTILS_H
19
20 #include "ultrahdr/jpegr.h"
21
22 // TODO (dichenzhang): This is old version metadata, new version can be found in
23 // https://drive.google.com/file/d/1yUGmjGytRuBa2vpr9eM5Uu8CVhyyddjp/view?resourcekey=0-HGzFrzPQzu5FNYLRAJXQBA
24 // and in gainmapmetadata.h/.cpp
25 // This file is kept in order to keep the backward compatibility.
26 namespace ultrahdr {
27
EndianSwap32(uint32_t value)28 static constexpr uint32_t EndianSwap32(uint32_t value) {
29 return ((value & 0xFF) << 24) | ((value & 0xFF00) << 8) | ((value & 0xFF0000) >> 8) |
30 (value >> 24);
31 }
EndianSwap16(uint16_t value)32 static inline uint16_t EndianSwap16(uint16_t value) {
33 return static_cast<uint16_t>((value >> 8) | ((value & 0xFF) << 8));
34 }
35
36 /*
37 * Mutable data structure. Holds information for metadata.
38 */
39 class DataStruct {
40 private:
41 void* data;
42 size_t writePos;
43 size_t length;
44
45 public:
46 DataStruct(size_t s);
47 ~DataStruct();
48
49 void* getData();
50 size_t getLength();
51 size_t getBytesWritten();
52 bool write8(uint8_t value);
53 bool write16(uint16_t value);
54 bool write32(uint32_t value);
55 bool write(const void* src, size_t size);
56 };
57
58 /*
59 * Helper function used for writing data to destination.
60 *
61 * @param destination destination of the data to be written.
62 * @param source source of data being written.
63 * @param length length of the data to be written.
64 * @param position cursor in desitination where the data is to be written.
65 * @return success or error code.
66 */
67 uhdr_error_info_t Write(uhdr_compressed_image_t* destination, const void* source, size_t length,
68 size_t& position);
69
70 /*
71 * Parses XMP packet and fills metadata with data from XMP
72 *
73 * @param xmp_data pointer to XMP packet
74 * @param xmp_size size of XMP packet
75 * @param metadata place to store HDR metadata values
76 * @return success or error code.
77 */
78 uhdr_error_info_t getMetadataFromXMP(uint8_t* xmp_data, size_t xmp_size,
79 uhdr_gainmap_metadata_ext_t* metadata);
80
81 /*
82 * This method generates XMP metadata for the primary image.
83 *
84 * below is an example of the XMP metadata that this function generates where
85 * secondary_image_length = 1000
86 *
87 * <x:xmpmeta
88 * xmlns:x="adobe:ns:meta/"
89 * x:xmptk="Adobe XMP Core 5.1.2">
90 * <rdf:RDF
91 * xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
92 * <rdf:Description
93 * xmlns:Container="http://ns.google.com/photos/1.0/container/"
94 * xmlns:Item="http://ns.google.com/photos/1.0/container/item/"
95 * xmlns:hdrgm="http://ns.adobe.com/hdr-gain-map/1.0/"
96 * hdrgm:Version="1">
97 * <Container:Directory>
98 * <rdf:Seq>
99 * <rdf:li
100 * rdf:parseType="Resource">
101 * <Container:Item
102 * Item:Semantic="Primary"
103 * Item:Mime="image/jpeg"/>
104 * </rdf:li>
105 * <rdf:li
106 * rdf:parseType="Resource">
107 * <Container:Item
108 * Item:Semantic="GainMap"
109 * Item:Mime="image/jpeg"
110 * Item:Length="1000"/>
111 * </rdf:li>
112 * </rdf:Seq>
113 * </Container:Directory>
114 * </rdf:Description>
115 * </rdf:RDF>
116 * </x:xmpmeta>
117 *
118 * @param secondary_image_length length of secondary image
119 * @return XMP metadata in type of string
120 */
121 std::string generateXmpForPrimaryImage(size_t secondary_image_length,
122 uhdr_gainmap_metadata_ext_t& metadata);
123
124 /*
125 * This method generates XMP metadata for the recovery map image.
126 * Link: https://developer.android.com/media/platform/hdr-image-format#XMP-attributes
127 *
128 * below is an example of the XMP metadata that this function generates where
129 * max_content_boost = 8.0
130 * min_content_boost = 0.5
131 *
132 * <x:xmpmeta
133 * xmlns:x="adobe:ns:meta/"
134 * x:xmptk="Adobe XMP Core 5.1.2">
135 * <rdf:RDF
136 * xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
137 * <rdf:Description
138 * xmlns:hdrgm="http://ns.adobe.com/hdr-gain-map/1.0/"
139 * hdrgm:Version="1"
140 * hdrgm:GainMapMin="-1"
141 * hdrgm:GainMapMax="3"
142 * hdrgm:Gamma="1"
143 * hdrgm:OffsetSDR="0"
144 * hdrgm:OffsetHDR="0"
145 * hdrgm:HDRCapacityMin="0"
146 * hdrgm:HDRCapacityMax="3"
147 * hdrgm:BaseRenditionIsHDR="False"/>
148 * </rdf:RDF>
149 * </x:xmpmeta>
150 *
151 * @param metadata JPEG/R metadata to encode as XMP
152 * @return XMP metadata in type of string
153 */
154 std::string generateXmpForSecondaryImage(uhdr_gainmap_metadata_ext_t& metadata);
155
156 } // namespace ultrahdr
157
158 #endif // ULTRAHDR_JPEGRUTILS_H
159