OpenJPH
Open-source implementation of JPEG2000 Part-15
Loading...
Searching...
No Matches
ojph_codestream_local.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_codestream_local.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
47#include "../transform/ojph_colour.h"
48#include "../transform/ojph_transform.h"
49
50namespace ojph {
51
52 namespace local
53 {
54
57 : precinct_scratch(NULL), allocator(NULL), elastic_alloc(NULL)
58 {
59 tiles = NULL;
60 lines = NULL;
61 comp_size = NULL;
62 recon_comp_size = NULL;
63 allocator = NULL;
64 outfile = NULL;
65 infile = NULL;
66
67 num_comps = 0;
69 planar = -1;
72 need_tlm = false;
73
74 cur_comp = 0;
75 cur_line = 0;
76 cur_tile_row = 0;
77 resilient = false;
79
81
83 qcc = qcc_store;
85 coc = coc_store;
86
87 atk = atk_store;
88 atk[0].init_irv97();
89 atk[0].link(atk_store + 1);
90 atk[1].init_rev53();
91 atk[1].link(atk_store + 2);
92
94 elastic_alloc = new mem_elastic_allocator(1048576); //1 megabyte
95
98 }
99
102 {
103 if (qcc_store != qcc)
104 delete[] qcc;
105 if (allocator)
106 delete allocator;
107 if (elastic_alloc)
108 delete elastic_alloc;
109 }
110
113 {
119 if (num_tiles.area() > 65535)
120 OJPH_ERROR(0x00030011, "number of tiles cannot exceed 65535");
121
122 //allocate tiles
124
125 ui32 num_tileparts = 0;
126 point index;
127 rect tile_rect, recon_tile_rect;
128 ui32 ds = 1 << skipped_res_for_recon;
129 for (index.y = 0; index.y < num_tiles.h; ++index.y)
130 {
131 ui32 y0 = sz.get_tile_offset().y
132 + index.y * sz.get_tile_size().h;
133 ui32 y1 = y0 + sz.get_tile_size().h; //end of tile
134
135 tile_rect.org.y = ojph_max(y0, sz.get_image_offset().y);
136 tile_rect.siz.h =
137 ojph_min(y1, sz.get_image_extent().y) - tile_rect.org.y;
138
139 recon_tile_rect.org.y = ojph_max(ojph_div_ceil(y0, ds),
141 recon_tile_rect.siz.h = ojph_min(ojph_div_ceil(y1, ds),
143 - recon_tile_rect.org.y;
144
145 for (index.x = 0; index.x < num_tiles.w; ++index.x)
146 {
147 ui32 x0 = sz.get_tile_offset().x
148 + index.x * sz.get_tile_size().w;
149 ui32 x1 = x0 + sz.get_tile_size().w;
150
151 tile_rect.org.x = ojph_max(x0, sz.get_image_offset().x);
152 tile_rect.siz.w =
153 ojph_min(x1, sz.get_image_extent().x) - tile_rect.org.x;
154
155 recon_tile_rect.org.x = ojph_max(ojph_div_ceil(x0, ds),
157 recon_tile_rect.siz.w = ojph_min(ojph_div_ceil(x1, ds),
159 - recon_tile_rect.org.x;
160
161 ui32 tps = 0; // number of tileparts for this tile
162 tile::pre_alloc(this, tile_rect, recon_tile_rect, tps);
163 num_tileparts += tps;
164 }
165 }
166
167 //allocate lines
168 //These lines are used by codestream to exchange data with external
169 // world
172 allocator->pre_alloc_obj<size>(num_comps); //for *comp_size
173 allocator->pre_alloc_obj<size>(num_comps); //for *recon_comp_size
174 for (ui32 i = 0; i < num_comps; ++i)
176
177 //allocate tlm
178 if (outfile != NULL && need_tlm)
180
181 //precinct scratch buffer
182 ui32 num_decomps = cod.get_num_decompositions();
183 size log_cb = cod.get_log_block_dims();
184
185 size ratio;
186 for (ui32 r = 0; r <= num_decomps; ++r)
187 {
188 size log_PP = cod.get_log_precinct_size(r);
189 ratio.w = ojph_max(ratio.w, log_PP.w - ojph_min(log_cb.w, log_PP.w));
190 ratio.h = ojph_max(ratio.h, log_PP.h - ojph_min(log_cb.h, log_PP.h));
191 }
192 ui32 max_ratio = ojph_max(ratio.w, ratio.h);
193 max_ratio = 1 << max_ratio;
194 // assuming that we have a hierarchy of n levels.
195 // This needs 4/3 times the area, rounded up
196 // (rounding up leaves one extra entry).
197 // This exta entry is necessary
198 // We need 4 such tables. These tables store
199 // 1. missing msbs and 2. their flags,
200 // 3. number of layers and 4. their flags
202 4 * ((max_ratio * max_ratio * 4 + 2) / 3);
203
205 }
206
209 {
210 allocator->alloc();
211
212 //precinct scratch buffer
215
216 //get tiles
217 tiles = this->allocator->post_alloc_obj<tile>((size_t)num_tiles.area());
218
219 ui32 num_tileparts = 0;
220 point index;
221 rect tile_rect;
223 for (index.y = 0; index.y < num_tiles.h; ++index.y)
224 {
225 ui32 y0 = sz.get_tile_offset().y
226 + index.y * sz.get_tile_size().h;
227 ui32 y1 = y0 + sz.get_tile_size().h; //end of tile
228
229 tile_rect.org.y = ojph_max(y0, sz.get_image_offset().y);
230 tile_rect.siz.h =
231 ojph_min(y1, sz.get_image_extent().y) - tile_rect.org.y;
232
233 ui32 offset = 0;
234 for (index.x = 0; index.x < num_tiles.w; ++index.x)
235 {
236 ui32 x0 = sz.get_tile_offset().x
237 + index.x * sz.get_tile_size().w;
238 ui32 x1 = x0 + sz.get_tile_size().w;
239
240 tile_rect.org.x = ojph_max(x0, sz.get_image_offset().x);
241 tile_rect.siz.w =
242 ojph_min(x1, sz.get_image_extent().x) - tile_rect.org.x;
243
244 ui32 tps = 0; // number of tileparts for this tile
245 ui32 idx = index.y * num_tiles.w + index.x;
246 tiles[idx].finalize_alloc(this, tile_rect, idx, offset, tps);
247 num_tileparts += tps;
248 }
249 }
250
251 //allocate lines
252 //These lines are used by codestream to exchange data with external
253 // world
254 this->num_comps = sz.get_num_components();
259 for (ui32 i = 0; i < this->num_comps; ++i)
260 {
261 comp_size[i].w = siz.get_width(i);
262 comp_size[i].h = siz.get_height(i);
263 ui32 cw = siz.get_recon_width(i);
264 recon_comp_size[i].w = cw;
266 lines[i].wrap(allocator->post_alloc_data<si32>(cw, 0), cw, 0);
267 }
268
269 cur_comp = 0;
270 cur_line = 0;
271
272 //allocate tlm
273 if (outfile != NULL && need_tlm)
274 tlm.init(num_tileparts,
276 }
277
278
281 {
282 //two possibilities lossy single tile or lossless
283 //For the following code, we use the least strict profile
284 ojph::param_siz sz(&siz);
285 ojph::param_cod cd(&cod);
286 bool reversible = cd.is_reversible();
287 bool imf2k = !reversible, imf4k = !reversible, imf8k = !reversible;
288 bool imf2kls = reversible, imf4kls = reversible, imf8kls = reversible;
289
290 if (reversible)
291 {
292 point ext = sz.get_image_extent();
293 if (ext.x <= 2048 && ext.y <= 1556)
294 imf2kls &= true;
295 if (ext.x <= 4096 && ext.y <= 3112)
296 imf4kls &= true;
297 if (ext.x <= 8192 && ext.y <= 6224)
298 imf8kls &= true;
299
300 if (!imf2kls && !imf4kls && !imf8kls)
301 OJPH_ERROR(0x000300C1,
302 "Image dimensions do not meet any of the lossless IMF profiles");
303 }
304 else
305 {
306 point ext = sz.get_image_extent();
307 if (ext.x <= 2048 && ext.y <= 1556)
308 imf2k &= true;
309 if (ext.x <= 4096 && ext.y <= 3112)
310 imf4k &= true;
311 if (ext.x <= 8192 && ext.y <= 6224)
312 imf8k &= true;
313
314 if (!imf2k && !imf4k && !imf8k)
315 OJPH_ERROR(0x000300C2,
316 "Image dimensions do not meet any of the lossy IMF profiles");
317 }
318
319
320 if (sz.get_image_offset().x != 0 || sz.get_image_offset().y != 0)
321 OJPH_ERROR(0x000300C3,
322 "For IMF profile, image offset (XOsiz, YOsiz) has to be 0.");
323 if (sz.get_tile_offset().x != 0 || sz.get_tile_offset().y != 0)
324 OJPH_ERROR(0x000300C4,
325 "For IMF profile, tile offset (XTOsiz, YTOsiz) has to be 0.");
326 if (sz.get_num_components() > 3)
327 OJPH_ERROR(0x000300C5,
328 "For IMF profile, the number of components has to be less "
329 " or equal to 3");
330 bool test_ds1 = true, test_ds2 = true;
331 for (ojph::ui32 i = 0; i < sz.get_num_components(); ++i)
332 {
333 point downsamping = sz.get_downsampling(i);
334 test_ds1 &= downsamping.y == 1;
335 test_ds2 &= downsamping.y == 1;
336
337 test_ds1 &= downsamping.x == 1;
338 if (i == 1 || i == 2)
339 test_ds2 &= downsamping.x == 2;
340 else
341 test_ds2 &= downsamping.x == 1;
342 }
343 if (!test_ds1 && !test_ds2)
344 OJPH_ERROR(0x000300C6,
345 "For IMF profile, either no component downsampling is used,"
346 " or the x-dimension of the 2nd and 3rd components is downsampled"
347 " by 2.");
348
349 bool test_bd = true;
350 for (ojph::ui32 i = 0; i < sz.get_num_components(); ++i)
351 {
352 ui32 bit_depth = sz.get_bit_depth(i);
353 bool is_signed = sz.is_signed(i);
354 test_bd &= bit_depth >= 8 && bit_depth <= 16 && is_signed == false;
355 }
356 if (!test_bd)
357 OJPH_ERROR(0x000300C7,
358 "For IMF profile, compnent bit_depth has to be between"
359 " 8 and 16 bits inclusively, and the samples must be unsigned");
360
361 if (cd.get_log_block_dims().w != 5 || cd.get_log_block_dims().h != 5)
362 OJPH_ERROR(0x000300C8,
363 "For IMF profile, codeblock dimensions are restricted."
364 " Use \"-block_size {32,32}\" at the commandline");
365
366 ui32 num_decomps = cd.get_num_decompositions();
367 bool test_pz = cd.get_log_precinct_size(0).w == 7
368 && cd.get_log_precinct_size(0).h == 7;
369 for (ui32 i = 1; i <= num_decomps; ++i)
370 test_pz = cd.get_log_precinct_size(i).w == 8
371 && cd.get_log_precinct_size(i).h == 8;
372 if (!test_pz)
373 OJPH_ERROR(0x000300C9,
374 "For IMF profile, precinct sizes are restricted."
375 " Use \"-precincts {128,128},{256,256}\" at the commandline");
376
378 OJPH_ERROR(0x000300CA,
379 "For IMF profile, the CPRL progression order must be used."
380 " Use \"-prog_order CPRL\".");
381
382 imf2k &= num_decomps <= 5;
383 imf2kls &= num_decomps <= 5;
384 imf4k &= num_decomps <= 6;
385 imf4kls &= num_decomps <= 6;
386 imf8k &= num_decomps <= 7;
387 imf8kls &= num_decomps <= 7;
388
389 if (num_decomps == 0 ||
390 (!imf2k && !imf4k && !imf8k && !imf2kls && !imf4kls && !imf8kls))
391 OJPH_ERROR(0x000300CB,
392 "Number of decompositions does not match the IMF profile"
393 " dictated by wavelet reversibility and image dimensions.");
394
395 ui32 tiles_w = sz.get_image_extent().x;
396 tiles_w = ojph_div_ceil(tiles_w, sz.get_tile_size().w);
397 ui32 tiles_h = sz.get_image_extent().y;
398 tiles_h = ojph_div_ceil(tiles_h, sz.get_tile_size().h);
399 ui32 total_tiles = tiles_w * tiles_h;
400
401 if (total_tiles > 1)
402 {
403 if (!reversible)
404 OJPH_ERROR(0x000300CC,
405 "Lossy IMF profile must have one tile.");
406
407 size tt = sz.get_tile_size();
408 imf2kls &= (tt.w == 1024 && tt.h == 1024);
409 imf2kls &= (tt.w >= 1024 && num_decomps <= 4)
410 || (tt.w >= 2048 && num_decomps <= 5);
411 imf4kls &= (tt.w == 1024 && tt.h == 1024)
412 || (tt.w == 2048 && tt.h == 2048);
413 imf4kls &= (tt.w >= 1024 && num_decomps <= 4)
414 || (tt.w >= 2048 && num_decomps <= 5)
415 || (tt.w >= 4096 && num_decomps <= 6);
416 imf8kls &= (tt.w == 1024 && tt.h == 1024)
417 || (tt.w == 2048 && tt.h == 2048)
418 || (tt.w == 4096 && tt.h == 4096);
419 imf8kls &= (tt.w >= 1024 && num_decomps <= 4)
420 || (tt.w >= 2048 && num_decomps <= 5)
421 || (tt.w >= 4096 && num_decomps <= 6)
422 || (tt.w >= 8192 && num_decomps <= 7);
423 if (!imf2kls && !imf4kls && !imf8kls)
424 OJPH_ERROR(0x000300CD,
425 "Number of decompositions does not match the IMF profile"
426 " dictated by wavelet reversibility and image dimensions and"
427 " tiles.");
428 }
429
430 need_tlm = true;
433 {
435 OJPH_WARN(0x000300C1,
436 "In IMF profile, tile part divisions at the component level must be "
437 "employed, while at the resolution level is not allowed. "
438 "This has been corrected.");
439 }
440 }
441
444 {
445 ojph::param_siz sz(&siz);
446 ojph::param_cod cd(&cod);
447
448 if (sz.get_image_offset().x != 0 || sz.get_image_offset().y != 0)
449 OJPH_ERROR(0x000300B1,
450 "For broadcast profile, image offset (XOsiz, YOsiz) has to be 0.");
451 if (sz.get_tile_offset().x != 0 || sz.get_tile_offset().y != 0)
452 OJPH_ERROR(0x000300B2,
453 "For broadcast profile, tile offset (XTOsiz, YTOsiz) has to be 0.");
454 if (sz.get_num_components() > 4)
455 OJPH_ERROR(0x000300B3,
456 "For broadcast profile, the number of components has to be less "
457 " or equal to 4");
458 bool test_ds1 = true, test_ds2 = true;
459 for (ojph::ui32 i = 0; i < sz.get_num_components(); ++i)
460 {
461 point downsamping = sz.get_downsampling(i);
462 test_ds1 &= downsamping.y == 1;
463 test_ds2 &= downsamping.y == 1;
464
465 test_ds1 &= downsamping.x == 1;
466 if (i == 1 || i == 2)
467 test_ds2 &= downsamping.x == 2;
468 else
469 test_ds2 &= downsamping.x == 1;
470 }
471 if (!test_ds1 && !test_ds2)
472 OJPH_ERROR(0x000300B4,
473 "For broadcast profile, either no component downsampling is used,"
474 " or the x-dimension of the 2nd and 3rd components is downsampled"
475 " by 2.");
476
477 bool test_bd = true;
478 for (ojph::ui32 i = 0; i < sz.get_num_components(); ++i)
479 {
480 ui32 bit_depth = sz.get_bit_depth(i);
481 bool is_signed = sz.is_signed(i);
482 test_bd &= bit_depth >= 8 && bit_depth <= 12 && is_signed == false;
483 }
484 if (!test_bd)
485 OJPH_ERROR(0x000300B5,
486 "For broadcast profile, compnent bit_depth has to be between"
487 " 8 and 12 bits inclusively, and the samples must be unsigned");
488
489 ui32 num_decomps = cd.get_num_decompositions();
490 if (num_decomps == 0 || num_decomps > 5)
491 OJPH_ERROR(0x000300B6,
492 "For broadcast profile, number of decompositions has to be between"
493 "1 and 5 inclusively.");
494
495 if (cd.get_log_block_dims().w < 5 || cd.get_log_block_dims().w > 7)
496 OJPH_ERROR(0x000300B7,
497 "For broadcast profile, codeblock dimensions are restricted such"
498 " that codeblock width has to be either 32, 64, or 128.");
499
500 if (cd.get_log_block_dims().h < 5 || cd.get_log_block_dims().h > 7)
501 OJPH_ERROR(0x000300B8,
502 "For broadcast profile, codeblock dimensions are restricted such"
503 " that codeblock height has to be either 32, 64, or 128.");
504
505 bool test_pz = cd.get_log_precinct_size(0).w == 7
506 && cd.get_log_precinct_size(0).h == 7;
507 for (ui32 i = 1; i <= num_decomps; ++i)
508 test_pz = cd.get_log_precinct_size(i).w == 8
509 && cd.get_log_precinct_size(i).h == 8;
510 if (!test_pz)
511 OJPH_ERROR(0x000300B9,
512 "For broadcast profile, precinct sizes are restricted."
513 " Use \"-precincts {128,128},{256,256}\" at the commandline");
514
516 OJPH_ERROR(0x000300BA,
517 "For broadcast profile, the CPRL progression order must be used."
518 " Use \"-prog_order CPRL\".");
519
520 ui32 tiles_w = sz.get_image_extent().x;
521 tiles_w = ojph_div_ceil(tiles_w, sz.get_tile_size().w);
522 ui32 tiles_h = sz.get_image_extent().y;
523 tiles_h = ojph_div_ceil(tiles_h, sz.get_tile_size().h);
524 ui32 total_tiles = tiles_w * tiles_h;
525
526 if (total_tiles != 1 && total_tiles != 4)
527 OJPH_ERROR(0x000300BB,
528 "The broadcast profile can only have 1 or 4 tiles");
529
530 need_tlm = true;
533 {
535 OJPH_WARN(0x000300B1,
536 "In BROADCAST profile, tile part divisions at the component level "
537 "must be employed, while at the resolution level is not allowed. "
538 "This has been corrected.");
539 }
540 }
541
544 const comment_exchange* comments,
545 ui32 num_comments)
546 {
547 //finalize
554 if (profile == OJPH_PN_IMF)
556 else if (profile == OJPH_PN_BROADCAST)
558
560 if ((po == OJPH_PO_LRCP || po == OJPH_PO_RLCP) &&
562 {
564 OJPH_INFO(0x00030021,
565 "For LRCP and RLCP progression orders, tilepart divisions at the "
566 "component level, means that we have a tilepart for every "
567 "resolution and component.\n");
568 }
570 {
571 tilepart_div &= ~OJPH_TILEPART_COMPONENTS;
572 OJPH_WARN(0x00030021,
573 "For RPCL progression, having tilepart divisions at the component "
574 "level means a tilepart for every precinct, which does not "
575 "make sense, since we can have no more than 255 tile parts. This "
576 "has been corrected by removing tilepart divisions at the component "
577 "level.");
578 }
579 if (po == OJPH_PO_PCRL && tilepart_div != 0)
580 {
581 tilepart_div = 0;
582 OJPH_WARN(0x00030022,
583 "For PCRL progression, having tilepart divisions at the component "
584 "level or the resolution level means a tile part for every "
585 "precinct, which does not make sense, since we can have no more "
586 "than 255 tile parts. This has been corrected by removing tilepart "
587 "divisions; use another progression if you want tileparts.");
588 }
590 {
591 tilepart_div &= ~OJPH_TILEPART_RESOLUTIONS;
592 OJPH_WARN(0x00030023,
593 "For CPRL progression, having tilepart divisions at the resolution "
594 "level means a tile part for every precinct, which does not "
595 "make sense, since we can have no more than 255 tile parts. This "
596 "has been corrected by removing tilepart divisions at the "
597 "resolution level.");
598 }
599
600 if (planar == -1) //not initialized
602 else if (planar == 0) //interleaved is chosen
603 {
604 }
605 else if (planar == 1) //plannar is chosen
606 {
607 if (cod.is_employing_color_transform() == true)
608 OJPH_ERROR(0x00030021,
609 "the planar interface option cannot be used when colour "
610 "transform is employed");
611 }
612 else
613 assert(0);
614
615 assert(this->outfile == NULL);
616 this->outfile = file;
617 this->pre_alloc();
618 this->finalize_alloc();
619
621 if (file->write(&t, 2) != 2)
622 OJPH_ERROR(0x00030022, "Error writing to file");
623
624 if (!siz.write(file))
625 OJPH_ERROR(0x00030023, "Error writing to file");
626
627 if (!cap.write(file))
628 OJPH_ERROR(0x00030024, "Error writing to file");
629
630 if (!cod.write(file))
631 OJPH_ERROR(0x00030025, "Error writing to file");
632
633 if (!qcd.write(file))
634 OJPH_ERROR(0x00030026, "Error writing to file");
635
636 if (!nlt.write(file))
637 OJPH_ERROR(0x00030027, "Error writing to file");
638
639 char buf[] = " OpenJPH Ver "
643 size_t len = strlen(buf);
645 *(ui16*)(buf + 2) = swap_byte((ui16)(len - 2));
646 //1 for General use (IS 8859-15:1999 (Latin) values)
647 *(ui16*)(buf + 4) = swap_byte((ui16)(1));
648 if (file->write(buf, len) != len)
649 OJPH_ERROR(0x00030028, "Error writing to file");
650
651 if (comments != NULL) {
652 for (ui32 i = 0; i < num_comments; ++i)
653 {
655 if (file->write(&t, 2) != 2)
656 OJPH_ERROR(0x00030029, "Error writing to file");
657 t = swap_byte((ui16)(comments[i].len + 4));
658 if (file->write(&t, 2) != 2)
659 OJPH_ERROR(0x0003002A, "Error writing to file");
660 //1 for General use (IS 8859-15:1999 (Latin) values)
661 t = swap_byte(comments[i].Rcom);
662 if (file->write(&t, 2) != 2)
663 OJPH_ERROR(0x0003002B, "Error writing to file");
664 if (file->write(comments[i].data, comments[i].len)!=comments[i].len)
665 OJPH_ERROR(0x0003002C, "Error writing to file");
666 }
667 }
668 }
669
671 static
672 int find_marker(infile_base *f, const ui16* char_list, int list_len)
673 {
674 //returns the marker index in char_list, or -1
675 while (!f->eof())
676 {
677 ui8 new_char;
678 size_t num_bytes = f->read(&new_char, 1);
679 if (num_bytes != 1)
680 return -1;
681 if (new_char == 0xFF)
682 {
683 size_t num_bytes = f->read(&new_char, 1);
684
685 if (num_bytes != 1)
686 return -1;
687
688 for (int i = 0; i < list_len; ++i)
689 if (new_char == (char_list[i] & 0xFF))
690 return i;
691 }
692 }
693 return -1;
694 }
695
697 static
698 int skip_marker(infile_base *file, const char *marker,
699 const char *msg, int msg_level, bool resilient)
700 {
701 ojph_unused(marker);
702 ui16 com_len;
703 if (file->read(&com_len, 2) != 2)
704 {
705 if (resilient)
706 return -1;
707 else
708 OJPH_ERROR(0x00030041, "error reading marker");
709 }
710 com_len = swap_byte(com_len);
711 file->seek(com_len - 2, infile_base::OJPH_SEEK_CUR);
712 if (msg != NULL && msg_level != OJPH_MSG_LEVEL::NO_MSG)
713 {
714 if (msg_level == OJPH_MSG_LEVEL::INFO)
715 {
716 OJPH_INFO(0x00030001, "%s", msg);
717 }
718 else if (msg_level == OJPH_MSG_LEVEL::WARN)
719 {
720 OJPH_WARN(0x00030001, "%s", msg);
721 }
722 else if (msg_level == OJPH_MSG_LEVEL::ERROR)
723 {
724 OJPH_ERROR(0x00030001, "%s", msg);
725 }
726 else // there is the option of ALL_MSG but it should not be used here
727 assert(0);
728 }
729 return 0;
730 }
731
734 {
735 ui16 marker_list[20] = { SOC, SIZ, CAP, PRF, CPF, COD, COC, QCD, QCC,
736 RGN, POC, PPM, TLM, PLM, CRG, COM, DFS, ATK, NLT, SOT };
737 find_marker(file, marker_list, 1); //find SOC
738 find_marker(file, marker_list + 1, 1); //find SIZ
739 siz.read(file);
740 int marker_idx = 0;
741 int received_markers = 0; //check that COD, & QCD received
742 while (true)
743 {
744 marker_idx = find_marker(file, marker_list + 2, 18);
745 if (marker_idx == 0)
746 cap.read(file);
747 else if (marker_idx == 1)
748 //Skipping PRF marker segment; this should not cause any issues
749 skip_marker(file, "PRF", NULL, OJPH_MSG_LEVEL::NO_MSG, false);
750 else if (marker_idx == 2)
751 //Skipping CPF marker segment; this should not cause any issues
752 skip_marker(file, "CPF", NULL, OJPH_MSG_LEVEL::NO_MSG, false);
753 else if (marker_idx == 3)
754 {
756 received_markers |= 1;
758 int num_qlayers = c.get_num_layers();
759 if (num_qlayers != 1)
760 OJPH_ERROR(0x00030053, "The current implementation supports "
761 "1 quality layer only. This codestream has %d quality layers",
762 num_qlayers);
763 }
764 else if (marker_idx == 4)
765 {
767 if (coc == coc_store &&
768 num_comps * sizeof(param_cod) > sizeof(coc_store))
769 {
770 coc = new param_cod[num_comps];
771 }
774 }
775 else if (marker_idx == 5)
776 {
777 qcd.read(file);
778 received_markers |= 2;
779 }
780 else if (marker_idx == 6)
781 {
783 if (qcc == qcc_store &&
784 num_comps * sizeof(param_qcc) > sizeof(qcc_store))
785 {
786 qcc = new param_qcc[num_comps];
787 }
789 }
790 else if (marker_idx == 7)
791 skip_marker(file, "RGN", "RGN is not supported yet",
792 OJPH_MSG_LEVEL::WARN, false);
793 else if (marker_idx == 8)
794 skip_marker(file, "POC", "POC is not supported yet",
795 OJPH_MSG_LEVEL::WARN, false);
796 else if (marker_idx == 9)
797 skip_marker(file, "PPM", "PPM is not supported yet",
798 OJPH_MSG_LEVEL::WARN, false);
799 else if (marker_idx == 10)
800 //Skipping TLM marker segment; this should not cause any issues
801 skip_marker(file, "TLM", NULL, OJPH_MSG_LEVEL::NO_MSG, false);
802 else if (marker_idx == 11)
803 //Skipping PLM marker segment; this should not cause any issues
804 skip_marker(file, "PLM", NULL, OJPH_MSG_LEVEL::NO_MSG, false);
805 else if (marker_idx == 12)
806 //Skipping CRG marker segment;
807 skip_marker(file, "CRG", "CRG has been ignored; CRG is related to"
808 " where the Cb and Cr colour components are co-sited or located"
809 " with respect to the Y' luma component. Perhaps, it is better"
810 " to get the individual components and assemble the samples"
811 " according to your needs",
812 OJPH_MSG_LEVEL::INFO, false);
813 else if (marker_idx == 13)
814 skip_marker(file, "COM", NULL, OJPH_MSG_LEVEL::NO_MSG, false);
815 else if (marker_idx == 14)
816 dfs.read(file);
817 else if (marker_idx == 15)
818 atk[2].read(file);
819 else if (marker_idx == 16)
820 nlt.read(file);
821 else if (marker_idx == 17)
822 break;
823 else
824 OJPH_ERROR(0x00030051, "File ended before finding a tile segment");
825 }
826
828 for (int i = 0; i < used_coc_fields; ++i)
829 {
830 if (i == 0) cod.link_cod(coc);
831 else coc[i - 1].link_cod(coc + i);
832 coc[i].update_atk(atk);
833 }
834 siz.link(&cod);
835 if (dfs.exists())
836 siz.link(&dfs);
837
838 if (received_markers != 3)
839 OJPH_ERROR(0x00030052, "markers error, COD and QCD are required");
840
841 this->infile = file;
843 }
844
847 ui32 skipped_res_for_recon)
848 {
850 OJPH_ERROR(0x000300A1,
851 "skipped_resolution for data %d must be equal or smaller than "
852 " skipped_resolution for reconstruction %d\n",
855 OJPH_ERROR(0x000300A2,
856 "skipped_resolution for data %d must be smaller than "
857 " the number of decomposition levels %d\n",
859
860 this->skipped_res_for_read = skipped_res_for_read;
861 this->skipped_res_for_recon = skipped_res_for_recon;
863 }
864
867 {
868 if (infile != NULL)
869 OJPH_ERROR(0x000300A3, "Codestream resilience must be enabled before"
870 " reading file headers.\n");
871 this->resilient = true;
872 }
873
876 {
877 this->pre_alloc();
878 this->finalize_alloc();
879
880 while (true)
881 {
882 param_sot sot;
883 if (sot.read(infile, resilient))
884 {
885 ui64 tile_start_location = (ui64)infile->tell();
886
887 if (sot.get_tile_index() > (int)num_tiles.area())
888 {
889 if (resilient)
890 OJPH_INFO(0x00030061, "wrong tile index")
891 else
892 OJPH_ERROR(0x00030061, "wrong tile index")
893 }
894
895 if (sot.get_tile_part_index())
896 { //tile part
897 if (sot.get_num_tile_parts() &&
899 {
900 if (resilient)
901 OJPH_INFO(0x00030062,
902 "error in tile part number, should be smaller than total"
903 " number of tile parts")
904 else
905 OJPH_ERROR(0x00030062,
906 "error in tile part number, should be smaller than total"
907 " number of tile parts")
908 }
909
910 bool sod_found = false;
911 ui16 other_tile_part_markers[7] = { SOT, POC, PPT, PLT, COM,
912 NLT, SOD };
913 while (true)
914 {
915 int marker_idx = 0;
916 int result = 0;
917 marker_idx = find_marker(infile, other_tile_part_markers + 1, 6);
918 if (marker_idx == 0)
919 result = skip_marker(infile, "POC",
920 "POC marker segment in a tile is not supported yet",
922 else if (marker_idx == 1)
923 result = skip_marker(infile, "PPT",
924 "PPT marker segment in a tile is not supported yet",
926 else if (marker_idx == 2)
927 //Skipping PLT marker segment;this should not cause any issues
928 result = skip_marker(infile, "PLT", NULL,
930 else if (marker_idx == 3)
931 result = skip_marker(infile, "COM", NULL,
933 else if (marker_idx == 4)
934 result = skip_marker(infile, "NLT",
935 "NLT marker in tile is not supported yet",
937 else if (marker_idx == 5)
938 {
939 sod_found = true;
940 break;
941 }
942
943 if (marker_idx == -1) //marker not found
944 {
945 if (resilient)
946 OJPH_INFO(0x00030063,
947 "File terminated early before start of data is found"
948 " for tile indexed %d and tile part %d",
950 else
951 OJPH_ERROR(0x00030063,
952 "File terminated early before start of data is found"
953 " for tile indexed %d and tile part %d",
955 break;
956 }
957 if (result == -1) //file terminated during marker seg. skipping
958 {
959 if (resilient)
960 OJPH_INFO(0x00030064,
961 "File terminated during marker segment skipping")
962 else
963 OJPH_ERROR(0x00030064,
964 "File terminated during marker segment skipping")
965 break;
966 }
967 }
968 if (sod_found)
970 tile_start_location);
971 }
972 else
973 { //first tile part
974 bool sod_found = false;
975 ui16 first_tile_part_markers[12] = { SOT, COD, COC, QCD, QCC, RGN,
976 POC, PPT, PLT, COM, NLT, SOD };
977 while (true)
978 {
979 int marker_idx = 0;
980 int result = 0;
981 marker_idx = find_marker(infile, first_tile_part_markers+1, 11);
982 if (marker_idx == 0)
983 result = skip_marker(infile, "COD",
984 "COD marker segment in a tile is not supported yet",
986 else if (marker_idx == 1)
987 result = skip_marker(infile, "COC",
988 "COC marker segment in a tile is not supported yet",
990 else if (marker_idx == 2)
991 result = skip_marker(infile, "QCD",
992 "QCD marker segment in a tile is not supported yet",
994 else if (marker_idx == 3)
995 result = skip_marker(infile, "QCC",
996 "QCC marker segment in a tile is not supported yet",
998 else if (marker_idx == 4)
999 result = skip_marker(infile, "RGN",
1000 "RGN marker segment in a tile is not supported yet",
1002 else if (marker_idx == 5)
1003 result = skip_marker(infile, "POC",
1004 "POC marker segment in a tile is not supported yet",
1006 else if (marker_idx == 6)
1007 result = skip_marker(infile, "PPT",
1008 "PPT marker segment in a tile is not supported yet",
1010 else if (marker_idx == 7)
1011 //Skipping PLT marker segment;this should not cause any issues
1012 result = skip_marker(infile, "PLT", NULL,
1014 else if (marker_idx == 8)
1015 result = skip_marker(infile, "COM", NULL,
1017 else if (marker_idx == 9)
1018 result = skip_marker(infile, "NLT",
1019 "PPT marker segment in a tile is not supported yet",
1021 else if (marker_idx == 10)
1022 {
1023 sod_found = true;
1024 break;
1025 }
1026
1027 if (marker_idx == -1) //marker not found
1028 {
1029 if (resilient)
1030 OJPH_INFO(0x00030065,
1031 "File terminated early before start of data is found"
1032 " for tile indexed %d and tile part %d",
1034 else
1035 OJPH_ERROR(0x00030065,
1036 "File terminated early before start of data is found"
1037 " for tile indexed %d and tile part %d",
1039 break;
1040 }
1041 if (result == -1) //file terminated during marker seg. skipping
1042 {
1043 if (resilient)
1044 OJPH_INFO(0x00030066,
1045 "File terminated during marker segment skipping")
1046 else
1047 OJPH_ERROR(0x00030066,
1048 "File terminated during marker segment skipping")
1049 break;
1050 }
1051 }
1052 if (sod_found)
1054 tile_start_location);
1055 }
1056 }
1057
1058 // check the next marker; either SOT or EOC,
1059 // if something is broken, just an end of file
1060 ui16 next_markers[2] = { SOT, EOC };
1061 int marker_idx = find_marker(infile, next_markers, 2);
1062 if (marker_idx == -1)
1063 {
1064 OJPH_INFO(0x00030067, "File terminated early");
1065 break;
1066 }
1067 else if (marker_idx == 0)
1068 ;
1069 else if (marker_idx == 1)
1070 break;
1071 }
1072 }
1073
1075 void codestream::set_planar(int planar)
1076 {
1077 this->planar = planar;
1078 }
1079
1081 void codestream::set_profile(const char *s)
1082 {
1083 size_t len = strlen(s);
1084 if (len == 9 && strncmp(s, OJPH_PN_STRING_BROADCAST, 9) == 0)
1086 else if (len == 3 && strncmp(s, OJPH_PN_STRING_IMF, 3) == 0)
1088 else
1089 OJPH_ERROR(0x000300A1, "unkownn or unsupported profile");
1090 }
1091
1094 {
1095 tilepart_div = value;
1096 }
1097
1100 {
1101 need_tlm = needed;
1102 }
1103
1106 {
1107 si32 repeat = (si32)num_tiles.area();
1108 for (si32 i = 0; i < repeat; ++i)
1109 tiles[i].prepare_for_flush();
1110 if (need_tlm)
1111 { //write tlm
1112 for (si32 i = 0; i < repeat; ++i)
1113 tiles[i].fill_tlm(&tlm);
1114 tlm.write(outfile);
1115 }
1116 for (si32 i = 0; i < repeat; ++i)
1117 tiles[i].flush(outfile);
1119 if (!outfile->write(&t, 2))
1120 OJPH_ERROR(0x00030071, "Error writing to file");
1121 }
1122
1125 {
1126 if (infile)
1127 infile->close();
1128 if (outfile)
1129 outfile->close();
1130 }
1131
1134 {
1135 if (line)
1136 {
1137 bool success = false;
1138 while (!success)
1139 {
1140 success = true;
1141 for (ui32 i = 0; i < num_tiles.w; ++i)
1142 {
1143 ui32 idx = i + cur_tile_row * num_tiles.w;
1144 if ((success &= tiles[idx].push(line, cur_comp)) == false)
1145 break;
1146 }
1147 cur_tile_row += success == false ? 1 : 0;
1148 if (cur_tile_row >= num_tiles.h)
1149 cur_tile_row = 0;
1150 }
1151
1152 if (planar) //process one component at a time
1153 {
1154 if (++cur_line >= comp_size[cur_comp].h)
1155 {
1156 cur_line = 0;
1157 cur_tile_row = 0;
1158 if (++cur_comp >= num_comps)
1159 {
1160 next_component = 0;
1161 return NULL;
1162 }
1163 }
1164 }
1165 else //process all component for a line
1166 {
1167 if (++cur_comp >= num_comps)
1168 {
1169 cur_comp = 0;
1170 if (++cur_line >= comp_size[cur_comp].h)
1171 {
1172 next_component = 0;
1173 return NULL;
1174 }
1175 }
1176 }
1177 }
1178
1179 next_component = cur_comp;
1180 return this->lines + cur_comp;
1181 }
1182
1185 {
1186 bool success = false;
1187 while (!success)
1188 {
1189 success = true;
1190 for (ui32 i = 0; i < num_tiles.w; ++i)
1191 {
1192 ui32 idx = i + cur_tile_row * num_tiles.w;
1193 if ((success &= tiles[idx].pull(lines + cur_comp, cur_comp)) == false)
1194 break;
1195 }
1196 cur_tile_row += success == false ? 1 : 0;
1197 if (cur_tile_row >= num_tiles.h)
1198 cur_tile_row = 0;
1199 }
1200 comp_num = cur_comp;
1201
1202 if (planar) //process one component at a time
1203 {
1204 if (++cur_line >= recon_comp_size[cur_comp].h)
1205 {
1206 cur_line = 0;
1207 cur_tile_row = 0;
1208 if (cur_comp++ >= num_comps)
1209 {
1210 comp_num = 0;
1211 return NULL;
1212 }
1213 }
1214 }
1215 else //process all component for a line
1216 {
1217 if (++cur_comp >= num_comps)
1218 {
1219 cur_comp = 0;
1220 if (cur_line++ >= recon_comp_size[cur_comp].h)
1221 {
1222 comp_num = 0;
1223 return NULL;
1224 }
1225 }
1226 }
1227
1228 return lines + comp_num;
1229 }
1230
1231 }
1232}
virtual bool eof()=0
virtual void close()
Definition ojph_file.h:245
virtual si64 tell()=0
virtual size_t read(void *ptr, size_t size)=0
void wrap(T *buffer, size_t num_ele, ui32 pre_size)
line_buf * exchange(line_buf *line, ui32 &next_component)
void restrict_input_resolution(ui32 skipped_res_for_data, ui32 skipped_res_for_recon)
mem_elastic_allocator * elastic_alloc
mem_fixed_allocator * allocator
void write_headers(outfile_base *file, const comment_exchange *comments, ui32 num_comments)
void read_headers(infile_base *file)
line_buf * pull(ui32 &comp_num)
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
void parse_tile_header(const param_sot &sot, infile_base *file, const ui64 &tile_start_location)
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
virtual void close()
Definition ojph_file.h:93
virtual size_t write(const void *ptr, size_t size)=0
int get_progression_order() const
ui32 get_num_decompositions() const
size get_log_block_dims() const
bool is_reversible() const
size get_log_precinct_size(ui32 level_num) const
int get_num_layers() const
point get_image_extent() const
ui32 get_bit_depth(ui32 comp_num) const
point get_image_offset() const
size get_tile_size() const
point get_downsampling(ui32 comp_num) const
point get_tile_offset() const
bool is_signed(ui32 comp_num) const
ui32 get_num_components() const
static int find_marker(infile_base *f, const ui16 *char_list, int list_len)
static int skip_marker(infile_base *file, const char *marker, const char *msg, int msg_level, bool resilient)
void init_wavelet_transform_functions()
void init_colour_transform_functions()
static ui16 swap_byte(ui16 t)
const char OJPH_PN_STRING_BROADCAST[]
const char OJPH_PN_STRING_IMF[]
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
@ OJPH_PN_BROADCAST
@ OJPH_PN_UNDEFINED
int32_t si32
Definition ojph_defs.h:55
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_INT_TO_STRING(I)
Definition ojph_defs.h:61
#define ojph_div_ceil(a, b)
Definition ojph_defs.h:70
#define ojph_min(a, b)
Definition ojph_defs.h:76
#define ojph_unused(x)
Definition ojph_defs.h:78
#define OJPH_INFO(t,...)
MACROs to insert file and line number for info, warning, and error.
#define OJPH_ERROR(t,...)
#define OJPH_WARN(t,...)
#define OPENJPH_VERSION_PATCH
#define OPENJPH_VERSION_MAJOR
#define OPENJPH_VERSION_MINOR
bool read(infile_base *file)
void link(param_atk *next)
void check_validity(const param_cod &cod, const param_qcd &qcd)
void read(infile_base *file)
bool write(outfile_base *file)
void check_validity(const param_siz &siz)
bool write(outfile_base *file)
void link_cod(const param_cod *coc)
bool is_employing_color_transform() const
size get_log_precinct_size(ui32 res_num) const
void read(infile_base *file, cod_type type)
void update_atk(const param_atk *atk)
bool read(infile_base *file)
bool write(outfile_base *file) const
void read(infile_base *file)
void check_validity(param_siz &siz)
void read(infile_base *file, ui32 num_comps)
void check_validity(const param_siz &siz, const param_cod &cod)
bool write(outfile_base *file)
void read(infile_base *file)
void set_skipped_resolutions(ui32 skipped_resolutions)
ui32 get_recon_height(ui32 comp_num) const
void check_validity(const param_cod &cod)
bool write(outfile_base *file)
ui32 get_height(ui32 comp_num) const
void link(const param_cod *cod)
void read(infile_base *file)
ui32 get_width(ui32 comp_num) const
ui32 get_recon_width(ui32 comp_num) const
bool read(infile_base *file, bool resilient)
bool write(outfile_base *file)
void init(ui32 num_pairs, Ttlm_Ptlm_pair *store)
point org
Definition ojph_base.h:66
ui64 area() const
Definition ojph_base.h:53