001: /*
002: * $RCSfile: InterpolationBicubic.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:57:10 $
010: * $State: Exp $
011: */
012: package javax.media.jai;
013:
014: /**
015: * A class representing bicubic interpolation.
016: *
017: * <p> InterpolationBicubic is a subclass of Interpolation that
018: * performs interpolation using the piecewise cubic polynomial:
019: *
020: * <pre>
021: * r(x) = (a + 2)|x|^3 - (a + 3)|x|^2 + 1 , 0 <= |x| < 1
022: * r(x) = a|x|^3 - 5a|x|^2 + 8a|x| - 4a , 1 <= |x| < 2
023: * r(x) = 0 , otherwise
024: * </pre>
025: *
026: * with 'a' set to -0.5.
027: *
028: * This definition is also sometimes known as "cubic convolution",
029: * using the parameter 'a' recommended by Rifman.
030: * (Reference: Digital Image Warping, George Wolberg, 1990, pp 129-131,
031: * IEEE Computer Society Press, ISBN 0-8186-8944-7)
032: *
033: * <p> A neighborhood extending one sample to the left of and above the
034: * central sample, and two samples to the right of and below the central
035: * sample is required to perform bicubic interpolation.
036: *
037: * <p> This implementation creates an <code>InterpolationTable</code>
038: * whose integer coefficients have eight bits of precision to the
039: * right of the binary point.
040: *
041: * <p> The diagrams below illustrate the pixels involved in one-dimensional
042: * interpolation. Point s0 is the interpolation kernel key position.
043: * xfrac and yfrac, indicated by the dots, represent the point of interpolation
044: * between two pixels. This value lies between 0.0 and 1.0 exclusive for
045: * floating point and 0 and 2<sup>subsampleBits</sup> exclusive for integer
046: * interpolations.
047: *
048: * <pre>
049: * <b>
050: * Horizontal Vertical
051: *
052: * s_ s0 . s1 s2 s_
053: * ^
054: * xfrac s0
055: * .< yfrac
056: * s1
057: *
058: * s2
059: * </b>
060: * </pre>
061: *
062: * <p> The diagram below illustrates the pixels involved in
063: * two-dimensional interpolation.
064: *
065: * <pre>
066: * <b>
067: * s__ s_0 s_1 s_2
068: *
069: *
070: *
071: * s0_ s00 s01 s02
072: *
073: * . < yfrac
074: *
075: * s1_ s10 s11 s12
076: *
077: *
078: *
079: * s2_ s20 s21 s22
080: * ^
081: * xfrac
082: * </b>
083: * </pre>
084: *
085: * <p> The class is marked 'final' so that it may be more easily inlined.
086: *
087: * @see Interpolation
088: */
089: public final class InterpolationBicubic extends InterpolationTable {
090:
091: private static final int PRECISION_BITS = 8;
092:
093: private static float[] dataHelper(int subsampleBits) {
094:
095: int one = 1 << subsampleBits;
096: int arrayLength = one * 4;
097: float tableValues[] = new float[arrayLength];
098: float f;
099:
100: float onef = (float) one;
101: float t;
102: int count = 0;
103: for (int i = 0; i < one; i++) {
104: t = (float) i;
105: f = (i / onef);
106:
107: tableValues[count++] = bicubic(f + 1.0F);
108: tableValues[count++] = bicubic(f);
109: tableValues[count++] = bicubic(f - 1.0F);
110: tableValues[count++] = bicubic(f - 2.0F);
111:
112: }
113:
114: return tableValues;
115: }
116:
117: // The parameter "a" for the bicubic polynomial
118: private static final float A = -0.5F;
119:
120: // Define all of the polynomial coefficients in terms of "a"
121: private static final float A3 = A + 2.0F;
122: private static final float A2 = -(A + 3.0F);
123: private static final float A0 = 1.0F;
124:
125: private static final float B3 = A;
126: private static final float B2 = -(5.0F * A);
127: private static final float B1 = 8.0F * A;
128: private static final float B0 = -(4.0F * A);
129:
130: /** Returns the bicubic polynomial value at a certain value of x. */
131: private static float bicubic(float x) {
132:
133: if (x < 0) {
134: x = -x;
135: }
136:
137: // Evaluate with Horner's rule
138: if (x >= 1) {
139: return (((B3 * x) + B2) * x + B1) * x + B0;
140: } else {
141: return ((A3 * x) + A2) * x * x + A0;
142: }
143:
144: }
145:
146: /**
147: * Constructs an InterpolationBicubic with a given subsample
148: * precision, in bits. This precision is applied to both axes.
149: *
150: * <p> This implementation creates an <code>InterpolationTable</code>
151: * whose integer coefficients have eight bits of precision to the
152: * right of the binary point.
153: *
154: * @param subsampleBits the subsample precision.
155: */
156: public InterpolationBicubic(int subsampleBits) {
157: super (1, 1, 4, 4, subsampleBits, subsampleBits, PRECISION_BITS,
158: dataHelper(subsampleBits), null);
159: }
160:
161: }
|