2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2002 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
12 * Copyright (c) 1999-2000 Image Power, Inc.
13 * Copyright (c) 1999-2000 The University of British Columbia
14 * Copyright (c) 2001-2003 Michael David Adams
16 * All rights reserved.
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:
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.
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
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.
61 * __END_OF_JASPER_LICENSE__
65 * Sequence/Matrix Library
67 * $Id: jas_seq.c,v 1.1 2005/05/22 18:32:59 malaterre Exp $
70 /******************************************************************************\
72 \******************************************************************************/
78 #include "jasper/jas_seq.h"
79 #include "jasper/jas_malloc.h"
80 #include "jasper/jas_math.h"
82 /******************************************************************************\
83 * Constructors and destructors.
84 \******************************************************************************/
86 jas_matrix_t *jas_seq2d_create(int xstart, int ystart, int xend, int yend)
89 assert(xstart <= xend && ystart <= yend);
90 if (!(matrix = jas_matrix_create(yend - ystart, xend - xstart))) {
93 matrix->xstart_ = xstart;
94 matrix->ystart_ = ystart;
100 jas_matrix_t *jas_matrix_create(int numrows, int numcols)
102 jas_matrix_t *matrix;
105 if (!(matrix = jas_malloc(sizeof(jas_matrix_t)))) {
109 matrix->numrows_ = numrows;
110 matrix->numcols_ = numcols;
112 matrix->maxrows_ = numrows;
114 matrix->datasize_ = numrows * numcols;
116 if (matrix->maxrows_ > 0) {
117 if (!(matrix->rows_ = jas_malloc(matrix->maxrows_ *
118 sizeof(jas_seqent_t *)))) {
119 jas_matrix_destroy(matrix);
124 if (matrix->datasize_ > 0) {
125 if (!(matrix->data_ = jas_malloc(matrix->datasize_ *
126 sizeof(jas_seqent_t)))) {
127 jas_matrix_destroy(matrix);
132 for (i = 0; i < numrows; ++i) {
133 matrix->rows_[i] = &matrix->data_[i * matrix->numcols_];
136 for (i = 0; i < matrix->datasize_; ++i) {
137 matrix->data_[i] = 0;
142 matrix->xend_ = matrix->numcols_;
143 matrix->yend_ = matrix->numrows_;
148 void jas_matrix_destroy(jas_matrix_t *matrix)
151 assert(!(matrix->flags_ & JAS_MATRIX_REF));
152 jas_free(matrix->data_);
156 jas_free(matrix->rows_);
162 jas_seq2d_t *jas_seq2d_copy(jas_seq2d_t *x)
167 y = jas_seq2d_create(jas_seq2d_xstart(x), jas_seq2d_ystart(x), jas_seq2d_xend(x),
170 for (i = 0; i < x->numrows_; ++i) {
171 for (j = 0; j < x->numcols_; ++j) {
172 *jas_matrix_getref(y, i, j) = jas_matrix_get(x, i, j);
178 jas_matrix_t *jas_matrix_copy(jas_matrix_t *x)
183 y = jas_matrix_create(x->numrows_, x->numcols_);
184 for (i = 0; i < x->numrows_; ++i) {
185 for (j = 0; j < x->numcols_; ++j) {
186 *jas_matrix_getref(y, i, j) = jas_matrix_get(x, i, j);
192 /******************************************************************************\
194 \******************************************************************************/
196 void jas_seq2d_bindsub(jas_matrix_t *s, jas_matrix_t *s1, int xstart, int ystart,
199 jas_matrix_bindsub(s, s1, ystart - s1->ystart_, xstart - s1->xstart_,
200 yend - s1->ystart_ - 1, xend - s1->xstart_ - 1);
203 void jas_matrix_bindsub(jas_matrix_t *mat0, jas_matrix_t *mat1, int r0, int c0,
209 if (!(mat0->flags_ & JAS_MATRIX_REF)) {
210 jas_free(mat0->data_);
216 jas_free(mat0->rows_);
219 mat0->flags_ |= JAS_MATRIX_REF;
220 mat0->numrows_ = r1 - r0 + 1;
221 mat0->numcols_ = c1 - c0 + 1;
222 mat0->maxrows_ = mat0->numrows_;
223 mat0->rows_ = jas_malloc(mat0->maxrows_ * sizeof(jas_seqent_t *));
224 for (i = 0; i < mat0->numrows_; ++i) {
225 mat0->rows_[i] = mat1->rows_[r0 + i] + c0;
228 mat0->xstart_ = mat1->xstart_ + c0;
229 mat0->ystart_ = mat1->ystart_ + r0;
230 mat0->xend_ = mat0->xstart_ + mat0->numcols_;
231 mat0->yend_ = mat0->ystart_ + mat0->numrows_;
234 /******************************************************************************\
235 * Arithmetic operations.
236 \******************************************************************************/
238 int jas_matrix_cmp(jas_matrix_t *mat0, jas_matrix_t *mat1)
243 if (mat0->numrows_ != mat1->numrows_ || mat0->numcols_ !=
247 for (i = 0; i < mat0->numrows_; i++) {
248 for (j = 0; j < mat0->numcols_; j++) {
249 if (jas_matrix_get(mat0, i, j) != jas_matrix_get(mat1, i, j)) {
257 void jas_matrix_divpow2(jas_matrix_t *matrix, int n)
261 jas_seqent_t *rowstart;
265 rowstep = jas_matrix_rowstep(matrix);
266 for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
267 rowstart += rowstep) {
268 for (j = matrix->numcols_, data = rowstart; j > 0; --j,
270 *data = (*data >= 0) ? ((*data) >> n) :
271 (-((-(*data)) >> n));
276 void jas_matrix_clip(jas_matrix_t *matrix, jas_seqent_t minval, jas_seqent_t maxval)
281 jas_seqent_t *rowstart;
285 rowstep = jas_matrix_rowstep(matrix);
286 for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
287 rowstart += rowstep) {
289 for (j = matrix->numcols_, data = rowstart; j > 0; --j,
294 } else if (v > maxval) {
301 void jas_matrix_asr(jas_matrix_t *matrix, int n)
305 jas_seqent_t *rowstart;
310 rowstep = jas_matrix_rowstep(matrix);
311 for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
312 rowstart += rowstep) {
313 for (j = matrix->numcols_, data = rowstart; j > 0; --j,
320 void jas_matrix_asl(jas_matrix_t *matrix, int n)
324 jas_seqent_t *rowstart;
328 rowstep = jas_matrix_rowstep(matrix);
329 for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
330 rowstart += rowstep) {
331 for (j = matrix->numcols_, data = rowstart; j > 0; --j,
338 /******************************************************************************\
340 \******************************************************************************/
342 int jas_matrix_resize(jas_matrix_t *matrix, int numrows, int numcols)
347 size = numrows * numcols;
348 if (size > matrix->datasize_ || numrows > matrix->maxrows_) {
352 matrix->numrows_ = numrows;
353 matrix->numcols_ = numcols;
355 for (i = 0; i < numrows; ++i) {
356 matrix->rows_[i] = &matrix->data_[numcols * i];
362 int jas_matrix_output(jas_matrix_t *matrix, FILE *out)
368 fprintf(out, "%d %d\n", jas_matrix_numrows(matrix), jas_matrix_numcols(matrix));
369 for (i = 0; i < jas_matrix_numrows(matrix); ++i) {
370 for (j = 0; j < jas_matrix_numcols(matrix); ++j) {
371 x = jas_matrix_get(matrix, i, j);
372 fprintf(out, "%ld", JAS_CAST(long, x));
373 if (j < jas_matrix_numcols(matrix) - 1) {
382 void jas_matrix_setall(jas_matrix_t *matrix, jas_seqent_t val)
386 jas_seqent_t *rowstart;
390 rowstep = jas_matrix_rowstep(matrix);
391 for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
392 rowstart += rowstep) {
393 for (j = matrix->numcols_, data = rowstart; j > 0; --j,
401 jas_matrix_t *jas_matrix_input(FILE *in)
403 jas_matrix_t *matrix;
410 if (fscanf(in, "%d %d", &numrows, &numcols) != 2)
412 if (!(matrix = jas_matrix_create(numrows, numcols)))
415 /* Get matrix data. */
416 for (i = 0; i < jas_matrix_numrows(matrix); i++) {
417 for (j = 0; j < jas_matrix_numcols(matrix); j++) {
418 if (fscanf(in, "%ld", &x) != 1) {
419 jas_matrix_destroy(matrix);
422 jas_matrix_set(matrix, i, j, JAS_CAST(jas_seqent_t, x));