Coverage Report

Created: 2025-07-23 08:18

/work/include/jasper/jas_seq.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3
 *   British Columbia.
4
 * Copyright (c) 2001-2002 Michael David Adams.
5
 * All rights reserved.
6
 */
7
8
/* __START_OF_JASPER_LICENSE__
9
 * 
10
 * JasPer License Version 2.0
11
 * 
12
 * Copyright (c) 2001-2006 Michael David Adams
13
 * Copyright (c) 1999-2000 Image Power, Inc.
14
 * Copyright (c) 1999-2000 The University of British Columbia
15
 * 
16
 * All rights reserved.
17
 * 
18
 * Permission is hereby granted, free of charge, to any person (the
19
 * "User") obtaining a copy of this software and associated documentation
20
 * files (the "Software"), to deal in the Software without restriction,
21
 * including without limitation the rights to use, copy, modify, merge,
22
 * publish, distribute, and/or sell copies of the Software, and to permit
23
 * persons to whom the Software is furnished to do so, subject to the
24
 * following conditions:
25
 * 
26
 * 1.  The above copyright notices and this permission notice (which
27
 * includes the disclaimer below) shall be included in all copies or
28
 * substantial portions of the Software.
29
 * 
30
 * 2.  The name of a copyright holder shall not be used to endorse or
31
 * promote products derived from the Software without specific prior
32
 * written permission.
33
 * 
34
 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35
 * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36
 * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37
 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39
 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
40
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41
 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
45
 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46
 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47
 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48
 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49
 * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
50
 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51
 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
52
 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53
 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54
 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55
 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56
 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57
 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58
 * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59
 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60
 * 
61
 * __END_OF_JASPER_LICENSE__
62
 */
63
64
/*!
65
 * @file jas_seq.h
66
 * @brief Sequence/Matrix Library
67
 */
