/
jpegpipeline.cpp
117 lines (102 loc) · 2.68 KB
/
jpegpipeline.cpp
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
#include <stdio.h>
#include <stdexcept>
#include "jpegpipeline.h"
void JpegPipeline::reset()
{
state = State::WANT_FF;
pos = -1;
s1 = 0;
s2 = 0;
skip_bytes = 0;
entropy_data_follows = false;
current_type = 0;
}
/**
* @brief RawStreamReceiver::accept_byte Accepts next byte of input and sets state accordingly.
* @param byte Next byte to handle.
* @return
*/
void JpegPipeline::acceptByte(uint8_t byte)
{
pos++;
switch(state)
{
case State::END_OF_JPEG:
forward(byte);
return;
case State::INVALID:
throw Exception("State invalid");
return;
case State::WANT_FF:
if (byte != 0xFF) {
throw Exception("Expected 0xFF");
}
state = State::WANT_TYPE;
return;
case State::WANT_S1:
s1 = byte;
state = State::WANT_S2;
return;
case State::WANT_S2:
s2 = byte;
state = State::SKIP_BYTES;
skip_bytes = (s1 << 8) + s2 - 2; // -2 since we already read S1 S2
return;
case State::SKIP_BYTES:
skip_bytes--;
if (skip_bytes == 0) {
if (entropy_data_follows) {
state = State::WANT_ENTROPY_DATA;
}
else {
state = State::WANT_FF;
}
}
return;
case State::WANT_ENTROPY_DATA:
if (byte == 0xFF) {
state = State::ENTROPY_GOT_FF;
}
return;
case State::ENTROPY_GOT_FF:
if (byte == 0) {
// Just an escaped 0
state = State::WANT_ENTROPY_DATA;
return;
}
else if (byte == 0xFF) {
// Padding
state = State::ENTROPY_GOT_FF;
return;
}
// If not FF00 and FFFF then we got a real segment type now.
state = State::WANT_TYPE;
entropy_data_follows = false;
// FALL THROUGH
case State::WANT_TYPE:
current_type = byte;
switch(byte)
{
case 0xd8: // SOI (Start of image)
state = State::WANT_FF;
return;
case 0xd9: // EOI (End of image)
state = State::END_OF_JPEG;
return;
case 0xda: // SOS (Start of scan)
case 0xc0: // Baseline DCT
case 0xc4: // Huffman Table
entropy_data_follows = true;
// Above sections will have entropy data following.
// Fall through
case 0xdb: // Quantization Table
case 0xe0: // JFIF APP0
case 0xe1: // JFIF APP0
state = State::WANT_S1;
return;
default:
throw Exception("Unknown JPEG segment type.");
return;
}
}
}