Coverage Report

Created: 2025-08-11 08:01

/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
843
#define MX_MAX  127    /* maximal supported mx offset for
43
      * adaptive template in the encoder */
44
45
44.9M
#define TPB2CX  0x195  /* contexts for TP special pixels */
46
252M
#define TPB3CX  0x0e5
47
20.4M
#define TPDCX   0xc3f
48
49
/* marker codes */
50
127k
#define MARKER_STUFF    0x00
51
#define MARKER_RESERVE  0x01
52
126k
#define MARKER_SDNORM   0x02
53
138k
#define MARKER_SDRST    0x03
54
12.2k
#define MARKER_ABORT    0x04
55
592
#define MARKER_NEWLEN   0x05
56
18.3k
#define MARKER_ATMOVE   0x06
57
1.89k
#define MARKER_COMMENT  0x07
58
512k
#define MARKER_ESC      0xff
59
60
/* loop array indices */
61
283k
#define STRIPE  0
62
356k
#define LAYER   1
63
319k
#define PLANE   2
64
65
/* special jbg_buf pointers (instead of NULL) */
66
217k
#define SDE_DONE ((struct jbg_buf *) -1)
67
288k
#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.34M
#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.34M
{
131
1.34M
  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.34M
  if (size > SIZE_MAX / nmemb)
139
0
    abort();
140
141
1.34M
  p = malloc(nmemb * size);
142
143
1.34M
  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.34M
  return p;
152
1.34M
}
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.34M
{
183
1.34M
  free(ptr);
184
185
#if 0
186
  fprintf(stderr, "free(%p)\n", ptr);
187
#endif
188
189
1.34M
}
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
81.4k
{
214
81.4k
  struct jbg_buf *new_block;
215
216
  /* Test whether a block from the free list is available */
217
81.4k
  if (*free_list) {
218
26.6k
    new_block = *free_list;
219
26.6k
    *free_list = new_block->next;
220
54.7k
  } else {
221
    /* request a new memory block */
222
54.7k
    new_block = (struct jbg_buf *) checked_malloc(1, sizeof(struct jbg_buf));
223
54.7k
  }
224
81.4k
  new_block->len = 0;
225
81.4k
  new_block->next = NULL;
226
81.4k
  new_block->previous = NULL;
227
81.4k
  new_block->last = new_block;
228
81.4k
  new_block->free_list = free_list;
229
230
81.4k
  return new_block;
231
81.4k
}
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
843
{
240
843
  struct jbg_buf *tmp;
241
242
55.6k
  while (*free_list) {
243
54.7k
    tmp = (*free_list)->next;
244
54.7k
    checked_free(*free_list);
245
54.7k
    *free_list = tmp;
246
54.7k
  }
247
248
843
  return;
249
843
}
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
74.1M
{
261
74.1M
  struct jbg_buf *now;
262
263
74.1M
  now = ((struct jbg_buf *) head)->last;
264
74.1M
  if (now->len < JBG_BUFSIZE - 1) {
265
74.1M
    now->d[now->len++] = b;
266
74.1M
    return;
267
74.1M
  }
268
7.34k
  now->next = jbg_buf_init(((struct jbg_buf *) head)->free_list);
269
7.34k
  now->next->previous = now;
270
7.34k
  now->next->d[now->next->len++] = b;
271
7.34k
  ((struct jbg_buf *) head)->last = now->next;
272
273
7.34k
  return;
274
74.1M
}
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
72.5k
{
286
72.5k
  struct jbg_buf *last;
287
288
72.5k
  while (1) {
289
    /* remove trailing 0x00 in last block of list until this block is empty */
290
72.5k
    last = head->last;
291
81.2k
    while (last->len && last->d[last->len - 1] == 0)
292
8.68k
      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
72.5k
    if (last->previous && !last->len) {
296
9
      head->last->next = *head->free_list;
297
9
      *head->free_list = head->last;
298
9
      head->last = last->previous;
299
9
      head->last->next = NULL;
300
9
    } else
301
72.5k
      break;
302
72.5k
  }
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
72.5k
  if (head->last->len && head->last->d[head->last->len - 1] == MARKER_ESC)
310
135
    jbg_buf_write(MARKER_STUFF, head);
311
312
72.5k
  return;
313
72.5k
}
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.55k
{
323
1.55k
  new_prefix->last->next = *start;
324
1.55k
  new_prefix->last->next->previous = new_prefix->last;
325
1.55k
  new_prefix->last = new_prefix->last->next->last;
326
1.55k
  *start = new_prefix;
327
328
1.55k
  return;
329
1.55k
}
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
72.5k
{
343
72.5k
  struct jbg_buf *tmp;
344
345
153k
  while (*head) {
346
81.4k
    data_out((*head)->d, (*head)->len, file);
347
81.4k
    tmp = (*head)->next;
348
81.4k
    (*head)->next = *(*head)->free_list;
349
81.4k
    *(*head)->free_list = *head;
350
81.4k
    *head = tmp;
351
81.4k
  }
352
353
72.5k
  return;
354
72.5k
}
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.55M
{
366
1.55M
  unsigned long mask;
367
368
1.55M
  assert(n >= 0 && n < 32);
369
1.55M
  mask = (1UL << n) - 1;     /* the lowest n bits are 1 here */
370
1.55M
  return (x >> n) + ((mask & x) != 0);
371
1.55M
}
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.68k
{
382
1.68k
  s->l0 = jbg_ceil_half(s->yd, s->d) / 35;   /* 35 stripes/image */
383
1.68k
  while ((s->l0 << s->d) > 128)              /* but <= 128 lines/stripe */
384
0
    --s->l0;
385
1.68k
  if (s->l0 < 2) s->l0 = 2;
386
1.68k
}
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.23k
{
395
5.23k
  unsigned long y0 = jbg_ceil_half(yd, d);
396
397
5.23k
  return y0 / l0 + (y0 % l0 != 0);
398
5.23k
}
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,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0,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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,2,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
843
{
775
843
  unsigned long l, lx;
776
843
  int i;
777
778
843
  assert(x > 0 && y > 0 && planes > 0 && planes < 256);
779
843
  s->xd = x;
780
843
  s->yd = y;
781
843
  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
843
  s->planes = planes;
785
843
  s->data_out = data_out;
786
843
  s->file = file;
787
788
843
  s->d = 0;
789
843
  s->dl = 0;
790
843
  s->dh = s->d;
791
843
  jbg_set_default_l0(s);
792
843
  s->mx = 8;
793
843
  s->my = 0;
794
843
  s->order = JBG_ILEAVE | JBG_SMID;
795
843
  s->options = JBG_TPBON | JBG_TPDON | JBG_DPON;
796
843
  s->comment = NULL;
797
843
  s->dppriv = jbg_dptable;
798
843
  s->res_tab = jbg_resred;
799
800
843
  s->highres = (int *) checked_malloc(planes, sizeof(int));
801
843
  s->lhp[0] = p;
802
843
  s->lhp[1] = (unsigned char **)
803
843
    checked_malloc(planes, sizeof(unsigned char *));
804
1.68k
  for (i = 0; i < planes; i++) {
805
843
    s->highres[i] = 0;
806
843
    s->lhp[1][i] = (unsigned char *)
807
843
      checked_malloc(jbg_ceil_half(y, 1), jbg_ceil_half(x, 1+3));
808
843
  }
809
810
843
  s->free_list = NULL;
811
843
  s->s = (struct jbg_arenc_state *)
812
843
    checked_malloc(s->planes, sizeof(struct jbg_arenc_state));
813
843
  s->tx = (int *) checked_malloc(s->planes, sizeof(int));
814
843
  lx = jbg_ceil_half(x, 1);
815
843
  s->tp = (char *) checked_malloc(lx, sizeof(char));
816
408k
  for (l = 0; l < lx; s->tp[l++] = 2) ;
817
843
  s->sde = NULL;
818
819
843
  return;
820
843
}
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
843
{
838
2.16k
  for (s->d = 0; s->d < 6; s->d++)
839
2.16k
    if (jbg_ceil_half(s->xd, s->d) <= x && jbg_ceil_half(s->yd, s->d) <= y)
840
843
      break;
841
843
  s->dl = 0;
842
843
  s->dh = s->d;
843
843
  jbg_set_default_l0(s);
844
843
  return s->d;
845
843
}
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
843
{
874
843
  if (dl >= 0     && dl <= s->d) s->dl = dl;
875
843
  if (dh >= s->dl && dh <= s->d) s->dh = dh;
876
877
843
  return s->d;
878
843
}
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
843
{
889
843
  if (order >= 0 && order <= 0x0f) s->order = order;
890
843
  if (options >= 0) s->options = options;
891
843
  if (l0 > 0) s->l0 = l0;
892
843
  if (mx >= 0 && mx < 128) s->mx = mx;
893
843
  if (my >= 0 && my < 256) s->my = my;
894
895
843
  return;
896
843
}
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
72.5k
{
907
72.5k
  unsigned char *hp, *lp1, *lp2, *p0, *p1, *q1, *q2;
908
72.5k
  unsigned long hl, ll, hx, hy, lx, ly, hbpl, lbpl;
909
72.5k
  unsigned long line_h0 = 0, line_h1 = 0;
910
72.5k
  unsigned long line_h2, line_h3, line_l1, line_l2, line_l3;
911
72.5k
  struct jbg_arenc_state *se;
912
72.5k
  unsigned long y;  /* current line number in highres image */
913
72.5k
  unsigned long i;  /* current line number within highres stripe */
914
72.5k
  unsigned long j;  /* current column number in highres image */
915
72.5k
  long o;
916
72.5k
  unsigned a, p, t;
917
72.5k
  int ltp, ltp_old, cx;
918
72.5k
  unsigned long c_all, c[MX_MAX + 1], cmin, cmax, clmin, clmax;
919
72.5k
  int tmax, at_determined;
920
72.5k
  int new_tx;
921
72.5k
  long new_tx_line = -1;
922
72.5k
  int reset;
923
72.5k
  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
72.5k
  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
72.5k
  hl = s->l0 << layer;
943
  /* number of lines per stripe in lowres image */
944
72.5k
  ll = hl >> 1;
945
  /* current line number in highres image */
946
72.5k
  y = stripe * hl;
947
  /* number of pixels in highres image */
948
72.5k
  hx = jbg_ceil_half(s->xd, s->d - layer);
949
72.5k
  hy = jbg_ceil_half(s->yd, s->d - layer);
950
  /* number of pixels in lowres image */
951
72.5k
  lx = jbg_ceil_half(hx, 1);
952
72.5k
  ly = jbg_ceil_half(hy, 1);
953
  /* bytes per line in highres and lowres image */
954
72.5k
  hbpl = jbg_ceil_half(hx, 3);
955
72.5k
  lbpl = jbg_ceil_half(lx, 3);
956
  /* pointer to first image byte of highres stripe */
957
72.5k
  hp = s->lhp[s->highres[plane]][plane] + stripe * hl * hbpl;
958
72.5k
  lp2 = s->lhp[1 - s->highres[plane]][plane] + stripe * ll * lbpl;
959
72.5k
  lp1 = lp2 + lbpl;
960
961
  /* check whether we can refer to any state of a previous stripe */
962
72.5k
  reset = (stripe == 0) || (s->options & JBG_SDRST);
963
964
  /* initialize arithmetic encoder */
965
72.5k
  se = s->s + plane;
966
72.5k
  arith_encode_init(se, !reset);
967
72.5k
  s->sde[stripe][layer][plane] = jbg_buf_init(&s->free_list);
968
72.5k
  se->byte_out = jbg_buf_write;
969
72.5k
  se->file = s->sde[stripe][layer][plane];
970
971
  /* initialize adaptive template movement algorithm */
972
72.5k
  c_all = 0;
973
725k
  for (t = 0; t <= s->mx; t++)
974
652k
    c[t] = 0;
975
72.5k
  if (stripe == 0)    /* the SDRST case is handled at the end */
976
2.16k
    s->tx[plane] = 0;
977
72.5k
  new_tx = -1;
978
72.5k
  at_determined = 0;  /* we haven't yet decided the template move */
979
72.5k
  if (s->mx == 0)
980
0
    at_determined = 1;
981
982
  /* initialize typical prediction */
983
72.5k
  ltp = 0;
984
72.5k
  if (reset)
985
2.16k
    ltp_old = 0;
986
70.3k
  else {
987
70.3k
    ltp_old = 1;
988
70.3k
    p1 = hp - hbpl;
989
70.3k
    if (y > 1) {
990
70.3k
      q1 = p1 - hbpl;
991
823k
      while (p1 < hp && (ltp_old = (*p1++ == *q1++)) != 0) ;
992
70.3k
    } else
993
0
      while (p1 < hp && (ltp_old = (*p1++ == 0)) != 0) ;
994
70.3k
  }
995
996
72.5k
  if (layer == 0) {
997
998
    /*
999
     *  Encode lowest resolution layer
1000
     */
1001
1002
262k
    for (i = 0; i < hl && y < hy; i++, y++) {
1003
1004
      /* check whether it is worth to perform an ATMOVE */
1005
235k
      if (!at_determined && c_all > 2048) {
1006
12.7k
  cmin = clmin = 0xffffffffL;
1007
12.7k
  cmax = clmax = 0;
1008
12.7k
  tmax = 0;
1009
89.0k
  for (t = (s->options & JBG_LRLTWO) ? 5 : 3; t <= s->mx; t++) {
1010
76.3k
    if (c[t] > cmax) cmax = c[t];
1011
76.3k
    if (c[t] < cmin) cmin = c[t];
1012
76.3k
    if (c[t] > c[tmax]) tmax = t;
1013
76.3k
  }
1014
12.7k
  clmin = (c[0] < cmin) ? c[0] : cmin;
1015
12.7k
  clmax = (c[0] > cmax) ? c[0] : cmax;
1016
12.7k
  if (c_all - cmax < (c_all >> 3) &&
1017
12.7k
      cmax - c[s->tx[plane]] > c_all - cmax &&
1018
12.7k
      cmax - c[s->tx[plane]] > (c_all >> 4) &&
1019
      /*                     ^ T.82 said < here, fixed in Cor.1/25 */
1020
12.7k
      cmax - (c_all - c[s->tx[plane]]) > c_all - cmax &&
1021
12.7k
      cmax - (c_all - c[s->tx[plane]]) > (c_all >> 4) &&
1022
12.7k
      cmax - cmin > (c_all >> 2) &&
1023
12.7k
      (s->tx[plane] || clmax - clmin > (c_all >> 3))) {
1024
    /* we have decided to perform an ATMOVE */
1025
391
    new_tx = tmax;
1026
391
    if (!(s->options & JBG_DELAY_AT)) {
1027
391
      new_tx_line = i;
1028
391
      s->tx[plane] = new_tx;
1029
391
    }
1030
#ifdef DEBUG
1031
    fprintf(stderr, "ATMOVE: line=%ld, tx=%d, c_all=%ld\n",
1032
      i, new_tx, c_all);
1033
#endif
1034
391
  }
1035
12.7k
  at_determined = 1;
1036
12.7k
      }
1037
235k
      assert(s->tx[plane] >= 0); /* i.e., tx can safely be cast to unsigned */
1038
1039
      /* typical prediction */
1040
235k
      if (s->options & JBG_TPBON) {
1041
235k
  ltp = 1;
1042
235k
  p1 = hp;
1043
235k
  if (i > 0 || !reset) {
1044
234k
    q1 = hp - hbpl;
1045
1.38M
    while (q1 < hp && (ltp = (*p1++ == *q1++)) != 0) ;
1046
234k
  } else
1047
5.08k
    while (p1 < hp + hbpl && (ltp = (*p1++ == 0)) != 0) ;
1048
235k
  arith_encode(se, (s->options & JBG_LRLTWO) ? TPB2CX : TPB3CX,
1049
235k
         ltp == ltp_old);
1050
#ifdef DEBUG
1051
  tp_lines += ltp;
1052
#endif
1053
235k
  ltp_old = ltp;
1054
235k
  if (ltp) {
1055
    /* skip next line */
1056
24.6k
    hp += hbpl;
1057
24.6k
    continue;
1058
24.6k
  }
1059
235k
      }
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
211k
      line_h1 = line_h2 = line_h3 = 0;
1071
211k
      if (i > 0 || !reset) line_h2 = (long)*(hp - hbpl) << 8;
1072
211k
      if (i > 1 || !reset) line_h3 = (long)*(hp - hbpl - hbpl) << 8;
1073
1074
      /* encode line */
1075
8.15M
      for (j = 0; j < hx; hp++) {
1076
7.94M
  line_h1 |= *hp;
1077
7.94M
  if (j < hbpl * 8 - 8 && (i > 0 || !reset)) {
1078
7.70M
    line_h2 |= *(hp - hbpl + 1);
1079
7.70M
    if (i > 1 || !reset)
1080
7.68M
      line_h3 |= *(hp - hbpl - hbpl + 1);
1081
7.70M
  }
1082
7.94M
  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
7.94M
  } else {
1127
    /* three line template */
1128
63.0M
    do {
1129
63.0M
      line_h1 <<= 1;  line_h2 <<= 1;  line_h3 <<= 1;
1130
63.0M
      if (s->tx[plane]) {
1131
13.9M
        if ((unsigned) s->tx[plane] > j)
1132
176k
    a = 0;
1133
13.7M
        else {
1134
13.7M
    o = (j - s->tx[plane]) - (j & ~7L);
1135
13.7M
    a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
1136
13.7M
    a <<= 2;
1137
13.7M
        }
1138
13.9M
        assert(s->tx[plane] > 23 ||
1139
13.9M
         a == ((line_h1 >> (6 + s->tx[plane])) & 0x004));
1140
13.9M
        arith_encode(se, (((line_h3 >>  8) & 0x380) |
1141
13.9M
        ((line_h2 >> 12) & 0x078) | a |
1142
13.9M
        ((line_h1 >>  9) & 0x003)),
1143
13.9M
         (line_h1 >> 8) & 1);
1144
13.9M
      } else
1145
49.1M
        arith_encode(se, (((line_h3 >>  8) & 0x380) |
1146
49.1M
        ((line_h2 >> 12) & 0x07c) |
1147
49.1M
        ((line_h1 >>  9) & 0x003)),
1148
49.1M
         (line_h1 >> 8) & 1);
1149
#ifdef DEBUG
1150
      encoded_pixels++;
1151
#endif
1152
      /* statistics for adaptive template changes */
1153
63.0M
      if (!at_determined && j >= s->mx && j < hx-2) {
1154
39.8M
        p = (line_h1 & 0x100) != 0; /* current pixel value */
1155
39.8M
        c[0] += ((line_h2 & 0x4000) != 0) == p; /* default position */
1156
39.8M
        assert(!(((line_h2 >> 6) ^ line_h1) & 0x100) ==
1157
39.8M
         (((line_h2 & 0x4000) != 0) == p));
1158
278M
        for (t = 3; t <= s->mx && t <= j; t++) {
1159
238M
    o = (j - t) - (j & ~7L);
1160
238M
    a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
1161
238M
    assert(t > 23 ||
1162
238M
           (a == p) == !(((line_h1 >> t) ^ line_h1) & 0x100));
1163
238M
    c[t] += a == p;
1164
238M
        }
1165
39.8M
        for (; t <= s->mx; t++) {
1166
0
    c[t] += 0 == p;
1167
0
        }
1168
39.8M
        ++c_all;
1169
39.8M
      }
1170
63.0M
    } while (++j & 7 && j < hx);
1171
7.94M
  } /* if (s->options & JBG_LRLTWO) */
1172
7.94M
      } /* for (j = ...) */
1173
211k
    } /* for (i = ...) */
1174
1175
45.3k
  } else {
1176
1177
    /*
1178
     *  Encode differential layer
1179
     */
1180
1181
1.39M
    for (i = 0; i < hl && y < hy; i++, y++) {
1182
1183
      /* check whether it is worth to perform an ATMOVE */
1184
1.34M
      if (!at_determined && c_all > 2048) {
1185
38.1k
  cmin = clmin = 0xffffffffL;
1186
38.1k
  cmax = clmax = 0;
1187
38.1k
  tmax = 0;
1188
267k
  for (t = 3; t <= s->mx; t++) {
1189
228k
    if (c[t] > cmax) cmax = c[t];
1190
228k
    if (c[t] < cmin) cmin = c[t];
1191
228k
    if (c[t] > c[tmax]) tmax = t;
1192
228k
  }
1193
38.1k
  clmin = (c[0] < cmin) ? c[0] : cmin;
1194
38.1k
  clmax = (c[0] > cmax) ? c[0] : cmax;
1195
38.1k
  if (c_all - cmax < (c_all >> 3) &&
1196
38.1k
      cmax - c[s->tx[plane]] > c_all - cmax &&
1197
38.1k
      cmax - c[s->tx[plane]] > (c_all >> 4) &&
1198
      /*                     ^ T.82 said < here, fixed in Cor.1/25 */
1199
38.1k
      cmax - (c_all - c[s->tx[plane]]) > c_all - cmax &&
1200
38.1k
      cmax - (c_all - c[s->tx[plane]]) > (c_all >> 4) &&
1201
38.1k
      cmax - cmin > (c_all >> 2) &&
1202
38.1k
      (s->tx[plane] || clmax - clmin > (c_all >> 3))) {
1203
    /* we have decided to perform an ATMOVE */
1204
1.16k
    new_tx = tmax;
1205
1.16k
    if (!(s->options & JBG_DELAY_AT)) {
1206
1.16k
      new_tx_line = i;
1207
1.16k
      s->tx[plane] = new_tx;
1208
1.16k
    }
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.16k
  }
1214
38.1k
  at_determined = 1;
1215
38.1k
      }
1216
1217
1.34M
      if ((i >> 1) >= ll - 1 || (y >> 1) >= ly - 1)
1218
90.0k
  lp1 = lp2;
1219
1220
      /* typical prediction */
1221
1.34M
      if (s->options & JBG_TPDON && (i & 1) == 0) {
1222
673k
  q1 = lp1; q2 = lp2;
1223
673k
  p0 = p1 = hp;
1224
673k
  if (i < hl - 1 && y < hy - 1)
1225
672k
    p0 = hp + hbpl;
1226
673k
  if (i > 1 || !reset)
1227
672k
    line_l3 = (long)*(q2 - lbpl) << 8;
1228
1.32k
  else
1229
1.32k
    line_l3 = 0;
1230
673k
  line_l2 = (long)*q2 << 8;
1231
673k
  line_l1 = (long)*q1 << 8;
1232
673k
  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.6M
    do {
1241
65.6M
      if ((j >> 2) < hbpl) {
1242
65.6M
        line_h1 = *(p1++);
1243
65.6M
        line_h0 = *(p0++);
1244
65.6M
      }
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
13.0M
    if ((line_h1 & 0x300) == 0 && (line_h0 & 0x300) == 0)
1256
13.0M
      s->tp[j] = 0;
1257
41.0k
    else {
1258
41.0k
      ltp = 0;
1259
#ifdef DEBUG
1260
      tp_exceptions++;
1261
#endif
1262
41.0k
    }
1263
249M
        else if (cx == 0x1ff)
1264
21.9M
    if ((line_h1 & 0x300) == 0x300 && (line_h0 & 0x300) == 0x300)
1265
21.8M
      s->tp[j] = 1;
1266
178k
    else {
1267
178k
      ltp = 0;
1268
#ifdef DEBUG
1269
      tp_exceptions++;
1270
#endif
1271
178k
    }
1272
227M
        else
1273
227M
    s->tp[j] = 2;
1274
262M
      } while (++j & 3 && j < lx);
1275
65.6M
    } while (j & 7 && j < lx);
1276
32.9M
  } /* for (j = ...) */
1277
673k
  arith_encode(se, TPDCX, !ltp);
1278
#ifdef DEBUG
1279
  tp_lines += ltp;
1280
#endif
1281
673k
      }
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.34M
      line_h1 = line_h2 = line_h3 = line_l1 = line_l2 = line_l3 = 0;
