RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
JpegMarkers.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2009-2014 Klaus Post
5 Copyright (C) 2017 Axel Waggershauser
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#include "adt/Optional.h"
23#include "io/ByteStream.h"
24#include <cstdint>
25
26#pragma once
27
28/*
29 * The following enum and two structs are stolen from the IJG JPEG library
30 * Comments added by tm. See also Copyright in PrefixCodeDecoder.h.
31 */
32
33namespace rawspeed {
34
35// JPEG marker codes
36enum class JpegMarker : uint8_t {
37 STUFF = 0x00,
38 SOF0 = 0xc0, // baseline DCT
39 SOF1 = 0xc1, // extended sequential DCT
40 SOF2 = 0xc2, // progressive DCT
41 SOF3 = 0xc3, // lossless (sequential)
42
43 SOF5 = 0xc5, // differential sequential DCT
44
45 SOF6 = 0xc6, // differential progressive DCT
46
47 SOF7 = 0xc7, // differential lossless
48
49 JPG = 0xc8, // JPEG extensions
50 SOF9 = 0xc9, // extended sequential DCT
51 SOF10 = 0xca, // progressive DCT
52 SOF11 = 0xcb, // lossless (sequential)
53
54 SOF13 = 0xcd, // differential sequential DCT
55
56 SOF14 = 0xce, // differential progressive DCT
57
58 SOF15 = 0xcf, // differential lossless
59
60 DHT = 0xc4, // define Huffman tables
61
62 DAC = 0xcc, // define arithmetic conditioning table
63
64 RST0 = 0xd0, // restart
65 RST1 = 0xd1, // restart
66 RST2 = 0xd2, // restart
67 RST3 = 0xd3, // restart
68 RST4 = 0xd4, // restart
69 RST5 = 0xd5, // restart
70 RST6 = 0xd6, // restart
71 RST7 = 0xd7, // restart
72
73 SOI = 0xd8, // start of image
74 EOI = 0xd9, // end of image
75 SOS = 0xda, // start of scan
76 DQT = 0xdb, // define quantization tables
77 DNL = 0xdc, // define number of lines
78 DRI = 0xdd, // define restart interval
79 DHP = 0xde, // define hierarchical progression
80 EXP = 0xdf, // expand reference image(s)
81
82 APP0 = 0xe0, // application marker, used for JFIF
83 APP1 = 0xe1, // application marker
84 APP2 = 0xe2, // application marker
85 APP3 = 0xe3, // application marker
86 APP4 = 0xe4, // application marker
87 APP5 = 0xe5, // application marker
88 APP6 = 0xe6, // application marker
89 APP7 = 0xe7, // application marker
90 APP8 = 0xe8, // application marker
91 APP9 = 0xe9, // application marker
92 APP10 = 0xea, // application marker
93 APP11 = 0xeb, // application marker
94 APP12 = 0xec, // application marker
95 APP13 = 0xed, // application marker
96 APP14 = 0xee, // application marker, used by Adobe
97 APP15 = 0xef, // application marker
98
99 JPG0 = 0xf0, // reserved for JPEG extensions
100
101 JPG13 = 0xfd, // reserved for JPEG extensions
102
103 COM = 0xfe, // comment
104
105 TEM = 0x01, // temporary use
106 FILL = 0xFF
107
108};
109
111 uint8_t c0 = input.peekByte(0);
112 uint8_t c1 = input.peekByte(1);
113
114 if (c0 == 0xFF && c1 != 0 && c1 != 0xFF)
115 return static_cast<JpegMarker>(c1);
116 return {};
117}
118
120 bool skipPadding) {
121 while (input.getRemainSize() >= 2) {
122 if (Optional<JpegMarker> m = peekMarker(input))
123 return input;
124
125 // Marker not found. Might there be leading padding bytes?
126 if (!skipPadding)
127 break; // Nope, give up.
128
129 // Advance by a single(!) byte and try again.
130 input.skipBytes(1);
131 }
132
133 return std::nullopt;
134}
135
136// Get the number of this restart marker (modulo 8).
138 switch (m) {
139 using enum JpegMarker;
140 case RST0:
141 case RST1:
142 case RST2:
143 case RST3:
144 case RST4:
145 case RST5:
146 case RST6:
147 case RST7:
148 return static_cast<uint8_t>(m) - static_cast<uint8_t>(RST0);
149 default:
150 return std::nullopt; // Not a restart marker.
151 }
152}
153
154} // namespace rawspeed
size_type RAWSPEED_READONLY getRemainSize() const
Definition ByteStream.h:87
uint8_t peekByte(size_type i=0) const
Definition ByteStream.h:183
void skipBytes(size_type nbytes)
Definition ByteStream.h:130
Optional< ByteStream > advanceToNextMarker(ByteStream input, bool skipPadding)
Optional< int > getRestartMarkerNumber(JpegMarker m)
Optional< JpegMarker > peekMarker(ByteStream input)