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