Coverage Report

Created: 2026-04-01 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/jpegxr/io.c
Line
Count
Source
1
2
3
/*************************************************************************
4
*
5
* This software module was originally contributed by Microsoft
6
* Corporation in the course of development of the
7
* ITU-T T.832 | ISO/IEC 29199-2 ("JPEG XR") format standard for
8
* reference purposes and its performance may not have been optimized.
9
*
10
* This software module is an implementation of one or more
11
* tools as specified by the JPEG XR standard.
12
*
13
* ITU/ISO/IEC give You a royalty-free, worldwide, non-exclusive
14
* copyright license to copy, distribute, and make derivative works
15
* of this software module or modifications thereof for use in
16
* products claiming conformance to the JPEG XR standard as
17
* specified by ITU-T T.832 | ISO/IEC 29199-2.
18
*
19
* ITU/ISO/IEC give users the same free license to this software
20
* module or modifications thereof for research purposes and further
21
* ITU/ISO/IEC standardization.
22
*
23
* Those intending to use this software module in products are advised
24
* that its use may infringe existing patents. ITU/ISO/IEC have no
25
* liability for use of this software module or modifications thereof.
26
*
27
* Copyright is not released for products that do not conform to
28
* to the JPEG XR standard as specified by ITU-T T.832 |
29
* ISO/IEC 29199-2.
30
*
31
* Microsoft Corporation retains full right to modify and use the code
32
* for its own purpose, to assign or donate the code to a third party,
33
* and to inhibit third parties from using the code for products that
34
* do not conform to the JPEG XR standard as specified by ITU-T T.832 |
35
* ISO/IEC 29199-2.
36
*
37
* This copyright notice must be included in all copies or derivative
38
* works.
39
*
40
* Copyright (c) ITU-T/ISO/IEC 2008, 2009.
41
***********************************************************************/
42
43
#ifdef _MSC_VER
44
#pragma comment (user,"$Id: io.c,v 1.14 2008/03/05 06:58:10 gus Exp $")
45
#else
46
#ident "$Id: io.c,v 1.14 2008/03/05 06:58:10 gus Exp $"
47
#endif
48
49
# include "jxr_priv.h"
50
# include <assert.h>
51
52
void _jxr_rbitstream_initialize(struct rbitstream*str, FILE*fd)
53
0
{
54
0
    str->bits_avail = 0;
55
0
    str->fd = fd;
56
0
    str->read_count = 0;
57
0
}
58
59
size_t _jxr_rbitstream_bitpos(struct rbitstream*str)
60
0
{
61
0
    return str->read_count*8 - str->bits_avail;
62
0
}
63
64
void _jxr_rbitstream_mark(struct rbitstream*str)
65
0
{
66
0
    assert(str->bits_avail == 0);
67
0
    str->mark_stream_position = ftell(str->fd);
68
0
    assert(str->mark_stream_position >= 0);
69
0
    str->read_count = 0;
70
0
}
71
72
void _jxr_rbitstream_seek(struct rbitstream*str, uint64_t off)
73
0
{
74
0
    int rc;
75
0
    assert(str->bits_avail == 0);
76
    /* NOTE: Should be using fseek64? */
77
0
    rc = fseek(str->fd, str->mark_stream_position + (long)off, SEEK_SET);
78
0
    str->read_count = (size_t) off;
79
0
    assert(rc >= 0);
80
0
}
81
82
/*
83
* Cause the bitstream to consume enough bits that the next bit is on
84
* a byte boundary. This is for getting alignment, which is sometimes
85
* needed.
86
*/
87
void _jxr_rbitstream_syncbyte(struct rbitstream*str)
88
0
{
89
0
    str->bits_avail = 0;
90
0
}
91
92
/*
93
* Get the next 8 bits from the input file and adjust the bitstream
94
* pointers so that the bits can be read out.
95
*/
96
static int get_byte(struct rbitstream*str)
97
0
{
98
0
    int tmp;
99
0
    assert(str->bits_avail == 0);
100
0
    tmp = fgetc(str->fd);
101
0
    if (tmp == EOF)
102
0
        return EOF;
103
104
0
    str->byte = tmp;
105
0
    str->bits_avail = 8;
106
0
    str->read_count += 1;
107
#if 0
108
    DEBUG(" in byte: 0x%02x (bitpos=%zd)\n",
109
        str->byte, str->read_count*8-8);
110
#endif
111
0
    return 0;
112
0
}
113
114
/*
115
* The following read integers of various width from the data input
116
* stream. This handles the bit ordering and packing of the bits.
117
*/
118
int _jxr_rbitstream_uint1(struct rbitstream*str)
119
0
{
120
0
    if (str->bits_avail == 0) {
121
0
        get_byte(str);
122
0
    }
123
124
0
    assert(str->bits_avail > 0);
125
126
0
    str->bits_avail -= 1;
127
0
    return (str->byte & (1 << str->bits_avail))? 1 : 0;
128
0
}
129
130
uint8_t _jxr_rbitstream_uint2(struct rbitstream*str)
131
0
{
132
0
    uint8_t tmp = 0;
133
134
0
    tmp |= _jxr_rbitstream_uint1(str);
135
0
    tmp <<= 1;
136
0
    tmp |= _jxr_rbitstream_uint1(str);
137
0
    return tmp;
138
0
}
139
140
uint8_t _jxr_rbitstream_uint3(struct rbitstream*str)
141
0
{
142
0
    uint8_t tmp = 0;
143
144
0
    tmp |= _jxr_rbitstream_uint1(str);
145
0
    tmp <<= 1;
146
0
    tmp |= _jxr_rbitstream_uint1(str);
147
0
    tmp <<= 1;
148
0
    tmp |= _jxr_rbitstream_uint1(str);
149
0
    return tmp;
150
0
}
151
152
uint8_t _jxr_rbitstream_uint4(struct rbitstream*str)
153
0
{
154
0
    uint8_t tmp;
155
0
    int idx;
156
157
0
    if (str->bits_avail == 0)
158
0
        get_byte(str);
159
160
0
    if (str->bits_avail == 4) {
161
0
        str->bits_avail = 0;
162
0
        return str->byte & 0x0f;
163
0
    }
164
165
0
    tmp = 0;
166
0
    for (idx = 0 ; idx < 4 ; idx += 1) {
167
0
        tmp <<= 1;
168
0
        tmp |= _jxr_rbitstream_uint1(str);
169
0
    }
170
171
0
    return tmp;
172
0
}
173
174
uint8_t _jxr_rbitstream_uint6(struct rbitstream*str)
175
0
{
176
0
    uint8_t tmp;
177
0
    int idx;
178
179
0
    tmp = _jxr_rbitstream_uint4(str);
180
0
    for (idx = 4 ; idx < 6 ; idx += 1) {
181
0
        tmp <<= 1;
182
0
        tmp |= _jxr_rbitstream_uint1(str);
183
0
    }
184
185
0
    return tmp;
186
0
}
187
188
uint8_t _jxr_rbitstream_uint8(struct rbitstream*str)
189
0
{
190
0
    uint8_t tmp;
191
0
    int idx;
192
193
0
    if (str->bits_avail == 0)
194
0
        get_byte(str);
195
196
0
    if (str->bits_avail == 8) {
197
0
        str->bits_avail = 0;
198
0
        return str->byte;
199
0
    }
200
201
0
    tmp = 0;
202
0
    for (idx = 0 ; idx < 8 ; idx += 1) {
203
0
        tmp <<= 1;
204
0
        tmp |= _jxr_rbitstream_uint1(str);
205
0
    }
206
207
0
    return tmp;
208
0
}
209
210
uint16_t _jxr_rbitstream_uint12(struct rbitstream*str)
211
0
{
212
0
    uint16_t tmp = 0;
213
214
0
    tmp = _jxr_rbitstream_uint8(str);
215
0
    tmp <<= 4;
216
0
    tmp |= _jxr_rbitstream_uint4(str);
217
0
    return tmp;
218
0
}
219
220
uint16_t _jxr_rbitstream_uint15(struct rbitstream*str)
221
0
{
222
0
    uint16_t tmp = 0;
223
224
0
    tmp = _jxr_rbitstream_uint8(str);
225
0
    tmp <<= 4;
226
0
    tmp |= _jxr_rbitstream_uint4(str);
227
0
    tmp <<= 3;
228
0
    tmp |= _jxr_rbitstream_uint3(str);
229
0
    return tmp;
230
0
}
231
232
uint16_t _jxr_rbitstream_uint16(struct rbitstream*str)
233
0
{
234
0
    uint16_t tmp = 0;
235
236
0
    tmp = _jxr_rbitstream_uint8(str);
237
0
    tmp <<= 8;
238
0
    tmp |= _jxr_rbitstream_uint8(str);
239
0
    return tmp;
240
0
}
241
242
uint32_t _jxr_rbitstream_uint32(struct rbitstream*str)
243
0
{
244
0
    uint32_t tmp = 0;
245
246
0
    tmp = _jxr_rbitstream_uint16(str);
247
0
    tmp <<= 16;
248
0
    tmp |= _jxr_rbitstream_uint16(str);
249
0
    return tmp;
250
0
}
251
252
uint32_t _jxr_rbitstream_uintN(struct rbitstream*str, int N)
253
0
{
254
0
    uint32_t tmp = 0;
255
0
    assert(N <= 32);
256
257
0
    while (N > 0) {
258
0
        tmp <<= 1;
259
0
        tmp |= _jxr_rbitstream_uint1(str);
260
0
        N -= 1;
261
0
    }
262
0
    return tmp;
263
0
}
264
265
int _jxr_rbitstream_intE(struct rbitstream*str, int code_size,
266
                         const unsigned char*codeb, const signed char*codev)
