Coverage Report

Created: 2026-04-09 07:06

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