forked from google/knusperli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpeg_data_decoder.cc
56 lines (50 loc) · 1.72 KB
/
jpeg_data_decoder.cc
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
/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jpeg_data_decoder.h"
#include "output_image.h"
namespace knusperli {
// Mimic libjpeg's heuristics to guess jpeg color space.
// Requires that the jpg has 3 components.
bool HasYCbCrColorSpace(const JPEGData& jpg) {
bool has_Adobe_marker = false;
uint8_t Adobe_transform = 0;
for (const std::string& app : jpg.app_data) {
if (static_cast<uint8_t>(app[0]) == 0xe0) {
return true;
} else if (static_cast<uint8_t>(app[0]) == 0xee && app.size() >= 15) {
has_Adobe_marker = true;
Adobe_transform = app[14];
}
}
if (has_Adobe_marker) {
return (Adobe_transform != 0);
}
const int cid0 = jpg.components[0].id;
const int cid1 = jpg.components[1].id;
const int cid2 = jpg.components[2].id;
return (cid0 != 'R' || cid1 != 'G' || cid2 != 'B');
}
std::vector<uint8_t> DecodeJpegToRGB(const JPEGData& jpg) {
if (jpg.components.size() == 1 ||
(jpg.components.size() == 3 &&
HasYCbCrColorSpace(jpg) && (jpg.Is420() || jpg.Is444()))) {
OutputImage img(jpg.width, jpg.height);
img.CopyFromJpegData(jpg);
return img.ToSRGB();
}
return std::vector<uint8_t>();
}
} // namespace knusperli