-
Notifications
You must be signed in to change notification settings - Fork 2
/
simpledjvu.cpp
268 lines (239 loc) · 7.91 KB
/
simpledjvu.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
265
266
267
268
/*
* Simpledjvu-0.1
* Based on djvulibre (http://djvu.sourceforge.net/)
* Copyright 2012, Mikhail Dektyarev <[email protected]>
*
* This file is part of Simpledjvu.
*
* Simpledjvu is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Simpledjvu 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Simpledjvu. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <djvulibre.h>
#include <normalize.h>
#include <pgm2jb2.h>
#include <vector>
#include <array>
#include <string>
#include <iostream>
#include <cstdlib>
using std::vector;
using std::array;
using std::string;
const byte THRESHOLD_LEVEL = 128;
struct Keys {
bool include_bg;
bool include_fg;
int mask_mul;
bool use_normalized;
int normalize_iters;
int threshold_level;
int cjb2_loss_level;
vector<int> slices_bg;
vector<int> slices_fg;
Keys():
include_bg(true),
include_fg(true),
mask_mul(2),
use_normalized(false),
normalize_iters(200),
threshold_level(128),
cjb2_loss_level(1),
slices_bg({74, 89, 99}),
slices_fg({89}) {
}
};
/*
* it should be const GBitmap, but for unknown reason GBitmap::save_pgm is not declared as const
*/
void save(const GP<GBitmap> gimage, const char *fname, bool pgm = true) {
if (pgm) {
gimage->save_pgm(*ByteStream::create(GURL::Filename::UTF8(fname), "wb"));
}
else {
gimage->save_pbm(*ByteStream::create(GURL::Filename::UTF8(fname), "wb"));
}
}
enum Chunk { BACKGROUND, FOREGROUND};
GP<GBitmap> make_chunk_mask(const GBitmap &mask, Chunk chunk) {
GP<GBitmap> result = GBitmap::create(mask.rows(), mask.columns());
result->set_grays(2);
int ok_color = chunk == BACKGROUND;
for (int i = 0; i < mask.rows(); ++i) {
for (int j = 0; j < mask.columns(); ++j) {
(*result)[i][j] = mask[i][j] == ok_color ||
(i > 0 && mask[i-1][j] == ok_color) ||
(i < mask.rows() - 1 && mask[i+1][j] == ok_color) ||
(j > 0 && mask[i][j-1] == ok_color) ||
(j < mask.columns() - 1 && mask[i][j+1] == ok_color);
}
}
return result;
}
void print_help() {
std::cerr
<< "Usage: simpledjvu [options] input.pgm output.djvu\n"
<< "where options =\n"
<< "\t-nobg\n"
<< "\t-nofg\n"
<< "\t-mask_mul n\n"
<< "\t-use_normalized\n"
<< "\t-normalize_iters n\n"
<< "\t-threshold_level n\n"
<< "\t-cjb2_loss_level n\n"
<< "\t-slices_bg n1,n2,..\n"
<< "\t-slices_fg n1,n2,...\n"
;
}
bool parse_keys(int argc, char *argv[], Keys *keys, char **input, char **output) {
if (argc <= 2) {
std::cerr << "Not enough arguments\n";
return false;
}
(*input) = argv[argc - 2];
(*output) = argv[argc - 1];
for (int i = 1; i < argc - 2; ++i) {
if (argv[i][0] != '-') {
std::cerr << "Wrong option format: " << argv[i] << '\n';
return false;
}
string arg = argv[i];
if (arg == "-nobg") {
keys->include_bg = false;
}
else if (arg == "-nofg") {
keys->include_fg = false;
}
else if (arg == "-use_normalized") {
keys->use_normalized = true;
}
else if (arg == "-mask_mul" || arg == "-normalize_iters" || arg == "-threshold_level" || arg == "-cjb2_loss_level") {
++i;
int n;
char *endptr;
n = strtol(argv[i], &endptr, 10);
if (*endptr) {
std::cerr << "Bad number: " << argv[i] << '\n';
return false;
}
if (arg == "-mask_mul") {
keys->mask_mul = n;
}
else if (arg == "-normalize_iters") {
keys->normalize_iters = n;
}
else if (arg == "-threshold_level") {
keys->threshold_level = n;
}
else if (arg == "-cjb2_loss_level") {
keys->cjb2_loss_level = n;
}
}
else if (arg == "-slices_bg" || arg == "-slices_fg") {
++i;
char *nptr = argv[i], *endptr;
vector<int> ns;
while (*nptr) {
int n = strtol(nptr, &endptr, 10);
if (endptr == nptr || *endptr != ',' && *endptr != 0) {
std::cerr << "Bad numbers: " << argv[i] << '\n';
return false;
}
ns.push_back(n);
nptr = *endptr ? endptr + 1 : endptr;
}
if (arg == "-slices_bg") {
keys->slices_bg = ns;
}
else if (arg == "-slices_fg") {
keys->slices_fg = ns;
}
}
else {
std::cerr << "Unknown option: " << argv[i] << '\n';
return false;
}
}
return true;
}
/*
* random parts from c44 tool source code
* random mix of references and pointers, just like in main djvulibre code
*
* @todo: understand, how does it work
*/
void write_part_to_djvu(const GBitmap &image, const vector<int> &slices, const GP<GBitmap> &gmask, IFFByteStream &iff, Chunk chunk) {
GP<IW44Image> iw = IW44Image::create_encode(image, gmask);
vector<IWEncoderParms> parms(slices.size());
for (int i = 0; i < parms.size(); ++i) {
parms[i].slices = slices[i];
// is it necessary?
parms[i].bytes = 0;
parms[i].decibels = 0;
}
for (const auto& parm : parms) {
if (chunk == BACKGROUND) {
iff.put_chunk("BG44");
}
else {
iff.put_chunk("FG44");
}
iw->encode_chunk(iff.get_bytestream(), parm);
iff.close_chunk();
}
}
int main(int argc, char *argv[]) {
Keys keys;
char *input, *output;
if (!parse_keys(argc, argv, &keys, &input, &output)) {
print_help();
return 1;
}
GP<GBitmap> gimage = GBitmap::create(*ByteStream::create(GURL::Filename::UTF8(input), "rb"));
GP<GBitmap> gnormalized_small = get_norm_image(*gimage, keys.normalize_iters);
GP<GBitmap> gnormalized = GBitmap::create(gimage->rows() * keys.mask_mul, gimage->columns() * keys.mask_mul);
rescale_bitmap(*gnormalized_small, *gnormalized);
gnormalized->binarize_grays(keys.threshold_level);
GP<JB2Image> gmask = pbm2jb2(gnormalized, keys.cjb2_loss_level);
/*
* this code is based on djvumake and c44 tools source
*/
GP<IFFByteStream> giff = IFFByteStream::create(ByteStream::create(GURL::Filename::UTF8(output), "wb"));
IFFByteStream &iff = *giff;
iff.put_chunk("FORM:DJVU", 1);
GP<DjVuInfo> ginfo = DjVuInfo::create();
ginfo->width = gmask->get_width();
ginfo->height = gmask->get_height();
ginfo->dpi = 300;
iff.put_chunk("INFO");
ginfo->encode(*iff.get_bytestream());
iff.close_chunk();
iff.put_chunk("Sjbz");
gmask->encode(iff.get_bytestream());
iff.close_chunk();
gnormalized_small->binarize_grays(THRESHOLD_LEVEL);
GP<GBitmap> gbetter_image;
if (keys.use_normalized) {
gbetter_image = get_norm_image(*gimage, 2);
}
else {
gbetter_image = gimage;
}
if (keys.include_fg) {
write_part_to_djvu(*gbetter_image, keys.slices_fg, make_chunk_mask(*gnormalized_small, FOREGROUND), iff, FOREGROUND);
}
if (keys.include_bg) {
write_part_to_djvu(*gbetter_image, keys.slices_bg, make_chunk_mask(*gnormalized_small, BACKGROUND), iff, BACKGROUND);
}
return 0;
}