Coverage Report

Created: 2025-07-23 08:18

/src/jbigkit/libjbig/jbig.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Portable JBIG image compression library
3
 *
4
 *  Copyright 1995-2014 -- Markus Kuhn -- http://www.cl.cam.ac.uk/~mgk25/
5
 *
6
 *  This module implements a portable standard C encoder and decoder
7
 *  using the JBIG1 lossless bi-level image compression algorithm
8
 *  specified in International Standard ISO 11544:1993 and
9
 *  ITU-T Recommendation T.82. See the file jbig.txt for usage
10
 *  instructions and application examples.
11
 *
12
 *  This program is free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  This program is distributed in the hope that it will be useful,
18
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 *  GNU General Public License for more details.
21
 *
22
 *  You should have received a copy of the GNU General Public License
23
 *  along with this program; if not, write to the Free Software
24
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
 *
26
 *  If you want to use this program under different license conditions,
27
 *  then contact the author for an arrangement.
28
 */
29
30
#ifdef DEBUG
31
#include <stdio.h>
32
#else
33
#define NDEBUG
34
#endif
35
36
#include <stdlib.h>
37
#include <string.h>
38
#include <assert.h>
39
40
#include "jbig.h"
41
42
863
#define MX_MAX  127    /* maximal supported mx offset for
43
      * adaptive template in the encoder */
44
45
113M
#define TPB2CX  0x195  /* contexts for TP special pixels */
46
234M
#define TPB3CX  0x0e5
47
20.7M
#define TPDCX   0xc3f
48
49
/* marker codes */
50
113k
#define MARKER_STUFF    0x00
51
#define MARKER_RESERVE  0x01
52
124k
#define MARKER_SDNORM   0x02
53
127k
#define MARKER_SDRST    0x03
54
11.5k
#define MARKER_ABORT    0x04
55
596
#define MARKER_NEWLEN   0x05
56
14.6k
#define MARKER_ATMOVE   0x06
57
1.86k
#define MARKER_COMMENT  0x07
58
480k
#define MARKER_ESC      0xff
59
60
/* loop array indices */
61
265k
#define STRIPE  0
62
333k
#define LAYER   1
63
298k
#define PLANE   2
64
65
/* special jbg_buf pointers (instead of NULL) */
66
220k
#define SDE_DONE ((struct jbg_buf *) -1)
67
292k
#define SDE_TODO ((struct jbg_buf *) 0)
68
69
/* object code version id */
70
71
const char jbg_version[] =
72
  "JBIG-KIT " JBG_VERSION " -- (c) 1995-2014 Markus Kuhn -- "
73
  "Licence: " JBG_LICENCE "\n";
74
75
/*
76
 * The following array specifies for each combination of the 3
77
 * ordering bits, which ii[] variable represents which dimension
78
 * of s->sde.
79
 */
80
static const int iindex[8][3] = {
81
  { 2, 1, 0 },    /* no ordering bit set */
82
  { -1, -1, -1},  /* SMID -> illegal combination */
83
  { 2, 0, 1 },    /* ILEAVE */
84
  { 1, 0, 2 },    /* SMID + ILEAVE */
85
  { 0, 2, 1 },    /* SEQ */
86
  { 1, 2, 0 },    /* SEQ + SMID */
87
  { 0, 1, 2 },    /* SEQ + ILEAVE */
88
  { -1, -1, -1 }  /* SEQ + SMID + ILEAVE -> illegal combination */
89
};
90
91
#define _(String) String  /* to mark translatable string for GNU gettext */
92
93
/*
94
 * Array with English ASCII error messages that correspond
95
 * to return values from public functions in this library.
96
 */
97
static const char *errmsg[] = {
98
  _("All OK"),                                               /* JBG_EOK */
99
  _("Reached specified image size"),                         /* JBG_EOK_INTR */
100
  _("Unexpected end of input data stream"),                  /* JBG_EAGAIN */
101
  _("Not enough memory available"),                          /* JBG_ENOMEM */
102
  _("ABORT marker segment encountered"),                     /* JBG_EABORT */
103
  _("Unknown marker segment encountered"),                   /* JBG_EMARKER */
104
  _("Input data stream contains invalid data"),              /* JBG_EINVAL */
105
  _("Input data stream uses unimplemented JBIG features"),   /* JBG_EIMPL */
106
  _("Incremental BIE does not continue previous one")        /* JBG_ENOCONT */
107
};
108
109
110
/*
111
 * The following three functions are the only places in this code, were
112
 * C library memory management functions are called. The whole JBIG
113
 * library has been designed in order to allow multi-threaded
114
 * execution. No static or global variables are used, so all fuctions
115
 * are fully reentrant. However if you want to use this multi-thread
116
 * capability and your malloc, realloc and free are not reentrant,
117
 * then simply add the necessary semaphores or mutex primitives below.
118
 * In contrast to C's malloc() and realloc(), but like C's calloc(),
119
 * these functions take two parameters nmemb and size that are multiplied
120
 * before being passed on to the corresponding C function.
121
 * This we can catch all overflows during a size_t multiplication a
122
 * a single place.
123
 */
124
125
#ifndef SIZE_MAX
126
1.33M
#define SIZE_MAX ((size_t) -1)     /* largest value of size_t */
127
#endif
128
129
static void *checked_malloc(size_t nmemb, size_t size)
130
1.33M
{
131
1.33M
  void *p;
132
133
  /* Full manual exception handling is ugly here for performance
134
   * reasons. If an adequate handling of lack of memory is required,
135
   * then use C++ and throw a C++ exception instead of abort(). */
136
137
  /* assert that nmemb * size <= SIZE_MAX */
138
1.33M
  if (size > SIZE_MAX / nmemb)
139
0
    abort();
140
141
1.33M
  p = malloc(nmemb * size);
142
143
1.33M
  if (!p)
144
0
    abort();
145
146
#if 0
147
  fprintf(stderr, "%p = malloc(%lu * %lu)\n", p,
148
    (unsigned long) nmemb, (unsigned long) size);
149
#endif
150
151
1.33M
  return p;
152
1.33M
}
153
154
155
static void *checked_realloc(void *ptr, size_t nmemb, size_t size)
156
0
{
157
0
  void *p;
158
159
  /* Full manual exception handling is ugly here for performance
160
   * reasons. If an adequate handling of lack of memory is required,
161
   * then use C++ and throw a C++ exception here instead of abort(). */
162
163
  /* assert that nmemb * size <= SIZE_MAX */
164
0
  if (size > SIZE_MAX / nmemb)
165
0
    abort();
166
167
0
  p = realloc(ptr, nmemb * size);
168
169
0
  if (!p)
170
0
    abort();
171
172
#if 0
173
  fprintf(stderr, "%p = realloc(%p, %lu * %lu)\n", p, ptr,
174
    (unsigned long) nmemb, (unsigned long) size);
175
#endif
176
177
0
  return p;
178
0
}
179
180
181
static void checked_free(void *ptr)
182
1.33M
{
183
1.33M
  free(ptr);
184
185
#if 0
186
  fprintf(stderr, "free(%p)\n", ptr);
187
#endif
188
189
1.33M
}
190
191
192
193
194
/*
195
 * Memory management for buffers which are used for temporarily
196
 * storing SDEs by the encoder.
197
 *
198
 * The following functions manage a set of struct jbg_buf storage
199
 * containers were each can keep JBG_BUFSIZE bytes. The jbg_buf
200
 * containers can be linked to form linear double-chained lists for
201
 * which a number of operations are provided. Blocks which are
202
 * tempoarily not used any more are returned to a freelist which each
203
 * encoder keeps. Only the destructor of the encoder actually returns
204
 * the block via checked_free() to the stdlib memory management.
205
 */
206
207
208
/*
209
 * Allocate a new buffer block and initialize it. Try to get it from
210
 * the free_list, and if it is empty, call checked_malloc().
211
 */
212
static struct jbg_buf *jbg_buf_init(struct jbg_buf **free_list)
213
82.0k
{
214
82.0k
  struct jbg_buf *new_block;
215
216
  /* Test whether a block from the free list is available */
217
82.0k
  if (*free_list) {
218
27.0k
    new_block = *free_list;
219
27.0k
    *free_list = new_block->next;
220
55.0k
  } else {
221
    /* request a new memory block */
222
55.0k
    new_block = (struct jbg_buf *) checked_malloc(1, sizeof(struct jbg_buf));
223
55.0k
  }
224
82.0k
  new_block->len = 0;
225
82.0k
  new_block->next = NULL;
226
82.0k
  new_block->previous = NULL;
227
82.0k
  new_block->last = new_block;
228
82.0k
  new_block->free_list = free_list;
229
230
82.0k
  return new_block;
231
82.0k
}
232
233
234
/*
235
 * Return an entire free_list to the memory management of stdlib.
236
 * This is only done by jbg_enc_free().
237
 */
238
static void jbg_buf_free(struct jbg_buf **free_list)
239
863
{
240
863
  struct jbg_buf *tmp;
241
242
55.8k
  while (*free_list) {
243
55.0k
    tmp = (*free_list)->next;
244
55.0k
    checked_free(*free_list);
245
55.0k
    *free_list = tmp;
246
55.0k
  }
247
248
863
  return;
249
863
}
250
251
252
/*
253
 * Append a single byte to a single list that starts with the block
254
 * *(struct jbg_buf *) head. The type of *head is void here in order to
255
 * keep the interface of the arithmetic encoder gereric, which uses this
256
 * function as a call-back function in order to deliver single bytes
257
 * for a PSCD.
258
 */
259
static void jbg_buf_write(int b, void *head)
260
73.2M
{
261
73.2M
  struct jbg_buf *now;
262
263
73.2M
  now = ((struct jbg_buf *) head)->last;
264
73.2M
  if (now->len < JBG_BUFSIZE - 1) {
265
73.2M
    now->d[now->len++] = b;
266
73.2M
    return;
267
73.2M
  }
268
7.10k
  now->next = jbg_buf_init(((struct jbg_buf *) head)->free_list);
269
7.10k
  now->next->previous = now;
270
7.10k
  now->next->d[now->next->len++] = b;
271
7.10k
  ((struct jbg_buf *) head)->last = now->next;
272
273
7.10k
  return;
274
73.2M
}
275
276
277
/*
278
 * Remove any trailing zero bytes from the end of a linked jbg_buf list,
279
 * however make sure that no zero byte is removed which directly
280
 * follows a 0xff byte (i.e., keep MARKER_ESC MARKER_STUFF sequences
281
 * intact). This function is used to remove any redundant final zero
282
 * bytes from a PSCD.
283
 */
284
static void jbg_buf_remove_zeros(struct jbg_buf *head)
285
73.3k
{
286
73.3k
  struct jbg_buf *last;
287
288
73.3k
  while (1) {
289
    /* remove trailing 0x00 in last block of list until this block is empty */
290
73.3k
    last = head->last;
291
80.5k
    while (last->len && last->d[last->len - 1] == 0)
292
7.24k
      last->len--;
293
    /* if block became really empty, remove it in case it is not the
294
     * only remaining block and then loop to next block */
295
73.3k
    if (last->previous && !last->len) {
296
10
      head->last->next = *head->free_list;
297
10
      *head->free_list = head->last;
298
10
      head->last = last->previous;
299
10
      head->last->next = NULL;
300
10
    } else
301
73.3k
      break;
302
73.3k
  }
303
304
  /*
305
   * If the final non-zero byte is 0xff (MARKER_ESC), then we just have
306
   * removed a MARKER_STUFF and we will append it again now in order
307
   * to preserve PSCD status of byte stream.
308
   */
309
73.3k
  if (head->last->len && head->last->d[head->last->len - 1] == MARKER_ESC)
310
124
    jbg_buf_write(MARKER_STUFF, head);
311
312
73.3k
  return;
313
73.3k
}
314
315
316
/*
317
 * The jbg_buf list which starts with block *new_prefix is concatenated
318
 * with the list which starts with block **start and *start will then point
319
 * to the first block of the new list.
320
 */
321
static void jbg_buf_prefix(struct jbg_buf *new_prefix, struct jbg_buf **start)
322
1.58k
{
323
1.58k
  new_prefix->last->next = *start;
324
1.58k
  new_prefix->last->next->previous = new_prefix->last;
325
1.58k
  new_prefix->last = new_prefix->last->next->last;
326
1.58k
  *start = new_prefix;
327
328
1.58k
  return;
329
1.58k
}
330
331
332
/*
333
 * Send the contents of a jbg_buf list that starts with block **head to
334
 * the call back function data_out and return the blocks of the jbg_buf
335
 * list to the freelist from which these jbg_buf blocks have been taken.
336
 * After the call, *head == NULL.
337
 */
338
static void jbg_buf_output(struct jbg_buf **head,
339
      void (*data_out)(unsigned char *start,
340
           size_t len, void *file),
341
      void *file)
342
73.3k
{
343
73.3k
  struct jbg_buf *tmp;
344
345
155k
  while (*head) {
346
82.0k
    data_out((*head)->d, (*head)->len, file);
347
82.0k
    tmp = (*head)->next;
348
82.0k
    (*head)->next = *(*head)->free_list;
349
82.0k
    *(*head)->free_list = *head;
350
82.0k
    *head = tmp;
351
82.0k
  }
352
353
73.3k
  return;
354
73.3k
}
355
356
357
/*
358
 * Calculate y = ceil(x/2) applied n times, which is equivalent to
359
 * y = ceil(x/(2^n)). This function is used to
360
 * determine the number of pixels per row or column after n resolution
361
 * reductions. E.g. X[d-1] = jbg_ceil_half(X[d], 1) and X[0] =
362
 * jbg_ceil_half(X[d], d) as defined in clause 6.2.3 of T.82.
363
 */
364
unsigned long jbg_ceil_half(unsigned long x, int n)
365
1.48M
{
366
1.48M
  unsigned long mask;
367
368
1.48M
  assert(n >= 0 && n < 32);
369
1.48M
  mask = (1UL << n) - 1;     /* the lowest n bits are 1 here */
370
1.48M
  return (x >> n) + ((mask & x) != 0);
371
1.48M
}
372
373
374
/*
375
 * Set L0 (the number of lines in a stripe at lowest resolution)
376
 * to a default value, such that there are about 35 stripes, as
377
 * suggested in Annex C of ITU-T T.82, without exceeding the
378
 * limit 128/2^D suggested in Annex A.
379
 */
380
static void jbg_set_default_l0(struct jbg_enc_state *s)
381
1.72k
{
382
1.72k
  s->l0 = jbg_ceil_half(s->yd, s->d) / 35;   /* 35 stripes/image */
383
1.72k
  while ((s->l0 << s->d) > 128)              /* but <= 128 lines/stripe */
384
0
    --s->l0;
385
1.72k
  if (s->l0 < 2) s->l0 = 2;
386
1.72k
}
387
388
389
/*
390
 * Calculate the number of stripes, as defined in clause 6.2.3 of T.82.
391
 */
392
unsigned long jbg_stripes(unsigned long l0, unsigned long yd,
393
        unsigned long d)
394
5.26k
{
395
5.26k
  unsigned long y0 = jbg_ceil_half(yd, d);
396
397
5.26k
  return y0 / l0 + (y0 % l0 != 0);
398
5.26k
}
399
400
401
/*
402
 * Resolution reduction table given by ITU-T T.82 Table 17
403
 */
404
405
static char jbg_resred[4096] = {
406
  0,0,0,1,0,0,0,1,0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
407
  0,0,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
408
  0,0,0,0,0,0,0,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
409
  0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
410
  0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
411
  0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
412
  0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
413
  1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
414
  0,0,0,0,0,0,0,1,0,0,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,
415
  0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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0,1,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,
522
  0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,1,
523
  0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,
524
  0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,
525
  1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,1,1,0,1,1,1,1,1,1,1,
526
  0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,
527
  0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,
528
  0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
529
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,
530
  0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,
531
  0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,0,1,1,1,1,1,1,1,
532
  0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,
533
  0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,1,1,0,0,0,1,0,1,1,1,0,1,1,1
534
};
535
536
/*
537
 * Deterministic prediction tables given by ITU-T T.82 tables
538
 * 19 to 22. The table below is organized differently, the
539
 * index bits are permutated for higher efficiency.
540
 */
541
542
static char jbg_dptable[256 + 512 + 2048 + 4096] = {
543
  /* phase 0: offset=0 */
544
  0,2,2,2,2,2,2,2,2,2,2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,2,2,1,2,2,2,2,2,2,2,2,2,0,2,0,2,1,2,1,
758
  2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
759
  2,0,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
760
  2,1,2,1,2,0,2,0,1,1,1,2,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,1,
761
  2,0,2,0,2,1,2,1,2,2,2,0,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
762
  2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
763
};
764
765
766
/*
767
 * Initialize the status struct for the encoder.
768
 */
769
void jbg_enc_init(struct jbg_enc_state *s, unsigned long x, unsigned long y,
770
                  int planes, unsigned char **p,
771
                  void (*data_out)(unsigned char *start, size_t len,
772
           void *file),
773
      void *file)
