w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

jbig2arith.cc
Go to the documentation of this file.
1 // Copyright 2006 Google Inc. All Rights Reserved.
2 // Author: agl@imperialviolet.org (Adam Langley)
3 //
4 // Copyright (C) 2006 Google Inc.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 
18 #include "jbig2arith.h"
19 #include <string.h>
20 #include <stdio.h>
21 #include <sys/time.h>
22 
23 #define u64 uint64_t
24 #define u32 uint32_t
25 #define u16 uint16_t
26 #define u8 uint8_t
27 
28 // C++ doesn't have C99 restricted pointers, but GCC does allow __restrict__
29 #define restrict __restrict__
30 
31 // -----------------------------------------------------------------------------
32 // This the structure for a single state of the adaptive arithmetic compressor
33 // -----------------------------------------------------------------------------
34 struct context {
37 } __attribute__ ((packed));
38 
39 
40 // -----------------------------------------------------------------------------
41 // And this is the table of states for that adaptive compressor
42 // -----------------------------------------------------------------------------
43 struct context ctbl[] = {
44  // This is the standard state table from
45  // Table E.1 of the standard. The switch has been omitted and
46  // those states are included below
47 #define STATETABLE \
48  {0x5601, F( 1), SWITCH(F( 1))},\
49  {0x3401, F( 2), F( 6)},\
50  {0x1801, F( 3), F( 9)},\
51  {0x0ac1, F( 4), F(12)},\
52  {0x0521, F( 5), F(29)},\
53  {0x0221, F(38), F(33)},\
54  {0x5601, F( 7), SWITCH(F( 6))},\
55  {0x5401, F( 8), F(14)},\
56  {0x4801, F( 9), F(14)},\
57  {0x3801, F(10), F(14)},\
58  {0x3001, F(11), F(17)},\
59  {0x2401, F(12), F(18)},\
60  {0x1c01, F(13), F(20)},\
61  {0x1601, F(29), F(21)},\
62  {0x5601, F(15), SWITCH(F(14))},\
63  {0x5401, F(16), F(14)},\
64  {0x5101, F(17), F(15)},\
65  {0x4801, F(18), F(16)},\
66  {0x3801, F(19), F(17)},\
67  {0x3401, F(20), F(18)},\
68  {0x3001, F(21), F(19)},\
69  {0x2801, F(22), F(19)},\
70  {0x2401, F(23), F(20)},\
71  {0x2201, F(24), F(21)},\
72  {0x1c01, F(25), F(22)},\
73  {0x1801, F(26), F(23)},\
74  {0x1601, F(27), F(24)},\
75  {0x1401, F(28), F(25)},\
76  {0x1201, F(29), F(26)},\
77  {0x1101, F(30), F(27)},\
78  {0x0ac1, F(31), F(28)},\
79  {0x09c1, F(32), F(29)},\
80  {0x08a1, F(33), F(30)},\
81  {0x0521, F(34), F(31)},\
82  {0x0441, F(35), F(32)},\
83  {0x02a1, F(36), F(33)},\
84  {0x0221, F(37), F(34)},\
85  {0x0141, F(38), F(35)},\
86  {0x0111, F(39), F(36)},\
87  {0x0085, F(40), F(37)},\
88  {0x0049, F(41), F(38)},\
89  {0x0025, F(42), F(39)},\
90  {0x0015, F(43), F(40)},\
91  {0x0009, F(44), F(41)},\
92  {0x0005, F(45), F(42)},\
93  {0x0001, F(45), F(43)},
94 #undef F
95 #define F(x) x
96 #define SWITCH(x) (x + 46)
98 #undef SWITCH
99 #undef F
100 
101 #define F(x) (x + 46)
102 #define SWITCH(x) ((x) - 46)
103  STATETABLE
104 #undef SWITCH
105 #undef F
106 };
107 
108 //#define BRANCH_OPT
109 
110 // GCC peephole optimisations
111 #ifdef BRANCH_OPT
112 #define likely(x) __builtin_expect((x),1)
113 #define unlikely(x) __builtin_expect((x),0)
114 #else
115 #define likely(x) x
116 #define unlikely(x) x
117 #endif
118 
119 // see comments in .h file
120 void
122  memset(ctx->context, 0, JBIG2_MAX_CTX);
123  memset(ctx->intctx, 0, 13 * 512);
124  ctx->a = 0x8000;
125  ctx->c = 0;
126  ctx->ct = 12;
127  ctx->bp = -1;
128  ctx->b = 0;
129  ctx->outbuf_used = 0;
131  ctx->output_chunks = new std::vector<uint8_t *>;
132  ctx->iaidctx = NULL;
133 }
134 
135 // see comments in .h file
136 void
138  ctx->a = 0x8000;
139  ctx->c = 0;
140  ctx->ct = 12;
141  ctx->bp = -1;
142  ctx->b = 0;
143  free(ctx->iaidctx);
144  ctx->iaidctx = NULL;
145  memset(ctx->context, 0, JBIG2_MAX_CTX);
146  memset(ctx->intctx, 0, 13 * 512);
147 }
148 
149 // see comments in .h file
150 void
152  ctx->outbuf_used = 0;
153 
154  for (std::vector<uint8_t *>::iterator i = ctx->output_chunks->begin();
155  i != ctx->output_chunks->end(); ++i) {
156  free(*i);
157  }
158  ctx->output_chunks->clear();
159  ctx->bp = -1;
160 }
161 
162 // see comments in .h file
163 void
165  for (std::vector<uint8_t *>::iterator i = ctx->output_chunks->begin();
166  i != ctx->output_chunks->end(); ++i) {
167  free(*i);
168  }
169  delete ctx->output_chunks;
170  free(ctx->outbuf);
171  free(ctx->iaidctx);
172 }
173 
174 // -----------------------------------------------------------------------------
175 // Emit a byte from the compressor by appending to the current output buffer.
176 // If the buffer is full, allocate a new one
177 // -----------------------------------------------------------------------------
178 static void inline
179 emit(struct jbig2enc_ctx *restrict ctx) {
180  if (unlikely(ctx->outbuf_used == JBIG2_OUTPUTBUFFER_SIZE)) {
181  ctx->output_chunks->push_back(ctx->outbuf);
182  ctx->outbuf = (u8 *) malloc(JBIG2_OUTPUTBUFFER_SIZE);
183  ctx->outbuf_used = 0;
184  }
185 
186  ctx->outbuf[ctx->outbuf_used++] = ctx->b;
187 }
188 
189 // -----------------------------------------------------------------------------
190 // The BYTEOUT procedure from the standard
191 // -----------------------------------------------------------------------------
192 static void
194  if (ctx->b == 0xff) goto rblock;
195 
196  if (ctx->c < 0x8000000) goto lblock;
197  ctx->b += 1;
198  if (ctx->b != 0xff) goto lblock;
199  ctx->c &= 0x7ffffff;
200 
201 rblock:
202  if (ctx->bp >= 0) {
203 #ifdef TRACE
204  printf("emit %x\n", ctx->b);
205 #endif
206  emit(ctx);
207  }
208  ctx->b = ctx->c >> 20;
209  ctx->bp++;
210  ctx->c &= 0xfffff;
211  ctx->ct = 7;
212  return;
213 
214 lblock:
215  if (ctx->bp >= 0) {
216 #ifdef TRACE
217  printf("emit %x\n", ctx->b);
218 #endif
219  emit(ctx);
220  }
221  ctx->b = ctx->c >> 19;
222  ctx->bp++;
223  ctx->c &= 0x7ffff;
224  ctx->ct = 8;
225  return;
226 }
227 
228 // -----------------------------------------------------------------------------
229 // A merging of the ENCODE, CODELPS and CODEMPS procedures from the standard
230 // -----------------------------------------------------------------------------
231 static void
233  const u8 i = context[ctxnum];
234  const u8 mps = i > 46 ? 1 : 0;
235  const u16 qe = ctbl[i].qe;
236 
237 #ifdef CODER_DEBUGGING
238  fprintf(stderr, "B: %d %d %d %d\n", ctxnum, qe, ctx->a, d);
239 #endif
240 
241 #ifdef TRACE
242  static int ec = 0;
243  printf("%d\t%d %d %x %x %x %d %x %d\n", ec++, i, mps, qe, ctx->a, ctx->c, ctx->ct, ctx->b, ctx->bp);
244 #endif
245 
246  if (unlikely(d != mps)) goto codelps;
247  ctx->a -= qe;
248  if (unlikely((ctx->a & 0x8000) == 0)) {
249  if (unlikely(ctx->a < qe)) {
250  ctx->a = qe;
251  } else {
252  ctx->c += qe;
253  }
254  context[ctxnum] = ctbl[i].mps;
255  goto renorme;
256  } else {
257  ctx->c += qe;
258  }
259 
260  return;
261 
262 codelps:
263  ctx->a -= qe;
264  if (ctx->a < qe) {
265  ctx->c += qe;
266  } else {
267  ctx->a = qe;
268  }
269  context[ctxnum] = ctbl[i].lps;
270 
271 renorme:
272  do {
273  ctx->a <<= 1;
274  ctx->c <<= 1;
275  ctx->ct -= 1;
276  if (unlikely(!ctx->ct)) {
277  byteout(ctx);
278  }
279  } while ((ctx->a & 0x8000) == 0);
280 }
281 
282 // -----------------------------------------------------------------------------
283 // The FINALISE procudure from the standard
284 // -----------------------------------------------------------------------------
285 static void
287  // SETBITS
288  const u32 tempc = ctx->c + ctx->a;
289  ctx->c |= 0xffff;
290  if (ctx->c >= tempc) {
291  ctx->c -= 0x8000;
292  }
293 
294  ctx->c <<= ctx->ct;
295  byteout(ctx);
296  ctx->c <<= ctx->ct;
297  byteout(ctx);
298  emit(ctx);
299  if (ctx->b != 0xff) {
300 #ifdef TRACE
301  printf("emit 0xff\n");
302 #endif
303  ctx->b = 0xff;
304  emit(ctx);
305  }
306 #ifdef TRACE
307  printf("emit 0xac\n");
308 #endif
309  ctx->b = 0xac;
310  emit(ctx);
311 }
312 
313 // see comments in .h file
314 void
316  encode_final(ctx);
317 }
318 
319 // -----------------------------------------------------------------------------
320 // When encoding integers there are a number of different cases. This structure
321 // contains all the information for one of those cases
322 // -----------------------------------------------------------------------------
324  int bot, top; // the range of numbers for which this is valid
325  u8 data, bits; // the bits of data to write first, and the number which are valid
326  // These bits are taken from the bottom of the u8, in reverse order
327  u16 delta; // the amount to subtract from the value before encoding it
328  u8 intbits; // number of bits to use to encode the integer
329 };
330 
331 // table for how to encode integers of a given range
332 static struct intencrange_s intencrange[] = {
333  {0, 3, 0, 2, 0, 2},
334  {-1, -1, 9, 4, 0, 0},
335  {-3, -2, 5, 3, 2, 1},
336  {4, 19, 2, 3, 4, 4},
337  {-19,-4, 3, 3, 4, 4},
338  {20, 83, 6, 4, 20, 6},
339  {-83,-20, 7, 4, 20, 6},
340  {84, 339, 14,5, 84, 8},
341  {-339,-84,15,5, 84, 8},
342  {340,4435,30,6, 340, 12},
343  {-4435,-340,31,6,340, 12},
344  {4436,2000000000,62,6,4436, 32},
345  {-2000000000,-4436,63,6,4436, 32}
346 };
347 
348 // see comments in .h file
349 void
351  u8 *const context = ctx->intctx[proc];
352 
353  encode_bit(ctx, context, 1, 1);
354  encode_bit(ctx, context, 3, 0);
355  encode_bit(ctx, context, 6, 0);
356  encode_bit(ctx, context, 12, 0);
357 }
358 
359 // see comments in .h file
360 void
361 jbig2enc_int(struct jbig2enc_ctx *restrict ctx, int proc, int value) {
362  u8 *const context = ctx->intctx[proc];
363  int i;
364 
365  if (value > 2000000000 || value < -2000000000) abort();
366 
367  u32 prev = 1;
368 
369  for (i = 0; ; ++i) {
370  if (intencrange[i].bot <= value && intencrange[i].top >= value) break;
371  }
372  if (value < 0) value = -value;
373  value -= intencrange[i].delta;
374 
375  u8 data = intencrange[i].data;
376  for (int j = 0; j < intencrange[i].bits; ++j) {
377  const u8 v = data & 1;
378  encode_bit(ctx, context, prev, v);
379  data >>= 1;
380  if (prev & 0x100) {
381  // prev > 256
382  prev = (((prev << 1) | v) & 0x1ff) | 0x100;
383  } else {
384  prev = (prev << 1) | v;
385  }
386  }
387 
388  // move the data in value to the top of the word
389  value <<= (32 - intencrange[i].intbits);
390  for (int j = 0; j < intencrange[i].intbits; ++j) {
391  const u8 v = (value & 0x80000000) >> 31;
392  encode_bit(ctx, context, prev, v);
393  // roll the next bit into place
394  value <<= 1;
395  if (prev & 0x100) {
396  // prev > 256
397  prev = (((prev << 1) | v) & 0x1ff) | 0x100;
398  } else {
399  prev = (prev << 1) | v;
400  }
401  }
402 }
403 
404 // see comments in .h file
405 void
406 jbig2enc_iaid(struct jbig2enc_ctx *restrict ctx, int symcodelen, int value) {
407  if (!ctx->iaidctx) {
408  // we've not yet allocated the context index buffer for this
409  ctx->iaidctx = (u8 *) malloc(1 << symcodelen);
410  memset(ctx->iaidctx, 0, 1 << symcodelen);
411  }
412  const u32 mask = (1 << (symcodelen + 1)) - 1;
413 
414  value <<= (32 - symcodelen); // roll the data to the top of the word
415  u32 prev = 1;
416  for (int i = 0; i < symcodelen; ++i) {
417  const u32 tval = prev & mask;
418  const u8 v = (value & 0x80000000) >> 31;
419  encode_bit(ctx, ctx->iaidctx, tval, v);
420  prev = (prev << 1) | v;
421  value <<= 1;
422  }
423 }
424 
425 // This is the test input to the coder as given in the standard (H.2)
426 static const u8 input[] = { 0, 2, 0, 0x51, 0, 0, 0, 0xc0, 0x03, 0x52, 0x87,
427  0x2a, 0xaa, 0xaa, 0xaa, 0xaa, 0x82, 0xc0, 0x20, 0, 0xfc, 0xd7, 0x9e, 0xf6,
428  0xbf, 0x7f, 0xed, 0x90, 0x4f, 0x46, 0xa3, 0xbf } ;
429 
430 // -----------------------------------------------------------------------------
431 // This function is used by jbig2enc_image to fetch values from the image and
432 // to automatically extend the range of the image on three sides with zero's
433 // -----------------------------------------------------------------------------
434 static u8 image_get(const u8 *restrict image, int x, int y, int mx, int my) {
435  if (y < 0) return 0;
436  if (x >= mx) return 0;
437  if (y >= my) return 0;
438  return image[mx * y + x];
439 }
440 
441 // see comments in .h file
442 unsigned
443 jbig2enc_datasize(const struct jbig2enc_ctx *ctx) {
444  return JBIG2_OUTPUTBUFFER_SIZE * ctx->output_chunks->size() + ctx->outbuf_used;
445 }
446 
447 // see comments in .h file
448 void
450  int j = 0;
451  for (std::vector<u8 *>::const_iterator i = ctx->output_chunks->begin();
452  i != ctx->output_chunks->end(); ++i) {
455  }
456 
457  memcpy(&buffer[j], ctx->outbuf, ctx->outbuf_used);
458 }
459 
460 // This is the context used for the TPGD bits
461 #define TPGDCTX 0x9b25
462 
463 // -----------------------------------------------------------------------------
464 // This is designed for Leptonica's 1bpp packed format images. Each row is some
465 // number of 32-bit words. Pixels are in native-byte-order in each word.
466 // -----------------------------------------------------------------------------
467 void
469  int mx, int my, bool duplicate_line_removal) {
470  const u32 *restrict data = (u32 *) idata;
471  u8 *const context = ctx->context;
472  const unsigned words_per_row = (mx + 31) / 32;
473  const unsigned bytes_per_row = words_per_row * 4;
474 
475  u8 ltp = 0, sltp = 0;
476 
477  for (int y = 0; y < my; ++y) {
478  int x = 0;
479 
480  // the c* values store the context bits for each row. The template is fixed
481  // as template 0 with the floating bits in the default locations.
482  u16 c1, c2, c3;
483  // the w* values contain words from each of the rows: w1 is from two rows
484  // up etc. The next bit to roll onto the context values are kept at the top
485  // of these words.
486  u32 w1, w2, w3;
487  w1 = w2 = w3 = 0;
488 
489  if (y >= 2) w1 = data[(y - 2) * words_per_row];
490  if (y >= 1) {
491  w2 = data[(y - 1) * words_per_row];
492 
493  if (duplicate_line_removal) {
494  // it's possible that the last row was the same as this row
495  if (memcmp(&data[y * words_per_row], &data[(y - 1) * words_per_row],
496  bytes_per_row) == 0) {
497  sltp = ltp ^ 1;
498  ltp = 1;
499  } else {
500  sltp = ltp;
501  ltp = 0;
502  }
503  }
504  }
505  if (duplicate_line_removal) {
506  encode_bit(ctx, context, TPGDCTX, sltp);
507  if (ltp) continue;
508  }
509  w3 = data[y * words_per_row];
510 
511  // the top three bits are the start of the context c1
512  c1 = w1 >> 29;
513  c2 = w2 >> 28;
514  // and we need to remove the used bits from the w* vars
515  w1 <<= 3;
516  w2 <<= 4;
517  c3 = 0;
518  for (x = 0; x < mx; ++x) {
519  const u16 tval = (c1 << 11) | (c2 << 4) | c3;
520  const u8 v = (w3 & 0x80000000) >> 31;
521 
522  //fprintf(stderr, "%d %d %d %d\n", x, y, tval, v);
523  encode_bit(ctx, context, tval, v);
524  c1 <<= 1;
525  c2 <<= 1;
526  c3 <<= 1;
527  c1 |= (w1 & 0x80000000) >> 31;
528  c2 |= (w2 & 0x80000000) >> 31;
529  c3 |= v;
530  const int m = x % 32;
531  if (m == 28 && y >= 2) {
532  // need to roll in another word from two lines up
533  const unsigned wordno = (x / 32) + 1;
534  if (wordno >= words_per_row) {
535  w1 = 0;
536  } else {
537  w1 = data[(y - 2) * words_per_row + wordno];
538  }
539  } else {
540  w1 <<= 1;
541  }
542 
543  if (m == 27 && y >= 1) {
544  // need to roll in another word from the last line
545  const unsigned wordno = (x / 32) + 1;
546  if (wordno >= words_per_row) {
547  w2 = 0;
548  } else {
549  w2 = data[(y - 1) * words_per_row + wordno];
550  }
551  } else {
552  w2 <<= 1;
553  }
554 
555  if (m == 31) {
556  // need to roll in another word from this line
557  const unsigned wordno = (x / 32) + 1;
558  if (wordno >= words_per_row) {
559  w3 = 0;
560  } else {
561  w3 = data[y * words_per_row + wordno];
562  }
563  } else {
564  w3 <<= 1;
565  }
566 
567  c1 &= 31;
568  c2 &= 127;
569  c3 &= 15;
570  }
571  }
572 }
573 
574 void
575 jbig2enc_refine(struct jbig2enc_ctx *__restrict__ ctx,
576  const uint8_t *__restrict__ itempl, int tx, int ty,
577  const uint8_t *__restrict__ itarget, int mx, int my,
578  int ox, int oy) {
579  const u32 *restrict templdata = (u32 *) itempl;
580  const u32 *restrict data = (u32 *) itarget;
581  u8 *restrict const context = ctx->context;
582 
583  static int image_counter = 0;
584 
585  image_counter++;
586 
587 #ifdef SYM_DEBUGGING
588  fprintf(stderr, "refine:%d %d %d %d\n", tx, ty, mx, my);
589 #endif
590 
591  const unsigned templwords_per_row = (tx + 31) / 32;
592  const unsigned words_per_row = (mx + 31) / 32;
593 
594  for (int y = 0; y < my; ++y) {
595  int x;
596  const int temply = y + oy;
597  // the template is fixed to the 13 pixel template with the floating bits in
598  // the default locations.
599  // we have 5 words of context. The first three are the last, current and
600  // next rows of the template. The last two are the last and current rows of
601  // the target.
602  // To form the 14 bits of content these are packed from the least
603  // significant bits rightward.
604  u16 c1, c2, c3, c4, c5;
605  // the w* values contain words from each of the corresponding rows. The
606  // next bit to be part of the context is kept at the top of these words
607  u32 w1, w2, w3, w4, w5;
608  w1 = w2 = w3 = w4 = w5 = 0;
609 
610  if (temply >= 1 && (temply - 1) < ty) w1 = templdata[(temply - 1) * templwords_per_row];
611  if (temply >= 0 && temply < ty) w2 = templdata[temply * templwords_per_row];
612  if (temply >= -1 && temply + 1 < ty) w3 = templdata[(temply + 1) * templwords_per_row];
613 
614  // the x offset prevents a hassel because we are dealing with bits. Thus we
615  // restrict it to being {-1, 0, 1}.
616  if (y >= 1) w4 = data[(y - 1) * words_per_row];
617  w5 = data[y * words_per_row];
618 
619  const int shiftoffset = 30 + ox;
620  c1 = w1 >> shiftoffset;
621  c2 = w2 >> shiftoffset;
622  c3 = w3 >> shiftoffset;
623 
624  c4 = w4 >> 30;
625  c5 = 0;
626 
627  // the w* should contain the next bit to be included in the context, in the
628  // MSB position. Thus we need to roll the used bits out of the way.
629  const int bits_to_trim = 2 - ox;
630  w1 <<= bits_to_trim;
631  w2 <<= bits_to_trim;
632  w3 <<= bits_to_trim;
633 
634  w4 <<= 2;
635 
636  for (x = 0; x < mx; ++x) {
637  const u16 tval = (c1 << 10) | (c2 << 7) | (c3 << 4) | (c4 << 1) | c5;
638  const u8 v = w5 >> 31;
639 
640 #ifdef SYM_DEBUGGING
641  fprintf(stderr, "%d %d %d %d\n", x, y, tval, v);
642 #endif
643  encode_bit(ctx, context, tval, v);
644  c1 <<= 1;
645  c2 <<= 1;
646  c3 <<= 1;
647  c4 <<= 1;
648  c1 |= w1 >> 31;
649  c2 |= w2 >> 31;
650  c3 |= w3 >> 31;
651  c4 |= w4 >> 31;
652  c5 = v;
653 
654  const int m = x % 32;
655  const unsigned wordno = (x / 32) + 1;
656  if (m == 29 + ox) {
657  // have run out of bits in the w[123] values. Need to get more.
658 
659  if (wordno >= templwords_per_row) {
660  w1 = w2 = w3 = 0;
661  } else {
662  if (temply >= 1 && (temply - 1 < ty)) {
663  w1 = templdata[(temply - 1) * templwords_per_row + wordno];
664  } else {
665  w1 = 0;
666  }
667  if (temply >= 0 && temply < ty) {
668  w2 = templdata[temply * templwords_per_row + wordno];
669  } else {
670  w2 = 0;
671  }
672  if (temply >= -1 && (temply + 1) < ty) {
673  w3 = templdata[(temply + 1) * templwords_per_row + wordno];
674  } else {
675  w3 = 0;
676  }
677  }
678  } else {
679  w1 <<= 1;
680  w2 <<= 1;
681  w3 <<= 1;
682  }
683 
684  if (m == 29 && y >= 1) {
685  // run out of data from w4
686  if (wordno >= words_per_row) {
687  w4 = 0;
688  } else {
689  w4 = data[(y - 1) * words_per_row + wordno];
690  }
691  } else {
692  w4 <<= 1;
693  }
694 
695  if (m == 31) {
696  // run out of data from w5
697  if (wordno >= words_per_row) {
698  w5 = 0;
699  } else {
700  w5 = data[y * words_per_row + wordno];
701  }
702  } else {
703  w5 <<= 1;
704  }
705 
706  c1 &= 7;
707  c2 &= 7;
708  c3 &= 7;
709  c4 &= 7;
710  }
711  }
712 }
713 
714 // see comments in .h file
715 void
717  int mx, int my, bool duplicate_line_removal) {
718  u8 *const context = ctx->context;
719  u8 ltp = 0;
720  u8 sltp = 0;
721  for (int y = 0; y < my; ++y) {
722  int x = 0;
723  u16 c1 = (image_get(data, x, y - 2, mx, my) << 2) |
724  (image_get(data, x + 1, y - 2, mx, my) << 1) |
725  (image_get(data, x + 2, y - 2, mx, my));
726  u16 c2 = (image_get(data, x, y - 1, mx, my) << 3) |
727  (image_get(data, x + 1, y - 1, mx, my) << 2) |
728  (image_get(data, x + 2, y - 1, mx, my) << 1) |
729  (image_get(data, x + 3, y - 1, mx, my));
730  u16 c3 = 0;
731  if (y > 0) {
732  // it's possible that the last row was the same as this row
733  if (memcmp(&data[y * mx], &data[(y - 1) * mx], mx) == 0) {
734  sltp = ltp ^ 1;
735  ltp = 1;
736  } else {
737  sltp = ltp;
738  ltp = 0;
739  }
740  }
741  if (duplicate_line_removal) {
742  encode_bit(ctx, context, TPGDCTX, sltp);
743  if (ltp) continue;
744  }
745  for (x = 0; x < mx; ++x) {
746  const u16 tval = (c1 << 11) | (c2 << 4) | c3;
747  const u8 v = image_get(data, x, y, mx, my);
748  encode_bit(ctx, context, tval, v);
749  c1 <<= 1;
750  c2 <<= 1;
751  c3 <<= 1;
752  c1 |= image_get(data, x + 3, y - 2, mx, my);
753  c2 |= image_get(data, x + 4, y - 1, mx, my);
754  c3 |= v;
755  c1 &= 31;
756  c2 &= 127;
757  c3 &= 15;
758  }
759  }
760 }
#define w1
#define w4
static char mx
Definition: bmpfont.h:51
#define free(a)
Definition: decNumber.cpp:310
int v
Definition: dviconv.c:10
int printf()
struct rect data
Definition: dvipdfm.c:64
#define w3
#define w5
#define w2
static FIELD_PTR prev
Definition: genind.c:36
#define d(n)
Definition: gpos-common.c:151
#define ec
Definition: gsftopk.c:502
#define memcmp(s1, s2, n)
Definition: gsftopk.c:66
#define memcpy(d, s, n)
Definition: gsftopk.c:64
#define restrict
Definition: jbig2arith.cc:29
void jbig2enc_bitimage(struct jbig2enc_ctx *__restrict__ ctx, const uint8_t *__restrict__ idata, int mx, int my, bool duplicate_line_removal)
Definition: jbig2arith.cc:468
static void encode_bit(struct jbig2enc_ctx *__restrict__ ctx, uint8_t *__restrict__ context, uint32_t ctxnum, uint8_t d)
Definition: jbig2arith.cc:232
void jbig2enc_int(struct jbig2enc_ctx *__restrict__ ctx, int proc, int value)
Definition: jbig2arith.cc:361
#define TPGDCTX
Definition: jbig2arith.cc:461
#define u16
Definition: jbig2arith.cc:25
#define u32
Definition: jbig2arith.cc:24
#define STATETABLE
struct context ctbl[]
Definition: jbig2arith.cc:43
void jbig2enc_dealloc(struct jbig2enc_ctx *ctx)
Definition: jbig2arith.cc:164
void jbig2enc_refine(struct jbig2enc_ctx *__restrict__ ctx, const uint8_t *__restrict__ itempl, int tx, int ty, const uint8_t *__restrict__ itarget, int mx, int my, int ox, int oy)
Definition: jbig2arith.cc:575
void jbig2enc_reset(struct jbig2enc_ctx *ctx)
Definition: jbig2arith.cc:137
static void byteout(struct jbig2enc_ctx *__restrict__ ctx)
Definition: jbig2arith.cc:193
void jbig2enc_image(struct jbig2enc_ctx *__restrict__ ctx, const uint8_t *__restrict__ data, int mx, int my, bool duplicate_line_removal)
Definition: jbig2arith.cc:716
void jbig2enc_oob(struct jbig2enc_ctx *__restrict__ ctx, int proc)
Definition: jbig2arith.cc:350
unsigned jbig2enc_datasize(const struct jbig2enc_ctx *ctx)
Definition: jbig2arith.cc:443
void jbig2enc_final(struct jbig2enc_ctx *__restrict__ ctx)
Definition: jbig2arith.cc:315
void jbig2enc_flush(struct jbig2enc_ctx *ctx)
Definition: jbig2arith.cc:151
static void emit(struct jbig2enc_ctx *__restrict__ ctx)
Definition: jbig2arith.cc:179
void jbig2enc_tobuffer(const struct jbig2enc_ctx *__restrict__ ctx, uint8_t *__restrict__ buffer)
Definition: jbig2arith.cc:449
static void encode_final(struct jbig2enc_ctx *__restrict__ ctx)
Definition: jbig2arith.cc:286
static uint8_t image_get(const uint8_t *__restrict__ image, int x, int y, int mx, int my)
Definition: jbig2arith.cc:434
#define unlikely(x)
Definition: jbig2arith.cc:116
static struct intencrange_s intencrange[]
Definition: jbig2arith.cc:332
void jbig2enc_init(struct jbig2enc_ctx *ctx)
Definition: jbig2arith.cc:121
#define u8
Definition: jbig2arith.cc:26
void jbig2enc_iaid(struct jbig2enc_ctx *__restrict__ ctx, int symcodelen, int value)
Definition: jbig2arith.cc:406
#define JBIG2_MAX_CTX
Definition: jbig2arith.h:24
#define JBIG2_OUTPUTBUFFER_SIZE
Definition: jbig2arith.h:25
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific i
Definition: afcover.h:80
kerning y
Definition: ttdriver.c:212
int int double double double char double char * top
Definition: gdfx.h:19
unsigned char uint8_t
Definition: stdint.h:78
#define fprintf
Definition: mendex.h:64
#define malloc
Definition: alloca.c:91
#define __attribute__(A)
Definition: synctex.c:338
void abort()
float x
Definition: cordic.py:15
struct _proc * proc
Definition: obx.h:95
union value value
Definition: obx.h:44
bstring c int memset(void *s, int c, int length)
#define mask(n)
Definition: lbitlib.c:93
Definition: obx.h:103
Definition: utils.c:300
uint8_t mps
Definition: jbig2arith.cc:36
uint8_t lps
Definition: jbig2arith.cc:36
uint16_t qe
Definition: jbig2arith.cc:35
Definition: sd.h:76
Definition: execute.c:108
uint8_t intbits
Definition: jbig2arith.cc:328
uint8_t bits
Definition: jbig2arith.cc:325
uint16_t delta
Definition: jbig2arith.cc:327
uint8_t data
Definition: jbig2arith.cc:325
uint16_t a
Definition: jbig2arith.h:43
std::vector< uint8_t * > * output_chunks
Definition: jbig2arith.h:48
uint8_t * outbuf
Definition: jbig2arith.h:49
uint8_t context[65536]
Definition: jbig2arith.h:51
uint32_t c
Definition: jbig2arith.h:42
uint8_t b
Definition: jbig2arith.h:44
uint8_t intctx[13][512]
Definition: jbig2arith.h:52
uint8_t ct
Definition: jbig2arith.h:44
int outbuf_used
Definition: jbig2arith.h:50
uint8_t * iaidctx
Definition: jbig2arith.h:54
#define c2
Definition: t1io.c:53
#define c1
Definition: t1io.c:52
int j
Definition: t4ht.c:1589
m
Definition: tex4ht.c:3990
Definition: obx.h:51