RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
AbstractLJpegDecoder.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#pragma once
23
24#include "adt/Casts.h"
26#include "codes/HuffmanCode.h"
28#include "common/RawImage.h"
32#include "io/ByteStream.h"
33#include <algorithm>
34#include <array>
35#include <cstdint>
36#include <memory>
37#include <vector>
38
39/*
40 * The two following structs are stolen from the IJG JPEG library
41 * Comments added by tm. See also Copyright in PrefixCodeDecoder.h.
42 */
43
44namespace rawspeed {
45
46/*
47 * The following structure stores basic information about one component.
48 */
49struct JpegComponentInfo final {
50 /*
51 * These values are fixed over the whole image.
52 * They are read from the SOF marker.
53 */
54 uint32_t componentId = ~0U; /* identifier for this component (0..255) */
55
56 /*
57 * Huffman table selector (0..3). The value may vary
58 * between scans. It is read from the SOS marker.
59 */
61 uint32_t superH = ~0U; // Horizontal Supersampling
62 uint32_t superV = ~0U; // Vertical Supersampling
63};
64
65class SOFInfo final {
66public:
67 std::array<JpegComponentInfo, 4> compInfo;
68 uint32_t w = 0; // Width
69 uint32_t h = 0; // Height
70 uint32_t cps = 0; // Components
71 uint32_t prec = 0; // Precision
72 bool initialized = false;
73};
74
76 // std::vector of unique HTs, to not recreate HT, but cache them
77 std::vector<std::unique_ptr<const HuffmanCode<BaselineCodeTag>>>
79 std::vector<std::unique_ptr<const PrefixCodeDecoder<>>>
81
83 std::array<const PrefixCodeDecoder<>*, 4> huff{
84 {}}; // 4 pointers into the store
85
86 virtual void anchor() const;
87
88public:
90 [[nodiscard]] int getSamplePrecision() const { return frame.prec; }
91
92 virtual ~AbstractLJpegDecoder() = default;
93
94protected:
95 bool fixDng16Bug = false; // DNG v1.0.x compatibility
96 bool fullDecodeHT = true; // FullDecode Huffman
97
98 // Certain non-standard-complaint LJpeg's (old Hasselblad cameras) might not
99 // end with an EOI marker. This erratum considers an implicit EOI marker
100 // to be present after the (first) full Scan.
101 [[nodiscard]] virtual bool erratumImplicitEOIMarkerAfterScan() const {
102 return false;
103 }
104
105 void decodeSOI();
106 void parseSOF(ByteStream data, SOFInfo* i);
107 void parseSOS(ByteStream data);
108 void parseDHT(ByteStream data);
109 void parseDRI(ByteStream dri);
110 JpegMarker getNextMarker(bool allowskip);
111
112 [[nodiscard]] std::vector<const PrefixCodeDecoder<>*>
113 getPrefixCodeDecoders(int N_COMP) const {
114 std::vector<const PrefixCodeDecoder<>*> ht(N_COMP);
115 for (int i = 0; i < N_COMP; ++i) {
116 const unsigned dcTblNo = frame.compInfo[i].dcTblNo;
117 if (const auto dcTbls = implicit_cast<unsigned>(huff.size());
118 dcTblNo >= dcTbls) {
119 ThrowRDE("Decoding table %u for comp %i does not exist (tables = %u)",
120 dcTblNo, i, dcTbls);
121 }
122 ht[i] = huff[dcTblNo];
123 }
124
125 return ht;
126 }
127
128 [[nodiscard]] std::vector<uint16_t> getInitialPredictors(int N_COMP) const {
129 std::vector<uint16_t> pred(N_COMP);
130 if (frame.prec < (Pt + 1)) {
131 ThrowRDE("Invalid precision (%u) and point transform (%u) combination!",
132 frame.prec, Pt);
133 }
134 std::fill(pred.begin(), pred.end(), 1 << (frame.prec - Pt - 1));
135 return pred;
136 }
137
138 [[nodiscard]] virtual ByteStream::size_type decodeScan() = 0;
139
142
146};
147
148} // namespace rawspeed
#define ThrowRDE(...)
virtual ByteStream::size_type decodeScan()=0
virtual ~AbstractLJpegDecoder()=default
void parseSOF(ByteStream data, SOFInfo *i)
std::vector< std::unique_ptr< const PrefixCodeDecoder<> > > PrefixCodeDecoderStore
virtual bool erratumImplicitEOIMarkerAfterScan() const
JpegMarker getNextMarker(bool allowskip)
AbstractLJpegDecoder(ByteStream bs, RawImage img)
std::vector< std::unique_ptr< const HuffmanCode< BaselineCodeTag > > > huffmanCodeStore
std::array< const PrefixCodeDecoder<> *, 4 > huff
std::vector< uint16_t > getInitialPredictors(int N_COMP) const
std::vector< const PrefixCodeDecoder<> * > getPrefixCodeDecoders(int N_COMP) const
uint32_t size_type
Definition Buffer.h:49
std::array< JpegComponentInfo, 4 > compInfo
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition Casts.h:32