774
863
{
775
863
  unsigned long l, lx;
776
863
  int i;
777
778
863
  assert(x > 0 && y > 0 && planes > 0 && planes < 256);
779
863
  s->xd = x;
780
863
  s->yd = y;
781
863
  s->yd1 = y; /* This is the hight initially announced in BIH. To provoke
782
                 generation of NEWLEN for T.85 compatibility tests,
783
                 overwrite with new value s->yd1 > s->yd  */
784
863
  s->planes = planes;
785
863
  s->data_out = data_out;
786
863
  s->file = file;
787
788
863
  s->d = 0;
789
863
  s->dl = 0;
790
863
  s->dh = s->d;
791
863
  jbg_set_default_l0(s);
792
863
  s->mx = 8;
793
863
  s->my = 0;
794
863
  s->order = JBG_ILEAVE | JBG_SMID;
795
863
  s->options = JBG_TPBON | JBG_TPDON | JBG_DPON;
796
863
  s->comment = NULL;
797
863
  s->dppriv = jbg_dptable;
798
863
  s->res_tab = jbg_resred;
799
800
863
  s->highres = (int *) checked_malloc(planes, sizeof(int));
801
863
  s->lhp[0] = p;
802
863
  s->lhp[1] = (unsigned char **)
803
863
    checked_malloc(planes, sizeof(unsigned char *));
804
1.72k
  for (i = 0; i < planes; i++) {
805
863
    s->highres[i] = 0;
806
863
    s->lhp[1][i] = (unsigned char *)
807
863
      checked_malloc(jbg_ceil_half(y, 1), jbg_ceil_half(x, 1+3));
808
863
  }
809
810
863
  s->free_list = NULL;
811
863
  s->s = (struct jbg_arenc_state *)
812
863
    checked_malloc(s->planes, sizeof(struct jbg_arenc_state));
813
863
  s->tx = (int *) checked_malloc(s->planes, sizeof(int));
814
863
  lx = jbg_ceil_half(x, 1);
815
863
  s->tp = (char *) checked_malloc(lx, sizeof(char));
816
415k
  for (l = 0; l < lx; s->tp[l++] = 2) ;
817
863
  s->sde = NULL;
818
819
863
  return;
820
863
}
821
822
823
/*
824
 * This function selects the number of differential layers based on
825
 * the maximum size requested for the lowest resolution layer. If
826
 * possible, a number of differential layers is selected, which will
827
 * keep the size of the lowest resolution layer below or equal to the
828
 * given width x and height y. However not more than 6 differential
829
 * resolution layers will be used. In addition, a reasonable value for
830
 * l0 (height of one stripe in the lowest resolution layer) is
831
 * selected, which obeys the recommended limitations for l0 in annex A
832
 * and C of the JBIG standard. The selected number of resolution layers
833
 * is returned.
834
 */
835
int jbg_enc_lrlmax(struct jbg_enc_state *s, unsigned long x,
836
       unsigned long y)
837
863
{
838
2.20k
  for (s->d = 0; s->d < 6; s->d++)
839
2.20k
    if (jbg_ceil_half(s->xd, s->d) <= x && jbg_ceil_half(s->yd, s->d) <= y)
840
863
      break;
841
863
  s->dl = 0;
842
863
  s->dh = s->d;
843
863
  jbg_set_default_l0(s);
844
863
  return s->d;
845
863
}
846
847
848
/*
849
 * As an alternative to jbg_enc_lrlmax(), the following function allows
850
 * to specify the number of layers directly. The stripe height and layer
851
 * range is also adjusted automatically here.
852
 */
853
void jbg_enc_layers(struct jbg_enc_state *s, int d)
854
0
{
855
0
  if (d < 0 || d > 31)
856
0
    return;
857
0
  s->d  = d;
858
0
  s->dl = 0;
859
0
  s->dh = s->d;
860
0
  jbg_set_default_l0(s);
861
0
  return;
862
0
}
863
864
865
/*
866
 * Specify the highest and lowest resolution layers which will be
867
 * written to the output file. Call this function not before
868
 * jbg_enc_layers() or jbg_enc_lrlmax(), because these two functions
869
 * reset the lowest and highest resolution layer to default values.
870
 * Negative values are ignored. The total number of layers is returned.
871
 */
872
int jbg_enc_lrange(struct jbg_enc_state *s, int dl, int dh)
873
863
{
874
863
  if (dl >= 0     && dl <= s->d) s->dl = dl;
875
863
  if (dh >= s->dl && dh <= s->d) s->dh = dh;
876
877
863
  return s->d;
878
863
}
879
880
881
/*
882
 * The following function allows to specify the bits describing the
883
 * options of the format as well as the maximum AT movement window and
884
 * the number of layer 0 lines per stripes.
885
 */
886
void jbg_enc_options(struct jbg_enc_state *s, int order, int options,
887
         unsigned long l0, int mx, int my)
888
863
{
889
863
  if (order >= 0 && order <= 0x0f) s->order = order;
890
863
  if (options >= 0) s->options = options;
891
863
  if (l0 > 0) s->l0 = l0;
892
863
  if (mx >= 0 && mx < 128) s->mx = mx;
893
863
  if (my >= 0 && my < 256) s->my = my;
894
895
863
  return;
896
863
}
897
898
899
/*
900
 * This function actually does all the tricky work involved in producing
901
 * a SDE, which is stored in the appropriate s->sde[][][] element
902
 * for later output in the correct order.
903
 */
904
static void encode_sde(struct jbg_enc_state *s,
905
           long stripe, int layer, int plane)