68
69
#ifndef JAS_SEQ_H
70
#define JAS_SEQ_H
71
72
/******************************************************************************\
73
* Includes.
74
\******************************************************************************/
75
76
/* The configuration header file should be included first. */
77
#include <jasper/jas_config.h> /* IWYU pragma: keep */
78
79
#include <jasper/jas_types.h>
80
#include <jasper/jas_fix.h>
81
#include <jasper/jas_math.h>
82
83
#include <stdio.h>
84
85
#ifdef __cplusplus
86
extern "C" {
87
#endif
88
89
/*!
90
 * @addtogroup module_seq
91
 * @{
92
 */
93
94
/******************************************************************************\
95
* Constants.
96
\******************************************************************************/
97
98
/* This matrix is a reference to another matrix. */
99
#define JAS_MATRIX_REF  0x0001
100
101
/******************************************************************************\
102
* Types.
103
\******************************************************************************/
104
105
/* An element in a sequence. */
106
typedef jas_fix_t jas_seqent_t;
107
#define PRIjas_seqent PRIjas_fix
108
109
/* An element in a matrix. */
110
typedef jas_fix_t jas_matent_t;
111
112
typedef jas_fix_t jas_matind_t;
113
114
/*!
115
@brief Matrix type.
116
*/
117
typedef struct {
118
119
  /* Additional state information. */
120
  int flags_;
121
122
  /* The starting horizontal index. */
123
  jas_matind_t xstart_;
124
125
  /* The starting vertical index. */
126
  jas_matind_t ystart_;
127
128
  /* The ending horizontal index. */
129
  jas_matind_t xend_;
130
131
  /* The ending vertical index. */
132
  jas_matind_t yend_;
133
134
  /* The number of rows in the matrix. */
135
  jas_matind_t numrows_;
136
137
  /* The number of columns in the matrix. */
138
  jas_matind_t numcols_;
139
140
  /* Pointers to the start of each row. */
141
  jas_seqent_t **rows_;
142
143
  /* The allocated size of the rows array. */
144
  int_fast32_t maxrows_;
145
146
  /* The matrix data buffer. */
147
  jas_seqent_t *data_;
148
149
  /* The allocated size of the data array. */
150
  int_fast32_t datasize_;
151
152
} jas_matrix_t;
153
154
/*!
155
@struct jas_seq2d_t
156
@brief Two-dimensional sequence type.
157
*/
158
typedef jas_matrix_t jas_seq2d_t;
159
160
/*!
161
@struct jas_seq_t
162
@brief One-dimensional sequence type.
163
*/
164
typedef jas_matrix_t jas_seq_t;
165
166
/******************************************************************************\
167
* Functions/macros for matrix class.
168
\******************************************************************************/
169
170
/*!
171
@brief
172
Get the number of rows in a matrix.
173
*/
174
JAS_ATTRIBUTE_PURE
175
static inline jas_matind_t jas_matrix_numrows(const jas_matrix_t *matrix)
176
0
{
177
0
  return matrix->numrows_;
178
0
}
179
180
/*!
181
@brief Get the number of columns in a matrix.
182
*/
183
JAS_ATTRIBUTE_PURE
184
static inline jas_matind_t jas_matrix_numcols(const jas_matrix_t *matrix)
185
0
{
186
0
  return matrix->numcols_;
187
0
}
188
189
/*!
190
@brief
191
Get the number of elements in a matrix.
192
*/
193
JAS_ATTRIBUTE_PURE
194
static inline jas_matind_t jas_matrix_size(const jas_matrix_t *matrix)
195
0
{
196
0
  return jas_matrix_numcols(matrix) * jas_matrix_numrows(matrix);
197
0
}
198
199
/*!
200
@brief
201
Test if a matrix is empty (i.e., contains no elements).
202
*/
203
JAS_ATTRIBUTE_PURE
204
static inline bool jas_matrix_empty(const jas_matrix_t *matrix)
205
0
{
206
0
  return jas_matrix_numcols(matrix) == 0 || jas_matrix_numrows(matrix) == 0;
207
0
}
208
209
/*!
210
@brief
211
Get a matrix element.
212
*/
213
JAS_ATTRIBUTE_PURE
214
static inline jas_seqent_t jas_matrix_get(const jas_matrix_t *matrix, jas_matind_t i, jas_matind_t j)
215
0
{
216
0
  assert(i >= 0 && i < matrix->numrows_ && j >= 0 && j < matrix->numcols_);
217
0
  return matrix->rows_[i][j];
218
0
}
219
220
/*!
221
@brief
222
Set a matrix element.
223
*/
224
static inline void jas_matrix_set(jas_matrix_t *matrix, jas_matind_t i, jas_matind_t j, jas_seqent_t v)
225
0
{
226
0
  assert(i >= 0 && i < matrix->numrows_ && j >= 0 && j < matrix->numcols_);
227
0
  matrix->rows_[i][j] = v;
228
0
}
229
230
/*!
231
@brief
232
Get an element from a matrix that is known to be a row or column vector.
233
*/
234
JAS_ATTRIBUTE_PURE
235
static inline jas_seqent_t jas_matrix_getv(const jas_matrix_t *matrix, jas_matind_t i)
236
0
{
237
0
  return matrix->numrows_ == 1
238
0
    ? matrix->rows_[0][i]
239
0
    : matrix->rows_[i][0];
240
0
}
241
242
/*!
243
@brief
244
Set an element in a matrix that is known to be a row or column vector.
245
*/
246
static inline void jas_matrix_setv(jas_matrix_t *matrix, jas_matind_t i, jas_seqent_t v)
247
0
{
248
0
  if (matrix->numrows_ == 1)
249
0
    matrix->rows_[0][i] = v;
250
0
  else
251
0
    matrix->rows_[i][0] = v;
252
0
}
253
254
/*!
255
@brief
256
Get the address of an element in a matrix.
257
*/
258
JAS_ATTRIBUTE_PURE
259
static inline jas_seqent_t *jas_matrix_getref(const jas_matrix_t *matrix, jas_matind_t i, jas_matind_t j)
260
0
{
261
0
  return &matrix->rows_[i][j];
262
0
}
263
264
/*!
265
@brief
266
Get a reference to a particular row of a 2-D sequence.
267
*/
268
JAS_ATTRIBUTE_PURE
269
static inline jas_seqent_t *jas_matrix_getvref(const jas_matrix_t *matrix, jas_matind_t i)
270
0
{
271
0
  return matrix->numrows_ > 1
272
0
    ? jas_matrix_getref(matrix, i, 0)
273
0
    : jas_matrix_getref(matrix, 0, i);
274
0
}
275
276
/*!
277
@brief
278
Create a matrix with the specified dimensions.
279
*/
280
JAS_EXPORT
281
jas_matrix_t *jas_matrix_create(jas_matind_t numrows, jas_matind_t numcols);
282
283
/*!
284
@brief
285
Destroy a matrix.
286
*/
287
JAS_EXPORT
288
void jas_matrix_destroy(jas_matrix_t *matrix);
289
290
/*!
291
@brief
292
Resize a matrix.  The previous contents of the matrix are lost.
293
*/
294
JAS_EXPORT
295
int jas_matrix_resize(jas_matrix_t *matrix, jas_matind_t numrows, jas_matind_t numcols);
296
297
/*!
298
@brief
299
Write a matrix to a C standard library stream.
300
*/
301
JAS_EXPORT
302
int jas_matrix_output(jas_matrix_t *matrix, FILE *out);
303
304
/*!
305
@brief
306
Create a matrix that references part of another matrix.
307
*/
308
JAS_EXPORT
309
int jas_matrix_bindsub(jas_matrix_t *mat0, jas_matrix_t *mat1, jas_matind_t r0,
310
  jas_matind_t c0, jas_matind_t r1, jas_matind_t c1);
311
312
/*!
313
@brief
314
Create a matrix that is a reference to a row of another matrix.
315
*/
316
static inline int jas_matrix_bindrow(jas_matrix_t *mat0, jas_matrix_t *mat1, jas_matind_t r)
317
0
{
318
0
  return jas_matrix_bindsub(mat0, mat1, r, 0, r, mat1->numcols_ - 1);
319
0
}
320
321
/*!
322
@brief
323
Create a matrix that is a reference to a column of another matrix.
324
*/
325
static inline int jas_matrix_bindcol(jas_matrix_t *mat0, jas_matrix_t *mat1, jas_matind_t c)
326
0
{
327
0
  return jas_matrix_bindsub(mat0, mat1, 0, c, mat1->numrows_ - 1, c);
328
0
}
329
330
/*!
331
@brief
332
Clip the values of matrix elements to the specified range.
333
*/
334
JAS_EXPORT
335
void jas_matrix_clip(jas_matrix_t *matrix, jas_seqent_t minval,
336
  jas_seqent_t maxval);
337
338
/*!
339
@brief
340
Arithmetic shift left of all elements in a matrix.
341
*/
342
JAS_EXPORT
343
void jas_matrix_asl(jas_matrix_t *matrix, unsigned n);
344
345
/*!
346
@brief
347
Arithmetic shift right of all elements in a matrix.
348
*/
349
JAS_EXPORT
350
void jas_matrix_asr(jas_matrix_t *matrix, unsigned n);
351
352
/*!
353
@brief
354
Almost-but-not-quite arithmetic shift right of all elements in a matrix.
355
*/
356
JAS_EXPORT
357
void jas_matrix_divpow2(jas_matrix_t *matrix, unsigned n);
358
359
/*!
360
@brief
361
Set all elements of a matrix to the specified value.
362
*/
363
JAS_EXPORT
364
void jas_matrix_setall(jas_matrix_t *matrix, jas_seqent_t val);
365
366
/*!
367
@brief
368
The spacing between rows of a matrix.
369
*/
370
JAS_ATTRIBUTE_PURE
371
static inline size_t jas_matrix_rowstep(const jas_matrix_t *matrix)
372
0
{
373
0
  return matrix->numrows_ > 1
374
0
    ? (size_t)(matrix->rows_[1] - matrix->rows_[0])
375
0
    : 0u;
376
0
}
377
378
/*!
379
@brief
380
The spacing between columns of a matrix.
381
*/
382
JAS_ATTRIBUTE_PURE
383
static inline size_t jas_matrix_step(const jas_matrix_t *matrix)
384
0
{
385
0
  return matrix->numrows_ > 1
386
0
    ? jas_matrix_rowstep(matrix)
387
0
    : 1;
388
0
}
389
390
/*!
391
@brief
392
Compare two matrices for equality.
393
*/
394
JAS_EXPORT
395
int jas_matrix_cmp(jas_matrix_t *mat0, jas_matrix_t *mat1);
396
397
/*!
398
@brief
399
Copy a matrix.
400
*/
401
JAS_EXPORT
402
jas_matrix_t *jas_matrix_copy(jas_matrix_t *x);
403
404
/*!
405
@brief
406
Read a matrix from a C standard library stream.
407
*/
408
JAS_EXPORT
409
jas_matrix_t *jas_matrix_input(FILE *);
410
411
/*!
412
@brief
413
*/
414
JAS_ATTRIBUTE_CONST
415
static inline jas_seqent_t jas_seqent_asl(jas_seqent_t x, unsigned n)
416
0
{
417
0
#if 0
418
0
#ifdef JAS_ENABLE_32BIT
419
0
  return jas_least32_asl(x, n);
420
0
#else
421
0
  return jas_fast32_asl(x, n);
422
0
#endif
423
0
#endif
424
0
  return jas_fix_asl(x, n);
425
0
}
426
427
/*!
428
@brief
429
*/
430
JAS_ATTRIBUTE_CONST
431
static inline jas_seqent_t jas_seqent_asr(jas_seqent_t x, unsigned n)
432
0
{
433
0
#if 0
434
0
#ifdef JAS_ENABLE_32BIT
435
0
  return jas_least32_asr(x, n);
436
0
#else
437
0
  return jas_fast32_asr(x, n);
438
0
#endif
439
0
#endif
440
0
  return jas_fix_asr(x, n);
441
0
}
442
443
/******************************************************************************\
444
* Functions/macros for 2-D sequence class.
445
\******************************************************************************/
446
447
/*!
448
@brief
449
Copy a 2-D sequence.
450
*/
451
JAS_EXPORT
452
jas_seq2d_t *jas_seq2d_copy(jas_seq2d_t *x);
453
454
/*!
455
@brief
456
Create a 2-D sequence.
457
*/
458
JAS_EXPORT
459
jas_matrix_t *jas_seq2d_create(jas_matind_t xstart, jas_matind_t ystart,
460
  jas_matind_t xend, jas_matind_t yend);
461
462
/*!
463
@brief
464
Destroy a 2-D sequence.
465
*/
466
static inline void jas_seq2d_destroy(jas_seq2d_t *s)
467
0
{
468
0
  jas_matrix_destroy(s);
469
0
}
470
471
/*!
472
@brief
473
Get the starting x-coordinate of the sequence.
474
*/
475
JAS_ATTRIBUTE_PURE
476
static inline jas_matind_t jas_seq2d_xstart(const jas_seq2d_t *s)
477
0
{
478
0
  return s->xstart_;
479
0
}
480
481
/*!
482
@brief
483
Get the starting y-coordinate of the sequence.
484
*/
485
JAS_ATTRIBUTE_PURE
486
static inline jas_matind_t jas_seq2d_ystart(const jas_seq2d_t *s)
487
0
{
488
0
  return s->ystart_;
489
0
}
490
491
/*!
492
@brief
493
Get the ending x-coordinate of the sequence.
494
*/
495
JAS_ATTRIBUTE_PURE
496
static inline jas_matind_t jas_seq2d_xend(const jas_seq2d_t *s)
497
0
{
498
0
  return s->xend_;
499
0
}
500
501
/*!
502
@brief
503
Get the ending y-coordinate of the sequence.
504
*/
505
JAS_ATTRIBUTE_PURE
506
static inline jas_matind_t jas_seq2d_yend(const jas_seq2d_t *s)
507
0
{
508
0
  return s->yend_;
509
0
}
510
511
/*!
512
@brief
513
Get a pointer (i.e., reference) to an element of a 2-D sequence.
514
*/
515
JAS_ATTRIBUTE_PURE
516
static inline jas_seqent_t *jas_seq2d_getref(const jas_seq2d_t *s, jas_matind_t x, jas_matind_t y)
517
0
{
518
0
  return jas_matrix_getref(s, y - s->ystart_, x - s->xstart_);
519
0
}
520
521
/*!
522
@brief
523
Get an element of a 2-D sequence.
524
*/
525
JAS_ATTRIBUTE_PURE
526
static inline jas_seqent_t jas_seq2d_get(const jas_seq2d_t *s, jas_matind_t x, jas_matind_t y)
527
0
{
528
0
  return jas_matrix_get(s, y - s->ystart_, x - s->xstart_);
529
0
}
530
531
/*!
532
@brief
533
Get the stride between successive rows in the sequence.
534
*/
535
JAS_ATTRIBUTE_PURE
536
static inline size_t jas_seq2d_rowstep(const jas_seq2d_t *s)
537
0
{
538
0
  return jas_matrix_rowstep(s);
539
0
}
540
541
/*!
542
@brief
543
Get the number of columns in the sequence.
544
*/
545
JAS_ATTRIBUTE_PURE
546
static inline unsigned jas_seq2d_width(const jas_seq2d_t *s)
547
0
{
548
0
  return (unsigned)(s->xend_ - s->xstart_);
549
0
}
550
551
/*!
552
@brief
553
Get the number of rows in the sequence.
554
*/
555
JAS_ATTRIBUTE_PURE
556
static inline unsigned jas_seq2d_height(const jas_seq2d_t *s)
557
0
{
558
0
  return (unsigned)(s->yend_ - s->ystart_);
559
0
}
560
561
/*!
562
@brief
563
Set the shift (i.e., starting x- and y-coordinates) of the sequence.
564
*/
565
static inline void jas_seq2d_setshift(jas_seq2d_t *s, jas_matind_t x, jas_matind_t y)
566
0
{
567
0
  s->xstart_ = x;
568
0
  s->ystart_ = y;
569
0
  s->xend_ = s->xstart_ + s->numcols_;
570
0
  s->yend_ = s->ystart_ + s->numrows_;
571
0
}
572
573
/*!
574
@brief
575
Get the number of elements in the sequence.
576
*/
577
JAS_ATTRIBUTE_PURE
578
static inline jas_matind_t jas_seq2d_size(const jas_seq2d_t *s)
579
0
{
580
0
  return jas_seq2d_width(s) * jas_seq2d_height(s);
581
0
}
582
583
/*!
584
@brief
585
Test if the sequence is empty (i.e., contains no elements).
586
*/
587
JAS_ATTRIBUTE_PURE
588
static inline bool jas_seq2d_empty(const jas_seq2d_t *s)
589
0
{
590
0
  return jas_seq2d_width(s) == 0 || jas_seq2d_height(s) == 0;
591
0
}
592
593
/*!
594
@brief
595
Initialize a sequence to reference a subsequence of another sequence.
596
*/
597
JAS_EXPORT
598
int jas_seq2d_bindsub(jas_matrix_t *s, jas_matrix_t *s1, jas_matind_t xstart,
599
  jas_matind_t ystart, jas_matind_t xend, jas_matind_t yend);
600
601
/******************************************************************************\
602
* Functions/macros for 1-D sequence class.
603
\******************************************************************************/
604
605
/*!
606
@brief
607
Create a 1-D sequence.
608
*/
609
static inline jas_seq_t *jas_seq_create(jas_matind_t start, jas_matind_t end)
610
0
{
611
0
  return jas_seq2d_create(start, 0, end, 1);
612
0
}
613
614
/*!
615
@brief
616
Destroy a 1-D sequence.
617
*/
618
static inline void jas_seq_destroy(jas_seq_t *seq)
619
0
{
620
0
  jas_seq2d_destroy(seq);
621
0
}
622
623
/*!
624
@brief
625
Set an element of a sequence.
626
*/
627
static inline void jas_seq_set(jas_seq_t *seq, jas_matind_t i, jas_seqent_t v)
628
0
{
629
0
  seq->rows_[0][i - seq->xstart_] = v;
630
0
}
631
632
/*!
633
@brief
634
Get a pointer (i.e., reference) to an element of a sequence.
635
*/
636
JAS_ATTRIBUTE_PURE
637
static inline jas_seqent_t *jas_seq_getref(const jas_seq_t *seq, jas_matind_t i)
638
0
{
639
0
  return &seq->rows_[0][i - seq->xstart_];
640
0
}
641
642
/*!
643
@brief
644
Get an element of a sequence.
645
*/
646
JAS_ATTRIBUTE_PURE
647
static inline jas_seqent_t jas_seq_get(const jas_seq_t *seq, jas_matind_t i)
648
0
{
649
0
  return seq->rows_[0][i - seq->xstart_];
650
0
}
651
652
/*!
653
@brief
654
Get the starting index of a sequence.
655
*/
656
JAS_ATTRIBUTE_PURE
657
static inline jas_matind_t jas_seq_start(const jas_seq_t *seq)
658
0
{
659
0
  return seq->xstart_;
660
0
}
661
662
/*!
663
@brief
664
Get the ending index of a sequence.
665
*/
666
JAS_ATTRIBUTE_PURE
667
static inline jas_matind_t jas_seq_end(const jas_seq_t *seq)
668
0
{
669
0
  return seq->xend_;
670
0
}
671
672
/*!
673
 * @}
674
 */
675
676
#ifdef __cplusplus
677
}
678
#endif
679
680
#endif