• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef IMAGE_IO_JPEG_JPEG_INFO_H_  // NOLINT
2 #define IMAGE_IO_JPEG_JPEG_INFO_H_  // NOLINT
3 
4 #include <vector>
5 
6 #include "image_io/base/data_range.h"
7 #include "image_io/jpeg/jpeg_segment_info.h"
8 #include "image_io/jpeg/jpeg_xmp_info.h"
9 
10 namespace photos_editing_formats {
11 namespace image_io {
12 
13 /// A class to represent interesting depth and image information in a JPEG file,
14 /// and where it is located so that it can be efficiently extracted.
15 class JpegInfo {
16  public:
JpegInfo()17   JpegInfo() { JpegXmpInfo::InitializeVector(&xmp_info_vector_); }
18   JpegInfo(const JpegInfo&) = default;
19   JpegInfo& operator=(const JpegInfo&) = default;
20 
21   /// @return The vector of data ranges indicating the locations of the images.
GetImageRanges()22   const std::vector<DataRange>& GetImageRanges() const { return image_ranges_; }
23 
24   /// @return The vector of interesting segment info structures.
GetSegmentInfos()25   const std::vector<JpegSegmentInfo>& GetSegmentInfos() const {
26     return segment_infos_;
27   }
28 
29   /// @param image_index The image containing the sought after segment info.
30   /// @param type The type of segment info to get.
31   /// @return The segment info, or one that is invalid if not found.
GetSegmentInfo(size_t image_index,const std::string & type)32   JpegSegmentInfo GetSegmentInfo(size_t image_index,
33                                  const std::string& type) const {
34     for (const auto& segment_info : GetSegmentInfos()) {
35       if (segment_info.GetImageIndex() == image_index &&
36           segment_info.GetType() == type) {
37         return segment_info;
38       }
39     }
40     return JpegSegmentInfo(0, DataRange(), "");
41   }
42 
43   /// @return True if there is Apple depth information.
HasAppleDepth()44   bool HasAppleDepth() const { return apple_depth_image_range_.IsValid(); }
45 
46   /// @return True if there is Apple matte information.
HasAppleMatte()47   bool HasAppleMatte() const { return apple_matte_image_range_.IsValid(); }
48 
49   /// @return True if there is GDepth type depth information.
HasGDepth()50   bool HasGDepth() const {
51     return HasImage(JpegXmpInfo::kGDepthInfoType);
52   }
53 
54   /// @return True if there is GImage information.
HasGImage()55   bool HasGImage() const {
56     return HasImage(JpegXmpInfo::kGImageInfoType);
57   }
58 
59   /// @return True if there is either Apple or GDepth information.
HasDepth()60   bool HasDepth() const { return HasAppleDepth() || HasGDepth(); }
61 
62   /// @return True if there is an extratable image present.
HasExtractableImage()63   bool HasExtractableImage() const {
64     return HasAppleDepth() || HasAppleMatte() || HasGDepth() || HasGImage();
65   }
66 
67   /// @param xmp_info_type The type of xmp image information desired.
68   /// @return True if there is information of the given type.
HasImage(JpegXmpInfo::Type xmp_info_type)69   bool HasImage(JpegXmpInfo::Type xmp_info_type) const {
70     return !GetSegmentDataRanges(xmp_info_type).empty();
71   }
72 
73   /// @return The DataRange where the Apple depth information is located.
GetAppleDepthImageRange()74   const DataRange& GetAppleDepthImageRange() const {
75     return apple_depth_image_range_;
76   }
77 
78   /// @return The DataRange where the Apple matte information is located.
GetAppleMatteImageRange()79   const DataRange& GetAppleMatteImageRange() const {
80     return apple_matte_image_range_;
81   }
82 
83   /// @param type The type of Xmp data to get the mime type of.
84   /// @return The mime type for the Xmp data of the given type.
GetMimeType(JpegXmpInfo::Type type)85   std::string GetMimeType(JpegXmpInfo::Type type) const {
86     return xmp_info_vector_[type].GetMimeType();
87   }
88 
89   /// @param type The type of Xmp data to get the segment data ranges of.
90   /// @return The segment data ranges containing the Xmp data of the given type.
GetSegmentDataRanges(JpegXmpInfo::Type type)91   const std::vector<DataRange>& GetSegmentDataRanges(
92       JpegXmpInfo::Type type) const {
93     return xmp_info_vector_[type].GetSegmentDataRanges();
94   }
95 
96   /// Adds a DataRange to the vector of image DataRanges.
97   /// @param image_range The data range of an image.
AddImageRange(const DataRange & image_range)98   void AddImageRange(const DataRange& image_range) {
99     image_ranges_.push_back(image_range);
100   }
101 
102   /// Adds a JpegSegmentInfo to the vector of JpegSegmentInfos.
103   /// @param jpeg_segment_info The info structure to add.
AddSegmentInfo(const JpegSegmentInfo & segment_info)104   void AddSegmentInfo(const JpegSegmentInfo& segment_info) {
105     segment_infos_.push_back(segment_info);
106   }
107 
108   /// @param data_range The DataRange where Apple depth information is located.
SetAppleDepthImageRange(const DataRange & data_range)109   void SetAppleDepthImageRange(const DataRange& data_range) {
110     apple_depth_image_range_ = data_range;
111   }
112 
113   /// @param data_range The DataRange where Apple matte information is located.
SetAppleMatteImageRange(const DataRange & data_range)114   void SetAppleMatteImageRange(const DataRange& data_range) {
115     apple_matte_image_range_ = data_range;
116   }
117 
118   /// @param type The type of Xmp data to set the mime type of.
119   /// @param mime_type The mime type of the Xmp data.
SetMimeType(JpegXmpInfo::Type type,const std::string & mime_type)120   void SetMimeType(JpegXmpInfo::Type type, const std::string& mime_type) {
121     xmp_info_vector_[type].SetMimeType(mime_type);
122   }
123 
124   /// @param type The type of Xmp data to set segment data ranges of.
125   /// @param segment_data_ranges The segment that contain the Xmp data.
SetSegmentDataRanges(JpegXmpInfo::Type type,const std::vector<DataRange> & segment_data_ranges)126   void SetSegmentDataRanges(JpegXmpInfo::Type type,
127                             const std::vector<DataRange>& segment_data_ranges) {
128     xmp_info_vector_[type].SetSegmentDataRanges(segment_data_ranges);
129   }
130 
131  private:
132   /// The DataRanges of all images.
133   std::vector<DataRange> image_ranges_;
134 
135   /// Interesting segment information. Currently information about APP0/JFIF,
136   /// APP1/EXIF and APP2/MPF segments are saved here.
137   std::vector<JpegSegmentInfo> segment_infos_;
138 
139   /// The DataRange of the Apple depth information.
140   DataRange apple_depth_image_range_;
141 
142   /// The DataRange of the Apple depth information.
143   DataRange apple_matte_image_range_;
144 
145   /// A vector holding information about the Xmp segments containing GDepth and
146   /// GImage data.
147   std::vector<JpegXmpInfo> xmp_info_vector_;
148 };
149 
150 }  // namespace image_io
151 }  // namespace photos_editing_formats
152 
153 #endif // IMAGE_IO_JPEG_JPEG_INFO_H_  // NOLINT
154