forked from darktable-org/rawspeed
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLJpegDecoder.cpp
More file actions
161 lines (128 loc) · 5.71 KB
/
LJpegDecoder.cpp
File metadata and controls
161 lines (128 loc) · 5.71 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
RawSpeed - RAW file decoder.
Copyright (C) 2017 Axel Waggershauser
Copyright (C) 2017 Roman Lebedev
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "decompressors/LJpegDecoder.h"
#include "adt/Casts.h"
#include "adt/Invariant.h"
#include "adt/Point.h"
#include "common/RawImage.h"
#include "decoders/RawDecoderException.h"
#include "decompressors/AbstractLJpegDecoder.h"
#include "decompressors/LJpegDecompressor.h"
#include "io/Buffer.h"
#include "io/ByteStream.h"
#include <algorithm>
#include <array>
#include <cstdint>
#include <iterator>
#include <vector>
using std::copy_n;
namespace rawspeed {
LJpegDecoder::LJpegDecoder(ByteStream bs, const RawImage& img)
: AbstractLJpegDecoder(bs, img) {
if (mRaw->getDataType() != RawImageType::UINT16)
ThrowRDE("Unexpected data type (%u)",
static_cast<unsigned>(mRaw->getDataType()));
if ((mRaw->getCpp() != 1 || mRaw->getBpp() != sizeof(uint16_t)) &&
(mRaw->getCpp() != 2 || mRaw->getBpp() != 2 * sizeof(uint16_t)) &&
(mRaw->getCpp() != 3 || mRaw->getBpp() != 3 * sizeof(uint16_t)))
ThrowRDE("Unexpected component count (%u)", mRaw->getCpp());
if (!mRaw->dim.hasPositiveArea())
ThrowRDE("Image has zero size");
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
// Yeah, sure, here it would be just dumb to leave this for production :)
if (mRaw->dim.x > 7424 || mRaw->dim.y > 5552) {
ThrowRDE("Unexpected image dimensions found: (%u; %u)", mRaw->dim.x,
mRaw->dim.y);
}
#endif
}
void LJpegDecoder::decode(uint32_t offsetX, uint32_t offsetY, uint32_t width,
uint32_t height, iPoint2D maxDim_,
bool fixDng16Bug_) {
if (offsetX >= static_cast<unsigned>(mRaw->dim.x))
ThrowRDE("X offset outside of image");
if (offsetY >= static_cast<unsigned>(mRaw->dim.y))
ThrowRDE("Y offset outside of image");
if (width > static_cast<unsigned>(mRaw->dim.x))
ThrowRDE("Tile wider than image");
if (height > static_cast<unsigned>(mRaw->dim.y))
ThrowRDE("Tile taller than image");
if (offsetX + width > static_cast<unsigned>(mRaw->dim.x))
ThrowRDE("Tile overflows image horizontally");
if (offsetY + height > static_cast<unsigned>(mRaw->dim.y))
ThrowRDE("Tile overflows image vertically");
if (width == 0 || height == 0)
return; // We do not need anything from this tile.
if (!maxDim_.hasPositiveArea() ||
implicit_cast<unsigned>(maxDim_.x) < width ||
implicit_cast<unsigned>(maxDim_.y) < height)
ThrowRDE("Requested tile is larger than tile's maximal dimensions");
offX = offsetX;
offY = offsetY;
w = width;
h = height;
maxDim = maxDim_;
fixDng16Bug = fixDng16Bug_;
AbstractLJpegDecoder::decodeSOI();
}
Buffer::size_type LJpegDecoder::decodeScan() {
invariant(frame.cps > 0);
if (predictorMode != 1)
ThrowRDE("Unsupported predictor mode: %u", predictorMode);
for (uint32_t i = 0; i < frame.cps; i++)
if (frame.compInfo[i].superH != 1 || frame.compInfo[i].superV != 1)
ThrowRDE("Unsupported subsampling");
int N_COMP = frame.cps;
std::vector<LJpegDecompressor::PerComponentRecipe> rec;
rec.reserve(N_COMP);
std::generate_n(std::back_inserter(rec), N_COMP,
[&rec, hts = getPrefixCodeDecoders(N_COMP),
initPred = getInitialPredictors(
N_COMP)]() -> LJpegDecompressor::PerComponentRecipe {
const auto i = implicit_cast<int>(rec.size());
return {*hts[i], initPred[i]};
});
const iRectangle2D imgFrame = {
{static_cast<int>(offX), static_cast<int>(offY)},
{static_cast<int>(w), static_cast<int>(h)}};
const auto jpegFrameDim = iPoint2D(frame.w, frame.h);
auto maxRes = iPoint2D(mRaw->getCpp() * maxDim.x, maxDim.y);
if (maxRes.area() != N_COMP * jpegFrameDim.area())
ThrowRDE("LJpeg frame area does not match maximal tile area");
if (maxRes.x % jpegFrameDim.x != 0 || maxRes.y % jpegFrameDim.y != 0)
ThrowRDE("Maximal output tile size is not a multiple of LJpeg frame size");
auto MCUSize = iPoint2D{maxRes.x / jpegFrameDim.x, maxRes.y / jpegFrameDim.y};
if (MCUSize.area() != implicit_cast<uint64_t>(N_COMP))
ThrowRDE("Unexpected MCU size, does not match LJpeg component count");
const LJpegDecompressor::Frame jpegFrame = {MCUSize, jpegFrameDim};
int numLJpegRowsPerRestartInterval;
if (numMCUsPerRestartInterval == 0) {
// Restart interval not enabled, so all of the rows
// are contained in the first (implicit) restart interval.
numLJpegRowsPerRestartInterval = jpegFrameDim.y;
} else {
const int numMCUsPerRow = jpegFrameDim.x;
if (numMCUsPerRestartInterval % numMCUsPerRow != 0)
ThrowRDE("Restart interval is not a multiple of frame row size");
numLJpegRowsPerRestartInterval = numMCUsPerRestartInterval / numMCUsPerRow;
}
LJpegDecompressor d(mRaw, imgFrame, jpegFrame, rec,
numLJpegRowsPerRestartInterval,
input.peekRemainingBuffer().getAsArray1DRef());
return d.decode();
}
} // namespace rawspeed