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
49
50namespace ojph {
51
52 namespace local
53 {
54
67
70 {
71 if (allocator)
72 delete allocator;
73 if (elastic_alloc)
74 delete elastic_alloc;
75 }
76
79 {
80 tiles = NULL;
81 lines = NULL;
82 comp_size = NULL;
83 recon_comp_size = NULL;
84 outfile = NULL;
85 infile = NULL;
86
87 num_comps = 0;
89 planar = -1;
92 need_tlm = false;
93
94 cur_comp = 0;
95 cur_line = 0;
96 cur_tile_row = 0;
97 resilient = false;
99
101
102 cod.restart();
103 qcd.restart();
104 nlt.restart();
105 dfs.restart();
106 atk.restart();
107
108 allocator->restart();
109 elastic_alloc->restart();
110 }
111
114 {
120 if (num_tiles.area() > 65535)
121 OJPH_ERROR(0x00030011, "the number of tiles cannot exceed 65535");
122 if (num_tiles.area() == 0)
123 OJPH_ERROR(0x00030012, "the number of tiles cannot be 0");
124
125 //allocate tiles
126 allocator->pre_alloc_obj<tile>((size_t)num_tiles.area());
127
128 ui32 num_tileparts = 0;
129 point index;
130 rect tile_rect, recon_tile_rect;
131 ui32 ds = 1 << skipped_res_for_recon;
132 for (index.y = 0; index.y < num_tiles.h; ++index.y)
133 {
134 ui32 y0 = sz.get_tile_offset().y
135 + index.y * sz.get_tile_size().h;
136 ui32 y1 = y0 + sz.get_tile_size().h; //end of tile
137
138 tile_rect.org.y = ojph_max(y0, sz.get_image_offset().y);
139 tile_rect.siz.h =
140 ojph_min(y1, sz.get_image_extent().y) - tile_rect.org.y;
141
142 recon_tile_rect.org.y = ojph_max(ojph_div_ceil(y0, ds),
144 recon_tile_rect.siz.h = ojph_min(ojph_div_ceil(y1, ds),
146 - recon_tile_rect.org.y;
147
148 for (index.x = 0; index.x < num_tiles.w; ++index.x)
149 {
150 ui32 x0 = sz.get_tile_offset().x
151 + index.x * sz.get_tile_size().w;
152 ui32 x1 = x0 + sz.get_tile_size().w;
153
154 tile_rect.org.x = ojph_max(x0, sz.get_image_offset().x);
155 tile_rect.siz.w =
156 ojph_min(x1, sz.get_image_extent().x) - tile_rect.org.x;
157
158 recon_tile_rect.org.x = ojph_max(ojph_div_ceil(x0, ds),
160 recon_tile_rect.siz.w = ojph_min(ojph_div_ceil(x1, ds),
162 - recon_tile_rect.org.x;
163
164 ui32 tps = 0; // number of tileparts for this tile
165 tile::pre_alloc(this, tile_rect, recon_tile_rect, tps);
166 num_tileparts += tps;
167 }
168 }
169
170 //allocate lines
171 //These lines are used by codestream to exchange data with external
172 // world
174 allocator->pre_alloc_obj<line_buf>(num_comps);
175 allocator->pre_alloc_obj<size>(num_comps); //for *comp_size
176 allocator->pre_alloc_obj<size>(num_comps); //for *recon_comp_size
177 for (ui32 i = 0; i < num_comps; ++i)
178 allocator->pre_alloc_data<si32>(siz.get_recon_width(i), 0);
179
180 //allocate tlm
181 if (outfile != NULL && need_tlm)
182 allocator->pre_alloc_obj<param_tlm::Ttlm_Ptlm_pair>(num_tileparts);
183
184 //precinct scratch buffer
185 // The precinct scratch is shared by all components, but each component
186 // may override the codeblock/precinct geometry via a COC marker. The
187 // per-component tag-tree storage (resolution.cpp) is derived from that
188 // component's effective params, so size the shared buffer from the
189 // largest ratio across every component (the main COD and all COC
190 // overrides). Sizing from the COD alone under-reserves the buffer for
191 // any component whose COC declares a smaller codeblock than the COD.
192 size ratio;
193 for (ui32 c = 0; c < num_comps; ++c)
194 {
195 const param_cod* cdp = cod.get_coc(c);
196 ui32 num_decomps = cdp->get_num_decompositions();
197 size log_cb = cdp->get_log_block_dims();
198 for (ui32 r = 0; r <= num_decomps; ++r)
199 {
200 size log_PP = cdp->get_log_precinct_size(r);
201 ratio.w = ojph_max(ratio.w, log_PP.w - ojph_min(log_cb.w, log_PP.w));
202 ratio.h = ojph_max(ratio.h, log_PP.h - ojph_min(log_cb.h, log_PP.h));
203 }
204 }
205 ui32 max_ratio = ojph_max(ratio.w, ratio.h);
206 max_ratio = 1 << max_ratio;
207 // assuming that we have a hierarchy of n levels.
208 // This needs 4/3 times the area, rounded up
209 // (rounding up leaves one extra entry).
210 // This exta entry is necessary
211 // We need 4 such tables. These tables store
212 // 1. missing msbs and 2. their flags,
213 // 3. number of layers and 4. their flags
215 4 * ((max_ratio * max_ratio * 4 + 2) / 3);
216
218 }
219
222 {
223 allocator->alloc();
224
225 //precinct scratch buffer
228
229 //get tiles
230 tiles = this->allocator->post_alloc_obj<tile>((size_t)num_tiles.area());
231
232 ui32 num_tileparts = 0;
233 point index;
234 rect tile_rect;
236 for (index.y = 0; index.y < num_tiles.h; ++index.y)
237 {
238 ui32 y0 = sz.get_tile_offset().y
239 + index.y * sz.get_tile_size().h;
240 ui32 y1 = y0 + sz.get_tile_size().h; //end of tile
241
242 tile_rect.org.y = ojph_max(y0, sz.get_image_offset().y);
243 tile_rect.siz.h =
244 ojph_min(y1, sz.get_image_extent().y) - tile_rect.org.y;
245
246 ui32 offset = 0;
247 for (index.x = 0; index.x < num_tiles.w; ++index.x)
248 {
249 ui32 x0 = sz.get_tile_offset().x
250 + index.x * sz.get_tile_size().w;
251 ui32 x1 = x0 + sz.get_tile_size().w;
252
253 tile_rect.org.x = ojph_max(x0, sz.get_image_offset().x);
254 tile_rect.siz.w =
255 ojph_min(x1, sz.get_image_extent().x) - tile_rect.org.x;
256
257 ui32 tps = 0; // number of tileparts for this tile
258 ui32 idx = index.y * num_tiles.w + index.x;
259 tiles[idx].finalize_alloc(this, tile_rect, idx, offset, tps);
260 num_tileparts += tps;
261 }
262 }
263
264 //allocate lines
265 //These lines are used by codestream to exchange data with external
266 // world
267 this->num_comps = sz.get_num_components();
268 lines = allocator->post_alloc_obj<line_buf>(this->num_comps);
269 comp_size = allocator->post_alloc_obj<size>(this->num_comps);
270 recon_comp_size = allocator->post_alloc_obj<size>(this->num_comps);
271 employ_color_transform = cod.is_employing_color_transform();
272 for (ui32 i = 0; i < this->num_comps; ++i)
273 {
274 comp_size[i].w = siz.get_width(i);
275 comp_size[i].h = siz.get_height(i);
276 ui32 cw = siz.get_recon_width(i);
277 recon_comp_size[i].w = cw;
278 recon_comp_size[i].h = siz.get_recon_height(i);
279 lines[i].wrap(allocator->post_alloc_data<si32>(cw, 0), cw, 0);
280 }
281
282 cur_comp = 0;
283 cur_line = 0;
284
285 //allocate tlm
286 if (outfile != NULL && need_tlm)
287 tlm.init(num_tileparts,
288 allocator->post_alloc_obj<param_tlm::Ttlm_Ptlm_pair>(num_tileparts));
289 }
290
291
294 {
295 //two possibilities lossy single tile or lossless
296 //For the following code, we use the least strict profile
297 ojph::param_siz sz(&siz);
298 ojph::param_cod cd(&cod);
299 bool reversible = cd.is_reversible();
300 bool imf2k = !reversible, imf4k = !reversible, imf8k = !reversible;
301 bool imf2kls = reversible, imf4kls = reversible, imf8kls = reversible;
302
303 if (reversible)
304 {
305 point ext = sz.get_image_extent();
306 if (ext.x <= 2048 && ext.y <= 1556)
307 imf2kls &= true;
308 if (ext.x <= 4096 && ext.y <= 3112)
309 imf4kls &= true;
310 if (ext.x <= 8192 && ext.y <= 6224)
311 imf8kls &= true;
312
313 if (!imf2kls && !imf4kls && !imf8kls)
314 OJPH_ERROR(0x000300C1,
315 "Image dimensions do not meet any of the lossless IMF profiles");
316 }
317 else
318 {
319 point ext = sz.get_image_extent();
320 if (ext.x <= 2048 && ext.y <= 1556)
321 imf2k &= true;
322 if (ext.x <= 4096 && ext.y <= 3112)
323 imf4k &= true;
324 if (ext.x <= 8192 && ext.y <= 6224)
325 imf8k &= true;
326
327 if (!imf2k && !imf4k && !imf8k)
328 OJPH_ERROR(0x000300C2,
329 "Image dimensions do not meet any of the lossy IMF profiles");
330 }
331
332
333 if (sz.get_image_offset().x != 0 || sz.get_image_offset().y != 0)
334 OJPH_ERROR(0x000300C3,
335 "For IMF profile, image offset (XOsiz, YOsiz) has to be 0.");
336 if (sz.get_tile_offset().x != 0 || sz.get_tile_offset().y != 0)
337 OJPH_ERROR(0x000300C4,
338 "For IMF profile, tile offset (XTOsiz, YTOsiz) has to be 0.");
339 if (sz.get_num_components() > 3)
340 OJPH_ERROR(0x000300C5,
341 "For IMF profile, the number of components has to be less "
342 " or equal to 3");
343 bool test_ds1 = true, test_ds2 = true;
344 for (ojph::ui32 i = 0; i < sz.get_num_components(); ++i)
345 {
346 point downsamping = sz.get_downsampling(i);
347 test_ds1 &= downsamping.y == 1;
348 test_ds2 &= downsamping.y == 1;
349
350 test_ds1 &= downsamping.x == 1;
351 if (i == 1 || i == 2)
352 test_ds2 &= downsamping.x == 2;
353 else
354 test_ds2 &= downsamping.x == 1;
355 }
356 if (!test_ds1 && !test_ds2)
357 OJPH_ERROR(0x000300C6,
358 "For IMF profile, either no component downsampling is used,"
359 " or the x-dimension of the 2nd and 3rd components is downsampled"
360 " by 2.");
361
362 bool test_bd = true;
363 for (ojph::ui32 i = 0; i < sz.get_num_components(); ++i)
364 {
365 ui32 bit_depth = sz.get_bit_depth(i);
366 bool is_signed = sz.is_signed(i);
367 test_bd &= bit_depth >= 8 && bit_depth <= 16 && is_signed == false;
368 }
369 if (!test_bd)
370 OJPH_ERROR(0x000300C7,
371 "For IMF profile, compnent bit_depth has to be between"
372 " 8 and 16 bits inclusively, and the samples must be unsigned");
373
374 if (cd.get_log_block_dims().w != 5 || cd.get_log_block_dims().h != 5)
375 OJPH_ERROR(0x000300C8,
376 "For IMF profile, codeblock dimensions are restricted."
377 " Use \"-block_size {32,32}\" at the commandline");
378
379 ui32 num_decomps = cd.get_num_decompositions();
380 bool test_pz = cd.get_log_precinct_size(0).w == 7
381 && cd.get_log_precinct_size(0).h == 7;
382 for (ui32 i = 1; i <= num_decomps; ++i)
383 test_pz = cd.get_log_precinct_size(i).w == 8
384 && cd.get_log_precinct_size(i).h == 8;
385 if (!test_pz)
386 OJPH_ERROR(0x000300C9,
387 "For IMF profile, precinct sizes are restricted."
388 " Use \"-precincts {128,128},{256,256}\" at the commandline");
389
391 OJPH_ERROR(0x000300CA,
392 "For IMF profile, the CPRL progression order must be used."
393 " Use \"-prog_order CPRL\".");
394
395 imf2k &= num_decomps <= 5;
396 imf2kls &= num_decomps <= 5;
397 imf4k &= num_decomps <= 6;
398 imf4kls &= num_decomps <= 6;
399 imf8k &= num_decomps <= 7;
400 imf8kls &= num_decomps <= 7;
401
402 if (num_decomps == 0 ||
403 (!imf2k && !imf4k && !imf8k && !imf2kls && !imf4kls && !imf8kls))
404 OJPH_ERROR(0x000300CB,
405 "Number of decompositions does not match the IMF profile"
406 " dictated by wavelet reversibility and image dimensions.");
407
408 ui32 tiles_w = sz.get_image_extent().x;
409 tiles_w = ojph_div_ceil(tiles_w, sz.get_tile_size().w);
410 ui32 tiles_h = sz.get_image_extent().y;
411 tiles_h = ojph_div_ceil(tiles_h, sz.get_tile_size().h);
412 ui32 total_tiles = tiles_w * tiles_h;
413
414 if (total_tiles > 1)
415 {
416 if (!reversible)
417 OJPH_ERROR(0x000300CC,
418 "Lossy IMF profile must have one tile.");
419
420 size tt = sz.get_tile_size();
421 imf2kls &= (tt.w == 1024 && tt.h == 1024);
422 imf2kls &= (tt.w >= 1024 && num_decomps <= 4)
423 || (tt.w >= 2048 && num_decomps <= 5);
424 imf4kls &= (tt.w == 1024 && tt.h == 1024)
425 || (tt.w == 2048 && tt.h == 2048);
426 imf4kls &= (tt.w >= 1024 && num_decomps <= 4)
427 || (tt.w >= 2048 && num_decomps <= 5)
428 || (tt.w >= 4096 && num_decomps <= 6);
429 imf8kls &= (tt.w == 1024 && tt.h == 1024)
430 || (tt.w == 2048 && tt.h == 2048)
431 || (tt.w == 4096 && tt.h == 4096);
432 imf8kls &= (tt.w >= 1024 && num_decomps <= 4)
433 || (tt.w >= 2048 && num_decomps <= 5)
434 || (tt.w >= 4096 && num_decomps <= 6)
435 || (tt.w >= 8192 && num_decomps <= 7);
436 if (!imf2kls && !imf4kls && !imf8kls)
437 OJPH_ERROR(0x000300CD,
438 "Number of decompositions does not match the IMF profile"
439 " dictated by wavelet reversibility and image dimensions and"
440 " tiles.");
441 }
442
443 need_tlm = true;
446 {
448 OJPH_WARN(0x000300C1,
449 "In IMF profile, tile part divisions at the component level must be "
450 "employed, while at the resolution level is not allowed. "
451 "This has been corrected.");
452 }
453 }
454
457 {
458 ojph::param_siz sz(&siz);
459 ojph::param_cod cd(&cod);
460
461 if (sz.get_image_offset().x != 0 || sz.get_image_offset().y != 0)
462 OJPH_ERROR(0x000300B1,
463 "For broadcast profile, image offset (XOsiz, YOsiz) has to be 0.");
464 if (sz.get_tile_offset().x != 0 || sz.get_tile_offset().y != 0)
465 OJPH_ERROR(0x000300B2,
466 "For broadcast profile, tile offset (XTOsiz, YTOsiz) has to be 0.");
467 if (sz.get_num_components() > 4)
468 OJPH_ERROR(0x000300B3,
469 "For broadcast profile, the number of components has to be less "
470 " or equal to 4");
471 bool test_ds1 = true, test_ds2 = true;
472 for (ojph::ui32 i = 0; i < sz.get_num_components(); ++i)
473 {
474 point downsamping = sz.get_downsampling(i);
475 test_ds1 &= downsamping.y == 1;
476 test_ds2 &= downsamping.y == 1;
477
478 test_ds1 &= downsamping.x == 1;
479 if (i == 1 || i == 2)
480 test_ds2 &= downsamping.x == 2;
481 else
482 test_ds2 &= downsamping.x == 1;
483 }
484 if (!test_ds1 && !test_ds2)
485 OJPH_ERROR(0x000300B4,
486 "For broadcast profile, either no component downsampling is used,"
487 " or the x-dimension of the 2nd and 3rd components is downsampled"
488 " by 2.");
489
490 bool test_bd = true;
491 for (ojph::ui32 i = 0; i < sz.get_num_components(); ++i)
492 {
493 ui32 bit_depth = sz.get_bit_depth(i);
494 bool is_signed = sz.is_signed(i);
495 test_bd &= bit_depth >= 8 && bit_depth <= 12 && is_signed == false;
496 }
497 if (!test_bd)
498 OJPH_ERROR(0x000300B5,
499 "For broadcast profile, compnent bit_depth has to be between"
500 " 8 and 12 bits inclusively, and the samples must be unsigned");
501
502 ui32 num_decomps = cd.get_num_decompositions();
503 if (num_decomps == 0 || num_decomps > 5)
504 OJPH_ERROR(0x000300B6,
505 "For broadcast profile, number of decompositions has to be between"
506 "1 and 5 inclusively.");
507
508 if (cd.get_log_block_dims().w < 5 || cd.get_log_block_dims().w > 7)
509 OJPH_ERROR(0x000300B7,
510 "For broadcast profile, codeblock dimensions are restricted such"
511 " that codeblock width has to be either 32, 64, or 128.");
512
513 if (cd.get_log_block_dims().h < 5 || cd.get_log_block_dims().h > 7)
514 OJPH_ERROR(0x000300B8,
515 "For broadcast profile, codeblock dimensions are restricted such"
516 " that codeblock height has to be either 32, 64, or 128.");
517
518 bool test_pz = cd.get_log_precinct_size(0).w == 7
519 && cd.get_log_precinct_size(0).h == 7;
520 for (ui32 i = 1; i <= num_decomps; ++i)
521 test_pz = cd.get_log_precinct_size(i).w == 8
522 && cd.get_log_precinct_size(i).h == 8;
523 if (!test_pz)
524 OJPH_ERROR(0x000300B9,
525 "For broadcast profile, precinct sizes are restricted."
526 " Use \"-precincts {128,128},{256,256}\" at the commandline");
527
529 OJPH_ERROR(0x000300BA,
530 "For broadcast profile, the CPRL progression order must be used."
531 " Use \"-prog_order CPRL\".");
532
533 ui32 tiles_w = sz.get_image_extent().x;
534 tiles_w = ojph_div_ceil(tiles_w, sz.get_tile_size().w);
535 ui32 tiles_h = sz.get_image_extent().y;
536 tiles_h = ojph_div_ceil(tiles_h, sz.get_tile_size().h);
537 ui32 total_tiles = tiles_w * tiles_h;
538
539 if (total_tiles != 1 && total_tiles != 4)
540 OJPH_ERROR(0x000300BB,
541 "The broadcast profile can only have 1 or 4 tiles");
542
543 need_tlm = true;
546 {
548 OJPH_WARN(0x000300B1,
549 "In BROADCAST profile, tile part divisions at the component level "
550 "must be employed, while at the resolution level is not allowed. "
551 "This has been corrected.");
552 }
553 }
554
557 const comment_exchange* comments,
558 ui32 num_comments)
559 {
560 //finalize
561 siz.set_cod(cod);
562 // set the tile size if it was not set by the user
563 size tile_size = siz.get_tile_size();
564 if (tile_size.h == 0 && tile_size.w == 0)
565 {
566 point img_offset = siz.get_image_offset();
567 point img_extent = siz.get_image_extent();
568 size t(img_extent.x + img_offset.x, img_extent.y + img_offset.y);
569 siz.set_tile_size(t);
570 }
571 siz.check_validity();
572 cod.check_validity(siz);
573 cod.update_atk(&atk);
574 qcd.check_validity(siz, cod);
575 cap.check_validity(cod, qcd);
576 nlt.check_validity(siz);
577 if (profile == OJPH_PN_IMF)
579 else if (profile == OJPH_PN_BROADCAST)
581
583 if ((po == OJPH_PO_LRCP || po == OJPH_PO_RLCP) &&
585 {
587 OJPH_INFO(0x00030021,
588 "For LRCP and RLCP progression orders, tilepart divisions at the "
589 "component level, means that we have a tilepart for every "
590 "resolution and component.\n");
591 }
593 {
595 OJPH_WARN(0x00030021,
596 "For RPCL progression, having tilepart divisions at the component "
597 "level means a tilepart for every precinct, which does not "
598 "make sense, since we can have no more than 255 tile parts. This "
599 "has been corrected by removing tilepart divisions at the component "
600 "level.");
601 }
602 if (po == OJPH_PO_PCRL && tilepart_div != 0)
603 {
604 tilepart_div = 0;
605 OJPH_WARN(0x00030022,
606 "For PCRL progression, having tilepart divisions at the component "
607 "level or the resolution level means a tile part for every "
608 "precinct, which does not make sense, since we can have no more "
609 "than 255 tile parts. This has been corrected by removing tilepart "
610 "divisions; use another progression if you want tileparts.");
611 }
613 {
615 OJPH_WARN(0x00030023,
616 "For CPRL progression, having tilepart divisions at the resolution "
617 "level means a tile part for every precinct, which does not "
618 "make sense, since we can have no more than 255 tile parts. This "
619 "has been corrected by removing tilepart divisions at the "
620 "resolution level.");
621 }
622
623 if (planar == -1) //not initialized
624 planar = cod.is_employing_color_transform() ? 1 : 0;
625 else if (planar == 0) //interleaved is chosen
626 {
627 }
628 else if (planar == 1) //plannar is chosen
629 {
630 if (cod.is_employing_color_transform() == true)
631 OJPH_ERROR(0x00030021,
632 "the planar interface option cannot be used when colour "
633 "transform is employed");
634 }
635 else
636 assert(0);
637
638 assert(this->outfile == NULL);
639 this->outfile = file;
640 this->pre_alloc();
641 this->finalize_alloc();
642
644 if (file->write(&t, 2) != 2)
645 OJPH_ERROR(0x00030022, "Error writing to file");
646
647 if (!siz.write(file))
648 OJPH_ERROR(0x00030023, "Error writing to file");
649
650 if (!cap.write(file))
651 OJPH_ERROR(0x00030024, "Error writing to file");
652
653 if (!cod.write(file))
654 OJPH_ERROR(0x00030025, "Error writing to file");
655
656 if (!cod.write_coc(file, num_comps))
657 OJPH_ERROR(0x0003002E, "Error writing to file");
658
659 if (!qcd.write(file))
660 OJPH_ERROR(0x00030026, "Error writing to file");
661
662 if (!qcd.write_qcc(file, num_comps))
663 OJPH_ERROR(0x0003002D, "Error writing to file");
664
665 if (!nlt.write(file))
666 OJPH_ERROR(0x00030027, "Error writing to file");
667
668 char buf[] = " OpenJPH Ver "
672 size_t len = strlen(buf);
674 *(ui16*)(buf + 2) = swap_bytes_if_le((ui16)(len - 2));
675 //1 for General use (IS 8859-15:1999 (Latin) values)
676 *(ui16*)(buf + 4) = swap_bytes_if_le((ui16)(1));
677 if (file->write(buf, len) != len)
678 OJPH_ERROR(0x00030028, "Error writing to file");
679
680 if (comments != NULL) {
681 for (ui32 i = 0; i < num_comments; ++i)
682 {
684 if (file->write(&t, 2) != 2)
685 OJPH_ERROR(0x00030029, "Error writing to file");
686 t = swap_bytes_if_le((ui16)(comments[i].len + 4));
687 if (file->write(&t, 2) != 2)
688 OJPH_ERROR(0x0003002A, "Error writing to file");
689 //1 for General use (IS 8859-15:1999 (Latin) values)
690 t = swap_bytes_if_le(comments[i].Rcom);
691 if (file->write(&t, 2) != 2)
692 OJPH_ERROR(0x0003002B, "Error writing to file");
693 if (file->write(comments[i].data, comments[i].len)!=comments[i].len)
694 OJPH_ERROR(0x0003002C, "Error writing to file");
695 }
696 }
697 }
698
700 static
701 int find_marker(infile_base *f, const ui16* char_list, int list_len)
702 {
703 //returns the marker index in char_list, or -1
704 while (!f->eof())
705 {
706 ui8 new_char;
707 size_t num_bytes = f->read(&new_char, 1);
708 if (num_bytes != 1)
709 return -1;
710 if (new_char == 0xFF)
711 {
712 size_t num_bytes = f->read(&new_char, 1);
713
714 if (num_bytes != 1)
715 return -1;
716
717 for (int i = 0; i < list_len; ++i)
718 if (new_char == (char_list[i] & 0xFF))
719 return i;
720 }
721 }
722 return -1;
723 }
724
726 static
727 int skip_marker(infile_base *file, const char *marker,
728 const char *msg, int msg_level, bool resilient)
729 {
730 ojph_unused(marker);
731 ui16 com_len;
732 if (file->read(&com_len, 2) != 2)
733 {
734 if (resilient)
735 return -1;
736 else
737 OJPH_ERROR(0x00030041, "error reading marker");
738 }
739 com_len = swap_bytes_if_le(com_len);
740 file->seek(com_len - 2, infile_base::OJPH_SEEK_CUR);
741 if (msg != NULL && msg_level != OJPH_MSG_NO_MSG)
742 {
743 if (msg_level == OJPH_MSG_INFO)
744 {
745 OJPH_INFO(0x00030001, "%s", msg);
746 }
747 else if (msg_level == OJPH_MSG_WARN)
748 {
749 OJPH_WARN(0x00030001, "%s", msg);
750 }
751 else if (msg_level == OJPH_MSG_ERROR)
752 {
753 OJPH_ERROR(0x00030001, "%s", msg);
754 }
755 else // there is the option of ALL_MSG but it should not be used here
756 assert(0);
757 }
758 return 0;
759 }
760
763 {
764 ui16 marker_list[20] = { SOC, SIZ, CAP, PRF, CPF, COD, COC, QCD, QCC,
765 RGN, POC, PPM, TLM, PLM, CRG, COM, DFS, ATK, NLT, SOT };
766 find_marker(file, marker_list, 1); //find SOC
767 find_marker(file, marker_list + 1, 1); //find SIZ
768 siz.read(file);
769 int marker_idx = 0;
770 int received_markers = 0; //check that COD, & QCD received
771 while (true)
772 {
773 marker_idx = find_marker(file, marker_list + 2, 18);
774 if (marker_idx == 0)
775 cap.read(file);
776 else if (marker_idx == 1)
777 //Skipping PRF marker segment; this should not cause any issues
778 skip_marker(file, "PRF", NULL, OJPH_MSG_NO_MSG, false);
779 else if (marker_idx == 2)
780 //Skipping CPF marker segment; this should not cause any issues
781 skip_marker(file, "CPF", NULL, OJPH_MSG_NO_MSG, false);
782 else if (marker_idx == 3)
783 {
784 cod.read(file);
785 received_markers |= 1;
787 int num_qlayers = c.get_num_layers();
788 if (num_qlayers != 1)
789 OJPH_ERROR(0x00030053, "The current implementation supports "
790 "1 quality layer only. This codestream has %d quality layers",
791 num_qlayers);
792 }
793 else if (marker_idx == 4)
794 {
795 param_cod* p = cod.add_coc_object(param_cod::OJPH_COD_UNKNOWN);
796 p->read_coc(file, siz.get_num_components(), &cod);
797 if (p->get_comp_idx() >= siz.get_num_components())
798 OJPH_INFO(0x00030056, "The codestream carries a COC marker "
799 "segment for a component indexed by %d, which is more than the "
800 "allowed index number, since the codestream has %d components",
801 p->get_comp_idx(), num_comps);
802 param_cod *q = cod.get_coc(p->get_comp_idx());
803 if (p != q && p->get_comp_idx() == q->get_comp_idx())
804 OJPH_ERROR(0x00030057, "The codestream has two COC marker "
805 "segments for one component of index %d", p->get_comp_idx());
806 }
807 else if (marker_idx == 5)
808 {
809 qcd.read(file);
810 received_markers |= 2;
811 }
812 else if (marker_idx == 6)
813 {
814 param_qcd* p = qcd.add_qcc_object(param_qcd::OJPH_QCD_UNKNOWN);
815 p->read_qcc(file, siz.get_num_components());
816 if (p->get_comp_idx() >= siz.get_num_components())
817 OJPH_ERROR(0x00030054, "The codestream carries a QCC marker "
818 "segment for a component indexed by %d, which is more than the "
819 "allowed index number, since the codestream has %d components",
820 p->get_comp_idx(), num_comps);
821 param_qcd *q = qcd.get_qcc(p->get_comp_idx());
822 if (p != q && p->get_comp_idx() == q->get_comp_idx())
823 OJPH_ERROR(0x00030055, "The codestream has two QCC marker "
824 "segments for one component of index %d", p->get_comp_idx());
825 }
826 else if (marker_idx == 7)
827 skip_marker(file, "RGN", "RGN is not supported yet",
828 OJPH_MSG_WARN, false);
829 else if (marker_idx == 8)
830 skip_marker(file, "POC", "POC is not supported yet",
831 OJPH_MSG_WARN, false);
832 else if (marker_idx == 9)
833 skip_marker(file, "PPM", "PPM is not supported yet",
834 OJPH_MSG_WARN, false);
835 else if (marker_idx == 10)
836 //Skipping TLM marker segment; this should not cause any issues
837 skip_marker(file, "TLM", NULL, OJPH_MSG_NO_MSG, false);
838 else if (marker_idx == 11)
839 //Skipping PLM marker segment; this should not cause any issues
840 skip_marker(file, "PLM", NULL, OJPH_MSG_NO_MSG, false);
841 else if (marker_idx == 12)
842 //Skipping CRG marker segment;
843 skip_marker(file, "CRG", "CRG has been ignored; CRG is related to"
844 " where the Cb and Cr colour components are co-sited or located"
845 " with respect to the Y' luma component. Perhaps, it is better"
846 " to get the individual components and assemble the samples"
847 " according to your needs",
848 OJPH_MSG_INFO, false);
849 else if (marker_idx == 13)
850 skip_marker(file, "COM", NULL, OJPH_MSG_NO_MSG, false);
851 else if (marker_idx == 14)
852 dfs.read(file);
853 else if (marker_idx == 15)
854 atk.read(file);
855 else if (marker_idx == 16)
856 nlt.read(file);
857 else if (marker_idx == 17)
858 break;
859 else
860 OJPH_ERROR(0x00030051, "File ended before finding a tile segment");
861 }
862
863 cod.update_atk(&atk);
864 siz.link(&cod);
865 if (dfs.exists())
866 siz.link(&dfs);
867
868 if (received_markers != 3)
869 OJPH_ERROR(0x00030052, "markers error, COD and QCD are required");
870
871 this->infile = file;
872 planar = cod.is_employing_color_transform() ? 0 : 1;
873 }
874
878 {
880 OJPH_ERROR(0x000300A1,
881 "skipped_resolution for data %d must be equal or smaller than "
882 " skipped_resolution for reconstruction %d\n",
884 if (skipped_res_for_read > cod.get_num_decompositions())
885 OJPH_ERROR(0x000300A2,
886 "skipped_resolution for data %d must be smaller than "
887 " the number of decomposition levels %d\n",
888 skipped_res_for_read, cod.get_num_decompositions());
889
890 this->skipped_res_for_read = skipped_res_for_read;
891 this->skipped_res_for_recon = skipped_res_for_recon;
892 siz.set_skipped_resolutions(skipped_res_for_recon);
893 }
894
897 {
898 if (infile != NULL)
899 OJPH_ERROR(0x000300A3, "Codestream resilience must be enabled before"
900 " reading file headers.\n");
901 this->resilient = true;
902 }
903
906 {
907 this->pre_alloc();
908 this->finalize_alloc();
909
910 while (true)
911 {
912 param_sot sot;
913 if (sot.read(infile, resilient))
914 {
915 ui64 tile_start_location = (ui64)infile->tell();
916 bool skip_tile = false;
917
918 if (sot.get_tile_index() >= (int)num_tiles.area())
919 {
920 if (resilient) {
921 OJPH_INFO(0x00030061, "wrong tile index")
922 skip_tile = true; // skip the faulty tile
923 }
924 else
925 OJPH_ERROR(0x00030061, "wrong tile index")
926 }
927
928 if (!skip_tile)
929 {
930 if (sot.get_tile_part_index())
931 { //tile part
932 if (sot.get_num_tile_parts() &&
934 {
935 if (resilient)
936 OJPH_INFO(0x00030062,
937 "error in tile part number, should be smaller than total"
938 " number of tile parts")
939 else
940 OJPH_ERROR(0x00030062,
941 "error in tile part number, should be smaller than total"
942 " number of tile parts")
943 }
944
945 bool sod_found = false;
946 ui16 other_tile_part_markers[7] = { SOT, POC, PPT, PLT, COM,
947 NLT, SOD };
948 while (true)
949 {
950 int marker_idx = 0;
951 int result = 0;
952 marker_idx = find_marker(infile, other_tile_part_markers+1, 6);
953 if (marker_idx == 0)
954 result = skip_marker(infile, "POC",
955 "POC marker segment in a tile is not supported yet",
957 else if (marker_idx == 1)
958 result = skip_marker(infile, "PPT",
959 "PPT marker segment in a tile is not supported yet",
961 else if (marker_idx == 2)
962 //Skipping PLT marker segment;this should not cause any issues
963 result = skip_marker(infile, "PLT", NULL,
965 else if (marker_idx == 3)
966 result = skip_marker(infile, "COM", NULL,
968 else if (marker_idx == 4)
969 result = skip_marker(infile, "NLT",
970 "NLT marker in tile is not supported yet",
972 else if (marker_idx == 5)
973 {
974 sod_found = true;
975 break;
976 }
977
978 if (marker_idx == -1) //marker not found
979 {
980 if (resilient)
981 OJPH_INFO(0x00030063,
982 "File terminated early before start of data is found"
983 " for tile indexed %d and tile part %d",
985 else
986 OJPH_ERROR(0x00030063,
987 "File terminated early before start of data is found"
988 " for tile indexed %d and tile part %d",
990 break;
991 }
992 if (result == -1) //file terminated during marker seg. skipping
993 {
994 if (resilient)
995 OJPH_INFO(0x00030064,
996 "File terminated during marker segment skipping")
997 else
998 OJPH_ERROR(0x00030064,
999 "File terminated during marker segment skipping")
1000 break;
1001 }
1002 }
1003 if (sod_found)
1004 tiles[sot.get_tile_index()].parse_tile_header(sot, infile,
1005 tile_start_location);
1006 }
1007 else
1008 { //first tile part
1009 bool sod_found = false;
1010 ui16 first_tile_part_markers[12] = { SOT, COD, COC, QCD, QCC, RGN,
1011 POC, PPT, PLT, COM, NLT, SOD };
1012 while (true)
1013 {
1014 int marker_idx = 0;
1015 int result = 0;
1016 marker_idx = find_marker(infile, first_tile_part_markers+1, 11);
1017 if (marker_idx == 0)
1018 result = skip_marker(infile, "COD",
1019 "COD marker segment in a tile is not supported yet",
1021 else if (marker_idx == 1)
1022 result = skip_marker(infile, "COC",
1023 "COC marker segment in a tile is not supported yet",
1025 else if (marker_idx == 2)
1026 result = skip_marker(infile, "QCD",
1027 "QCD marker segment in a tile is not supported yet",
1029 else if (marker_idx == 3)
1030 result = skip_marker(infile, "QCC",
1031 "QCC marker segment in a tile is not supported yet",
1033 else if (marker_idx == 4)
1034 result = skip_marker(infile, "RGN",
1035 "RGN marker segment in a tile is not supported yet",
1037 else if (marker_idx == 5)
1038 result = skip_marker(infile, "POC",
1039 "POC marker segment in a tile is not supported yet",
1041 else if (marker_idx == 6)
1042 result = skip_marker(infile, "PPT",
1043 "PPT marker segment in a tile is not supported yet",
1045 else if (marker_idx == 7)
1046 //Skipping PLT marker segment;this should not cause any issues
1047 result = skip_marker(infile, "PLT", NULL,
1049 else if (marker_idx == 8)
1050 result = skip_marker(infile, "COM", NULL,
1052 else if (marker_idx == 9)
1053 result = skip_marker(infile, "NLT",
1054 "PPT marker segment in a tile is not supported yet",
1056 else if (marker_idx == 10)
1057 {
1058 sod_found = true;
1059 break;
1060 }
1061
1062 if (marker_idx == -1) //marker not found
1063 {
1064 if (resilient)
1065 OJPH_INFO(0x00030065,
1066 "File terminated early before start of data is found"
1067 " for tile indexed %d and tile part %d",
1069 else
1070 OJPH_ERROR(0x00030065,
1071 "File terminated early before start of data is found"
1072 " for tile indexed %d and tile part %d",
1074 break;
1075 }
1076 if (result == -1) //file terminated during marker seg. skipping
1077 {
1078 if (resilient)
1079 OJPH_INFO(0x00030066,
1080 "File terminated during marker segment skipping")
1081 else
1082 OJPH_ERROR(0x00030066,
1083 "File terminated during marker segment skipping")
1084 break;
1085 }
1086 }
1087 if (sod_found)
1088 tiles[sot.get_tile_index()].parse_tile_header(sot, infile,
1089 tile_start_location);
1090 }
1091 }
1092 }
1093
1094 // check the next marker; either SOT or EOC,
1095 // if something is broken, just an end of file
1096 ui16 next_markers[2] = { SOT, EOC };
1097 int marker_idx = find_marker(infile, next_markers, 2);
1098 if (marker_idx == -1)
1099 {
1100 OJPH_INFO(0x00030067, "File terminated early");
1101 break;
1102 }
1103 else if (marker_idx == 0)
1104 ;
1105 else if (marker_idx == 1)
1106 break;
1107 }
1108 }
1109
1112 {
1113 this->planar = planar;
1114 }
1115
1117 void codestream::set_profile(const char *s)
1118 {
1119 size_t len = strlen(s);
1120 if (len == 9 && strncmp(s, OJPH_PN_STRING_BROADCAST, 9) == 0)
1122 else if (len == 3 && strncmp(s, OJPH_PN_STRING_IMF, 3) == 0)
1124 else
1125 OJPH_ERROR(0x000300A1, "unkownn or unsupported profile");
1126 }
1127
1130 {
1131 tilepart_div = value;
1132 }
1133
1136 {
1137 need_tlm = needed;
1138 }
1139
1142 {
1143 si32 repeat = (si32)num_tiles.area();
1144 for (si32 i = 0; i < repeat; ++i)
1145 tiles[i].prepare_for_flush();
1146 if (need_tlm)
1147 { //write tlm
1148 for (si32 i = 0; i < repeat; ++i)
1149 tiles[i].fill_tlm(&tlm);
1150 tlm.write(outfile);
1151 }
1152 for (si32 i = 0; i < repeat; ++i)
1153 tiles[i].flush(outfile);
1155 if (!outfile->write(&t, 2))
1156 OJPH_ERROR(0x00030071, "Error writing to file");
1157 }
1158
1161 {
1162 if (infile)
1163 infile->close();
1164 if (outfile)
1165 outfile->close();
1166 }
1167
1170 {
1171 if (line)
1172 {
1173 bool success = false;
1174 while (!success)
1175 {
1176 success = true;
1177 for (ui32 i = 0; i < num_tiles.w; ++i)
1178 {
1179 ui32 idx = i + cur_tile_row * num_tiles.w;
1180 if ((success &= tiles[idx].push(line, cur_comp)) == false)
1181 break;
1182 }
1183 cur_tile_row += success == false ? 1 : 0;
1184 if (cur_tile_row >= num_tiles.h)
1185 cur_tile_row = 0;
1186 }
1187
1188 if (planar) //process one component at a time
1189 {
1190 if (++cur_line >= comp_size[cur_comp].h)
1191 {
1192 cur_line = 0;
1193 cur_tile_row = 0;
1194 if (++cur_comp >= num_comps)
1195 {
1196 next_component = 0;
1197 return NULL;
1198 }
1199 }
1200 }
1201 else //process all component for a line
1202 {
1203 if (++cur_comp >= num_comps)
1204 {
1205 cur_comp = 0;
1206 if (++cur_line >= comp_size[cur_comp].h)
1207 {
1208 next_component = 0;
1209 return NULL;
1210 }
1211 }
1212 }
1213 }
1214
1215 next_component = cur_comp;
1216 return this->lines + cur_comp;
1217 }
1218
1221 {
1222 bool success = false;
1223 while (!success)
1224 {
1225 success = true;
1226 for (ui32 i = 0; i < num_tiles.w; ++i)
1227 {
1228 ui32 idx = i + cur_tile_row * num_tiles.w;
1229 if ((success &= tiles[idx].pull(lines + cur_comp, cur_comp)) == false)
1230 break;
1231 }
1232 cur_tile_row += success == false ? 1 : 0;
1233 if (cur_tile_row >= num_tiles.h)
1234 cur_tile_row = 0;
1235 }
1236 comp_num = cur_comp;
1237
1238 if (planar) //process one component at a time
1239 {
1240 if (++cur_line >= recon_comp_size[cur_comp].h)
1241 {
1242 cur_line = 0;
1243 cur_tile_row = 0;
1244 if (cur_comp++ >= num_comps)
1245 {
1246 comp_num = 0;
1247 return NULL;
1248 }
1249 }
1250 }
1251 else //process all component for a line
1252 {
1253 if (++cur_comp >= num_comps)
1254 {
1255 cur_comp = 0;
1256 if (cur_line++ >= recon_comp_size[cur_comp].h)
1257 {
1258 comp_num = 0;
1259 return NULL;
1260 }
1261 }
1262 }
1263
1264 return lines + comp_num;
1265 }
1266
1267 }
1268}
virtual bool eof()=0
virtual size_t read(void *ptr, size_t size)=0
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)
static void pre_alloc(codestream *codestream, const rect &tile_rect, const rect &recon_tile_rect, ui32 &num_tileparts)
Definition ojph_tile.cpp:56
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()
const char OJPH_PN_STRING_BROADCAST[]
static ui16 swap_bytes_if_le(ui16 t)
Definition ojph_arch.h:397
const char OJPH_PN_STRING_IMF[]
uint64_t ui64
Definition ojph_defs.h:56
@ OJPH_MSG_INFO
@ OJPH_MSG_ERROR
@ OJPH_MSG_WARN
@ OJPH_MSG_NO_MSG
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
size get_log_precinct_size(ui32 res_num) const
void read_coc(infile_base *file, ui32 num_comps, param_cod *top_cod)
void read_qcc(infile_base *file, ui32 num_comps)
bool read(infile_base *file, bool resilient)
point org
Definition ojph_base.h:66