1305
1.34M
      if (i > 0 || !reset) line_h2 = (long)*(hp - hbpl) << 8;
1306
1.34M
      if (i > 1 || !reset) {
1307
1.34M
  line_h3 = (long)*(hp - hbpl - hbpl) << 8;
1308
1.34M
  line_l3 = (long)*(lp2 - lbpl) << 8;
1309
1.34M
      }
1310
1.34M
      line_l2 = (long)*lp2 << 8;
1311
1.34M
      line_l1 = (long)*lp1 << 8;
1312
1313
      /* encode line */
1314
82.1M
      for (j = 0; j < hx; lp1++, lp2++) {
1315
80.7M
  if ((j >> 1) < lbpl * 8 - 8) {
1316
79.4M
    if (i > 1 || !reset)
1317
79.2M
      line_l3 |= *(lp2 - lbpl + 1);
1318
79.4M
    line_l2 |= *(lp2 + 1);
1319
79.4M
    line_l1 |= *(lp1 + 1);
1320
79.4M
  }
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
159M
      if (i > 0 || !reset) {
1334
159M
        line_h2 |= *(hp - hbpl + 1);
1335
159M
        if (i > 1 || !reset)
1336
159M
    line_h3 |= *(hp - hbpl - hbpl + 1);
1337
159M
      }
1338
159M
    }
