Grok  9.7.5
coding_units.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019 - 2021, Osamu Watanabe
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <cstdint>
32 #include <vector>
33 #include "open_htj2k_typedef.hpp"
34 #include <cassert>
35 #include <string>
36 #include <memory>
37 #include "utils.hpp"
38 #include <cstring>
39 #include <functional>
40 
41 /********************************************************************************
42  * j2k_region
43  *******************************************************************************/
44 class j2k_region {
45  public:
46  // top-left coordinate (inclusive) of a region in the reference grid
48  // bottom-right coordinate (exclusive) of a region in the reference grid
50  // return top-left coordinate (inclusive)
51  element_siz get_pos0() const { return pos0; }
52  // return bottom-right coordinate (exclusive)
53  element_siz get_pos1() const { return pos1; }
54  // get size of a region
55  void get_size(element_siz &out) const {
56  out.x = pos1.x - pos0.x;
57  out.y = pos1.y - pos0.y;
58  }
59  // set top-left coordinate (inclusive)
60  void set_pos0(element_siz in) { pos0 = in; }
61  // set bottom-right coordinate (exclusive)
62  void set_pos1(element_siz in) { pos1 = in; }
63  j2k_region() = default;
64  j2k_region(element_siz p0, element_siz p1) : pos0(p0), pos1(p1) {}
65 };
66 
67 /********************************************************************************
68  * j2k_codeblock
69  *******************************************************************************/
70 class j2k_codeblock : public j2k_region {
71  public:
73 
74  private:
75  const uint32_t index;
76  const uint8_t band;
77  const uint8_t M_b;
78  std::unique_ptr<uint8_t[]> compressed_data;
79  uint8_t *current_address;
80 
81  public:
82  std::unique_ptr<uint8_t[]> block_states;
83  const uint8_t R_b;
84  const uint8_t transformation;
85  const float stepsize;
86  const uint32_t band_stride;
87  const uint16_t num_layers;
88  std::unique_ptr<int32_t[]> sample_buf;
90  float *const f_samples;
91  uint32_t length;
92  uint16_t Cmodes;
93  uint8_t num_passes;
94  uint8_t num_ZBP;
96  uint32_t Lblock;
97  // length of a coding pass in byte
98  std::vector<uint32_t> pass_length;
99  // index of the coding-pass from which layer starts
100  std::unique_ptr<uint8_t[]> layer_start;
101  // number of coding-passes included in a layer
102  std::unique_ptr<uint8_t[]> layer_passes;
104 
105  j2k_codeblock(const uint32_t &idx, uint8_t orientation, uint8_t M_b, uint8_t R_b, uint8_t transformation,
106  float stepsize, uint32_t band_stride, sprec_t *ibuf, float *fbuf, uint32_t offset,
107  const uint16_t &numlayers, const uint8_t &codeblock_style, const element_siz &p0,
108  const element_siz &p1, const element_siz &s);
109  void modify_state(const std::function<void(uint8_t &, uint8_t)> &callback, uint8_t val, int16_t j1,
110  int16_t j2) {
111  callback(block_states[(j1 + 1) * (size.x + 2) + (j2 + 1)], val);
112  }
113  uint8_t get_state(const std::function<uint8_t(uint8_t &)> &callback, int16_t j1, int16_t j2) const {
114  return callback(block_states[(j1 + 1) * (size.x + 2) + (j2 + 1)]);
115  }
116  // DEBUG FUNCTION, SOON BE DELETED
117  uint8_t get_orientation() const { return band; }
118  uint8_t get_context_label_sig(const uint16_t &j1, const uint16_t &j2) const;
119  uint8_t get_signLUT_index(const uint16_t &j1, const uint16_t &j2) const;
120  uint8_t get_Mb() const;
121  uint8_t *get_compressed_data();
122  void set_compressed_data(uint8_t *buf, uint16_t size);
123  float *get_fsample_addr(const int16_t &j1, const int16_t &j2) const;
124  void update_sample(const uint8_t &symbol, const uint8_t &p, const uint16_t &j1, const uint16_t &j2) const;
125  void update_sign(const int8_t &val, const uint16_t &j1, const uint16_t &j2) const;
126  uint8_t get_sign(const uint16_t &j1, const uint16_t &j2) const;
127  void set_MagSgn_and_sigma(uint32_t &or_val);
128  void calc_mbr(uint8_t &mbr, uint16_t i, uint16_t j, uint32_t mbr_info, uint8_t causal_cond) const;
129 };
130 
131 int32_t htj2k_encode(j2k_codeblock *block, uint8_t ROIshift) noexcept;
Definition: open_htj2k_typedef.hpp:41
uint32_t x
Definition: open_htj2k_typedef.hpp:43
uint32_t y
Definition: open_htj2k_typedef.hpp:44
Definition: coding_units.hpp:70
bool already_included
Definition: coding_units.hpp:103
const uint32_t band_stride
Definition: coding_units.hpp:86
void set_compressed_data(uint8_t *buf, uint16_t size)
Definition: coding_units.cpp:83
uint8_t fast_skip_passes
Definition: coding_units.hpp:95
std::unique_ptr< uint8_t[]> layer_start
Definition: coding_units.hpp:100
void calc_mbr(uint8_t &mbr, uint16_t i, uint16_t j, uint32_t mbr_info, uint8_t causal_cond) const
Definition: ht_block_decoding.cpp:46
const uint8_t M_b
Definition: coding_units.hpp:77
j2k_codeblock(const uint32_t &idx, uint8_t orientation, uint8_t M_b, uint8_t R_b, uint8_t transformation, float stepsize, uint32_t band_stride, sprec_t *ibuf, float *fbuf, uint32_t offset, const uint16_t &numlayers, const uint8_t &codeblock_style, const element_siz &p0, const element_siz &p1, const element_siz &s)
Definition: coding_units.cpp:40
void update_sample(const uint8_t &symbol, const uint8_t &p, const uint16_t &j1, const uint16_t &j2) const
std::unique_ptr< uint8_t[]> layer_passes
Definition: coding_units.hpp:102
const uint32_t index
Definition: coding_units.hpp:75
uint8_t num_ZBP
Definition: coding_units.hpp:94
void set_MagSgn_and_sigma(uint32_t &or_val)
Definition: ht_block_encoding.cpp:45
uint8_t get_sign(const uint16_t &j1, const uint16_t &j2) const
std::unique_ptr< uint8_t[]> block_states
Definition: coding_units.hpp:82
uint32_t Lblock
Definition: coding_units.hpp:96
uint8_t num_passes
Definition: coding_units.hpp:93
uint8_t get_context_label_sig(const uint16_t &j1, const uint16_t &j2) const
const uint8_t transformation
Definition: coding_units.hpp:84
void modify_state(const std::function< void(uint8_t &, uint8_t)> &callback, uint8_t val, int16_t j1, int16_t j2)
Definition: coding_units.hpp:109
uint8_t * get_compressed_data()
Definition: coding_units.cpp:81
const uint8_t R_b
Definition: coding_units.hpp:83
void update_sign(const int8_t &val, const uint16_t &j1, const uint16_t &j2) const
float *const f_samples
Definition: coding_units.hpp:90
uint8_t get_orientation() const
Definition: coding_units.hpp:117
uint8_t get_Mb() const
Definition: coding_units.cpp:79
std::unique_ptr< int32_t[]> sample_buf
Definition: coding_units.hpp:88
std::vector< uint32_t > pass_length
Definition: coding_units.hpp:98
const float stepsize
Definition: coding_units.hpp:85
sprec_t *const i_samples
Definition: coding_units.hpp:89
std::unique_ptr< uint8_t[]> compressed_data
Definition: coding_units.hpp:78
float * get_fsample_addr(const int16_t &j1, const int16_t &j2) const
uint8_t * current_address
Definition: coding_units.hpp:79
uint32_t length
Definition: coding_units.hpp:91
uint8_t get_state(const std::function< uint8_t(uint8_t &)> &callback, int16_t j1, int16_t j2) const
Definition: coding_units.hpp:113
uint8_t get_signLUT_index(const uint16_t &j1, const uint16_t &j2) const
const uint8_t band
Definition: coding_units.hpp:76
uint16_t Cmodes
Definition: coding_units.hpp:92
const uint16_t num_layers
Definition: coding_units.hpp:87
const element_siz size
Definition: coding_units.hpp:72
Definition: coding_units.hpp:44
j2k_region()=default
void get_size(element_siz &out) const
Definition: coding_units.hpp:55
void set_pos0(element_siz in)
Definition: coding_units.hpp:60
j2k_region(element_siz p0, element_siz p1)
Definition: coding_units.hpp:64
element_siz get_pos0() const
Definition: coding_units.hpp:51
element_siz pos1
Definition: coding_units.hpp:49
element_siz get_pos1() const
Definition: coding_units.hpp:53
element_siz pos0
Definition: coding_units.hpp:47
void set_pos1(element_siz in)
Definition: coding_units.hpp:62
int32_t htj2k_encode(j2k_codeblock *block, uint8_t ROIshift) noexcept
Definition: ht_block_encoding.cpp:433
int32_t sprec_t
Definition: open_htj2k_typedef.hpp:37