Coverage Report

Created: 2026-10-28 08:05

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