267
0
{
268
0
    int bits = 0;
269
0
    unsigned val = 0;
270
271
0
    while (codeb[val << (code_size-bits)] != bits) {
272
0
        val <<= 1;
273
0
        val |= _jxr_rbitstream_uint1(str);
274
0
        bits += 1;
275
0
        assert(bits <= code_size);
276
0
    }
277
278
0
    return codev[val << (code_size-bits)];
279
0
}
280
281
int64_t _jxr_rbitstream_intVLW(struct rbitstream*str)
282
0
{
283
0
    uint64_t val = _jxr_rbitstream_uint8(str);
284
0
    if (val < 0xfb) {
285
0
        uint64_t tmp = _jxr_rbitstream_uint8(str);
286
0
        val = val*256 + tmp;
287
288
0
    } else if (val == 0xfb) {
289
0
        val = _jxr_rbitstream_uint32(str);
290
291
0
    } else if (val == 0xfc) {
292
0
        uint64_t tmp;
293
0
        val = _jxr_rbitstream_uint32(str);
294
0
        tmp = _jxr_rbitstream_uint32(str);
295
0
        val = (val << (uint64_t)32) + tmp;
296
297
0
    } else {
298
0
        return 0;
299
0
    }
300
301
0
    return val;
302
0
}
303
304
void _jxr_wbitstream_initialize(struct wbitstream*str, FILE*fd)
305
0
{
306
0
    str->byte = 0;
307
0
    str->bits_ready = 0;
308
0
    str->fd = fd;
309
0
    str->write_count = 0;
310
0
}
311
312
size_t _jxr_wbitstream_bitpos(struct wbitstream*str)
313
0
{
314
0
    return str->write_count*8 + str->bits_ready;
315
0
}
316
317
static void put_byte(struct wbitstream*str)
318
0
{
319
0
    assert(str->bits_ready == 8);
320
0
    fputc(str->byte, str->fd);
321
0
    str->byte = 0;
322
0
    str->bits_ready = 0;
323
0
    str->write_count += 1;
324
0
}
325
326
void _jxr_wbitstream_syncbyte(struct wbitstream*str)
327
0
{
328
0
    if (str->bits_ready > 0)
329
0
        str->bits_ready = 8;
330
0
}
331
332
void _jxr_wbitstream_flush(struct wbitstream*str)
333
0
{
334
0
    _jxr_wbitstream_syncbyte(str);
335
0
    if (str->bits_ready > 0)
336
0
        put_byte(str);
337
0
}
338
339
void _jxr_wbitstream_uint1(struct wbitstream*str, int val)
340
0
{
341
0
    if (str->bits_ready == 8)
342
0
        put_byte(str);
343
344
0
    if (val)
345
0
        str->byte |= 0x80 >> str->bits_ready;
346
0
    str->bits_ready += 1;
347
0
}
348
349
void _jxr_wbitstream_uint2(struct wbitstream*str, uint8_t val)
350
0
{
351
0
    int idx;
352
0
    for (idx = 0 ; idx < 2 ; idx += 1) {
353
0
        _jxr_wbitstream_uint1(str, val & (0x02 >> idx));
354
0
    }
355
0
}
356
357
void _jxr_wbitstream_uint3(struct wbitstream*str, uint8_t val)
358
0
{
359
0
    int idx;
360
0
    for (idx = 0 ; idx < 3 ; idx += 1) {
361
0
        _jxr_wbitstream_uint1(str, val & (0x04 >> idx));
362
0
    }
363
0
}
364
365
void _jxr_wbitstream_uint4(struct wbitstream*str, uint8_t val)
366
0
{
367
0
    int idx;
368
0
    for (idx = 0 ; idx < 4 ; idx += 1) {
369
0
        _jxr_wbitstream_uint1(str, val & (0x08 >> idx));
370
0
    }
371
0
}
372
373
void _jxr_wbitstream_uint6(struct wbitstream*str, uint8_t val)
374
0
{
375
0
    int idx;
376
0
    for (idx = 0 ; idx < 6 ; idx += 1) {
377
0
        _jxr_wbitstream_uint1(str, val & (0x20 >> idx));
378
0
    }
379
0
}
380
381
void _jxr_wbitstream_uint8(struct wbitstream*str, uint8_t val)
382
0
{
383
0
    int idx;
384
385
0
    if (str->bits_ready == 8)
386
0
        put_byte(str);
387
388
0
    if (str->bits_ready == 0) {
389
0
        str->bits_ready = 8;
390
0
        str->byte = val;
391
0
        return;
392
0
    }
393
0
    for (idx = 0 ; idx < 8 ; idx += 1) {
394
0
        _jxr_wbitstream_uint1(str, val & (0x80 >> idx));
395
0
    }
396
0
}
397
398
void _jxr_wbitstream_uint12(struct wbitstream*str, uint16_t val)
399
0
{
400
0
    int idx;
401
0
    for (idx = 0 ; idx < 12 ; idx += 1) {
402
0
        _jxr_wbitstream_uint1(str, val & (0x800 >> idx));
403
0
    }
404
0
}
405
406
void _jxr_wbitstream_uint15(struct wbitstream*str, uint16_t val)
407
0
{
408
0
    int idx;
409
0
    for (idx = 0 ; idx < 15 ; idx += 1) {
410
0
        _jxr_wbitstream_uint1(str, val & (0x4000 >> idx));
411
0
    }
412
0
}
413
414
void _jxr_wbitstream_uint16(struct wbitstream*str, uint16_t val)
415
0
{
416
0
    _jxr_wbitstream_uint8(str, val >> 8);
417
0
    _jxr_wbitstream_uint8(str, val >> 0);
418
0
}
419
420
void _jxr_wbitstream_uint32(struct wbitstream*str, uint32_t val)
421
0
{
422
0
    _jxr_wbitstream_uint8(str, val >> 24);
423
0
    _jxr_wbitstream_uint8(str, val >> 16);
424
0
    _jxr_wbitstream_uint8(str, val >> 8);
425
0
    _jxr_wbitstream_uint8(str, val >> 0);
426
0
}
427
428
void _jxr_wbitstream_uintN(struct wbitstream*str, uint32_t val, int N)
429
0
{
430
0
    assert(N <= 32);
431
0
    while (N > 0) {
432
0
        _jxr_wbitstream_uint1(str, 1 & (val >> (N-1)));
433
0
        N -= 1;
434
0
    }
435
0
}
436
437
void _jxr_wbitstream_intVLW(struct wbitstream*str, uint64_t val)
438
0
{
439
0
    if (val == 0) {
440
0
        _jxr_wbitstream_uint8(str, 0xfe);
441
0
    } else if (val < 0xfb00) {
442
0
        _jxr_wbitstream_uint16(str, (uint16_t)val);
443
0
    } else if (val < 0x100000000ULL) {
444
0
        _jxr_wbitstream_uint8(str, 0xfb);
445
0
        _jxr_wbitstream_uint32(str, (uint32_t)val);
446
0
    } else {
447
0
        _jxr_wbitstream_uint8(str, 0xfc);
448
0
        _jxr_wbitstream_uint32(str, val >> 32ULL);
449
0
        _jxr_wbitstream_uint32(str, val >> 0ULL);
450
0
    }
451
0
}
452
453
void _jxr_wbitstream_mark(struct wbitstream*str)
454
0
{
455
0
    if (str->bits_ready == 8)
456
0
        _jxr_wbitstream_flush(str);
457
458
0
    assert(str->bits_ready == 0);
459
    /* str->mark_stream_position = ftell(str->fd); */
460
0
    str->write_count = 0;
461
0
}
462
463
const char* _jxr_vlc_index_name(int vlc)
464
0
{
465
0
    switch (vlc) {
466
0
        case AbsLevelIndDCLum: return "AbsLevelIndDCLum";
467
0
        case AbsLevelIndDCChr: return "AbsLevelIndDCChr";
468
0
        case DecFirstIndLPLum: return "DecFirstIndLPLum";
469
0
        case AbsLevelIndLP0: return "AbsLevelIndLP0";
470
0
        case AbsLevelIndLP1: return "AbsLevelIndLP1";
471
0
        case AbsLevelIndHP0: return "AbsLevelIndHP0";
472
0
        case AbsLevelIndHP1: return "AbsLevelIndHP1";
473
0
        case DecIndLPLum0: return "DecIndLPLum0";
474
0
        case DecIndLPLum1: return "DecIndLPLum1";
475
0
        case DecFirstIndLPChr: return "DecFirstIndLPChr";
476
0
        case DecIndLPChr0: return "DecIndLPChr0";
477
0
        case DecIndLPChr1: return "DecIndLPChr1";
478
0
        case DecNumCBP: return "DecNumCBP";
479
0
        case DecNumBlkCBP: return "DecNumBlkCBP";
480
0
        case DecIndHPLum0: return "DecIndHPLum0";
481
0
        case DecIndHPLum1: return "DecIndHPLum1";
482
0
        case DecFirstIndHPLum: return "DecFirstIndHPLum";
483
0
        case DecFirstIndHPChr: return "DecFirstIndHPChr";
484
0
        case DecIndHPChr0: return "DecIndHPChr0";
485
0
        case DecIndHPChr1: return "DecIndHPChr1";
486
0
        default: return "?????";
487
0
    }
488
0
}
489
490
491
/*
492
* $Log: io.c,v $
493
* Revision 1.16 2009/05/29 12:00:00 microsoft
494
* Reference Software v1.6 updates.
495
*
496
* Revision 1.15 2009/04/13 12:00:00 microsoft
497
* Reference Software v1.5 updates.
498
*
499
* Revision 1.14 2008/03/05 06:58:10 gus
500
* *** empty log message ***
501
*
502
* Revision 1.13 2008/02/28 18:50:31 steve
503
* Portability fixes.
504
*
505
* Revision 1.12 2008/02/26 23:52:44 steve
506
* Remove ident for MS compilers.
507
*
508
* Revision 1.11 2007/12/07 01:20:34 steve
509
* Fix adapt not adapting on line ends.
510
*
511
* Revision 1.10 2007/12/06 17:53:35 steve
512
* No longer need bitval dump.
513
*
514
* Revision 1.9 2007/11/30 01:50:58 steve
515
* Compression of DCONLY GRAY.
516
*
517
* Revision 1.8 2007/11/26 01:47:15 steve
518
* Add copyright notices per MS request.
519
*
520
* Revision 1.7 2007/11/19 18:22:34 steve
521
* Skip ESCaped FLEXBITS tiles.
522
*
523
* Revision 1.6 2007/11/14 23:56:17 steve
524
* Fix TILE ordering, using seeks, for FREQUENCY mode.
525
*
526
* Revision 1.5 2007/11/08 19:38:38 steve
527
* Get stub DCONLY compression to work.
528
*
529
* Revision 1.4 2007/11/08 02:52:32 steve
530
* Some progress in some encoding infrastructure.
531
*
532
* Revision 1.3 2007/08/15 01:54:11 steve
533
* Add level2 filter to decoder.
534
*
535
* Revision 1.2 2007/06/21 17:31:22 steve
536
* Successfully parse LP components.
537
*
538
* Revision 1.1 2007/06/06 17:19:12 steve
539
* Introduce to CVS.
540
*
541
*/
542