Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
244 chris 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
 * Fixed-Point Number Class
115
 *
116
 * $Id: jas_fix.h,v 1.1 2003/05/15 01:30:32 ace Exp $
117
 */
118
 
119
#ifndef JAS_FIX_H
120
#define JAS_FIX_H
121
 
122
/******************************************************************************\
123
* Includes.
124
\******************************************************************************/
125
 
126
#include <stdio.h>
127
#include <stdlib.h>
128
#include <math.h>
129
 
130
#include "jas_config.h"
131
#include "jas_types.h"
132
 
133
#ifdef __cplusplus
134
extern "C" {
135
#endif
136
 
137
/******************************************************************************\
138
* Constants.
139
\******************************************************************************/
140
 
141
/* The representation of the value zero. */
142
#define JAS_FIX_ZERO(fix_t, fracbits) \
143
        JAS_CAST(fix_t, 0)
144
 
145
/* The representation of the value one. */
146
#define JAS_FIX_ONE(fix_t, fracbits) \
147
        (JAS_CAST(fix_t, 1) << (fracbits))
148
 
149
/* The representation of the value one half. */
150
#define JAS_FIX_HALF(fix_t, fracbits) \
151
        (JAS_CAST(fix_t, 1) << ((fracbits) - 1))
152
 
153
/******************************************************************************\
154
* Conversion operations.
155
\******************************************************************************/
156
 
157
/* Convert an int to a fixed-point number. */
158
#define JAS_INTTOFIX(fix_t, fracbits, x) \
159
        JAS_CAST(fix_t, (x) << (fracbits))
160
 
161
/* Convert a fixed-point number to an int. */
162
#define JAS_FIXTOINT(fix_t, fracbits, x) \
163
        JAS_CAST(int, (x) >> (fracbits))
164
 
165
/* Convert a fixed-point number to a double. */
166
#define JAS_FIXTODBL(fix_t, fracbits, x) \
167
        (JAS_CAST(double, x) / (JAS_CAST(fix_t, 1) << (fracbits)))
168
 
169
/* Convert a double to a fixed-point number. */
170
#define JAS_DBLTOFIX(fix_t, fracbits, x) \
171
        JAS_CAST(fix_t, ((x) * JAS_CAST(double, JAS_CAST(fix_t, 1) << (fracbits))))
172
 
173
/******************************************************************************\
174
* Basic arithmetic operations.
175
* All other arithmetic operations are synthesized from these basic operations.
176
* There are three macros for each type of arithmetic operation.
177
* One macro always performs overflow/underflow checking, one never performs
178
* overflow/underflow checking, and one is generic with its behavior
179
* depending on compile-time flags.
180
* Only the generic macros should be invoked directly by application code.
181
\******************************************************************************/
182
 
183
/* Calculate the sum of two fixed-point numbers. */
184
#if !defined(DEBUG_OVERFLOW)
185
#define JAS_FIX_ADD                     JAS_FIX_ADD_FAST
186
#else
187
#define JAS_FIX_ADD                     JAS_FIX_ADD_OFLOW
188
#endif
189
 
190
/* Calculate the sum of two fixed-point numbers without overflow checking. */
191
#define JAS_FIX_ADD_FAST(fix_t, fracbits, x, y) ((x) + (y))
192
 
193
/* Calculate the sum of two fixed-point numbers with overflow checking. */
194
#define JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \
195
        ((x) >= 0) ? \
196
          (((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \
197
          ((x) + (y))) : \
198
          (((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \
199
          (x) + (y)))
200
 
201
/* Calculate the product of two fixed-point numbers. */
202
#if !defined(DEBUG_OVERFLOW)
203
#define JAS_FIX_MUL                     JAS_FIX_MUL_FAST
204
#else
205
#define JAS_FIX_MUL                     JAS_FIX_MUL_OFLOW
206
#endif
207
 
208
/* Calculate the product of two fixed-point numbers without overflow
209
  checking. */
210
#define JAS_FIX_MUL_FAST(fix_t, fracbits, bigfix_t, x, y) \
211
        JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y)) >> \
212
          (fracbits))
213
 
214
/* Calculate the product of two fixed-point numbers with overflow
215
  checking. */
216
#define JAS_FIX_MUL_OFLOW(fix_t, fracbits, bigfix_t, x, y) \
217
        ((JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> (fracbits)) == \
218
          JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
219
          (fracbits))) ? \