906
73.3k
{
907
73.3k
  unsigned char *hp, *lp1, *lp2, *p0, *p1, *q1, *q2;
908
73.3k
  unsigned long hl, ll, hx, hy, lx, ly, hbpl, lbpl;
909
73.3k
  unsigned long line_h0 = 0, line_h1 = 0;
910
73.3k
  unsigned long line_h2, line_h3, line_l1, line_l2, line_l3;
911
73.3k
  struct jbg_arenc_state *se;
912
73.3k
  unsigned long y;  /* current line number in highres image */
913
73.3k
  unsigned long i;  /* current line number within highres stripe */
914
73.3k
  unsigned long j;  /* current column number in highres image */
915
73.3k
  long o;
916
73.3k
  unsigned a, p, t;
917
73.3k
  int ltp, ltp_old, cx;
918
73.3k
  unsigned long c_all, c[MX_MAX + 1], cmin, cmax, clmin, clmax;
919
73.3k
  int tmax, at_determined;
920
73.3k
  int new_tx;
921
73.3k
  long new_tx_line = -1;
922
73.3k
  int reset;
923
73.3k
  struct jbg_buf *new_jbg_buf;
924
925
#ifdef DEBUG
926
  static long tp_lines, tp_exceptions, tp_pixels, dp_pixels;
927
  static long encoded_pixels;
928
#endif
929
930
  /* return immediately if this stripe has already been encoded */
931
73.3k
  if (s->sde[stripe][layer][plane] != SDE_TODO)
932
0
    return;
933
934
#ifdef DEBUG
935
  if (stripe == 0)
936
    tp_lines = tp_exceptions = tp_pixels = dp_pixels = encoded_pixels = 0;
937
  fprintf(stderr, "encode_sde: s/d/p = %2ld/%2d/%2d\n",
938
    stripe, layer, plane);
939
#endif
940
941
  /* number of lines per stripe in highres image */
942
73.3k
  hl = s->l0 << layer;
943
  /* number of lines per stripe in lowres image */
944
73.3k
  ll = hl >> 1;
945
  /* current line number in highres image */
946
73.3k
  y = stripe * hl;
947
  /* number of pixels in highres image */
948
73.3k
  hx = jbg_ceil_half(s->xd, s->d - layer);
949
73.3k
  hy = jbg_ceil_half(s->yd, s->d - layer);
950
  /* number of pixels in lowres image */
951
73.3k
  lx = jbg_ceil_half(hx, 1);
952
73.3k
  ly = jbg_ceil_half(hy, 1);
953
  /* bytes per line in highres and lowres image */
954
73.3k
  hbpl = jbg_ceil_half(hx, 3);
955
73.3k
  lbpl = jbg_ceil_half(lx, 3);
956
  /* pointer to first image byte of highres stripe */
957
73.3k
  hp = s->lhp[s->highres[plane]][plane] + stripe * hl * hbpl;
958
73.3k
  lp2 = s->lhp[1 - s->highres[plane]][plane] + stripe * ll * lbpl;
959
73.3k
  lp1 = lp2 + lbpl;
960
961
  /* check whether we can refer to any state of a previous stripe */
962
73.3k
  reset = (stripe == 0) || (s->options & JBG_SDRST);
963
964
  /* initialize arithmetic encoder */
965
73.3k
  se = s->s + plane;
966
73.3k
  arith_encode_init(se, !reset);
967
73.3k
  s->sde[stripe][layer][plane] = jbg_buf_init(&s->free_list);
968
73.3k
  se->byte_out = jbg_buf_write;
969
73.3k
  se->file = s->sde[stripe][layer][plane];
970
971
  /* initialize adaptive template movement algorithm */
972
73.3k
  c_all = 0;
973
733k
  for (t = 0; t <= s->mx; t++)
974
660k
    c[t] = 0;
975
73.3k
  if (stripe == 0)    /* the SDRST case is handled at the end */
976
2.20k
    s->tx[plane] = 0;
977
73.3k
  new_tx = -1;
978
73.3k
  at_determined = 0;  /* we haven't yet decided the template move */
979
73.3k
  if (s->mx == 0)
980
0
    at_determined = 1;
981
982
  /* initialize typical prediction */
983
73.3k
  ltp = 0;
984
73.3k
  if (reset)
985
2.20k
    ltp_old = 0;
986
71.1k
  else {
987
71.1k
    ltp_old = 1;
988
71.1k
    p1 = hp - hbpl;
989
71.1k
    if (y > 1) {
990
71.1k
      q1 = p1 - hbpl;
991
865k
      while (p1 < hp && (ltp_old = (*p1++ == *q1++)) != 0) ;
992
71.1k
    } else
993
0
      while (p1 < hp && (ltp_old = (*p1++ == 0)) != 0) ;
994
71.1k
  }
995
996
73.3k
  if (layer == 0) {
997
998
    /*
999
     *  Encode lowest resolution layer
1000
     */
1001
1002
267k
    for (i = 0; i < hl && y < hy; i++, y++) {
1003
1004
      /* check whether it is worth to perform an ATMOVE */
1005
239k
      if (!at_determined && c_all > 2048) {
1006
12.8k
  cmin = clmin = 0xffffffffL;
1007
12.8k
  cmax = clmax = 0;
1008
12.8k
  tmax = 0;
1009
89.6k
  for (t = (s->options & JBG_LRLTWO) ? 5 : 3; t <= s->mx; t++) {
1010
76.8k
    if (c[t] > cmax) cmax = c[t];
1011
76.8k
    if (c[t] < cmin) cmin = c[t];
1012
76.8k
    if (c[t] > c[tmax]) tmax = t;
1013
76.8k
  }
1014
12.8k
  clmin = (c[0] < cmin) ? c[0] : cmin;
1015
12.8k
  clmax = (c[0] > cmax) ? c[0] : cmax;
1016
12.8k
  if (c_all - cmax < (c_all >> 3) &&
1017
12.8k
      cmax - c[s->tx[plane]] > c_all - cmax &&
1018
12.8k
      cmax - c[s->tx[plane]] > (c_all >> 4) &&
1019
      /*                     ^ T.82 said < here, fixed in Cor.1/25 */
1020
12.8k
      cmax - (c_all - c[s->tx[plane]]) > c_all - cmax &&
1021
12.8k
      cmax - (c_all - c[s->tx[plane]]) > (c_all >> 4) &&
1022
12.8k
      cmax - cmin > (c_all >> 2) &&
1023
12.8k
      (s->tx[plane] || clmax - clmin > (c_all >> 3))) {
1024
    /* we have decided to perform an ATMOVE */
1025
393
    new_tx = tmax;
1026
393
    if (!(s->options & JBG_DELAY_AT)) {
1027
393
      new_tx_line = i;
1028
393
      s->tx[plane] = new_tx;
1029
393
    }
1030
#ifdef DEBUG
1031
    fprintf(stderr, "ATMOVE: line=%ld, tx=%d, c_all=%ld\n",
1032
      i, new_tx, c_all);
1033
#endif
1034
393
  }
1035
12.8k
  at_determined = 1;
1036
12.8k
      }
1037
239k
      assert(s->tx[plane] >= 0); /* i.e., tx can safely be cast to unsigned */
1038
1039
      /* typical prediction */
1040
239k
      if (s->options & JBG_TPBON) {
1041
239k
  ltp = 1;
1042
239k
  p1 = hp;
1043
239k
  if (i > 0 || !reset) {
1044
238k
    q1 = hp - hbpl;
1045
1.45M
    while (q1 < hp && (ltp = (*p1++ == *q1++)) != 0) ;
1046
238k
  } else
1047
5.36k
    while (p1 < hp + hbpl && (ltp = (*p1++ == 0)) != 0) ;
1048
239k
  arith_encode(se, (s->options & JBG_LRLTWO) ? TPB2CX : TPB3CX,
1049
239k
         ltp == ltp_old);
1050
#ifdef DEBUG
1051
  tp_lines += ltp;
1052
#endif
1053
239k
  ltp_old = ltp;
1054
239k
  if (ltp) {
1055
    /* skip next line */
1056
25.4k
    hp += hbpl;
1057
25.4k
    continue;
1058
25.4k
  }
1059
239k
      }
1060
1061
      /*
1062
       * Layout of the variables line_h1, line_h2, line_h3, which contain
1063
       * as bits the neighbour pixels of the currently coded pixel X:
1064
       *
1065
       *          76543210765432107654321076543210     line_h3
1066
       *          76543210765432107654321076543210     line_h2
1067
       *  76543210765432107654321X76543210             line_h1
1068
       */
1069
1070
214k
      line_h1 = line_h2 = line_h3 = 0;
1071
214k
      if (i > 0 || !reset) line_h2 = (long)*(hp - hbpl) << 8;
1072
214k
      if (i > 1 || !reset) line_h3 = (long)*(hp - hbpl - hbpl) << 8;
1073
1074
      /* encode line */
1075
8.23M
      for (j = 0; j < hx; hp++) {
1076
8.01M
  line_h1 |= *hp;
1077
8.01M
  if (j < hbpl * 8 - 8 && (i > 0 || !reset)) {
1078
7.77M
    line_h2 |= *(hp - hbpl + 1);
1079
7.77M
    if (i > 1 || !reset)
1080
7.75M
      line_h3 |= *(hp - hbpl - hbpl + 1);
1081
7.77M
  }
1082
8.01M
  if (s->options & JBG_LRLTWO) {
1083
    /* two line template */
1084
0
    do {
1085
0
      line_h1 <<= 1;  line_h2 <<= 1;  line_h3 <<= 1;
1086
0
      if (s->tx[plane]) {
1087
0
        if ((unsigned) s->tx[plane] > j)
1088
0
    a = 0;
1089
0
        else {
1090
0
    o = (j - s->tx[plane]) - (j & ~7L);
1091
0
    a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
1092
0
    a <<= 4;
1093
0
        }
1094
0
        assert(s->tx[plane] > 23 ||
1095
0
         a == ((line_h1 >> (4 + s->tx[plane])) & 0x010));
1096
0
        arith_encode(se, (((line_h2 >> 10) & 0x3e0) | a |
1097
0
        ((line_h1 >>  9) & 0x00f)),
1098
0
         (line_h1 >> 8) & 1);
1099
0
      }
1100
0
      else
1101
0
        arith_encode(se, (((line_h2 >> 10) & 0x3f0) |
1102
0
        ((line_h1 >>  9) & 0x00f)),
1103
0
         (line_h1 >> 8) & 1);
1104
#ifdef DEBUG
1105
      encoded_pixels++;
1106
#endif
1107
      /* statistics for adaptive template changes */
1108
0
      if (!at_determined && j >= s->mx && j < hx-2) {
1109
0
        p = (line_h1 & 0x100) != 0; /* current pixel value */
1110
0
        c[0] += ((line_h2 & 0x4000) != 0) == p; /* default position */
1111
0
        assert(!(((line_h2 >> 6) ^ line_h1) & 0x100) ==
1112
0
         (((line_h2 & 0x4000) != 0) == p));
1113
0
        for (t = 5; t <= s->mx && t <= j; t++) {
1114
0
    o = (j - t) - (j & ~7L);
1115
0
    a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
1116
0
    assert(t > 23 ||
1117
0
           (a == p) == !(((line_h1 >> t) ^ line_h1) & 0x100));
1118
0
    c[t] += a == p;
1119
0
        }
1120
0
        for (; t <= s->mx; t++) {
1121
0
    c[t] += 0 == p;
1122
0
        }
1123
0
        ++c_all;
1124
0
      }
1125
0
    } while (++j & 7 && j < hx);
1126
8.01M
  } else {
1127
    /* three line template */
1128
63.5M
    do {
1129
63.5M
      line_h1 <<= 1;  line_h2 <<= 1;  line_h3 <<= 1;
1130
63.5M
      if (s->tx[plane]) {
1131
14.4M
        if ((unsigned) s->tx[plane] > j)
1132
184k
    a = 0;
1133
14.3M
        else {
1134
14.3M
    o = (j - s->tx[plane]) - (j & ~7L);
1135
14.3M
    a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
1136
14.3M
    a <<= 2;
1137
14.3M
        }
1138
14.4M
        assert(s->tx[plane] > 23 ||
1139
14.4M
         a == ((line_h1 >> (6 + s->tx[plane])) & 0x004));
1140
14.4M
        arith_encode(se, (((line_h3 >>  8) & 0x380) |
1141
14.4M
        ((line_h2 >> 12) & 0x078) | a |
1142
14.4M
        ((line_h1 >>  9) & 0x003)),
1143
14.4M
         (line_h1 >> 8) & 1);
1144
14.4M
      } else
1145
49.0M
        arith_encode(se, (((line_h3 >>  8) & 0x380) |
1146
49.0M
        ((line_h2 >> 12) & 0x07c) |
1147
49.0M
        ((line_h1 >>  9) & 0x003)),
1148
49.0M
         (line_h1 >> 8) & 1);
1149
#ifdef DEBUG
1150
      encoded_pixels++;
1151
#endif
1152
      /* statistics for adaptive template changes */
1153
63.5M
      if (!at_determined && j >= s->mx && j < hx-2) {
1154
40.3M
        p = (line_h1 & 0x100) != 0; /* current pixel value */
1155
40.3M
        c[0] += ((line_h2 & 0x4000) != 0) == p; /* default position */
1156
40.3M
        assert(!(((line_h2 >> 6) ^ line_h1) & 0x100) ==
1157
40.3M
         (((line_h2 & 0x4000) != 0) == p));
1158
282M
        for (t = 3; t <= s->mx && t <= j; t++) {
1159
242M
    o = (j - t) - (j & ~7L);
1160
242M
    a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
1161
242M
    assert(t > 23 ||
1162
242M
           (a == p) == !(((line_h1 >> t) ^ line_h1) & 0x100));
1163
242M
    c[t] += a == p;
1164
242M
        }
1165
40.3M
        for (; t <= s->mx; t++) {
1166
0
    c[t] += 0 == p;
1167
0
        }
1168
40.3M
        ++c_all;
1169
40.3M
      }
1170
63.5M
    } while (++j & 7 && j < hx);
1171
8.01M
  } /* if (s->options & JBG_LRLTWO) */
1172
8.01M
      } /* for (j = ...) */
1173
214k
    } /* for (i = ...) */
1174
1175
45.7k
  } else {
1176
1177
    /*
1178
     *  Encode differential layer
1179
     */
1180
1181
1.40M
    for (i = 0; i < hl && y < hy; i++, y++) {
1182
1183
      /* check whether it is worth to perform an ATMOVE */
1184
1.35M
      if (!at_determined && c_all > 2048) {
1185
38.6k
  cmin = clmin = 0xffffffffL;
1186
38.6k
  cmax = clmax = 0;
1187
38.6k
  tmax = 0;
1188
270k
  for (t = 3; t <= s->mx; t++) {
1189
231k
    if (c[t] > cmax) cmax = c[t];
1190
231k
    if (c[t] < cmin) cmin = c[t];
1191
231k
    if (c[t] > c[tmax]) tmax = t;
1192
231k
  }
1193
38.6k
  clmin = (c[0] < cmin) ? c[0] : cmin;
1194
38.6k
  clmax = (c[0] > cmax) ? c[0] : cmax;
1195
38.6k
  if (c_all - cmax < (c_all >> 3) &&
1196
38.6k
      cmax - c[s->tx[plane]] > c_all - cmax &&
1197
38.6k
      cmax - c[s->tx[plane]] > (c_all >> 4) &&
1198
      /*                     ^ T.82 said < here, fixed in Cor.1/25 */
1199
38.6k
      cmax - (c_all - c[s->tx[plane]]) > c_all - cmax &&
1200
38.6k
      cmax - (c_all - c[s->tx[plane]]) > (c_all >> 4) &&
1201
38.6k
      cmax - cmin > (c_all >> 2) &&
1202
38.6k
      (s->tx[plane] || clmax - clmin > (c_all >> 3))) {
1203
    /* we have decided to perform an ATMOVE */
1204
1.19k
    new_tx = tmax;
1205
1.19k
    if (!(s->options & JBG_DELAY_AT)) {
1206
1.19k
      new_tx_line = i;
1207
1.19k
      s->tx[plane] = new_tx;
1208
1.19k
    }
1209
#ifdef DEBUG
1210
    fprintf(stderr, "ATMOVE: line=%ld, tx=%d, c_all=%ld\n",
1211
      i, new_tx, c_all);
1212
#endif
1213
1.19k
  }
1214
38.6k
  at_determined = 1;
1215
38.6k
      }
1216
1217
1.35M
      if ((i >> 1) >= ll - 1 || (y >> 1) >= ly - 1)
1218
90.8k
  lp1 = lp2;
1219
1220
      /* typical prediction */
1221
1.35M
      if (s->options & JBG_TPDON && (i & 1) == 0) {
1222
680k
  q1 = lp1; q2 = lp2;
1223
680k
  p0 = p1 = hp;
1224
680k
  if (i < hl - 1 && y < hy - 1)
1225
679k
    p0 = hp + hbpl;
1226
680k
  if (i > 1 || !reset)
1227
678k
    line_l3 = (long)*(q2 - lbpl) << 8;
1228
1.34k
  else
1229
1.34k
    line_l3 = 0;
1230
680k
  line_l2 = (long)*q2 << 8;
1231
680k
  line_l1 = (long)*q1 << 8;
1232
680k
  ltp = 1;
1233
33.6M
  for (j = 0; j < lx && ltp; q1++, q2++) {
1234
32.9M
    if (j < lbpl * 8 - 8) {
1235
32.4M
      if (i > 1 || !reset)
1236
32.3M
        line_l3 |= *(q2 - lbpl + 1);
1237
32.4M
      line_l2 |= *(q2 + 1);
1238
32.4M
      line_l1 |= *(q1 + 1);
1239
32.4M
    }
1240
65.7M
    do {
1241
65.7M
      if ((j >> 2) < hbpl) {
1242
65.7M
        line_h1 = *(p1++);
1243
65.7M
        line_h0 = *(p0++);
1244
65.7M
      }
1245
262M
      do {
1246
262M
        line_l3 <<= 1;
1247
262M
        line_l2 <<= 1;
1248
262M
        line_l1 <<= 1;
1249
262M
        line_h1 <<= 2;
1250
262M
        line_h0 <<= 2;
1251
262M
        cx = (((line_l3 >> 15) & 0x007) |
1252
262M
        ((line_l2 >> 12) & 0x038) |
1253
262M
        ((line_l1 >> 9)  & 0x1c0));
1254
262M
        if (cx == 0x000)
1255
12.6M
    if ((line_h1 & 0x300) == 0 && (line_h0 & 0x300) == 0)
1256
12.6M
      s->tp[j] = 0;
1257
35.0k
    else {
1258
35.0k
      ltp = 0;
1259
#ifdef DEBUG
1260
      tp_exceptions++;
1261
#endif
1262
35.0k
    }
1263
249M
        else if (cx == 0x1ff)
1264
23.1M
    if ((line_h1 & 0x300) == 0x300 && (line_h0 & 0x300) == 0x300)
1265
22.9M
      s->tp[j] = 1;
1266
187k
    else {
1267
187k
      ltp = 0;
1268
#ifdef DEBUG
1269
      tp_exceptions++;
1270
#endif
1271
187k
    }
1272
226M
        else
1273
226M
    s->tp[j] = 2;
1274
262M
      } while (++j & 3 && j < lx);
1275
65.7M
    } while (j & 7 && j < lx);
1276
32.9M
  } /* for (j = ...) */
1277
680k
  arith_encode(se, TPDCX, !ltp);
1278
#ifdef DEBUG
1279
  tp_lines += ltp;
1280
#endif
1281
680k
      }
1282
1283
1284
      /*
1285
       * Layout of the variables line_h1, line_h2, line_h3, which contain
1286
       * as bits the high resolution neighbour pixels of the currently coded
1287
       * highres pixel X:
1288
       *
1289
       *            76543210 76543210 76543210 76543210     line_h3
1290
       *            76543210 76543210 76543210 76543210     line_h2
1291
       *   76543210 76543210 7654321X 76543210              line_h1
1292
       *
1293
       * Layout of the variables line_l1, line_l2, line_l3, which contain
1294
       * the low resolution pixels near the currently coded pixel as bits.
1295
       * The lowres pixel in which the currently coded highres pixel is
1296
       * located is marked as Y:
1297
       *
1298
       *            76543210 76543210 76543210 76543210     line_l3
1299
       *            76543210 7654321Y 76543210 76543210     line_l2
1300
       *            76543210 76543210 76543210 76543210     line_l1
1301
       */
1302
1303
1304
1.35M
      line_h1 = line_h2 = line_h3 = line_l1 = line_l2 = line_l3 = 0;
1305
1.35M
      if (i > 0 || !reset) line_h2 = (long)*(hp - hbpl) << 8;
1306
1.35M
      if (i > 1 || !reset) {
1307
1.35M
  line_h3 = (long)*(hp - hbpl - hbpl) << 8;
1308
1.35M
  line_l3 = (long)*(lp2 - lbpl) << 8;
1309
1.35M
      }
1310
1.35M
      line_l2 = (long)*lp2 << 8;
1311
1.35M
      line_l1 = (long)*lp1 << 8;
1312
1313
      /* encode line */
1314
82.4M
      for (j = 0; j < hx; lp1++, lp2++) {
1315
81.1M
  if ((j >> 1) < lbpl * 8 - 8) {
1316
79.7M
    if (i > 1 || !reset)
1317
79.6M
      line_l3 |= *(lp2 - lbpl + 1);
1318
79.7M
    line_l2 |= *(lp2 + 1);
1319
79.7M
    line_l1 |= *(lp1 + 1);
1320
79.7M
  }
1321
161M
  do { /* ... while (j & 15 && j < hx) */
1322
1323
161M
    assert(hp - (s->lhp[s->highres[plane]][plane] +
1324
161M
           (stripe * hl + i) * hbpl)
1325
161M
     == (ptrdiff_t) j >> 3);
1326
1327
161M
    assert(lp2 - (s->lhp[1-s->highres[plane]][plane] +
1328
161M
      (stripe * ll + (i>>1)) * lbpl)
1329
161M
     == (ptrdiff_t) j >> 4);
1330
1331
161M
    line_h1 |= *hp;
1332
161M
    if (j < hbpl * 8 - 8) {
1333
160M
      if (i > 0 || !reset) {
1334
160M
        line_h2 |= *(hp - hbpl + 1);
1335
160M
        if (i > 1 || !reset)
1336
160M
    line_h3 |= *(hp - hbpl - hbpl + 1);
1337
160M
      }
1338
160M
    }
1339
645M
    do { /* ... while (j & 7 && j < hx) */
1340
645M
      line_l1 <<= 1;  line_l2 <<= 1;  line_l3 <<= 1;
1341
645M
      if (ltp && s->tp[j >> 1] < 2) {
1342
        /* pixel are typical and have not to be encoded */
1343
64.5M
        line_h1 <<= 2;  line_h2 <<= 2;  line_h3 <<= 2;
1344
#ifdef DEBUG
1345
        do {
1346
    ++tp_pixels;
1347
        } while (++j & 1 && j < hx);
1348
#else
1349
64.5M
        j += 2;
1350
64.5M
#endif
1351
64.5M
      } else
1352
1.16G
        do { /* ... while (++j & 1 && j < hx) */
1353
1.16G
    line_h1 <<= 1;  line_h2 <<= 1;  line_h3 <<= 1;
1354
1355
    /* deterministic prediction */
1356
1.16G
    if (s->options & JBG_DPON) {
1357
1.16G
      if ((y & 1) == 0) {
1358
580M
        if ((j & 1) == 0) {
1359
          /* phase 0 */
1360
290M
          if (s->dppriv[((line_l3 >> 16) & 0x003) |
1361
290M
            ((line_l2 >> 14) & 0x00c) |
1362
290M
            ((line_h1 >> 5)  & 0x010) |
1363
290M
            ((line_h2 >> 10) & 0x0e0)] < 2) {
1364
#ifdef DEBUG
1365
      ++dp_pixels;
1366
#endif
1367
7.48M
      continue;
1368
7.48M
          }
1369
290M
        } else {
1370
          /* phase 1 */
1371
290M
          if (s->dppriv[(((line_l3 >> 16) & 0x003) |
1372
290M
             ((line_l2 >> 14) & 0x00c) |
1373
290M
             ((line_h1 >> 5)  & 0x030) |
1374
290M
             ((line_h2 >> 10) & 0x1c0)) + 256] < 2) {
1375
#ifdef DEBUG
1376
      ++dp_pixels;
1377
#endif
1378
16.9M
      continue;
1379
16.9M
          }
1380
290M
        }
1381
580M
      } else {
1382
580M
        if ((j & 1) == 0) {
1383
          /* phase 2 */
1384
290M
          if (s->dppriv[(((line_l3 >> 16) & 0x003) |
1385
290M
             ((line_l2 >> 14) & 0x00c) |
1386
290M
             ((line_h1 >> 5)  & 0x010) |
1387
290M
             ((line_h2 >> 10) & 0x0e0) |
1388
290M
             ((line_h3 >> 7) & 0x700)) + 768] < 2) {
1389
#ifdef DEBUG
1390
      ++dp_pixels;
1391
#endif
1392
56.9M
      continue;
1393
56.9M
          }
1394
290M
        } else {
1395
          /* phase 3 */
1396
290M
          if (s->dppriv[(((line_l3 >> 16) & 0x003) |
1397
290M
             ((line_l2 >> 14) & 0x00c) |
1398
290M
             ((line_h1 >> 5)  & 0x030) |
1399
290M
             ((line_h2 >> 10) & 0x1c0) |
1400
290M
             ((line_h3 >> 7)  & 0xe00)) + 2816] < 2) {
1401
#ifdef DEBUG
1402
      ++dp_pixels;
1403
#endif
1404
80.0M
      continue;
1405
80.0M
          }
1406
290M
        }
1407
580M
      }
1408
1.16G
    }
1409
1410
    /* determine context */
1411
999M
    if (s->tx[plane]) {
1412
390M
      if ((unsigned) s->tx[plane] > j)
1413
1.71M
        a = 0;
1414
389M
      else {
1415
389M
        o = (j - s->tx[plane]) - (j & ~7L);
1416
389M
        a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
1417
389M
        a <<= 4;
1418
389M
      }
1419
390M
      assert(s->tx[plane] > 23 ||
1420
390M
       a == ((line_h1 >> (4 + s->tx[plane])) & 0x010));
1421
390M
      cx = (((line_h1 >> 9)  & 0x003) | a |
1422
390M
      ((line_h2 >> 13) & 0x00c) |
1423
390M
      ((line_h3 >> 11) & 0x020));
1424
390M
    } else
1425
608M
      cx = (((line_h1 >> 9)  & 0x003) |
1426
608M
      ((line_h2 >> 13) & 0x01c) |
1427
608M
      ((line_h3 >> 11) & 0x020));
1428
999M
    if (j & 1)
1429
483M
      cx |= (((line_l2 >> 9)  & 0x0c0) |
1430
483M
       ((line_l1 >> 7)  & 0x300)) | (1UL << 10);
1431
516M
    else
1432
516M
      cx |= (((line_l2 >> 10) & 0x0c0) |
1433
516M
       ((line_l1 >> 8)  & 0x300));
1434
999M
    cx |= (y & 1) << 11;
1435
1436
999M
    arith_encode(se, cx, (line_h1 >> 8) & 1);
1437
#ifdef DEBUG
1438
    encoded_pixels++;
1439
#endif
1440
1441
    /* statistics for adaptive template changes */
1442
999M
    if (!at_determined && j >= s->mx) {
1443
101M
      c[0] += !(((line_h2 >> 6) ^ line_h1) & 0x100);
1444
710M
      for (t = 3; t <= s->mx; t++)
1445
609M
        c[t] += !(((line_h1 >> t) ^ line_h1) & 0x100);
1446
101M
      ++c_all;
1447
101M
    }
1448
1449
1.16G
        } while (++j & 1 && j < hx);
1450
645M
    } while (j & 7 && j < hx);
1451
0
    hp++;
1452
161M
  } while (j & 15 && j < hx);
1453
81.1M
      } /* for (j = ...) */
1454
1455
      /* low resolution pixels are used twice */
1456
1.35M
      if ((i & 1) == 0) {
1457
680k
  lp1 -= lbpl;
1458
680k
  lp2 -= lbpl;
1459
680k
      }
1460
1461
1.35M
    } /* for (i = ...) */
1462
45.7k
  }
1463
1464
73.3k
  arith_encode_flush(se);
1465
73.3k
  jbg_buf_remove_zeros(s->sde[stripe][layer][plane]);
1466
73.3k
  jbg_buf_write(MARKER_ESC, s->sde[stripe][layer][plane]);
1467
73.3k
  jbg_buf_write((s->options & JBG_SDRST) ? MARKER_SDRST : MARKER_SDNORM,
1468
73.3k
    s->sde[stripe][layer][plane]);
1469
73.3k
  if (s->options & JBG_SDRST)
1470
0
    s->tx[plane] = 0;
1471
1472
  /* add ATMOVE */
1473
73.3k
  if (new_tx != -1) {
1474
1.58k
    if (s->options & JBG_DELAY_AT) {
1475
      /* ATMOVE will become active at the first line of the next stripe */
1476
0
      s->tx[plane] = new_tx;
1477
0
      jbg_buf_write(MARKER_ESC, s->sde[stripe][layer][plane]);
1478
0
      jbg_buf_write(MARKER_ATMOVE, s->sde[stripe][layer][plane]);
1479
0
      jbg_buf_write(0, s->sde[stripe][layer][plane]);
1480
0
      jbg_buf_write(0, s->sde[stripe][layer][plane]);
1481
0
      jbg_buf_write(0, s->sde[stripe][layer][plane]);
1482
0
      jbg_buf_write(0, s->sde[stripe][layer][plane]);
1483
0
      jbg_buf_write(s->tx[plane], s->sde[stripe][layer][plane]);
1484
0
      jbg_buf_write(0, s->sde[stripe][layer][plane]);
1485
1.58k
    } else {
1486
      /* ATMOVE has already become active during this stripe
1487
       * => we have to prefix the SDE data with an ATMOVE marker */
1488
1.58k
      new_jbg_buf = jbg_buf_init(&s->free_list);
1489
1.58k
      jbg_buf_write(MARKER_ESC, new_jbg_buf);
1490
1.58k
      jbg_buf_write(MARKER_ATMOVE, new_jbg_buf);
1491
1.58k
      jbg_buf_write((new_tx_line >> 24) & 0xff, new_jbg_buf);
1492
1.58k
      jbg_buf_write((new_tx_line >> 16) & 0xff, new_jbg_buf);
1493
1.58k
      jbg_buf_write((new_tx_line >> 8) & 0xff, new_jbg_buf);
1494
1.58k
      jbg_buf_write(new_tx_line & 0xff, new_jbg_buf);
1495
1.58k
      jbg_buf_write(new_tx, new_jbg_buf);
1496
1.58k
      jbg_buf_write(0, new_jbg_buf);
1497
1.58k
      jbg_buf_prefix(new_jbg_buf, &s->sde[stripe][layer][plane]);
1498
1.58k
    }
1499
1.58k
  }
1500
1501
#if 0
1502
  if (stripe == s->stripes - 1)
1503
    fprintf(stderr, "tp_lines = %ld, tp_exceptions = %ld, tp_pixels = %ld, "
1504
      "dp_pixels = %ld, encoded_pixels = %ld\n",
1505
      tp_lines, tp_exceptions, tp_pixels, dp_pixels, encoded_pixels);
1506
#endif
1507
1508
73.3k
  return;
1509
73.3k
}
1510
1511
1512
/*
1513
 * Create the next lower resolution version of an image
1514
 */
