JasPer: jas_seq.h Source File

JasPer 2.0.33 |

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