1 #ifndef DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_IMAGE_H_ // NOLINT 2 #define DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_IMAGE_H_ // NOLINT 3 4 #include <memory> 5 #include <string> 6 #include <unordered_map> 7 8 #include "dynamic_depth/element.h" 9 #include "dynamic_depth/item.h" 10 #include "xmpmeta/xml/deserializer.h" 11 #include "xmpmeta/xml/serializer.h" 12 13 namespace dynamic_depth { 14 15 // The ItemSemantic of this Image. 16 enum class ImageItemSemantic { kPrimary = 1, kOriginal = 2 }; 17 18 /** 19 * An Image element for a Dynamic Depth device. 20 */ 21 class Image : public Element { 22 public: 23 // Appends child elements' namespaces' and their respective hrefs to the 24 // given collection, and any parent nodes' names to prefix_names. 25 // Key: Name of the namespace. 26 // Value: Full namespace URL. 27 // Example: ("Image", "http://ns.google.com/photos/dd/1.0/image/") 28 void GetNamespaces( 29 std::unordered_map<string, string>* ns_name_href_map) override; 30 31 // Serializes this object. 32 bool Serialize( 33 ::dynamic_depth::xmpmeta::xml::Serializer* serializer) const override; 34 35 // Creates an original (non-primary) Image from the given fields. Returns null 36 // if one of the following is true: 37 // - Mime field is empty. 38 // - Data field is null or empty. 39 // - Uri field is empty. 40 // - The items field is null. 41 // Param description: 42 // - Data is NOT base64-encoded. This method takes care of encoding when it 43 // is passed to the generated Item element. 44 // - Mime is the mimetype of the image data. 45 // - Uri is the case-sensitive Item:DataUri of this Image, and must be 46 // unique amongst all Dynamic Depth elements. This will be the only way 47 // that metadata parsers will be able to retrieve the image. 48 // Both data and mime are used to construct a new Container:Item, which will 49 // be the last one appended to items. 50 // It is the caller's responsibility to use items to construct a Container, 51 // and ensure that it is serialized along with this Image element. 52 static std::unique_ptr<Image> FromData( 53 const string& data, const string& mime, const string& item_uri, 54 std::vector<std::unique_ptr<Item>>* items); 55 56 // Same as above, but more performant because it avoids an extra string copy. 57 static std::unique_ptr<Image> FromData( 58 const uint8_t* data, size_t data_size, const string& mime, 59 const string& item_uri, std::vector<std::unique_ptr<Item>>* items); 60 61 // Image instantiator for the primary (container) image. 62 static std::unique_ptr<Image> FromDataForPrimaryImage( 63 const string& mime, std::vector<std::unique_ptr<Item>>* items); 64 65 // Returns the deserialized Image; null if parsing fails. 66 static std::unique_ptr<Image> FromDeserializer( 67 const ::dynamic_depth::xmpmeta::xml::Deserializer& parent_deserializer); 68 69 const string& GetItemUri() const; 70 ImageItemSemantic GetItemSemantic() const; 71 72 // Disallow copying. 73 Image(const Image&) = delete; 74 void operator=(const Image&) = delete; 75 76 private: 77 Image(); 78 79 // Extracts image fields. 80 bool ParseImageFields( 81 const ::dynamic_depth::xmpmeta::xml::Deserializer& deserializer); 82 83 string item_uri_; 84 ImageItemSemantic item_semantic_; 85 }; 86 87 } // namespace dynamic_depth 88 89 #endif // DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_IMAGE_H_ // NOLINT 90