Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 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 | |
Dichen Zhang | 761b52d | 2023-02-10 22:38:38 +0000 | [diff] [blame] | 17 | #include <jpegrecoverymap/jpegrutils.h> |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <cmath> |
| 21 | |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 22 | #include <image_io/xml/xml_reader.h> |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 23 | #include <image_io/xml/xml_writer.h> |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 24 | #include <image_io/base/message_handler.h> |
| 25 | #include <image_io/xml/xml_element_rules.h> |
| 26 | #include <image_io/xml/xml_handler.h> |
| 27 | #include <image_io/xml/xml_rule.h> |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 28 | #include <utils/Log.h> |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 29 | |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 30 | using namespace photos_editing_formats::image_io; |
| 31 | using namespace std; |
Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 32 | |
Dichen Zhang | b2ed830 | 2023-02-10 23:05:04 +0000 | [diff] [blame] | 33 | namespace android::jpegrecoverymap { |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 34 | /* |
| 35 | * Helper function used for generating XMP metadata. |
| 36 | * |
| 37 | * @param prefix The prefix part of the name. |
| 38 | * @param suffix The suffix part of the name. |
| 39 | * @return A name of the form "prefix:suffix". |
| 40 | */ |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 41 | static inline string Name(const string &prefix, const string &suffix) { |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 42 | std::stringstream ss; |
| 43 | ss << prefix << ":" << suffix; |
| 44 | return ss.str(); |
| 45 | } |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 46 | |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 47 | DataStruct::DataStruct(int s) { |
| 48 | data = malloc(s); |
| 49 | length = s; |
| 50 | memset(data, 0, s); |
| 51 | writePos = 0; |
| 52 | } |
| 53 | |
| 54 | DataStruct::~DataStruct() { |
| 55 | if (data != nullptr) { |
| 56 | free(data); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void* DataStruct::getData() { |
| 61 | return data; |
| 62 | } |
| 63 | |
| 64 | int DataStruct::getLength() { |
| 65 | return length; |
| 66 | } |
| 67 | |
| 68 | int DataStruct::getBytesWritten() { |
| 69 | return writePos; |
| 70 | } |
| 71 | |
| 72 | bool DataStruct::write8(uint8_t value) { |
| 73 | uint8_t v = value; |
| 74 | return write(&v, 1); |
| 75 | } |
| 76 | |
| 77 | bool DataStruct::write16(uint16_t value) { |
| 78 | uint16_t v = value; |
| 79 | return write(&v, 2); |
| 80 | } |
| 81 | bool DataStruct::write32(uint32_t value) { |
| 82 | uint32_t v = value; |
| 83 | return write(&v, 4); |
| 84 | } |
| 85 | |
| 86 | bool DataStruct::write(const void* src, int size) { |
| 87 | if (writePos + size > length) { |
| 88 | ALOGE("Writing out of boundary: write position: %d, size: %d, capacity: %d", |
| 89 | writePos, size, length); |
| 90 | return false; |
| 91 | } |
| 92 | memcpy((uint8_t*) data + writePos, src, size); |
| 93 | writePos += size; |
| 94 | return true; |
| 95 | } |
| 96 | |
Dichen Zhang | 91dfc57 | 2023-01-19 11:22:43 -0800 | [diff] [blame] | 97 | /* |
| 98 | * Helper function used for writing data to destination. |
| 99 | */ |
| 100 | status_t Write(jr_compressed_ptr destination, const void* source, size_t length, int &position) { |
| 101 | if (position + length > destination->maxLength) { |
| 102 | return ERROR_JPEGR_BUFFER_TOO_SMALL; |
| 103 | } |
| 104 | |
| 105 | memcpy((uint8_t*)destination->data + sizeof(uint8_t) * position, source, length); |
| 106 | position += length; |
| 107 | return NO_ERROR; |
| 108 | } |
| 109 | |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 110 | // Extremely simple XML Handler - just searches for interesting elements |
| 111 | class XMPXmlHandler : public XmlHandler { |
| 112 | public: |
| 113 | |
| 114 | XMPXmlHandler() : XmlHandler() { |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 115 | state = NotStrarted; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | enum ParseState { |
| 119 | NotStrarted, |
| 120 | Started, |
| 121 | Done |
| 122 | }; |
| 123 | |
| 124 | virtual DataMatchResult StartElement(const XmlTokenContext& context) { |
| 125 | string val; |
| 126 | if (context.BuildTokenValue(&val)) { |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 127 | if (!val.compare(containerName)) { |
| 128 | state = Started; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 129 | } else { |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 130 | if (state != Done) { |
| 131 | state = NotStrarted; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | return context.GetResult(); |
| 136 | } |
| 137 | |
| 138 | virtual DataMatchResult FinishElement(const XmlTokenContext& context) { |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 139 | if (state == Started) { |
| 140 | state = Done; |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 141 | lastAttributeName = ""; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 142 | } |
| 143 | return context.GetResult(); |
| 144 | } |
| 145 | |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 146 | virtual DataMatchResult AttributeName(const XmlTokenContext& context) { |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 147 | string val; |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 148 | if (state == Started) { |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 149 | if (context.BuildTokenValue(&val)) { |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 150 | if (!val.compare(maxContentBoostAttrName)) { |
| 151 | lastAttributeName = maxContentBoostAttrName; |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 152 | } else if (!val.compare(minContentBoostAttrName)) { |
| 153 | lastAttributeName = minContentBoostAttrName; |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 154 | } else { |
| 155 | lastAttributeName = ""; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | return context.GetResult(); |
| 160 | } |
| 161 | |
| 162 | virtual DataMatchResult AttributeValue(const XmlTokenContext& context) { |
| 163 | string val; |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 164 | if (state == Started) { |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 165 | if (context.BuildTokenValue(&val, true)) { |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 166 | if (!lastAttributeName.compare(maxContentBoostAttrName)) { |
| 167 | maxContentBoostStr = val; |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 168 | } else if (!lastAttributeName.compare(minContentBoostAttrName)) { |
| 169 | minContentBoostStr = val; |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 170 | } |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | return context.GetResult(); |
| 174 | } |
| 175 | |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 176 | bool getMaxContentBoost(float* max_content_boost) { |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 177 | if (state == Done) { |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 178 | stringstream ss(maxContentBoostStr); |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 179 | float val; |
| 180 | if (ss >> val) { |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 181 | *max_content_boost = exp2(val); |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 182 | return true; |
| 183 | } else { |
| 184 | return false; |
| 185 | } |
| 186 | } else { |
| 187 | return false; |
| 188 | } |
| 189 | } |
| 190 | |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 191 | bool getMinContentBoost(float* min_content_boost) { |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 192 | if (state == Done) { |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 193 | stringstream ss(minContentBoostStr); |
| 194 | float val; |
| 195 | if (ss >> val) { |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 196 | *min_content_boost = exp2(val); |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 197 | return true; |
| 198 | } else { |
| 199 | return false; |
| 200 | } |
| 201 | } else { |
| 202 | return false; |
| 203 | } |
| 204 | } |
| 205 | |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 206 | private: |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 207 | static const string containerName; |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 208 | static const string maxContentBoostAttrName; |
| 209 | string maxContentBoostStr; |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 210 | static const string minContentBoostAttrName; |
| 211 | string minContentBoostStr; |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 212 | string lastAttributeName; |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 213 | ParseState state; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 216 | // GContainer XMP constants - URI and namespace prefix |
| 217 | const string kContainerUri = "http://ns.google.com/photos/1.0/container/"; |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 218 | const string kContainerPrefix = "Container"; |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 219 | |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 220 | // GContainer XMP constants - element and attribute names |
| 221 | const string kConDirectory = Name(kContainerPrefix, "Directory"); |
| 222 | const string kConItem = Name(kContainerPrefix, "Item"); |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 223 | |
| 224 | // GContainer XMP constants - names for XMP handlers |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 225 | const string XMPXmlHandler::containerName = "rdf:Description"; |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 226 | // Item XMP constants - URI and namespace prefix |
| 227 | const string kItemUri = "http://ns.google.com/photos/1.0/container/item/"; |
| 228 | const string kItemPrefix = "Item"; |
| 229 | |
| 230 | // Item XMP constants - element and attribute names |
| 231 | const string kItemLength = Name(kItemPrefix, "Length"); |
| 232 | const string kItemMime = Name(kItemPrefix, "Mime"); |
| 233 | const string kItemSemantic = Name(kItemPrefix, "Semantic"); |
| 234 | |
| 235 | // Item XMP constants - element and attribute values |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 236 | const string kSemanticPrimary = "Primary"; |
| 237 | const string kSemanticGainMap = "GainMap"; |
| 238 | const string kMimeImageJpeg = "image/jpeg"; |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 239 | |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 240 | // GainMap XMP constants - URI and namespace prefix |
| 241 | const string kGainMapUri = "http://ns.adobe.com/hdr-gain-map/1.0/"; |
| 242 | const string kGainMapPrefix = "hdrgm"; |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 243 | |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 244 | // GainMap XMP constants - element and attribute names |
| 245 | const string kMapVersion = Name(kGainMapPrefix, "Version"); |
| 246 | const string kMapGainMapMin = Name(kGainMapPrefix, "GainMapMin"); |
| 247 | const string kMapGainMapMax = Name(kGainMapPrefix, "GainMapMax"); |
| 248 | const string kMapGamma = Name(kGainMapPrefix, "Gamma"); |
| 249 | const string kMapOffsetSdr = Name(kGainMapPrefix, "OffsetSDR"); |
| 250 | const string kMapOffsetHdr = Name(kGainMapPrefix, "OffsetHDR"); |
| 251 | const string kMapHDRCapacityMin = Name(kGainMapPrefix, "HDRCapacityMin"); |
| 252 | const string kMapHDRCapacityMax = Name(kGainMapPrefix, "HDRCapacityMax"); |
| 253 | const string kMapBaseRenditionIsHDR = Name(kGainMapPrefix, "BaseRenditionIsHDR"); |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 254 | |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 255 | // GainMap XMP constants - names for XMP handlers |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 256 | const string XMPXmlHandler::minContentBoostAttrName = kMapGainMapMin; |
| 257 | const string XMPXmlHandler::maxContentBoostAttrName = kMapGainMapMax; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 258 | |
Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 259 | bool getMetadataFromXMP(uint8_t* xmp_data, size_t xmp_size, jpegr_metadata_struct* metadata) { |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 260 | string nameSpace = "http://ns.adobe.com/xap/1.0/\0"; |
| 261 | |
| 262 | if (xmp_size < nameSpace.size()+2) { |
| 263 | // Data too short |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | if (strncmp(reinterpret_cast<char*>(xmp_data), nameSpace.c_str(), nameSpace.size())) { |
| 268 | // Not correct namespace |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | // Position the pointers to the start of XMP XML portion |
| 273 | xmp_data += nameSpace.size()+1; |
| 274 | xmp_size -= nameSpace.size()+1; |
| 275 | XMPXmlHandler handler; |
| 276 | |
| 277 | // We need to remove tail data until the closing tag. Otherwise parser will throw an error. |
| 278 | while(xmp_data[xmp_size-1]!='>' && xmp_size > 1) { |
| 279 | xmp_size--; |
| 280 | } |
| 281 | |
| 282 | string str(reinterpret_cast<const char*>(xmp_data), xmp_size); |
| 283 | MessageHandler msg_handler; |
| 284 | unique_ptr<XmlRule> rule(new XmlElementRule); |
| 285 | XmlReader reader(&handler, &msg_handler); |
| 286 | reader.StartParse(std::move(rule)); |
| 287 | reader.Parse(str); |
| 288 | reader.FinishParse(); |
| 289 | if (reader.HasErrors()) { |
| 290 | // Parse error |
| 291 | return false; |
| 292 | } |
| 293 | |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 294 | if (!handler.getMaxContentBoost(&metadata->maxContentBoost)) { |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 295 | return false; |
| 296 | } |
| 297 | |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 298 | if (!handler.getMinContentBoost(&metadata->minContentBoost)) { |
| 299 | return false; |
| 300 | } |
| 301 | |
Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 302 | return true; |
| 303 | } |
| 304 | |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 305 | string generateXmpForPrimaryImage(int secondary_image_length) { |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 306 | const vector<string> kConDirSeq({kConDirectory, string("rdf:Seq")}); |
| 307 | const vector<string> kLiItem({string("rdf:li"), kConItem}); |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 308 | |
| 309 | std::stringstream ss; |
| 310 | photos_editing_formats::image_io::XmlWriter writer(ss); |
| 311 | writer.StartWritingElement("x:xmpmeta"); |
| 312 | writer.WriteXmlns("x", "adobe:ns:meta/"); |
| 313 | writer.WriteAttributeNameAndValue("x:xmptk", "Adobe XMP Core 5.1.2"); |
| 314 | writer.StartWritingElement("rdf:RDF"); |
| 315 | writer.WriteXmlns("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); |
| 316 | writer.StartWritingElement("rdf:Description"); |
| 317 | writer.WriteXmlns(kContainerPrefix, kContainerUri); |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 318 | writer.WriteXmlns(kItemPrefix, kItemUri); |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 319 | |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 320 | writer.StartWritingElements(kConDirSeq); |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 321 | |
| 322 | size_t item_depth = writer.StartWritingElement("rdf:li"); |
| 323 | writer.WriteAttributeNameAndValue("rdf:parseType", "Resource"); |
| 324 | writer.StartWritingElement(kConItem); |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 325 | writer.WriteAttributeNameAndValue(kItemSemantic, kSemanticPrimary); |
| 326 | writer.WriteAttributeNameAndValue(kItemMime, kMimeImageJpeg); |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 327 | writer.FinishWritingElementsToDepth(item_depth); |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 328 | |
| 329 | writer.StartWritingElement("rdf:li"); |
| 330 | writer.WriteAttributeNameAndValue("rdf:parseType", "Resource"); |
| 331 | writer.StartWritingElement(kConItem); |
| 332 | writer.WriteAttributeNameAndValue(kItemSemantic, kSemanticGainMap); |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 333 | writer.WriteAttributeNameAndValue(kItemMime, kMimeImageJpeg); |
| 334 | writer.WriteAttributeNameAndValue(kItemLength, secondary_image_length); |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 335 | |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 336 | writer.FinishWriting(); |
| 337 | |
| 338 | return ss.str(); |
| 339 | } |
| 340 | |
Dichen Zhang | e286f1c | 2023-03-14 00:22:00 +0000 | [diff] [blame] | 341 | string generateXmpForSecondaryImage(jpegr_metadata_struct& metadata) { |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 342 | const vector<string> kConDirSeq({kConDirectory, string("rdf:Seq")}); |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 343 | |
| 344 | std::stringstream ss; |
| 345 | photos_editing_formats::image_io::XmlWriter writer(ss); |
| 346 | writer.StartWritingElement("x:xmpmeta"); |
| 347 | writer.WriteXmlns("x", "adobe:ns:meta/"); |
| 348 | writer.WriteAttributeNameAndValue("x:xmptk", "Adobe XMP Core 5.1.2"); |
| 349 | writer.StartWritingElement("rdf:RDF"); |
| 350 | writer.WriteXmlns("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); |
| 351 | writer.StartWritingElement("rdf:Description"); |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 352 | writer.WriteXmlns(kGainMapPrefix, kGainMapUri); |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 353 | writer.WriteAttributeNameAndValue(kMapVersion, metadata.version); |
Dichen Zhang | 61ede36 | 2023-02-22 18:50:13 +0000 | [diff] [blame] | 354 | writer.WriteAttributeNameAndValue(kMapGainMapMin, log2(metadata.minContentBoost)); |
| 355 | writer.WriteAttributeNameAndValue(kMapGainMapMax, log2(metadata.maxContentBoost)); |
| 356 | writer.WriteAttributeNameAndValue(kMapGamma, "1"); |
| 357 | writer.WriteAttributeNameAndValue(kMapOffsetSdr, "0"); |
| 358 | writer.WriteAttributeNameAndValue(kMapOffsetHdr, "0"); |
Nick Deakin | 31f03e3 | 2023-03-31 16:00:13 -0400 | [diff] [blame^] | 359 | writer.WriteAttributeNameAndValue( |
| 360 | kMapHDRCapacityMin, std::max(log2(metadata.minContentBoost), 0.0f)); |
| 361 | writer.WriteAttributeNameAndValue(kMapHDRCapacityMax, log2(metadata.maxContentBoost)); |
| 362 | writer.WriteAttributeNameAndValue(kMapBaseRenditionIsHDR, "False"); |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 363 | writer.FinishWriting(); |
| 364 | |
| 365 | return ss.str(); |
| 366 | } |
| 367 | |
Dichen Zhang | b2ed830 | 2023-02-10 23:05:04 +0000 | [diff] [blame] | 368 | } // namespace android::jpegrecoverymap |