Debian Codegraph @

converter/other/jpeg2000/libjasper/jpc/jpc_qmfb.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 * Quadrature Mirror-Image Filter Bank (QMFB) Library
115 *
116 * $Id$
117 */
118
119 /******************************************************************************\
120 * Includes.
121 \******************************************************************************/
122
123 #include <assert.h>
124
125 #include "jasper/jas_malloc.h"
126 #include "jasper/jas_math.h"
127
128 #include "jpc_qmfb.h"
129 #include "jpc_tsfb.h"
130 #include "jpc_math.h"
131
132 /******************************************************************************\
133 *
134 \******************************************************************************/
135
136 static jpc_qmfb1d_t *jpc_qmfb1d_create(void);
137
138 static int jpc_ft_getnumchans(jpc_qmfb1d_t *qmfb);
139 static int jpc_ft_getanalfilters(jpc_qmfb1d_t *qmfb, int len, jas_seq2d_t **filters);
140 static int jpc_ft_getsynfilters(jpc_qmfb1d_t *qmfb, int len, jas_seq2d_t **filters);
141 static void jpc_ft_analyze(jpc_qmfb1d_t *qmfb, int flags, jas_seq2d_t *x);
142 static void jpc_ft_synthesize(jpc_qmfb1d_t *qmfb, int flags, jas_seq2d_t *x);
143
144 static int jpc_ns_getnumchans(jpc_qmfb1d_t *qmfb);
145 static int jpc_ns_getanalfilters(jpc_qmfb1d_t *qmfb, int len, jas_seq2d_t **filters);
146 static int jpc_ns_getsynfilters(jpc_qmfb1d_t *qmfb, int len, jas_seq2d_t **filters);
147 static void jpc_ns_analyze(jpc_qmfb1d_t *qmfb, int flags, jas_seq2d_t *x);
148 static void jpc_ns_synthesize(jpc_qmfb1d_t *qmfb, int flags, jas_seq2d_t *x);
149
150 /******************************************************************************\
151 *
152 \******************************************************************************/
153
154 jpc_qmfb1dops_t jpc_ft_ops = {
155 jpc_ft_getnumchans,
156 jpc_ft_getanalfilters,
157 jpc_ft_getsynfilters,
158 jpc_ft_analyze,
159 jpc_ft_synthesize
160 };
161
162 jpc_qmfb1dops_t jpc_ns_ops = {
163 jpc_ns_getnumchans,
164 jpc_ns_getanalfilters,
165 jpc_ns_getsynfilters,
166 jpc_ns_analyze,
167 jpc_ns_synthesize
168 };
169
170 /******************************************************************************\
171 *
172 \******************************************************************************/
173
174 static void jpc_qmfb1d_setup(jpc_fix_t *startptr, int startind, int endind,
175 int intrastep, jpc_fix_t **lstartptr, int *lstartind, int *lendind,
176 jpc_fix_t **hstartptr, int *hstartind, int *hendind)
177 {
178 *lstartind = JPC_CEILDIVPOW2(startind, 1);
179 *lendind = JPC_CEILDIVPOW2(endind, 1);
180 *hstartind = JPC_FLOORDIVPOW2(startind, 1);
181 *hendind = JPC_FLOORDIVPOW2(endind, 1);
182 *lstartptr = startptr;
183 *hstartptr = &startptr[(*lendind - *lstartind) * intrastep];
184 }
185
186 static void jpc_qmfb1d_split(jpc_fix_t *startptr, int startind, int endind,
187 int step, jpc_fix_t *lstartptr, int lstartind, int lendind,
188 jpc_fix_t *hstartptr, int hstartind, int hendind)
189 {
190 #define QMFB_SPLITBUFSIZE 4096
191 jpc_fix_t splitbuf[QMFB_SPLITBUFSIZE];
192 jpc_fix_t *buf = splitbuf;
193 int llen;
194 int hlen;
195 int twostep;
196 jpc_fix_t *tmpptr;
197 jpc_fix_t *ptr;
198 jpc_fix_t *hptr;
199 jpc_fix_t *lptr;
200 int n;
201 int state;
202
203 twostep = step << 1;
204 llen = lendind - lstartind;
205 hlen = hendind - hstartind;
206
207 if (hstartind < lstartind) {
208 /* The first sample in the input signal is to appear
209 in the highpass subband signal. */
210 /* Copy the appropriate samples into the lowpass subband
211 signal, saving any samples destined for the highpass subband
212 signal as they are overwritten. */
213 tmpptr = buf;
214 ptr = &startptr[step];
215 lptr = lstartptr;
216 n = llen;
217 state = 1;
218 while (n-- > 0) {
219 if (state) {
220 *tmpptr = *lptr;
221 ++tmpptr;
222 }
223 *lptr = *ptr;
224 ptr += twostep;
225 lptr += step;
226 state ^= 1;
227 }
228 /* Copy the appropriate samples into the highpass subband
229 signal. */
230 /* Handle the nonoverwritten samples. */
231 hptr = &hstartptr[(hlen - 1) * step];
232 ptr = &startptr[(((llen + hlen - 1) >> 1) << 1) * step];
233 n = hlen - (tmpptr - buf);
234 while (n-- > 0) {
235 *hptr = *ptr;
236 hptr -= step;
237 ptr -= twostep;
238 }
239 /* Handle the overwritten samples. */
240 n = tmpptr - buf;
241 while (n-- > 0) {
242 --tmpptr;
243 *hptr = *tmpptr;
244 hptr -= step;
245 }
246 } else {
247 /* The first sample in the input signal is to appear
248 in the lowpass subband signal. */
249 /* Copy the appropriate samples into the lowpass subband
250 signal, saving any samples for the highpass subband
251 signal as they are overwritten. */
252 state = 0;
253 ptr = startptr;
254 lptr = lstartptr;
255 tmpptr = buf;
256 n = llen;
257 while (n-- > 0) {
258 if (state) {
259 *tmpptr = *lptr;
260 ++tmpptr;
261 }
262 *lptr = *ptr;
263 ptr += twostep;
264 lptr += step;
265 state ^= 1;
266 }
267 /* Copy the appropriate samples into the highpass subband
268 signal. */
269 /* Handle the nonoverwritten samples. */
270 ptr = &startptr[((((llen + hlen) >> 1) << 1) - 1) * step];
271 hptr = &hstartptr[(hlen - 1) * step];
272 n = hlen - (tmpptr - buf);
273 while (n-- > 0) {
274 *hptr = *ptr;
275 ptr -= twostep;
276 hptr -= step;
277 }
278 /* Handle the overwritten samples. */
279 n = tmpptr - buf;
280 while (n-- > 0) {
281 --tmpptr;
282 *hptr = *tmpptr;
283 hptr -= step;
284 }
285 }
286 }
287
288 static void jpc_qmfb1d_join(jpc_fix_t *startptr, int startind, int endind,
289 int step, jpc_fix_t *lstartptr, int lstartind, int lendind,
290 jpc_fix_t *hstartptr, int hstartind, int hendind)
291 {
292 #define QMFB_JOINBUFSIZE 4096
293 jpc_fix_t joinbuf[QMFB_JOINBUFSIZE];
294 jpc_fix_t *buf = joinbuf;
295 int llen;
296 int hlen;
297 int twostep;
298 jpc_fix_t *tmpptr;
299 jpc_fix_t *ptr;
300 jpc_fix_t *hptr;
301 jpc_fix_t *lptr;
302 int n;
303 int state;
304
305 twostep = step << 1;
306 llen = lendind - lstartind;
307 hlen = hendind - hstartind;
308
309 if (hstartind < lstartind) {
310 /* The first sample in the highpass subband signal is to
311 appear first in the output signal. */
312 /* Copy the appropriate samples into the first phase of the
313 output signal. */
314 tmpptr = buf;
315 hptr = hstartptr;
316 ptr = startptr;
317 n = (llen + 1) >> 1;
318 while (n-- > 0) {
319 *tmpptr = *ptr;
320 *ptr = *hptr;
321 ++tmpptr;
322 ptr += twostep;
323 hptr += step;
324 }
325 n = hlen - ((llen + 1) >> 1);
326 while (n-- > 0) {
327 *ptr = *hptr;
328 ptr += twostep;
329 hptr += step;
330 }
331 /* Copy the appropriate samples into the second phase of
332 the output signal. */
333 ptr -= (lendind > hendind) ? (step) : (step + twostep);
334 state = !((llen - 1) & 1);
335 lptr = &lstartptr[(llen - 1) * step];
336 n = llen;
337 while (n-- > 0) {
338 if (state) {
339 --tmpptr;
340 *ptr = *tmpptr;
341 } else {
342 *ptr = *lptr;
343 }
344 lptr -= step;
345 ptr -= twostep;
346 state ^= 1;
347 }
348 } else {
349 /* The first sample in the lowpass subband signal is to
350 appear first in the output signal. */
351 /* Copy the appropriate samples into the first phase of the
352 output signal (corresponding to even indexed samples). */
353 lptr = &lstartptr[(llen - 1) * step];
354 ptr = &startptr[((llen - 1) << 1) * step];
355 n = llen >> 1;
356 tmpptr = buf;
357 while (n-- > 0) {
358 *tmpptr = *ptr;
359 *ptr = *lptr;
360 ++tmpptr;
361 ptr -= twostep;
362 lptr -= step;
363 }
364 n = llen - (llen >> 1);
365 while (n-- > 0) {
366 *ptr = *lptr;
367 ptr -= twostep;
368 lptr -= step;
369 }
370 /* Copy the appropriate samples into the second phase of
371 the output signal (corresponding to odd indexed
372 samples). */
373 ptr = &startptr[step];
374 hptr = hstartptr;
375 state = !(llen & 1);
376 n = hlen;
377 while (n-- > 0) {
378 if (state) {
379 --tmpptr;
380 *ptr = *tmpptr;
381 } else {
382 *ptr = *hptr;
383 }
384 hptr += step;
385 ptr += twostep;
386 state ^= 1;
387 }
388 }
389 }
390
391 /******************************************************************************\
392 * Code for 5/3 transform.
393 \******************************************************************************/
394
395 static int jpc_ft_getnumchans(jpc_qmfb1d_t *qmfb)
396 {
397 return 2;
398 }
399
400 static int jpc_ft_getanalfilters(jpc_qmfb1d_t *qmfb, int len, jas_seq2d_t **filters)
401 {
402 abort();
403 return -1;
404 }
405
406 static int jpc_ft_getsynfilters(jpc_qmfb1d_t *qmfb, int len, jas_seq2d_t **filters)
407 {
408 jas_seq_t *lf;
409 jas_seq_t *hf;
410
411 lf = 0;
412 hf = 0;
413
414 if (len > 1 || (!len)) {
415 if (!(lf = jas_seq_create(-1, 2))) {
416 goto error;
417 }
418 jas_seq_set(lf, -1, jpc_dbltofix(0.5));
419 jas_seq_set(lf, 0, jpc_dbltofix(1.0));
420 jas_seq_set(lf, 1, jpc_dbltofix(0.5));
421 if (!(hf = jas_seq_create(-1, 4))) {
422 goto error;
423 }
424 jas_seq_set(hf, -1, jpc_dbltofix(-0.125));
425 jas_seq_set(hf, 0, jpc_dbltofix(-0.25));
426 jas_seq_set(hf, 1, jpc_dbltofix(0.75));
427 jas_seq_set(hf, 2, jpc_dbltofix(-0.25));
428 jas_seq_set(hf, 3, jpc_dbltofix(-0.125));
429 } else if (len == 1) {
430 if (!(lf = jas_seq_create(0, 1))) {
431 goto error;
432 }
433 jas_seq_set(lf, 0, jpc_dbltofix(1.0));
434 if (!(hf = jas_seq_create(0, 1))) {
435 goto error;
436 }
437 jas_seq_set(hf, 0, jpc_dbltofix(2.0));
438 } else {
439 abort();
440 }
441
442 filters[0] = lf;
443 filters[1] = hf;
444
445 return 0;
446
447 error:
448 if (lf) {
449 jas_seq_destroy(lf);
450 }
451 if (hf) {
452 jas_seq_destroy(hf);
453 }
454 return -1;
455 }
456
457 #define NFT_LIFT0(lstartptr, lstartind, lendind, hstartptr, hstartind, hendind, step, pluseq) \
458 { \
459 jpc_fix_t *lptr = (lstartptr); \
460 jpc_fix_t *hptr = (hstartptr); \
461 int n = (hendind) - (hstartind); \
462 if ((hstartind) < (lstartind)) { \
463 pluseq(*hptr, *lptr); \
464 hptr += (step); \
465 --n; \
466 } \
467 if ((hendind) >= (lendind)) { \
468 --n; \
469 } \
470 while (n-- > 0) { \
471 pluseq(*hptr, jpc_fix_asr(jpc_fix_add(*lptr, lptr[(step)]), 1)); \
472 hptr += (step); \
473 lptr += (step); \
474 } \
475 if ((hendind) >= (lendind)) { \
476 pluseq(*hptr, *lptr); \
477 } \
478 }
479
480 #define NFT_LIFT1(lstartptr, lstartind, lendind, hstartptr, hstartind, hendind, step, pluseq) \
481 { \
482 jpc_fix_t *lptr = (lstartptr); \
483 jpc_fix_t *hptr = (hstartptr); \
484 int n = (lendind) - (lstartind); \
485 if ((hstartind) >= (lstartind)) { \
486 pluseq(*lptr, *hptr); \
487 lptr += (step); \
488 --n; \
489 } \
490 if ((lendind) > (hendind)) { \
491 --n; \
492 } \
493 while (n-- > 0) { \
494 pluseq(*lptr, jpc_fix_asr(jpc_fix_add(*hptr, hptr[(step)]), 2)); \
495 lptr += (step); \
496 hptr += (step); \
497 } \
498 if ((lendind) > (hendind)) { \
499 pluseq(*lptr, *hptr); \
500 } \
501 }
502
503 #define RFT_LIFT0(lstartptr, lstartind, lendind, hstartptr, hstartind, hendind, step, pmeqop) \
504 { \
505 jpc_fix_t *lptr = (lstartptr); \
506 jpc_fix_t *hptr = (hstartptr); \
507 int n = (hendind) - (hstartind); \
508 if ((hstartind) < (lstartind)) { \
509 *hptr pmeqop *lptr; \
510 hptr += (step); \
511 --n; \
512 } \
513 if ((hendind) >= (lendind)) { \
514 --n; \
515 } \
516 while (n-- > 0) { \
517 *hptr pmeqop (*lptr + lptr[(step)]) >> 1; \
518 hptr += (step); \
519 lptr += (step); \
520 } \
521 if ((hendind) >= (lendind)) { \
522 *hptr pmeqop *lptr; \
523 } \
524 }
525
526 #define RFT_LIFT1(lstartptr, lstartind, lendind, hstartptr, hstartind, hendind, step, pmeqop) \
527 { \
528 jpc_fix_t *lptr = (lstartptr); \
529 jpc_fix_t *hptr = (hstartptr); \
530 int n = (lendind) - (lstartind); \
531 if ((hstartind) >= (lstartind)) { \
532 *lptr pmeqop ((*hptr << 1) + 2) >> 2; \
533 lptr += (step); \
534 --n; \
535 } \
536 if ((lendind) > (hendind)) { \
537 --n; \
538 } \
539 while (n-- > 0) { \
540 *lptr pmeqop ((*hptr + hptr[(step)]) + 2) >> 2; \
541 lptr += (step); \
542 hptr += (step); \
543 } \
544 if ((lendind) > (hendind)) { \
545 *lptr pmeqop ((*hptr << 1) + 2) >> 2; \
546 } \
547 }
548
549 static void jpc_ft_analyze(jpc_qmfb1d_t *qmfb, int flags, jas_seq2d_t *x)
550 {
551 jpc_fix_t *startptr;
552 int startind;
553 int endind;
554 jpc_fix_t * lstartptr;
555 int lstartind;
556 int lendind;
557 jpc_fix_t * hstartptr;
558 int hstartind;
559 int hendind;
560 int interstep;
561 int intrastep;
562 int numseq;
563
564 if (flags & JPC_QMFB1D_VERT) {
565 interstep = 1;
566 intrastep = jas_seq2d_rowstep(x);
567 numseq = jas_seq2d_width(x);
568 startind = jas_seq2d_ystart(x);
569 endind = jas_seq2d_yend(x);
570 } else {
571 interstep = jas_seq2d_rowstep(x);
572 intrastep = 1;
573 numseq = jas_seq2d_height(x);
574 startind = jas_seq2d_xstart(x);
575 endind = jas_seq2d_xend(x);
576 }
577
578 assert(startind < endind);
579
580 startptr = jas_seq2d_getref(x, jas_seq2d_xstart(x), jas_seq2d_ystart(x));
581 if (flags & JPC_QMFB1D_RITIMODE) {
582 while (numseq-- > 0) {
583 jpc_qmfb1d_setup(startptr, startind, endind, intrastep,
584 &lstartptr, &lstartind, &lendind, &hstartptr,
585 &hstartind, &hendind);
586 if (endind - startind > 1) {
587 jpc_qmfb1d_split(startptr, startind, endind,
588 intrastep, lstartptr, lstartind, lendind,
589 hstartptr, hstartind, hendind);
590 RFT_LIFT0(lstartptr, lstartind, lendind,
591 hstartptr, hstartind, hendind, intrastep, -=);
592 RFT_LIFT1(lstartptr, lstartind, lendind,
593 hstartptr, hstartind, hendind, intrastep, +=);
594 } else {
595 if (lstartind == lendind) {
596 *startptr <<= 1;
597 }
598 }
599 startptr += interstep;
600 }
601 } else {
602 while (numseq-- > 0) {
603 jpc_qmfb1d_setup(startptr, startind, endind, intrastep,
604 &lstartptr, &lstartind, &lendind, &hstartptr,
605 &hstartind, &hendind);
606 if (endind - startind > 1) {
607 jpc_qmfb1d_split(startptr, startind, endind,
608 intrastep, lstartptr, lstartind, lendind,
609 hstartptr, hstartind, hendind);
610 NFT_LIFT0(lstartptr, lstartind, lendind,
611 hstartptr, hstartind, hendind, intrastep,
612 jpc_fix_minuseq);
613 NFT_LIFT1(lstartptr, lstartind, lendind,
614 hstartptr, hstartind, hendind, intrastep,
615 jpc_fix_pluseq);
616 } else {
617 if (lstartind == lendind) {
618 *startptr = jpc_fix_asl(*startptr, 1);
619 }
620 }
621 startptr += interstep;
622 }
623 }
624 }
625
626 static void jpc_ft_synthesize(jpc_qmfb1d_t *qmfb, int flags, jas_seq2d_t *x)
627 {
628 jpc_fix_t *startptr;
629 int startind;
630 int endind;
631 jpc_fix_t *lstartptr;
632 int lstartind;
633 int lendind;
634 jpc_fix_t *hstartptr;
635 int hstartind;
636 int hendind;
637 int interstep;
638 int intrastep;
639 int numseq;
640
641 if (flags & JPC_QMFB1D_VERT) {
642 interstep = 1;
643 intrastep = jas_seq2d_rowstep(x);
644 numseq = jas_seq2d_width(x);
645 startind = jas_seq2d_ystart(x);
646 endind = jas_seq2d_yend(x);
647 } else {
648 interstep = jas_seq2d_rowstep(x);
649 intrastep = 1;
650 numseq = jas_seq2d_height(x);
651 startind = jas_seq2d_xstart(x);
652 endind = jas_seq2d_xend(x);
653 }
654
655 assert(startind < endind);
656
657 startptr = jas_seq2d_getref(x, jas_seq2d_xstart(x), jas_seq2d_ystart(x));
658 if (flags & JPC_QMFB1D_RITIMODE) {
659 while (numseq-- > 0) {
660 jpc_qmfb1d_setup(startptr, startind, endind, intrastep,
661 &lstartptr, &lstartind, &lendind, &hstartptr,
662 &hstartind, &hendind);
663 if (endind - startind > 1) {
664 RFT_LIFT1(lstartptr, lstartind, lendind,
665 hstartptr, hstartind, hendind, intrastep, -=);
666 RFT_LIFT0(lstartptr, lstartind, lendind,
667 hstartptr, hstartind, hendind, intrastep, +=);
668 jpc_qmfb1d_join(startptr, startind, endind,
669 intrastep, lstartptr, lstartind, lendind,
670 hstartptr, hstartind, hendind);
671 } else {
672 if (lstartind == lendind) {
673 *startptr >>= 1;
674 }
675 }
676 startptr += interstep;
677 }
678 } else {
679 while (numseq-- > 0) {
680 jpc_qmfb1d_setup(startptr, startind, endind, intrastep,
681 &lstartptr, &lstartind, &lendind, &hstartptr,
682 &hstartind, &hendind);
683 if (endind - startind > 1) {
684 NFT_LIFT1(lstartptr, lstartind, lendind,
685 hstartptr, hstartind, hendind, intrastep,
686 jpc_fix_minuseq);
687 NFT_LIFT0(lstartptr, lstartind, lendind,
688 hstartptr, hstartind, hendind, intrastep,
689 jpc_fix_pluseq);
690 jpc_qmfb1d_join(startptr, startind, endind,
691 intrastep, lstartptr, lstartind, lendind,
692 hstartptr, hstartind, hendind);
693 } else {
694 if (lstartind == lendind) {
695 *startptr = jpc_fix_asr(*startptr, 1);
696 }
697 }
698 startptr += interstep;
699 }
700 }
701 }
702
703 /******************************************************************************\
704 * Code for 9/7 transform.
705 \******************************************************************************/
706
707 static int jpc_ns_getnumchans(jpc_qmfb1d_t *qmfb)
708 {
709 return 2;
710 }
711
712 static int jpc_ns_getanalfilters(jpc_qmfb1d_t *qmfb, int len, jas_seq2d_t **filters)
713 {
714 abort();
715 return -1;
716 }
717
718 static int jpc_ns_getsynfilters(jpc_qmfb1d_t *qmfb, int len, jas_seq2d_t **filters)
719 {
720 jas_seq_t *lf;
721 jas_seq_t *hf;
722
723 lf = 0;
724 hf = 0;
725
726 if (len > 1 || (!len)) {
727 if (!(lf = jas_seq_create(-3, 4))) {
728 goto error;
729 }
730 jas_seq_set(lf, -3, jpc_dbltofix(-0.09127176311424948));
731 jas_seq_set(lf, -2, jpc_dbltofix(-0.05754352622849957));
732 jas_seq_set(lf, -1, jpc_dbltofix(0.5912717631142470));
733 jas_seq_set(lf, 0, jpc_dbltofix(1.115087052456994));
734 jas_seq_set(lf, 1, jpc_dbltofix(0.5912717631142470));
735 jas_seq_set(lf, 2, jpc_dbltofix(-0.05754352622849957));
736 jas_seq_set(lf, 3, jpc_dbltofix(-0.09127176311424948));
737 if (!(hf = jas_seq_create(-3, 6))) {
738 goto error;
739 }
740 jas_seq_set(hf, -3, jpc_dbltofix(-0.02674875741080976 * 2.0));
741 jas_seq_set(hf, -2, jpc_dbltofix(-0.01686411844287495 * 2.0));
742 jas_seq_set(hf, -1, jpc_dbltofix(0.07822326652898785 * 2.0));
743 jas_seq_set(hf, 0, jpc_dbltofix(0.2668641184428723 * 2.0));
744 jas_seq_set(hf, 1, jpc_dbltofix(-0.6029490182363579 * 2.0));
745 jas_seq_set(hf, 2, jpc_dbltofix(0.2668641184428723 * 2.0));
746 jas_seq_set(hf, 3, jpc_dbltofix(0.07822326652898785 * 2.0));
747 jas_seq_set(hf, 4, jpc_dbltofix(-0.01686411844287495 * 2.0));
748 jas_seq_set(hf, 5, jpc_dbltofix(-0.02674875741080976 * 2.0));
749 } else if (len == 1) {
750 if (!(lf = jas_seq_create(0, 1))) {
751 goto error;
752 }
753 jas_seq_set(lf, 0, jpc_dbltofix(1.0));
754 if (!(hf = jas_seq_create(0, 1))) {
755 goto error;
756 }
757 jas_seq_set(hf, 0, jpc_dbltofix(2.0));
758 } else {
759 abort();
760 }
761
762 filters[0] = lf;
763 filters[1] = hf;
764
765 return 0;
766
767 error:
768 if (lf) {
769 jas_seq_destroy(lf);
770 }
771 if (hf) {
772 jas_seq_destroy(hf);
773 }
774 return -1;
775 }
776
777 #define NNS_LIFT0(lstartptr, lstartind, lendind, hstartptr, hstartind, hendind, step, alpha) \
778 { \
779 jpc_fix_t *lptr = (lstartptr); \
780 jpc_fix_t *hptr = (hstartptr); \
781 int n = (hendind) - (hstartind); \
782 jpc_fix_t twoalpha = jpc_fix_mulbyint(alpha, 2); \
783 if ((hstartind) < (lstartind)) { \
784 jpc_fix_pluseq(*hptr, jpc_fix_mul(*lptr, (twoalpha))); \
785 hptr += (step); \
786 --n; \
787 } \
788 if ((hendind) >= (lendind)) { \
789 --n; \
790 } \
791 while (n-- > 0) { \
792 jpc_fix_pluseq(*hptr, jpc_fix_mul(jpc_fix_add(*lptr, lptr[(step)]), (alpha))); \
793 hptr += (step); \
794 lptr += (step); \
795 } \
796 if ((hendind) >= (lendind)) { \
797 jpc_fix_pluseq(*hptr, jpc_fix_mul(*lptr, (twoalpha))); \
798 } \
799 }
800
801 #define NNS_LIFT1(lstartptr, lstartind, lendind, hstartptr, hstartind, hendind, step, alpha) \
802 { \
803 jpc_fix_t *lptr = (lstartptr); \
804 jpc_fix_t *hptr = (hstartptr); \
805 int n = (lendind) - (lstartind); \
806 int twoalpha = jpc_fix_mulbyint(alpha, 2); \
807 if ((hstartind) >= (lstartind)) { \
808 jpc_fix_pluseq(*lptr, jpc_fix_mul(*hptr, (twoalpha))); \
809 lptr += (step); \
810 --n; \
811 } \
812 if ((lendind) > (hendind)) { \
813 --n; \
814 } \
815 while (n-- > 0) { \
816 jpc_fix_pluseq(*lptr, jpc_fix_mul(jpc_fix_add(*hptr, hptr[(step)]), (alpha))); \
817 lptr += (step); \
818 hptr += (step); \
819 } \
820 if ((lendind) > (hendind)) { \
821 jpc_fix_pluseq(*lptr, jpc_fix_mul(*hptr, (twoalpha))); \
822 } \
823 }
824
825 #define NNS_SCALE(startptr, startind, endind, step, alpha) \
826 { \
827 jpc_fix_t *ptr = (startptr); \
828 int n = (endind) - (startind); \
829 while (n-- > 0) { \
830 jpc_fix_muleq(*ptr, alpha); \
831 ptr += (step); \
832 } \
833 }
834
835 static void jpc_ns_analyze(jpc_qmfb1d_t *qmfb, int flags, jas_seq2d_t *x)
836 {
837 jpc_fix_t *startptr;
838 int startind;
839 int endind;
840 jpc_fix_t *lstartptr;
841 int lstartind;
842 int lendind;
843 jpc_fix_t *hstartptr;
844 int hstartind;
845 int hendind;
846 int interstep;
847 int intrastep;
848 int numseq;
849
850 if (flags & JPC_QMFB1D_VERT) {
851 interstep = 1;
852 intrastep = jas_seq2d_rowstep(x);
853 numseq = jas_seq2d_width(x);
854 startind = jas_seq2d_ystart(x);
855 endind = jas_seq2d_yend(x);
856 } else {
857 interstep = jas_seq2d_rowstep(x);
858 intrastep = 1;
859 numseq = jas_seq2d_height(x);
860 startind = jas_seq2d_xstart(x);
861 endind = jas_seq2d_xend(x);
862 }
863
864 assert(startind < endind);
865
866 startptr = jas_seq2d_getref(x, jas_seq2d_xstart(x), jas_seq2d_ystart(x));
867 if (!(flags & JPC_QMFB1D_RITIMODE)) {
868 while (numseq-- > 0) {
869 jpc_qmfb1d_setup(startptr, startind, endind, intrastep,
870 &lstartptr, &lstartind, &lendind, &hstartptr,
871 &hstartind, &hendind);
872 if (endind - startind > 1) {
873 jpc_qmfb1d_split(startptr, startind, endind,
874 intrastep, lstartptr, lstartind, lendind,
875 hstartptr, hstartind, hendind);
876 NNS_LIFT0(lstartptr, lstartind, lendind,
877 hstartptr, hstartind, hendind, intrastep,
878 jpc_dbltofix(-1.586134342));
879 NNS_LIFT1(lstartptr, lstartind, lendind,
880 hstartptr, hstartind, hendind, intrastep,
881 jpc_dbltofix(-0.052980118));
882 NNS_LIFT0(lstartptr, lstartind, lendind,
883 hstartptr, hstartind, hendind, intrastep,
884 jpc_dbltofix(0.882911075));
885 NNS_LIFT1(lstartptr, lstartind, lendind,
886 hstartptr, hstartind, hendind, intrastep,
887 jpc_dbltofix(0.443506852));
888 NNS_SCALE(lstartptr, lstartind, lendind,
889 intrastep, jpc_dbltofix(1.0/1.23017410558578));
890 NNS_SCALE(hstartptr, hstartind, hendind,
891 intrastep, jpc_dbltofix(1.0/1.62578613134411));
892 } else {
893 #if 0
894 if (lstartind == lendind) {
895 *startptr = jpc_fix_asl(*startptr, 1);
896 }
897 #endif
898 }
899 startptr += interstep;
900 }
901 } else {
902 /* The reversible integer-to-integer mode is not valid for this
903 transform. */
904 abort();
905 }
906 }
907
908 static void jpc_ns_synthesize(jpc_qmfb1d_t *qmfb, int flags, jas_seq2d_t *x)
909 {
910 jpc_fix_t *startptr;
911 int startind;
912 int endind;
913 jpc_fix_t *lstartptr;
914 int lstartind;
915 int lendind;
916 jpc_fix_t *hstartptr;
917 int hstartind;
918 int hendind;
919 int interstep;
920 int intrastep;
921 int numseq;
922
923 if (flags & JPC_QMFB1D_VERT) {
924 interstep = 1;
925 intrastep = jas_seq2d_rowstep(x);
926 numseq = jas_seq2d_width(x);
927 startind = jas_seq2d_ystart(x);
928 endind = jas_seq2d_yend(x);
929 } else {
930 interstep = jas_seq2d_rowstep(x);
931 intrastep = 1;
932 numseq = jas_seq2d_height(x);
933 startind = jas_seq2d_xstart(x);
934 endind = jas_seq2d_xend(x);
935 }
936
937 assert(startind < endind);
938
939 startptr = jas_seq2d_getref(x, jas_seq2d_xstart(x), jas_seq2d_ystart(x));
940 if (!(flags & JPC_QMFB1D_RITIMODE)) {
941 while (numseq-- > 0) {
942 jpc_qmfb1d_setup(startptr, startind, endind, intrastep,
943 &lstartptr, &lstartind, &lendind, &hstartptr,
944 &hstartind, &hendind);
945 if (endind - startind > 1) {
946 NNS_SCALE(lstartptr, lstartind, lendind,
947 intrastep, jpc_dbltofix(1.23017410558578));
948 NNS_SCALE(hstartptr, hstartind, hendind,
949 intrastep, jpc_dbltofix(1.62578613134411));
950 NNS_LIFT1(lstartptr, lstartind, lendind,
951 hstartptr, hstartind, hendind, intrastep,
952 jpc_dbltofix(-0.443506852));
953 NNS_LIFT0(lstartptr, lstartind, lendind,
954 hstartptr, hstartind, hendind, intrastep,
955 jpc_dbltofix(-0.882911075));
956 NNS_LIFT1(lstartptr, lstartind, lendind,
957 hstartptr, hstartind, hendind, intrastep,
958 jpc_dbltofix(0.052980118));
959 NNS_LIFT0(lstartptr, lstartind, lendind,
960 hstartptr, hstartind, hendind, intrastep,
961 jpc_dbltofix(1.586134342));
962 jpc_qmfb1d_join(startptr, startind, endind,
963 intrastep, lstartptr, lstartind, lendind,
964 hstartptr, hstartind, hendind);
965 } else {
966 #if 0
967 if (lstartind == lendind) {
968 *startptr = jpc_fix_asr(*startptr, 1);
969 }
970 #endif
971 }
972 startptr += interstep;
973 }
974 } else {
975 /* The reversible integer-to-integer mode is not valid
976 for this transform. */
977 abort();
978 }
979 }
980
981 /******************************************************************************\
982 *
983 \******************************************************************************/
984
985 jpc_qmfb1d_t *jpc_qmfb1d_make(int qmfbid)
986 {
987 jpc_qmfb1d_t *qmfb;
988 if (!(qmfb = jpc_qmfb1d_create())) {
989 return 0;
990 }
991 switch (qmfbid) {
992 case JPC_QMFB1D_FT:
993 qmfb->ops = &jpc_ft_ops;
994 break;
995 case JPC_QMFB1D_NS:
996 qmfb->ops = &jpc_ns_ops;
997 break;
998 default:
999 jpc_qmfb1d_destroy(qmfb);
1000 return 0;
1001 break;
1002 }
1003 return qmfb;
1004 }
1005
1006 static jpc_qmfb1d_t *jpc_qmfb1d_create()
1007 {
1008 jpc_qmfb1d_t *qmfb;
1009 if (!(qmfb = jas_malloc(sizeof(jpc_qmfb1d_t)))) {
1010 return 0;
1011 }
1012 qmfb->ops = 0;
1013 return qmfb;
1014 }
1015
1016 jpc_qmfb1d_t *jpc_qmfb1d_copy(jpc_qmfb1d_t *qmfb)
1017 {
1018 jpc_qmfb1d_t *newqmfb;
1019
1020 if (!(newqmfb = jpc_qmfb1d_create())) {
1021 return 0;
1022 }
1023 newqmfb->ops = qmfb->ops;
1024 return newqmfb;
1025 }
1026
1027 void jpc_qmfb1d_destroy(jpc_qmfb1d_t *qmfb)
1028 {
1029 jas_free(qmfb);
1030 }
1031
1032 /******************************************************************************\
1033 *
1034 \******************************************************************************/
1035
1036 void jpc_qmfb1d_getbands(jpc_qmfb1d_t *qmfb, int flags, uint_fast32_t xstart,
1037 uint_fast32_t ystart, uint_fast32_t xend, uint_fast32_t yend, int maxbands,
1038 int *numbandsptr, jpc_qmfb1dband_t *bands)
1039 {
1040 int start;
1041 int end;
1042 if (flags & JPC_QMFB1D_VERT) {
1043 start = ystart;
1044 end = yend;
1045 } else {
1046 start = xstart;
1047 end = xend;
1048 }
1049 assert(jpc_qmfb1d_getnumchans(qmfb) == 2);
1050 assert(start <= end);
1051 bands[0].start = JPC_CEILDIVPOW2(start, 1);
1052 bands[0].end = JPC_CEILDIVPOW2(end, 1);
1053 bands[0].locstart = start;
1054 bands[0].locend = start + bands[0].end - bands[0].start;
1055 bands[1].start = JPC_FLOORDIVPOW2(start, 1);
1056 bands[1].end = JPC_FLOORDIVPOW2(end, 1);
1057 bands[1].locstart = bands[0].locend;
1058 bands[1].locend = bands[1].locstart + bands[1].end - bands[1].start;
1059 assert(bands[1].locend == end);
1060 *numbandsptr = 2;
1061 }
1062
1063 /******************************************************************************\
1064 *
1065 \******************************************************************************/
1066
1067 int jpc_qmfb1d_getnumchans(jpc_qmfb1d_t *qmfb)
1068 {
1069 return (*qmfb->ops->getnumchans)(qmfb);
1070 }
1071
1072 int jpc_qmfb1d_getanalfilters(jpc_qmfb1d_t *qmfb, int len, jas_seq2d_t **filters)
1073 {
1074 return (*qmfb->ops->getanalfilters)(qmfb, len, filters);
1075 }
1076
1077 int jpc_qmfb1d_getsynfilters(jpc_qmfb1d_t *qmfb, int len, jas_seq2d_t **filters)
1078 {
1079 return (*qmfb->ops->getsynfilters)(qmfb, len, filters);
1080 }
1081
1082 void jpc_qmfb1d_analyze(jpc_qmfb1d_t *qmfb, int flags, jas_seq2d_t *x)
1083 {
1084 (*qmfb->ops->analyze)(qmfb, flags, x);
1085 }
1086
1087 void jpc_qmfb1d_synthesize(jpc_qmfb1d_t *qmfb, int flags, jas_seq2d_t *x)
1088 {
1089 (*qmfb->ops->synthesize)(qmfb, flags, x);
1090 }