• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ANDROID_ULTRAHDR_JPEGRUTILS_H
18 #define ANDROID_ULTRAHDR_JPEGRUTILS_H
19 
20 #include <ultrahdr/jpegr.h>
21 #include <utils/RefBase.h>
22 
23 #include <sstream>
24 #include <stdint.h>
25 #include <string>
26 #include <cstdio>
27 
28 namespace android::ultrahdr {
29 
EndianSwap32(uint32_t value)30 static constexpr uint32_t EndianSwap32(uint32_t value) {
31     return ((value & 0xFF) << 24) |
32            ((value & 0xFF00) << 8) |
33            ((value & 0xFF0000) >> 8) |
34            (value >> 24);
35 }
EndianSwap16(uint16_t value)36 static inline uint16_t EndianSwap16(uint16_t value) {
37     return static_cast<uint16_t>((value >> 8) | ((value & 0xFF) << 8));
38 }
39 
40 #if USE_BIG_ENDIAN
41     #define Endian_SwapBE32(n) EndianSwap32(n)
42     #define Endian_SwapBE16(n) EndianSwap16(n)
43 #else
44     #define Endian_SwapBE32(n) (n)
45     #define Endian_SwapBE16(n) (n)
46 #endif
47 
48 struct ultrahdr_metadata_struct;
49 /*
50  * Mutable data structure. Holds information for metadata.
51  */
52 class DataStruct : public RefBase {
53 private:
54     void* data;
55     int writePos;
56     int length;
57     ~DataStruct();
58 
59 public:
60     DataStruct(int s);
61     void* getData();
62     int getLength();
63     int getBytesWritten();
64     bool write8(uint8_t value);
65     bool write16(uint16_t value);
66     bool write32(uint32_t value);
67     bool write(const void* src, int size);
68 };
69 
70 /*
71  * Helper function used for writing data to destination.
72  *
73  * @param destination destination of the data to be written.
74  * @param source source of data being written.
75  * @param length length of the data to be written.
76  * @param position cursor in desitination where the data is to be written.
77  * @return status of succeed or error code.
78  */
79 status_t Write(jr_compressed_ptr destination, const void* source, size_t length, int &position);
80 
81 
82 /*
83  * Parses XMP packet and fills metadata with data from XMP
84  *
85  * @param xmp_data pointer to XMP packet
86  * @param xmp_size size of XMP packet
87  * @param metadata place to store HDR metadata values
88  * @return true if metadata is successfully retrieved, false otherwise
89 */
90 bool getMetadataFromXMP(uint8_t* xmp_data, size_t xmp_size, ultrahdr_metadata_struct* metadata);
91 
92 /*
93  * This method generates XMP metadata for the primary image.
94  *
95  * below is an example of the XMP metadata that this function generates where
96  * secondary_image_length = 1000
97  *
98  * <x:xmpmeta
99  *   xmlns:x="adobe:ns:meta/"
100  *   x:xmptk="Adobe XMP Core 5.1.2">
101  *   <rdf:RDF
102  *     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
103  *     <rdf:Description
104  *       xmlns:Container="http://ns.google.com/photos/1.0/container/"
105  *       xmlns:Item="http://ns.google.com/photos/1.0/container/item/"
106  *       xmlns:hdrgm="http://ns.adobe.com/hdr-gain-map/1.0/"
107  *       hdrgm:Version="1">
108  *       <Container:Directory>
109  *         <rdf:Seq>
110  *           <rdf:li
111  *             rdf:parseType="Resource">
112  *             <Container:Item
113  *               Item:Semantic="Primary"
114  *               Item:Mime="image/jpeg"/>
115  *           </rdf:li>
116  *           <rdf:li
117  *             rdf:parseType="Resource">
118  *             <Container:Item
119  *               Item:Semantic="GainMap"
120  *               Item:Mime="image/jpeg"
121  *               Item:Length="1000"/>
122  *           </rdf:li>
123  *         </rdf:Seq>
124  *       </Container:Directory>
125  *     </rdf:Description>
126  *   </rdf:RDF>
127  * </x:xmpmeta>
128  *
129  * @param secondary_image_length length of secondary image
130  * @return XMP metadata in type of string
131  */
132 std::string generateXmpForPrimaryImage(int secondary_image_length,
133                                        ultrahdr_metadata_struct& metadata);
134 
135 /*
136  * This method generates XMP metadata for the recovery map image.
137  *
138  * below is an example of the XMP metadata that this function generates where
139  * max_content_boost = 8.0
140  * min_content_boost = 0.5
141  *
142  * <x:xmpmeta
143  *   xmlns:x="adobe:ns:meta/"
144  *   x:xmptk="Adobe XMP Core 5.1.2">
145  *   <rdf:RDF
146  *     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
147  *     <rdf:Description
148  *       xmlns:hdrgm="http://ns.adobe.com/hdr-gain-map/1.0/"
149  *       hdrgm:Version="1"
150  *       hdrgm:GainMapMin="-1"
151  *       hdrgm:GainMapMax="3"
152  *       hdrgm:Gamma="1"
153  *       hdrgm:OffsetSDR="0"
154  *       hdrgm:OffsetHDR="0"
155  *       hdrgm:HDRCapacityMin="0"
156  *       hdrgm:HDRCapacityMax="3"
157  *       hdrgm:BaseRenditionIsHDR="False"/>
158  *   </rdf:RDF>
159  * </x:xmpmeta>
160  *
161  * @param metadata JPEG/R metadata to encode as XMP
162  * @return XMP metadata in type of string
163  */
164  std::string generateXmpForSecondaryImage(ultrahdr_metadata_struct& metadata);
165 }  // namespace android::ultrahdr
166 
167 #endif //ANDROID_ULTRAHDR_JPEGRUTILS_H
168