220
          JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
221
          (fracbits))) : JAS_FIX_OFLOW())
222
 
223
/* Calculate the product of a fixed-point number and an int. */
224
#if !defined(DEBUG_OVERFLOW)
225
#define JAS_FIX_MULBYINT        JAS_FIX_MULBYINT_FAST
226
#else
227
#define JAS_FIX_MULBYINT        JAS_FIX_MULBYINT_OFLOW
228
#endif
229
 
230
/* Calculate the product of a fixed-point number and an int without overflow
231
  checking. */
232
#define JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \
233
        JAS_CAST(fix_t, ((x) * (y)))
234
 
235
/* Calculate the product of a fixed-point number and an int with overflow
236
  checking. */
237
#define JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \
238
        JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)
239
 
240
/* Calculate the quotient of two fixed-point numbers. */
241
#if !defined(DEBUG_OVERFLOW)
242
#define JAS_FIX_DIV                     JAS_FIX_DIV_FAST
243
#else
244
#define JAS_FIX_DIV                     JAS_FIX_DIV_UFLOW
245
#endif
246
 
247
/* Calculate the quotient of two fixed-point numbers without underflow
248
  checking. */
249
#define JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y) \
250
        JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) << (fracbits)) / (y))
251
 
252
/* Calculate the quotient of two fixed-point numbers with underflow
253
  checking. */
254
#define JAS_FIX_DIV_UFLOW(fix_t, fracbits, bigfix_t, x, y) \
255
        JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y)
256
 
257
/* Negate a fixed-point number. */
258
#if !defined(DEBUG_OVERFLOW)
259
#define JAS_FIX_NEG                     JAS_FIX_NEG_FAST
260
#else
261
#define JAS_FIX_NEG                     JAS_FIX_NEG_OFLOW
262
#endif
263
 
264
/* Negate a fixed-point number without overflow checking. */
265
#define JAS_FIX_NEG_FAST(fix_t, fracbits, x) \
266
        (-(x))
267
 
268
/* Negate a fixed-point number with overflow checking. */
269
/* Yes, overflow is actually possible for two's complement representations,
270
  although highly unlikely to occur. */
271
#define JAS_FIX_NEG_OFLOW(fix_t, fracbits, x) \
272
        (((x) < 0) ? (-(x) > 0 || JAS_FIX_OFLOW(), -(x)) : (-(x)))
273
 
274
/* Perform an arithmetic shift left of a fixed-point number. */
275
#if !defined(DEBUG_OVERFLOW)
276
#define JAS_FIX_ASL                     JAS_FIX_ASL_FAST
277
#else
278
#define JAS_FIX_ASL                     JAS_FIX_ASL_OFLOW
279
#endif
280
 
281
/* Perform an arithmetic shift left of a fixed-point number without overflow
282
  checking. */
283
#define JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \
284
        ((x) << (n))
285
 
286
/* Perform an arithmetic shift left of a fixed-point number with overflow
287
  checking. */
288
#define JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \
289
        ((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))
290
 
291
/* Perform an arithmetic shift right of a fixed-point number. */
292
#if !defined(DEBUG_OVERFLOW)
293
#define JAS_FIX_ASR                     JAS_FIX_ASR_FAST
294
#else
295
#define JAS_FIX_ASR                     JAS_FIX_ASR_UFLOW
296
#endif
297
 
298
/* Perform an arithmetic shift right of a fixed-point number without underflow
299
  checking. */
300
#define JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \
301
        ((x) >> (n))
302
 
303
/* Perform an arithmetic shift right of a fixed-point number with underflow
304
  checking. */
305
#define JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \
306
        JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)
307
 
308
/******************************************************************************\
309
* Other basic arithmetic operations.
310
\******************************************************************************/
311
 
312
/* Calculate the difference between two fixed-point numbers. */
313
#define JAS_FIX_SUB(fix_t, fracbits, x, y) \
314
        JAS_FIX_ADD(fix_t, fracbits, x, JAS_FIX_NEG(fix_t, fracbits, y))
315
 
316
/* Add one fixed-point number to another. */
317
#define JAS_FIX_PLUSEQ(fix_t, fracbits, x, y) \
318
        ((x) = JAS_FIX_ADD(fix_t, fracbits, x, y))
