/src/jasper/src/libjasper/base/jas_seq.c
Line | Count | Source |
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 | | /******************************************************************************\ |
71 | | * Includes. |
72 | | \******************************************************************************/ |
73 | | |
74 | | #define JAS_FOR_INTERNAL_USE_ONLY |
75 | | |
76 | | #include "jasper/jas_seq.h" |
77 | | #include "jasper/jas_malloc.h" |
78 | | #include "jasper/jas_math.h" |
79 | | #include "jasper/jas_debug.h" |
80 | | |
81 | | #include <assert.h> |
82 | | |
83 | | /******************************************************************************\ |
84 | | * Constructors and destructors. |
85 | | \******************************************************************************/ |
86 | | |
87 | | jas_matrix_t *jas_seq2d_create(jas_matind_t xstart, jas_matind_t ystart, |
88 | | jas_matind_t xend, jas_matind_t yend) |
89 | 19.5M | { |
90 | 19.5M | jas_matrix_t *matrix; |
91 | 19.5M | assert(xstart <= xend && ystart <= yend); |
92 | 19.5M | if (!(matrix = jas_matrix_create(yend - ystart, xend - xstart))) { |
93 | 4 | return 0; |
94 | 4 | } |
95 | 19.5M | matrix->xstart_ = xstart; |
96 | 19.5M | matrix->ystart_ = ystart; |
97 | 19.5M | matrix->xend_ = xend; |
98 | 19.5M | matrix->yend_ = yend; |
99 | 19.5M | return matrix; |
100 | 19.5M | } |
101 | | |
102 | | jas_matrix_t *jas_matrix_create(jas_matind_t numrows, jas_matind_t numcols) |
103 | 26.4M | { |
104 | 26.4M | jas_matrix_t *matrix; |
105 | 26.4M | jas_matind_t i; |
106 | 26.4M | size_t size; |
107 | | |
108 | 26.4M | matrix = 0; |
109 | | |
110 | 26.4M | if (numrows < 0 || numcols < 0) { |
111 | 0 | return NULL; |
112 | 0 | } |
113 | | |
114 | | // matrix->datasize_ = numrows * numcols; |
115 | 26.4M | if (!jas_safe_size_mul(numrows, numcols, &size) || |
116 | 26.4M | size > UINT_FAST32_MAX) { |
117 | 0 | return NULL; |
118 | 0 | } |
119 | | |
120 | 26.4M | if (!(matrix = jas_malloc(sizeof(jas_matrix_t)))) { |
121 | 3 | return NULL; |
122 | 3 | } |
123 | 26.4M | matrix->flags_ = 0; |
124 | 26.4M | matrix->numrows_ = numrows; |
125 | 26.4M | matrix->numcols_ = numcols; |
126 | 26.4M | matrix->rows_ = 0; |
127 | 26.4M | matrix->maxrows_ = numrows; |
128 | 26.4M | matrix->data_ = 0; |
129 | 26.4M | matrix->datasize_ = size; |
130 | | |
131 | 26.4M | if (matrix->maxrows_ > 0) { |
132 | 6.98M | if (!(matrix->rows_ = jas_alloc2(matrix->maxrows_, |
133 | 6.98M | sizeof(jas_seqent_t *)))) { |
134 | 0 | goto error; |
135 | 0 | } |
136 | 6.98M | } |
137 | | |
138 | 26.4M | if (matrix->datasize_ > 0) { |
139 | 6.98M | if (!(matrix->data_ = jas_alloc2(matrix->datasize_, |
140 | 6.98M | sizeof(jas_seqent_t)))) { |
141 | 1 | goto error; |
142 | 1 | } |
143 | 6.98M | memset(matrix->data_, 0, matrix->datasize_ * sizeof(jas_seqent_t)); |
144 | 6.98M | } |
145 | | |
146 | 100M | for (i = 0; i < numrows; ++i) { |
147 | 73.9M | matrix->rows_[i] = &matrix->data_[i * matrix->numcols_]; |
148 | 73.9M | } |
149 | | |
150 | 26.4M | matrix->xstart_ = 0; |
151 | 26.4M | matrix->ystart_ = 0; |
152 | 26.4M | matrix->xend_ = matrix->numcols_; |
153 | 26.4M | matrix->yend_ = matrix->numrows_; |
154 | | |
155 | 26.4M | return matrix; |
156 | | |
157 | 1 | error: |
158 | 1 | jas_matrix_destroy(matrix); |
159 | 1 | return 0; |
160 | 26.4M | } |
161 | | |
162 | | void jas_matrix_destroy(jas_matrix_t *matrix) |
163 | 26.4M | { |
164 | 26.4M | if (matrix->data_) { |
165 | 6.98M | assert(!(matrix->flags_ & JAS_MATRIX_REF)); |
166 | 6.98M | } |
167 | 26.4M | jas_free(matrix->data_); |
168 | 26.4M | jas_free(matrix->rows_); |
169 | 26.4M | jas_free(matrix); |
170 | 26.4M | } |
171 | | |
172 | | jas_seq2d_t *jas_seq2d_copy(jas_seq2d_t *x) |
173 | 0 | { |
174 | 0 | jas_matrix_t *y; |
175 | 0 | jas_matind_t i; |
176 | 0 | jas_matind_t j; |
177 | 0 | y = jas_seq2d_create(jas_seq2d_xstart(x), jas_seq2d_ystart(x), |
178 | 0 | jas_seq2d_xend(x), jas_seq2d_yend(x)); |
179 | 0 | assert(y); |
180 | 0 | for (i = 0; i < x->numrows_; ++i) { |
181 | 0 | for (j = 0; j < x->numcols_; ++j) { |
182 | 0 | *jas_matrix_getref(y, i, j) = jas_matrix_get(x, i, j); |
183 | 0 | } |
184 | 0 | } |
185 | 0 | return y; |
186 | 0 | } |
187 | | |
188 | | jas_matrix_t *jas_matrix_copy(jas_matrix_t *x) |
189 | 0 | { |
190 | 0 | jas_matrix_t *y; |
191 | 0 | jas_matind_t i; |
192 | 0 | jas_matind_t j; |
193 | 0 | y = jas_matrix_create(x->numrows_, x->numcols_); |
194 | 0 | for (i = 0; i < x->numrows_; ++i) { |
195 | 0 | for (j = 0; j < x->numcols_; ++j) { |
196 | 0 | *jas_matrix_getref(y, i, j) = jas_matrix_get(x, i, j); |
197 | 0 | } |
198 | 0 | } |
199 | 0 | return y; |
200 | 0 | } |
201 | | |
202 | | /******************************************************************************\ |
203 | | * Bind operations. |
204 | | \******************************************************************************/ |
205 | | |
206 | | int jas_seq2d_bindsub(jas_matrix_t *s, jas_matrix_t *s1, jas_matind_t xstart, |
207 | | jas_matind_t ystart, jas_matind_t xend, jas_matind_t yend) |
208 | 19.4M | { |
209 | 19.4M | if (xstart < s1->xstart_ || ystart < s1->ystart_ || |
210 | 19.4M | xend > s1->xend_ || yend > s1->yend_) { |
211 | 0 | return -1; |
212 | 0 | } |
213 | 19.4M | return jas_matrix_bindsub(s, s1, ystart - s1->ystart_, |
214 | 19.4M | xstart - s1->xstart_, yend - s1->ystart_ - 1, xend - s1->xstart_ - 1); |
215 | 19.4M | } |
216 | | |
217 | | int jas_matrix_bindsub(jas_matrix_t *mat0, jas_matrix_t *mat1, |
218 | | jas_matind_t r0, jas_matind_t c0, jas_matind_t r1, jas_matind_t c1) |
219 | 19.4M | { |
220 | 19.4M | jas_matind_t i; |
221 | | |
222 | 19.4M | if (mat0->data_) { |
223 | 0 | if (!(mat0->flags_ & JAS_MATRIX_REF)) { |
224 | 0 | jas_free(mat0->data_); |
225 | 0 | } |
226 | 0 | mat0->data_ = 0; |
227 | 0 | mat0->datasize_ = 0; |
228 | 0 | } |
229 | 19.4M | if (mat0->rows_) { |
230 | 0 | jas_free(mat0->rows_); |
231 | 0 | mat0->rows_ = 0; |
232 | 0 | } |
233 | 19.4M | mat0->flags_ |= JAS_MATRIX_REF; |
234 | 19.4M | mat0->numrows_ = r1 - r0 + 1; |
235 | 19.4M | mat0->numcols_ = c1 - c0 + 1; |
236 | 19.4M | mat0->maxrows_ = mat0->numrows_; |
237 | 19.4M | if (!(mat0->rows_ = jas_alloc2(mat0->maxrows_, sizeof(jas_seqent_t *)))) { |
238 | 1 | return -1; |
239 | 1 | } |
240 | | |
241 | 134M | for (i = 0; i < mat0->numrows_; ++i) { |
242 | 115M | mat0->rows_[i] = mat1->rows_[r0 + i] + c0; |
243 | 115M | } |
244 | | |
245 | 19.4M | mat0->xstart_ = mat1->xstart_ + c0; |
246 | 19.4M | mat0->ystart_ = mat1->ystart_ + r0; |
247 | 19.4M | mat0->xend_ = mat0->xstart_ + mat0->numcols_; |
248 | 19.4M | mat0->yend_ = mat0->ystart_ + mat0->numrows_; |
249 | | |
250 | 19.4M | return 0; |
251 | 19.4M | } |
252 | | |
253 | | /******************************************************************************\ |
254 | | * Arithmetic operations. |
255 | | \******************************************************************************/ |
256 | | |
257 | | int jas_matrix_cmp(jas_matrix_t *mat0, jas_matrix_t *mat1) |
258 | 0 | { |
259 | 0 | jas_matind_t i; |
260 | 0 | jas_matind_t j; |
261 | |
|
262 | 0 | if (mat0->numrows_ != mat1->numrows_ || mat0->numcols_ != |
263 | 0 | mat1->numcols_) { |
264 | 0 | return 1; |
265 | 0 | } |
266 | 0 | for (i = 0; i < mat0->numrows_; i++) { |
267 | 0 | for (j = 0; j < mat0->numcols_; j++) { |
268 | 0 | if (jas_matrix_get(mat0, i, j) != jas_matrix_get(mat1, i, j)) { |
269 | 0 | return 1; |
270 | 0 | } |
271 | 0 | } |
272 | 0 | } |
273 | 0 | return 0; |
274 | 0 | } |
275 | | |
276 | | void jas_matrix_divpow2(jas_matrix_t *matrix, unsigned n) |
277 | 0 | { |
278 | 0 | jas_matind_t i; |
279 | 0 | jas_matind_t j; |
280 | 0 | jas_seqent_t *rowstart; |
281 | 0 | jas_matind_t rowstep; |
282 | 0 | jas_seqent_t *data; |
283 | |
|
284 | 0 | if (jas_matrix_numrows(matrix) > 0 && jas_matrix_numcols(matrix) > 0) { |
285 | 0 | assert(matrix->rows_); |
286 | 0 | rowstep = jas_matrix_rowstep(matrix); |
287 | 0 | for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i, |
288 | 0 | rowstart += rowstep) { |
289 | 0 | for (j = matrix->numcols_, data = rowstart; j > 0; --j, |
290 | 0 | ++data) { |
291 | 0 | *data = (*data >= 0) ? ((*data) >> n) : |
292 | 0 | (-((-(*data)) >> n)); |
293 | 0 | } |
294 | 0 | } |
295 | 0 | } |
296 | 0 | } |
297 | | |
298 | | void jas_matrix_clip(jas_matrix_t *matrix, jas_seqent_t minval, |
299 | | jas_seqent_t maxval) |
300 | 20.3k | { |
301 | 20.3k | jas_matind_t i; |
302 | 20.3k | jas_matind_t j; |
303 | 20.3k | jas_seqent_t v; |
304 | 20.3k | jas_seqent_t *rowstart; |
305 | 20.3k | jas_seqent_t *data; |
306 | 20.3k | jas_matind_t rowstep; |
307 | | |
308 | 20.3k | if (jas_matrix_numrows(matrix) > 0 && jas_matrix_numcols(matrix) > 0) { |
309 | 19.7k | assert(matrix->rows_); |
310 | 19.7k | rowstep = jas_matrix_rowstep(matrix); |
311 | 3.06M | for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i, |
312 | 3.04M | rowstart += rowstep) { |
313 | 3.04M | data = rowstart; |
314 | 464M | for (j = matrix->numcols_, data = rowstart; j > 0; --j, |
315 | 461M | ++data) { |
316 | 461M | v = *data; |
317 | 461M | if (v < minval) { |
318 | 53.4M | *data = minval; |
319 | 407M | } else if (v > maxval) { |
320 | 53.3M | *data = maxval; |
321 | 53.3M | } |
322 | 461M | } |
323 | 3.04M | } |
324 | 19.7k | } |
325 | 20.3k | } |
326 | | |
327 | | void jas_matrix_asr(jas_matrix_t *matrix, unsigned n) |
328 | 0 | { |
329 | 0 | jas_matind_t i; |
330 | 0 | jas_matind_t j; |
331 | 0 | jas_seqent_t *rowstart; |
332 | 0 | jas_matind_t rowstep; |
333 | 0 | jas_seqent_t *data; |
334 | |
|
335 | 0 | if (jas_matrix_numrows(matrix) > 0 && jas_matrix_numcols(matrix) > 0) { |
336 | 0 | assert(matrix->rows_); |
337 | 0 | rowstep = jas_matrix_rowstep(matrix); |
338 | 0 | for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i, |
339 | 0 | rowstart += rowstep) { |
340 | 0 | for (j = matrix->numcols_, data = rowstart; j > 0; --j, |
341 | 0 | ++data) { |
342 | | //*data >>= n; |
343 | 0 | *data = jas_seqent_asr(*data, n); |
344 | 0 | } |
345 | 0 | } |
346 | 0 | } |
347 | 0 | } |
348 | | |
349 | | void jas_matrix_asl(jas_matrix_t *matrix, unsigned n) |
350 | 145k | { |
351 | 145k | jas_matind_t i; |
352 | 145k | jas_matind_t j; |
353 | 145k | jas_seqent_t *rowstart; |
354 | 145k | jas_matind_t rowstep; |
355 | 145k | jas_seqent_t *data; |
356 | | |
357 | 145k | if (jas_matrix_numrows(matrix) > 0 && jas_matrix_numcols(matrix) > 0) { |
358 | 145k | assert(matrix->rows_); |
359 | 145k | rowstep = jas_matrix_rowstep(matrix); |
360 | 4.98M | for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i, |
361 | 4.83M | rowstart += rowstep) { |
362 | 1.53G | for (j = matrix->numcols_, data = rowstart; j > 0; --j, |
363 | 1.52G | ++data) { |
364 | | //*data <<= n; |
365 | 1.52G | *data = jas_seqent_asl(*data, n); |
366 | 1.52G | } |
367 | 4.83M | } |
368 | 145k | } |
369 | 145k | } |
370 | | |
371 | | /******************************************************************************\ |
372 | | * Code. |
373 | | \******************************************************************************/ |
374 | | |
375 | | int jas_matrix_resize(jas_matrix_t *matrix, jas_matind_t numrows, |
376 | | jas_matind_t numcols) |
377 | 0 | { |
378 | 0 | jas_matind_t size; |
379 | 0 | jas_matind_t i; |
380 | |
|
381 | 0 | size = numrows * numcols; |
382 | 0 | if (size > matrix->datasize_ || numrows > matrix->maxrows_) { |
383 | 0 | return -1; |
384 | 0 | } |
385 | | |
386 | 0 | matrix->numrows_ = numrows; |
387 | 0 | matrix->numcols_ = numcols; |
388 | |
|
389 | 0 | for (i = 0; i < numrows; ++i) { |
390 | 0 | matrix->rows_[i] = &matrix->data_[numcols * i]; |
391 | 0 | } |
392 | |
|
393 | 0 | return 0; |
394 | 0 | } |
395 | | |
396 | | void jas_matrix_setall(jas_matrix_t *matrix, jas_seqent_t val) |
397 | 0 | { |
398 | 0 | jas_matind_t i; |
399 | 0 | jas_matind_t j; |
400 | 0 | jas_seqent_t *rowstart; |
401 | 0 | jas_matind_t rowstep; |
402 | 0 | jas_seqent_t *data; |
403 | |
|
404 | 0 | if (jas_matrix_numrows(matrix) > 0 && jas_matrix_numcols(matrix) > 0) { |
405 | 0 | assert(matrix->rows_); |
406 | 0 | rowstep = jas_matrix_rowstep(matrix); |
407 | 0 | for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i, |
408 | 0 | rowstart += rowstep) { |
409 | 0 | for (j = matrix->numcols_, data = rowstart; j > 0; --j, |
410 | 0 | ++data) { |
411 | 0 | *data = val; |
412 | 0 | } |
413 | 0 | } |
414 | 0 | } |
415 | 0 | } |