1339
642M
    do { /* ... while (j & 7 && j < hx) */
1340
642M
      line_l1 <<= 1;  line_l2 <<= 1;  line_l3 <<= 1;
1341
642M
      if (ltp && s->tp[j >> 1] < 2) {
1342
        /* pixel are typical and have not to be encoded */
1343
64.1M
        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.1M
        j += 2;
1350
64.1M
#endif
1351
64.1M
      } else
1352
1.15G
        do { /* ... while (++j & 1 && j < hx) */
1353
1.15G
    line_h1 <<= 1;  line_h2 <<= 1;  line_h3 <<= 1;
1354
1355
    /* deterministic prediction */
1356
1.15G
    if (s->options & JBG_DPON) {
1357
1.15G
      if ((y & 1) == 0) {
1358
578M
        if ((j & 1) == 0) {
1359
          /* phase 0 */
1360
289M
          if (s->dppriv[((line_l3 >> 16) & 0x003) |
1361
289M
            ((line_l2 >> 14) & 0x00c) |
1362
289M
            ((line_h1 >> 5)  & 0x010) |
1363
289M
            ((line_h2 >> 10) & 0x0e0)] < 2) {
1364
#ifdef DEBUG
1365
      ++dp_pixels;
1366
#endif
1367
8.31M
      continue;
1368
8.31M
          }
1369
289M
        } else {
1370
          /* phase 1 */
1371
289M
          if (s->dppriv[(((line_l3 >> 16) & 0x003) |
1372
289M
             ((line_l2 >> 14) & 0x00c) |
1373
289M
             ((line_h1 >> 5)  & 0x030) |
1374
289M
             ((line_h2 >> 10) & 0x1c0)) + 256] < 2) {
1375
#ifdef DEBUG
1376
      ++dp_pixels;
1377
#endif
1378
17.1M
      continue;
1379
17.1M
          }
1380
289M
        }
1381
578M
      } else {
1382
578M
        if ((j & 1) == 0) {
1383
          /* phase 2 */
1384
289M
          if (s->dppriv[(((line_l3 >> 16) & 0x003) |
1385
289M
             ((line_l2 >> 14) & 0x00c) |
1386
289M
             ((line_h1 >> 5)  & 0x010) |
1387
289M
             ((line_h2 >> 10) & 0x0e0) |
1388
289M
             ((line_h3 >> 7) & 0x700)) + 768] < 2) {
1389
#ifdef DEBUG
1390
      ++dp_pixels;
1391
#endif
1392
57.2M
      continue;
1393
57.2M
          }
1394
289M
        } else {
1395
          /* phase 3 */
1396
288M
          if (s->dppriv[(((line_l3 >> 16) & 0x003) |
1397
288M
             ((line_l2 >> 14) & 0x00c) |
1398
288M
             ((line_h1 >> 5)  & 0x030) |
1399
288M
             ((line_h2 >> 10) & 0x1c0) |
1400
288M
             ((line_h3 >> 7)  & 0xe00)) + 2816] < 2) {
1401
#ifdef DEBUG
1402
      ++dp_pixels;
1403
#endif
1404
79.6M
      continue;
1405
79.6M
          }
1406
288M
        }
1407
578M
      }
1408
1.15G
    }
1409
1410
    /* determine context */
1411
994M
    if (s->tx[plane]) {
1412
387M
      if ((unsigned) s->tx[plane] > j)
1413
1.66M
        a = 0;
1414
385M
      else {
1415
385M
        o = (j - s->tx[plane]) - (j & ~7L);
1416
385M
        a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
1417
385M
        a <<= 4;
1418
385M
      }
1419
387M
      assert(s->tx[plane] > 23 ||
1420
387M
       a == ((line_h1 >> (4 + s->tx[plane])) & 0x010));
1421
387M
      cx = (((line_h1 >> 9)  & 0x003) | a |
1422
387M
      ((line_h2 >> 13) & 0x00c) |
1423
387M
      ((line_h3 >> 11) & 0x020));
1424
387M
    } else
1425
606M
      cx = (((line_h1 >> 9)  & 0x003) |
1426
606M
      ((line_h2 >> 13) & 0x01c) |
1427
606M
      ((line_h3 >> 11) & 0x020));
1428
994M
    if (j & 1)
1429
481M
      cx |= (((line_l2 >> 9)  & 0x0c0) |
1430
481M
       ((line_l1 >> 7)  & 0x300)) | (1UL << 10);
1431
513M
    else
1432
513M
      cx |= (((line_l2 >> 10) & 0x0c0) |
1433
513M
       ((line_l1 >> 8)  & 0x300));
1434
994M
    cx |= (y & 1) << 11;
1435
1436
994M
    arith_encode(se, cx, (line_h1 >> 8) & 1);
1437
#ifdef DEBUG
1438
    encoded_pixels++;
1439
#endif
1440
1441
    /* statistics for adaptive template changes */
1442
994M
    if (!at_determined && j >= s->mx) {
1443
100M
      c[0] += !(((line_h2 >> 6) ^ line_h1) & 0x100);
1444
704M
      for (t = 3; t <= s->mx; t++)
1445
603M
        c[t] += !(((line_h1 >> t) ^ line_h1) & 0x100);
1446
100M
      ++c_all;
1447
100M
    }
1448
1449
1.15G
        } while (++j & 1 && j < hx);
1450
642M
    } while (j & 7 && j < hx);
1451
0
    hp++;
1452
161M
  } while (j & 15 && j < hx);
1453
80.7M
      } /* for (j = ...) */
1454
1455
      /* low resolution pixels are used twice */
1456
1.34M
      if ((i & 1) == 0) {
1457
673k
  lp1 -= lbpl;
1458
673k
  lp2 -= lbpl;
1459
673k
      }
1460
1461
1.34M
    } /* for (i = ...) */
1462
45.3k
  }
1463
1464
72.5k
  arith_encode_flush(se);
1465
72.5k
  jbg_buf_remove_zeros(s->sde[stripe][layer][plane]);
1466
72.5k
  jbg_buf_write(MARKER_ESC, s->sde[stripe][layer][plane]);
1467
72.5k
  jbg_buf_write((s->options & JBG_SDRST) ? MARKER_SDRST : MARKER_SDNORM,
1468
72.5k
    s->sde[stripe][layer][plane]);
1469
72.5k
  if (s->options & JBG_SDRST)
1470
0
    s->tx[plane] = 0;
1471
1472
  /* add ATMOVE */
1473
72.5k
  if (new_tx != -1) {
1474
1.55k
    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.55k
    } else {
1486
      /* ATMOVE has already become active during this stripe
1487
       * => we have to prefix the SDE data with an ATMOVE marker */
1488
1.55k
      new_jbg_buf = jbg_buf_init(&s->free_list);
1489
1.55k
      jbg_buf_write(MARKER_ESC, new_jbg_buf);
1490
1.55k
      jbg_buf_write(MARKER_ATMOVE, new_jbg_buf);
1491
1.55k
      jbg_buf_write((new_tx_line >> 24) & 0xff, new_jbg_buf);
1492
1.55k
      jbg_buf_write((new_tx_line >> 16) & 0xff, new_jbg_buf);
1493
1.55k
      jbg_buf_write((new_tx_line >> 8) & 0xff, new_jbg_buf);
1494
1.55k
      jbg_buf_write(new_tx_line & 0xff, new_jbg_buf);
1495
1.55k
      jbg_buf_write(new_tx, new_jbg_buf);
1496
1.55k
      jbg_buf_write(0, new_jbg_buf);
1497
1.55k
      jbg_buf_prefix(new_jbg_buf, &s->sde[stripe][layer][plane]);
1498
1.55k
    }
