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
56 {
57 static const int needed;
58
59 bit_write_buf() { ccl = NULL; avail_bits = 0; tmp = 0; }
63 };
64
66 const int bit_write_buf::needed = 512;
67
69 static inline
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
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
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
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
void get_buffer(ui32 needed_bytes, coded_lists *&p)
Definition ojph_mem.cpp:115
static bool bb_terminate(bit_read_buf *bbp, bool uses_eph)
static void bb_put_zeros(bit_write_buf *bbp, int num_zeros, mem_elastic_allocator *elastic, coded_lists *&cur_coded_list, ui32 &ph_bytes)
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)
static void bb_init(bit_read_buf *bbp, ui32 bytes_left, infile_base *file)
static void bb_put_bit(bit_write_buf *bbp, ui32 bit, mem_elastic_allocator *elastic, coded_lists *&cur_coded_list, ui32 &ph_bytes)
static void bb_expand_buf(bit_write_buf *bbp, mem_elastic_allocator *elastic, coded_lists *&cur_coded_list)
uint64_t ui64
Definition ojph_defs.h:56
uint32_t ui32
Definition ojph_defs.h:54
uint8_t ui8
Definition ojph_defs.h:50
coded_lists * next_list
Definition ojph_mem.h:197