Debian Codegraph @

converter/other/jpeg2000/libjasper/base/jas_seq.c

raw
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 Software License
11 *
12 * IMAGE POWER JPEG-2000 PUBLIC LICENSE
13 * ************************************
14 *
15 * GRANT:
16 *
17 * Permission is hereby granted, free of charge, to any person (the "User")
18 * obtaining a copy of this software and associated documentation, to deal
19 * in the JasPer Software without restriction, including without limitation
20 * the right to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the JasPer Software (in source and binary forms),
22 * and to permit persons to whom the JasPer Software is furnished to do so,
23 * provided further that the License Conditions below are met.
24 *
25 * License Conditions
26 * ******************
27 *
28 * A. Redistributions of source code must retain the above copyright notice,
29 * and this list of conditions, and the following disclaimer.
30 *
31 * B. Redistributions in binary form must reproduce the above copyright
32 * notice, and this list of conditions, and the following disclaimer in
33 * the documentation and/or other materials provided with the distribution.
34 *
35 * C. Neither the name of Image Power, Inc. nor any other contributor
36 * (including, but not limited to, the University of British Columbia and
37 * Michael David Adams) may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
39 *
40 * D. User agrees that it shall not commence any action against Image Power,
41 * Inc., the University of British Columbia, Michael David Adams, or any
42 * other contributors (collectively "Licensors") for infringement of any
43 * intellectual property rights ("IPR") held by the User in respect of any
44 * technology that User owns or has a right to license or sublicense and
45 * which is an element required in order to claim compliance with ISO/IEC
46 * 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property
47 * rights worldwide arising under statutory or common law, and whether
48 * or not perfected, including, without limitation, all (i) patents and
49 * patent applications owned or licensable by User; (ii) rights associated
50 * with works of authorship including copyrights, copyright applications,
51 * copyright registrations, mask work rights, mask work applications,
52 * mask work registrations; (iii) rights relating to the protection of
53 * trade secrets and confidential information; (iv) any right analogous
54 * to those set forth in subsections (i), (ii), or (iii) and any other
55 * proprietary rights relating to intangible property (other than trademark,
56 * trade dress, or service mark rights); and (v) divisions, continuations,
57 * renewals, reissues and extensions of the foregoing (as and to the extent
58 * applicable) now existing, hereafter filed, issued or acquired.
59 *
60 * E. If User commences an infringement action against any Licensor(s) then
61 * such Licensor(s) shall have the right to terminate User's license and
62 * all sublicenses that have been granted hereunder by User to other parties.
63 *
64 * F. This software is for use only in hardware or software products that
65 * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license
66 * or right to this Software is granted for products that do not comply
67 * with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased
68 * from the ISO.
69 *
70 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
71 * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
72 * THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
73 * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
74 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
75 * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
76 * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING
77 * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
78 * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
79 * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
80 * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
81 * IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
82 * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
83 * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
84 * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
85 * REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
86 * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
87 * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
88 * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
89 * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
90 * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
91 * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
92 * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
93 * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
94 * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
95 * OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
96 * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
97 * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
98 * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
99 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
100 * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
101 * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
102 * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
103 * ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
104 * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT
105 * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
106 * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
107 * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
108 * NOTICE SPECIFIED IN THIS SECTION.
109 *
110 * __END_OF_JASPER_LICENSE__
111 */
112
113 /*
114 * Sequence/Matrix Library
115 *
116 * $Id$
117 */
118
119 /******************************************************************************\
120 * Includes.
121 \******************************************************************************/
122
123 #include <stdlib.h>
124 #include <assert.h>
125 #include <math.h>
126
127 #include "jasper/jas_seq.h"
128 #include "jasper/jas_malloc.h"
129 #include "jasper/jas_math.h"
130
131 /******************************************************************************\
132 * Constructors and destructors.
133 \******************************************************************************/
134
135 jas_matrix_t *jas_seq2d_create(int xstart, int ystart, int xend, int yend)
136 {
137 jas_matrix_t *matrix;
138 assert(xstart <= xend && ystart <= yend);
139 if (!(matrix = jas_matrix_create(yend - ystart, xend - xstart))) {
140 return 0;
141 }
142 matrix->xstart_ = xstart;
143 matrix->ystart_ = ystart;
144 matrix->xend_ = xend;
145 matrix->yend_ = yend;
146 return matrix;
147 }
148
149 jas_matrix_t *jas_matrix_create(int numrows, int numcols)
150 {
151 jas_matrix_t *matrix;
152 int i;
153
154 if (!(matrix = jas_malloc(sizeof(jas_matrix_t)))) {
155 return 0;
156 }
157 matrix->flags_ = 0;
158 matrix->numrows_ = numrows;
159 matrix->numcols_ = numcols;
160 matrix->rows_ = 0;
161 matrix->maxrows_ = numrows;
162 matrix->data_ = 0;
163 matrix->datasize_ = numrows * numcols;
164
165 if (matrix->maxrows_ > 0) {
166 if (!(matrix->rows_ = jas_malloc(matrix->maxrows_ *
167 sizeof(jas_seqent_t *)))) {
168 jas_matrix_destroy(matrix);
169 return 0;
170 }
171 }
172
173 if (matrix->datasize_ > 0) {
174 if (!(matrix->data_ = jas_malloc(matrix->datasize_ *
175 sizeof(jas_seqent_t)))) {
176 jas_matrix_destroy(matrix);
177 return 0;
178 }
179 }
180
181 for (i = 0; i < numrows; ++i) {
182 matrix->rows_[i] = &matrix->data_[i * matrix->numcols_];
183 }
184
185 for (i = 0; i < matrix->datasize_; ++i) {
186 matrix->data_[i] = 0;
187 }
188
189 matrix->xstart_ = 0;
190 matrix->ystart_ = 0;
191 matrix->xend_ = matrix->numcols_;
192 matrix->yend_ = matrix->numrows_;
193
194 return matrix;
195 }
196
197 void jas_matrix_destroy(jas_matrix_t *matrix)
198 {
199 if (matrix->data_) {
200 assert(!(matrix->flags_ & JAS_MATRIX_REF));
201 jas_free(matrix->data_);
202 matrix->data_ = 0;
203 }
204 if (matrix->rows_) {
205 jas_free(matrix->rows_);
206 matrix->rows_ = 0;
207 }
208 jas_free(matrix);
209 }
210
211 jas_seq2d_t *jas_seq2d_copy(jas_seq2d_t *x)
212 {
213 jas_matrix_t *y;
214 int i;
215 int j;
216 y = jas_seq2d_create(jas_seq2d_xstart(x), jas_seq2d_ystart(x), jas_seq2d_xend(x),
217 jas_seq2d_yend(x));
218 assert(y);
219 for (i = 0; i < x->numrows_; ++i) {
220 for (j = 0; j < x->numcols_; ++j) {
221 *jas_matrix_getref(y, i, j) = jas_matrix_get(x, i, j);
222 }
223 }
224 return y;
225 }
226
227 jas_matrix_t *jas_matrix_copy(jas_matrix_t *x)
228 {
229 jas_matrix_t *y;
230 int i;
231 int j;
232 y = jas_matrix_create(x->numrows_, x->numcols_);
233 for (i = 0; i < x->numrows_; ++i) {
234 for (j = 0; j < x->numcols_; ++j) {
235 *jas_matrix_getref(y, i, j) = jas_matrix_get(x, i, j);
236 }
237 }
238 return y;
239 }
240
241 /******************************************************************************\
242 * Bind operations.
243 \******************************************************************************/
244
245 void jas_seq2d_bindsub(jas_matrix_t *s, jas_matrix_t *s1, int xstart, int ystart,
246 int xend, int yend)
247 {
248 jas_matrix_bindsub(s, s1, ystart - s1->ystart_, xstart - s1->xstart_,
249 yend - s1->ystart_ - 1, xend - s1->xstart_ - 1);
250 }
251
252 void jas_matrix_bindsub(jas_matrix_t *mat0, jas_matrix_t *mat1, int r0, int c0,
253 int r1, int c1)
254 {
255 int i;
256
257 if (mat0->data_) {
258 if (!(mat0->flags_ & JAS_MATRIX_REF)) {
259 jas_free(mat0->data_);
260 }
261 mat0->data_ = 0;
262 mat0->datasize_ = 0;
263 }
264 if (mat0->rows_) {
265 jas_free(mat0->rows_);
266 mat0->rows_ = 0;
267 }
268 mat0->flags_ |= JAS_MATRIX_REF;
269 mat0->numrows_ = r1 - r0 + 1;
270 mat0->numcols_ = c1 - c0 + 1;
271 mat0->maxrows_ = mat0->numrows_;
272 mat0->rows_ = jas_malloc(mat0->maxrows_ * sizeof(jas_seqent_t *));
273 for (i = 0; i < mat0->numrows_; ++i) {
274 mat0->rows_[i] = mat1->rows_[r0 + i] + c0;
275 }
276
277 mat0->xstart_ = mat1->xstart_ + c0;
278 mat0->ystart_ = mat1->ystart_ + r0;
279 mat0->xend_ = mat0->xstart_ + mat0->numcols_;
280 mat0->yend_ = mat0->ystart_ + mat0->numrows_;
281 }
282
283 /******************************************************************************\
284 * Arithmetic operations.
285 \******************************************************************************/
286
287 int jas_matrix_cmp(jas_matrix_t *mat0, jas_matrix_t *mat1)
288 {
289 int i;
290 int j;
291
292 if (mat0->numrows_ != mat1->numrows_ || mat0->numcols_ !=
293 mat1->numcols_) {
294 return 1;
295 }
296 for (i = 0; i < mat0->numrows_; i++) {
297 for (j = 0; j < mat0->numcols_; j++) {
298 if (jas_matrix_get(mat0, i, j) != jas_matrix_get(mat1, i, j)) {
299 return 1;
300 }
301 }
302 }
303 return 0;
304 }
305
306 void jas_matrix_divpow2(jas_matrix_t *matrix, int n)
307 {
308 int i;
309 int j;
310 jas_seqent_t *rowstart;
311 int rowstep;
312 jas_seqent_t *data;
313
314 rowstep = jas_matrix_rowstep(matrix);
315 for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
316 rowstart += rowstep) {
317 for (j = matrix->numcols_, data = rowstart; j > 0; --j,
318 ++data) {
319 *data = (*data >= 0) ? ((*data) >> n) :
320 (-((-(*data)) >> n));
321 }
322 }
323 }
324
325 void jas_matrix_clip(jas_matrix_t *matrix, jas_seqent_t minval, jas_seqent_t maxval)
326 {
327 int i;
328 int j;
329 jas_seqent_t v;
330 jas_seqent_t *rowstart;
331 jas_seqent_t *data;
332 int rowstep;
333
334 rowstep = jas_matrix_rowstep(matrix);
335 for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
336 rowstart += rowstep) {
337 data = rowstart;
338 for (j = matrix->numcols_, data = rowstart; j > 0; --j,
339 ++data) {
340 v = *data;
341 if (v < minval) {
342 *data = minval;
343 } else if (v > maxval) {
344 *data = maxval;
345 }
346 }
347 }
348 }
349
350 void jas_matrix_asr(jas_matrix_t *matrix, int n)
351 {
352 int i;
353 int j;
354 jas_seqent_t *rowstart;
355 int rowstep;
356 jas_seqent_t *data;
357
358 assert(n >= 0);
359 rowstep = jas_matrix_rowstep(matrix);
360 for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
361 rowstart += rowstep) {
362 for (j = matrix->numcols_, data = rowstart; j > 0; --j,
363 ++data) {
364 *data >>= n;
365 }
366 }
367 }
368
369 void jas_matrix_asl(jas_matrix_t *matrix, int n)
370 {
371 int i;
372 int j;
373 jas_seqent_t *rowstart;
374 int rowstep;
375 jas_seqent_t *data;
376
377 rowstep = jas_matrix_rowstep(matrix);
378 for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
379 rowstart += rowstep) {
380 for (j = matrix->numcols_, data = rowstart; j > 0; --j,
381 ++data) {
382 *data <<= n;
383 }
384 }
385 }
386
387 /******************************************************************************\
388 * Code.
389 \******************************************************************************/
390
391 int jas_matrix_resize(jas_matrix_t *matrix, int numrows, int numcols)
392 {
393 int size;
394 int i;
395
396 size = numrows * numcols;
397 if (size > matrix->datasize_ || numrows > matrix->maxrows_) {
398 return -1;
399 }
400
401 matrix->numrows_ = numrows;
402 matrix->numcols_ = numcols;
403
404 for (i = 0; i < numrows; ++i) {
405 matrix->rows_[i] = &matrix->data_[numcols * i];
406 }
407
408 return 0;
409 }
410
411 int jas_matrix_output(jas_matrix_t *matrix, FILE *out)
412 {
413 int i;
414 int j;
415 jas_seqent_t x;
416
417 fprintf(out, "%d %d\n",
418 (int)jas_matrix_numrows(matrix), (int)jas_matrix_numcols(matrix));
419 for (i = 0; i < jas_matrix_numrows(matrix); ++i) {
420 for (j = 0; j < jas_matrix_numcols(matrix); ++j) {
421 x = jas_matrix_get(matrix, i, j);
422 fprintf(out, "%ld", JAS_CAST(long, x));
423 if (j < jas_matrix_numcols(matrix) - 1) {
424 fprintf(out, " ");
425 }
426 }
427 fprintf(out, "\n");
428 }
429 return 0;
430 }
431
432 void jas_matrix_setall(jas_matrix_t *matrix, jas_seqent_t val)
433 {
434 int i;
435 int j;
436 jas_seqent_t *rowstart;
437 int rowstep;
438 jas_seqent_t *data;
439
440 rowstep = jas_matrix_rowstep(matrix);
441 for (i = matrix->numrows_, rowstart = matrix->rows_[0]; i > 0; --i,
442 rowstart += rowstep) {
443 for (j = matrix->numcols_, data = rowstart; j > 0; --j,
444 ++data) {
445 *data = val;
446 }
447 }
448 }
449
450 jas_matrix_t *jas_matrix_input(FILE *in)
451 {
452 jas_matrix_t *matrix;
453 int i;
454 int j;
455 long x;
456 int numrows;
457 int numcols;
458
459 if (fscanf(in, "%d %d", &numrows, &numcols) != 2)
460 return 0;
461 if (!(matrix = jas_matrix_create(numrows, numcols)))
462 return 0;
463
464 /* Get matrix data. */
465 for (i = 0; i < jas_matrix_numrows(matrix); i++) {
466 for (j = 0; j < jas_matrix_numcols(matrix); j++) {
467 if (fscanf(in, "%ld", &x) != 1) {
468 jas_matrix_destroy(matrix);
469 return 0;
470 }
471 jas_matrix_set(matrix, i, j, JAS_CAST(jas_seqent_t, x));
472 }
473 }
474
475 return matrix;
476 }