summaryrefslogtreecommitdiff
path: root/includes/image_io/jpeg/jpeg_segment_info.h
blob: 6fb25db5ce06a13e8d58c5a33e06bbcb342f5073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef IMAGE_IO_JPEG_JPEG_SEGMENT_INFO_H_  // NOLINT
#define IMAGE_IO_JPEG_JPEG_SEGMENT_INFO_H_  // NOLINT

#include <string>
#include <vector>

#include "image_io/base/data_range.h"
#include "image_io/base/types.h"

namespace photos_editing_formats {
namespace image_io {

/// Interesting segment types.
const char kExif[] = "Exif";
const char kJfif[] = "JFIF";
const char kMpf[] = "MPF";

/// A class that holds interesting information about a JpegSegment.
class JpegSegmentInfo {
 public:
  /// @param image_index The index of the image in a @c DataSource that contains
  ///     the segment.
  /// @param data_range The range in the segment in the @c DataSource.
  /// @param type The type of segment.
  JpegSegmentInfo(size_t image_index, const DataRange& data_range,
                  const std::string& type)
      : image_index_(image_index), data_range_(data_range), type_(type) {}

  /// Constructs an empty, invalid segment info.
  JpegSegmentInfo() : image_index_(0) {}

  JpegSegmentInfo(const JpegSegmentInfo&) = default;
  JpegSegmentInfo& operator=(const JpegSegmentInfo&) = default;

  /// @param rhs The segment info to compare with this one.
  /// @return Whether the segment infos are equal
  bool operator==(const JpegSegmentInfo& rhs) const {
    return image_index_ == rhs.image_index_ && data_range_ == rhs.data_range_ &&
           type_ == rhs.type_ && bytes_ == rhs.bytes_;
  }

  /// @param rhs The segment info to compare with this one.
  /// @return Whether the segment infos are not equal
  bool operator!=(const JpegSegmentInfo& rhs) const {
    return !(*this == rhs);
  }

  /// @return Whether the segment info is valid.
  bool IsValid() const { return !type_.empty() && data_range_.IsValid(); }

  /// @return The image index of the segment info.
  size_t GetImageIndex() const { return image_index_; }

  /// @return The data range of the segment info.
  const DataRange& GetDataRange() const { return data_range_; }

  /// @return The type of the segment info.
  const std::string& GetType() const { return type_; }

  /// @return The (optional) bytes of the segment to which the info refers.  The
  ///     vector will be empty unless the GetMutableBytes() function has been
  ///     and the vector filled with the segment contents.
  const std::vector<Byte>& GetBytes() const { return bytes_; }

  /// @return A non-const pointer to the bytes vector.
  std::vector<Byte>* GetMutableBytes() { return &bytes_; }

 private:
  // The image index where the segment is located.
  size_t image_index_;

  // The data range of the segment.
  DataRange data_range_;

  // The type of segment.
  std::string type_;

  // The (optional) bytes of the segment.
  std::vector<Byte> bytes_;
};

}  // namespace image_io
}  // namespace photos_editing_formats

#endif // IMAGE_IO_JPEG_JPEG_SEGMENT_INFO_H_  // NOLINT