OpenJPH: /build/openjph/src/OpenJPH/src/core/codestream/ojph_bitbuffer_write.h Source File

OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
ojph_bitbuffer_write.h
Go to the documentation of this file.
1//***************************************************************************/
2// This software is released under the 2-Clause BSD license, included
3// below.
4//
5// Copyright (c) 2019, Aous Naman
6// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2019, The University of New South Wales, Australia
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// 1. Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//
16// 2. Redistributions in binary form must reproduce the above copyright
17// notice, this list of conditions and the following disclaimer in the
18// documentation and/or other materials provided with the distribution.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//***************************************************************************/
32// This file is part of the OpenJPH software implementation.
33// File: ojph_bitbuffer_write.h
34// Author: Aous Naman
35// Date: 28 August 2019
36//***************************************************************************/
37
38
39#ifndef OJPH_BITBUFFER_WRITE_H
40#define OJPH_BITBUFFER_WRITE_H
41
42#include "ojph_defs.h"
43#include "ojph_file.h"
44
45namespace ojph {
46
48 //defined elsewhere
49 class mem_elastic_allocator;
50 struct coded_lists;
51
52 namespace local {
53
55 struct bit_write_buf
56 {
57 static const int needed;
58
59 bit_write_buf() { ccl = NULL; avail_bits = 0; tmp = 0; }
60 coded_lists* ccl;
61 int avail_bits;
62 ui64 tmp;
63 };
64
66 const int bit_write_buf::needed = 512;
67
69 static inline
70 void bb_expand_buf(bit_write_buf *bbp, mem_elastic_allocator *elastic,
71 coded_lists*& cur_coded_list)
72 {
73 assert(cur_coded_list == NULL);
74 elastic->get_buffer(bit_write_buf::needed, cur_coded_list);
75 bbp->ccl = cur_coded_list;
76 bbp->tmp = 0;
77 }
78
80 static inline
81 void bb_init(bit_write_buf *bbp, mem_elastic_allocator *elastic,
82 coded_lists*& cur_coded_list)
83 {
84 bb_expand_buf(bbp, elastic, cur_coded_list);
85 bbp->avail_bits = 8;
86 }
87
89 static inline
90 void bb_put_bit(bit_write_buf *bbp, ui32 bit,
91 mem_elastic_allocator *elastic,
92 coded_lists*& cur_coded_list, ui32& ph_bytes)
93 {
94 --bbp->avail_bits;
95 bbp->tmp |= (bit & 1) << bbp->avail_bits;
96 if (bbp->avail_bits <= 0)
97 {
98 bbp->avail_bits = 8 - (bbp->tmp != 0xFF ? 0 : 1);
99 bbp->ccl->buf[bbp->ccl->buf_size - bbp->ccl->avail_size] =
100 (ui8)(bbp->tmp & 0xFF);
101 bbp->tmp = 0;
102 --bbp->ccl->avail_size;
103 if (bbp->ccl->avail_size == 0)
104 {
105 bb_expand_buf(bbp, elastic, cur_coded_list->next_list);
106 cur_coded_list = cur_coded_list->next_list;
107 ph_bytes += bit_write_buf::needed;
108 }
109 }
110 }
111
113 static inline
114 void bb_put_zeros(bit_write_buf *bbp, int num_zeros,
115 mem_elastic_allocator *elastic,
116 coded_lists*& cur_coded_list, ui32& ph_bytes)
117 {
118 for (int i = num_zeros; i > 0; --i)
119 bb_put_bit(bbp, 0, elastic, cur_coded_list, ph_bytes);
120 }
121
123 static inline
124 void bb_put_bits(bit_write_buf *bbp, ui32 data, int num_bits,
125 mem_elastic_allocator *elastic,
126 coded_lists*& cur_coded_list, ui32& ph_bytes)
127 {
128 assert(num_bits <= 32);
129 for (int i = num_bits - 1; i >= 0; --i)
130 bb_put_bit(bbp, data >> i, elastic, cur_coded_list, ph_bytes);
131 }
132
134 static inline
135 void bb_terminate(bit_write_buf *bbp)
136 {
137 if (bbp->avail_bits < 8) //bits have been written
138 {
139 ui8 val = (ui8)(bbp->tmp & 0xFF);
140 bbp->ccl->buf[bbp->ccl->buf_size - bbp->ccl->avail_size] = val;
141 --bbp->ccl->avail_size;
142 }
143 }
144
145 }
146}
147#endif // !OJPH_BITBUFFER_WRITE_H
ojph::mem_elastic_allocator
Definition ojph_mem.h:218
ojph::mem_elastic_allocator::get_buffer
void get_buffer(ui32 needed_bytes, coded_lists *&p)
Definition ojph_mem.cpp:113
ojph::local
Definition ojph_bitbuffer_read.h:53
ojph::local::bb_terminate
static bool bb_terminate(bit_read_buf *bbp, bool uses_eph)
Definition ojph_bitbuffer_read.h:169
ojph::local::bb_put_zeros
static void bb_put_zeros(bit_write_buf *bbp, int num_zeros, mem_elastic_allocator *elastic, coded_lists *&cur_coded_list, ui32 &ph_bytes)
Definition ojph_bitbuffer_write.h:114
ojph::local::bb_put_bits
static void bb_put_bits(bit_write_buf *bbp, ui32 data, int num_bits, mem_elastic_allocator *elastic, coded_lists *&cur_coded_list, ui32 &ph_bytes)
Definition ojph_bitbuffer_write.h:124
ojph::local::bb_init
static void bb_init(bit_read_buf *bbp, ui32 bytes_left, infile_base *file)
Definition ojph_bitbuffer_read.h:68
ojph::local::bb_put_bit
static void bb_put_bit(bit_write_buf *bbp, ui32 bit, mem_elastic_allocator *elastic, coded_lists *&cur_coded_list, ui32 &ph_bytes)
Definition ojph_bitbuffer_write.h:90
ojph::local::bb_expand_buf
static void bb_expand_buf(bit_write_buf *bbp, mem_elastic_allocator *elastic, coded_lists *&cur_coded_list)
Definition ojph_bitbuffer_write.h:70
ojph
Definition ojph_img_io.h:52
ojph::ui64
uint64_t ui64
Definition ojph_defs.h:56
ojph::ui32
uint32_t ui32
Definition ojph_defs.h:54
ojph::ui8
uint8_t ui8
Definition ojph_defs.h:50
ojph_defs.h
ojph_file.h
ojph::coded_lists
Definition ojph_mem.h:202
ojph::coded_lists::buf_size
ui32 buf_size
Definition ojph_mem.h:211
ojph::coded_lists::buf
ui8 * buf
Definition ojph_mem.h:213
ojph::coded_lists::next_list
coded_lists * next_list
Definition ojph_mem.h:210
ojph::coded_lists::avail_size
ui32 avail_size
Definition ojph_mem.h:212
ojph::local::bit_write_buf
Definition ojph_bitbuffer_write.h:56
ojph::local::bit_write_buf::avail_bits
int avail_bits
Definition ojph_bitbuffer_write.h:61
ojph::local::bit_write_buf::needed
static const int needed
Definition ojph_bitbuffer_write.h:57
ojph::local::bit_write_buf::tmp
ui64 tmp
Definition ojph_bitbuffer_write.h:62
ojph::local::bit_write_buf::ccl
coded_lists * ccl
Definition ojph_bitbuffer_write.h:60
ojph::local::bit_write_buf::bit_write_buf
bit_write_buf()
Definition ojph_bitbuffer_write.h:59