1499
1.55k
  }
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
72.5k
  return;
1509
72.5k
}
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.32k
{
1518
1.32k
  unsigned long hl, ll, hx, hy, lx, ly, hbpl, lbpl;
1519
1.32k
  unsigned char *hp1, *hp2, *hp3, *lp;
1520
1.32k
  unsigned long line_h1, line_h2, line_h3, line_l2;
1521
1.32k
  unsigned long y;  /* current line number in lowres image */
1522
1.32k
  unsigned long i;  /* current line number within lowres stripe */
1523
1.32k
  unsigned long j;  /* current column number in lowres image */
1524
1.32k
  int pix, k, l;
1525
1526
  /* number of lines per stripe in highres image */
1527
1.32k
  hl = s->l0 << higher_layer;
1528
  /* number of lines per stripe in lowres image */
1529
1.32k
  ll = hl >> 1;
1530
  /* number of pixels in highres image */
1531
1.32k
  hx = jbg_ceil_half(s->xd, s->d - higher_layer);
1532
1.32k
  hy = jbg_ceil_half(s->yd, s->d - higher_layer);
1533
  /* number of pixels in lowres image */
1534
1.32k
  lx = jbg_ceil_half(hx, 1);
1535
1.32k
  ly = jbg_ceil_half(hy, 1);
1536
  /* bytes per line in highres and lowres image */
1537
1.32k
  hbpl = jbg_ceil_half(hx, 3);
1538
1.32k
  lbpl = jbg_ceil_half(lx, 3);
1539
  /* pointers to first image bytes */
1540
1.32k
  hp2 = s->lhp[s->highres[plane]][plane];
1541
1.32k
  hp1 = hp2 + hbpl;
1542
1.32k
  hp3 = hp2 - hbpl;
1543
1.32k
  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
46.6k
  for (y = 0; y < ly;) {
1569
718k
    for (i = 0; i < ll && y < ly; i++, y++) {
1570
673k
      if (2*y + 1 >= hy)
1571
592
  hp1 = hp2;
1572
673k
      pix = 0;
1573
673k
      line_h1 = line_h2 = line_h3 = line_l2 = 0;
1574
41.0M
      for (j = 0; j < lbpl * 8; j += 8) {
1575
40.4M
  *lp = 0;
1576
40.4M
  if (i > 0 || (y > 0 && !(s->options & JBG_SDRST)))
1577
40.3M
    line_l2 |= *(lp-lbpl);
1578
120M
  for (k = 0; k < 8 && j + k < lx; k += 4) {
1579
80.5M
    if (((j + k) >> 2) < hbpl) {
1580
80.5M
      if (i > 0 || (y > 0 && !(s->options & JBG_SDRST)))
1581
80.4M
        line_h3 |= *hp3;
1582
80.5M
      ++hp3;
1583
80.5M
      line_h2 |= *(hp2++);
1584
80.5M
      line_h1 |= *(hp1++);
1585
80.5M
    }
1586
402M
    for (l = 0; l < 4 && j + k + l < lx; l++) {
1587
321M
      line_h3 <<= 2;
1588
321M
      line_h2 <<= 2;
1589
321M
      line_h1 <<= 2;
1590
321M
      line_l2 <<= 1;
1591
321M
      pix = s->res_tab[((line_h1 >> 8) & 0x007) |
1592
321M
           ((line_h2 >> 5) & 0x038) |
1593
321M
           ((line_h3 >> 2) & 0x1c0) |
1594
321M
           (pix << 9) | ((line_l2 << 2) & 0xc00)];
1595
321M
      *lp = (*lp << 1) | pix;
1596
321M
    }
1597
80.5M
  }
1598
40.4M
  ++lp;
1599
40.4M
      }
1600
673k
      *(lp - 1) <<= lbpl * 8 - lx;
1601
673k
      hp1 += hbpl;
1602
673k
      hp2 += hbpl;
1603
673k
      hp3 += hbpl;
1604
673k
    }
1605
45.3k
  }
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.32k
  return;
1621
1.32k
}
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
72.5k
{
1642
72.5k
  int lfcl;     /* lowest fully coded layer */
1643
72.5k
  long i;
1644
72.5k
  unsigned long u;
1645
1646
72.5k
  assert(s->sde[stripe][layer][plane] != SDE_DONE);
1647
1648
72.5k
  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.3k
    jbg_buf_output(&s->sde[stripe][layer][plane], s->data_out, s->file);
1654
45.3k
    s->sde[stripe][layer][plane] = SDE_DONE;
1655
45.3k
    return;
1656
45.3k
  }
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.2k
  lfcl = 0;
1663
71.2k
  for (i = s->d; i >= 0; i--)
1664
71.2k
    if (s->sde[s->stripes - 1][i][plane] == SDE_TODO) {
1665
27.2k
      lfcl = i + 1;
1666
27.2k
      break;
1667
27.2k
    }
1668
27.2k
  if (lfcl > s->d && s->d > 0 && stripe == 0) {
1669
    /* perform the first resolution reduction */
1670
688
    resolution_reduction(s, plane, s->d);
1671
688
  }
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.5k
  while (lfcl - 1 > layer) {
1675
46.6k
    for (u = 0; u < s->stripes; u++)
1676
45.3k
      encode_sde(s, u, lfcl - 1, plane);
1677
1.32k
    --lfcl;
1678
1.32k
    s->highres[plane] ^= 1;
1679
1.32k
    if (lfcl > 1)
1680
634
      resolution_reduction(s, plane, lfcl - 1);
1681
1.32k
  }
1682
1683
27.2k
  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.2k
  jbg_buf_output(&s->sde[stripe][layer][plane], s->data_out, s->file);
1689
27.2k
  s->sde[stripe][layer][plane] = SDE_DONE;
