aboutsummaryrefslogtreecommitdiff
path: root/third_party/image_io/src/jpeg/jpeg_scanner.cc
blob: c039f70c86d131e2c73b718579bfe2e9613196d2 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "image_io/jpeg/jpeg_scanner.h"

#include <sstream>

#include "image_io/base/message_handler.h"
#include "image_io/jpeg/jpeg_segment.h"

namespace photos_editing_formats {
namespace image_io {

using std::stringstream;

/// The minimum size for the DataSegments requested from the DataSource. Using
/// this value will guarentee that a JpegSegment will occupy at most two
/// DataSegments.
const size_t kMinBufferDataRequestSize = 0x10000;

void JpegScanner::Run(DataSource* data_source,
                      JpegSegmentProcessor* segment_processor) {
  if (data_source_) {
    // The Run() function is already active.
    return;
  }
  data_source_ = data_source;
  segment_processor_ = segment_processor;
  current_location_ = 0;
  done_ = false;
  has_error_ = false;
  data_source_->Reset();
  current_segment_ = data_source_->GetDataSegment(current_location_,
                                                  kMinBufferDataRequestSize);
  segment_processor_->Start(this);
  FindAndProcessSegments();
  segment_processor_->Finish(this);
  data_source_ = nullptr;
  segment_processor_ = nullptr;
  current_segment_.reset();
  next_segment_.reset();
}

void JpegScanner::FindAndProcessSegments() {
  while (!IsDone() && !HasError()) {
    size_t begin_segment_location =
        current_segment_->Find(current_location_, JpegMarker::kStart);
    if (begin_segment_location == current_segment_->GetEnd()) {
      GetNextSegment();
      if (next_segment_) {
        current_location_ =
            std::max(current_location_, next_segment_->GetBegin());
        current_segment_ = next_segment_;
        next_segment_.reset();
        continue;
      }
      SetDone();
      break;
    }
    size_t payload_size = 0;
    JpegMarker marker(
        GetByte(begin_segment_location + JpegMarker::kTypeOffset));
    if (marker.IsValid() && !HasError()) {
      payload_size = GetPayloadSize(marker, begin_segment_location);
      if (marker.IsValid() && interesting_marker_flags_[marker.GetType()]) {
        size_t end_segment_location =
            begin_segment_location + JpegMarker::kLength + payload_size;
        GetByte(end_segment_location - 1);
        if (!HasError()) {
          JpegSegment segment(begin_segment_location, end_segment_location,
                              current_segment_.get(), next_segment_.get());
          segment_processor_->Process(this, segment);
        }
      }
    }
    current_location_ =
        begin_segment_location + JpegMarker::kLength + payload_size;
  }
}

size_t JpegScanner::GetPayloadSize(const JpegMarker& marker,
                                   size_t begin_location) {
  if (marker.HasVariablePayloadSize()) {
    return (GetByte(begin_location + JpegMarker::kLength) << 8) |
           GetByte(begin_location + JpegMarker::kLength + 1);
  } else {
    return 0;
  }
}

ValidatedByte JpegScanner::GetValidatedByte(size_t location) {
  if (current_segment_->Contains(location)) {
    return current_segment_->GetValidatedByte(location);
  }
  GetNextSegment();
  if (next_segment_ && next_segment_->Contains(location)) {
    return next_segment_->GetValidatedByte(location);
  }
  if (message_handler_) {
    stringstream sstream;
    sstream << location;
    message_handler_->ReportMessage(Message::kPrematureEndOfDataError,
                                    sstream.str());
  }
  return InvalidByte();
}

Byte JpegScanner::GetByte(size_t location) {
  ValidatedByte validated_byte = GetValidatedByte(location);
  if (validated_byte.is_valid) {
    return validated_byte.value;
  }
  has_error_ = true;
  return 0;
}

void JpegScanner::GetNextSegment() {
  if (!next_segment_ && current_segment_) {
    next_segment_ = data_source_->GetDataSegment(current_segment_->GetEnd(),
                                                 kMinBufferDataRequestSize);
  }
}

}  // namespace image_io
}  // namespace photos_editing_formats