jas_seq.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * Sequence/Matrix Library
  64. *
  65. * $Id: jas_seq.h,v 1.2 2008-05-26 09:41:51 vp153 Exp $
  66. */
  67. #ifndef JAS_SEQ_H
  68. #define JAS_SEQ_H
  69. /******************************************************************************\
  70. * Includes.
  71. \******************************************************************************/
  72. #include <jasper/jas_config.h>
  73. #include <jasper/jas_stream.h>
  74. #include <jasper/jas_types.h>
  75. #ifdef __cplusplus
  76. extern "C" {
  77. #endif
  78. /******************************************************************************\
  79. * Constants.
  80. \******************************************************************************/
  81. /* This matrix is a reference to another matrix. */
  82. #define JAS_MATRIX_REF 0x0001
  83. /******************************************************************************\
  84. * Types.
  85. \******************************************************************************/
  86. /* An element in a sequence. */
  87. typedef int_fast32_t jas_seqent_t;
  88. /* An element in a matrix. */
  89. typedef int_fast32_t jas_matent_t;
  90. /* Matrix. */
  91. typedef struct {
  92. /* Additional state information. */
  93. int flags_;
  94. /* The starting horizontal index. */
  95. int_fast32_t xstart_;
  96. /* The starting vertical index. */
  97. int_fast32_t ystart_;
  98. /* The ending horizontal index. */
  99. int_fast32_t xend_;
  100. /* The ending vertical index. */
  101. int_fast32_t yend_;
  102. /* The number of rows in the matrix. */
  103. int_fast32_t numrows_;
  104. /* The number of columns in the matrix. */
  105. int_fast32_t numcols_;
  106. /* Pointers to the start of each row. */
  107. jas_seqent_t **rows_;
  108. /* The allocated size of the rows array. */
  109. int_fast32_t maxrows_;
  110. /* The matrix data buffer. */
  111. jas_seqent_t *data_;
  112. /* The allocated size of the data array. */
  113. int_fast32_t datasize_;
  114. } jas_matrix_t;
  115. typedef jas_matrix_t jas_seq2d_t;
  116. typedef jas_matrix_t jas_seq_t;
  117. /******************************************************************************\
  118. * Functions/macros for matrix class.
  119. \******************************************************************************/
  120. /* Get the number of rows. */
  121. #define jas_matrix_numrows(matrix) \
  122. ((matrix)->numrows_)
  123. /* Get the number of columns. */
  124. #define jas_matrix_numcols(matrix) \
  125. ((matrix)->numcols_)
  126. /* Get a matrix element. */
  127. #define jas_matrix_get(matrix, i, j) \
  128. ((matrix)->rows_[i][j])
  129. /* Set a matrix element. */
  130. #define jas_matrix_set(matrix, i, j, v) \
  131. ((matrix)->rows_[i][j] = (v))
  132. /* Get an element from a matrix that is known to be a row or column vector. */
  133. #define jas_matrix_getv(matrix, i) \
  134. (((matrix)->numrows_ == 1) ? ((matrix)->rows_[0][i]) : \
  135. ((matrix)->rows_[i][0]))
  136. /* Set an element in a matrix that is known to be a row or column vector. */
  137. #define jas_matrix_setv(matrix, i, v) \
  138. (((matrix)->numrows_ == 1) ? ((matrix)->rows_[0][i] = (v)) : \
  139. ((matrix)->rows_[i][0] = (v)))
  140. /* Get the address of an element in a matrix. */
  141. #define jas_matrix_getref(matrix, i, j) \
  142. (&(matrix)->rows_[i][j])
  143. #define jas_matrix_getvref(matrix, i) \
  144. (((matrix)->numrows_ > 1) ? jas_matrix_getref(matrix, i, 0) : jas_matrix_getref(matrix, 0, i))
  145. #define jas_matrix_length(matrix) \
  146. (max((matrix)->numrows_, (matrix)->numcols_))
  147. /* Create a matrix with the specified dimensions. */
  148. jas_matrix_t *jas_matrix_create(int numrows, int numcols);
  149. /* Destroy a matrix. */
  150. void jas_matrix_destroy(jas_matrix_t *matrix);
  151. /* Resize a matrix. The previous contents of the matrix are lost. */
  152. int jas_matrix_resize(jas_matrix_t *matrix, int numrows, int numcols);
  153. int jas_matrix_output(jas_matrix_t *matrix, FILE *out);
  154. /* Create a matrix that references part of another matrix. */
  155. void jas_matrix_bindsub(jas_matrix_t *mat0, jas_matrix_t *mat1, int r0, int c0,
  156. int r1, int c1);
  157. /* Create a matrix that is a reference to a row of another matrix. */
  158. #define jas_matrix_bindrow(mat0, mat1, r) \
  159. (jas_matrix_bindsub((mat0), (mat1), (r), 0, (r), (mat1)->numcols_ - 1))
  160. /* Create a matrix that is a reference to a column of another matrix. */
  161. #define jas_matrix_bindcol(mat0, mat1, c) \
  162. (jas_matrix_bindsub((mat0), (mat1), 0, (c), (mat1)->numrows_ - 1, (c)))
  163. /* Clip the values of matrix elements to the specified range. */
  164. void jas_matrix_clip(jas_matrix_t *matrix, jas_seqent_t minval,
  165. jas_seqent_t maxval);
  166. /* Arithmetic shift left of all elements in a matrix. */
  167. void jas_matrix_asl(jas_matrix_t *matrix, int n);
  168. /* Arithmetic shift right of all elements in a matrix. */
  169. void jas_matrix_asr(jas_matrix_t *matrix, int n);
  170. /* Almost-but-not-quite arithmetic shift right of all elements in a matrix. */
  171. void jas_matrix_divpow2(jas_matrix_t *matrix, int n);
  172. /* Set all elements of a matrix to the specified value. */
  173. void jas_matrix_setall(jas_matrix_t *matrix, jas_seqent_t val);
  174. /* The spacing between rows of a matrix. */
  175. #define jas_matrix_rowstep(matrix) \
  176. (((matrix)->numrows_ > 1) ? ((matrix)->rows_[1] - (matrix)->rows_[0]) : (0))
  177. /* The spacing between columns of a matrix. */
  178. #define jas_matrix_step(matrix) \
  179. (((matrix)->numrows_ > 1) ? (jas_matrix_rowstep(matrix)) : (1))
  180. /* Compare two matrices for equality. */
  181. int jas_matrix_cmp(jas_matrix_t *mat0, jas_matrix_t *mat1);
  182. jas_matrix_t *jas_matrix_copy(jas_matrix_t *x);
  183. jas_matrix_t *jas_matrix_input(FILE *);
  184. /******************************************************************************\
  185. * Functions/macros for 2-D sequence class.
  186. \******************************************************************************/
  187. jas_seq2d_t *jas_seq2d_copy(jas_seq2d_t *x);
  188. jas_matrix_t *jas_seq2d_create(int xstart, int ystart, int xend, int yend);
  189. #define jas_seq2d_destroy(s) \
  190. jas_matrix_destroy(s)
  191. #define jas_seq2d_xstart(s) \
  192. ((s)->xstart_)
  193. #define jas_seq2d_ystart(s) \
  194. ((s)->ystart_)
  195. #define jas_seq2d_xend(s) \
  196. ((s)->xend_)
  197. #define jas_seq2d_yend(s) \
  198. ((s)->yend_)
  199. #define jas_seq2d_getref(s, x, y) \
  200. (jas_matrix_getref(s, (y) - (s)->ystart_, (x) - (s)->xstart_))
  201. #define jas_seq2d_get(s, x, y) \
  202. (jas_matrix_get(s, (y) - (s)->ystart_, (x) - (s)->xstart_))
  203. #define jas_seq2d_rowstep(s) \
  204. jas_matrix_rowstep(s)
  205. #define jas_seq2d_width(s) \
  206. ((s)->xend_ - (s)->xstart_)
  207. #define jas_seq2d_height(s) \
  208. ((s)->yend_ - (s)->ystart_)
  209. #define jas_seq2d_setshift(s, x, y) \
  210. ((s)->xstart_ = (x), (s)->ystart_ = (y), \
  211. (s)->xend_ = (s)->xstart_ + (s)->numcols_, \
  212. (s)->yend_ = (s)->ystart_ + (s)->numrows_)
  213. void jas_seq2d_bindsub(jas_matrix_t *s, jas_matrix_t *s1, int xstart,
  214. int ystart, int xend, int yend);
  215. /******************************************************************************\
  216. * Functions/macros for 1-D sequence class.
  217. \******************************************************************************/
  218. #define jas_seq_create(start, end) \
  219. (jas_seq2d_create(start, 0, end, 1))
  220. #define jas_seq_destroy(seq) \
  221. (jas_seq2d_destroy(seq))
  222. #define jas_seq_set(seq, i, v) \
  223. ((seq)->rows_[0][(i) - (seq)->xstart_] = (v))
  224. #define jas_seq_getref(seq, i) \
  225. (&(seq)->rows_[0][(i) - (seq)->xstart_])
  226. #define jas_seq_get(seq, i) \
  227. ((seq)->rows_[0][(i) - (seq)->xstart_])
  228. #define jas_seq_start(seq) \
  229. ((seq)->xstart_)
  230. #define jas_seq_end(seq) \
  231. ((seq)->xend_)
  232. #ifdef __cplusplus
  233. }
  234. #endif
  235. #endif