1690
1691
27.2k
  if (stripe == s->stripes - 1 && layer > 0 &&
1692
27.2k
      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.2k
  return;
1699
72.5k
}
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
34
{
1760
34
  int i, j, k;
1761
34
  int trans0[ 8] = { 1, 0, 3, 2, 7, 6, 5, 4 };
1762
34
  int trans1[ 9] = { 1, 0, 3, 2, 8, 7, 6, 5, 4 };
1763
34
  int trans2[11] = { 1, 0, 3, 2, 10, 9, 8, 7, 6, 5, 4 };
1764
34
  int trans3[12] = { 1, 0, 3, 2, 11, 10, 9, 8, 7, 6, 5, 4 };
1765
1766
34
#define FILL_TABLE2(offset, len, trans) \
1767
235k
  for (i = 0; i < len; i++) { \
1768
235k
    k = 0; \
1769
2.66M
    for (j = 0; i >> j; j++) \
1770
2.42M
      k |= ((i >> j) & 1) << trans[j]; \
1771
235k
    internal[k + offset] = \
1772
235k
      (dptable[(i + offset) >> 2] >> ((3 - (i & 3)) << 1)) & 3; \
1773
235k
  }
1774
1775
34
  FILL_TABLE2(   0,  256, trans0);
1776
34
  FILL_TABLE2( 256,  512, trans1);
1777
34
  FILL_TABLE2( 768, 2048, trans2);
1778
34
  FILL_TABLE2(2816, 4096, trans3);
1779
1780
34
  return;
1781
34
}
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
843
{
1790
843
  unsigned long bpl;
1791
843
  unsigned char buf[20];
1792
843
  unsigned long xd, yd, y;
1793
843
  long ii[3], is[3], ie[3];    /* generic variables for the 3 nested loops */
1794
843
  unsigned long stripe;
1795
843
  int layer, plane;
1796
843
  int order;
1797
843
  unsigned char dpbuf[1728];
1798
1799
  /* some sanity checks */
1800
843
  s->order &= JBG_HITOLO | JBG_SEQ | JBG_ILEAVE | JBG_SMID;
1801
843
  order = s->order & (JBG_SEQ | JBG_ILEAVE | JBG_SMID);
1802
843
  if (iindex[order][0] < 0)
1803
0
    s->order = order = JBG_SMID | JBG_ILEAVE;
1804
843
  if (s->options & JBG_DPON && s->dppriv != jbg_dptable)
1805
0
    s->options |= JBG_DPPRIV;
1806
843
  if (s->mx > MX_MAX)
1807
0
    s->mx = MX_MAX;
1808
843
  s->my = 0;
1809
843
  if (s->mx && s->mx < ((s->options & JBG_LRLTWO) ? 5U : 3U))
1810
0
    s->mx = 0;
1811
843
  if (s->d > 255 || s->d < 0 || s->dh > s->d || s->dh < 0 ||
1812
843
      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
843
  if (s->d > 31 || (s->d != 0 && s->l0 >= (1UL << (32 - s->d))))
1816
0
    return;
1817
843
  if (s->yd1 < s->yd)
1818
0
    s->yd1 = s->yd;
1819
843
  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
843
  if (s->xd & 7) {
1824
641
    bpl = jbg_ceil_half(s->xd, 3);     /* bytes per line */
1825
1.28k
    for (plane = 0; plane < s->planes; plane++)
1826
715k
      for (y = 0; y < s->yd; y++)
1827
714k
  s->lhp[0][plane][y * bpl + bpl - 1] &= ~((1 << (8 - (s->xd & 7))) - 1);
1828
641
  }
1829
1830
  /* prepare BIH */
1831
843
  buf[0] = s->dl;
1832
843
  buf[1] = s->dh;
1833
843
  buf[2] = s->planes;
1834
843
  buf[3] = 0;
1835
843
  xd = jbg_ceil_half(s->xd, s->d - s->dh);
1836
843
  yd = jbg_ceil_half(s->yd1, s->d - s->dh);
1837
843
  buf[4] = xd >> 24;
1838
843
  buf[5] = (xd >> 16) & 0xff;
1839
843
  buf[6] = (xd >> 8) & 0xff;
1840
843
  buf[7] = xd & 0xff;
1841
843
  buf[8] = yd >> 24;
1842
843
  buf[9] = (yd >> 16) & 0xff;
1843
843
  buf[10] = (yd >> 8) & 0xff;
1844
843
  buf[11] = yd & 0xff;
1845
843
  buf[12] = s->l0 >> 24;
1846
843
  buf[13] = (s->l0 >> 16) & 0xff;
1847
843
  buf[14] = (s->l0 >> 8) & 0xff;
1848
843
  buf[15] = s->l0 & 0xff;
1849
843
  buf[16] = s->mx;
1850
843
  buf[17] = s->my;
1851
843
  buf[18] = s->order;
1852
843
  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
843
  s->stripes = jbg_stripes(s->l0, s->yd, s->d);
1862
1863
  /* allocate buffers for SDE pointers */
1864
843
  if (s->sde == NULL) {
1865
843
    s->sde = (struct jbg_buf ****)
1866
843
      checked_malloc(s->stripes, sizeof(struct jbg_buf ***));
1867
28.0k
    for (stripe = 0; stripe < s->stripes; stripe++) {
1868
27.2k
      s->sde[stripe] = (struct jbg_buf ***)
1869
27.2k
  checked_malloc(s->d + 1, sizeof(struct jbg_buf **));
1870
99.7k
      for (layer = 0; layer < s->d + 1; layer++) {
1871
72.5k
  s->sde[stripe][layer] = (struct jbg_buf **)
1872
72.5k
    checked_malloc(s->planes, sizeof(struct jbg_buf *));
1873
145k
  for (plane = 0; plane < s->planes; plane++)
1874
72.5k
    s->sde[stripe][layer][plane] = SDE_TODO;
1875
72.5k
      }
1876
27.2k
    }
1877
843
  }
1878
1879
  /* output BIH */
1880
843
  s->data_out(buf, 20, s->file);
1881
843
  if ((s->options & (JBG_DPON | JBG_DPPRIV | JBG_DPLAST)) ==
1882
843
      (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
843
  is[iindex[order][STRIPE]] = 0;
1912
843
  ie[iindex[order][STRIPE]] = s->stripes - 1;
1913
843
  is[iindex[order][LAYER]] = s->dl;
1914
843
  ie[iindex[order][LAYER]] = s->dh;
1915
843
  is[iindex[order][PLANE]] = 0;
1916
843
  ie[iindex[order][PLANE]] = s->planes - 1;
1917
1918
3.00k
  for (ii[0] = is[0]; ii[0] <= ie[0]; ii[0]++)
1919
74.7k
    for (ii[1] = is[1]; ii[1] <= ie[1]; ii[1]++)
1920
145k
      for (ii[2] = is[2]; ii[2] <= ie[2]; ii[2]++) {
1921
1922
72.5k
  stripe = ii[iindex[order][STRIPE]];
1923
72.5k
  if (s->order & JBG_HITOLO)
1924
0
    layer = s->dh - (ii[iindex[order][LAYER]] - s->dl);
1925
72.5k
  else
1926
72.5k
    layer = ii[iindex[order][LAYER]];
1927
72.5k
  plane = ii[iindex[order][PLANE]];
1928
1929
  /* output comment marker segment if there is any pending */
1930
72.5k
  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
72.5k
  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
72.5k
  if (s->yd1 > s->yd &&
1951
72.5k
      (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
72.5k
      }
1973
1974
843
  return;
1975
843
}
1976
1977
1978
void jbg_enc_free(struct jbg_enc_state *s)
1979
843
{
1980
843
  unsigned long stripe;
1981
843
  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
843
  if (s->sde) {
1989
28.0k
    for (stripe = 0; stripe < s->stripes; stripe++) {
1990
99.7k
      for (layer = 0; layer < s->d + 1; layer++) {
1991
145k
  for (plane = 0; plane < s->planes; plane++)
1992
72.5k
    if (s->sde[stripe][layer][plane] != SDE_DONE &&
1993
72.5k
        s->sde[stripe][layer][plane] != SDE_TODO)
1994
0
      jbg_buf_free(&s->sde[stripe][layer][plane]);
1995
72.5k
  checked_free(s->sde[stripe][layer]);
1996
72.5k
      }
1997
27.2k
      checked_free(s->sde[stripe]);
1998
27.2k
    }
1999
843
    checked_free(s->sde);
2000
843
  }
2001
2002
  /* clear free_list */
2003
843
  jbg_buf_free(&s->free_list);
2004
2005
  /* clear memory for arithmetic encoder states */
2006
843
  checked_free(s->s);
2007
2008
  /* clear memory for differential-layer typical prediction buffer */
2009
843
  checked_free(s->tp);
2010
2011
  /* clear memory for adaptive template pixel offsets */
2012
843
  checked_free(s->tx);
2013
2014
  /* clear lowres image buffers */
2015
843
  if (s->lhp[1]) {
2016
1.68k
    for (plane = 0; plane < s->planes; plane++)
2017
843
      checked_free(s->lhp[1][plane]);
2018
843
    checked_free(s->lhp[1]);
2019
843
  }
2020
2021
  /* clear buffer for index of highres image in lhp */
2022
843
  checked_free(s->highres);
2023
2024
843
  return;
2025
843
}
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
14.9k
{
2033
14.9k
  errnum >>= 4;
2034
14.9k
  if (errnum < 0 || (unsigned) errnum >= sizeof(errmsg)/sizeof(errmsg[0]))
2035
0
    return "Unknown error code passed to jbg_strerror()";
2036
2037
14.9k
  return errmsg[errnum];
2038
14.9k
}
2039
2040
2041
/*
2042
 * The constructor for a decoder
2043
 */
2044
void jbg_dec_init(struct jbg_dec_state *s)
2045
4.78k
{
2046
4.78k
  s->order = 0;
2047
4.78k
  s->d = -1;
2048
4.78k
  s->bie_len = 0;
2049
4.78k
  s->buf_len = 0;
2050
4.78k
  s->dppriv = NULL;
2051
4.78k
  s->xmax = 4294967295UL;
2052
4.78k
  s->ymax = 4294967295UL;
2053
4.78k
  s->dmax = 256;
2054
4.78k
  s->maxmem = 2000000000;  /* no final image larger than 2 GB by default */
2055
4.78k
  s->s = NULL;
2056
2057
4.78k
  return;
2058
4.78k
}
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.33k
{
2071
3.33k
  if (xmax > 0) s->xmax = xmax;
2072
3.33k
  if (ymax > 0) s->ymax = ymax;
2073
2074
3.33k
  return;
2075
3.33k
}
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
99.0k
{
2088
99.0k
  unsigned long stripe;
2089
99.0k
  unsigned int layer, plane;
2090
99.0k
  unsigned long hl, ll, y, hx, hy, lx, ly, hbpl, lbpl;
2091
99.0k
  unsigned char *hp, *lp1, *lp2, *p1, *q1;
2092
99.0k
  register unsigned long line_h1, line_h2, line_h3;
2093
99.0k
  register unsigned long line_l1, line_l2, line_l3;
2094
99.0k
  struct jbg_ardec_state *se;
2095
99.0k
  unsigned long x;
2096
99.0k
  long o;
2097
99.0k
  unsigned a;
2098
99.0k
  int n;
2099
99.0k
  int pix, cx = 0, slntp, tx;
2100
2101
  /* SDE loop variables */
2102
99.0k
  stripe = s->ii[iindex[s->order & 7][STRIPE]];
2103
99.0k
  layer = s->ii[iindex[s->order & 7][LAYER]];
2104
99.0k
  plane = s->ii[iindex[s->order & 7][PLANE]];
2105
2106
  /* forward data to arithmetic decoder */
2107
99.0k
  se = s->s[plane] + layer - s->dl;
2108
99.0k
  se->pscd_ptr = data;
2109
99.0k
  se->pscd_end = data + len;
2110
2111
  /* number of lines per stripe in highres image */
2112
99.0k
  hl = s->l0 << layer;
2113
  /* number of lines per stripe in lowres image */
2114
99.0k
  ll = hl >> 1;
2115
  /* current line number in highres image */
2116
99.0k
  y = stripe * hl + s->i;
2117
  /* number of pixels in highres image */
2118
99.0k
  hx = jbg_ceil_half(s->xd, s->d - layer);
2119
99.0k
  hy = jbg_ceil_half(s->yd, s->d - layer);
2120
  /* number of pixels in lowres image */
2121
99.0k
  lx = jbg_ceil_half(hx, 1);
2122
99.0k
  ly = jbg_ceil_half(hy, 1);
2123
  /* bytes per line in highres and lowres image */
2124
99.0k
  hbpl = jbg_ceil_half(hx, 3);
2125
99.0k
  lbpl = jbg_ceil_half(lx, 3);
2126
  /* pointer to highres and lowres image bytes */
2127
99.0k
  hp  = s->lhp[ layer    & 1][plane] + (stripe * hl + s->i) * hbpl +
2128
99.0k
    (s->x >> 3);
2129
99.0k
  lp2 = s->lhp[(layer-1) & 1][plane] + (stripe * ll + (s->i >> 1)) * lbpl +
2130
99.0k
    (s->x >> 4);
2131
99.0k
  lp1 = lp2 + lbpl;
2132
2133
  /* restore a few local variables */
2134
99.0k
  line_h1 = s->line_h1;
2135
99.0k
  line_h2 = s->line_h2;
2136
99.0k
  line_h3 = s->line_h3;
2137
99.0k
  line_l1 = s->line_l1;
2138
99.0k
  line_l2 = s->line_l2;
2139
99.0k
  line_l3 = s->line_l3;
2140
99.0k
  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
99.0k
  if (s->x == 0 && s->i == 0 &&
2149
99.0k
      (stripe == 0 || s->reset[plane][layer - s->dl]) && s->pseudo) {
2150
26.8k
    s->tx[plane][layer - s->dl] = s->ty[plane][layer - s->dl] = 0;
2151
26.8k
    s->lntp[plane][layer - s->dl] = 1;
2152
26.8k
  }
2153
2154
99.0k
  if (layer == 0) {
2155
2156
    /*
2157
     *  Decode lowest resolution layer
2158
     */
2159
2160
838M
    for (; s->i < hl && y < hy; s->i++, y++) {
2161
2162
      /* adaptive template changes */
2163
838M
      if (x == 0 && s->pseudo)
2164
844M
  for (n = 0; n < s->at_moves; n++)
2165
6.72M
    if (s->at_line[n] == s->i) {
2166
1.43k
      s->tx[plane][layer - s->dl] = s->at_tx[n];
2167
1.43k
      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.43k
    }
2173
838M
      tx = s->tx[plane][layer - s->dl];
2174
838M
      assert(tx >= 0); /* i.e., tx can safely be cast to unsigned */
2175
2176
      /* typical prediction */
2177
838M
      if (s->options & JBG_TPBON && s->pseudo) {
2178
297M
  slntp = arith_decode(se, (s->options & JBG_LRLTWO) ? TPB2CX : TPB3CX);
2179
297M
  if (slntp < 0)
2180
3.27k
    goto leave;
2181
297M
  s->lntp[plane][layer - s->dl] =
2182
297M
    !(slntp ^ s->lntp[plane][layer - s->dl]);
2183
297M
  if (!s->lntp[plane][layer - s->dl]) {
2184
    /* this line is 'typical' (i.e. identical to the previous one) */
2185
209M
    p1 = hp;
2186
209M
    if (s->i == 0 && (stripe == 0 || s->reset[plane][layer - s->dl]))
2187
7.46G
      while (p1 < hp + hbpl) *p1++ = 0;
2188
209M
    else {
2189
209M
      q1 = hp - hbpl;
2190
7.24G
      while (q1 < hp) *p1++ = *q1++;
2191
209M
    }
2192
209M
    hp += hbpl;
2193
209M
    continue;
2194
209M
  }
2195
  /* this line is 'not typical' and has to be coded completely */
2196
297M
      }
2197
628M
      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
628M
      if (x == 0) {
2209
628M
  line_h1 = line_h2 = line_h3 = 0;
2210
628M
  if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl]))
2211
628M
    line_h2 = (long)*(hp - hbpl) << 8;
2212
628M
  if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl]))
2213
628M
    line_h3 = (long)*(hp - hbpl - hbpl) << 8;
2214
628M
      }
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.22G
      while (x < hx) {
2262
2.59G
  if ((x & 7) == 0) {
2263
2.59G
    if (x < hbpl * 8 - 8 &&
2264
2.59G
        (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl]))) {
2265
1.86G
      line_h2 |= *(hp - hbpl + 1);
2266
1.86G
      if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl]))