1515
static void resolution_reduction(struct jbg_enc_state *s, int plane,
1516
         int higher_layer)
1517
1.34k
{
1518
1.34k
  unsigned long hl, ll, hx, hy, lx, ly, hbpl, lbpl;
1519
1.34k
  unsigned char *hp1, *hp2, *hp3, *lp;
1520
1.34k
  unsigned long line_h1, line_h2, line_h3, line_l2;
1521
1.34k
  unsigned long y;  /* current line number in lowres image */
1522
1.34k
  unsigned long i;  /* current line number within lowres stripe */
1523
1.34k
  unsigned long j;  /* current column number in lowres image */
1524
1.34k
  int pix, k, l;
1525
1526
  /* number of lines per stripe in highres image */
1527
1.34k
  hl = s->l0 << higher_layer;
1528
  /* number of lines per stripe in lowres image */
1529
1.34k
  ll = hl >> 1;
1530
  /* number of pixels in highres image */
1531
1.34k
  hx = jbg_ceil_half(s->xd, s->d - higher_layer);
1532
1.34k
  hy = jbg_ceil_half(s->yd, s->d - higher_layer);
1533
  /* number of pixels in lowres image */
1534
1.34k
  lx = jbg_ceil_half(hx, 1);
1535
1.34k
  ly = jbg_ceil_half(hy, 1);
1536
  /* bytes per line in highres and lowres image */
1537
1.34k
  hbpl = jbg_ceil_half(hx, 3);
1538
1.34k
  lbpl = jbg_ceil_half(lx, 3);
1539
  /* pointers to first image bytes */
1540
1.34k
  hp2 = s->lhp[s->highres[plane]][plane];
1541
1.34k
  hp1 = hp2 + hbpl;
1542
1.34k
  hp3 = hp2 - hbpl;
1543
1.34k
  lp = s->lhp[1 - s->highres[plane]][plane];
1544
1545
#ifdef DEBUG
1546
  fprintf(stderr, "resolution_reduction: plane = %d, higher_layer = %d\n",
1547
    plane, higher_layer);
1548
#endif
1549
1550
  /*
1551
   * Layout of the variables line_h1, line_h2, line_h3, which contain
1552
   * as bits the high resolution neighbour pixels of the currently coded
1553
   * lowres pixel /\:
1554
   *              \/
1555
   *
1556
   *   76543210 76543210 76543210 76543210     line_h3
1557
   *   76543210 76543210 765432/\ 76543210     line_h2
1558
   *   76543210 76543210 765432\/ 76543210     line_h1
1559
   *
1560
   * Layout of the variable line_l2, which contains the low resolution
1561
   * pixels near the currently coded pixel as bits. The lowres pixel
1562
   * which is currently coded is marked as X:
1563
   *
1564
   *   76543210 76543210 76543210 76543210     line_l2
1565
   *                            X
1566
   */
1567
1568
47.0k
  for (y = 0; y < ly;) {
1569
725k
    for (i = 0; i < ll && y < ly; i++, y++) {
1570
680k
      if (2*y + 1 >= hy)
1571
604
  hp1 = hp2;
1572
680k
      pix = 0;
1573
680k
      line_h1 = line_h2 = line_h3 = line_l2 = 0;
1574
41.2M
      for (j = 0; j < lbpl * 8; j += 8) {
1575
40.5M
  *lp = 0;
1576
40.5M
  if (i > 0 || (y > 0 && !(s->options & JBG_SDRST)))
1577
40.5M
    line_l2 |= *(lp-lbpl);
1578
121M
  for (k = 0; k < 8 && j + k < lx; k += 4) {
1579
80.9M
    if (((j + k) >> 2) < hbpl) {
1580
80.9M
      if (i > 0 || (y > 0 && !(s->options & JBG_SDRST)))
1581
80.7M
        line_h3 |= *hp3;
1582
80.9M
      ++hp3;
1583
80.9M
      line_h2 |= *(hp2++);
1584
80.9M
      line_h1 |= *(hp1++);
1585
80.9M
    }
1586
403M
    for (l = 0; l < 4 && j + k + l < lx; l++) {
1587
322M
      line_h3 <<= 2;
1588
322M
      line_h2 <<= 2;
1589
322M
      line_h1 <<= 2;
1590
322M
      line_l2 <<= 1;
1591
322M
      pix = s->res_tab[((line_h1 >> 8) & 0x007) |
1592
322M
           ((line_h2 >> 5) & 0x038) |
1593
322M
           ((line_h3 >> 2) & 0x1c0) |
1594
322M
           (pix << 9) | ((line_l2 << 2) & 0xc00)];
1595
322M
      *lp = (*lp << 1) | pix;
1596
322M
    }
1597
80.9M
  }
1598
40.5M
  ++lp;
1599
40.5M
      }
1600
680k
      *(lp - 1) <<= lbpl * 8 - lx;
1601
680k
      hp1 += hbpl;
1602
680k
      hp2 += hbpl;
1603
680k
      hp3 += hbpl;
1604
680k
    }
1605
45.7k
  }
1606
1607
#ifdef DEBUG
1608
  {
1609
    FILE *f;
1610
    char fn[50];
1611
1612
    sprintf(fn, "dbg_d=%02d.pbm", higher_layer - 1);
1613
    f = fopen(fn, "wb");
1614
    fprintf(f, "P4\n%lu %lu\n", lx, ly);
1615
    fwrite(s->lhp[1 - s->highres[plane]][plane], 1, lbpl * ly, f);
1616
    fclose(f);
1617
  }
1618
#endif
1619
1620
1.34k
  return;
1621
1.34k
}
1622
1623
1624
/*
1625
 * This function is called inside the three loops of jbg_enc_out() in
1626
 * order to write the next SDE. It has first to generate the required
1627
 * SDE and all SDEs which have to be encoded before this SDE can be
1628
 * created. The problem here is that if we want to output a lower
1629
 * resolution layer, we have to apply the resolution reduction
1630
 * algorithm first to get it. As we try to safe as much memory as
1631
 * possible, the resolution reduction will overwrite previous higher
1632
 * resolution bitmaps. Consequently, we have to encode and buffer SDEs
1633
 * which depend on higher resolution layers before we can start the
1634
 * resolution reduction. All the logic about which SDE has to be
1635
 * encoded before resolution reduction is allowed is handled
1636
 * here. This approach may be a bit more complex than alternative ways
1637
 * of doing it, but it minimizes the amount of temporary memory used.
1638
 */
1639
static void output_sde(struct jbg_enc_state *s,
1640
           unsigned long stripe, int layer, int plane)
1641
73.3k
{
1642
73.3k
  int lfcl;     /* lowest fully coded layer */
1643
73.3k
  long i;
1644
73.3k
  unsigned long u;
1645
1646
73.3k
  assert(s->sde[stripe][layer][plane] != SDE_DONE);
1647
1648
73.3k
  if (s->sde[stripe][layer][plane] != SDE_TODO) {
1649
#ifdef DEBUG
1650
    fprintf(stderr, "writing SDE: s/d/p = %2lu/%2d/%2d\n",
1651
      stripe, layer, plane);
1652
#endif
1653
45.7k
    jbg_buf_output(&s->sde[stripe][layer][plane], s->data_out, s->file);
1654
45.7k
    s->sde[stripe][layer][plane] = SDE_DONE;
1655
45.7k
    return;
1656
45.7k
  }
1657
1658
  /* Determine the smallest resolution layer in this plane for which
1659
   * not yet all stripes have been encoded into SDEs. This layer will
1660
   * have to be completely coded, before we can apply the next
1661
   * resolution reduction step. */
1662
27.6k
  lfcl = 0;
1663
72.0k
  for (i = s->d; i >= 0; i--)
1664
72.0k
    if (s->sde[s->stripes - 1][i][plane] == SDE_TODO) {
1665
27.6k
      lfcl = i + 1;
1666
27.6k
      break;
1667
27.6k
    }
1668
27.6k
  if (lfcl > s->d && s->d > 0 && stripe == 0) {
1669
    /* perform the first resolution reduction */
1670
696
    resolution_reduction(s, plane, s->d);
1671
696
  }
1672
  /* In case HITOLO is not used, we have to encode and store the higher
1673
   * resolution layers first, although we do not need them right now. */
1674
28.9k
  while (lfcl - 1 > layer) {
1675
47.0k
    for (u = 0; u < s->stripes; u++)
1676
45.7k
      encode_sde(s, u, lfcl - 1, plane);
1677
1.34k
    --lfcl;
1678
1.34k
    s->highres[plane] ^= 1;
1679
1.34k
    if (lfcl > 1)
1680
650
      resolution_reduction(s, plane, lfcl - 1);
1681
1.34k
  }
1682
1683
27.6k
  encode_sde(s, stripe, layer, plane);
1684
1685
#ifdef DEBUG
1686
  fprintf(stderr, "writing SDE: s/d/p = %2lu/%2d/%2d\n", stripe, layer, plane);
1687
#endif
1688
27.6k
  jbg_buf_output(&s->sde[stripe][layer][plane], s->data_out, s->file);
1689
27.6k
  s->sde[stripe][layer][plane] = SDE_DONE;
1690
1691
27.6k
  if (stripe == s->stripes - 1 && layer > 0 &&
1692
27.6k
      s->sde[0][layer-1][plane] == SDE_TODO) {
1693
0
    s->highres[plane] ^= 1;
1694
0
    if (layer > 1)
1695
0
      resolution_reduction(s, plane, layer - 1);
1696
0
  }
1697
1698
27.6k
  return;
1699
73.3k
}
1700
1701
1702
/*
1703
 * Convert the table which controls the deterministic prediction
1704
 * process from the internal format into the representation required
1705
 * for the 1728 byte long DPTABLE element of a BIH.
1706
 *
1707
 * The bit order of the DPTABLE format (see also ITU-T T.82 figure 13) is
1708
 *
1709
 * high res:   4  5  6     low res:  0  1
1710
 *             7  8  9               2  3
1711
 *            10 11 12
1712
 *
1713
 * were 4 table entries are packed into one byte, while we here use
1714
 * internally an unpacked 6912 byte long table indexed by the following
1715
 * bit order:
1716
 *
1717
 * high res:   7  6  5     high res:   8  7  6     low res:  1  0
1718
 * (phase 0)   4  .  .     (phase 1)   5  4  .               3  2
1719
 *             .  .  .                 .  .  .
1720
 *
1721
 * high res:  10  9  8     high res:  11 10  9
1722
 * (phase 2)   7  6  5     (phase 3)   8  7  6
1723
 *             4  .  .                 5  4  .
1724
 */
1725
void jbg_int2dppriv(unsigned char *dptable, const char *internal)
1726
0
{
1727
0
  int i, j, k;
1728
0
  int trans0[ 8] = { 1, 0, 3, 2, 7, 6, 5, 4 };
1729
0
  int trans1[ 9] = { 1, 0, 3, 2, 8, 7, 6, 5, 4 };
1730
0
  int trans2[11] = { 1, 0, 3, 2, 10, 9, 8, 7, 6, 5, 4 };
1731
0
  int trans3[12] = { 1, 0, 3, 2, 11, 10, 9, 8, 7, 6, 5, 4 };
1732
1733
0
  for (i = 0; i < 1728; dptable[i++] = 0) ;
1734
1735
0
#define FILL_TABLE1(offset, len, trans) \
1736
0
  for (i = 0; i < len; i++) { \
1737
0
    k = 0; \
1738
0
    for (j = 0; i >> j; j++) \
1739
0
      k |= ((i >> j) & 1) << trans[j]; \
1740
0
    dptable[(i + offset) >> 2] |= \
1741
0
      (internal[k + offset] & 3) << ((3 - (i&3)) << 1); \
1742
0
  }
1743
1744
0
  FILL_TABLE1(   0,  256, trans0);
1745
0
  FILL_TABLE1( 256,  512, trans1);
1746
0
  FILL_TABLE1( 768, 2048, trans2);
1747
0
  FILL_TABLE1(2816, 4096, trans3);
1748
1749
0
  return;
1750
0
}
1751
1752
1753
/*
1754
 * Convert the table which controls the deterministic prediction
1755
 * process from the 1728 byte long DPTABLE format into the 6912 byte long
1756
 * internal format.
1757
 */
1758
void jbg_dppriv2int(char *internal, const unsigned char *dptable)
1759
36
{
1760
36
  int i, j, k;
1761
36
  int trans0[ 8] = { 1, 0, 3, 2, 7, 6, 5, 4 };
1762
36
  int trans1[ 9] = { 1, 0, 3, 2, 8, 7, 6, 5, 4 };
1763
36
  int trans2[11] = { 1, 0, 3, 2, 10, 9, 8, 7, 6, 5, 4 };
1764
36
  int trans3[12] = { 1, 0, 3, 2, 11, 10, 9, 8, 7, 6, 5, 4 };
1765
1766
36
#define FILL_TABLE2(offset, len, trans) \
1767
248k
  for (i = 0; i < len; i++) { \
1768
248k
    k = 0; \
1769
2.82M
    for (j = 0; i >> j; j++) \
1770
2.57M
      k |= ((i >> j) & 1) << trans[j]; \
1771
248k
    internal[k + offset] = \
1772
248k
      (dptable[(i + offset) >> 2] >> ((3 - (i & 3)) << 1)) & 3; \
1773
248k
  }
1774
1775
36
  FILL_TABLE2(   0,  256, trans0);
1776
36
  FILL_TABLE2( 256,  512, trans1);
1777
36
  FILL_TABLE2( 768, 2048, trans2);
1778
36
  FILL_TABLE2(2816, 4096, trans3);
1779
1780
36
  return;
1781
36
}
1782
1783
1784
/*
1785
 * Encode one full BIE and pass the generated data to the specified
1786
 * call-back function
1787
 */
