OpenJPH
Open-source implementation of JPEG2000 Part-15
Loading...
Searching...
No Matches
ojph_tile.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_tile.cpp
34// Author: Aous Naman
35// Date: 28 August 2019
36//***************************************************************************/
37
38
39#include <climits>
40#include <cmath>
41
42#include "ojph_mem.h"
43#include "ojph_params.h"
45#include "ojph_tile.h"
46#include "ojph_tile_comp.h"
47
49
50namespace ojph {
51
52 namespace local
53 {
54
57 const rect& recon_tile_rect, ui32& num_tileparts)
58 {
60
61 //allocate tiles_comp
62 const param_siz *szp = codestream->get_siz();
65 allocator->pre_alloc_obj<rect>(num_comps); //for comp_rects
66 allocator->pre_alloc_obj<rect>(num_comps); //for recon_comp_rects
67 allocator->pre_alloc_obj<ui32>(num_comps); //for line_offsets
68 allocator->pre_alloc_obj<ui32>(num_comps); //for num_bits
69 allocator->pre_alloc_obj<bool>(num_comps); //for is_signed
70 allocator->pre_alloc_obj<bool>(num_comps); //for reversible
71 allocator->pre_alloc_obj<ui8>(num_comps); //for nlt_type3
72 allocator->pre_alloc_obj<ui32>(num_comps); //for cur_line
73
74 {
78 num_tileparts = 1; //for num_rc_bytes
79 else if (t == OJPH_TILEPART_COMPONENTS)
80 num_tileparts = num_comps;
81 else if (t == OJPH_TILEPART_RESOLUTIONS)
82 {
83 ui32 max_decs = 0;
84 for (ui32 c = 0; c < num_comps; ++c) {
86 max_decs = ojph_max(max_decs, s);
87 }
88 num_tileparts = 1 + max_decs;
89 }
91 {
92 num_tileparts = 0;
93 for (ui32 c = 0; c < num_comps; ++c) {
95 num_tileparts += s + 1;
96 }
97 }
98 if (num_tileparts > 255)
99 OJPH_ERROR(0x000300D1, "Trying to create %d tileparts; a tile "
100 "cannot have more than 255 tile parts.", num_tileparts);
101 }
102
103 ui32 tx0 = tile_rect.org.x;
104 ui32 ty0 = tile_rect.org.y;
105 ui32 tx1 = tile_rect.org.x + tile_rect.siz.w;
106 ui32 ty1 = tile_rect.org.y + tile_rect.siz.h;
107 ui32 recon_tx0 = recon_tile_rect.org.x;
108 ui32 recon_ty0 = recon_tile_rect.org.y;
109 ui32 recon_tx1 = recon_tile_rect.org.x + recon_tile_rect.siz.w;
110 ui32 recon_ty1 = recon_tile_rect.org.y + recon_tile_rect.siz.h;
111
112 ui32 width = 0;
113 for (ui32 i = 0; i < num_comps; ++i)
114 {
115 point downsamp = szp->get_downsampling(i);
116
117 ui32 tcx0 = ojph_div_ceil(tx0, downsamp.x);
118 ui32 tcy0 = ojph_div_ceil(ty0, downsamp.y);
119 ui32 tcx1 = ojph_div_ceil(tx1, downsamp.x);
120 ui32 tcy1 = ojph_div_ceil(ty1, downsamp.y);
121 ui32 recon_tcx0 = ojph_div_ceil(recon_tx0, downsamp.x);
122 ui32 recon_tcy0 = ojph_div_ceil(recon_ty0, downsamp.y);
123 ui32 recon_tcx1 = ojph_div_ceil(recon_tx1, downsamp.x);
124 ui32 recon_tcy1 = ojph_div_ceil(recon_ty1, downsamp.y);
125
126 rect comp_rect;
127 comp_rect.org.x = tcx0;
128 comp_rect.org.y = tcy0;
129 comp_rect.siz.w = tcx1 - tcx0;
130 comp_rect.siz.h = tcy1 - tcy0;
131
132 rect recon_comp_rect;
133 recon_comp_rect.org.x = recon_tcx0;
134 recon_comp_rect.org.y = recon_tcy0;
135 recon_comp_rect.siz.w = recon_tcx1 - recon_tcx0;
136 recon_comp_rect.siz.h = recon_tcy1 - recon_tcy0;
137
138 tile_comp::pre_alloc(codestream, i, comp_rect, recon_comp_rect);
139 width = ojph_max(width, recon_comp_rect.siz.w);
140 }
141
142 //allocate lines
143 const param_cod* cdp = codestream->get_cod();
145 {
146 bool reversible[3];
147 for (ui32 i = 0; i < 3; ++i)
149 if (reversible[0] != reversible[1] || reversible[1] != reversible[2])
150 OJPH_ERROR(0x000300A2, "When the colour transform is employed. "
151 "all colour components must undergo either reversible or "
152 "irreversible wavelet transform; if not, then it is not clear "
153 "what colour transform should be used (reversible or "
154 "irreversible). Here we found that the first three colour "
155 "components uses %s, %s, and %s transforms, respectively.",
156 reversible[0] ? "reversible" : "irreversible",
157 reversible[1] ? "reversible" : "irreversible",
158 reversible[2] ? "reversible" : "irreversible");
159
160 allocator->pre_alloc_obj<line_buf>(3);
161 if (reversible[0])
162 for (int i = 0; i < 3; ++i)
163 allocator->pre_alloc_data<si32>(width, 0);
164 else
165 for (int i = 0; i < 3; ++i)
166 allocator->pre_alloc_data<float>(width, 0);
167 }
168 }
169
172 ui32 tile_idx, ui32& offset,
173 ui32 &num_tileparts)
174 {
175 //this->parent = codestream;
177
178 sot.init(0, (ui16)tile_idx, 0, 1);
180
181 //allocate tiles_comp
182 const param_siz *szp = codestream->get_siz();
183 const param_nlt *nlp = codestream->get_nlt();
184
185 this->num_bytes = 0;
192 num_bits = allocator->post_alloc_obj<ui32>(num_comps);
193 is_signed = allocator->post_alloc_obj<bool>(num_comps);
194 reversible = allocator->post_alloc_obj<bool>(num_comps);
195 nlt_type3 = allocator->post_alloc_obj<ui8>(num_comps);
196 cur_line = allocator->post_alloc_obj<ui32>(num_comps);
197
201 {
205 num_tileparts = 1; //for num_rc_bytes
206 else if (t == OJPH_TILEPART_COMPONENTS)
207 num_tileparts = num_comps;
208 else if (t == OJPH_TILEPART_RESOLUTIONS)
209 {
210 ui32 max_decs = 0;
211 for (ui32 c = 0; c < num_comps; ++c) {
213 max_decs = ojph_max(max_decs, s);
214 }
215 num_tileparts = 1 + max_decs;
216 }
218 {
219 num_tileparts = 0;
220 for (ui32 c = 0; c < num_comps; ++c) {
222 num_tileparts += s + 1;
223 }
224 }
225 if (num_tileparts > 255)
226 OJPH_ERROR(0x000300D1, "Trying to create %d tileparts; a tile "
227 "cannot have more than 255 tile parts.", num_tileparts);
228 }
229
231 this->tile_rect = tile_rect;
232
233 ui32 tx0 = tile_rect.org.x;
234 ui32 ty0 = tile_rect.org.y;
235 ui32 tx1 = tile_rect.org.x + tile_rect.siz.w;
236 ui32 ty1 = tile_rect.org.y + tile_rect.siz.h;
237
238 ui32 width = 0;
239 for (ui32 i = 0; i < num_comps; ++i)
240 {
241 ui8 bd; bool is; // used for nlt_type3
242
243 point downsamp = szp->get_downsampling(i);
244 point recon_downsamp = szp->get_recon_downsampling(i);
245
246 ui32 tcx0 = ojph_div_ceil(tx0, downsamp.x);
247 ui32 tcy0 = ojph_div_ceil(ty0, downsamp.y);
248 ui32 tcx1 = ojph_div_ceil(tx1, downsamp.x);
249 ui32 tcy1 = ojph_div_ceil(ty1, downsamp.y);
250 ui32 recon_tcx0 = ojph_div_ceil(tx0, recon_downsamp.x);
251 ui32 recon_tcy0 = ojph_div_ceil(ty0, recon_downsamp.y);
252 ui32 recon_tcx1 = ojph_div_ceil(tx1, recon_downsamp.x);
253 ui32 recon_tcy1 = ojph_div_ceil(ty1, recon_downsamp.y);
254
255 line_offsets[i] =
256 recon_tcx0 - ojph_div_ceil(tx0 - offset, recon_downsamp.x);
257 comp_rects[i].org.x = tcx0;
258 comp_rects[i].org.y = tcy0;
259 comp_rects[i].siz.w = tcx1 - tcx0;
260 comp_rects[i].siz.h = tcy1 - tcy0;
261 recon_comp_rects[i].org.x = recon_tcx0;
262 recon_comp_rects[i].org.y = recon_tcy0;
263 recon_comp_rects[i].siz.w = recon_tcx1 - recon_tcx0;
264 recon_comp_rects[i].siz.h = recon_tcy1 - recon_tcy0;
265
266 comps[i].finalize_alloc(codestream, this, i, comp_rects[i],
268 width = ojph_max(width, recon_comp_rects[i].siz.w);
269
270 num_bits[i] = szp->get_bit_depth(i);
271 is_signed[i] = szp->is_signed(i);
272 bool result = nlp->get_nonlinear_transform(i, bd, is, nlt_type3[i]);
273 if (result == true && (bd != num_bits[i] || is != is_signed[i]))
274 OJPH_ERROR(0x000300A1, "Mismatch between Ssiz (bit_depth = %d, "
275 "is_signed = %s) from SIZ marker segment, and BDnlt "
276 "(bit_depth = %d, is_signed = %s) from NLT marker segment, "
277 "for component %d", i, num_bits[i],
278 is_signed[i] ? "True" : "False", bd, is ? "True" : "False");
279 if (result == false)
281 cur_line[i] = 0;
283 }
284
285 offset += tile_rect.siz.w;
286
287 //allocate lines
288 const param_cod* cdp = codestream->get_cod();
290 if (this->employ_color_transform)
291 {
292 num_lines = 3;
293 lines = allocator->post_alloc_obj<line_buf>(num_lines);
294 if (reversible[0])
295 for (int i = 0; i < 3; ++i)
296 lines[i].wrap(
297 allocator->post_alloc_data<si32>(width, 0), width, 0);
298 else
299 for (int i = 0; i < 3; ++i)
300 lines[i].wrap(
301 allocator->post_alloc_data<float>(width, 0), width, 0);
302 }
303 else
304 {
305 lines = NULL;
306 num_lines = 0;
307 }
308 next_tile_part = 0;
309 }
310
312 bool tile::push(line_buf *line, ui32 comp_num)
313 {
314 constexpr ui8 type3 =
316
317 assert(comp_num < num_comps);
318 if (cur_line[comp_num] >= comp_rects[comp_num].siz.h)
319 return false;
320 cur_line[comp_num]++;
321
322 //converts to signed representation
323 //employs color transform if there is a need
324 if (!employ_color_transform || comp_num >= 3)
325 {
326 assert(comp_num < num_comps);
327 ui32 comp_width = comp_rects[comp_num].siz.w;
328 line_buf *tc = comps[comp_num].get_line();
329 if (reversible[comp_num])
330 {
331 si64 shift = (si64)1 << (num_bits[comp_num] - 1);
332 if (is_signed[comp_num] && nlt_type3[comp_num] == type3)
333 rev_convert_nlt_type3(line, line_offsets[comp_num],
334 tc, 0, shift + 1, comp_width);
335 else {
336 shift = is_signed[comp_num] ? 0 : -shift;
337 rev_convert(line, line_offsets[comp_num], tc, 0,
338 shift, comp_width);
339 }
340 }
341 else
342 {
343 if (nlt_type3[comp_num] == type3)
345 tc, num_bits[comp_num], is_signed[comp_num], comp_width);
346 else
347 irv_convert_to_float(line, line_offsets[comp_num],
348 tc, num_bits[comp_num], is_signed[comp_num], comp_width);
349 }
350 comps[comp_num].push_line();
351 }
352 else
353 {
354 si64 shift = (si64)1 << (num_bits[comp_num] - 1);
355 ui32 comp_width = comp_rects[comp_num].siz.w;
356 if (reversible[comp_num])
357 {
358 if (is_signed[comp_num] && nlt_type3[comp_num] == type3)
359 rev_convert_nlt_type3(line, line_offsets[comp_num],
360 lines + comp_num, 0, shift + 1, comp_width);
361 else {
362 shift = is_signed[comp_num] ? 0 : -shift;
363 rev_convert(line, line_offsets[comp_num], lines + comp_num, 0,
364 shift, comp_width);
365 }
366
367 if (comp_num == 2)
368 { // reversible color transform
369 rct_forward(lines + 0, lines + 1, lines + 2,
370 comps[0].get_line(),
371 comps[1].get_line(),
372 comps[2].get_line(), comp_width);
373 comps[0].push_line();
374 comps[1].push_line();
375 comps[2].push_line();
376 }
377 }
378 else
379 {
380 if (nlt_type3[comp_num] == type3)
382 lines + comp_num, num_bits[comp_num], is_signed[comp_num],
383 comp_width);
384 else
385 irv_convert_to_float(line, line_offsets[comp_num],
386 lines + comp_num, num_bits[comp_num], is_signed[comp_num],
387 comp_width);
388 if (comp_num == 2)
389 { // irreversible color transform
390 ict_forward(lines[0].f32, lines[1].f32, lines[2].f32,
391 comps[0].get_line()->f32,
392 comps[1].get_line()->f32,
393 comps[2].get_line()->f32, comp_width);
394 comps[0].push_line();
395 comps[1].push_line();
396 comps[2].push_line();
397 }
398 }
399 }
400
401 return true;
402 }
403
405 bool tile::pull(line_buf* tgt_line, ui32 comp_num)
406 {
407 constexpr ui8 type3 =
409
410 assert(comp_num < num_comps);
411 if (cur_line[comp_num] >= recon_comp_rects[comp_num].siz.h)
412 return false;
413
414 cur_line[comp_num]++;
415
417 {
418 line_buf *src_line = comps[comp_num].pull_line();
419 ui32 comp_width = recon_comp_rects[comp_num].siz.w;
420 if (reversible[comp_num])
421 {
422 si64 shift = (si64)1 << (num_bits[comp_num] - 1);
423 if (is_signed[comp_num] && nlt_type3[comp_num] == type3)
424 rev_convert_nlt_type3(src_line, 0, tgt_line,
425 line_offsets[comp_num], shift + 1, comp_width);
426 else {
427 shift = is_signed[comp_num] ? 0 : shift;
428 rev_convert(src_line, 0, tgt_line,
429 line_offsets[comp_num], shift, comp_width);
430 }
431 }
432 else
433 {
434 if (nlt_type3[comp_num] == type3)
435 irv_convert_to_integer_nlt_type3(src_line, tgt_line,
436 line_offsets[comp_num], num_bits[comp_num],
437 is_signed[comp_num], comp_width);
438 else
439 irv_convert_to_integer(src_line, tgt_line,
440 line_offsets[comp_num], num_bits[comp_num],
441 is_signed[comp_num], comp_width);
442 }
443 }
444 else
445 {
446 assert(num_comps >= 3);
447 ui32 comp_width = recon_comp_rects[comp_num].siz.w;
448 if (comp_num == 0)
449 {
450 if (reversible[comp_num])
451 rct_backward(comps[0].pull_line(), comps[1].pull_line(),
452 comps[2].pull_line(), lines + 0, lines + 1,
453 lines + 2, comp_width);
454 else
455 ict_backward(comps[0].pull_line()->f32, comps[1].pull_line()->f32,
456 comps[2].pull_line()->f32, lines[0].f32, lines[1].f32,
457 lines[2].f32, comp_width);
458 }
459 if (reversible[comp_num])
460 {
461 si64 shift = (si64)1 << (num_bits[comp_num] - 1);
462 line_buf* src_line;
463 if (comp_num < 3)
464 src_line = lines + comp_num;
465 else
466 src_line = comps[comp_num].pull_line();
467 if (is_signed[comp_num] && nlt_type3[comp_num] == type3)
468 rev_convert_nlt_type3(src_line, 0, tgt_line,
469 line_offsets[comp_num], shift + 1, comp_width);
470 else {
471 shift = is_signed[comp_num] ? 0 : shift;
472 rev_convert(src_line, 0, tgt_line,
473 line_offsets[comp_num], shift, comp_width);
474 }
475 }
476 else
477 {
478 line_buf* lbp;
479 if (comp_num < 3)
480 lbp = lines + comp_num;
481 else
482 lbp = comps[comp_num].pull_line();
483 if (nlt_type3[comp_num] == type3)
485 line_offsets[comp_num], num_bits[comp_num],
486 is_signed[comp_num], comp_width);
487 else
488 irv_convert_to_integer(lbp, tgt_line,
489 line_offsets[comp_num], num_bits[comp_num],
490 is_signed[comp_num], comp_width);
491 }
492 }
493
494 return true;
495 }
496
497
500 {
501 this->num_bytes = 0;
502 //prepare precinct headers
503 for (ui32 c = 0; c < num_comps; ++c)
504 num_bytes += comps[c].prepare_precincts();
505 }
506
509 {
511 tlm->set_next_pair(sot.get_tile_index(), this->num_bytes);
512 }
514 {
516 ui32 max_decs = 0;
517 for (ui32 c = 0; c < num_comps; ++c)
518 max_decs = ojph_max(max_decs, comps[c].get_num_decompositions());
519 for (ui32 r = 0; r <= max_decs; ++r)
520 {
521 ui32 bytes = 0;
522 for (ui32 c = 0; c < num_comps; ++c)
523 bytes += comps[c].get_num_bytes(r);
524 tlm->set_next_pair(sot.get_tile_index(), bytes);
525 }
526 }
528 {
530 {
531 ui32 max_decs = 0;
532 for (ui32 c = 0; c < num_comps; ++c)
533 max_decs = ojph_max(max_decs, comps[c].get_num_decompositions());
534 for (ui32 r = 0; r <= max_decs; ++r)
535 for (ui32 c = 0; c < num_comps; ++c)
536 if (r <= comps[c].get_num_decompositions())
537 tlm->set_next_pair(sot.get_tile_index(),
538 comps[c].get_num_bytes(r));
539 }
540 else if (prog_order == OJPH_PO_CPRL)
541 for (ui32 c = 0; c < num_comps; ++c)
542 tlm->set_next_pair(sot.get_tile_index(), comps[c].get_num_bytes());
543 else
544 assert(0); // should not be here
545 }
546 else
547 {
549 ui32 max_decs = 0;
550 for (ui32 c = 0; c < num_comps; ++c)
551 max_decs = ojph_max(max_decs, comps[c].get_num_decompositions());
552 for (ui32 r = 0; r <= max_decs; ++r)
553 for (ui32 c = 0; c < num_comps; ++c)
554 if (r <= comps[c].get_num_decompositions())
555 tlm->set_next_pair(sot.get_tile_index(),
556 comps[c].get_num_bytes(r));
557 }
558 }
559
560
563 {
564 ui32 max_decompositions = 0;
565 for (ui32 c = 0; c < num_comps; ++c)
566 max_decompositions = ojph_max(max_decompositions,
567 comps[c].get_num_decompositions());
568
570 {
571 //write tile header
572 if (!sot.write(file, this->num_bytes))
573 OJPH_ERROR(0x00030081, "Error writing to file");
574
575 //write start of data
577 if (!file->write(&t, 2))
578 OJPH_ERROR(0x00030082, "Error writing to file");
579 }
580
581
582 //sequence the writing of precincts according to progression order
584 {
586 {
587 for (ui32 r = 0; r <= max_decompositions; ++r)
588 for (ui32 c = 0; c < num_comps; ++c)
589 comps[c].write_precincts(r, file);
590 }
592 {
593 for (ui32 r = 0; r <= max_decompositions; ++r)
594 {
595 ui32 bytes = 0;
596 for (ui32 c = 0; c < num_comps; ++c)
597 bytes += comps[c].get_num_bytes(r);
598
599 //write tile header
600 if (!sot.write(file, bytes, (ui8)r, (ui8)(max_decompositions + 1)))
601 OJPH_ERROR(0x00030083, "Error writing to file");
602
603 //write start of data
605 if (!file->write(&t, 2))
606 OJPH_ERROR(0x00030084, "Error writing to file");
607
608 //write precincts
609 for (ui32 c = 0; c < num_comps; ++c)
610 comps[c].write_precincts(r, file);
611 }
612 }
613 else
614 {
615 ui32 num_tileparts = num_comps * (max_decompositions + 1);
616 for (ui32 r = 0; r <= max_decompositions; ++r)
617 for (ui32 c = 0; c < num_comps; ++c)
618 if (r <= comps[c].get_num_decompositions()) {
619 //write tile header
620 if (!sot.write(file, comps[c].get_num_bytes(r),
621 (ui8)(c + r * num_comps), (ui8)num_tileparts))
622 OJPH_ERROR(0x00030085, "Error writing to file");
623 //write start of data
625 if (!file->write(&t, 2))
626 OJPH_ERROR(0x00030086, "Error writing to file");
627 comps[c].write_precincts(r, file);
628 }
629 }
630 }
631 else if (prog_order == OJPH_PO_RPCL)
632 {
633 for (ui32 r = 0; r <= max_decompositions; ++r)
634 {
636 {
637 ui32 bytes = 0;
638 for (ui32 c = 0; c < num_comps; ++c)
639 bytes += comps[c].get_num_bytes(r);
640 //write tile header
641 if (!sot.write(file, bytes, (ui8)r, (ui8)(max_decompositions + 1)))
642 OJPH_ERROR(0x00030087, "Error writing to file");
643
644 //write start of data
646 if (!file->write(&t, 2))
647 OJPH_ERROR(0x00030088, "Error writing to file");
648 }
649 while (true)
650 {
651 bool found = false;
652 ui32 comp_num = 0;
653 point smallest(INT_MAX, INT_MAX), cur;
654 for (ui32 c = 0; c < num_comps; ++c)
655 {
656 if (!comps[c].get_top_left_precinct(r, cur))
657 continue;
658 else
659 found = true;
660
661 if (cur.y < smallest.y)
662 { smallest = cur; comp_num = c; }
663 else if (cur.y == smallest.y && cur.x < smallest.x)
664 { smallest = cur; comp_num = c; }
665 }
666 if (found == true)
667 comps[comp_num].write_one_precinct(r, file);
668 else
669 break;
670 }
671 }
672 }
673 else if (prog_order == OJPH_PO_PCRL)
674 {
675 while (true)
676 {
677 bool found = false;
678 ui32 comp_num = 0;
679 ui32 res_num = 0;
680 point smallest(INT_MAX, INT_MAX), cur;
681 for (ui32 c = 0; c < num_comps; ++c)
682 {
683 for (ui32 r = 0; r <= comps[c].get_num_decompositions(); ++r)
684 {
685 if (!comps[c].get_top_left_precinct(r, cur))
686 continue;
687 else
688 found = true;
689
690 if (cur.y < smallest.y)
691 { smallest = cur; comp_num = c; res_num = r; }
692 else if (cur.y == smallest.y && cur.x < smallest.x)
693 { smallest = cur; comp_num = c; res_num = r; }
694 else if (cur.y == smallest.y && cur.x == smallest.x &&
695 c < comp_num)
696 { smallest = cur; comp_num = c; res_num = r; }
697 else if (cur.y == smallest.y && cur.x == smallest.x &&
698 c == comp_num && r < res_num)
699 { smallest = cur; comp_num = c; res_num = r; }
700 }
701 }
702 if (found == true)
703 comps[comp_num].write_one_precinct(res_num, file);
704 else
705 break;
706 }
707 }
708 else if (prog_order == OJPH_PO_CPRL)
709 {
710 for (ui32 c = 0; c < num_comps; ++c)
711 {
713 {
714 ui32 bytes = comps[c].get_num_bytes();
715 //write tile header
716 if (!sot.write(file, bytes, (ui8)c, (ui8)num_comps))
717 OJPH_ERROR(0x0003008A, "Error writing to file");
718
719 //write start of data
721 if (!file->write(&t, 2))
722 OJPH_ERROR(0x0003008B, "Error writing to file");
723 }
724
725 while (true)
726 {
727 bool found = false;
728 ui32 res_num = 0;
729 point smallest(INT_MAX, INT_MAX), cur;
730 for (ui32 r = 0; r <= max_decompositions; ++r)
731 {
732 if (!comps[c].get_top_left_precinct(r, cur)) //res exist?
733 continue;
734 else
735 found = true;
736
737 if (cur.y < smallest.y)
738 { smallest = cur; res_num = r; }
739 else if (cur.y == smallest.y && cur.x < smallest.x)
740 { smallest = cur; res_num = r; }
741 }
742 if (found == true)
743 comps[c].write_one_precinct(res_num, file);
744 else
745 break;
746 }
747 }
748 }
749 else
750 assert(0);
751
752 }
753
756 const ui64& tile_start_location)
757 {
758 if (sot.get_tile_part_index() != next_tile_part)
759 {
760 if (resilient)
761 OJPH_INFO(0x00030091, "wrong tile part index")
762 else
763 OJPH_ERROR(0x00030091, "wrong tile part index")
764 }
766
767 //tile_end_location used on failure
768 ui64 tile_end_location = tile_start_location + sot.get_payload_length();
769
770 ui32 data_left = sot.get_payload_length(); //bytes left to parse
771 data_left -= (ui32)((ui64)file->tell() - tile_start_location);
772
773 if (data_left == 0)
774 return;
775
776 ui32 max_decompositions = 0;
777 for (ui32 c = 0; c < num_comps; ++c)
778 max_decompositions = ojph_max(max_decompositions,
779 comps[c].get_num_decompositions());
780
781 try
782 {
783 //sequence the reading of precincts according to progression order
785 {
786 max_decompositions -= skipped_res_for_read;
787 for (ui32 r = 0; r <= max_decompositions; ++r)
788 for (ui32 c = 0; c < num_comps; ++c)
789 if (data_left > 0)
790 comps[c].parse_precincts(r, data_left, file);
791 }
792 else if (prog_order == OJPH_PO_RPCL)
793 {
794 max_decompositions -= skipped_res_for_read;
795 for (ui32 r = 0; r <= max_decompositions; ++r)
796 {
797 while (true)
798 {
799 bool found = false;
800 ui32 comp_num = 0;
801 point smallest(INT_MAX, INT_MAX), cur;
802 for (ui32 c = 0; c < num_comps; ++c)
803 {
804 if (!comps[c].get_top_left_precinct(r, cur))
805 continue;
806 else
807 found = true;
808
809 if (cur.y < smallest.y)
810 { smallest = cur; comp_num = c; }
811 else if (cur.y == smallest.y && cur.x < smallest.x)
812 { smallest = cur; comp_num = c; }
813 }
814 if (found == true && data_left > 0)
815 comps[comp_num].parse_one_precinct(r, data_left, file);
816 else
817 break;
818 }
819 }
820 }
821 else if (prog_order == OJPH_PO_PCRL)
822 {
823 while (true)
824 {
825 bool found = false;
826 ui32 comp_num = 0;
827 ui32 res_num = 0;
828 point smallest(INT_MAX, INT_MAX), cur;
829 for (ui32 c = 0; c < num_comps; ++c)
830 {
831 for (ui32 r = 0; r <= comps[c].get_num_decompositions(); ++r)
832 {
833 if (!comps[c].get_top_left_precinct(r, cur))
834 continue;
835 else
836 found = true;
837
838 if (cur.y < smallest.y)
839 { smallest = cur; comp_num = c; res_num = r; }
840 else if (cur.y == smallest.y && cur.x < smallest.x)
841 { smallest = cur; comp_num = c; res_num = r; }
842 else if (cur.y == smallest.y && cur.x == smallest.x &&
843 c < comp_num)
844 { smallest = cur; comp_num = c; res_num = r; }
845 else if (cur.y == smallest.y && cur.x == smallest.x &&
846 c == comp_num && r < res_num)
847 { smallest = cur; comp_num = c; res_num = r; }
848 }
849 }
850 if (found == true && data_left > 0)
851 comps[comp_num].parse_one_precinct(res_num, data_left, file);
852 else
853 break;
854 }
855 }
856 else if (prog_order == OJPH_PO_CPRL)
857 {
858 for (ui32 c = 0; c < num_comps; ++c)
859 {
860 while (true)
861 {
862 bool found = false;
863 ui32 res_num = 0;
864 point smallest(INT_MAX, INT_MAX), cur;
865 for (ui32 r = 0; r <= max_decompositions; ++r)
866 {
867 if (!comps[c].get_top_left_precinct(r, cur)) //res exist?
868 continue;
869 else
870 found = true;
871
872 if (cur.y < smallest.y)
873 { smallest = cur; res_num = r; }
874 else if (cur.y == smallest.y && cur.x < smallest.x)
875 { smallest = cur; res_num = r; }
876 }
877 if (found == true && data_left > 0)
878 comps[c].parse_one_precinct(res_num, data_left, file);
879 else
880 break;
881 }
882 }
883 }
884 else
885 assert(0);
886
887 }
888 catch (const char *error)
889 {
890 if (resilient)
891 OJPH_INFO(0x00030092, "%s", error)
892 else
893 OJPH_ERROR(0x00030092, "%s", error)
894 }
895 file->seek((si64)tile_end_location, infile_base::OJPH_SEEK_SET);
896 }
897
898 }
899}
virtual si64 tell()=0
const param_cod * get_coc(ui32 comp_num)
mem_fixed_allocator * get_allocator()
static void pre_alloc(codestream *codestream, ui32 comp_num, const rect &comp_rect, const rect &recon_comp_rect)
bool pull(line_buf *, ui32 comp_num)
line_buf * lines
Definition ojph_tile.h:83
void finalize_alloc(codestream *codestream, const rect &tile_rect, ui32 tile_idx, ui32 &offset, ui32 &num_tileparts)
static void pre_alloc(codestream *codestream, const rect &tile_rect, const rect &recon_tile_rect, ui32 &num_tileparts)
Definition ojph_tile.cpp:56
tile_comp * comps
Definition ojph_tile.h:81
void fill_tlm(param_tlm *tlm)
rect * recon_comp_rects
Definition ojph_tile.h:86
void flush(outfile_base *file)
ui32 skipped_res_for_read
Definition ojph_tile.h:88
param_sot sot
Definition ojph_tile.h:97
bool push(line_buf *line, ui32 comp_num)
bool employ_color_transform
Definition ojph_tile.h:84
void parse_tile_header(const param_sot &sot, infile_base *file, const ui64 &tile_start_location)
ui32 * line_offsets
Definition ojph_tile.h:87
void pre_alloc_data(size_t num_ele, ui32 pre_size)
Definition ojph_mem.h:89
void pre_alloc_obj(size_t num_ele)
Definition ojph_mem.h:95
T * post_alloc_data(size_t num_ele, ui32 pre_size)
Definition ojph_mem.h:129
T * post_alloc_obj(size_t num_ele)
Definition ojph_mem.h:136
virtual size_t write(const void *ptr, size_t size)=0
int get_progression_order() const
@ OJPH_NLT_BINARY_COMPLEMENT_NLT
void(* rct_forward)(const line_buf *r, const line_buf *g, const line_buf *b, line_buf *y, line_buf *cb, line_buf *cr, ui32 repeat)
void(* ict_forward)(const float *r, const float *g, const float *b, float *y, float *cb, float *cr, ui32 repeat)
void(* irv_convert_to_integer_nlt_type3)(const line_buf *src_line, line_buf *dst_line, ui32 dst_line_offset, ui32 bit_depth, bool is_signed, ui32 width)
void(* irv_convert_to_float)(const line_buf *src_line, ui32 src_line_offset, line_buf *dst_line, ui32 bit_depth, bool is_signed, ui32 width)
void(* ict_backward)(const float *y, const float *cb, const float *cr, float *r, float *g, float *b, ui32 repeat)
void(* rct_backward)(const line_buf *r, const line_buf *g, const line_buf *b, line_buf *y, line_buf *cb, line_buf *cr, ui32 repeat)
void(* irv_convert_to_integer)(const line_buf *src_line, line_buf *dst_line, ui32 dst_line_offset, ui32 bit_depth, bool is_signed, ui32 width)
static ui16 swap_byte(ui16 t)
void(* irv_convert_to_float_nlt_type3)(const line_buf *src_line, ui32 src_line_offset, line_buf *dst_line, ui32 bit_depth, bool is_signed, ui32 width)
void(* rev_convert_nlt_type3)(const line_buf *src_line, const ui32 src_line_offset, line_buf *dst_line, const ui32 dst_line_offset, si64 shift, ui32 width)
void(* rev_convert)(const line_buf *src_line, const ui32 src_line_offset, line_buf *dst_line, const ui32 dst_line_offset, si64 shift, ui32 width)
int64_t si64
Definition ojph_defs.h:57
uint64_t ui64
Definition ojph_defs.h:56
uint16_t ui16
Definition ojph_defs.h:52
@ OJPH_TILEPART_RESOLUTIONS
@ OJPH_TILEPART_NO_DIVISIONS
@ OJPH_TILEPART_COMPONENTS
int32_t si32
Definition ojph_defs.h:55
message_error error
uint32_t ui32
Definition ojph_defs.h:54
uint8_t ui8
Definition ojph_defs.h:50
#define ojph_max(a, b)
Definition ojph_defs.h:73
#define ojph_div_ceil(a, b)
Definition ojph_defs.h:70
#define OJPH_INFO(t,...)
MACROs to insert file and line number for info, warning, and error.
#define OJPH_ERROR(t,...)
bool is_employing_color_transform() const
bool get_nonlinear_transform(ui32 comp_num, ui8 &bit_depth, bool &is_signed, ui8 &nl_type) const
ui32 get_bit_depth(ui32 comp_num) const
bool is_signed(ui32 comp_num) const
point get_recon_downsampling(ui32 comp_num) const
point get_downsampling(ui32 comp_num) const
void set_next_pair(ui16 Ttlm, ui32 Ptlm)
point org
Definition ojph_base.h:66