2267
1.83G
        line_h3 |= *(hp - hbpl - hbpl + 1);
2268
1.86G
    }
2269
2.59G
  }
2270
2.59G
  if (s->options & JBG_LRLTWO) {
2271
    /* two line template */
2272
1.98G
    do {
2273
1.98G
      if (tx) {
2274
90.2M
        if ((unsigned) tx > x)
2275
25.2k
    a = 0;
2276
90.2M
        else if (tx < 8)
2277
89.6M
    a = ((line_h1 >> (tx - 5)) & 0x010);
2278
550k
        else {
2279
550k
    o = (x - tx) - (x & ~7L);
2280
550k
    a = (hp[o >> 3] >> (7 - (o & 7))) & 1;
2281
550k
    a <<= 4;
2282
550k
        }
2283
90.2M
        assert(tx > 31 ||
2284
90.2M
         a == ((line_h1 >> (tx - 5)) & 0x010));
2285
90.2M
        pix = arith_decode(se, (((line_h2 >> 9) & 0x3e0) | a |
2286
90.2M
              (line_h1 & 0x00f)));
2287
90.2M
      } else
2288
1.89G
        pix = arith_decode(se, (((line_h2 >> 9) & 0x3f0) |
2289
1.89G
              (line_h1 & 0x00f)));
2290
1.98G
      if (pix < 0)
2291
6.40k
        goto leave;
2292
1.98G
      line_h1 = (line_h1 << 1) | pix;
2293
1.98G
      line_h2 <<= 1;
2294
1.98G
    } while ((++x & 7) && x < hx);
2295
2.31G
  } else {
2296
    /* three line template */
2297
15.0G
    do {
2298
15.0G
      if (tx) {
2299
202M
        if ((unsigned) tx > x)
2300
910k
    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
202M
        assert(tx > 31 ||
2309
202M
         a == ((line_h1 >> (tx - 3)) & 0x004));
2310
202M
        pix = arith_decode(se, (((line_h3 >>  7) & 0x380) |
2311
202M
              ((line_h2 >> 11) & 0x078) | a |
2312
202M
              (line_h1 & 0x003)));
2313
202M
      } 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
13.2k
        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.31G
  } /* if (s->options & JBG_LRLTWO) */
2325
2.59G
  *hp++ = line_h1;
2326
2.59G
      } /* while */
2327
628M
      *(hp - 1) <<= hbpl * 8 - hx;
2328
628M
      x = 0;
2329
628M
      s->pseudo = 1;
2330
628M
    } /* for (i = ...) */
2331
2332
64.0k
  } else {
2333
2334
    /*
2335
     *  Decode differential layer
2336
     */
2337
2338
153M
    for (; s->i < hl && y < hy; s->i++, y++) {
2339
2340
      /* adaptive template changes */
2341
153M
      if (x == 0)
2342
158M
  for (n = 0; n < s->at_moves; n++)
2343
5.15M
    if (s->at_line[n] == s->i) {
2344
619
      s->tx[plane][layer - s->dl] = s->at_tx[n];
2345
619
      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
619
    }
2351
153M
      tx = s->tx[plane][layer - s->dl];
2352
2353
      /* handle lower border of low-resolution image */
2354
153M
      if ((s->i >> 1) >= ll - 1 || (y >> 1) >= ly - 1)
2355
24.6k
  lp1 = lp2;
2356
2357
      /* typical prediction */
2358
153M
      if ((s->options & JBG_TPDON) && s->pseudo) {
2359
19.7M
  if ((s->lntp[plane][layer - s->dl] = arith_decode(se, TPDCX)) < 0)
2360
1.38k
    goto leave;
2361
19.7M
      }
2362
153M
      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
153M
      if (x == 0) {
2385
153M
  line_h1 = line_h2 = line_h3 = line_l1 = line_l2 = line_l3 = 0;
2386
153M
  if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl])) {
2387
153M
    line_h2 = (long)*(hp - hbpl) << 8;
2388
153M
    if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl]))
2389
153M
      line_h3 = (long)*(hp - hbpl - hbpl) << 8;
2390
153M
  }
2391
153M
  if (s->i > 1 || (y > 1 && !s->reset[plane][layer-s->dl]))
2392
153M
    line_l3 = (long)*(lp2 - lbpl) << 8;
2393
153M
  line_l2 = (long)*lp2 << 8;
2394
153M
  line_l1 = (long)*lp1 << 8;
2395
153M
      }
2396
2397
      /* decode line */
2398
611M
      while (x < hx) {
2399
458M
  if ((x & 15) == 0)
2400
458M
    if ((x >> 1) < lbpl * 8 - 8) {
2401
304M
      line_l1 |= *(lp1 + 1);
2402
304M
      line_l2 |= *(lp2 + 1);
2403
304M
      if (s->i > 1 ||
2404
304M
    (y > 1 && !s->reset[plane][layer - s->dl]))
2405
271M
        line_l3 |= *(lp2 - lbpl + 1);
2406
304M
    }
2407
770M
  do {
2408
2409
770M
    assert(hp  - (s->lhp[ layer     &1][plane] + (stripe * hl + s->i)
2410
770M
      * hbpl) == (ptrdiff_t) x >> 3);
2411
770M
    assert(lp2 - (s->lhp[(layer-1) &1][plane] + (stripe * ll + (s->i>>1))
2412
770M
      * lbpl) == (ptrdiff_t) x >> 4);
2413
2414
770M
    if ((x & 7) == 0)
2415
770M
      if (x < hbpl * 8 - 8) {
2416
617M
        if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl])) {
2417
572M
    line_h2 |= *(hp + 1 - hbpl);
2418
572M
    if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl]))
2419
549M
      line_h3 |= *(hp + 1 - hbpl - hbpl);
2420
572M
        }
2421
617M
      }
2422
2.64G
    do {
2423
2.64G
      if (!s->lntp[plane][layer - s->dl])
2424
471M
              cx = (((line_l3 >> 14) & 0x007) |
2425
471M
                    ((line_l2 >> 11) & 0x038) |
2426
471M
                    ((line_l1 >> 8)  & 0x1c0));
2427
2.64G
      if (!s->lntp[plane][layer - s->dl] &&
2428
2.64G
    (cx == 0x000 || cx == 0x1ff)) {
2429
        /* pixels are typical and have not to be decoded */
2430
459M
        do {
2431
459M
    line_h1 = (line_h1 << 1) | (cx & 1);
2432
459M
        } while ((++x & 1) && x < hx);
2433
230M
        line_h2 <<= 2;  line_h3 <<= 2;
2434
230M
      } else
2435
4.70G
        do {
2436
2437
    /* deterministic prediction */
2438
4.70G
    if (s->options & JBG_DPON)
2439
1.85G
      if ((y & 1) == 0)
2440
932M
        if ((x & 1) == 0)
2441
          /* phase 0 */
2442
473M
          pix = s->dppriv[((line_l3 >> 15) & 0x003) |
2443
473M
              ((line_l2 >> 13) & 0x00c) |
2444
473M
              ((line_h1 <<  4) & 0x010) |
2445
473M
              ((line_h2 >>  9) & 0x0e0)];
2446
458M
        else
2447
          /* phase 1 */
2448
458M
          pix = s->dppriv[(((line_l3 >> 15) & 0x003) |
2449
458M
               ((line_l2 >> 13) & 0x00c) |
2450
458M
               ((line_h1 <<  4) & 0x030) |
2451
458M
               ((line_h2 >>  9) & 0x1c0)) + 256];
2452
926M
      else
2453
926M
        if ((x & 1) == 0)
2454
          /* phase 2 */
2455
471M
          pix = s->dppriv[(((line_l3 >> 15) & 0x003) |
2456
471M
               ((line_l2 >> 13) & 0x00c) |
2457
471M
               ((line_h1 <<  4) & 0x010) |
2458
471M
               ((line_h2 >>  9) & 0x0e0) |
2459
471M
               ((line_h3 >>  6) & 0x700)) + 768];
2460
455M
        else
2461
          /* phase 3 */
2462
455M
          pix = s->dppriv[(((line_l3 >> 15) & 0x003) |
2463
455M
               ((line_l2 >> 13) & 0x00c) |
2464
455M
               ((line_h1 <<  4) & 0x030) |
2465
455M
               ((line_h2 >>  9) & 0x1c0) |
2466
455M
               ((line_h3 >>  6) & 0xe00)) + 2816];
2467
2.84G
    else
2468
2.84G
      pix = 2;
2469
2470
4.70G
    if (pix & 2) {
2471
4.11G
      if (tx)
2472
9.77M
        cx = ((line_h1         & 0x003) |
2473
9.77M
        (((line_h1 << 2) >> (tx - 3)) & 0x010) |
2474
9.77M
        ((line_h2 >> 12) & 0x00c) |
2475
9.77M
        ((line_h3 >> 10) & 0x020));
2476
4.10G
      else
2477
4.10G
        cx = ((line_h1         & 0x003) |
2478
4.10G
        ((line_h2 >> 12) & 0x01c) |
2479
4.10G
        ((line_h3 >> 10) & 0x020));
2480
4.11G
      if (x & 1)
2481
1.96G
        cx |= (((line_l2 >> 8) & 0x0c0) |
2482
1.96G
         ((line_l1 >> 6) & 0x300)) | (1UL << 10);
2483
2.15G
      else
2484
2.15G
        cx |= (((line_l2 >> 9) & 0x0c0) |
2485
2.15G
         ((line_l1 >> 7) & 0x300));
2486
4.11G
      cx |= (y & 1) << 11;
2487
2488
4.11G
      pix = arith_decode(se, cx);
2489
4.11G
      if (pix < 0)
2490
10.7k
        goto leave;
2491
4.11G
    }
2492
2493
4.70G
    line_h1 = (line_h1 << 1) | pix;
2494
4.70G
    line_h2 <<= 1;
2495
4.70G
    line_h3 <<= 1;
2496
2497
4.70G
        } while ((++x & 1) && x < hx);
2498
2.64G
      line_l1 <<= 1; line_l2 <<= 1;  line_l3 <<= 1;
2499
2.64G
    } while ((x & 7) && x < hx);
