-
Notifications
You must be signed in to change notification settings - Fork 2
/
generic_region.c
159 lines (123 loc) · 4.97 KB
/
generic_region.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
#include "doc_coder.h"
#include "dictionary.h"
#include "encode.h"
#include "entropy.h"
#include "mq.h"
void encode_cleanup_image(void);
void refine_encode_original_image(void);
extern void bin_encode_direct(char *, int, int, ARITH_CODED_BITSTREAM *);
extern void bin_encode_refine(char *, int, int, char *, int, int, int, int,
ARITH_CODED_BITSTREAM *);
extern void reset_arith_bitmap_coders(void);
extern void arith_encode_init(void);
extern void arith_encode_flush(ARITH_CODED_BITSTREAM *);
extern int write_coded_bitstream(char *, int);
extern void write_segment_header(SegHeader *);
extern void error(char *);
extern Codec *codec;
extern PixelMap *cleanup;
extern PixelMap *doc_buffer;
extern PixelMap *ori_buffer;
/* Subroutine: void encode_cleanup_image()
Function: encode the cleanup image
Input: none
Output: none
*/
void encode_cleanup_image()
{
SegHeader header;
GenRegionDataHeader data_header;
ARITH_CODED_BITSTREAM bitstr;
int out_bits;
int ori_file; /* file size before this segment is sent */
extern void write_gen_reg_seg_header(GenRegionDataHeader *);
bitstr.max_buffer_size = ((cleanup->width*cleanup->height)>>3)>>3;
bitstr.data = (char *)malloc(sizeof(char)*bitstr.max_buffer_size);
if(!bitstr.data)
error("encode_cleanup_image: cannot allocate memory\n");
ori_file = ftell(codec->fp_out);
reset_arith_bitmap_coders();
arith_encode_init();
bin_encode_direct(cleanup->data, cleanup->width, cleanup->height, &bitstr);
codec->report.uncoded_cleanup_bits = bitstr.uncoded_size;
arith_encode_flush(&bitstr);
/* write generic region segment header */
header.type = IM_GEN_REG;
header.retain_this = FALSE;
header.ref_seg_count = 0;
header.page_asso = codec->cur_page+1;
/* we know the generic region segment data header is 20 bytes long:
1. 17 bytes for region segment information field, and
2. 1 byte for generic region segment flags, and
3. 2 bytes for adaptive template pixel info when template = 2 */
header.seg_length = 20;
header.seg_length += bitstr.coded_size >> 3;
write_segment_header(&header);
/* write generic region segment data header */
data_header.reg_info.width = doc_buffer->width;
data_header.reg_info.height = doc_buffer->height;
data_header.reg_info.locx = 0;
data_header.reg_info.locy = doc_buffer->top_y;
data_header.reg_info.excombop = JB2_OR;
data_header.tpdon = FALSE; data_header.rtemplate = 2;
data_header.mmr = FALSE;
data_header.atx[0] = 2; data_header.aty[0] = -1;
write_gen_reg_seg_header(&data_header);
/* write generic region segment data */
out_bits = write_coded_bitstream(bitstr.data, bitstr.coded_size);
codec->report.cleanup_bits = out_bits;
free((void *)bitstr.data);
codec->report.gen_region_size = ftell(codec->fp_out) - ori_file;
}
/* Subroutine: void refine_encode_original_image()
Function: encode the original image with respect to a blank image, for
test purposes ONLY
Input: none
Output: none
*/
void refine_encode_original_image()
{
SegHeader header;
GenRefRegionDataHeader data_header;
ARITH_CODED_BITSTREAM bitstr;
int out_bits;
int ori_file; /* file size before this segment is sent */
extern void write_gen_ref_reg_seg_header(GenRefRegionDataHeader *);
bitstr.max_buffer_size = ((cleanup->width*cleanup->height)>>3)>>3;
bitstr.data = (char *)malloc(sizeof(char)*bitstr.max_buffer_size);
if(!bitstr.data)
error("refine_encode_original_image: cannot allocate memory\n");
ori_file = ftell(codec->fp_out);
reset_arith_bitmap_coders();
arith_encode_init();
bin_encode_refine(cleanup->data, cleanup->width, cleanup->height,
ori_buffer->data, ori_buffer->width, ori_buffer->height,
0, 0, &bitstr);
codec->report.uncoded_residue_bits = bitstr.uncoded_size;
arith_encode_flush(&bitstr);
/* write generic refinement segment header */
header.type = IM_GEN_REF_REG;
header.retain_this = FALSE;
header.ref_seg_count = 0;
header.page_asso = codec->cur_page+1;
/* the generic refinement region segment data header is 18 bytes long:
1. 17 bytes for region segment information field, and
2. 1 byte for generic region segment flags, and
3. 0 bytes for adaptive template pixel info when template = 1 */
header.seg_length = 18;
header.seg_length += bitstr.coded_size >> 3;
write_segment_header(&header);
/* write generic refinement region segment data header */
data_header.reg_info.width = doc_buffer->width;
data_header.reg_info.height = doc_buffer->height;
data_header.reg_info.locx = 0;
data_header.reg_info.locy = doc_buffer->top_y;
data_header.reg_info.excombop = JB2_OR;
data_header.tpdon = FALSE; data_header.rtemplate = 1;
write_gen_ref_reg_seg_header(&data_header);
/* write generic refinement region segment data */
out_bits = write_coded_bitstream(bitstr.data, bitstr.coded_size);
codec->report.residue_bits = out_bits;
free((void *)bitstr.data);
codec->report.gen_ref_region_size = ftell(codec->fp_out) - ori_file;
}