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> |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 18 | #include <image_io/xml/xml_reader.h> |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 19 | #include <image_io/xml/xml_writer.h> |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 20 | #include <image_io/base/message_handler.h> |
| 21 | #include <image_io/xml/xml_element_rules.h> |
| 22 | #include <image_io/xml/xml_handler.h> |
| 23 | #include <image_io/xml/xml_rule.h> |
| 24 | |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 25 | using namespace photos_editing_formats::image_io; |
| 26 | using namespace std; |
Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 27 | |
Dichen Zhang | b2ed830 | 2023-02-10 23:05:04 +0000 | [diff] [blame] | 28 | namespace android::jpegrecoverymap { |
Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 29 | |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 30 | /* |
| 31 | * Helper function used for generating XMP metadata. |
| 32 | * |
| 33 | * @param prefix The prefix part of the name. |
| 34 | * @param suffix The suffix part of the name. |
| 35 | * @return A name of the form "prefix:suffix". |
| 36 | */ |
| 37 | string Name(const string &prefix, const string &suffix) { |
| 38 | std::stringstream ss; |
| 39 | ss << prefix << ":" << suffix; |
| 40 | return ss.str(); |
| 41 | } |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 42 | |
Dichen Zhang | 91dfc57 | 2023-01-19 11:22:43 -0800 | [diff] [blame] | 43 | /* |
| 44 | * Helper function used for writing data to destination. |
| 45 | */ |
| 46 | status_t Write(jr_compressed_ptr destination, const void* source, size_t length, int &position) { |
| 47 | if (position + length > destination->maxLength) { |
| 48 | return ERROR_JPEGR_BUFFER_TOO_SMALL; |
| 49 | } |
| 50 | |
| 51 | memcpy((uint8_t*)destination->data + sizeof(uint8_t) * position, source, length); |
| 52 | position += length; |
| 53 | return NO_ERROR; |
| 54 | } |
| 55 | |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 56 | // Extremely simple XML Handler - just searches for interesting elements |
| 57 | class XMPXmlHandler : public XmlHandler { |
| 58 | public: |
| 59 | |
| 60 | XMPXmlHandler() : XmlHandler() { |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 61 | gContainerItemState = NotStrarted; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | enum ParseState { |
| 65 | NotStrarted, |
| 66 | Started, |
| 67 | Done |
| 68 | }; |
| 69 | |
| 70 | virtual DataMatchResult StartElement(const XmlTokenContext& context) { |
| 71 | string val; |
| 72 | if (context.BuildTokenValue(&val)) { |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 73 | if (!val.compare(gContainerItemName)) { |
| 74 | gContainerItemState = Started; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 75 | } else { |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 76 | if (gContainerItemState != Done) { |
| 77 | gContainerItemState = NotStrarted; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | return context.GetResult(); |
| 82 | } |
| 83 | |
| 84 | virtual DataMatchResult FinishElement(const XmlTokenContext& context) { |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 85 | if (gContainerItemState == Started) { |
| 86 | gContainerItemState = Done; |
| 87 | lastAttributeName = ""; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 88 | } |
| 89 | return context.GetResult(); |
| 90 | } |
| 91 | |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 92 | virtual DataMatchResult AttributeName(const XmlTokenContext& context) { |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 93 | string val; |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 94 | if (gContainerItemState == Started) { |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 95 | if (context.BuildTokenValue(&val)) { |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 96 | if (!val.compare(maxContentBoostAttrName)) { |
| 97 | lastAttributeName = maxContentBoostAttrName; |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 98 | } else if (!val.compare(minContentBoostAttrName)) { |
| 99 | lastAttributeName = minContentBoostAttrName; |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 100 | } else { |
| 101 | lastAttributeName = ""; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | return context.GetResult(); |
| 106 | } |
| 107 | |
| 108 | virtual DataMatchResult AttributeValue(const XmlTokenContext& context) { |
| 109 | string val; |
| 110 | if (gContainerItemState == Started) { |
| 111 | if (context.BuildTokenValue(&val, true)) { |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 112 | if (!lastAttributeName.compare(maxContentBoostAttrName)) { |
| 113 | maxContentBoostStr = val; |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 114 | } else if (!lastAttributeName.compare(minContentBoostAttrName)) { |
| 115 | minContentBoostStr = val; |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 116 | } |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return context.GetResult(); |
| 120 | } |
| 121 | |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 122 | bool getMaxContentBoost(float* max_content_boost) { |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 123 | if (gContainerItemState == Done) { |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 124 | stringstream ss(maxContentBoostStr); |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 125 | float val; |
| 126 | if (ss >> val) { |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 127 | *max_content_boost = val; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 128 | return true; |
| 129 | } else { |
| 130 | return false; |
| 131 | } |
| 132 | } else { |
| 133 | return false; |
| 134 | } |
| 135 | } |
| 136 | |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 137 | bool getMinContentBoost(float* min_content_boost) { |
| 138 | if (gContainerItemState == Done) { |
| 139 | stringstream ss(minContentBoostStr); |
| 140 | float val; |
| 141 | if (ss >> val) { |
| 142 | *min_content_boost = val; |
| 143 | return true; |
| 144 | } else { |
| 145 | return false; |
| 146 | } |
| 147 | } else { |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 152 | private: |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 153 | static const string gContainerItemName; |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 154 | static const string maxContentBoostAttrName; |
| 155 | string maxContentBoostStr; |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 156 | static const string minContentBoostAttrName; |
| 157 | string minContentBoostStr; |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 158 | string lastAttributeName; |
| 159 | ParseState gContainerItemState; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 162 | // GContainer XMP constants - URI and namespace prefix |
| 163 | const string kContainerUri = "http://ns.google.com/photos/1.0/container/"; |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 164 | const string kContainerPrefix = "Container"; |
Fyodor Kyslov | ad58a34 | 2022-12-12 02:10:35 +0000 | [diff] [blame] | 165 | |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 166 | // GContainer XMP constants - element and attribute names |
| 167 | const string kConDirectory = Name(kContainerPrefix, "Directory"); |
| 168 | const string kConItem = Name(kContainerPrefix, "Item"); |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 169 | |
| 170 | // GContainer XMP constants - names for XMP handlers |
| 171 | const string XMPXmlHandler::gContainerItemName = kConItem; |
| 172 | |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 173 | // Item XMP constants - URI and namespace prefix |
| 174 | const string kItemUri = "http://ns.google.com/photos/1.0/container/item/"; |
| 175 | const string kItemPrefix = "Item"; |
| 176 | |
| 177 | // Item XMP constants - element and attribute names |
| 178 | const string kItemLength = Name(kItemPrefix, "Length"); |
| 179 | const string kItemMime = Name(kItemPrefix, "Mime"); |
| 180 | const string kItemSemantic = Name(kItemPrefix, "Semantic"); |
| 181 | |
| 182 | // Item XMP constants - element and attribute values |
| 183 | const string kSemanticPrimary = "Primary"; |
| 184 | const string kSemanticRecoveryMap = "RecoveryMap"; |
| 185 | const string kMimeImageJpeg = "image/jpeg"; |
| 186 | |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 187 | // RecoveryMap XMP constants - URI and namespace prefix |
| 188 | const string kRecoveryMapUri = "http://ns.google.com/photos/1.0/recoverymap/"; |
| 189 | const string kRecoveryMapPrefix = "RecoveryMap"; |
| 190 | |
| 191 | // RecoveryMap XMP constants - element and attribute names |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 192 | const string kMapMaxContentBoost = Name(kRecoveryMapPrefix, "MaxContentBoost"); |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 193 | const string kMapMinContentBoost = Name(kRecoveryMapPrefix, "MinContentBoost"); |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 194 | const string kMapVersion = Name(kRecoveryMapPrefix, "Version"); |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 195 | |
| 196 | // RecoveryMap XMP constants - names for XMP handlers |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 197 | const string XMPXmlHandler::maxContentBoostAttrName = kMapMaxContentBoost; |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 198 | const string XMPXmlHandler::minContentBoostAttrName = kMapMinContentBoost; |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 199 | |
Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 200 | bool getMetadataFromXMP(uint8_t* xmp_data, size_t xmp_size, jpegr_metadata* metadata) { |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 201 | string nameSpace = "http://ns.adobe.com/xap/1.0/\0"; |
| 202 | |
| 203 | if (xmp_size < nameSpace.size()+2) { |
| 204 | // Data too short |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | if (strncmp(reinterpret_cast<char*>(xmp_data), nameSpace.c_str(), nameSpace.size())) { |
| 209 | // Not correct namespace |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | // Position the pointers to the start of XMP XML portion |
| 214 | xmp_data += nameSpace.size()+1; |
| 215 | xmp_size -= nameSpace.size()+1; |
| 216 | XMPXmlHandler handler; |
| 217 | |
| 218 | // We need to remove tail data until the closing tag. Otherwise parser will throw an error. |
| 219 | while(xmp_data[xmp_size-1]!='>' && xmp_size > 1) { |
| 220 | xmp_size--; |
| 221 | } |
| 222 | |
| 223 | string str(reinterpret_cast<const char*>(xmp_data), xmp_size); |
| 224 | MessageHandler msg_handler; |
| 225 | unique_ptr<XmlRule> rule(new XmlElementRule); |
| 226 | XmlReader reader(&handler, &msg_handler); |
| 227 | reader.StartParse(std::move(rule)); |
| 228 | reader.Parse(str); |
| 229 | reader.FinishParse(); |
| 230 | if (reader.HasErrors()) { |
| 231 | // Parse error |
| 232 | return false; |
| 233 | } |
| 234 | |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 235 | if (!handler.getMaxContentBoost(&metadata->maxContentBoost)) { |
Fyodor Kyslov | 6c87e7b | 2022-11-23 03:12:14 +0000 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 239 | if (!handler.getMinContentBoost(&metadata->minContentBoost)) { |
| 240 | return false; |
| 241 | } |
| 242 | |
Fyodor Kyslov | 1dcc442 | 2022-11-16 01:40:53 +0000 | [diff] [blame] | 243 | return true; |
| 244 | } |
| 245 | |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 246 | string generateXmp(int secondary_image_length, jpegr_metadata& metadata) { |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 247 | const vector<string> kConDirSeq({kConDirectory, string("rdf:Seq")}); |
| 248 | const vector<string> kLiItem({string("rdf:li"), kConItem}); |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 249 | |
| 250 | std::stringstream ss; |
| 251 | photos_editing_formats::image_io::XmlWriter writer(ss); |
| 252 | writer.StartWritingElement("x:xmpmeta"); |
| 253 | writer.WriteXmlns("x", "adobe:ns:meta/"); |
| 254 | writer.WriteAttributeNameAndValue("x:xmptk", "Adobe XMP Core 5.1.2"); |
| 255 | writer.StartWritingElement("rdf:RDF"); |
| 256 | writer.WriteXmlns("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); |
| 257 | writer.StartWritingElement("rdf:Description"); |
| 258 | writer.WriteXmlns(kContainerPrefix, kContainerUri); |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 259 | writer.WriteXmlns(kItemPrefix, kItemUri); |
Nick Deakin | 7dfe016 | 2023-01-19 18:27:09 -0500 | [diff] [blame] | 260 | writer.WriteXmlns(kRecoveryMapPrefix, kRecoveryMapUri); |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 261 | writer.StartWritingElements(kConDirSeq); |
| 262 | size_t item_depth = writer.StartWritingElements(kLiItem); |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 263 | writer.WriteAttributeNameAndValue(kItemSemantic, kSemanticPrimary); |
| 264 | writer.WriteAttributeNameAndValue(kItemMime, kMimeImageJpeg); |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 265 | writer.FinishWritingElementsToDepth(item_depth); |
| 266 | writer.StartWritingElements(kLiItem); |
Nick Deakin | 0175906 | 2023-02-02 18:21:43 -0500 | [diff] [blame] | 267 | writer.WriteAttributeNameAndValue(kItemSemantic, kSemanticRecoveryMap); |
| 268 | writer.WriteAttributeNameAndValue(kItemMime, kMimeImageJpeg); |
| 269 | writer.WriteAttributeNameAndValue(kItemLength, secondary_image_length); |
Nick Deakin | 3f89a3e | 2023-02-14 20:35:42 -0500 | [diff] [blame] | 270 | writer.WriteAttributeNameAndValue(kMapVersion, metadata.version); |
| 271 | writer.WriteAttributeNameAndValue(kMapMaxContentBoost, metadata.maxContentBoost); |
| 272 | writer.WriteAttributeNameAndValue(kMapMinContentBoost, metadata.minContentBoost); |
Dichen Zhang | dc8452b | 2022-11-23 17:17:56 +0000 | [diff] [blame] | 273 | writer.FinishWriting(); |
| 274 | |
| 275 | return ss.str(); |
| 276 | } |
| 277 | |
Dichen Zhang | b2ed830 | 2023-02-10 23:05:04 +0000 | [diff] [blame] | 278 | } // namespace android::jpegrecoverymap |