2500
770M
    *hp++ = line_h1;
2501
770M
  } while ((x & 15) && x < hx);
2502
458M
  ++lp1;
2503
458M
  ++lp2;
2504
458M
      } /* while */
2505
153M
      x = 0;
2506
2507
153M
      *(hp - 1) <<= hbpl * 8 - hx;
2508
153M
      if ((s->i & 1) == 0) {
2509
  /* low resolution pixels are used twice */
2510
76.6M
  lp1 -= lbpl;
2511
76.6M
  lp2 -= lbpl;
2512
76.6M
      } else
2513
76.6M
  s->pseudo = 1;
2514
2515
153M
    } /* for (i = ...) */
2516
2517
35.0k
  }
2518
2519
99.0k
 leave:
2520
2521
  /* save a few local variables */
2522
99.0k
  s->line_h1 = line_h1;
2523
99.0k
  s->line_h2 = line_h2;
2524
99.0k
  s->line_h3 = line_h3;
2525
99.0k
  s->line_l1 = line_l1;
2526
99.0k
  s->line_l2 = line_l2;
2527
99.0k
  s->line_l3 = line_l3;
2528
99.0k
  s->x = x;
2529
2530
99.0k
  return se->pscd_ptr - data;
2531
99.0k
}
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
14.9k
{
2571
14.9k
  int i, j, required_length;
2572
14.9k
  unsigned long x, y;
2573
14.9k
  unsigned long is[3], ie[3];
2574
14.9k
  size_t dummy_cnt;
2575
14.9k
  unsigned char *dppriv;
2576
2577
14.9k
  if (!cnt) cnt = &dummy_cnt;
2578
14.9k
  *cnt = 0;
2579
14.9k
  if (len < 1) return JBG_EAGAIN;
2580
2581
  /* read in 20-byte BIH */
2582
14.9k
  if (s->bie_len < 20) {
2583
99.8k
    while (s->bie_len < 20 && *cnt < len)
2584
95.0k
      s->buffer[s->bie_len++] = data[(*cnt)++];
2585
4.78k
    if (s->bie_len < 20)
2586
51
      return JBG_EAGAIN;
2587
    /* test whether this looks like a valid JBIG header at all */
2588
4.73k
    if (s->buffer[1] < s->buffer[0])
2589
51
      return JBG_EINVAL | 1;
2590
4.68k
    if (s->buffer[3] != 0)           return JBG_EINVAL | 2; /* padding != 0 */
2591
4.61k
    if ((s->buffer[18] & 0xf0) != 0) return JBG_EINVAL | 3; /* padding != 0 */
2592
4.54k
    if ((s->buffer[19] & 0x80) != 0) return JBG_EINVAL | 4; /* padding != 0 */
2593
4.52k
    if (s->buffer[0] != s->d + 1)
2594
19
      return JBG_ENOCONT | 1;
2595
4.50k
    s->dl = s->buffer[0];
2596
4.50k
    s->d = s->buffer[1];
2597
4.50k
    if (s->dl == 0)
2598
4.50k
      s->planes = s->buffer[2];
2599
0
    else
2600
0
      if (s->planes != s->buffer[2])
2601
0
  return JBG_ENOCONT | 2;
2602
4.50k
    x = (((long) s->buffer[ 4] << 24) | ((long) s->buffer[ 5] << 16) |
2603
4.50k
   ((long) s->buffer[ 6] <<  8) | (long) s->buffer[ 7]);
2604
4.50k
    y = (((long) s->buffer[ 8] << 24) | ((long) s->buffer[ 9] << 16) |
2605
4.50k
   ((long) s->buffer[10] <<  8) | (long) s->buffer[11]);
2606
4.50k
    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.50k
    s->xd = x;
2610
4.50k
    s->yd = y;
2611
4.50k
    s->l0 = (((long) s->buffer[12] << 24) | ((long) s->buffer[13] << 16) |
2612
4.50k
       ((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.50k
    if (s->yd == 0xffffffff)
2618
5
      return JBG_EIMPL | 1;
2619
4.50k
    if (!s->planes) return JBG_EINVAL | 5;
2620
4.45k
    if (!s->xd)     return JBG_EINVAL | 6;
2621
4.45k
    if (!s->yd)     return JBG_EINVAL | 7;
2622
4.44k
    if (!s->l0)     return JBG_EINVAL | 8;
2623
    /* prevent uint32 overflow: s->l0 * 2 ^ s->d < 2 ^ 32 */
2624
4.44k
    if (s->d > 31)
2625
29
      return JBG_EIMPL | 2;
2626
4.41k
    if ((s->d != 0 && s->l0 >= (1UL << (32 - s->d))))
2627
66
      return JBG_EIMPL | 3;
2628
4.34k
    s->mx = s->buffer[16];
2629
4.34k
    if (s->mx > 127)
2630
10
      return JBG_EINVAL | 9;
2631
4.33k
    s->my = s->buffer[17];
2632
#if 0
2633
    if (s->my > 0)
2634
      return JBG_EIMPL | 4;
2635
#endif
2636
4.33k
    s->order = s->buffer[18];
2637
4.33k
    if (iindex[s->order & 7][0] < 0)
2638
5
      return JBG_EINVAL | 10;
2639
    /* HITOLO and SEQ currently not yet implemented */
2640
4.33k
    if (s->dl != s->d && (s->order & JBG_HITOLO || s->order & JBG_SEQ))
2641
10
      return JBG_EIMPL | 5;
2642
4.32k
    s->options = s->buffer[19];
2643
2644
    /* will the final image require more bytes than permitted by s->maxmem? */
2645
4.32k
    if (s->maxmem / s->planes / s->yd / jbg_ceil_half(s->xd, 3) == 0)
2646
120
      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
169k
      for (i = 0; i < s->planes; i++) {
2667
165k
  s->s[i]     = (struct jbg_ardec_state *)
2668
165k
    checked_malloc(s->d - s->dl + 1, sizeof(struct jbg_ardec_state));
2669
165k
  s->tx[i]    = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int));
2670
165k
  s->ty[i]    = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int));
2671
165k
  s->reset[i] = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int));
2672
165k
  s->lntp[i]  = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int));
2673
165k
  s->lhp[ s->d    & 1][i] = (unsigned char *)
2674
165k
    checked_malloc(s->yd, jbg_ceil_half(s->xd, 3));
2675
165k
  s->lhp[(s->d-1) & 1][i] = (unsigned char *)
2676
165k
    checked_malloc(jbg_ceil_half(s->yd, 1), jbg_ceil_half(s->xd, 1+3));
2677
165k
      }
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
169k
    for (i = 0; i < s->planes; i++)
2700
702k
      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
14.4k
  if (s->bie_len < 20 + 1728 &&
2714
14.4k
      (s->options & (JBG_DPON | JBG_DPPRIV | JBG_DPLAST)) ==
2715
14.2k
      (JBG_DPON | JBG_DPPRIV)) {
2716
97
    assert(s->bie_len >= 20);
2717
97
    if (!s->dppriv || s->dppriv == jbg_dptable)
2718
97
      s->dppriv = (char *) checked_malloc(1728, sizeof(char));
2719
71.1k
    while (s->bie_len < 20 + 1728 && *cnt < len)
2720
71.0k
      s->dppriv[s->bie_len++ - 20] = data[(*cnt)++];
2721
97
    if (s->bie_len < 20 + 1728)
2722
63
      return JBG_EAGAIN;
2723
34
    dppriv = (unsigned char *) s->dppriv;
2724
34
    s->dppriv = (char *) checked_malloc(6912, sizeof(char));
2725
34
    jbg_dppriv2int(s->dppriv, dppriv);
2726
34
    checked_free(dppriv);
2727
34
  }
2728
2729
  /*
2730
   * BID processing loop
2731
   */
