• 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 ULTRAHDR_JPEGRUTILS_H
18 #define ULTRAHDR_JPEGRUTILS_H
19 
20 #include "ultrahdr/ultrahdr.h"
21 #include "ultrahdr/jpegr.h"
22 
23 // TODO (dichenzhang): This is old version metadata, new version can be found in
24 // https://drive.google.com/file/d/1yUGmjGytRuBa2vpr9eM5Uu8CVhyyddjp/view?resourcekey=0-HGzFrzPQzu5FNYLRAJXQBA
25 // and in gainmapmetadata.h/.cpp
26 // This file is kept in order to keep the backward compatibility.
27 namespace ultrahdr {
28 
EndianSwap32(uint32_t value)29 static constexpr uint32_t EndianSwap32(uint32_t value) {
30   return ((value & 0xFF) << 24) | ((value & 0xFF00) << 8) | ((value & 0xFF0000) >> 8) |
31          (value >> 24);
32 }
EndianSwap16(uint16_t value)33 static inline uint16_t EndianSwap16(uint16_t value) {
34   return static_cast<uint16_t>((value >> 8) | ((value & 0xFF) << 8));
35 }
36 
37 struct ultrahdr_metadata_struct;
38 /*
39  * Mutable data structure. Holds information for metadata.
40  */
41 class DataStruct {
42  private:
43   void* data;
44   int writePos;
45   int length;
46 
47  public:
48   DataStruct(int s);
49   ~DataStruct();
50 
51   void* getData();
52   int getLength();
53   int getBytesWritten();
54   bool write8(uint8_t value);
55   bool write16(uint16_t value);
56   bool write32(uint32_t value);
57   bool write(const void* src, int size);
58 };
59 
60 /*
61  * Helper function used for writing data to destination.
62  *
63  * @param destination destination of the data to be written.
64  * @param source source of data being written.
65  * @param length length of the data to be written.
66  * @param position cursor in desitination where the data is to be written.
67  * @return status of succeed or error code.
68  */
69 status_t Write(jr_compressed_ptr destination, const void* source, int length, int& position);
70 
71 /*
72  * Parses XMP packet and fills metadata with data from XMP
73  *
74  * @param xmp_data pointer to XMP packet
75  * @param xmp_size size of XMP packet
76  * @param metadata place to store HDR metadata values
77  * @return true if metadata is successfully retrieved, false otherwise
78  */
79 bool getMetadataFromXMP(uint8_t* xmp_data, int xmp_size, ultrahdr_metadata_struct* 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(int secondary_image_length,
122                                        ultrahdr_metadata_struct& 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(ultrahdr_metadata_struct& metadata);
155 }  // namespace ultrahdr
156 
157 #endif  // ULTRAHDR_JPEGRUTILS_H
158