Debian Codegraph @

converter/other/jbig/libjbig/jbig.c

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