1788
void jbg_enc_out(struct jbg_enc_state *s)
1789
863
{
1790
863
  unsigned long bpl;
1791
863
  unsigned char buf[20];
1792
863
  unsigned long xd, yd, y;
1793
863
  long ii[3], is[3], ie[3];    /* generic variables for the 3 nested loops */
1794
863
  unsigned long stripe;
1795
863
  int layer, plane;
1796
863
  int order;
1797
863
  unsigned char dpbuf[1728];
1798
1799
  /* some sanity checks */
1800
863
  s->order &= JBG_HITOLO | JBG_SEQ | JBG_ILEAVE | JBG_SMID;
1801
863
  order = s->order & (JBG_SEQ | JBG_ILEAVE | JBG_SMID);
1802
863
  if (iindex[order][0] < 0)
1803
0
    s->order = order = JBG_SMID | JBG_ILEAVE;
1804
863
  if (s->options & JBG_DPON && s->dppriv != jbg_dptable)
1805
0
    s->options |= JBG_DPPRIV;
1806
863
  if (s->mx > MX_MAX)
1807
0
    s->mx = MX_MAX;
1808
863
  s->my = 0;
1809
863
  if (s->mx && s->mx < ((s->options & JBG_LRLTWO) ? 5U : 3U))
1810
0
    s->mx = 0;
1811
863
  if (s->d > 255 || s->d < 0 || s->dh > s->d || s->dh < 0 ||
1812
863
      s->dl < 0 || s->dl > s->dh || s->planes < 0 || s->planes > 255)
1813
0
    return;
1814
  /* prevent uint32 overflow: s->l0 * 2 ^ s->d < 2 ^ 32 */
1815
863
  if (s->d > 31 || (s->d != 0 && s->l0 >= (1UL << (32 - s->d))))
1816
0
    return;
1817
863
  if (s->yd1 < s->yd)
1818
0
    s->yd1 = s->yd;
1819
863
  if (s->yd1 > s->yd)
1820
0
    s->options |= JBG_VLENGTH;
1821
1822
  /* ensure correct zero padding of bitmap at the final byte of each line */
1823
863
  if (s->xd & 7) {
1824
651
    bpl = jbg_ceil_half(s->xd, 3);     /* bytes per line */
1825
1.30k
    for (plane = 0; plane < s->planes; plane++)
1826
723k
      for (y = 0; y < s->yd; y++)
1827
722k
  s->lhp[0][plane][y * bpl + bpl - 1] &= ~((1 << (8 - (s->xd & 7))) - 1);
1828
651
  }
1829
1830
  /* prepare BIH */
1831
863
  buf[0] = s->dl;
1832
863
  buf[1] = s->dh;
1833
863
  buf[2] = s->planes;
1834
863
  buf[3] = 0;
1835
863
  xd = jbg_ceil_half(s->xd, s->d - s->dh);
1836
863
  yd = jbg_ceil_half(s->yd1, s->d - s->dh);
1837
863
  buf[4] = xd >> 24;
1838
863
  buf[5] = (xd >> 16) & 0xff;
1839
863
  buf[6] = (xd >> 8) & 0xff;
1840
863
  buf[7] = xd & 0xff;
1841
863
  buf[8] = yd >> 24;
1842
863
  buf[9] = (yd >> 16) & 0xff;
1843
863
  buf[10] = (yd >> 8) & 0xff;
1844
863
  buf[11] = yd & 0xff;
1845
863
  buf[12] = s->l0 >> 24;
1846
863
  buf[13] = (s->l0 >> 16) & 0xff;
1847
863
  buf[14] = (s->l0 >> 8) & 0xff;
1848
863
  buf[15] = s->l0 & 0xff;
1849
863
  buf[16] = s->mx;
1850
863
  buf[17] = s->my;
1851
863
  buf[18] = s->order;
1852
863
  buf[19] = s->options & 0x7f;
1853
1854
#if 0
1855
  /* sanitize L0 (if it was set to 0xffffffff for T.85-style NEWLEN tests) */
1856
  if (s->l0 > (s->yd >> s->d))
1857
    s->l0 = s->yd >> s->d;
1858
#endif
1859
1860
  /* calculate number of stripes that will be required */
1861
863
  s->stripes = jbg_stripes(s->l0, s->yd, s->d);
1862
1863
  /* allocate buffers for SDE pointers */
1864
863
  if (s->sde == NULL) {
1865
863
    s->sde = (struct jbg_buf ****)
1866
863
      checked_malloc(s->stripes, sizeof(struct jbg_buf ***));
1867
28.4k
    for (stripe = 0; stripe < s->stripes; stripe++) {
1868
27.6k
      s->sde[stripe] = (struct jbg_buf ***)
1869
27.6k
  checked_malloc(s->d + 1, sizeof(struct jbg_buf **));
1870
100k
      for (layer = 0; layer < s->d + 1; layer++) {
1871
73.3k
  s->sde[stripe][layer] = (struct jbg_buf **)
1872
73.3k
    checked_malloc(s->planes, sizeof(struct jbg_buf *));
1873
146k
  for (plane = 0; plane < s->planes; plane++)
1874
73.3k
    s->sde[stripe][layer][plane] = SDE_TODO;
1875
73.3k
      }
1876
27.6k
    }
1877
863
  }
1878
1879
  /* output BIH */
1880
863
  s->data_out(buf, 20, s->file);
1881
863
  if ((s->options & (JBG_DPON | JBG_DPPRIV | JBG_DPLAST)) ==
1882
863
      (JBG_DPON | JBG_DPPRIV)) {
1883
    /* write private table */
1884
0
    jbg_int2dppriv(dpbuf, s->dppriv);
1885
0
    s->data_out(dpbuf, 1728, s->file);
1886
0
  }
1887
1888
#if 0
1889
  /*
1890
   * Encode everything first. This is a simple-minded alternative to
1891
   * all the tricky on-demand encoding logic in output_sde() for
1892
   * debugging purposes.
1893
   */
1894
  for (layer = s->dh; layer >= s->dl; layer--) {
1895
    for (plane = 0; plane < s->planes; plane++) {
1896
      if (layer > 0)
1897
  resolution_reduction(s, plane, layer);
1898
      for (stripe = 0; stripe < s->stripes; stripe++)
1899
  encode_sde(s, stripe, layer, plane);
1900
      s->highres[plane] ^= 1;
1901
    }
1902
  }
1903
#endif
1904
1905
  /*
1906
   * Generic loops over all SDEs. Which loop represents layer, plane and
1907
   * stripe depends on the option flags.
1908
   */
1909
1910
  /* start and end value for each loop */
1911
863
  is[iindex[order][STRIPE]] = 0;
1912
863
  ie[iindex[order][STRIPE]] = s->stripes - 1;
1913
863
  is[iindex[order][LAYER]] = s->dl;
1914
863
  ie[iindex[order][LAYER]] = s->dh;
1915
863
  is[iindex[order][PLANE]] = 0;
1916
863
  ie[iindex[order][PLANE]] = s->planes - 1;
1917
1918
3.07k
  for (ii[0] = is[0]; ii[0] <= ie[0]; ii[0]++)
1919
75.5k
    for (ii[1] = is[1]; ii[1] <= ie[1]; ii[1]++)
1920
146k
      for (ii[2] = is[2]; ii[2] <= ie[2]; ii[2]++) {
1921
1922
73.3k
  stripe = ii[iindex[order][STRIPE]];
1923
73.3k
  if (s->order & JBG_HITOLO)
1924
0
    layer = s->dh - (ii[iindex[order][LAYER]] - s->dl);
1925
73.3k
  else
1926
73.3k
    layer = ii[iindex[order][LAYER]];
1927
73.3k
  plane = ii[iindex[order][PLANE]];
1928
1929
  /* output comment marker segment if there is any pending */
1930
73.3k
  if (s->comment) {
1931
0
    buf[0] = MARKER_ESC;
1932
0
    buf[1] = MARKER_COMMENT;
1933
0
    buf[2] = s->comment_len >> 24;
1934
0
    buf[3] = (s->comment_len >> 16) & 0xff;
1935
0
    buf[4] = (s->comment_len >> 8) & 0xff;
1936
0
    buf[5] = s->comment_len & 0xff;
1937
0
    s->data_out(buf, 6, s->file);
1938
0
    s->data_out(s->comment, s->comment_len, s->file);
1939
0
    s->comment = NULL;
1940
0
  }
1941
1942
73.3k
  output_sde(s, stripe, layer, plane);
1943
1944
  /*
1945
   * When we generate a NEWLEN test case (s->yd1 > s->yd), output
1946
   * NEWLEN after last stripe if we have only a single
1947
   * resolution layer or plane (see ITU-T T.85 profile), otherwise
1948
   * output NEWLEN before last stripe.
1949
   */
1950
73.3k
  if (s->yd1 > s->yd &&
1951
73.3k
      (stripe == s->stripes - 1 ||
1952
0
       (stripe == s->stripes - 2 &&
1953
0
        (s->dl != s->dh || s->planes > 1)))) {
1954
0
    s->yd1 = s->yd;
1955
0
    yd = jbg_ceil_half(s->yd, s->d - s->dh);
1956
0
    buf[0] = MARKER_ESC;
1957
0
    buf[1] = MARKER_NEWLEN;
1958
0
    buf[2] = yd >> 24;
1959
0
    buf[3] = (yd >> 16) & 0xff;
1960
0
    buf[4] = (yd >> 8) & 0xff;
1961
0
    buf[5] = yd & 0xff;
1962
0
    s->data_out(buf, 6, s->file);
1963
#ifdef DEBUG
1964
    fprintf(stderr, "NEWLEN: yd=%lu\n", yd);
1965
#endif
1966
0
    if (stripe == s->stripes - 1) {
1967
0
      buf[1] = MARKER_SDNORM;
1968
0
      s->data_out(buf, 2, s->file);
1969
0
    }
1970
0
  }
1971
1972
73.3k
      }
1973
1974
863
  return;
1975
863
}
1976
1977
1978
void jbg_enc_free(struct jbg_enc_state *s)
1979
863
{
1980
863
  unsigned long stripe;
1981
863
  int layer, plane;
1982
1983
#ifdef DEBUG
1984
  fprintf(stderr, "jbg_enc_free(%p)\n", (void *) s);
1985
#endif
1986
1987
  /* clear buffers for SDEs */
1988
863
  if (s->sde) {
1989
28.4k
    for (stripe = 0; stripe < s->stripes; stripe++) {
1990
100k
      for (layer = 0; layer < s->d + 1; layer++) {
1991
146k
  for (plane = 0; plane < s->planes; plane++)
1992
73.3k
    if (s->sde[stripe][layer][plane] != SDE_DONE &&
1993
73.3k
        s->sde[stripe][layer][plane] != SDE_TODO)
1994
0
      jbg_buf_free(&s->sde[stripe][layer][plane]);
1995
73.3k
  checked_free(s->sde[stripe][layer]);
1996
73.3k
      }
1997
27.6k
      checked_free(s->sde[stripe]);
1998
27.6k
    }
1999
863
    checked_free(s->sde);
2000
863
  }
2001
2002
  /* clear free_list */
2003
863
  jbg_buf_free(&s->free_list);
2004
2005
  /* clear memory for arithmetic encoder states */
2006
863
  checked_free(s->s);
2007
2008
  /* clear memory for differential-layer typical prediction buffer */
2009
863
  checked_free(s->tp);
2010
2011
  /* clear memory for adaptive template pixel offsets */
2012
863
  checked_free(s->tx);
2013
2014
  /* clear lowres image buffers */
2015
863
  if (s->lhp[1]) {
2016
1.72k
    for (plane = 0; plane < s->planes; plane++)
2017
863
      checked_free(s->lhp[1][plane]);
2018
863
    checked_free(s->lhp[1]);
2019
863
  }
2020
2021
  /* clear buffer for index of highres image in lhp */
2022
863
  checked_free(s->highres);
2023
2024
863
  return;
2025
863
}
2026
2027
2028
/*
2029
 * Convert the error codes used by jbg_dec_in() into an English ASCII string
2030
 */
2031
const char *jbg_strerror(int errnum)
2032
13.4k
{
2033
13.4k
  errnum >>= 4;
2034
13.4k
  if (errnum < 0 || (unsigned) errnum >= sizeof(errmsg)/sizeof(errmsg[0]))
2035
0
    return "Unknown error code passed to jbg_strerror()";
2036
2037
13.4k
  return errmsg[errnum];
2038
13.4k
}
2039
2040
2041
/*
2042
 * The constructor for a decoder
2043
 */
2044
void jbg_dec_init(struct jbg_dec_state *s)
2045
4.82k
{
2046
4.82k
  s->order = 0;
2047
4.82k
  s->d = -1;
2048
4.82k
  s->bie_len = 0;
2049
4.82k
  s->buf_len = 0;
2050
4.82k
  s->dppriv = NULL;
2051
4.82k
  s->xmax = 4294967295UL;
2052
4.82k
  s->ymax = 4294967295UL;
2053
4.82k
  s->dmax = 256;
2054
4.82k
  s->maxmem = 2000000000;  /* no final image larger than 2 GB by default */
2055
4.82k
  s->s = NULL;
2056
2057
4.82k
  return;
2058
4.82k
}
2059
2060
2061
/*
2062
 * Specify a maximum image size for the decoder. If the JBIG file has
2063
 * the order bit ILEAVE, but not the bit SEQ set, then the decoder
2064
 * will abort to decode after the image has reached the maximal
2065
 * resolution layer which is still not wider than xmax or higher than
2066
 * ymax.
2067
 */
2068
void jbg_dec_maxsize(struct jbg_dec_state *s, unsigned long xmax,
2069
         unsigned long ymax)
2070
3.37k
{
2071
3.37k
  if (xmax > 0) s->xmax = xmax;
2072
3.37k
  if (ymax > 0) s->ymax = ymax;
2073
2074
3.37k
  return;
2075
3.37k
}
2076
2077
2078
/*
2079
 * Decode the new len PSDC bytes to which data points and add them to
2080
 * the current stripe. Return the number of bytes which have actually
2081
 * been read (this will be less than len if a marker segment was
2082
 * part of the data or if the final byte was 0xff, in which case
2083
 * this code cannot determine whether we have a marker segment).
2084
 */
2085
static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,
2086
        size_t len)
2087
88.2k
{
2088
88.2k
  unsigned long stripe;
2089
88.2k
  unsigned int layer, plane;
2090
88.2k
  unsigned long hl, ll, y, hx, hy, lx, ly, hbpl, lbpl;
2091
88.2k
  unsigned char *hp, *lp1, *lp2, *p1, *q1;
2092
88.2k
  register unsigned long line_h1, line_h2, line_h3;
2093
88.2k
  register unsigned long line_l1, line_l2, line_l3;
2094
88.2k
  struct jbg_ardec_state *se;
2095
88.2k
  unsigned long x;
2096
88.2k
  long o;
2097
88.2k
  unsigned a;
2098
88.2k
  int n;
2099
88.2k
  int pix, cx = 0, slntp, tx;
2100
2101
  /* SDE loop variables */
2102
88.2k
  stripe = s->ii[iindex[s->order & 7][STRIPE]];
2103
88.2k
  layer = s->ii[iindex[s->order & 7][LAYER]];
2104
88.2k
  plane = s->ii[iindex[s->order & 7][PLANE]];
2105
2106
  /* forward data to arithmetic decoder */
2107
88.2k
  se = s->s[plane] + layer - s->dl;
2108
88.2k
  se->pscd_ptr = data;
2109
88.2k
  se->pscd_end = data + len;
2110
2111
  /* number of lines per stripe in highres image */
2112
88.2k
  hl = s->l0 << layer;
2113
  /* number of lines per stripe in lowres image */
2114
88.2k
  ll = hl >> 1;
2115
  /* current line number in highres image */
2116
88.2k
  y = stripe * hl + s->i;
2117
  /* number of pixels in highres image */
2118
88.2k
  hx = jbg_ceil_half(s->xd, s->d - layer);
2119
88.2k
  hy = jbg_ceil_half(s->yd, s->d - layer);
2120
  /* number of pixels in lowres image */
2121
88.2k
  lx = jbg_ceil_half(hx, 1);
2122
88.2k
  ly = jbg_ceil_half(hy, 1);
2123
  /* bytes per line in highres and lowres image */
2124
88.2k
  hbpl = jbg_ceil_half(hx, 3);
2125
88.2k
  lbpl = jbg_ceil_half(lx, 3);
2126
  /* pointer to highres and lowres image bytes */
2127
88.2k
  hp  = s->lhp[ layer    & 1][plane] + (stripe * hl + s->i) * hbpl +
2128
88.2k
    (s->x >> 3);
2129
88.2k
  lp2 = s->lhp[(layer-1) & 1][plane] + (stripe * ll + (s->i >> 1)) * lbpl +
2130
88.2k
    (s->x >> 4);
2131
88.2k
  lp1 = lp2 + lbpl;
2132
2133
  /* restore a few local variables */
2134
88.2k
  line_h1 = s->line_h1;
2135
88.2k
  line_h2 = s->line_h2;
2136
88.2k
  line_h3 = s->line_h3;
2137
88.2k
  line_l1 = s->line_l1;
2138
88.2k
  line_l2 = s->line_l2;
2139
88.2k
  line_l3 = s->line_l3;
2140
88.2k
  x = s->x;
2141
2142
#ifdef DEBUG
2143
  if (s->x == 0 && s->i == 0 && s->pseudo)
2144
    fprintf(stderr, "decode_pscd(%p, %p, %ld): s/d/p = %2lu/%2u/%2u\n",
2145
      (void *) s, (void *) data, (long) len, stripe, layer, plane);
2146
#endif
2147
2148
88.2k
  if (s->x == 0 && s->i == 0 &&
2149
88.2k
      (stripe == 0 || s->reset[plane][layer - s->dl]) && s->pseudo) {
2150
26.0k
    s->tx[plane][layer - s->dl] = s->ty[plane][layer - s->dl] = 0;
2151
26.0k
    s->lntp[plane][layer - s->dl] = 1;
2152
26.0k
  }
2153
2154
88.2k
  if (layer == 0) {
2155
2156
    /*
2157
     *  Decode lowest resolution layer
2158
     */
2159
2160
921M
    for (; s->i < hl && y < hy; s->i++, y++) {
2161
2162
      /* adaptive template changes */
2163
921M
      if (x == 0 && s->pseudo)
2164
931M
  for (n = 0; n < s->at_moves; n++)
2165
9.34M
    if (s->at_line[n] == s->i) {
2166
1.83k
      s->tx[plane][layer - s->dl] = s->at_tx[n];
2167
1.83k
      s->ty[plane][layer - s->dl] = s->at_ty[n];
2168
#ifdef DEBUG
2169
      fprintf(stderr, "ATMOVE: line=%lu, tx=%d, ty=%d.\n", s->i,
2170
        s->tx[plane][layer - s->dl], s->ty[plane][layer - s->dl]);
2171
#endif
2172
1.83k
    }
2173
921M
      tx = s->tx[plane][layer - s->dl];
2174
921M
      assert(tx >= 0); /* i.e., tx can safely be cast to unsigned */
2175
2176
      /* typical prediction */
2177
921M
      if (s->options & JBG_TPBON && s->pseudo) {
2178
347M
  slntp = arith_decode(se, (s->options & JBG_LRLTWO) ? TPB2CX : TPB3CX);
2179
347M
  if (slntp < 0)
2180
4.08k
    goto leave;
2181
347M
  s->lntp[plane][layer - s->dl] =
2182
347M
    !(slntp ^ s->lntp[plane][layer - s->dl]);
2183
347M
  if (!s->lntp[plane][layer - s->dl]) {
2184
    /* this line is 'typical' (i.e. identical to the previous one) */
2185
244M
    p1 = hp;
2186
244M
    if (s->i == 0 && (stripe == 0 || s->reset[plane][layer - s->dl]))
2187
6.74G
      while (p1 < hp + hbpl) *p1++ = 0;
2188
244M
    else {
2189
244M
      q1 = hp - hbpl;
2190
7.10G
      while (q1 < hp) *p1++ = *q1++;
2191
244M
    }
2192
244M
    hp += hbpl;
2193
244M
    continue;
2194
244M
  }
2195
  /* this line is 'not typical' and has to be coded completely */
2196
347M
      }
2197
677M
      s->pseudo = 0;
2198
2199
      /*
2200
       * Layout of the variables line_h1, line_h2, line_h3, which contain
2201
       * as bits the neighbour pixels of the currently decoded pixel X:
2202
       *
2203
       *                     76543210 76543210 76543210 76543210     line_h3
2204
       *                     76543210 76543210 76543210 76543210     line_h2
2205
       *   76543210 76543210 76543210 76543210 X                     line_h1
2206
       */
2207
2208
677M
      if (x == 0) {
2209
677M
  line_h1 = line_h2 = line_h3 = 0;
2210
677M
  if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl]))
2211
677M
    line_h2 = (long)*(hp - hbpl) << 8;
2212
677M
  if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl]))
2213
677M
    line_h3 = (long)*(hp - hbpl - hbpl) << 8;
2214
677M
      }
