Debian Codegraph @

src/jb2/jb2coder.cpp

raw
1 /*
2 * jb2coder.cpp - coding character positions and JB2 records
3 */
4
5 #include "../base/mdjvucfg.h"
6 #include "jb2coder.h"
7
8 // JB2Coder implementation {{{
9
10 JB2Coder::JB2Coder() :
11 watcher(),
12 image_size(0, jb2_big_positive_number, &watcher),
13 matching_symbol_index(0, 0, &watcher),// changed on the fly
14 symbol_column_number(0, 0, &watcher), // changed after start_of_image
15 symbol_row_number(0, 0, &watcher), // changed after start_of_image
16 same_line_column_offset
17 (jb2_big_negative_number, jb2_big_positive_number, &watcher),
18 same_line_row_offset
19 (jb2_big_negative_number, jb2_big_positive_number, &watcher),
20 new_line_column_offset
21 (jb2_big_negative_number, jb2_big_positive_number, &watcher),
22 new_line_row_offset
23 (jb2_big_negative_number, jb2_big_positive_number, &watcher),
24 comment_length(0, jb2_big_positive_number, &watcher),
25 comment_octet(0, 255, &watcher),
26 required_dictionary_size(0, jb2_big_positive_number, &watcher),
27 record_type(0, 11, &watcher),
28 first(-1, 0, 0, 1),
29 line_counter(0)
30 {
31 }
32
33 JB2Coder::~JB2Coder()
34 {
35 }
36
37 void JB2Coder::reset_numcontexts()
38 {
39 record_type.reset();
40 image_size.reset();
41 matching_symbol_index.reset();
42 symbol_column_number.reset();
43 symbol_row_number.reset();
44 same_line_column_offset.reset();
45 same_line_row_offset.reset();
46 new_line_column_offset.reset();
47 new_line_row_offset.reset();
48 comment_length.reset();
49 comment_octet.reset();
50 required_dictionary_size.reset();
51 }
52
53 // JB2Coder implementation }}}
54
55 JB2Decoder::JB2Decoder(FILE *f, int32 length)
56 : JB2BitmapDecoder(zp), zp(f, length) {}
57 JB2Encoder::JB2Encoder(FILE *f)
58 : JB2BitmapEncoder(zp), zp(f), no_symbols_yet(true) {}
59
60 // Coding character positions {{{
61
62 void JB2Decoder::decode_character_position(int32 &x, int32 &y, int32 w, int32 h)/*{{{*/
63 {
64 // implements section 8.5.8 (page 31) of DjVu 2 specs
65 // FIXME: too much code is duplicated with encode_character_position()
66
67 if (zp.decode(offset_type))
68 {
69 // new line
70
71 x = first.left + zp.decode(new_line_column_offset);
72 int dy = zp.decode(new_line_row_offset);
73 y = first.top + first.height - 1 - dy; // their offset is up, not down
74
75 // start a new line with the current symbol
76 line_counter = 1;
77 first.left = x;
78 first.top = y;
79 first.width = w;
80 first.height = h;
81 prev1 = first;
82 }
83 else
84 {
85 x = prev1.left + prev1.width - 1 + zp.decode(same_line_column_offset);
86 int baseline;
87
88 if (line_counter < 3)
89 {
90 baseline = first.top + first.height;
91 }
92 else
93 {
94 int b1 = prev1.top + prev1.height;
95 int b2 = prev2.top + prev2.height;
96 int b3 = prev3.top + prev3.height;
97
98 // baseline is the median of b1, b2 and b3
99 if (b1 > b2) {int t = b1; b1 = b2; b2 = t;}
100 if (b1 > b3) {int t = b1; b1 = b3; b3 = t;}
101 if (b2 > b3) {int t = b2; b2 = b3; b3 = t;}
102
103 baseline = b2;
104 }
105
106 y = baseline - h - zp.decode(same_line_row_offset);
107
108 // continue the current line with the current symbol
109 line_counter++;
110 prev3 = prev2;
111 prev2 = prev1;
112 prev1.left = x;
113 prev1.top = y;
114 prev1.width = w;
115 prev1.height = h;
116 }
117 }/*}}}*/
118 void JB2Encoder::encode_character_position(int x, int y, int w, int h)/*{{{*/
119 {
120 // implements section 8.5.8 (page 31) of DjVu 2 specs
121 // FIXME: too much code is duplicated with decode_character_position()
122
123 int new_line;
124 if (no_symbols_yet)
125 {
126 no_symbols_yet = false;
127 new_line = true;
128 }
129 else
130 {
131 new_line = x < prev1.left; // taken from DjVuLibre
132 }
133
134 zp.encode(new_line ? 1 : 0, offset_type);
135 if (new_line)
136 {
137 // new line
138
139 zp.encode(x - first.left, new_line_column_offset);
140 zp.encode(first.top + first.height - 1 - y, new_line_row_offset);
141
142 // start a new line with the current symbol
143 line_counter = 1;
144 first.left = x;
145 first.top = y;
146 first.width = w;
147 first.height = h;
148 prev1 = first;
149 }
150 else
151 {
152 zp.encode(x - prev1.left - prev1.width + 1, same_line_column_offset);
153 int baseline;
154
155 if (line_counter < 3)
156 {
157 baseline = first.top + first.height;
158 }
159 else
160 {
161 int b1 = prev1.top + prev1.height;
162 int b2 = prev2.top + prev2.height;
163 int b3 = prev3.top + prev3.height;
164
165 // baseline is the median of b1, b2 and b3
166 if (b1 > b2) {int t = b1; b1 = b2; b2 = t;}
167 if (b1 > b3) {int t = b1; b1 = b3; b3 = t;}
168 if (b2 > b3) {int t = b2; b2 = b3; b3 = t;}
169
170 baseline = b2;
171 }
172
173 zp.encode(baseline - h - y, same_line_row_offset);
174
175 // continue the current line with the current symbol
176 line_counter++;
177 prev3 = prev2;
178 prev2 = prev1;
179 prev1.left = x;
180 prev1.top = y;
181 prev1.width = w;
182 prev1.height = h;
183 }
184 }/*}}}*/
185
186 // Coding character positions }}}
187
188 int32 JB2Decoder::decode_blit(mdjvu_image_t img, int32 shape_index)
189 {
190 mdjvu_bitmap_t shape = mdjvu_image_get_bitmap(img, shape_index);
191 int32 w = mdjvu_bitmap_get_width(shape);
192 int32 h = mdjvu_bitmap_get_height(shape);
193 int32 x, y;
194 decode_character_position(x, y, w, h);
195 return mdjvu_image_add_blit(img, x, y, shape);
196 }
197
198 void JB2Encoder::encode_blit(mdjvu_image_t img, int32 blit, int32 w, int32 h)
199 {
200 int32 x = mdjvu_image_get_blit_x(img, blit);
201 int32 y = mdjvu_image_get_blit_y(img, blit);
202 encode_character_position(x, y, w, h);
203 }
204
205 void JB2Encoder::open_record(JB2RecordType type)
206 {
207 zp.encode(type, record_type);
208 }
209
210 void JB2Encoder::close_record()
211 {
212 if (watcher.count > JB2_NUMBER_CONTEXTS_MEMORY_BOUND)
213 {
214 zp.encode(jb2_require_dictionary_or_reset, record_type);
215 JB2Coder::reset_numcontexts();
216 JB2BitmapEncoder::reset_numcontexts();
217 watcher.reset();
218 }
219 }
220
221 void JB2Decoder::reset()
222 {
223 JB2Coder::reset_numcontexts();
224 JB2BitmapDecoder::reset_numcontexts();
225 }
226
227 JB2RecordType JB2Decoder::decode_record_type()
228 {
229 return (JB2RecordType) zp.decode(record_type);
230 }