OpenJPH
Open-source implementation of JPEG2000 Part-15
Loading...
Searching...
No Matches
ojph_resolution.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) 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_resolution.cpp
34// Author: Aous Naman
35// Date: 28 August 2019
36//***************************************************************************/
37
38
39#include <climits>
40#include <cmath>
41#include <new>
42
43#include "ojph_mem.h"
44#include "ojph_params.h"
46#include "ojph_resolution.h"
47#include "ojph_tile_comp.h"
48#include "ojph_tile.h"
49#include "ojph_subband.h"
50#include "ojph_precinct.h"
51
53
54namespace ojph {
55
56 namespace local
57 {
60 const rect& recon_res_rect,
61 ui32 comp_num, ui32 res_num)
62 {
64 const param_cod* cdp = codestream->get_coc(comp_num);
65 ui32 num_decomps = cdp->get_num_decompositions();
66 ui32 t = num_decomps - codestream->get_skipped_res_for_recon();
68
69 const param_atk* atk = cdp->access_atk();
71 if (cdp->is_dfs_defined()) {
72 const param_dfs* dfs = codestream->access_dfs();
73 if (dfs == NULL) {
74 OJPH_ERROR(0x00070001, "There is a problem with codestream "
75 "marker segments. COD/COC specifies the use of a DFS marker "
76 "but there are no DFS markers within the main codestream "
77 "headers");
78 }
79 else {
80 ui16 dfs_idx = cdp->get_dfs_index();
81 dfs = dfs->get_dfs(dfs_idx);
82 if (dfs == NULL) {
83 OJPH_ERROR(0x00070002, "There is a problem with codestream "
84 "marker segments. COD/COC specifies the use of a DFS marker "
85 "with index %d, but there are no such marker within the "
86 "main codestream headers", dfs_idx);
87 }
88 ds = dfs->get_dwt_type(num_decomps - res_num + 1);
89 }
90 }
91
93 if (res_num > 0)
94 {
95 if (ds == param_dfs::BIDIR_DWT)
97 else if (ds == param_dfs::HORZ_DWT)
99 else if (ds == param_dfs::VERT_DWT)
101 }
102
103 //allocate resolution/subbands
104 ui32 trx0 = res_rect.org.x;
105 ui32 try0 = res_rect.org.y;
106 ui32 trx1 = res_rect.org.x + res_rect.siz.w;
107 ui32 try1 = res_rect.org.y + res_rect.siz.h;
108 allocator->pre_alloc_obj<subband>(4);
109 if (res_num > 0)
110 {
111 if (ds == param_dfs::BIDIR_DWT)
112 {
113 for (ui32 i = 0; i < 4; ++i)
114 {
115 ui32 tbx0 = (trx0 - (i & 1) + 1) >> 1;
116 ui32 tbx1 = (trx1 - (i & 1) + 1) >> 1;
117 ui32 tby0 = (try0 - (i >> 1) + 1) >> 1;
118 ui32 tby1 = (try1 - (i >> 1) + 1) >> 1;
119
120 rect re;
121 re.org.x = tbx0;
122 re.org.y = tby0;
123 re.siz.w = tbx1 - tbx0;
124 re.siz.h = tby1 - tby0;
125 if (i == 0) {
126 allocator->pre_alloc_obj<resolution>(1);
128 skipped_res_for_recon ? recon_res_rect : re,
129 comp_num, res_num - 1);
130 }
131 else
134 }
135 }
136 else if (ds == param_dfs::VERT_DWT)
137 {
138 ui32 tby0, tby1;
139 rect re = res_rect;
140 tby0 = (try0 + 1) >> 1;
141 tby1 = (try1 + 1) >> 1;
142 re.org.y = tby0;
143 re.siz.h = tby1 - tby0;
144 allocator->pre_alloc_obj<resolution>(1);
146 skipped_res_for_recon ? recon_res_rect : re,
147 comp_num, res_num - 1);
148
149 tby0 = try0 >> 1;
150 tby1 = try1 >> 1;
151 re.org.y = tby0;
152 re.siz.h = tby1 - tby0;
155 }
156 else if (ds == param_dfs::HORZ_DWT)
157 {
158 ui32 tbx0, tbx1;
159 rect re = res_rect;
160 tbx0 = (trx0 + 1) >> 1;
161 tbx1 = (trx1 + 1) >> 1;
162 re.org.x = tbx0;
163 re.siz.w = tbx1 - tbx0;
164 allocator->pre_alloc_obj<resolution>(1);
166 skipped_res_for_recon ? recon_res_rect : re,
167 comp_num, res_num - 1);
168
169 tbx0 = trx0 >> 1;
170 tbx1 = trx1 >> 1;
171 re.org.x = tbx0;
172 re.siz.w = tbx1 - tbx0;
175 }
176 else
177 {
178 assert(ds == param_dfs::NO_DWT);
179 allocator->pre_alloc_obj<resolution>(1);
181 skipped_res_for_recon ? recon_res_rect : res_rect,
182 comp_num, res_num - 1);
183 }
184 }
185 else
188
189 //prealloc precincts
192 if (trx0 != trx1 && try0 != try1)
193 {
194 num_precincts.w = (trx1 + (1 << log_PP.w) - 1) >> log_PP.w;
195 num_precincts.w -= trx0 >> log_PP.w;
196 num_precincts.h = (try1 + (1 << log_PP.h) - 1) >> log_PP.h;
197 num_precincts.h -= try0 >> log_PP.h;
198 allocator->pre_alloc_obj<precinct>((size_t)num_precincts.area());
199 }
200
201 //allocate lines
202 if (skipped_res_for_recon == false)
203 {
205 allocator->pre_alloc_obj<line_buf>(num_steps + 2);
206 allocator->pre_alloc_obj<lifting_buf>(num_steps + 2);
207
209 ui32 precision = qp->propose_precision(cdp);
210 const param_atk* atk = cdp->access_atk();
211 bool reversible = atk->is_reversible();
212
213 ui32 width = res_rect.siz.w + 1;
214 if (reversible)
215 {
216 if (precision <= 32) {
217 for (ui32 i = 0; i < num_steps; ++i)
218 allocator->pre_alloc_data<si32>(width, 1);
219 allocator->pre_alloc_data<si32>(width, 1);
220 allocator->pre_alloc_data<si32>(width, 1);
221 }
222 else
223 {
224 for (ui32 i = 0; i < num_steps; ++i)
225 allocator->pre_alloc_data<si64>(width, 1);
226 allocator->pre_alloc_data<si64>(width, 1);
227 allocator->pre_alloc_data<si64>(width, 1);
228 }
229 }
230 else {
231 for (ui32 i = 0; i < num_steps; ++i)
232 allocator->pre_alloc_data<float>(width, 1);
233 allocator->pre_alloc_data<float>(width, 1);
234 allocator->pre_alloc_data<float>(width, 1);
235 }
236 }
237 }
238
241 const rect& res_rect,
242 const rect& recon_res_rect,
243 ui32 comp_num, ui32 res_num,
244 point comp_downsamp, point res_downsamp,
245 tile_comp* parent_tile_comp,
246 resolution* parent_res)
247 {
250 const param_cod* cdp = codestream->get_coc(comp_num);
251 ui32 t, num_decomps = cdp->get_num_decompositions();
252 t = num_decomps - codestream->get_skipped_res_for_recon();
254 t = num_decomps - codestream->get_skipped_res_for_read();
256
257 this->comp_downsamp = comp_downsamp;
258 this->parent_comp = parent_tile_comp;
259 this->parent_res = parent_res;
260 this->res_rect = res_rect;
261 this->comp_num = comp_num;
262 this->res_num = res_num;
263 this->num_bytes = 0;
264 this->atk = cdp->access_atk();
266 if (cdp->is_dfs_defined()) {
267 const param_dfs* dfs = codestream->access_dfs();
268 if (dfs == NULL) {
269 OJPH_ERROR(0x00070011, "There is a problem with codestream "
270 "marker segments. COD/COC specifies the use of a DFS marker "
271 "but there are no DFS markers within the main codestream "
272 "headers");
273 }
274 else {
275 ui16 dfs_idx = cdp->get_dfs_index();
276 dfs = dfs->get_dfs(dfs_idx);
277 if (dfs == NULL) {
278 OJPH_ERROR(0x00070012, "There is a problem with codestream "
279 "marker segments. COD/COC specifies the use of a DFS marker "
280 "with index %d, but there are no such marker within the "
281 "main codestream headers", dfs_idx);
282 }
283 ui32 num_decomps = cdp->get_num_decompositions();
284 ds = dfs->get_dwt_type(num_decomps - res_num + 1);
285 }
286 }
287
288 transform_flags = 0;
289 if (res_num > 0)
290 {
291 if (ds == param_dfs::BIDIR_DWT)
293 else if (ds == param_dfs::HORZ_DWT)
295 else if (ds == param_dfs::VERT_DWT)
297 }
298
299 //allocate resolution/subbands
300 ui32 trx0 = res_rect.org.x;
301 ui32 try0 = res_rect.org.y;
302 ui32 trx1 = res_rect.org.x + res_rect.siz.w;
303 ui32 try1 = res_rect.org.y + res_rect.siz.h;
304 bands = allocator->post_alloc_obj<subband>(4);
305 for (int i = 0; i < 4; ++i)
306 new (bands + i) subband;
307 if (res_num > 0)
308 {
309 if (ds == param_dfs::BIDIR_DWT)
310 {
311 for (ui32 i = 0; i < 4; ++i)
312 {
313 ui32 tbx0 = (trx0 - (i & 1) + 1) >> 1;
314 ui32 tbx1 = (trx1 - (i & 1) + 1) >> 1;
315 ui32 tby0 = (try0 - (i >> 1) + 1) >> 1;
316 ui32 tby1 = (try1 - (i >> 1) + 1) >> 1;
317
318 rect re;
319 re.org.x = tbx0;
320 re.org.y = tby0;
321 re.siz.w = tbx1 - tbx0;
322 re.siz.h = tby1 - tby0;
323 if (i == 0) {
324 point next_res_downsamp;
325 next_res_downsamp.x = res_downsamp.x * 2;
326 next_res_downsamp.y = res_downsamp.y * 2;
327
328 child_res = allocator->post_alloc_obj<resolution>(1);
330 skipped_res_for_recon ? recon_res_rect : re, comp_num,
331 res_num - 1, comp_downsamp, next_res_downsamp,
332 parent_tile_comp, this);
333 }
334 else
335 bands[i].finalize_alloc(codestream, re, this, res_num, i);
336 }
337 }
338 else if (ds == param_dfs::VERT_DWT)
339 {
340 ui32 tby0, tby1;
341 rect re = res_rect;
342 tby0 = (try0 + 1) >> 1;
343 tby1 = (try1 + 1) >> 1;
344 re.org.y = tby0;
345 re.siz.h = tby1 - tby0;
346
347 point next_res_downsamp;
348 next_res_downsamp.x = res_downsamp.x;
349 next_res_downsamp.y = res_downsamp.y * 2;
350 child_res = allocator->post_alloc_obj<resolution>(1);
352 skipped_res_for_recon ? recon_res_rect : re, comp_num,
353 res_num - 1, comp_downsamp, next_res_downsamp,
354 parent_tile_comp, this);
355
356 tby0 = try0 >> 1;
357 tby1 = try1 >> 1;
358 re.org.y = tby0;
359 re.siz.h = tby1 - tby0;
360 bands[2].finalize_alloc(codestream, re, this, res_num, 2);
361 }
362 else if (ds == param_dfs::HORZ_DWT)
363 {
364 ui32 tbx0, tbx1;
365 rect re = res_rect;
366 tbx0 = (trx0 + 1) >> 1;
367 tbx1 = (trx1 + 1) >> 1;
368 re.org.x = tbx0;
369 re.siz.w = tbx1 - tbx0;
370
371 point next_res_downsamp;
372 next_res_downsamp.x = res_downsamp.x * 2;
373 next_res_downsamp.y = res_downsamp.y;
374 child_res = allocator->post_alloc_obj<resolution>(1);
376 skipped_res_for_recon ? recon_res_rect : re, comp_num,
377 res_num - 1, comp_downsamp, next_res_downsamp,
378 parent_tile_comp, this);
379
380 tbx0 = trx0 >> 1;
381 tbx1 = trx1 >> 1;
382 re.org.x = tbx0;
383 re.siz.w = tbx1 - tbx0;
384 bands[1].finalize_alloc(codestream, re, this, res_num, 1);
385 }
386 else
387 {
388 assert(ds == param_dfs::NO_DWT);
389 child_res = allocator->post_alloc_obj<resolution>(1);
391 skipped_res_for_recon ? recon_res_rect : res_rect, comp_num,
392 res_num - 1, comp_downsamp, res_downsamp, parent_tile_comp, this);
393 }
394 }
395 else {
396 child_res = NULL;
398 }
399
400 //finalize precincts
403 precincts = NULL;
404 if (trx0 != trx1 && try0 != try1)
405 {
406 num_precincts.w = (trx1 + (1 << log_PP.w) - 1) >> log_PP.w;
407 num_precincts.w -= trx0 >> log_PP.w;
408 num_precincts.h = (try1 + (1 << log_PP.h) - 1) >> log_PP.h;
409 num_precincts.h -= try0 >> log_PP.h;
410 precincts =
411 allocator->post_alloc_obj<precinct>((size_t)num_precincts.area());
412 ui64 num = num_precincts.area();
413 for (ui64 i = 0; i < num; ++i)
414 precincts[i] = precinct();
415 }
416 // precincts will be initialized in full shortly
417
418 ui32 x_lower_bound = (trx0 >> log_PP.w) << log_PP.w;
419 ui32 y_lower_bound = (try0 >> log_PP.h) << log_PP.h;
420
421 precinct* pp = precincts;
422 point tile_top_left = parent_tile_comp->get_tile()->get_tile_rect().org;
423 for (ui32 y = 0; y < num_precincts.h; ++y)
424 {
425 ui32 ppy0 = y_lower_bound + (y << log_PP.h);
426 for (ui32 x = 0; x < num_precincts.w; ++x, ++pp)
427 {
428 ui32 ppx0 = x_lower_bound + (x << log_PP.w);
429 point t(res_downsamp.x * ppx0, res_downsamp.y * ppy0);
430 t.x = t.x > tile_top_left.x ? t.x : tile_top_left.x;
431 t.y = t.y > tile_top_left.y ? t.y : tile_top_left.y;
432 pp->img_point = t;
433 pp->bands = bands;
434 pp->may_use_sop = cdp->packets_may_use_sop();
435 pp->uses_eph = cdp->packets_use_eph();
437 pp->coded = NULL;
438 }
439 }
440 for (int i = 0; i < 4; ++i)
441 if (bands[i].exists())
443
444 // determine how to divide scratch into multiple levels of
445 // tag trees
446 size log_cb = cdp->get_log_block_dims();
447 log_PP.w -= (transform_flags & HORZ_TRX) ? 1 : 0;
448 log_PP.h -= (transform_flags & VERT_TRX) ? 1 : 0;
449 size ratio;
450 ratio.w = log_PP.w - ojph_min(log_cb.w, log_PP.w);
451 ratio.h = log_PP.h - ojph_min(log_cb.h, log_PP.h);
452 max_num_levels = ojph_max(ratio.w, ratio.h);
453 ui32 val = 1u << (max_num_levels << 1);
454 tag_tree_size = (int)((val * 4 + 2) / 3);
456 level_index[0] = 0;
457 for (ui32 i = 1; i <= max_num_levels; ++i, val >>= 2)
458 level_index[i] = level_index[i - 1] + val;
459 cur_precinct_loc = point(0, 0);
460
461 //allocate lines
462 if (skipped_res_for_recon == false)
463 {
464 this->atk = cdp->access_atk();
465 this->reversible = atk->is_reversible();
466 this->num_steps = atk->get_num_steps();
467 // create line buffers and lifting_bufs
468 lines = allocator->post_alloc_obj<line_buf>(num_steps + 2);
469 ssp = allocator->post_alloc_obj<lifting_buf>(num_steps + 2);
470 sig = ssp + num_steps;
471 aug = ssp + num_steps + 1;
472
473 // initiate lifting_bufs
474 for (ui32 i = 0; i < num_steps; ++i) {
475 new (ssp + i) lifting_buf;
476 ssp[i].line = lines + i;
477 };
478 new (sig) lifting_buf;
479 sig->line = lines + num_steps;
480 new (aug) lifting_buf;
481 aug->line = lines + num_steps + 1;
482
484 ui32 precision = qp->propose_precision(cdp);
485
486 // initiate storage of line_buf
487 ui32 width = res_rect.siz.w + 1;
488 if (this->reversible)
489 {
490 if (precision <= 32)
491 {
492 for (ui32 i = 0; i < num_steps; ++i)
493 ssp[i].line->wrap(
494 allocator->post_alloc_data<si32>(width, 1), width, 1);
495 sig->line->wrap(
496 allocator->post_alloc_data<si32>(width, 1), width, 1);
497 aug->line->wrap(
498 allocator->post_alloc_data<si32>(width, 1), width, 1);
499 }
500 else
501 {
502 for (ui32 i = 0; i < num_steps; ++i)
503 ssp[i].line->wrap(
504 allocator->post_alloc_data<si64>(width, 1), width, 1);
505 sig->line->wrap(
506 allocator->post_alloc_data<si64>(width, 1), width, 1);
507 aug->line->wrap(
508 allocator->post_alloc_data<si64>(width, 1), width, 1);
509 }
510 }
511 else
512 {
513 for (ui32 i = 0; i < num_steps; ++i)
514 ssp[i].line->wrap(
515 allocator->post_alloc_data<float>(width, 1), width, 1);
516 sig->line->wrap(
517 allocator->post_alloc_data<float>(width, 1), width, 1);
518 aug->line->wrap(
519 allocator->post_alloc_data<float>(width, 1), width, 1);
520 }
521
522 cur_line = 0;
524 vert_even = (res_rect.org.y & 1) == 0;
525 horz_even = (res_rect.org.x & 1) == 0;
526 }
527 }
528
531 {
532 if (vert_even)
533 {
534 ++cur_line;
535 sig->active = true;
536 return sig->line;
537 }
538 else
539 {
540 ++cur_line;
541 aug->active = true;
542 return aug->line;
543 }
544 }
545
548 {
549 if (res_num == 0)
550 {
551 assert(child_res == NULL);
553 bands[0].push_line();
554 return;
555 }
556
557 ui32 width = res_rect.siz.w;
558 if (width == 0)
559 return;
560 if (reversible)
561 {
562 if (res_rect.siz.h > 1)
563 {
564 if (!vert_even && cur_line < res_rect.siz.h) {
566 return;
567 }
568
569 do
570 {
571 //vertical transform
572 for (ui32 i = 0; i < num_steps; ++i)
573 {
574 if (aug->active && (sig->active || ssp[i].active))
575 {
576 line_buf* dp = aug->line;
577 line_buf* sp1 = sig->active ? sig->line : ssp[i].line;
578 line_buf* sp2 = ssp[i].active ? ssp[i].line : sig->line;
579 const lifting_step* s = atk->get_step(num_steps - i - 1);
580 rev_vert_step(s, sp1, sp2, dp, width, false);
581 }
582 lifting_buf t = *aug; *aug = ssp[i]; ssp[i] = *sig; *sig = t;
583 }
584
585 if (aug->active) {
587 bands[3].get_line(), aug->line, width, horz_even);
588 bands[2].push_line();
589 bands[3].push_line();
590 aug->active = false;
592 }
593 if (sig->active) {
595 bands[1].get_line(), sig->line, width, horz_even);
596 bands[1].push_line();
598 sig->active = false;
600 };
602 } while (cur_line >= res_rect.siz.h && rows_to_produce > 0);
603 }
604 else
605 {
606 if (vert_even) {
607 // horizontal transform
609 bands[1].get_line(), sig->line, width, horz_even);
610 bands[1].push_line();
612 }
613 else
614 {
615 // vertical transform
617 {
618 si32* sp = aug->line->i32;
619 for (ui32 i = width; i > 0; --i)
620 *sp++ <<= 1;
621 }
622 else
623 {
624 assert(aug->line->flags & line_buf::LFT_64BIT);
625 si64* sp = aug->line->i64;
626 for (ui32 i = width; i > 0; --i)
627 *sp++ <<= 1;
628 }
629 // horizontal transform
631 bands[3].get_line(), aug->line, width, horz_even);
632 bands[2].push_line();
633 bands[3].push_line();
634 }
635 }
636 }
637 else
638 {
639 if (res_rect.siz.h > 1)
640 {
641 if (!vert_even && cur_line < res_rect.siz.h) {
643 return;
644 }
645
646 do
647 {
648 //vertical transform
649 for (ui32 i = 0; i < num_steps; ++i)
650 {
651 if (aug->active && (sig->active || ssp[i].active))
652 {
653 line_buf* dp = aug->line;
654 line_buf* sp1 = sig->active ? sig->line : ssp[i].line;
655 line_buf* sp2 = ssp[i].active ? ssp[i].line : sig->line;
656 const lifting_step* s = atk->get_step(num_steps - i - 1);
657 irv_vert_step(s, sp1, sp2, dp, width, false);
658 }
659 lifting_buf t = *aug; *aug = ssp[i]; ssp[i] = *sig; *sig = t;
660 }
661
662 if (aug->active) {
663 const float K = atk->get_K();
664 irv_vert_times_K(K, aug->line, width);
665
667 bands[3].get_line(), aug->line, width, horz_even);
668 bands[2].push_line();
669 bands[3].push_line();
670 aug->active = false;
672 }
673 if (sig->active) {
674 const float K_inv = 1.0f / atk->get_K();
675 irv_vert_times_K(K_inv, sig->line, width);
676
678 bands[1].get_line(), sig->line, width, horz_even);
679 bands[1].push_line();
681 sig->active = false;
683 };
685 } while (cur_line >= res_rect.siz.h && rows_to_produce > 0);
686 }
687 else
688 {
689 if (vert_even) {
690 // horizontal transform
692 bands[1].get_line(), sig->line, width, horz_even);
693 bands[1].push_line();
695 }
696 else
697 {
698 // vertical transform
699 float* sp = aug->line->f32;
700 for (ui32 i = width; i > 0; --i)
701 *sp++ *= 2.0f;
702 // horizontal transform
704 bands[3].get_line(), aug->line, width, horz_even);
705 bands[2].push_line();
706 bands[3].push_line();
707 }
708 }
709 }
710 }
711
714 {
715 if (res_num == 0)
716 {
717 assert(child_res == NULL);
718 return bands[0].pull_line();
719 }
720
721 if (skipped_res_for_recon == true)
722 return child_res->pull_line();
723
724 ui32 width = res_rect.siz.w;
725 if (width == 0)
726 return NULL;
727
729 {
730 if (reversible)
731 {
732 if (res_rect.siz.h > 1)
733 {
734 if (sig->active) {
735 sig->active = false;
736 return sig->line;
737 };
738 for (;;)
739 {
740 //horizontal transform
741 if (cur_line < res_rect.siz.h)
742 {
743 if (vert_even) { // even
746 bands[1].pull_line(), width, horz_even);
747 else
748 memcpy(aug->line->p, child_res->pull_line()->p,
749 (size_t)width
751 aug->active = true;
753 ++cur_line;
754 continue;
755 }
756 else {
759 bands[3].pull_line(), width, horz_even);
760 else
761 memcpy(sig->line->p, bands[2].pull_line()->p,
762 (size_t)width
764 sig->active = true;
766 ++cur_line;
767 }
768 }
769
770 //vertical transform
771 for (ui32 i = 0; i < num_steps; ++i)
772 {
773 if (aug->active && (sig->active || ssp[i].active))
774 {
775 line_buf* dp = aug->line;
776 line_buf* sp1 = sig->active ? sig->line : ssp[i].line;
777 line_buf* sp2 = ssp[i].active ? ssp[i].line : sig->line;
778 const lifting_step* s = atk->get_step(i);
779 rev_vert_step(s, sp1, sp2, dp, width, true);
780 }
781 lifting_buf t = *aug; *aug = ssp[i]; ssp[i] = *sig; *sig = t;
782 }
783
784 if (aug->active) {
785 aug->active = false;
786 return aug->line;
787 }
788 if (sig->active) {
789 sig->active = false;
790 return sig->line;
791 };
792 }
793 }
794 else
795 {
796 if (vert_even) {
799 bands[1].pull_line(), width, horz_even);
800 else
801 memcpy(aug->line->p, child_res->pull_line()->p,
802 (size_t)width
804 }
805 else
806 {
809 bands[3].pull_line(), width, horz_even);
810 else
811 memcpy(aug->line->p, bands[2].pull_line()->p,
812 (size_t)width
815 {
816 si32* sp = aug->line->i32;
817 for (ui32 i = width; i > 0; --i)
818 *sp++ >>= 1;
819 }
820 else
821 {
822 assert(aug->line->flags & line_buf::LFT_64BIT);
823 si64* sp = aug->line->i64;
824 for (ui32 i = width; i > 0; --i)
825 *sp++ >>= 1;
826 }
827 }
828 return aug->line;
829 }
830 }
831 else
832 {
833 if (res_rect.siz.h > 1)
834 {
835 if (sig->active) {
836 sig->active = false;
837 return sig->line;
838 };
839 for (;;)
840 {
841 //horizontal transform
842 if (cur_line < res_rect.siz.h)
843 {
844 if (vert_even) { // even
847 bands[1].pull_line(), width, horz_even);
848 else
849 memcpy(aug->line->f32, child_res->pull_line()->f32,
850 width * sizeof(float));
851 aug->active = true;
853 ++cur_line;
854
855 const float K = atk->get_K();
856 irv_vert_times_K(K, aug->line, width);
857
858 continue;
859 }
860 else {
863 bands[3].pull_line(), width, horz_even);
864 else
865 memcpy(sig->line->f32, bands[2].pull_line()->f32,
866 width * sizeof(float));
867 sig->active = true;
869 ++cur_line;
870
871 const float K_inv = 1.0f / atk->get_K();
872 irv_vert_times_K(K_inv, sig->line, width);
873 }
874 }
875
876 //vertical transform
877 for (ui32 i = 0; i < num_steps; ++i)
878 {
879 if (aug->active && (sig->active || ssp[i].active))
880 {
881 line_buf* dp = aug->line;
882 line_buf* sp1 = sig->active ? sig->line : ssp[i].line;
883 line_buf* sp2 = ssp[i].active ? ssp[i].line : sig->line;
884 const lifting_step* s = atk->get_step(i);
885 irv_vert_step(s, sp1, sp2, dp, width, true);
886 }
887 lifting_buf t = *aug; *aug = ssp[i]; ssp[i] = *sig; *sig = t;
888 }
889
890 if (aug->active) {
891 aug->active = false;
892 return aug->line;
893 }
894 if (sig->active) {
895 sig->active = false;
896 return sig->line;
897 };
898 }
899 }
900 else
901 {
902 if (vert_even) {
905 bands[1].pull_line(), width, horz_even);
906 else
907 memcpy(aug->line->f32, child_res->pull_line()->f32,
908 width * sizeof(float));
909 }
910 else
911 {
914 bands[3].pull_line(), width, horz_even);
915 else
916 memcpy(aug->line->f32, bands[2].pull_line()->f32,
917 width * sizeof(float));
918 float* sp = aug->line->f32;
919 for (ui32 i = width; i > 0; --i)
920 *sp++ *= 0.5f;
921 }
922 return aug->line;
923 }
924 }
925 }
926 else
927 {
928 if (reversible)
929 {
932 bands[1].pull_line(), width, horz_even);
933 else
934 memcpy(aug->line->p, child_res->pull_line()->p,
935 (size_t)width * (aug->line->flags & line_buf::LFT_SIZE_MASK));
936 return aug->line;
937 }
938 else
939 {
942 bands[1].pull_line(), width, horz_even);
943 else
944 memcpy(aug->line->f32, child_res->pull_line()->f32,
945 width * sizeof(float));
946 return aug->line;
947 }
948 }
949 }
950
953 {
954 ui32 lower_resolutions_bytes = 0;
955 if (res_num != 0)
956 lower_resolutions_bytes = child_res->prepare_precinct();
957
958 this->num_bytes = 0;
959 si32 repeat = (si32)num_precincts.area();
960 for (si32 i = 0; i < repeat; ++i)
963 return this->num_bytes + lower_resolutions_bytes;
964 }
965
968 {
969 precinct* p = precincts;
970 for (si32 i = 0; i < (si32)num_precincts.area(); ++i)
971 p[i].write(file);
972 }
973
976 {
978 if (idx < num_precincts.area())
979 {
980 top_left = precincts[idx].img_point;
981 return true;
982 }
983 return false;
984 }
985
988 {
990 assert(idx < num_precincts.area());
991 precincts[idx].write(file);
992
994 {
997 }
998 }
999
1002 {
1003 precinct* p = precincts;
1005 for (ui32 i = idx; i < num_precincts.area(); ++i)
1006 {
1007 if (data_left == 0)
1008 break;
1009 p[i].parse(tag_tree_size, level_index, elastic, data_left, file,
1012 {
1013 cur_precinct_loc.x = 0;
1015 }
1016 }
1017 }
1018
1021 {
1023 assert(idx < num_precincts.area());
1024
1025 if (data_left == 0)
1026 return;
1027 precinct* p = precincts + idx;
1028 p->parse(tag_tree_size, level_index, elastic, data_left, file,
1031 {
1032 cur_precinct_loc.x = 0;
1034 }
1035 }
1036
1039 {
1040 if (this->res_num == resolution_num)
1041 return get_num_bytes();
1042 if (resolution_num < this->res_num) {
1043 assert(child_res);
1044 return child_res->get_num_bytes(resolution_num);
1045 }
1046 return 0;
1047 }
1048 }
1049}
float * f32
Definition ojph_mem.h:162
void wrap(T *buffer, size_t num_ele, ui32 pre_size)
const param_cod * get_coc(ui32 comp_num)
mem_elastic_allocator * get_elastic_alloc()
const param_qcd * access_qcd()
mem_fixed_allocator * get_allocator()
const param_dfs * access_dfs()
bool get_top_left_precinct(point &top_left)
void parse_one_precinct(ui32 &data_left, infile_base *file)
mem_elastic_allocator * elastic
void write_precincts(outfile_base *file)
static void pre_alloc(codestream *codestream, const rect &res_rect, const rect &recon_res_rect, ui32 comp_num, ui32 res_num)
void finalize_alloc(codestream *codestream, const rect &res_rect, const rect &recon_res_rect, ui32 comp_num, ui32 res_num, point comp_downsamp, point res_downsamp, tile_comp *parent_tile_comp, resolution *parent_res)
void parse_all_precincts(ui32 &data_left, infile_base *file)
void write_one_precinct(outfile_base *file)
static void pre_alloc(codestream *codestream, const rect &band_rect, ui32 comp_num, ui32 res_num, ui32 transform_flags)
void exchange_buf(line_buf *l)
line_buf * get_line()
void get_cb_indices(const size &num_precincts, precinct *precincts)
void finalize_alloc(codestream *codestream, const rect &band_rect, resolution *res, ui32 res_num, ui32 subband_num)
rect get_tile_rect()
Definition ojph_tile.h:75
void pre_alloc_data(size_t num_ele, ui32 pre_size)
Definition ojph_mem.h:66
void pre_alloc_obj(size_t num_ele)
Definition ojph_mem.h:72
T * post_alloc_data(size_t num_ele, ui32 pre_size)
Definition ojph_mem.h:89
T * post_alloc_obj(size_t num_ele)
Definition ojph_mem.h:96
void(* rev_horz_ana)(const param_atk *atk, const line_buf *ldst, const line_buf *hdst, const line_buf *src, ui32 width, bool even)
void(* irv_vert_times_K)(float K, const line_buf *aug, ui32 repeat)
void(* irv_vert_step)(const lifting_step *s, const line_buf *sig, const line_buf *other, const line_buf *aug, ui32 repeat, bool synthesis)
void(* rev_horz_syn)(const param_atk *atk, const line_buf *dst, const line_buf *lsrc, const line_buf *hsrc, ui32 width, bool even)
void(* irv_horz_ana)(const param_atk *atk, const line_buf *ldst, const line_buf *hdst, const line_buf *src, ui32 width, bool even)
void(* rev_vert_step)(const lifting_step *s, const line_buf *sig, const line_buf *other, const line_buf *aug, ui32 repeat, bool synthesis)
void(* irv_horz_syn)(const param_atk *atk, const line_buf *dst, const line_buf *lsrc, const line_buf *hsrc, ui32 width, bool even)
int64_t si64
Definition ojph_defs.h:57
uint64_t ui64
Definition ojph_defs.h:56
uint16_t ui16
Definition ojph_defs.h:52
int32_t si32
Definition ojph_defs.h:55
uint32_t ui32
Definition ojph_defs.h:54
#define ojph_max(a, b)
Definition ojph_defs.h:73
#define ojph_min(a, b)
Definition ojph_defs.h:76
#define OJPH_ERROR(t,...)
line_buf * line
Definition ojph_mem.h:171
const lifting_step * get_step(ui32 s) const
const param_atk * access_atk() const
size get_log_precinct_size(ui32 res_num) const
dfs_dwt_type get_dwt_type(ui32 decomp_level) const
const param_dfs * get_dfs(int index) const
ui32 propose_precision(const param_cod *cod) const
param_qcd * get_qcc(ui32 comp_idx)
void write(outfile_base *file)
void parse(int tag_tree_size, ui32 *lev_idx, mem_elastic_allocator *elastic, ui32 &data_left, infile_base *file, bool skipped)
point org
Definition ojph_base.h:66
ui64 area() const
Definition ojph_base.h:53