• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef IMAGE_IO_JPEG_JPEG_INFO_BUILDER_H_  // NOLINT
2 #define IMAGE_IO_JPEG_JPEG_INFO_BUILDER_H_  // NOLINT
3 
4 #include <set>
5 #include <string>
6 #include <vector>
7 
8 #include "image_io/base/data_range.h"
9 #include "image_io/jpeg/jpeg_info.h"
10 #include "image_io/jpeg/jpeg_segment_processor.h"
11 #include "image_io/jpeg/jpeg_xmp_info_builder.h"
12 
13 namespace photos_editing_formats {
14 namespace image_io {
15 
16 /// JpegInfoBuilder is JpegSegmentProcessor that collects the location and type
17 /// of depth information in the JPEG file so that subsequent operations can
18 /// efficiently maniuplate it.
19 class JpegInfoBuilder : public JpegSegmentProcessor {
20  public:
21   JpegInfoBuilder();
22 
23   /// @return The JpegInfo with the depth information obtained from the
24   ///     scanner as a result of processing the segments it processes.
GetInfo()25   const JpegInfo& GetInfo() const { return jpeg_info_; }
26 
27   /// @param image_limit The max number of images to process. By default there
28   ///     is no limit on the number of images processed.
SetImageLimit(int image_limit)29   void SetImageLimit(int image_limit) { image_limit_ = image_limit; }
30 
31   /// By default the info builder does not capture the value of the segment in
32   /// the segment infos contained in the @c JpegInfo object. Call this function
33   /// to capture the bytes of the indicated segment types.
34   /// @param type The type of segment info to capture the value of.
35   void SetCaptureSegmentBytes(const std::string& segment_info_type);
36 
37   /// @return True if the segment is a primary Xmp segment.
38   bool IsPrimaryXmpSegment(const JpegSegment& segment) const;
39 
40   /// @return True if the segment is an extended Xmp segment.
41   bool IsExtendedXmpSegment(const JpegSegment& segment) const;
42 
43   void Start(JpegScanner* scanner) override;
44   void Process(JpegScanner* scanner, const JpegSegment& segment) override;
45   void Finish(JpegScanner* scanner) override;
46 
47  private:
48   /// @return True if the data members indicate Apple depth is present.
49   bool HasAppleDepth() const;
50 
51   /// @return True if the data members indicate Apple matte is present.
52   bool HasAppleMatte() const;
53 
54   /// @return True if the segment is an Mpf segment.
55   bool IsMpfSegment(const JpegSegment& segment) const;
56 
57   /// @return True if the segment is an Exif segment.
58   bool IsExifSegment(const JpegSegment& segment) const;
59 
60   /// @return True if the segment is an Jfif segment.
61   bool IsJfifSegment(const JpegSegment& segment) const;
62 
63   /// Captures the segment bytes into the a JpegSegmentInfo's byte vector if
64   /// the SetCaptureSegmentBytes() has been called for the segment info type.
65   /// @param type The type of segment info being processed.
66   /// @param segment The segment being processed.
67   /// @param bytes A vector to hold the segment bytes.
68   void MaybeCaptureSegmentBytes(const std::string& type,
69                                 const JpegSegment& segment,
70                                 std::vector<Byte>* bytes) const;
71 
72   /// @return True if the segment's extended xmp guid matches the one from the
73   ///     primary xmp segment.
74   bool HasMatchingExtendedXmpGuid(const JpegSegment& segment) const;
75 
76   /// @return True if the segment contains the given id.
77   bool HasId(const JpegSegment& segment, const char* id) const;
78 
79   /// Sets the primary segment guid value using properties in the given segment.
80   /// @param The segment from which to obtain the primary xmp guid value.
81   void SetPrimaryXmpGuid(const JpegSegment& segment);
82 
83   /// Sets the Xmp mime type using property values in the given segment.
84   /// @param The segment from which to obtain the mime property value.
85   /// @param xmp_info_type The type of xmp data that determines the mime
86   ///     property name to look for.
87   void SetXmpMimeType(const JpegSegment& segment,
88                       JpegXmpInfo::Type xmp_info_type);
89 
90   /// The limit on the number of images to process. After this many images have
91   /// been found, the Process() function will tell the JpegScanner to stop.
92   int image_limit_;
93 
94   /// The number of images encountered in the JPEG file so far.
95   int image_count_;
96 
97   /// The number of APP2/MPF segments encountered per image. One criterial used
98   /// to determine if Apple depth data is present is that the first image has
99   /// an APP2/MPF segment.
100   std::vector<int> image_mpf_count_;
101 
102   /// The number of APP1/XMP segments encountered per image. Another criteria
103   /// used to determine if Apple depth data is present is that the second or
104   /// following image contains one of these segments.
105   std::vector<int> image_xmp_apple_depth_count_;
106 
107   /// The number of APP1/XMP segments encountered per image. Another criteria
108   /// used to determine if Apple matte data is present is that the second or
109   /// following image contains one of these segments.
110   std::vector<int> image_xmp_apple_matte_count_;
111 
112   /// The DataRange of the most recent SOI type segment. This is used to compute
113   /// the range of the image that represents the Apple depth data.
114   DataRange most_recent_soi_marker_range_;
115 
116   /// The GUID value of the APP1/XMP segments that contain GDepth/GImage data.
117   std::string primary_xmp_guid_;
118 
119   /// Builder helpers for gdepth and gimage xmp type segments.
120   JpegXmpInfoBuilder gdepth_info_builder_;
121   JpegXmpInfoBuilder gimage_info_builder_;
122 
123   /// The collected data describing the type/location of data in the JPEG file.
124   JpegInfo jpeg_info_;
125 
126   /// The types of the segment info type to capture the bytes of.
127   std::set<std::string> capture_segment_bytes_types_;
128 };
129 
130 }  // namespace image_io
131 }  // namespace photos_editing_formats
132 
133 #endif // IMAGE_IO_JPEG_JPEG_INFO_BUILDER_H_  // NOLINT
134