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-2003 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
 * $Id: jpc_t1cod.c,v 1.1 2003/05/15 01:30:32 ace Exp $
115
 */
116
 
117
/******************************************************************************\
118
* Includes.
119
\******************************************************************************/
120
 
121
#include <stdio.h>
122
#include <stdlib.h>
123
#include <assert.h>
124
#include <math.h>
125
 
126
#include "jas_types.h"
127
#include "jas_math.h"
128
 
129
#include "jpc_bs.h"
130
#include "jpc_dec.h"
131
#include "jpc_cs.h"
132
#include "jpc_mqcod.h"
133
#include "jpc_t1cod.h"
134
#include "jpc_tsfb.h"
135
 
136
double jpc_pow2i(int n);
137
 
138
/******************************************************************************\
139
* Global data.
140
\******************************************************************************/
141
 
142
int jpc_zcctxnolut[4 * 256];
143
int jpc_spblut[256];
144
int jpc_scctxnolut[256];
145
int jpc_magctxnolut[4096];
146
 
147
jpc_fix_t jpc_signmsedec[1 << JPC_NMSEDEC_BITS];
148
jpc_fix_t jpc_refnmsedec[1 << JPC_NMSEDEC_BITS];
149
jpc_fix_t jpc_signmsedec0[1 << JPC_NMSEDEC_BITS];
150
jpc_fix_t jpc_refnmsedec0[1 << JPC_NMSEDEC_BITS];
151
 
152
jpc_mqctx_t jpc_mqctxs[JPC_NUMCTXS];
153
 
154
/******************************************************************************\
155
*
156
\******************************************************************************/
157
 
158
void jpc_initmqctxs(void);
159
 
160
/******************************************************************************\
161
* Code.
162
\******************************************************************************/
163
 
164
int JPC_PASSTYPE(int passno)
165
{
166
        int passtype;
167
        switch (passno % 3) {
168
        case 0:
169
                passtype = JPC_CLNPASS;
170
                break;
171
        case 1:
172
                passtype = JPC_SIGPASS;
173
                break;
174
        case 2:
175
                passtype = JPC_REFPASS;
176
                break;
177
        default:
178
                passtype = -1;
179
                assert(0);
180
                break;
181
        }
182
        return passtype;
183
}
184
 
185
int JPC_NOMINALGAIN(int qmfbid, int numlvls, int lvlno, int orient)
186
{
187
        /* Avoid compiler warnings about unused parameters. */
188
        numlvls = 0;
189
 
190
if (qmfbid == JPC_COX_INS) {
191
        return 0;
192
}
193
        assert(qmfbid == JPC_COX_RFT);
194
        if (lvlno == 0) {
195
                assert(orient == JPC_TSFB_LL);
196
                return 0;
197
        } else {
198
                switch (orient) {
199
                case JPC_TSFB_LH:
200
                case JPC_TSFB_HL:
201
                        return 1;
202
                        break;
203
                case JPC_TSFB_HH:
204
                        return 2;
205
                        break;
206
                }
207
        }
208
        abort();
209
}
210
 
211
/******************************************************************************\
212
* Coding pass related functions.
213
\******************************************************************************/
214
 
215
int JPC_SEGTYPE(int passno, int firstpassno, int bypass)
216
{
217
        int passtype;
218
        if (bypass) {
219
                passtype = JPC_PASSTYPE(passno);
220
                if (passtype == JPC_CLNPASS) {
221
                        return JPC_SEG_MQ;
222
                }
223
                return ((passno < firstpassno + 10) ? JPC_SEG_MQ : JPC_SEG_RAW);
224
        } else {
225
                return JPC_SEG_MQ;
226
        }
227
}
228
 
229
int JPC_SEGPASSCNT(int passno, int firstpassno, int numpasses, int bypass, int termall)
230
{
231
        int ret;
232
        int passtype;
233
 
234
        if (termall) {
235
                ret = 1;
236
        } else if (bypass) {
237
                if (passno < firstpassno + 10) {
238
                        ret = 10 - (passno - firstpassno);
239
                } else {
240
                        passtype = JPC_PASSTYPE(passno);
241
                        switch (passtype) {
242
                        case JPC_SIGPASS:
243
                                ret = 2;
244
                                break;
245
                        case JPC_REFPASS:
246
                                ret = 1;
247
                                break;
248
                        case JPC_CLNPASS:
249
                                ret = 1;
250
                                break;
251
                        default:
252
                                ret = -1;
253
                                assert(0);
254
                                break;
255
                        }
256
                }
257
        } else {
258
                ret = JPC_PREC * 3 - 2;
259
        }
260
        ret = JAS_MIN(ret, numpasses - passno);
261
        return ret;
262
}
263
 
264
int JPC_ISTERMINATED(int passno, int firstpassno, int numpasses, int termall,
265
  int lazy)
