-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpnm.cpp
264 lines (251 loc) · 7.23 KB
/
pnm.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) Mathieu Malaterre
// SPDX-License-Identifier: BSD-3-Clause
#include "pnm.h"
#include "dest.h"
#include "factory.h"
#include "image.h"
#include "source.h"
#include "utils.h"
#include <limits>
#include <sstream>
#include <vector>
namespace jlst {
bool pnm::handle_type(std::string const& type) const
{
return type == "pam" || type == "pgm" || type == "ppm";
}
bool pnm::detect(source& s, image_info const&) const
{
const int c = s.peek();
return c == 'P';
}
namespace {
// Return 12 for input `4095`
constexpr std::uint32_t log2(const std::uint32_t n) noexcept
{
std::uint32_t x{};
while (n > (1u << x))
{
++x;
}
return x;
}
static inline std::string& pnm_trim_comment(std::string& s) noexcept
{
s.erase(s.begin()); // remove leading '#'
// no need to remove the leading/trailing whitespaces at this point.
return s;
}
// https://stackoverflow.com/questions/7677158/how-to-detect-negative-numbers-as-parsing-errors-when-reading-unsigned-integers
static inline std::uint32_t parse_u32(std::stringstream& ss)
{
// use a signed type:
long long ll;
// `-0` is read as `0` which is acceptable for our use-case
if (ss >> ll)
{
if (ll >= 0 && ll <= std::numeric_limits<uint32_t>::max())
return static_cast<std::uint32_t>(ll);
}
throw std::invalid_argument("parse_u32");
}
} // namespace
void pnm::read_info(source& fs, image& i) const
{
auto& ii = i.get_image_info();
std::string str;
str = fs.getline();
if (str == "P4")
{
throw std::invalid_argument("Single bit is not supported in JPEG-LS");
}
if (str != "P5" && str != "P6" && str != "P7")
{
throw std::invalid_argument(str);
}
bool ispam = false;
// component count:
if (str[1] == '5')
{
ii.frame_info().component_count = 1;
ii.interleave_mode() = charls::interleave_mode::none;
}
else if (str[1] == '6')
{
ii.frame_info().component_count = 3;
ii.interleave_mode() = charls::interleave_mode::sample;
}
else if (str[1] == '7')
{
ispam = true;
}
// multi-line comment:
std::string comment;
while (fs.peek() == '#')
{
str = fs.getline();
if (!comment.empty())
comment += "\n"; // FIXME: UNIX style ?
comment += pnm_trim_comment(str);
}
ii.comment() = comment;
if (ispam)
{
do
{
str = fs.getline();
if (str.rfind("WIDTH", 0) == 0)
{
std::istringstream iss(str);
iss >> str;
long long ll;
iss >> ll;
ii.frame_info().width = ll;
}
else if (str.rfind("HEIGHT", 0) == 0)
{
std::istringstream iss(str);
iss >> str;
long long ll;
iss >> ll;
ii.frame_info().height = ll;
}
else if (str.rfind("MAXVAL", 0) == 0)
{
// bits per sample:
std::stringstream ss(str);
ss >> str;
auto bps = parse_u32(ss);
ii.frame_info().bits_per_sample = log2(bps);
}
else if (str.rfind("TUPLTYPE", 0) == 0)
{
// wotsit ?
}
else if (str.rfind("DEPTH", 0) == 0)
{
std::istringstream iss(str);
iss >> str;
long long ll;
iss >> ll;
if (ll == 1)
{
ii.frame_info().component_count = ll;
ii.interleave_mode() = charls::interleave_mode::none;
}
else if (ll == 3)
{
ii.frame_info().component_count = ll;
ii.interleave_mode() = charls::interleave_mode::sample;
}
else
throw std::invalid_argument(std::to_string(ll));
}
else if (str.rfind("ENDHDR", 0) == 0)
{
}
else
throw std::invalid_argument(str);
} while (str.rfind("ENDHDR", 0) != 0);
}
else
{
// width / height:
str = fs.getline();
{
std::stringstream ss(str);
auto w = parse_u32(ss);
auto h = parse_u32(ss);
ii.frame_info().width = w;
ii.frame_info().height = h;
}
// bits per sample:
str = fs.getline();
{
std::stringstream ss(str);
auto bps = parse_u32(ss);
ii.frame_info().bits_per_sample = log2(bps);
}
}
// now is a good time to compute stride:
auto const bytes_per_sample{(ii.frame_info().bits_per_sample + 7) / 8};
i.get_image_data().stride() = ii.frame_info().width * bytes_per_sample * ii.frame_info().component_count;
}
void pnm::read_data(source& fs, image& img) const
{
auto& id = img.get_image_data();
auto& pd = id.pixel_data();
uint8_t* buf8 = pd.data();
auto len = pd.size();
fs.read(buf8, len);
auto& ii = img.get_image_info();
if (ii.frame_info().bits_per_sample > 8)
{
for (size_t i{}; i < len - 1; i += 2)
{
std::swap(buf8[i], buf8[i + 1]);
}
}
}
void pnm::write_info(dest& d, const image& i, const jls_options&) const
{
std::stringstream fs;
auto& ii = i.get_image_info();
if (ii.frame_info().component_count == 1)
fs << "P5\n";
else
fs << "P6\n";
// write comment if any:
auto& comment = ii.comment();
if (!comment.empty())
{
std::stringstream ss(comment);
std::string com;
while (ss >> com)
{
fs << '#' << com << '\n';
}
}
fs << ii.frame_info().width << " " << ii.frame_info().height << '\n';
fs << ((1 << ii.frame_info().bits_per_sample) - 1) << '\n';
const std::string s = fs.str();
d.write(s.c_str(), s.size());
}
void pnm::write_data(dest& fs, const image& img, jls_options const&) const
{
auto& ii = img.get_image_info();
auto& id = img.get_image_data();
auto& pd = id.pixel_data();
auto len = pd.size();
std::vector<unsigned char> buf8(pd);
auto const bytes_per_sample{(ii.frame_info().bits_per_sample + 7) / 8};
const size_t stride = ii.frame_info().width * bytes_per_sample * ii.frame_info().component_count;
if (ii.frame_info().component_count == 3)
{
if (ii.interleave_mode() == charls::interleave_mode::none)
{
std::vector<unsigned char> copy = utils::planar_to_triplet(buf8, ii.frame_info().width, ii.frame_info().height,
ii.frame_info().bits_per_sample, stride);
buf8.assign(copy.begin(), copy.end());
}
}
if (ii.frame_info().bits_per_sample > 8)
{
for (size_t i{}; i < len - 1; i += 2)
{
std::swap(buf8[i], buf8[i + 1]);
}
}
fs.write(buf8.data(), buf8.size());
}
format* pnm::clone() const
{
return new pnm;
}
static const format* get()
{
static const pnm pnm_;
return &pnm_;
}
static bool b = factory::instance().register_format(get());
} // namespace jlst