2215
2216
      /*
2217
       * Another tiny JBIG standard bug:
2218
       *
2219
       * While implementing the line_h3 handling here, I discovered
2220
       * another problem with the ITU-T T.82(1993 E) specification.
2221
       * This might be a somewhat pathological case, however. The
2222
       * standard is unclear about how a decoder should behave in the
2223
       * following situation:
2224
       *
2225
       * Assume we are in layer 0 and all stripes are single lines
2226
       * (L0=1 allowed by table 9). We are now decoding the first (and
2227
       * only) line of the third stripe. Assume, the first stripe was
2228
       * terminated by SDRST and the second stripe was terminated by
2229
       * SDNORM. While decoding the only line of the third stripe with
2230
       * the three-line template, we need access to pixels from the
2231
       * previous two stripes. We know that the previous stripe
2232
       * terminated with SDNROM, so we access the pixel from the
2233
       * second stripe. But do we have to replace the pixels from the
2234
       * first stripe by background pixels, because this stripe ended
2235
       * with SDRST? The standard, especially clause 6.2.5 does never
2236
       * mention this case, so the behaviour is undefined here. My
2237
       * current implementation remembers only the marker used to
2238
       * terminate the previous stripe. In the above example, the
2239
       * pixels of the first stripe are accessed despite the fact that
2240
       * this stripe ended with SDRST. An alternative (only slightly
2241
       * more complicated) implementation would be to remember the end
2242
       * marker (SDNORM or SDRST) of the previous two stripes in a
2243
       * plane/layer and to act accordingly when accessing the two
2244
       * previous lines. What am I supposed to do here?
2245
       *
2246
       * As the standard is unclear about the correct behaviour in the
2247
       * situation of the above example, I strongly suggest to avoid
2248
       * the following situation while encoding data with JBIG:
2249
       *
2250
       *   LRLTWO = 0, L0=1 and both SDNORM and SDRST appear in layer 0.
2251
       *
2252
       * I guess that only a very few if any encoders will switch
2253
       * between SDNORM and SDRST, so let us hope that this ambiguity
2254
       * in the standard will never cause any interoperability
2255
       * problems.
2256
       *
2257
       * Markus Kuhn -- 1995-04-30
2258
       */
2259
2260
      /* decode line */
2261
3.32G
      while (x < hx) {
2262
2.64G
  if ((x & 7) == 0) {
2263
2.64G
    if (x < hbpl * 8 - 8 &&
2264
2.64G
        (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl]))) {
2265
1.88G
      line_h2 |= *(hp - hbpl + 1);
2266
1.88G
      if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl]))
2267
1.84G
        line_h3 |= *(hp - hbpl - hbpl + 1);
2268
1.88G
    }
2269
2.64G
  }
2270
2.64G
  if (s->options & JBG_LRLTWO) {
2271
    /* two line template */
2272
2.01G
    do {
2273
2.01G
      if (tx) {
2274
90.3M
        if ((unsigned) tx > x)
2275
26.5k
    a = 0;
2276
90.2M
        else if (tx < 8)
2277
89.7M
    a = ((line_h1 >> (tx - 5)) & 0x010);
2278
551k
        else {
2279
551k
    o = (x - tx) - (x & ~7L);
2280
551k
    a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
2281
551k
    a <<= 4;
2282
551k
        }
2283
90.3M
        assert(tx > 31 ||
2284
90.3M
         a == ((line_h1 >> (tx - 5)) & 0x010));
2285
90.3M
        pix = arith_decode(se, (((line_h2 >> 9) & 0x3e0) | a |
2286
90.3M
              (line_h1 & 0x00f)));
2287
90.3M
      } else
2288
1.92G
        pix = arith_decode(se, (((line_h2 >> 9) & 0x3f0) |
2289
1.92G
              (line_h1 & 0x00f)));
2290
2.01G
      if (pix < 0)
2291
4.63k
        goto leave;
2292
2.01G
      line_h1 = (line_h1 << 1) | pix;
2293
2.01G
      line_h2 <<= 1;
2294
2.01G
    } while ((++x & 7) && x < hx);
2295
2.32G
  } else {
2296
    /* three line template */
2297
15.0G
    do {
2298
15.0G
      if (tx) {
2299
201M
        if ((unsigned) tx > x)
2300
631k
    a = 0;
2301
201M
        else if (tx < 8)
2302
52.8M
    a = ((line_h1 >> (tx - 3)) & 0x004);
2303
148M
        else {
2304
148M
    o = (x - tx) - (x & ~7L);
2305
148M
    a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
2306
148M
    a <<= 2;
2307
148M
        }
2308
201M
        assert(tx > 31 ||
2309
201M
         a == ((line_h1 >> (tx - 3)) & 0x004));
2310
201M
        pix = arith_decode(se, (((line_h3 >>  7) & 0x380) |
2311
201M
              ((line_h2 >> 11) & 0x078) | a |
2312
201M
              (line_h1 & 0x003)));
2313
201M
      } else
2314
14.8G
        pix = arith_decode(se, (((line_h3 >>  7) & 0x380) |
2315
14.8G
              ((line_h2 >> 11) & 0x07c) |
2316
14.8G
              (line_h1 & 0x003)));
2317
15.0G
      if (pix < 0)
2318
10.6k
        goto leave;
2319
2320
15.0G
      line_h1 = (line_h1 << 1) | pix;
2321
15.0G
      line_h2 <<= 1;
2322
15.0G
      line_h3 <<= 1;
2323
15.0G
    } while ((++x & 7) && x < hx);
2324
2.32G
  } /* if (s->options & JBG_LRLTWO) */
2325
2.64G
  *hp++ = line_h1;
2326
2.64G
      } /* while */
2327
677M
      *(hp - 1) <<= hbpl * 8 - hx;
2328
677M
      x = 0;
2329
677M
      s->pseudo = 1;
2330
677M
    } /* for (i = ...) */
2331
2332
56.3k
  } else {
2333
2334
    /*
2335
     *  Decode differential layer
2336
     */
2337
2338
163M
    for (; s->i < hl && y < hy; s->i++, y++) {
2339
2340
      /* adaptive template changes */
2341
163M
      if (x == 0)
2342
168M
  for (n = 0; n < s->at_moves; n++)
2343
4.97M
    if (s->at_line[n] == s->i) {
2344
546
      s->tx[plane][layer - s->dl] = s->at_tx[n];
2345
546
      s->ty[plane][layer - s->dl] = s->at_ty[n];
2346
#ifdef DEBUG
2347
      fprintf(stderr, "ATMOVE: line=%lu, tx=%d, ty=%d.\n", s->i,
2348
        s->tx[plane][layer - s->dl], s->ty[plane][layer - s->dl]);
2349
#endif
2350
546
    }
2351
163M
      tx = s->tx[plane][layer - s->dl];
2352
2353
      /* handle lower border of low-resolution image */
2354
163M
      if ((s->i >> 1) >= ll - 1 || (y >> 1) >= ly - 1)
2355
23.8k
  lp1 = lp2;
2356
2357
      /* typical prediction */
2358
163M
      if ((s->options & JBG_TPDON) && s->pseudo) {
2359
20.0M
  if ((s->lntp[plane][layer - s->dl] = arith_decode(se, TPDCX)) < 0)
2360
1.42k
    goto leave;
2361
20.0M
      }
2362
163M
      s->pseudo = 0;
2363
2364
      /*
2365
       * Layout of the variables line_h1, line_h2, line_h3, which contain
2366
       * as bits the high resolution neighbour pixels of the currently
2367
       * decoded highres pixel X:
2368
       *
2369
       *                     76543210 76543210 76543210 76543210     line_h3
2370
       *                     76543210 76543210 76543210 76543210     line_h2
2371
       *   76543210 76543210 76543210 76543210 X                     line_h1
2372
       *
2373
       * Layout of the variables line_l1, line_l2, line_l3, which contain
2374
       * the low resolution pixels near the currently decoded pixel as bits.
2375
       * The lowres pixel in which the currently coded highres pixel is
2376
       * located is marked as Y:
2377
       *
2378
       *                     76543210 76543210 76543210 76543210     line_l3
2379
       *                     76543210 76543210 Y6543210 76543210     line_l2
2380
       *                     76543210 76543210 76543210 76543210     line_l1
2381
       */
2382
2383
2384
163M
      if (x == 0) {
2385
163M
  line_h1 = line_h2 = line_h3 = line_l1 = line_l2 = line_l3 = 0;
2386
163M
  if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl])) {
2387
163M
    line_h2 = (long)*(hp - hbpl) << 8;
2388
163M
    if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl]))
2389
163M
      line_h3 = (long)*(hp - hbpl - hbpl) << 8;
2390
163M
  }
2391
163M
  if (s->i > 1 || (y > 1 && !s->reset[plane][layer-s->dl]))
2392
163M
    line_l3 = (long)*(lp2 - lbpl) << 8;
2393
163M
  line_l2 = (long)*lp2 << 8;
2394
163M
  line_l1 = (long)*lp1 << 8;
2395
163M
      }
2396
2397
      /* decode line */
2398
609M
      while (x < hx) {
2399
446M
  if ((x & 15) == 0)
2400
446M
    if ((x >> 1) < lbpl * 8 - 8) {
2401
282M
      line_l1 |= *(lp1 + 1);
2402
282M
      line_l2 |= *(lp2 + 1);
2403
282M
      if (s->i > 1 ||
2404
282M
    (y > 1 && !s->reset[plane][layer - s->dl]))
2405
253M
        line_l3 |= *(lp2 - lbpl + 1);
2406
282M
    }
2407
738M
  do {
2408
2409
738M
    assert(hp  - (s->lhp[ layer     &1][plane] + (stripe * hl + s->i)
2410
738M
      * hbpl) == (ptrdiff_t) x >> 3);
2411
738M
    assert(lp2 - (s->lhp[(layer-1) &1][plane] + (stripe * ll + (s->i>>1))
2412
738M
      * lbpl) == (ptrdiff_t) x >> 4);
2413
2414
738M
    if ((x & 7) == 0)
2415
738M
      if (x < hbpl * 8 - 8) {
2416
575M
        if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl])) {
2417
535M
    line_h2 |= *(hp + 1 - hbpl);
2418
535M
    if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl]))
2419
516M
      line_h3 |= *(hp + 1 - hbpl - hbpl);
2420
535M
        }
2421
575M
      }
2422
2.50G
    do {
2423
2.50G
      if (!s->lntp[plane][layer - s->dl])
2424
444M
              cx = (((line_l3 >> 14) & 0x007) |
2425
444M
                    ((line_l2 >> 11) & 0x038) |
2426
444M
                    ((line_l1 >> 8)  & 0x1c0));
2427
2.50G
      if (!s->lntp[plane][layer - s->dl] &&
2428
2.50G
    (cx == 0x000 || cx == 0x1ff)) {
2429
        /* pixels are typical and have not to be decoded */
2430
414M
        do {
2431
414M
    line_h1 = (line_h1 << 1) | (cx & 1);
2432
414M
        } while ((++x & 1) && x < hx);
2433
208M
        line_h2 <<= 2;  line_h3 <<= 2;
2434
208M
      } else
2435
4.46G
        do {
2436
2437
    /* deterministic prediction */
2438
4.46G
    if (s->options & JBG_DPON)
2439
1.66G
      if ((y & 1) == 0)
2440
837M
        if ((x & 1) == 0)
2441
          /* phase 0 */
2442
426M
          pix = s->dppriv[((line_l3 >> 15) & 0x003) |
2443
426M
              ((line_l2 >> 13) & 0x00c) |
2444
426M
              ((line_h1 <<  4) & 0x010) |
2445
426M
              ((line_h2 >>  9) & 0x0e0)];
2446
410M
        else
2447
          /* phase 1 */
2448
410M
          pix = s->dppriv[(((line_l3 >> 15) & 0x003) |
2449
410M
               ((line_l2 >> 13) & 0x00c) |
2450
410M
               ((line_h1 <<  4) & 0x030) |
2451
410M
               ((line_h2 >>  9) & 0x1c0)) + 256];
2452
831M
      else
2453
831M
        if ((x & 1) == 0)
2454
          /* phase 2 */
2455
424M
          pix = s->dppriv[(((line_l3 >> 15) & 0x003) |
2456
424M
               ((line_l2 >> 13) & 0x00c) |
2457
424M
               ((line_h1 <<  4) & 0x010) |
2458
424M
               ((line_h2 >>  9) & 0x0e0) |
2459
424M
               ((line_h3 >>  6) & 0x700)) + 768];
2460
407M
        else
2461
          /* phase 3 */
2462
407M
          pix = s->dppriv[(((line_l3 >> 15) & 0x003) |
2463
407M
               ((line_l2 >> 13) & 0x00c) |
2464
407M
               ((line_h1 <<  4) & 0x030) |
2465
407M
               ((line_h2 >>  9) & 0x1c0) |
2466
407M
               ((line_h3 >>  6) & 0xe00)) + 2816];
2467
2.79G
    else
2468
2.79G
      pix = 2;
2469
2470
4.46G
    if (pix & 2) {
2471
3.95G
      if (tx)
2472
4.75M
        cx = ((line_h1         & 0x003) |
2473
4.75M
        (((line_h1 << 2) >> (tx - 3)) & 0x010) |
2474
4.75M
        ((line_h2 >> 12) & 0x00c) |
2475
4.75M
        ((line_h3 >> 10) & 0x020));
2476
3.95G
      else
2477
3.95G
        cx = ((line_h1         & 0x003) |
2478
3.95G
        ((line_h2 >> 12) & 0x01c) |
2479
3.95G
        ((line_h3 >> 10) & 0x020));
2480
3.95G
      if (x & 1)
2481
1.88G
        cx |= (((line_l2 >> 8) & 0x0c0) |
2482
1.88G
         ((line_l1 >> 6) & 0x300)) | (1UL << 10);
2483
2.07G
      else
2484
2.07G
        cx |= (((line_l2 >> 9) & 0x0c0) |
2485
2.07G
         ((line_l1 >> 7) & 0x300));
2486
3.95G
      cx |= (y & 1) << 11;
2487
2488
3.95G
      pix = arith_decode(se, cx);
2489
3.95G
      if (pix < 0)
2490
8.23k
        goto leave;
2491
3.95G
    }
2492
2493
4.46G
    line_h1 = (line_h1 << 1) | pix;
2494
4.46G
    line_h2 <<= 1;
2495
4.46G
    line_h3 <<= 1;
2496
2497
4.46G
        } while ((++x & 1) && x < hx);
2498
2.50G
      line_l1 <<= 1; line_l2 <<= 1;  line_l3 <<= 1;
2499
2.50G
    } while ((x & 7) && x < hx);
2500
738M
    *hp++ = line_h1;
2501
738M
  } while ((x & 15) && x < hx);
2502
446M
  ++lp1;
2503
446M
  ++lp2;
2504
446M
      } /* while */
2505
163M
      x = 0;
2506
2507
163M
      *(hp - 1) <<= hbpl * 8 - hx;
2508
163M
      if ((s->i & 1) == 0) {
2509
  /* low resolution pixels are used twice */
2510
81.8M
  lp1 -= lbpl;
2511
81.8M
  lp2 -= lbpl;
2512
81.8M
      } else
2513
81.8M
  s->pseudo = 1;
2514
2515
163M
    } /* for (i = ...) */
2516
2517
31.9k
  }
2518
2519
88.2k
 leave:
2520
2521
  /* save a few local variables */
2522
88.2k
  s->line_h1 = line_h1;
2523
88.2k
  s->line_h2 = line_h2;
2524
88.2k
  s->line_h3 = line_h3;
2525
88.2k
  s->line_l1 = line_l1;
2526
88.2k
  s->line_l2 = line_l2;
2527
88.2k
  s->line_l3 = line_l3;
2528
88.2k
  s->x = x;
2529
2530
88.2k
  return se->pscd_ptr - data;
2531
88.2k
}
2532
2533
2534
/*
2535
 * Provide to the decoder a new BIE fragment of len bytes starting at data.
2536
 *
2537
 * Unless cnt is NULL, *cnt will contain the number of actually read bytes
2538
 * on return.
2539
 *
2540
 * Normal return values:
2541
 *
2542
 *   JBG_EAGAIN      All data bytes provided so far have been processed
2543
 *                   (*cnt == len) but the end of the data stream has
2544
 *                   not yet been recognized. Call the function again
2545
 *                   with additional BIE bytes.
2546
 *   JBG_EOK         The function has reached the end of a and
2547
 *                   a full image has been decoded. The function can
2548
 *                   be called again with data from the next BIE, if
2549
 *                   there exists one, in order to get to a higher
2550
 *                   resolution layer. The remaining len - *cnt bytes
2551
 *                   of the previous data block will then have to passed
2552
 *                   to this function again if len > *cnt.
2553
 *   JBG_EOK_INTR    Parsing the BIE has been interrupted as had been
2554
 *                   requested by a jbg_dec_maxsize() specification.
2555
 *                   This function can be called again with the
2556
 *                   rest of the BIE to continue the decoding process.
2557
 *                   The remaining len - *cnt bytes of the previous
2558
 *                   data block will then have to be passed to this
2559
 *                   function again if len > *cnt.
2560
 *
2561
 * Any other return value indicates that the decoding process was
2562
 * aborted by a serious problem and the only function you can then
2563
 * still call is jbg_dec_free() in order to remove the mess, and
2564
 * jbg85_strerror() to find out what to tell the user. (Looking at the
2565
 * least significant bits of the return value will provide additional
2566
 * information by identifying which test exactly has failed.)
2567
 */
2568
int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len,
2569
         size_t *cnt)