266
{
267
        int ret;
268
        int n;
269
        if (passno - firstpassno == numpasses - 1) {
270
                ret = 1;
271
        } else {
272
                n = JPC_SEGPASSCNT(passno, firstpassno, numpasses, lazy, termall);
273
                ret = (n <= 1) ? 1 : 0;
274
        }
275
 
276
        return ret;
277
}
278
 
279
/******************************************************************************\
280
* Lookup table code.
281
\******************************************************************************/
282
 
283
void jpc_initluts()
284
{
285
        int i;
286
        int orient;
287
        int refine;
288
        float u;
289
        float v;
290
        float t;
291
 
292
/* XXX - hack */
293
jpc_initmqctxs();
294
 
295
        for (orient = 0; orient < 4; ++orient) {
296
                for (i = 0; i < 256; ++i) {
297
                        jpc_zcctxnolut[(orient << 8) | i] = jpc_getzcctxno(i, orient);
298
                }
299
        }
300
 
301
        for (i = 0; i < 256; ++i) {
302
                jpc_spblut[i] = jpc_getspb(i << 4);
303
        }
304
 
305
        for (i = 0; i < 256; ++i) {
306
                jpc_scctxnolut[i] = jpc_getscctxno(i << 4);
307
        }
308
 
309
        for (refine = 0; refine < 2; ++refine) {
310
                for (i = 0; i < 2048; ++i) {
311
                        jpc_magctxnolut[(refine << 11) + i] = jpc_getmagctxno((refine ? JPC_REFINE : 0) | i);
312
                }
313
        }
314
 
315
        for (i = 0; i < (1 << JPC_NMSEDEC_BITS); ++i) {
316
                t = i * jpc_pow2i(-JPC_NMSEDEC_FRACBITS);
317
                u = t;
318
                v = t - 1.5;
319
                jpc_signmsedec[i] = jpc_dbltofix(floor((u * u - v * v) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
320
/* XXX - this calc is not correct */
321
                jpc_signmsedec0[i] = jpc_dbltofix(floor((u * u) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
322
                u = t - 1.0;
323
                if (i & (1 << (JPC_NMSEDEC_BITS - 1))) {
324
                        v = t - 1.5;
325
                } else {
326
                        v = t - 0.5;
327
                }
328
                jpc_refnmsedec[i] = jpc_dbltofix(floor((u * u - v * v) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
329
/* XXX - this calc is not correct */
330
                jpc_refnmsedec0[i] = jpc_dbltofix(floor((u * u) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
331
        }
332
}
333
 
334
jpc_fix_t jpc_getsignmsedec_func(jpc_fix_t x, int bitpos)
335
{
336
        jpc_fix_t y;
337
        assert(!(x & (~JAS_ONES(bitpos + 1))));
338
        y = jpc_getsignmsedec_macro(x, bitpos);
339
        return y;
340
}
341
 
342
int jpc_getzcctxno(int f, int orient)
343
{
344
        int h;
345
        int v;
346
        int d;
347
        int n;
348
        int t;
349
        int hv;
350
 
351
        /* Avoid compiler warning. */
352
        n = 0;
353
 
354
        h = ((f & JPC_WSIG) != 0) + ((f & JPC_ESIG) != 0);
355
        v = ((f & JPC_NSIG) != 0) + ((f & JPC_SSIG) != 0);
356
        d = ((f & JPC_NWSIG) != 0) + ((f & JPC_NESIG) != 0) + ((f & JPC_SESIG) != 0) + ((f & JPC_SWSIG) != 0);
357
        switch (orient) {
358
        case JPC_TSFB_HL:
359
                t = h;
360
                h = v;
361
                v = t;
362
        case JPC_TSFB_LL:
363
        case JPC_TSFB_LH:
364
                if (!h) {
365
                        if (!v) {
366
                                if (!d) {
367
                                        n = 0;
368
                                } else if (d == 1) {
369
                                        n = 1;
370
                                } else {
371
                                        n = 2;
372
                                }
373
                        } else if (v == 1) {
374
                                n = 3;
375
                        } else {
376
                                n = 4;
377
                        }
378
                } else if (h == 1) {
379
                        if (!v) {
380
                                if (!d) {
381
                                        n = 5;
382
                                } else {
383
                                        n = 6;
384
                                }
385
                        } else {
386
                                n = 7;
387
                        }
388
                } else {
389
                        n = 8;
390
                }
391
                break;
392
        case JPC_TSFB_HH:
393
                hv = h + v;
394
                if (!d) {
395
                        if (!hv) {
396
                                n = 0;
397
                        } else if (hv == 1) {
398
                                n = 1;
399
                        } else {
400
                                n = 2;
401
                        }
402
                } else if (d == 1) {
403
                        if (!hv) {
404
                                n = 3;
405
                        } else if (hv == 1) {
406
                                n = 4;
407
                        } else {
408
                                n = 5;
409
                        }
410
                } else if (d == 2) {
411
                        if (!hv) {
412
                                n = 6;
413
                        } else {
414
                                n = 7;
415
                        }
416
                } else {
417
                        n = 8;
418
                }
419
                break;
420
        }
421
        assert(n < JPC_NUMZCCTXS);
422
        return JPC_ZCCTXNO + n;
423
}
424
 
425
int jpc_getspb(int f)
426
{
427
        int hc;
428
        int vc;
429
        int n;
430
 
431
        hc = JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == JPC_ESIG) + ((f & (JPC_WSIG | JPC_WSGN)) == JPC_WSIG), 1) -
432
          JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == (JPC_ESIG | JPC_ESGN)) + ((f & (JPC_WSIG | JPC_WSGN)) == (JPC_WSIG | JPC_WSGN)), 1);
433
        vc = JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == JPC_NSIG) + ((f & (JPC_SSIG | JPC_SSGN)) == JPC_SSIG), 1) -
434
          JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == (JPC_NSIG | JPC_NSGN)) + ((f & (JPC_SSIG | JPC_SSGN)) == (JPC_SSIG | JPC_SSGN)), 1);
435
        if (!hc && !vc) {
436
                n = 0;
437
        } else {
438
                n = (!(hc > 0 || (!hc && vc > 0)));
439
        }
440
        return n;
441
}
442
 
