-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
282 lines (235 loc) · 8.35 KB
/
main.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Copyright 2004 The Regents of the University of California */
/* All Rights Reserved */
/* Permission to copy, modify and distribute any part of this JBIG2 codec for */
/* educational, research and non-profit purposes, without fee, and without a */
/* written agreement is hereby granted, provided that the above copyright */
/* notice, this paragraph and the following three paragraphs appear in all */
/* copies. */
/* Those desiring to incorporate this JBIG2 codec into commercial products */
/* or use for commercial purposes should contact the Technology Transfer */
/* Office, University of California, San Diego, 9500 Gilman Drive, Mail Code */
/* 0910, La Jolla, CA 92093-0910, Ph: (858) 534-5815, FAX: (858) 534-7345, */
/* E-MAIL:[email protected]. */
/* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR */
/* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING */
/* LOST PROFITS, ARISING OUT OF THE USE OF THIS JBIG2 CODEC, EVEN IF THE */
/* UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* THE JBIG2 CODEC PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE */
/* UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, */
/* UPDATES, ENHANCEMENTS, OR MODIFICATIONS. THE UNIVERSITY OF CALIFORNIA MAKES */
/* NO REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER IMPLIED OR */
/* EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF THE */
/* JBIG2 CODEC WILL NOT INFRINGE ANY PATENT, TRADEMARK OR OTHER RIGHTS. */
#include "doc_coder.h"
#include "entropy.h"
#include "mq.h"
Codec *codec;
PixelMap *doc_buffer;
PixelMap *ori_buffer;
Page pages[MAX_PAGE_NUM];
MarkList *all_marks;
WordList *all_words;
Mark *all_word_marks;
PixelMap *cleanup;
ARENC_STATE *mq_coder;
extern void parse_command_line(int, char **);
extern void read_pbm_file_header(void);
extern void read_in_doc_buffer(void);
extern int doc_buffer_blank(void);
extern void free_buffer_memory(PixelMap *);
extern void process_buffer(void);
extern void init_cleanup_image(void);
extern void reset_cleanup_image(void);
extern void init_dictionary(void);
extern void free_dictionary(void);
extern void make_mark_dictionary(void);
extern void update_mark_dictionary(void);
extern void opt_mark_dictionary_mixed(void);
extern void opt_mark_dictionary_tree(void);
extern void opt_mark_dictionary_class(void);
extern void make_mark_dictionary_pms(void);
extern void make_coding_strips(void);
extern void encode_dictionary(void);
extern void encode_coding_strips(void);
extern void encode_cleanup_image(void);
extern void free_coding_strips(void);
extern void free_marks_not_needed(void);
extern void save_doc_buffer(void);
extern void reconstruct_image(void);
extern void refine_encode_original_image(void);
extern void create_arith_coders(void);
extern void destroy_arith_coders(void);
extern void write_JBIG2_header(void);
extern void print_compression_param(void);
extern void print_compression_report(int, int);
extern void print_overall_compression_report(void);
extern void print_detailed_compression_report(int, int);
extern void reset_codec_report(void);
extern void error(char *);
extern void write_dictionary(void);
extern void write_all_marks(void);
extern void write_regions(void);
extern void write_cleanup_image(void);
extern void encode_page_info(void);
extern void encode_end_of_page(void);
extern void encode_end_of_file(void);
extern void encode_end_of_stripe(int);
extern void edge_smoothing();
int main(int argc, char **argv)
{
register int i, j;
char fn[1000], first[500], second[500], count[100];
int new_dict;
parse_command_line(argc, argv);
print_compression_param();
/* if multi-page, cut the file name into parts */
if(codec->page_num > 1) {
strcpy(fn, codec->report.file_header);
for(i = 0; i < strlen(fn); i++)
if(fn[i] >= '0' && fn[i] <= '9') break;
strncpy(first, fn, i);
first[i] = '\0';
for(j = i; j < strlen(fn) && fn[j] >= '0' && fn[j] <= '9'; j++);
strncpy(count, fn+i, j-i);
count[j-i] = '\0';
strcpy(second, fn+j);
}
/* if single-page, just copy the file name */
else strcpy(first, codec->report.file_header);
/* input image buffer */
doc_buffer = (PixelMap *)malloc(sizeof(PixelMap));
if(!doc_buffer)
error("main: Cannot allocate memory\n");
doc_buffer->top_y = 0;
/* cleanup image buffer */
cleanup = (PixelMap *)malloc(sizeof(PixelMap));
if(!cleanup)
error("main: Cannot allocate memory\n");
/* one extra buffer is needed if residue_coding is turned ON */
if(codec->residue_coding) {
ori_buffer = (PixelMap *)malloc(sizeof(PixelMap));
if(!ori_buffer)
error("main: Cannot allocate memory\n");
}
/* compressed file */
sprintf(fn, "%s%s.jb2", codec->report.data_path, first);
codec->fp_out = fopen(fn, "wb");
if(!codec->fp_out) error("cannot open binary file for writing\n");
/* coding structures */
create_arith_coders();
init_dictionary();
write_JBIG2_header();
codec->cur_seg = 0;
new_dict = TRUE; codec->report.overall_bits = 0;
for(i = 0; i < codec->page_num; i++) {
codec->cur_page = i;
/* read in and process the current page */
if(codec->page_num > 1)
sprintf(fn, "%s%s%d%s.pbm", codec->report.data_path,
first, atoi(count)+i, second);
else sprintf(fn, "%s%s.pbm", codec->report.data_path, first);
codec->fp_in = fopen(fn, "rb");
if(!codec->fp_in) error("Cannot open document file for reading\n");
read_pbm_file_header();
encode_page_info();
doc_buffer->top_y = 0;
for(j = 0; j < codec->split_num; j++) {
codec->cur_split = j;
read_in_doc_buffer();
if(codec->residue_coding) save_doc_buffer();
if(codec->lossy && !codec->pms) edge_smoothing();
all_marks = (MarkList *)malloc(sizeof(MarkList));
if(!all_marks)
error("main: Cannot allocate memory for all_marks\n");
all_marks->mark_num = 0;
pages[i*codec->split_num+j].all_marks = all_marks;
init_cleanup_image();
process_buffer();
free_buffer_memory(doc_buffer);
reset_codec_report();
codec->report.total_marks = all_marks->mark_num;
if(!codec->no_update) {
switch(codec->dict_type) {
case SE:
case OP:
make_mark_dictionary();
break;
case MIXED:
opt_mark_dictionary_mixed();
break;
case CLASS:
opt_mark_dictionary_class();
break;
case TREE:
opt_mark_dictionary_tree();
break;
case PMS_DICT:
make_mark_dictionary_pms();
break;
default:
error("Unknown dictionary type, aborting...\n");
break;
}
encode_dictionary();
if(!new_dict) update_mark_dictionary();
new_dict = FALSE;
}
else {
if(new_dict) {
switch(codec->dict_type) {
case SE:
case OP:
make_mark_dictionary();
break;
case MIXED:
opt_mark_dictionary_mixed();
break;
case CLASS:
opt_mark_dictionary_class();
break;
case TREE:
opt_mark_dictionary_tree();
break;
case PMS_DICT:
make_mark_dictionary_pms();
break;
default:
error("Unknown dictionary type, aborting...\n");
break;
}
encode_dictionary();
new_dict = FALSE;
}
}
make_coding_strips();
encode_coding_strips();
encode_cleanup_image();
if(codec->residue_coding) {
reconstruct_image();
refine_encode_original_image();
free((void *)ori_buffer->data);
}
free_coding_strips();
free_marks_not_needed();
free((void *)cleanup->data);
if(j < codec->split_num-1)
encode_end_of_stripe(doc_buffer->height+doc_buffer->top_y-1);
if(!codec->silent) {
print_compression_report(i, j);
if(codec->detail) print_detailed_compression_report(i, j);
}
doc_buffer->top_y += doc_buffer->height;
}
encode_end_of_page();
/* close the input file */
fclose(codec->fp_in);
}
encode_end_of_file();
print_overall_compression_report();
destroy_arith_coders();
free((void *)doc_buffer);
free((void *)cleanup);
fclose(codec->fp_out);
return 0;
}