2570
13.4k
{
2571
13.4k
  int i, j, required_length;
2572
13.4k
  unsigned long x, y;
2573
13.4k
  unsigned long is[3], ie[3];
2574
13.4k
  size_t dummy_cnt;
2575
13.4k
  unsigned char *dppriv;
2576
2577
13.4k
  if (!cnt) cnt = &dummy_cnt;
2578
13.4k
  *cnt = 0;
2579
13.4k
  if (len < 1) return JBG_EAGAIN;
2580
2581
  /* read in 20-byte BIH */
2582
13.4k
  if (s->bie_len < 20) {
2583
100k
    while (s->bie_len < 20 && *cnt < len)
2584
95.7k
      s->buffer[s->bie_len++] = data[(*cnt)++];
2585
4.82k
    if (s->bie_len < 20)
2586
50
      return JBG_EAGAIN;
2587
    /* test whether this looks like a valid JBIG header at all */
2588
4.77k
    if (s->buffer[1] < s->buffer[0])
2589
56
      return JBG_EINVAL | 1;
2590
4.71k
    if (s->buffer[3] != 0)           return JBG_EINVAL | 2; /* padding != 0 */
2591
4.64k
    if ((s->buffer[18] & 0xf0) != 0) return JBG_EINVAL | 3; /* padding != 0 */
2592
4.57k
    if ((s->buffer[19] & 0x80) != 0) return JBG_EINVAL | 4; /* padding != 0 */
2593
4.54k
    if (s->buffer[0] != s->d + 1)
2594
17
      return JBG_ENOCONT | 1;
2595
4.53k
    s->dl = s->buffer[0];
2596
4.53k
    s->d = s->buffer[1];
2597
4.53k
    if (s->dl == 0)
2598
4.53k
      s->planes = s->buffer[2];
2599
0
    else
2600
0
      if (s->planes != s->buffer[2])
2601
0
  return JBG_ENOCONT | 2;
2602
4.53k
    x = (((long) s->buffer[ 4] << 24) | ((long) s->buffer[ 5] << 16) |
2603
4.53k
   ((long) s->buffer[ 6] <<  8) | (long) s->buffer[ 7]);
2604
4.53k
    y = (((long) s->buffer[ 8] << 24) | ((long) s->buffer[ 9] << 16) |
2605
4.53k
   ((long) s->buffer[10] <<  8) | (long) s->buffer[11]);
2606
4.53k
    if (s->dl != 0 && ((s->xd << (s->d - s->dl + 1)) != x &&
2607
0
           (s->yd << (s->d - s->dl + 1)) != y))
2608
0
      return JBG_ENOCONT | 3;
2609
4.53k
    s->xd = x;
2610
4.53k
    s->yd = y;
2611
4.53k
    s->l0 = (((long) s->buffer[12] << 24) | ((long) s->buffer[13] << 16) |
2612
4.53k
       ((long) s->buffer[14] <<  8) | (long) s->buffer[15]);
2613
    /* ITU-T T.85 trick not directly supported by decoder; for full
2614
     * T.85 compatibility with respect to all NEWLEN marker scenarios,
2615
     * preprocess BIE with jbg_newlen() before passing it to the decoder,
2616
     * or consider using the decoder found in jbig85.c instead. */
2617
4.53k
    if (s->yd == 0xffffffff)
2618
5
      return JBG_EIMPL | 1;
2619
4.52k
    if (!s->planes) return JBG_EINVAL | 5;
2620
4.48k
    if (!s->xd)     return JBG_EINVAL | 6;
2621
4.47k
    if (!s->yd)     return JBG_EINVAL | 7;
2622
4.47k
    if (!s->l0)     return JBG_EINVAL | 8;
2623
    /* prevent uint32 overflow: s->l0 * 2 ^ s->d < 2 ^ 32 */
2624
4.46k
    if (s->d > 31)
2625
34
      return JBG_EIMPL | 2;
2626
4.42k
    if ((s->d != 0 && s->l0 >= (1UL << (32 - s->d))))
2627
67
      return JBG_EIMPL | 3;
2628
4.36k
    s->mx = s->buffer[16];
2629
4.36k
    if (s->mx > 127)
2630
9
      return JBG_EINVAL | 9;
2631
4.35k
    s->my = s->buffer[17];
2632
#if 0
2633
    if (s->my > 0)
2634
      return JBG_EIMPL | 4;
2635
#endif
2636
4.35k
    s->order = s->buffer[18];
2637
4.35k
    if (iindex[s->order & 7][0] < 0)
2638
5
      return JBG_EINVAL | 10;
2639
    /* HITOLO and SEQ currently not yet implemented */
2640
4.34k
    if (s->dl != s->d && (s->order & JBG_HITOLO || s->order & JBG_SEQ))
2641
11
      return JBG_EIMPL | 5;
2642
4.33k
    s->options = s->buffer[19];
2643
2644
    /* will the final image require more bytes than permitted by s->maxmem? */
2645
4.33k
    if (s->maxmem / s->planes / s->yd / jbg_ceil_half(s->xd, 3) == 0)
2646
129
      return JBG_ENOMEM;   /* increase s->maxmem if needed */
2647
2648
    /* calculate number of stripes that will be required */
2649
4.20k
    s->stripes = jbg_stripes(s->l0, s->yd, s->d);
2650
2651
    /* some initialization */
2652
4.20k
    s->ii[iindex[s->order & 7][STRIPE]] = 0;
2653
4.20k
    s->ii[iindex[s->order & 7][LAYER]] = s->dl;
2654
4.20k
    s->ii[iindex[s->order & 7][PLANE]] = 0;
2655
4.20k
    if (s->dl == 0) {
2656
4.20k
      s->s      = (struct jbg_ardec_state **)
2657
4.20k
  checked_malloc(s->planes, sizeof(struct jbg_ardec_state *));
2658
4.20k
      s->tx     = (int **) checked_malloc(s->planes, sizeof(int *));
2659
4.20k
      s->ty     = (int **) checked_malloc(s->planes, sizeof(int *));
2660
4.20k
      s->reset  = (int **) checked_malloc(s->planes, sizeof(int *));
2661
4.20k
      s->lntp   = (int **) checked_malloc(s->planes, sizeof(int *));
2662
4.20k
      s->lhp[0] = (unsigned char **)
2663
4.20k
  checked_malloc(s->planes, sizeof(unsigned char *));
2664
4.20k
      s->lhp[1] = (unsigned char **)
2665
4.20k
  checked_malloc(s->planes, sizeof(unsigned char *));
2666
167k
      for (i = 0; i < s->planes; i++) {
2667
163k
  s->s[i]     = (struct jbg_ardec_state *)
2668
163k
    checked_malloc(s->d - s->dl + 1, sizeof(struct jbg_ardec_state));
2669
163k
  s->tx[i]    = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int));
2670
163k
  s->ty[i]    = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int));
2671
163k
  s->reset[i] = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int));
2672
163k
  s->lntp[i]  = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int));
2673
163k
  s->lhp[ s->d    & 1][i] = (unsigned char *)
2674
163k
    checked_malloc(s->yd, jbg_ceil_half(s->xd, 3));
2675
163k
  s->lhp[(s->d-1) & 1][i] = (unsigned char *)
2676
163k
    checked_malloc(jbg_ceil_half(s->yd, 1), jbg_ceil_half(s->xd, 1+3));
2677
163k
      }
2678
4.20k
    } else {
2679
0
      for (i = 0; i < s->planes; i++) {
2680
0
  s->s[i]     = (struct jbg_ardec_state *)
2681
0
    checked_realloc(s->s[i], s->d - s->dl + 1,
2682
0
        sizeof(struct jbg_ardec_state));
2683
0
  s->tx[i]    = (int *) checked_realloc(s->tx[i],
2684
0
                s->d - s->dl + 1, sizeof(int));
2685
0
  s->ty[i]    = (int *) checked_realloc(s->ty[i],
2686
0
                s->d - s->dl + 1, sizeof(int));
2687
0
  s->reset[i] = (int *) checked_realloc(s->reset[i],
2688
0
                s->d - s->dl + 1, sizeof(int));
2689
0
  s->lntp[i]  = (int *) checked_realloc(s->lntp[i],
2690
0
                s->d - s->dl + 1, sizeof(int));
2691
0
  s->lhp[ s->d    & 1][i] = (unsigned char *)
2692
0
    checked_realloc(s->lhp[ s->d    & 1][i],
2693
0
        s->yd, jbg_ceil_half(s->xd, 3));
2694
0
  s->lhp[(s->d-1) & 1][i] = (unsigned char *)
2695
0
    checked_realloc(s->lhp[(s->d-1) & 1][i],
2696
0
        jbg_ceil_half(s->yd, 1), jbg_ceil_half(s->xd, 1+3));
2697
0
      }
2698
0
    }
2699
167k
    for (i = 0; i < s->planes; i++)
2700
700k
      for (j = 0; j <= s->d - s->dl; j++)
2701
537k
  arith_decode_init(s->s[i] + j, 0);
2702
4.20k
    if (s->dl == 0 || (s->options & JBG_DPON && !(s->options & JBG_DPPRIV)))
2703
4.20k
      s->dppriv = jbg_dptable;
2704
4.20k
    s->comment_skip = 0;
2705
4.20k
    s->buf_len = 0;
2706
4.20k
    s->x = 0;
2707
4.20k
    s->i = 0;
2708
4.20k
    s->pseudo = 1;
2709
4.20k
    s->at_moves = 0;
2710
4.20k
  }
2711
2712
  /* read in DPTABLE */
2713
12.8k
  if (s->bie_len < 20 + 1728 &&
2714
12.8k
      (s->options & (JBG_DPON | JBG_DPPRIV | JBG_DPLAST)) ==
2715
12.6k
      (JBG_DPON | JBG_DPPRIV)) {
2716
100
    assert(s->bie_len >= 20);
2717
100
    if (!s->dppriv || s->dppriv == jbg_dptable)
2718
100
      s->dppriv = (char *) checked_malloc(1728, sizeof(char));
2719
74.9k
    while (s->bie_len < 20 + 1728 && *cnt < len)
2720
74.8k
      s->dppriv[s->bie_len++ - 20] = data[(*cnt)++];
2721
100
    if (s->bie_len < 20 + 1728)
2722
64
      return JBG_EAGAIN;
2723
36
    dppriv = (unsigned char *) s->dppriv;
2724
36
    s->dppriv = (char *) checked_malloc(6912, sizeof(char));
2725
36
    jbg_dppriv2int(s->dppriv, dppriv);
2726
36
    checked_free(dppriv);
2727
36
  }
2728
2729
  /*
2730
   * BID processing loop
2731
   */
2732
2733
181k
  while (*cnt < len) {
2734
2735
    /* process floating marker segments */
2736
2737
    /* skip COMMENT contents */
2738
170k
    if (s->comment_skip) {
2739
8.19k
      if (s->comment_skip <= len - *cnt) {
2740
140
  *cnt += s->comment_skip;
2741
140
  s->comment_skip = 0;
2742
8.05k
      } else {
2743
8.05k
  s->comment_skip -= len - *cnt;
2744
8.05k
  *cnt = len;
2745
8.05k
      }
2746
8.19k
      continue;
2747
8.19k
    }
2748
2749
    /* load complete marker segments into s->buffer for processing */
2750
162k
    if (s->buf_len > 0) {
2751
66.7k
      assert(s->buffer[0] == MARKER_ESC);
2752
133k
      while (s->buf_len < 2 && *cnt < len)
2753
66.7k
  s->buffer[s->buf_len++] = data[(*cnt)++];
2754
66.7k
      if (s->buf_len < 2) continue;
2755
66.7k
      switch (s->buffer[1]) {
2756
747
      case MARKER_COMMENT: required_length = 6; break;
2757
5.94k
      case MARKER_ATMOVE:  required_length = 8; break;
2758
289
      case MARKER_NEWLEN:  required_length = 6; break;
2759
8
      case MARKER_ABORT:
2760
21.6k
      case MARKER_SDNORM:
2761
32.7k
      case MARKER_SDRST:   required_length = 2; break;
2762
26.5k
      case MARKER_STUFF:
2763
  /* forward stuffed 0xff to arithmetic decoder */
2764
26.5k
  s->buf_len = 0;
2765
26.5k
  decode_pscd(s, s->buffer, 2);
2766
26.5k
  continue;
2767
379
      default:
2768
379
  return JBG_EMARKER;
2769
66.7k
      }
2770
79.2k
      while (s->buf_len < required_length && *cnt < len)
2771
39.5k
  s->buffer[s->buf_len++] = data[(*cnt)++];
2772
39.7k
      if (s->buf_len < required_length) continue;
2773
      /* now the buffer is filled with exactly one marker segment */
2774
39.6k
      switch (s->buffer[1]) {
2775
739
      case MARKER_COMMENT:
2776
739
  s->comment_skip =
2777
739
    (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) |
2778
739
     ((long) s->buffer[4] <<  8) | (long) s->buffer[5]);
2779
739
  break;
2780
5.88k
      case MARKER_ATMOVE:
2781
5.88k
  if (s->at_moves < JBG_ATMOVES_MAX) {
2782
5.88k
    s->at_line[s->at_moves] =
2783
5.88k
      (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) |
2784
5.88k
       ((long) s->buffer[4] <<  8) | (long) s->buffer[5]);
2785
5.88k
    s->at_tx[s->at_moves] = (signed char) s->buffer[6];
2786
5.88k
    s->at_ty[s->at_moves] = s->buffer[7];
2787
5.88k
    if (s->at_tx[s->at_moves] < - (int) s->mx ||
2788
5.88k
        s->at_tx[s->at_moves] >   (int) s->mx ||
2789
5.88k
        s->at_ty[s->at_moves] >   (int) s->my ||
2790
5.88k
        (s->at_ty[s->at_moves] == 0 && s->at_tx[s->at_moves] < 0))
2791
87
      return JBG_EINVAL | 11;
2792
5.79k
    if (s->at_ty[s->at_moves] != 0)
2793
6
      return JBG_EIMPL | 6;
2794
5.79k
    s->at_moves++;
2795
5.79k
  } else
2796
3
    return JBG_EIMPL | 7; /* more than JBG_ATMOVES_MAX ATMOVES */
2797
5.79k
  break;
2798
5.79k
      case MARKER_NEWLEN:
2799
280
  y = (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) |
2800
280
       ((long) s->buffer[4] <<  8) | (long) s->buffer[5]);
2801
280
  if (y > s->yd)                   return JBG_EINVAL | 12;
2802
191
  if (!(s->options & JBG_VLENGTH)) return JBG_EINVAL | 13;
2803
189
  s->yd = y;
2804
  /* calculate again number of stripes that will be required */
2805
189
  s->stripes = jbg_stripes(s->l0, s->yd, s->d);
2806
189
  break;
2807
8
      case MARKER_ABORT:
2808
8
  return JBG_EABORT;
2809
2810
21.6k
      case MARKER_SDNORM:
2811
32.7k
      case MARKER_SDRST:
2812
  /* decode final pixels based on trailing zero bytes */
2813
32.7k
  decode_pscd(s, s->buffer, 2);
2814
2815
32.7k
  arith_decode_init(s->s[s->ii[iindex[s->order & 7][PLANE]]] +
2816
32.7k
        s->ii[iindex[s->order & 7][LAYER]] - s->dl,
2817
32.7k
        s->ii[iindex[s->order & 7][STRIPE]] != s->stripes - 1
2818
32.7k
        && s->buffer[1] != MARKER_SDRST);
2819
2820
32.7k
  s->reset[s->ii[iindex[s->order & 7][PLANE]]]
2821
32.7k
    [s->ii[iindex[s->order & 7][LAYER]] - s->dl] =
2822
32.7k
      (s->buffer[1] == MARKER_SDRST);
2823
2824
  /* prepare for next SDE */
2825
32.7k
  s->x = 0;
2826
32.7k
  s->i = 0;
2827
32.7k
  s->pseudo = 1;
2828
32.7k
  s->at_moves = 0;
2829
2830
  /* increment layer/stripe/plane loop variables */
2831
  /* start and end value for each loop: */
2832
32.7k
  is[iindex[s->order & 7][STRIPE]] = 0;
2833
32.7k
  ie[iindex[s->order & 7][STRIPE]] = s->stripes - 1;
2834
32.7k
  is[iindex[s->order & 7][LAYER]] = s->dl;
2835
32.7k
  ie[iindex[s->order & 7][LAYER]] = s->d;
2836
32.7k
  is[iindex[s->order & 7][PLANE]] = 0;
2837
32.7k
  ie[iindex[s->order & 7][PLANE]] = s->planes - 1;
2838
32.7k
  i = 2;  /* index to innermost loop */
2839
51.1k
  do {
2840
51.1k
    j = 0;  /* carry flag */
2841
51.1k
    if (++s->ii[i] > ie[i]) {
2842
      /* handling overflow of loop variable */
2843
19.3k
      j = 1;
2844
19.3k
      if (i > 0)
2845
18.3k
        s->ii[i] = is[i];
2846
19.3k
    }
2847
51.1k
  } while (--i >= 0 && j);
2848
2849
32.7k
  s->buf_len = 0;
2850
2851
  /* check whether this have been all SDEs */
2852
32.7k
  if (j) {
2853
#ifdef DEBUG
2854
    fprintf(stderr, "This was the final SDE in this BIE, "
2855
      "%ld bytes left.\n", (long) (len - *cnt));
2856
#endif
2857
950
    s->bie_len = 0;
2858
950
    return JBG_EOK;
2859
950
  }
2860
2861
  /* check whether we have to abort because of xmax/ymax */
2862
31.8k
  if (iindex[s->order & 7][LAYER] == 0 && i < 0) {
2863
    /* LAYER is the outermost loop and we have just gone to next layer */
2864
226
    if (jbg_ceil_half(s->xd, s->d - s->ii[0]) > s->xmax ||
2865
226
        jbg_ceil_half(s->yd, s->d - s->ii[0]) > s->ymax) {
2866
75
      s->xmax = 4294967295UL;
2867
75
      s->ymax = 4294967295UL;
2868
75
      return JBG_EOK_INTR;
2869
75
    }
2870
151
    if (s->ii[0] > (unsigned long) s->dmax) {
2871
0
      s->dmax = 256;
2872
0
      return JBG_EOK_INTR;
2873
0
    }
2874
151
  }
2875
2876
31.7k
  break;
2877
39.6k
      }
2878
38.4k
      s->buf_len = 0;
2879
2880
95.7k
    } else if (data[*cnt] == MARKER_ESC)
2881
66.8k
      s->buffer[s->buf_len++] = data[(*cnt)++];