443
int jpc_getscctxno(int f)
444
{
445
        int hc;
446
        int vc;
447
        int n;
448
 
449
        /* Avoid compiler warning. */
450
        n = 0;
451
 
452
        hc = JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == JPC_ESIG) + ((f & (JPC_WSIG | JPC_WSGN)) == JPC_WSIG),
453
          1) - JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == (JPC_ESIG | JPC_ESGN)) +
454
          ((f & (JPC_WSIG | JPC_WSGN)) == (JPC_WSIG | JPC_WSGN)), 1);
455
        vc = JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == JPC_NSIG) + ((f & (JPC_SSIG | JPC_SSGN)) == JPC_SSIG),
456
          1) - JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == (JPC_NSIG | JPC_NSGN)) +
457
          ((f & (JPC_SSIG | JPC_SSGN)) == (JPC_SSIG | JPC_SSGN)), 1);
458
        assert(hc >= -1 && hc <= 1 && vc >= -1 && vc <= 1);
459
        if (hc < 0) {
460
                hc = -hc;
461
                vc = -vc;
462
        }
463
        if (!hc) {
464
                if (vc == -1) {
465
                        n = 1;
466
                } else if (!vc) {
467
                        n = 0;
468
                } else {
469
                        n = 1;
470
                }
471
        } else if (hc == 1) {
472
                if (vc == -1) {
473
                        n = 2;
474
                } else if (!vc) {
475
                        n = 3;
476
                } else {
477
                        n = 4;
478
                }
479
        }
480
        assert(n < JPC_NUMSCCTXS);
481
        return JPC_SCCTXNO + n;
482
}
483
 
484
int jpc_getmagctxno(int f)
485
{
486
        int n;
487
 
488
        if (!(f & JPC_REFINE)) {
489
                n = (f & (JPC_OTHSIGMSK)) ? 1 : 0;
490
        } else {
491
                n = 2;
492
        }
493
 
494
        assert(n < JPC_NUMMAGCTXS);
495
        return JPC_MAGCTXNO + n;
496
}
497
 
498
void jpc_initctxs(jpc_mqctx_t *ctxs)
499
{
500
        jpc_mqctx_t *ctx;
501
        int i;
502
 
503
        ctx = ctxs;
504
        for (i = 0; i < JPC_NUMCTXS; ++i) {
505
                ctx->mps = 0;
506
                switch (i) {
507
                case JPC_UCTXNO:
508
                        ctx->ind = 46;
509
                        break;
510
                case JPC_ZCCTXNO:
511
                        ctx->ind = 4;
512
                        break;
513
                case JPC_AGGCTXNO:
514
                        ctx->ind = 3;
515
                        break;
516
                default:
517
                        ctx->ind = 0;
518
                        break;
519
                }
520
                ++ctx;
521
        }
522
}
523
 
524
void jpc_initmqctxs()
525
{
526
        jpc_initctxs(jpc_mqctxs);
527
}
528
 
529
/* Calculate the real quantity exp2(n), where x is an integer. */
530
double jpc_pow2i(int n)
531
{
532
        double x;
533
        double a;
534
 
535
        x = 1.0;
536
        if (n < 0) {
537
                a = 0.5;
538
                n = -n;
539
        } else {
540
                a = 2.0;
541
        }
542
        while (--n >= 0) {
543
                x *= a;
544
        }
545
        return x;
546
}