My Project  2.0.19
jas_seq.h
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  * Sequence/Matrix Library
66  *
67  * $Id$
68  */
69 
70 #ifndef JAS_SEQ_H
71 #define JAS_SEQ_H
72 
73 /******************************************************************************\
74 * Includes.
75 \******************************************************************************/
76 
77 /* The configuration header file should be included first. */
78 #include <jasper/jas_config.h> /* IWYU pragma: keep */
79 
80 #include <jasper/jas_types.h>
81 #include <jasper/jas_math.h>
82 
83 #include <stdio.h>
84 
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 
89 /******************************************************************************\
90 * Constants.
91 \******************************************************************************/
92 
93 /* This matrix is a reference to another matrix. */
94 #define JAS_MATRIX_REF 0x0001
95 
96 /******************************************************************************\
97 * Types.
98 \******************************************************************************/
99 
100 /* An element in a sequence. */
101 #ifdef JAS_ENABLE_32BIT
102 typedef int_least32_t jas_seqent_t;
103 #define PRIjas_seqent PRIiLEAST32
104 #else
105 typedef int_fast32_t jas_seqent_t;
106 #define PRIjas_seqent PRIiFAST32
107 #endif
108 
109 /* An element in a matrix. */
110 #ifdef JAS_ENABLE_32BIT
111 typedef int_least32_t jas_matent_t;
112 #else
113 typedef int_fast32_t jas_matent_t;
114 #endif
115 
116 #ifdef JAS_ENABLE_32BIT
117 typedef int_least32_t jas_matind_t;
118 #else
119 typedef int_fast32_t jas_matind_t;
120 #endif
121 
122 /* Matrix. */
123 
124 typedef struct {
125 
126  /* Additional state information. */
127  int flags_;
128 
129  /* The starting horizontal index. */
130  jas_matind_t xstart_;
131 
132  /* The starting vertical index. */
133  jas_matind_t ystart_;
134 
135  /* The ending horizontal index. */
136  jas_matind_t xend_;
137 
138  /* The ending vertical index. */
139  jas_matind_t yend_;
140 
141  /* The number of rows in the matrix. */
142  jas_matind_t numrows_;
143 
144  /* The number of columns in the matrix. */
145  jas_matind_t numcols_;
146 
147  /* Pointers to the start of each row. */
148  jas_seqent_t **rows_;
149 
150  /* The allocated size of the rows array. */
151  int_fast32_t maxrows_;
152 
153  /* The matrix data buffer. */
154  jas_seqent_t *data_;
155 
156  /* The allocated size of the data array. */
157  int_fast32_t datasize_;
158 
159 } jas_matrix_t;
160 
161 typedef jas_matrix_t jas_seq2d_t;
162 typedef jas_matrix_t jas_seq_t;
163 
164 /******************************************************************************\
165 * Functions/macros for matrix class.
166 \******************************************************************************/
167 
168 /* Get the number of rows. */
169 JAS_ATTRIBUTE_PURE
170 static inline jas_matind_t jas_matrix_numrows(const jas_matrix_t *matrix)
171 {
172  return matrix->numrows_;
173 }
174 
175 /* Get the number of columns. */
176 JAS_ATTRIBUTE_PURE
177 static inline jas_matind_t jas_matrix_numcols(const jas_matrix_t *matrix)
178 {
179  return matrix->numcols_;
180 }
181 
182 JAS_ATTRIBUTE_PURE
183 static inline jas_matind_t jas_matrix_size(const jas_matrix_t *matrix)
184 {
185  return jas_matrix_numcols(matrix) * jas_matrix_numrows(matrix);
186 }
187 
188 /* Get a matrix element. */
189 JAS_ATTRIBUTE_PURE
190 static inline jas_seqent_t jas_matrix_get(const jas_matrix_t *matrix, jas_matind_t i, jas_matind_t j)
191 {
192  return matrix->rows_[i][j];
193 }
194 
195 /* Set a matrix element. */
196 static inline void jas_matrix_set(jas_matrix_t *matrix, jas_matind_t i, jas_matind_t j, jas_seqent_t v)
197 {
198  matrix->rows_[i][j] = v;
199 }
200 
201 /* Get an element from a matrix that is known to be a row or column vector. */
202 JAS_ATTRIBUTE_PURE
203 static inline jas_seqent_t jas_matrix_getv(const jas_matrix_t *matrix, jas_matind_t i)
204 {
205  return matrix->numrows_ == 1
206  ? matrix->rows_[0][i]
207  : matrix->rows_[i][0];
208 }
209 
210 /* Set an element in a matrix that is known to be a row or column vector. */
211 static inline void jas_matrix_setv(jas_matrix_t *matrix, jas_matind_t i, jas_seqent_t v)
212 {
213  if (matrix->numrows_ == 1)
214  matrix->rows_[0][i] = v;
215  else
216  matrix->rows_[i][0] = v;
217 }
218 
219 /* Get the address of an element in a matrix. */
220 JAS_ATTRIBUTE_PURE
221 static inline jas_seqent_t *jas_matrix_getref(const jas_matrix_t *matrix, jas_matind_t i, jas_matind_t j)
222 {
223  return &matrix->rows_[i][j];
224 }
225 
226 JAS_ATTRIBUTE_PURE
227 static inline jas_seqent_t *jas_matrix_getvref(const jas_matrix_t *matrix, jas_matind_t i)
228 {
229  return matrix->numrows_ > 1
230  ? jas_matrix_getref(matrix, i, 0)
231  : jas_matrix_getref(matrix, 0, i);
232 }
233 
234 /* Create a matrix with the specified dimensions. */
235 JAS_DLLEXPORT jas_matrix_t *jas_matrix_create(jas_matind_t numrows, jas_matind_t numcols);
236 
237 /* Destroy a matrix. */
238 JAS_DLLEXPORT void jas_matrix_destroy(jas_matrix_t *matrix);
239 
240 /* Resize a matrix. The previous contents of the matrix are lost. */
241 JAS_DLLEXPORT int jas_matrix_resize(jas_matrix_t *matrix, jas_matind_t numrows, jas_matind_t numcols);
242 
243 JAS_DLLEXPORT int jas_matrix_output(jas_matrix_t *matrix, FILE *out);
244 
245 /* Create a matrix that references part of another matrix. */
246 JAS_DLLEXPORT int jas_matrix_bindsub(jas_matrix_t *mat0, jas_matrix_t *mat1, jas_matind_t r0,
247  jas_matind_t c0, jas_matind_t r1, jas_matind_t c1);
248 
249 /* Create a matrix that is a reference to a row of another matrix. */
250 static inline int jas_matrix_bindrow(jas_matrix_t *mat0, jas_matrix_t *mat1, jas_matind_t r)
251 {
252  return jas_matrix_bindsub(mat0, mat1, r, 0, r, mat1->numcols_ - 1);
253 }
254 
255 /* Create a matrix that is a reference to a column of another matrix. */
256 static inline int jas_matrix_bindcol(jas_matrix_t *mat0, jas_matrix_t *mat1, jas_matind_t c)
257 {
258  return jas_matrix_bindsub(mat0, mat1, 0, c, mat1->numrows_ - 1, c);
259 }
260 
261 /* Clip the values of matrix elements to the specified range. */
262 JAS_DLLEXPORT void jas_matrix_clip(jas_matrix_t *matrix, jas_seqent_t minval,
263  jas_seqent_t maxval);
264 
265 /* Arithmetic shift left of all elements in a matrix. */
266 JAS_DLLEXPORT void jas_matrix_asl(jas_matrix_t *matrix, int n);
267 
268 /* Arithmetic shift right of all elements in a matrix. */
269 JAS_DLLEXPORT void jas_matrix_asr(jas_matrix_t *matrix, int n);
270 
271 /* Almost-but-not-quite arithmetic shift right of all elements in a matrix. */
272 JAS_DLLEXPORT void jas_matrix_divpow2(jas_matrix_t *matrix, int n);
273 
274 /* Set all elements of a matrix to the specified value. */
275 JAS_DLLEXPORT void jas_matrix_setall(jas_matrix_t *matrix, jas_seqent_t val);
276 
277 /* The spacing between rows of a matrix. */
278 JAS_ATTRIBUTE_PURE
279 static inline size_t jas_matrix_rowstep(const jas_matrix_t *matrix)
280 {
281  return matrix->numrows_ > 1
282  ? matrix->rows_[1] - matrix->rows_[0]
283  : 0;
284 }
285 
286 /* The spacing between columns of a matrix. */
287 JAS_ATTRIBUTE_PURE
288 static inline size_t jas_matrix_step(const jas_matrix_t *matrix)
289 {
290  return matrix->numrows_ > 1
291  ? jas_matrix_rowstep(matrix)
292  : 1;
293 }
294 
295 /* Compare two matrices for equality. */
296 JAS_DLLEXPORT int jas_matrix_cmp(jas_matrix_t *mat0, jas_matrix_t *mat1);
297 
298 JAS_DLLEXPORT jas_matrix_t *jas_matrix_copy(jas_matrix_t *x);
299 
300 JAS_DLLEXPORT jas_matrix_t *jas_matrix_input(FILE *);
301 
302 #define jas_seqent_asl jas_fast32_asl
303 #define jas_seqent_asr jas_fast32_asr
304 
305 /******************************************************************************\
306 * Functions/macros for 2-D sequence class.
307 \******************************************************************************/
308 
309 JAS_DLLEXPORT jas_seq2d_t *jas_seq2d_copy(jas_seq2d_t *x);
310 
311 JAS_DLLEXPORT jas_matrix_t *jas_seq2d_create(jas_matind_t xstart, jas_matind_t ystart,
312  jas_matind_t xend, jas_matind_t yend);
313 
314 static inline void jas_seq2d_destroy(jas_seq2d_t *s)
315 {
316  jas_matrix_destroy(s);
317 }
318 
319 JAS_ATTRIBUTE_PURE
320 static inline jas_matind_t jas_seq2d_xstart(const jas_seq2d_t *s)
321 {
322  return s->xstart_;
323 }
324 
325 JAS_ATTRIBUTE_PURE
326 static inline jas_matind_t jas_seq2d_ystart(const jas_seq2d_t *s)
327 {
328  return s->ystart_;
329 }
330 
331 JAS_ATTRIBUTE_PURE
332 static inline jas_matind_t jas_seq2d_xend(const jas_seq2d_t *s)
333 {
334  return s->xend_;
335 }
336 
337 JAS_ATTRIBUTE_PURE
338 static inline jas_matind_t jas_seq2d_yend(const jas_seq2d_t *s)
339 {
340  return s->yend_;
341 }
342 
343 JAS_ATTRIBUTE_PURE
344 static inline jas_seqent_t *jas_seq2d_getref(const jas_seq2d_t *s, jas_matind_t x, jas_matind_t y)
345 {
346  return jas_matrix_getref(s, y - s->ystart_, x - s->xstart_);
347 }
348 
349 JAS_ATTRIBUTE_PURE
350 static inline jas_seqent_t jas_seq2d_get(const jas_seq2d_t *s, jas_matind_t x, jas_matind_t y)
351 {
352  return jas_matrix_get(s, y - s->ystart_, x - s->xstart_);
353 }
354 
355 JAS_ATTRIBUTE_PURE
356 static inline size_t jas_seq2d_rowstep(const jas_seq2d_t *s)
357 {
358  return jas_matrix_rowstep(s);
359 }
360 
361 JAS_ATTRIBUTE_PURE
362 static inline unsigned jas_seq2d_width(const jas_seq2d_t *s)
363 {
364  return s->xend_ - s->xstart_;
365 }
366 
367 JAS_ATTRIBUTE_PURE
368 static inline unsigned jas_seq2d_height(const jas_seq2d_t *s)
369 {
370  return s->yend_ - s->ystart_;
371 }
372 
373 static inline void jas_seq2d_setshift(jas_seq2d_t *s, jas_matind_t x, jas_matind_t y)
374 {
375  s->xstart_ = x;
376  s->ystart_ = y;
377  s->xend_ = s->xstart_ + s->numcols_;
378  s->yend_ = s->ystart_ + s->numrows_;
379 }
380 
381 JAS_ATTRIBUTE_PURE
382 static inline jas_matind_t jas_seq2d_size(const jas_seq2d_t *s)
383 {
384  return jas_seq2d_width(s) * jas_seq2d_height(s);
385 }
386 
387 JAS_DLLEXPORT int jas_seq2d_bindsub(jas_matrix_t *s, jas_matrix_t *s1, jas_matind_t xstart,
388  jas_matind_t ystart, jas_matind_t xend, jas_matind_t yend);
389 
390 /******************************************************************************\
391 * Functions/macros for 1-D sequence class.
392 \******************************************************************************/
393 
394 static inline jas_seq_t *jas_seq_create(jas_matind_t start, jas_matind_t end)
395 {
396  return jas_seq2d_create(start, 0, end, 1);
397 }
398 
399 static inline void jas_seq_destroy(jas_seq_t *seq)
400 {
401  jas_seq2d_destroy(seq);
402 }
403 
404 static inline void jas_seq_set(jas_seq_t *seq, jas_matind_t i, jas_seqent_t v)
405 {
406  seq->rows_[0][i - seq->xstart_] = v;
407 }
408 
409 JAS_ATTRIBUTE_PURE
410 static inline jas_seqent_t *jas_seq_getref(const jas_seq_t *seq, jas_matind_t i)
411 {
412  return &seq->rows_[0][i - seq->xstart_];
413 }
414 
415 JAS_ATTRIBUTE_PURE
416 static inline jas_seqent_t jas_seq_get(const jas_seq_t *seq, jas_matind_t i)
417 {
418  return seq->rows_[0][i - seq->xstart_];
419 }
420 
421 JAS_ATTRIBUTE_PURE
422 static inline jas_matind_t jas_seq_start(const jas_seq_t *seq)
423 {
424  return seq->xstart_;
425 }
426 
427 JAS_ATTRIBUTE_PURE
428 static inline jas_matind_t jas_seq_end(const jas_seq_t *seq)
429 {
430  return seq->xend_;
431 }
432 
433 #ifdef __cplusplus
434 }
435 #endif
436 
437 #endif