OpenJPH
Open-source implementation of JPEG2000 Part-15
Loading...
Searching...
No Matches
test_mixed_coc.cpp
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) Pierre-Anthony Lemieux
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// 2. Redistributions in binary form must reproduce the above copyright
15// notice, this list of conditions and the following disclaimer in the
16// documentation and/or other materials provided with the distribution.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//***************************************************************************/
30// This file is part of the OpenJPH software implementation.
31// File: test_mixed_coc.cpp
32// Author: Pierre-Anthony Lemieux
33// Date: 29 June 2026
34//***************************************************************************/
35
36#include <cstring>
37#include <vector>
38
39#include "ojph_mem.h"
40#include "ojph_file.h"
41#include "ojph_codestream.h"
42#include "ojph_params.h"
43#include "gtest/gtest.h"
44
46// Test encoding a 4-component image where 3 components use the irreversible
47// (9/7) wavelet and 1 component uses the reversible (5/3) wavelet.
48// This exercises COC and QCC marker generation for mixed coding modes.
49// Verifies that encoding succeeds, headers can be read back, and the
50// per-component coding settings are preserved.
51TEST(TestMixedCOC, FourCompMixedReversibility) {
52 const ojph::ui32 width = 64;
53 const ojph::ui32 height = 64;
54 const ojph::ui32 num_comps = 4;
55 const ojph::ui32 bit_depth = 8;
56 const ojph::ui32 num_decomps = 5;
57
58 // samples fed into component 3 during encoding, kept around so they can
59 // be compared against the decoded output further down
60 std::vector<ojph::si32> comp3_orig(width * height);
61
62 // encode
63 {
64 ojph::codestream codestream;
65
66 ojph::param_siz siz = codestream.access_siz();
67 siz.set_image_extent(ojph::point(width, height));
68 siz.set_num_components(num_comps);
69 for (ojph::ui32 c = 0; c < num_comps; ++c)
70 siz.set_component(c, ojph::point(1, 1), bit_depth, false);
72 siz.set_tile_size(ojph::size(width, height));
74
75 ojph::param_cod cod = codestream.access_cod();
76 cod.set_num_decomposition(num_decomps);
77 cod.set_block_dims(64, 64);
78 cod.set_color_transform(false);
79 cod.set_reversible(false);
80
81 cod.set_reversible(3, true);
82
83 codestream.access_qcd().set_irrev_quant(0.01f);
84 codestream.set_planar(true);
85
86 ojph::j2c_outfile j2c_file;
87 j2c_file.open("mixed_rev_irrev_4comp.j2c");
88 codestream.write_headers(&j2c_file);
89
90 ojph::ui32 next_comp;
91 ojph::line_buf* cur_line = codestream.exchange(NULL, next_comp);
92
93 for (ojph::ui32 c = 0; c < num_comps; ++c)
94 {
95 for (ojph::ui32 y = 0; y < height; ++y)
96 {
97 ASSERT_EQ(next_comp, c);
98 if (c == 3)
99 {
100 // non-constant pattern so a roundtrip mismatch would be caught
101 for (ojph::ui32 x = 0; x < width; ++x)
102 {
103 ojph::si32 val = (ojph::si32)((x + y * width) % 256);
104 cur_line->i32[x] = val;
105 comp3_orig[y * width + x] = val;
106 }
107 }
108 else if (cur_line->flags & ojph::line_buf::LFT_INTEGER)
109 memset(cur_line->i32, 0,
110 sizeof(ojph::si32) * cur_line->size);
111 else
112 memset(cur_line->f32, 0,
113 sizeof(float) * cur_line->size);
114 cur_line = codestream.exchange(cur_line, next_comp);
115 }
116 }
117
118 codestream.flush();
119 codestream.close();
120 }
121
122 // read back headers and verify per-component settings
123 {
124 ojph::codestream codestream;
125 ojph::j2c_infile j2c_file;
126 j2c_file.open("mixed_rev_irrev_4comp.j2c");
127 codestream.read_headers(&j2c_file);
128
129 ojph::param_siz siz = codestream.access_siz();
130 EXPECT_EQ(siz.get_num_components(), num_comps);
131 for (ojph::ui32 c = 0; c < num_comps; ++c)
132 EXPECT_EQ(siz.get_bit_depth(c), bit_depth);
133
134 ojph::param_cod cod = codestream.access_cod();
135 EXPECT_FALSE(cod.is_reversible());
136
137 for (ojph::ui32 c = 0; c < 3; ++c) {
138 EXPECT_FALSE(cod.is_reversible(c))
139 << "Component " << c << " should be irreversible";
140 }
141
142 EXPECT_TRUE(cod.is_reversible(3))
143 << "Component 3 should be reversible";
144
145 // decode all components and verify component 3 (reversible) samples
146 // are identical to what was encoded
147 codestream.restrict_input_resolution(0, 0);
148 codestream.set_planar(true);
149 codestream.create();
150
151 for (ojph::ui32 c = 0; c < num_comps; ++c)
152 {
153 ojph::ui32 comp_height = siz.get_recon_height(c);
154 ojph::ui32 comp_width = siz.get_recon_width(c);
155 for (ojph::ui32 y = 0; y < comp_height; ++y)
156 {
157 ojph::ui32 comp_num;
158 ojph::line_buf* line = codestream.pull(comp_num);
159 ASSERT_EQ(comp_num, c);
160 if (c == 3)
161 {
162 ASSERT_EQ(comp_width, width);
163 for (ojph::ui32 x = 0; x < width; ++x)
164 EXPECT_EQ(line->i32[x], comp3_orig[y * width + x])
165 << "Component 3 sample mismatch at (" << x << ", " << y << ")";
166 }
167 }
168 }
169
170 codestream.close();
171 }
172}
173
175int main(int argc, char** argv) {
176 ::testing::InitGoogleTest(&argc, argv);
177 return RUN_ALL_TESTS();
178}
The object represent a codestream.
param_siz access_siz()
Returns the underlying SIZ marker segment object.
param_cod access_cod()
Returns the underlying COD marker segment object.
void restrict_input_resolution(ui32 skipped_res_for_data, ui32 skipped_res_for_recon)
This function restricts resolution decoding for a codestream. It is for a reading (decoding) codestre...
void close()
Call this function to close the underlying file; works for both encoding and decoding codestreams.
void set_planar(bool planar)
Sets the sequence of pushing or pull rows from the machinery.
line_buf * exchange(line_buf *line, ui32 &next_component)
This call is used to send image data rows to the library. We expect to send one row from a single com...
param_qcd access_qcd()
Returns the underlying QCD marker segment object.
void read_headers(infile_base *file)
This call reads the headers of a codestream. It is for a reading (or decoding) codestream,...
void write_headers(outfile_base *file, const comment_exchange *comments=NULL, ui32 num_comments=0)
Writes codestream headers when the codestream is used for writing. This function should be called aft...
void create()
This call is for a decoding (or reading) codestream. Call this function after calling restrict_input_...
void flush()
This is the last call to a writing (encoding) codestream. This will write encoded bitstream data to t...
line_buf * pull(ui32 &comp_num)
This call is to pull one row from the codestream, being decoded. The returned line_buf object holds o...
void open(const char *filename)
void open(const char *filename)
Definition ojph_file.cpp:62
float * f32
Definition ojph_mem.h:187
void set_num_decomposition(ui32 num_decompositions)
bool is_reversible() const
void set_block_dims(ui32 width, ui32 height)
void set_color_transform(bool color_transform)
void set_reversible(bool reversible)
void set_irrev_quant(float delta)
Set the irreversible quantization base delta.
void set_tile_size(size s)
void set_component(ui32 comp_num, const point &downsampling, ui32 bit_depth, bool is_signed)
void set_num_components(ui32 num_comps)
ui32 get_bit_depth(ui32 comp_num) const
void set_tile_offset(point offset)
void set_image_offset(point offset)
ui32 get_recon_height(ui32 comp_num) const
void set_image_extent(point extent)
ui32 get_recon_width(ui32 comp_num) const
ui32 get_num_components() const
int32_t si32
Definition ojph_defs.h:55
uint32_t ui32
Definition ojph_defs.h:54
int main(int argc, char **argv)
TEST(TestMixedCOC, FourCompMixedReversibility)