2882
2883
28.8k
    else {
2884
2885
      /* we have found PSCD bytes */
2886
28.8k
      *cnt += decode_pscd(s, data + *cnt, len - *cnt);
2887
28.8k
      if (*cnt < len && data[*cnt] != 0xff) {
2888
#ifdef DEBUG
2889
  fprintf(stderr, "PSCD was longer than expected, unread bytes "
2890
    "%02x %02x %02x %02x ...\n", data[*cnt], data[*cnt+1],
2891
    data[*cnt+2], data[*cnt+3]);
2892
#endif
2893
244
  return JBG_EINVAL | 14;
2894
244
      }
2895
2896
28.8k
    }
2897
162k
  }  /* of BID processing loop 'while (*cnt < len) ...' */
2898
2899
10.9k
  return JBG_EAGAIN;
2900
12.7k
}
2901
2902
2903
/*
2904
 * After jbg_dec_in() returned JBG_EOK or JBG_EOK_INTR, you can call this
2905
 * function in order to find out the width of the image. Returns 0 if
2906
 * there is no image available yet.
2907
 */
2908
unsigned long jbg_dec_getwidth(const struct jbg_dec_state *s)
2909
945
{
2910
945
  if (s->d < 0)
2911
0
    return 0;
2912
945
  if (iindex[s->order & 7][LAYER] == 0) {
2913
81
    if (s->ii[0] < 1)
2914
0
      return 0;
2915
81
    else
2916
81
      return jbg_ceil_half(s->xd, s->d - (s->ii[0] - 1));
2917
81
  }
2918
2919
864
  return s->xd;
2920
945
}
2921
2922
2923
/*
2924
 * After jbg_dec_in() returned JBG_EOK or JBG_EOK_INTR, you can call this
2925
 * function in order to find out the height of the image. Returns 0 if
2926
 * there is no image available yet.
2927
 */
2928
unsigned long jbg_dec_getheight(const struct jbg_dec_state *s)
2929
945
{
2930
945
  if (s->d < 0)
2931
0
    return 0;
2932
945
  if (iindex[s->order & 7][LAYER] == 0) {
2933
81
    if (s->ii[0] < 1)
2934
0
      return 0;
2935
81
    else
2936
81
      return jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1));
2937
81
  }
2938
2939
864
  return s->yd;
2940
945
}
2941
2942
2943
/*
2944
 * After jbg_dec_in() returned JBG_EOK or JBG_EOK_INTR, you can call this
2945
 * function in order to get a pointer to the image. Returns NULL if
2946
 * there is no image available yet.
2947
 */
2948
unsigned char *jbg_dec_getimage(const struct jbg_dec_state *s, int plane)
2949
864
{
2950
864
  if (s->d < 0)
2951
0
    return NULL;
2952
864
  if (iindex[s->order & 7][LAYER] == 0) {
2953
74
    if (s->ii[0] < 1)
2954
0
      return NULL;
2955
74
    else
2956
74
      return s->lhp[(s->ii[0] - 1) & 1][plane];
2957
74
  }
2958
2959
790
  return s->lhp[s->d & 1][plane];
2960
864
}
2961
2962
2963
/*
2964
 * After jbg_dec_in() returned JBG_EOK or JBG_EOK_INTR, you can call
2965
 * this function in order to find out the size in bytes of one
2966
 * bitplane of the image.
2967
 */
2968
unsigned long jbg_dec_getsize(const struct jbg_dec_state *s)
2969
5
{
2970
5
  if (s->d < 0)
2971
0
    return 0;
2972
5
  if (iindex[s->order & 7][LAYER] == 0) {
2973
1
    if (s->ii[0] < 1)
2974
0
      return 0;
2975
1
    else
2976
1
      return
2977
1
  jbg_ceil_half(s->xd, s->d - (s->ii[0] - 1) + 3) * /* overflow risk? */
2978
1
  jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1));
2979
1
  }
2980
2981
4
  return jbg_ceil_half(s->xd, 3) * s->yd;
2982
5
}
2983
2984
2985
/*
2986
 * After jbg_dec_in() returned JBG_EOK or JBG_EOK_INTR, you can call
2987
 * this function in order to find out the size of the image that you
2988
 * can retrieve with jbg_merge_planes().
2989
 */
2990
unsigned long jbg_dec_getsize_merged(const struct jbg_dec_state *s)
2991
0
{
2992
0
  if (s->d < 0)
2993
0
    return 0;
2994
0
  if (iindex[s->order & 7][LAYER] == 0) {
2995
0
    if (s->ii[0] < 1)
2996
0
      return 0;
2997
0
    else
2998
0
      return
2999
0
  jbg_ceil_half(s->xd, s->d - (s->ii[0] - 1)) * /* overflow risk? */
3000
0
  jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1)) *
3001
0
  ((s->planes + 7) / 8);
3002
0
  }
3003
3004
0
  return s->xd * s->yd * ((s->planes + 7) / 8);
3005
0
}
3006
3007
3008
/*
3009
 * The destructor function which releases any resources obtained by the
3010
 * other decoder functions.
3011
 */
3012
void jbg_dec_free(struct jbg_dec_state *s)
3013
4.82k
{
3014
4.82k
  int i;
3015
3016
4.82k
  if (s->d < 0 || s->s == NULL)
3017
614
    return;
3018
4.20k
  s->d = -2;
3019
3020
167k
  for (i = 0; i < s->planes; i++) {
3021
163k
    checked_free(s->s[i]);
3022
163k
    checked_free(s->tx[i]);
3023
163k
    checked_free(s->ty[i]);
3024
163k
    checked_free(s->reset[i]);
3025
163k
    checked_free(s->lntp[i]);
3026
163k
    checked_free(s->lhp[0][i]);
3027
163k
    checked_free(s->lhp[1][i]);
3028
163k
  }
3029
3030
4.20k
  checked_free(s->s);
3031
4.20k
  checked_free(s->tx);
3032
4.20k
  checked_free(s->ty);
3033
4.20k
  checked_free(s->reset);
3034
4.20k
  checked_free(s->lntp);
3035
4.20k
  checked_free(s->lhp[0]);
3036
4.20k
  checked_free(s->lhp[1]);
3037
4.20k
  if (s->dppriv && s->dppriv != jbg_dptable)
3038
100
    checked_free(s->dppriv);
3039
3040
4.20k
  s->s = NULL;
3041
3042
4.20k
  return;
3043
4.82k
}
3044
3045
3046
/*
3047
 * Split bigendian integer pixel field into separate bit planes. In the
3048
 * src array, every pixel is represented by a ((has_planes + 7) / 8) byte
3049
 * long word, most significant byte first. While has_planes describes
3050
 * the number of used bits per pixel in the source image, encode_plane
3051
 * is the number of most significant bits among those that we
3052
 * actually transfer to dest.
3053
 */
3054
void jbg_split_planes(unsigned long x, unsigned long y, int has_planes,
3055
          int encode_planes,
3056
          const unsigned char *src, unsigned char **dest,
3057
          int use_graycode)
3058
0
{
3059
0
  unsigned long bpl = jbg_ceil_half(x, 3);  /* bytes per line in dest plane */
3060
0
  unsigned long line, i;
3061
0
  unsigned k = 8;
3062
0
  int p;
3063
0
  unsigned prev;     /* previous *src byte shifted by 8 bit to the left */
3064
0
  register int bits, msb = has_planes - 1;
3065
0
  int bitno;
3066
3067
  /* sanity checks */
3068
0
  if (encode_planes > has_planes)
3069
0
    encode_planes = has_planes;
3070
0
  use_graycode = use_graycode != 0 && encode_planes > 1;
3071
3072
0
  for (p = 0; p < encode_planes; p++)
3073
0
    memset(dest[p], 0, bpl * y);
3074
3075
0
  for (line = 0; line < y; line++) {                 /* lines loop */
3076
0
    for (i = 0; i * 8 < x; i++) {                    /* dest bytes loop */
3077
0
      for (k = 0; k < 8 && i * 8 + k < x; k++) {     /* pixel loop */
3078
0
  prev = 0;
3079
0
  for (p = 0; p < encode_planes; p++) {        /* bit planes loop */
3080
    /* calculate which bit in *src do we want */
3081
0
    bitno = (msb - p) & 7;
3082
    /* put this bit with its left neighbor right adjusted into bits */
3083
0
    bits = (prev | *src) >> bitno;
3084
    /* go to next *src byte, but keep old */
3085
0
    if (bitno == 0)
3086
0
      prev = *src++ << 8;
3087
    /* make space for inserting new bit */
3088
0
    dest[p][bpl * line + i] <<= 1;
3089
    /* insert bit, if requested apply Gray encoding */
3090
0
    dest[p][bpl * line + i] |= (bits ^ (use_graycode & (bits>>1))) & 1;
3091
    /*
3092
     * Theorem: Let b(n),...,b(1),b(0) be the digits of a
3093
     * binary word and let g(n),...,g(1),g(0) be the digits of the
3094
     * corresponding Gray code word, then g(i) = b(i) xor b(i+1).
3095
     */
3096
0
  }
3097
  /* skip unused *src bytes */
3098
0
  for (;p < has_planes; p++)
3099
0
    if (((msb - p) & 7) == 0)
3100
0
      src++;
3101
0
      }
3102
0
    }
3103
0
    for (p = 0; p < encode_planes; p++)              /* right padding loop */
3104
0
      dest[p][bpl * (line + 1) - 1] <<= 8 - k;
3105
0
  }
3106
3107
0
  return;
3108
0
}
3109
3110
/*
3111
 * Merge the separate bit planes decoded by the JBIG decoder into an
3112
 * integer pixel field. This is essentially the counterpart to
3113
 * jbg_split_planes().
3114
 */
3115
void jbg_dec_merge_planes(const struct jbg_dec_state *s, int use_graycode,
3116
        void (*data_out)(unsigned char *start, size_t len,
3117
             void *file), void *file)
3118
0
{
3119
0
#define BUFLEN 4096
3120
0
  unsigned long bpl, line, i;
3121
0
  unsigned k = 8;
3122
0
  int p;
3123
0
  unsigned char buf[BUFLEN];
3124
0
  unsigned char *bp = buf;
3125
0
  unsigned char **src;
3126
0
  unsigned long x, y;
3127
0
  unsigned v;
3128
3129
  /* sanity check */
3130
0
  use_graycode = use_graycode != 0;
3131
3132
0
  x = jbg_dec_getwidth(s);
3133
0
  y = jbg_dec_getheight(s);
3134
0
  if (x == 0 || y == 0)
3135
0
    return;
3136
0
  bpl = jbg_ceil_half(x, 3);   /* bytes per line in src plane */
3137
3138
0
  if (iindex[s->order & 7][LAYER] == 0)
3139
0
    if (s->ii[0] < 1)
3140
0
      return;
3141
0
    else
3142
0
      src = s->lhp[(s->ii[0] - 1) & 1];
3143
0
  else
3144
0
    src = s->lhp[s->d & 1];
3145
3146
0
  for (line = 0; line < y; line++) {                    /* lines loop */
3147
0
    for (i = 0; i * 8 < x; i++) {                       /* src bytes loop */
3148
0
      for (k = 0; k < 8 && i * 8 + k < x; k++) {        /* pixel loop */
3149
0
  v = 0;
3150
0
  for (p = 0; p < s->planes;) {                   /* dest bytes loop */
3151
0
    do {
3152
0
      v = (v << 1) |
3153
0
        (((src[p][bpl * line + i] >> (7 - k)) & 1) ^
3154
0
         (use_graycode & v));
3155
0
    } while ((s->planes - ++p) & 7);
3156
0
    *bp++ = v;
3157
0
    if (bp - buf == BUFLEN) {
3158
0
      data_out(buf, BUFLEN, file);
3159
0
      bp = buf;
3160
0
    }
3161
0
  }
3162
0
      }
3163
0
    }
3164
0
  }
3165
3166
0
  if (bp - buf > 0)
3167
0
    data_out(buf, bp - buf, file);
3168
3169
0
  return;
3170
0
}
3171
3172
3173
/*
3174
 * Given a pointer p to the first byte of either a marker segment or a
3175
 * PSCD, as well as the length len of the remaining data, return
3176
 * either the pointer to the first byte of the next marker segment or
3177
 * PSCD, or p+len if this was the last one, or NULL if some error was
3178
 * encountered. Possible errors are:
3179
 *
3180
 *  - not enough bytes left for complete marker segment
3181
 *  - no marker segment terminates the PSCD
3182
 *  - unknown marker code encountered
3183
 *
3184
 */
3185
unsigned char *jbg_next_pscdms(unsigned char *p, size_t len)
3186
26.5k
{
3187
26.5k
  unsigned char *pp;
3188
26.5k
  unsigned long l;
3189
3190
26.5k
  if (len < 2)
3191
20
    return NULL; /* not enough bytes left for complete marker segment */
3192
3193
26.4k
  if (p[0] != MARKER_ESC || p[1] == MARKER_STUFF) {
3194
28.7k
    do {
3195
69.0k
      while (p[0] == MARKER_ESC && p[1] == MARKER_STUFF) {
3196
40.2k
  p += 2;
3197
40.2k
  len -= 2;
3198
40.2k
  if (len < 2)
3199
31
    return NULL; /* not enough bytes left for complete marker segment */
3200
40.2k
      }
3201
28.7k
      assert(len >= 2);
3202
28.7k
      pp = (unsigned char *) memchr(p, MARKER_ESC, len - 1);
3203
28.7k
      if (!pp)
3204
937
  return NULL; /* no marker segment terminates the PSCD */
3205
27.8k
      l = pp - p;
3206
27.8k
      assert(l < len);
3207
27.8k
      p += l;
3208
27.8k
      len -= l;
3209
27.8k
    } while (p[1] == MARKER_STUFF);
3210
13.4k
  } else {
3211
13.4k
    switch (p[1]) {
3212
7.59k
    case MARKER_SDNORM:
3213
11.5k
    case MARKER_SDRST:
3214
11.5k
    case MARKER_ABORT:
3215
11.5k
      return p + 2;
3216
3
    case MARKER_NEWLEN:
3217
3
      if (len < 6)
3218
0
  return NULL; /* not enough bytes left for complete marker segment */
3219
3
      return p + 6;
3220
1.21k
    case MARKER_ATMOVE:
3221
1.21k
      if (len < 8)
3222
4
  return NULL; /* not enough bytes left for complete marker segment */
3223
1.20k
      return p + 8;
3224
374
    case MARKER_COMMENT:
3225
374
      if (len < 6)
3226
3
  return NULL; /* not enough bytes left for complete marker segment */
3227
371
      l = (((long) p[2] << 24) | ((long) p[3] << 16) |
3228
371
     ((long) p[4] <<  8) |  (long) p[5]);
3229
371
      if (len - 6 < l)
3230
23
  return NULL; /* not enough bytes left for complete marker segment */
3231
348
      return p + 6 + l;
3232
333
    default:
3233
      /* unknown marker sequence encountered */
3234
333
      return NULL;
3235
13.4k
    }
3236
13.4k
  }
3237
3238
12.0k
  return p;
3239
26.4k
}
3240
3241
3242
/*
3243
 * Scan a complete BIE for a NEWLEN marker segment, then read the new
3244
 * YD value found in it and use it to overwrite the one in the BIE
3245
 * header. Use this procedure if a BIE initially declares an
3246
 * unreasonably high provisional YD value (e.g., 0xffffffff) or
3247
 * depends on the fact that section 6.2.6.2 of ITU-T T.82 says that a
3248
 * NEWLEN marker segment "could refer to a line in the immediately
3249
 * preceding stripe due to an unexpected termination of the image or
3250
 * the use of only such stripe". ITU-T.85 explicitely suggests the
3251
 * use of this for fax machines that start transmission before having
3252
 * encountered the end of the page. None of this is necessary for
3253
 * BIEs produced by JBIG-KIT, which normally does not use NEWLEN.
3254
 */
3255
int jbg_newlen(unsigned char *bie, size_t len)
3256
1.45k
{
3257
1.45k
  unsigned char *p = bie + 20;
3258
1.45k
  int i;
3259
1.45k
  unsigned long y, yn;
3260
3261
1.45k
  if (len < 20)
3262
18
    return JBG_EAGAIN;
3263
1.43k
  if ((bie[19] & (JBG_DPON | JBG_DPPRIV | JBG_DPLAST))
3264
1.43k
      == (JBG_DPON | JBG_DPPRIV))
3265
45
    p += 1728; /* skip DPTABLE */
3266
1.43k
  if (p >= bie + len)
3267
46
    return JBG_EAGAIN;
3268
3269
26.5k
  while ((p = jbg_next_pscdms(p, len - (p - bie)))) {
3270
25.1k
    if (p == bie + len)
3271
4
      return JBG_EOK;
3272
25.1k
    else if (p[0] == MARKER_ESC)
3273
16.5k
      switch (p[1]) {
3274
24
      case MARKER_NEWLEN:
3275
24
        if (p + 5 >= bie + len)
3276
0
          return JBG_EAGAIN;
3277
24
  y = (((long) bie[ 8] << 24) | ((long) bie[ 9] << 16) |
3278
24
       ((long) bie[10] <<  8) |  (long) bie[11]);
3279
24
  yn = (((long) p[2] << 24) | ((long) p[3] << 16) |
3280
24
        ((long) p[4] <<  8) |  (long) p[5]);
3281
24
  if (yn > y) return JBG_EINVAL | 12;
3282
  /* overwrite YD in BIH with YD from NEWLEN */
3283
35
  for (i = 0; i < 4; i++) {
3284
28
    bie[8+i] = p[2+i];
3285
28
  }
3286
7
  return JBG_EOK;
3287
7
      case MARKER_ABORT:
3288
7
  return JBG_EABORT;
3289
16.5k
      }
3290
25.1k
  }
3291
1.35k
  return JBG_EINVAL | 0;
3292
1.38k
}