2732
2733
207k
  while (*cnt < len) {
2734
2735
    /* process floating marker segments */
2736
2737
    /* skip COMMENT contents */
2738
195k
    if (s->comment_skip) {
2739
9.76k
      if (s->comment_skip <= len - *cnt) {
2740
126
  *cnt += s->comment_skip;
2741
126
  s->comment_skip = 0;
2742
9.63k
      } else {
2743
9.63k
  s->comment_skip -= len - *cnt;
2744
9.63k
  *cnt = len;
2745
9.63k
      }
2746
9.76k
      continue;
2747
9.76k
    }
2748
2749
    /* load complete marker segments into s->buffer for processing */
2750
185k
    if (s->buf_len > 0) {
2751
77.2k
      assert(s->buffer[0] == MARKER_ESC);
2752
154k
      while (s->buf_len < 2 && *cnt < len)
2753
77.1k
  s->buffer[s->buf_len++] = data[(*cnt)++];
2754
77.2k
      if (s->buf_len < 2) continue;
2755
77.2k
      switch (s->buffer[1]) {
2756
763
      case MARKER_COMMENT: required_length = 6; break;
2757
7.60k
      case MARKER_ATMOVE:  required_length = 8; break;
2758
290
      case MARKER_NEWLEN:  required_length = 6; break;
2759
11
      case MARKER_ABORT:
2760
22.9k
      case MARKER_SDNORM:
2761
35.3k
      case MARKER_SDRST:   required_length = 2; break;
2762
32.7k
      case MARKER_STUFF:
2763
  /* forward stuffed 0xff to arithmetic decoder */
2764
32.7k
  s->buf_len = 0;
2765
32.7k
  decode_pscd(s, s->buffer, 2);
2766
32.7k
  continue;
2767
383
      default:
2768
383
  return JBG_EMARKER;
2769
77.2k
      }
2770
93.5k
      while (s->buf_len < required_length && *cnt < len)
2771
49.5k
  s->buffer[s->buf_len++] = data[(*cnt)++];
2772
44.0k
      if (s->buf_len < required_length) continue;
2773
      /* now the buffer is filled with exactly one marker segment */
2774
43.9k
      switch (s->buffer[1]) {
2775
754
      case MARKER_COMMENT:
2776
754
  s->comment_skip =
2777
754
    (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) |
2778
754
     ((long) s->buffer[4] <<  8) | (long) s->buffer[5]);
2779
754
  break;
2780
7.54k
      case MARKER_ATMOVE:
2781
7.54k
  if (s->at_moves < JBG_ATMOVES_MAX) {
2782
7.54k
    s->at_line[s->at_moves] =
2783
7.54k
      (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) |
2784
7.54k
       ((long) s->buffer[4] <<  8) | (long) s->buffer[5]);
2785
7.54k
    s->at_tx[s->at_moves] = (signed char) s->buffer[6];
2786
7.54k
    s->at_ty[s->at_moves] = s->buffer[7];
2787
7.54k
    if (s->at_tx[s->at_moves] < - (int) s->mx ||
2788
7.54k
        s->at_tx[s->at_moves] >   (int) s->mx ||
2789
7.54k
        s->at_ty[s->at_moves] >   (int) s->my ||
2790
7.54k
        (s->at_ty[s->at_moves] == 0 && s->at_tx[s->at_moves] < 0))
2791
87
      return JBG_EINVAL | 11;
2792
7.45k
    if (s->at_ty[s->at_moves] != 0)
2793
5
      return JBG_EIMPL | 6;
2794
7.44k
    s->at_moves++;
2795
7.44k
  } else
2796
2
    return JBG_EIMPL | 7; /* more than JBG_ATMOVES_MAX ATMOVES */
2797
7.44k
  break;
2798
7.44k
      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
192
  if (!(s->options & JBG_VLENGTH)) return JBG_EINVAL | 13;
2803
187
  s->yd = y;
2804
  /* calculate again number of stripes that will be required */
2805
187
  s->stripes = jbg_stripes(s->l0, s->yd, s->d);
2806
187
  break;
2807
11
      case MARKER_ABORT:
2808
11
  return JBG_EABORT;
2809
2810
22.8k
      case MARKER_SDNORM:
2811
35.3k
      case MARKER_SDRST:
2812
  /* decode final pixels based on trailing zero bytes */
2813
35.3k
  decode_pscd(s, s->buffer, 2);
2814
2815
35.3k
  arith_decode_init(s->s[s->ii[iindex[s->order & 7][PLANE]]] +
2816
35.3k
        s->ii[iindex[s->order & 7][LAYER]] - s->dl,
2817
35.3k
        s->ii[iindex[s->order & 7][STRIPE]] != s->stripes - 1
2818
35.3k
        && s->buffer[1] != MARKER_SDRST);
2819
2820
35.3k
  s->reset[s->ii[iindex[s->order & 7][PLANE]]]
2821
35.3k
    [s->ii[iindex[s->order & 7][LAYER]] - s->dl] =
2822
35.3k
      (s->buffer[1] == MARKER_SDRST);
2823
2824
  /* prepare for next SDE */
2825
35.3k
  s->x = 0;
2826
35.3k
  s->i = 0;
2827
35.3k
  s->pseudo = 1;
2828
35.3k
  s->at_moves = 0;
2829
2830
  /* increment layer/stripe/plane loop variables */
2831
  /* start and end value for each loop: */
2832
35.3k
  is[iindex[s->order & 7][STRIPE]] = 0;
2833
35.3k
  ie[iindex[s->order & 7][STRIPE]] = s->stripes - 1;
2834
35.3k
  is[iindex[s->order & 7][LAYER]] = s->dl;
2835
35.3k
  ie[iindex[s->order & 7][LAYER]] = s->d;
2836
35.3k
  is[iindex[s->order & 7][PLANE]] = 0;
2837
35.3k
  ie[iindex[s->order & 7][PLANE]] = s->planes - 1;
2838
35.3k
  i = 2;  /* index to innermost loop */
2839
53.8k
  do {
2840
53.8k
    j = 0;  /* carry flag */
2841
53.8k
    if (++s->ii[i] > ie[i]) {
2842
      /* handling overflow of loop variable */
2843
19.4k
      j = 1;
2844
19.4k
      if (i > 0)
2845
18.4k
        s->ii[i] = is[i];
2846
19.4k
    }
2847
53.8k
  } while (--i >= 0 && j);
2848
2849
35.3k
  s->buf_len = 0;
2850
2851
  /* check whether this have been all SDEs */
2852
35.3k
  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
935
    s->bie_len = 0;
2858
935
    return JBG_EOK;
2859
935
  }
2860
2861
  /* check whether we have to abort because of xmax/ymax */
2862
34.4k
  if (iindex[s->order & 7][LAYER] == 0 && i < 0) {
2863
    /* LAYER is the outermost loop and we have just gone to next layer */
2864
245
    if (jbg_ceil_half(s->xd, s->d - s->ii[0]) > s->xmax ||
2865
245
        jbg_ceil_half(s->yd, s->d - s->ii[0]) > s->ymax) {
2866
73
      s->xmax = 4294967295UL;
2867
73
      s->ymax = 4294967295UL;
2868
73
      return JBG_EOK_INTR;
2869
73
    }
2870
172
    if (s->ii[0] > (unsigned long) s->dmax) {
2871
0
      s->dmax = 256;
2872
0
      return JBG_EOK_INTR;
2873
0
    }
2874
172
  }
2875
2876
34.3k
  break;
2877
43.9k
      }
2878
42.7k
      s->buf_len = 0;
2879
2880
108k
    } else if (data[*cnt] == MARKER_ESC)
2881
77.3k
      s->buffer[s->buf_len++] = data[(*cnt)++];
2882
2883
30.9k
    else {
2884
2885
      /* we have found PSCD bytes */
2886
30.9k
      *cnt += decode_pscd(s, data + *cnt, len - *cnt);
2887
30.9k
      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
228
  return JBG_EINVAL | 14;
2894
228
      }
2895
2896
30.9k
    }
2897
185k
  }  /* of BID processing loop 'while (*cnt < len) ...' */
2898
2899
12.5k
  return JBG_EAGAIN;
2900
14.3k
}
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
928
{
2910
928
  if (s->d < 0)
2911
0
    return 0;
2912
928
  if (iindex[s->order & 7][LAYER] == 0) {
2913
73
    if (s->ii[0] < 1)
2914
0
      return 0;
2915
73
    else
2916
73
      return jbg_ceil_half(s->xd, s->d - (s->ii[0] - 1));
2917
73
  }
2918
2919
855
  return s->xd;
2920
928
}
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
928
{
2930
928
  if (s->d < 0)
2931
0
    return 0;
2932
928
  if (iindex[s->order & 7][LAYER] == 0) {
2933
73
    if (s->ii[0] < 1)
2934
0
      return 0;
2935
73
    else
2936
73
      return jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1));
2937
73
  }
2938
2939
855
  return s->yd;
2940
928
}
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
844
{
2950
844
  if (s->d < 0)
2951
0
    return NULL;
2952
844
  if (iindex[s->order & 7][LAYER] == 0) {
2953
67
    if (s->ii[0] < 1)
2954
0
      return NULL;
2955
67
    else
2956
67
      return s->lhp[(s->ii[0] - 1) & 1][plane];
2957
67
  }
2958
2959
777
  return s->lhp[s->d & 1][plane];
2960
844
}
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
7
{
2970
7
  if (s->d < 0)
2971
0
    return 0;
2972
7
  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
6
  return jbg_ceil_half(s->xd, 3) * s->yd;
2982
7
}
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.78k
{
3014
4.78k
  int i;
3015
3016
4.78k
  if (s->d < 0 || s->s == NULL)
3017
587
    return;
3018
4.20k
  s->d = -2;
3019
3020
169k
  for (i = 0; i < s->planes; i++) {
3021
165k
    checked_free(s->s[i]);
3022
165k
    checked_free(s->tx[i]);
3023
165k
    checked_free(s->ty[i]);
3024
165k
    checked_free(s->reset[i]);
3025
165k
    checked_free(s->lntp[i]);
3026
165k
    checked_free(s->lhp[0][i]);
3027
165k
    checked_free(s->lhp[1][i]);
3028
165k
  }
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
97
    checked_free(s->dppriv);
3039
3040
4.20k
  s->s = NULL;
3041
3042
4.20k
  return;
3043
4.78k
}
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
28.8k
{
3187
28.8k
  unsigned char *pp;
3188
28.8k
  unsigned long l;
3189
3190
28.8k
  if (len < 2)
3191
21
    return NULL; /* not enough bytes left for complete marker segment */
3192
3193
28.8k
  if (p[0] != MARKER_ESC || p[1] == MARKER_STUFF) {
3194
30.9k
    do {
3195
75.1k
      while (p[0] == MARKER_ESC && p[1] == MARKER_STUFF) {
3196
44.2k
  p += 2;
3197
44.2k
  len -= 2;
3198
44.2k
  if (len < 2)
3199
30
    return NULL; /* not enough bytes left for complete marker segment */
3200
44.2k
      }
3201
30.8k
      assert(len >= 2);
3202
30.8k
      pp = (unsigned char *) memchr(p, MARKER_ESC, len - 1);
3203
30.8k
      if (!pp)
3204
940
  return NULL; /* no marker segment terminates the PSCD */
3205
29.9k
      l = pp - p;
3206
29.9k
      assert(l < len);
3207
29.9k
      p += l;
3208
29.9k
      len -= l;
3209
29.9k
    } while (p[1] == MARKER_STUFF);
3210
14.5k
  } else {
3211
14.5k
    switch (p[1]) {
3212
8.16k
    case MARKER_SDNORM:
3213
12.2k
    case MARKER_SDRST:
3214
12.2k
    case MARKER_ABORT:
3215
12.2k
      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.61k
    case MARKER_ATMOVE:
3221
1.61k
      if (len < 8)
3222
4
  return NULL; /* not enough bytes left for complete marker segment */
3223
1.61k
      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
21
  return NULL; /* not enough bytes left for complete marker segment */
3231
350
      return p + 6 + l;
3232
333
    default:
3233
      /* unknown marker sequence encountered */
3234
333
      return NULL;
3235
14.5k
    }
3236
14.5k
  }
3237
3238
13.2k
  return p;
3239
28.8k
}
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
19
    return JBG_EAGAIN;
3263
1.43k
  if ((bie[19] & (JBG_DPON | JBG_DPPRIV | JBG_DPLAST))
3264
1.43k
      == (JBG_DPON | JBG_DPPRIV))
3265
44
    p += 1728; /* skip DPTABLE */
3266
1.43k
  if (p >= bie + len)
3267
47
    return JBG_EAGAIN;
3268
3269
28.8k
  while ((p = jbg_next_pscdms(p, len - (p - bie)))) {
3270
27.4k
    if (p == bie + len)
3271
4
      return JBG_EOK;
3272
27.4k
    else if (p[0] == MARKER_ESC)
3273
18.2k
      switch (p[1]) {
3274
19
      case MARKER_NEWLEN:
3275
19
        if (p + 5 >= bie + len)
3276
0
          return JBG_EAGAIN;
3277
19
  y = (((long) bie[ 8] << 24) | ((long) bie[ 9] << 16) |
3278
19
       ((long) bie[10] <<  8) |  (long) bie[11]);
3279
19
  yn = (((long) p[2] << 24) | ((long) p[3] << 16) |
3280
19
        ((long) p[4] <<  8) |  (long) p[5]);
3281
19
  if (yn > y) return JBG_EINVAL | 12;
3282
  /* overwrite YD in BIH with YD from NEWLEN */
3283
40
  for (i = 0; i < 4; i++) {
3284
32
    bie[8+i] = p[2+i];
3285
32
  }
3286
8
  return JBG_EOK;
3287
11
      case MARKER_ABORT:
3288
11
  return JBG_EABORT;
3289
18.2k
      }
3290
27.4k
  }
3291
1.35k
  return JBG_EINVAL | 0;
3292
1.38k
}