319
 
320
/* Subtract one fixed-point number from another. */
321
#define JAS_FIX_MINUSEQ(fix_t, fracbits, x, y) \
322
        ((x) = JAS_FIX_SUB(fix_t, fracbits, x, y))
323
 
324
/* Multiply one fixed-point number by another. */
325
#define JAS_FIX_MULEQ(fix_t, fracbits, bigfix_t, x, y) \
326
        ((x) = JAS_FIX_MUL(fix_t, fracbits, bigfix_t, x, y))
327
 
328
/******************************************************************************\
329
* Miscellaneous operations.
330
\******************************************************************************/
331
 
332
/* Calculate the absolute value of a fixed-point number. */
333
#define JAS_FIX_ABS(fix_t, fracbits, x) \
334
        (((x) >= 0) ? (x) : (JAS_FIX_NEG(fix_t, fracbits, x)))
335
 
336
/* Is a fixed-point number an integer? */
337
#define JAS_FIX_ISINT(fix_t, fracbits, x) \
338
        (JAS_FIX_FLOOR(fix_t, fracbits, x) == (x))
339
 
340
/* Get the sign of a fixed-point number. */
341
#define JAS_FIX_SGN(fix_t, fracbits, x) \
342
        ((x) >= 0 ? 1 : (-1))
343
 
344
/******************************************************************************\
345
* Relational operations.
346
\******************************************************************************/
347
 
348
/* Compare two fixed-point numbers. */
349
#define JAS_FIX_CMP(fix_t, fracbits, x, y) \
350
        ((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))
351
 
352
/* Less than. */
353
#define JAS_FIX_LT(fix_t, fracbits, x, y) \
354
        ((x) < (y))
355
 
356
/* Less than or equal. */
357
#define JAS_FIX_LTE(fix_t, fracbits, x, y) \
358
        ((x) <= (y))
359
 
360
/* Greater than. */
361
#define JAS_FIX_GT(fix_t, fracbits, x, y) \
362
        ((x) > (y))
363
 
364
/* Greater than or equal. */
365
#define JAS_FIX_GTE(fix_t, fracbits, x, y) \
366
        ((x) >= (y))
367
 
368
/******************************************************************************\
369
* Rounding functions.
370
\******************************************************************************/
371
 
372
/* Round a fixed-point number to the nearest integer. */
373
#define JAS_FIX_ROUND(fix_t, fracbits, x) \
374
        (((x) < 0) ? JAS_FIX_FLOOR(fix_t, fracbits, JAS_FIX_ADD(fix_t, fracbits, \
375
          (x), JAS_FIX_HALF(fix_t, fracbits))) : \
376
          JAS_FIX_NEG(fix_t, fracbits, JAS_FIX_FLOOR(fix_t, fracbits, \
377
          JAS_FIX_ADD(fix_t, fracbits, (-(x)), JAS_FIX_HALF(fix_t, fracbits)))))
378
 
379
/* Round a fixed-point number to the nearest integer in the direction of
380
  negative infinity (i.e., the floor function). */
381
#define JAS_FIX_FLOOR(fix_t, fracbits, x) \
382
        ((x) & (~((JAS_CAST(fix_t, 1) << (fracbits)) - 1)))
383
 
384
/* Round a fixed-point number to the nearest integer in the direction
385
  of zero. */
386
#define JAS_FIX_TRUNC(fix_t, fracbits, x) \
387
        (((x) >= 0) ? JAS_FIX_FLOOR(fix_t, fracbits, x) : \
388
          JAS_FIX_CEIL(fix_t, fracbits, x))
389
 
390
/******************************************************************************\
391
* The below macros are for internal library use only.  Do not invoke them
392
* directly in application code.
393
\******************************************************************************/
394
 
395
/* Handle overflow. */
396
#define JAS_FIX_OFLOW() \
397
        fprintf(stderr, "overflow error: file %s, line %d\n", __FILE__, __LINE__)
398
 
399
/* Handle underflow. */
400
#define JAS_FIX_UFLOW() \
401
        fprintf(stderr, "underflow error: file %s, line %d\n", __FILE__, __LINE__)
402
 
403
#ifdef __cplusplus
404
}
405
#endif
406
 
407
#endif