Grok  10.0.3
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 
34 #ifndef _MSC_VER
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wconversion"
37 #pragma GCC diagnostic ignored "-Wsign-conversion"
38 #pragma GCC diagnostic ignored "-Wunused-parameter"
39 #pragma GCC diagnostic ignored "-Wunused-variable"
40 #pragma GCC diagnostic ignored "-Wsign-compare"
41 #pragma GCC diagnostic ignored "-Wparentheses"
42 #endif
43 
44 #include "open_htj2k_typedef.hpp"
45 #include <cassert>
46 #include <string>
47 #include <memory>
48 #include "utils.hpp"
49 #include <cstring>
50 #include <functional>
51 
52 /********************************************************************************
53  * j2k_region
54  *******************************************************************************/
55 class j2k_region {
56  public:
57  // top-left coordinate (inclusive) of a region in the reference grid
59  // bottom-right coordinate (exclusive) of a region in the reference grid
61  // return top-left coordinate (inclusive)
62  element_siz get_pos0() const { return pos0; }
63  // return bottom-right coordinate (exclusive)
64  element_siz get_pos1() const { return pos1; }
65  // get size of a region
66  void get_size(element_siz &out) const {
67  out.x = pos1.x - pos0.x;
68  out.y = pos1.y - pos0.y;
69  }
70  // set top-left coordinate (inclusive)
71  void set_pos0(element_siz in) { pos0 = in; }
72  // set bottom-right coordinate (exclusive)
73  void set_pos1(element_siz in) { pos1 = in; }
74  j2k_region() = default;
75  j2k_region(element_siz p0, element_siz p1) : pos0(p0), pos1(p1) {}
76 };
77 
78 /********************************************************************************
79  * j2k_codeblock
80  *******************************************************************************/
81 class j2k_codeblock : public j2k_region {
82  public:
84 
85  private:
86  const uint32_t index;
87  const uint8_t band;
88  const uint8_t M_b;
89  std::unique_ptr<uint8_t[]> compressed_data;
90  uint8_t *current_address;
91 
92  public:
93  std::unique_ptr<uint8_t[]> block_states;
94  const uint8_t R_b;
95  const uint8_t transformation;
96  const float stepsize;
97  const uint32_t band_stride;
98  const uint16_t num_layers;
99  std::unique_ptr<int32_t[]> sample_buf;
101  float *const f_samples;
102  uint32_t length;
103  uint16_t Cmodes;
104  uint8_t num_passes;
105  uint8_t num_ZBP;
107  uint32_t Lblock;
108  // length of a coding pass in byte
109  std::vector<uint32_t> pass_length;
110  // index of the coding-pass from which layer starts
111  std::unique_ptr<uint8_t[]> layer_start;
112  // number of coding-passes included in a layer
113  std::unique_ptr<uint8_t[]> layer_passes;
115 
116  j2k_codeblock(const uint32_t &idx, uint8_t orientation, uint8_t M_b, uint8_t R_b, uint8_t transformation,
117  float stepsize, uint32_t band_stride, sprec_t *ibuf, float *fbuf, uint32_t offset,
118  const uint16_t &numlayers, const uint8_t &codeblock_style, const element_siz &p0,
119  const element_siz &p1, const element_siz &s);
120  void modify_state(const std::function<void(uint8_t &, uint8_t)> &callback, uint8_t val, int16_t j1,
121  int16_t j2) {
122  callback(block_states[(j1 + 1) * (size.x + 2) + (j2 + 1)], val);
123  }
124  uint8_t get_state(const std::function<uint8_t(uint8_t &)> &callback, int16_t j1, int16_t j2) const {
125  return callback(block_states[(j1 + 1) * (size.x + 2) + (j2 + 1)]);
126  }
127  // DEBUG FUNCTION, SOON BE DELETED
128  uint8_t get_orientation() const { return band; }
129  uint8_t get_context_label_sig(const uint16_t &j1, const uint16_t &j2) const;
130  uint8_t get_signLUT_index(const uint16_t &j1, const uint16_t &j2) const;
131  uint8_t get_Mb() const;
132  uint8_t *get_compressed_data();
133  void set_compressed_data(uint8_t *buf, uint16_t size);
134  float *get_fsample_addr(const int16_t &j1, const int16_t &j2) const;
135  void update_sample(const uint8_t &symbol, const uint8_t &p, const uint16_t &j1, const uint16_t &j2) const;
136  void update_sign(const int8_t &val, const uint16_t &j1, const uint16_t &j2) const;
137  uint8_t get_sign(const uint16_t &j1, const uint16_t &j2) const;
138  void set_MagSgn_and_sigma(uint32_t &or_val);
139  void calc_mbr(uint8_t &mbr, uint16_t i, uint16_t j, uint32_t mbr_info, uint8_t causal_cond) const;
140 };
141 
142 int32_t htj2k_encode(j2k_codeblock *block, uint8_t ROIshift) noexcept;
143 
144 
145 #ifndef _MSC_VER
146 #pragma GCC diagnostic pop
147 #endif
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:81
bool already_included
Definition: coding_units.hpp:114
const uint32_t band_stride
Definition: coding_units.hpp:97
void set_compressed_data(uint8_t *buf, uint16_t size)
Definition: coding_units.cpp:83
uint8_t fast_skip_passes
Definition: coding_units.hpp:106
std::unique_ptr< uint8_t[]> layer_start
Definition: coding_units.hpp:111
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:55
const uint8_t M_b
Definition: coding_units.hpp:88
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:113
const uint32_t index
Definition: coding_units.hpp:86
uint8_t num_ZBP
Definition: coding_units.hpp:105
void set_MagSgn_and_sigma(uint32_t &or_val)
Definition: ht_block_encoding.cpp:54
uint8_t get_sign(const uint16_t &j1, const uint16_t &j2) const
std::unique_ptr< uint8_t[]> block_states
Definition: coding_units.hpp:93
uint32_t Lblock
Definition: coding_units.hpp:107
uint8_t num_passes
Definition: coding_units.hpp:104
uint8_t get_context_label_sig(const uint16_t &j1, const uint16_t &j2) const
const uint8_t transformation
Definition: coding_units.hpp:95
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:120
uint8_t * get_compressed_data()
Definition: coding_units.cpp:81
const uint8_t R_b
Definition: coding_units.hpp:94
void update_sign(const int8_t &val, const uint16_t &j1, const uint16_t &j2) const
float *const f_samples
Definition: coding_units.hpp:101
uint8_t get_orientation() const
Definition: coding_units.hpp:128
uint8_t get_Mb() const
Definition: coding_units.cpp:79
std::unique_ptr< int32_t[]> sample_buf
Definition: coding_units.hpp:99
std::vector< uint32_t > pass_length
Definition: coding_units.hpp:109
const float stepsize
Definition: coding_units.hpp:96
sprec_t *const i_samples
Definition: coding_units.hpp:100
std::unique_ptr< uint8_t[]> compressed_data
Definition: coding_units.hpp:89
float * get_fsample_addr(const int16_t &j1, const int16_t &j2) const
uint8_t * current_address
Definition: coding_units.hpp:90
uint32_t length
Definition: coding_units.hpp:102
uint8_t get_state(const std::function< uint8_t(uint8_t &)> &callback, int16_t j1, int16_t j2) const
Definition: coding_units.hpp:124
uint8_t get_signLUT_index(const uint16_t &j1, const uint16_t &j2) const
const uint8_t band
Definition: coding_units.hpp:87
uint16_t Cmodes
Definition: coding_units.hpp:103
const uint16_t num_layers
Definition: coding_units.hpp:98
const element_siz size
Definition: coding_units.hpp:83
Definition: coding_units.hpp:55
j2k_region()=default
void get_size(element_siz &out) const
Definition: coding_units.hpp:66
void set_pos0(element_siz in)
Definition: coding_units.hpp:71
j2k_region(element_siz p0, element_siz p1)
Definition: coding_units.hpp:75
element_siz get_pos0() const
Definition: coding_units.hpp:62
element_siz pos1
Definition: coding_units.hpp:60
element_siz get_pos1() const
Definition: coding_units.hpp:64
element_siz pos0
Definition: coding_units.hpp:58
void set_pos1(element_siz in)
Definition: coding_units.hpp:73
int32_t htj2k_encode(j2k_codeblock *block, uint8_t ROIshift) noexcept
Definition: ht_block_encoding.cpp:442
int32_t sprec_t
Definition: open_htj2k_typedef.hpp:37