2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
#include <stdlib.h>
|
|
27 |
#include "jni_util.h"
|
|
28 |
#include "math.h"
|
|
29 |
|
|
30 |
#include "GraphicsPrimitiveMgr.h"
|
|
31 |
#include "Region.h"
|
|
32 |
|
|
33 |
#include "sun_java2d_loops_TransformHelper.h"
|
|
34 |
#include "java_awt_image_AffineTransformOp.h"
|
|
35 |
|
|
36 |
/*
|
|
37 |
* The stub functions replace the bilinear and bicubic interpolation
|
|
38 |
* functions with NOP versions so that the performance of the helper
|
|
39 |
* functions that fetch the data can be more directly tested. They
|
|
40 |
* are not compiled or enabled by default. Change the following
|
|
41 |
* #undef to a #define to build the stub functions.
|
|
42 |
*
|
|
43 |
* When compiled, they are enabled by the environment variable TXSTUB.
|
|
44 |
* When compiled, there is also code to disable the VIS versions and
|
|
45 |
* use the C versions in this file in their place by defining the TXNOVIS
|
|
46 |
* environment variable.
|
|
47 |
*/
|
|
48 |
#undef MAKE_STUBS
|
|
49 |
|
|
50 |
/* The number of IntArgbPre samples to store in the temporary buffer. */
|
|
51 |
#define LINE_SIZE 2048
|
|
52 |
|
|
53 |
/* The size of a stack allocated buffer to hold edge coordinates (see below). */
|
|
54 |
#define MAXEDGES 1024
|
|
55 |
|
|
56 |
/* Declare the software interpolation functions. */
|
|
57 |
static TransformInterpFunc BilinearInterp;
|
|
58 |
static TransformInterpFunc BicubicInterp;
|
|
59 |
|
|
60 |
#ifdef MAKE_STUBS
|
|
61 |
/* Optionally Declare the stub interpolation functions. */
|
|
62 |
static TransformInterpFunc BilinearInterpStub;
|
|
63 |
static TransformInterpFunc BicubicInterpStub;
|
|
64 |
#endif /* MAKE_STUBS */
|
|
65 |
|
|
66 |
/*
|
|
67 |
* Initially choose the software interpolation functions.
|
|
68 |
* These choices can be overridden by platform code that runs during the
|
|
69 |
* primitive registration phase of initialization by storing pointers to
|
|
70 |
* better functions in these pointers.
|
|
71 |
* Compiling the stubs also turns on code below that can re-install the
|
|
72 |
* software functions or stub functions on the first call to this primitive.
|
|
73 |
*/
|
|
74 |
TransformInterpFunc *pBilinearFunc = BilinearInterp;
|
|
75 |
TransformInterpFunc *pBicubicFunc = BicubicInterp;
|
|
76 |
|
|
77 |
/*
|
|
78 |
* Fill the edge buffer with pairs of coordinates representing the maximum
|
|
79 |
* left and right pixels of the destination surface that should be processed
|
|
80 |
* on each scanline, clipped to the bounds parameter.
|
|
81 |
* The number of scanlines to calculate is implied by the bounds parameter.
|
|
82 |
* Only pixels that map back through the specified (inverse) transform to a
|
|
83 |
* source coordinate that falls within the (0, 0, sw, sh) bounds of the
|
|
84 |
* source image should be processed.
|
|
85 |
* pEdgeBuf points to an array of jints that holds MAXEDGES*2 values.
|
|
86 |
* If more storage is needed, then this function allocates a new buffer.
|
|
87 |
* In either case, a pointer to the buffer actually used to store the
|
|
88 |
* results is returned.
|
|
89 |
* The caller is responsible for freeing the buffer if the return value
|
|
90 |
* is not the same as the original pEdgeBuf passed in.
|
|
91 |
*/
|
|
92 |
static jint *
|
|
93 |
calculateEdges(jint *pEdgeBuf,
|
|
94 |
SurfaceDataBounds *pBounds,
|
|
95 |
TransformInfo *pItxInfo,
|
|
96 |
jlong xbase, jlong ybase,
|
|
97 |
juint sw, juint sh)
|
|
98 |
{
|
|
99 |
jint *pEdges;
|
|
100 |
jlong dxdxlong, dydxlong;
|
|
101 |
jlong dxdylong, dydylong;
|
|
102 |
jlong drowxlong, drowylong;
|
|
103 |
jint dx1, dy1, dx2, dy2;
|
|
104 |
|
|
105 |
dxdxlong = DblToLong(pItxInfo->dxdx);
|
|
106 |
dydxlong = DblToLong(pItxInfo->dydx);
|
|
107 |
dxdylong = DblToLong(pItxInfo->dxdy);
|
|
108 |
dydylong = DblToLong(pItxInfo->dydy);
|
|
109 |
|
|
110 |
dx1 = pBounds->x1;
|
|
111 |
dy1 = pBounds->y1;
|
|
112 |
dx2 = pBounds->x2;
|
|
113 |
dy2 = pBounds->y2;
|
|
114 |
if ((dy2-dy1) > MAXEDGES) {
|
|
115 |
pEdgeBuf = malloc(2 * (dy2-dy1) * sizeof (*pEdges));
|
|
116 |
}
|
|
117 |
pEdges = pEdgeBuf;
|
|
118 |
|
|
119 |
drowxlong = (dx2-dx1-1) * dxdxlong;
|
|
120 |
drowylong = (dx2-dx1-1) * dydxlong;
|
|
121 |
|
|
122 |
while (dy1 < dy2) {
|
|
123 |
jlong xlong, ylong;
|
|
124 |
|
|
125 |
dx1 = pBounds->x1;
|
|
126 |
dx2 = pBounds->x2;
|
|
127 |
|
|
128 |
xlong = xbase;
|
|
129 |
ylong = ybase;
|
|
130 |
while (dx1 < dx2 &&
|
|
131 |
(((juint) WholeOfLong(ylong)) >= sh ||
|
|
132 |
((juint) WholeOfLong(xlong)) >= sw))
|
|
133 |
{
|
|
134 |
dx1++;
|
|
135 |
xlong += dxdxlong;
|
|
136 |
ylong += dydxlong;
|
|
137 |
}
|
|
138 |
|
|
139 |
xlong = xbase + drowxlong;
|
|
140 |
ylong = ybase + drowylong;
|
|
141 |
while (dx2 > dx1 &&
|
|
142 |
(((juint) WholeOfLong(ylong)) >= sh ||
|
|
143 |
((juint) WholeOfLong(xlong)) >= sw))
|
|
144 |
{
|
|
145 |
dx2--;
|
|
146 |
xlong -= dxdxlong;
|
|
147 |
ylong -= dydxlong;
|
|
148 |
}
|
|
149 |
|
|
150 |
*pEdges++ = dx1;
|
|
151 |
*pEdges++ = dx2;
|
|
152 |
|
|
153 |
/* Increment to next scanline */
|
|
154 |
xbase += dxdylong;
|
|
155 |
ybase += dydylong;
|
|
156 |
dy1++;
|
|
157 |
}
|
|
158 |
|
|
159 |
return pEdgeBuf;
|
|
160 |
}
|
|
161 |
|
|
162 |
/*
|
|
163 |
* Class: sun_java2d_loops_TransformHelper
|
|
164 |
* Method: Transform
|
|
165 |
* Signature: (Lsun/java2d/loops/MaskBlit;Lsun/java2d/SurfaceData;Lsun/java2d/SurfaceData;Ljava/awt/Composite;Lsun/java2d/pipe/Region;Ljava/awt/geom/AffineTransform;IIIIIIIII[I)V
|
|
166 |
*/
|
|
167 |
JNIEXPORT void JNICALL
|
|
168 |
Java_sun_java2d_loops_TransformHelper_Transform
|
|
169 |
(JNIEnv *env, jobject self,
|
|
170 |
jobject maskblit,
|
|
171 |
jobject srcData, jobject dstData,
|
|
172 |
jobject comp, jobject clip,
|
|
173 |
jobject itxform, jint txtype,
|
|
174 |
jint sx1, jint sy1, jint sx2, jint sy2,
|
|
175 |
jint dx1, jint dy1, jint dx2, jint dy2,
|
|
176 |
jintArray edgeArray, jint dxoff, jint dyoff)
|
|
177 |
{
|
|
178 |
SurfaceDataOps *srcOps;
|
|
179 |
SurfaceDataOps *dstOps;
|
|
180 |
SurfaceDataRasInfo srcInfo;
|
|
181 |
SurfaceDataRasInfo dstInfo;
|
|
182 |
NativePrimitive *pHelperPrim;
|
|
183 |
NativePrimitive *pMaskBlitPrim;
|
|
184 |
CompositeInfo compInfo;
|
|
185 |
RegionData clipInfo;
|
|
186 |
TransformInfo itxInfo;
|
|
187 |
jint maxlinepix;
|
|
188 |
TransformHelperFunc *pHelperFunc;
|
|
189 |
TransformInterpFunc *pInterpFunc;
|
|
190 |
jint edgebuf[MAXEDGES * 2];
|
|
191 |
jint *pEdges;
|
|
192 |
jdouble x, y;
|
|
193 |
jlong xbase, ybase;
|
|
194 |
jlong dxdxlong, dydxlong;
|
|
195 |
jlong dxdylong, dydylong;
|
|
196 |
|
|
197 |
#ifdef MAKE_STUBS
|
|
198 |
static int th_initialized;
|
|
199 |
|
|
200 |
/* For debugging only - used to swap in alternate funcs for perf testing */
|
|
201 |
if (!th_initialized) {
|
|
202 |
if (getenv("TXSTUB") != 0) {
|
|
203 |
pBilinearFunc = BilinearInterpStub;
|
|
204 |
pBicubicFunc = BicubicInterpStub;
|
|
205 |
} else if (getenv("TXNOVIS") != 0) {
|
|
206 |
pBilinearFunc = BilinearInterp;
|
|
207 |
pBicubicFunc = BicubicInterp;
|
|
208 |
}
|
|
209 |
th_initialized = 1;
|
|
210 |
}
|
|
211 |
#endif /* MAKE_STUBS */
|
|
212 |
|
|
213 |
pHelperPrim = GetNativePrim(env, self);
|
|
214 |
if (pHelperPrim == NULL) {
|
|
215 |
/* Should never happen... */
|
|
216 |
return;
|
|
217 |
}
|
|
218 |
pMaskBlitPrim = GetNativePrim(env, maskblit);
|
|
219 |
if (pMaskBlitPrim == NULL) {
|
|
220 |
/* Exception was thrown by GetNativePrim */
|
|
221 |
return;
|
|
222 |
}
|
|
223 |
if (pMaskBlitPrim->pCompType->getCompInfo != NULL) {
|
|
224 |
(*pMaskBlitPrim->pCompType->getCompInfo)(env, &compInfo, comp);
|
|
225 |
}
|
|
226 |
if (Region_GetInfo(env, clip, &clipInfo)) {
|
|
227 |
return;
|
|
228 |
}
|
|
229 |
|
|
230 |
srcOps = SurfaceData_GetOps(env, srcData);
|
|
231 |
dstOps = SurfaceData_GetOps(env, dstData);
|
|
232 |
if (srcOps == 0 || dstOps == 0) {
|
|
233 |
return;
|
|
234 |
}
|
|
235 |
|
|
236 |
/*
|
|
237 |
* Grab the appropriate pointer to the helper and interpolation
|
|
238 |
* routines and calculate the maximum number of destination pixels
|
|
239 |
* that can be processed in one intermediate buffer based on the
|
|
240 |
* size of the buffer and the number of samples needed per pixel.
|
|
241 |
*/
|
|
242 |
switch (txtype) {
|
|
243 |
case java_awt_image_AffineTransformOp_TYPE_NEAREST_NEIGHBOR:
|
|
244 |
pHelperFunc = pHelperPrim->funcs.transformhelpers->nnHelper;
|
|
245 |
pInterpFunc = NULL;
|
|
246 |
maxlinepix = LINE_SIZE;
|
|
247 |
break;
|
|
248 |
case java_awt_image_AffineTransformOp_TYPE_BILINEAR:
|
|
249 |
pHelperFunc = pHelperPrim->funcs.transformhelpers->blHelper;
|
|
250 |
pInterpFunc = pBilinearFunc;
|
|
251 |
maxlinepix = LINE_SIZE / 4;
|
|
252 |
break;
|
|
253 |
case java_awt_image_AffineTransformOp_TYPE_BICUBIC:
|
|
254 |
pHelperFunc = pHelperPrim->funcs.transformhelpers->bcHelper;
|
|
255 |
pInterpFunc = pBicubicFunc;
|
|
256 |
maxlinepix = LINE_SIZE / 16;
|
|
257 |
break;
|
|
258 |
}
|
|
259 |
|
|
260 |
srcInfo.bounds.x1 = sx1;
|
|
261 |
srcInfo.bounds.y1 = sy1;
|
|
262 |
srcInfo.bounds.x2 = sx2;
|
|
263 |
srcInfo.bounds.y2 = sy2;
|
|
264 |
dstInfo.bounds.x1 = dx1;
|
|
265 |
dstInfo.bounds.y1 = dy1;
|
|
266 |
dstInfo.bounds.x2 = dx2;
|
|
267 |
dstInfo.bounds.y2 = dy2;
|
|
268 |
SurfaceData_IntersectBounds(&dstInfo.bounds, &clipInfo.bounds);
|
|
269 |
if (srcOps->Lock(env, srcOps, &srcInfo, pHelperPrim->srcflags)
|
|
270 |
!= SD_SUCCESS)
|
|
271 |
{
|
|
272 |
return;
|
|
273 |
}
|
|
274 |
if (dstOps->Lock(env, dstOps, &dstInfo, pMaskBlitPrim->dstflags)
|
|
275 |
!= SD_SUCCESS)
|
|
276 |
{
|
|
277 |
SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
|
|
278 |
return;
|
|
279 |
}
|
|
280 |
Region_IntersectBounds(&clipInfo, &dstInfo.bounds);
|
|
281 |
|
|
282 |
Transform_GetInfo(env, itxform, &itxInfo);
|
|
283 |
dxdxlong = DblToLong(itxInfo.dxdx);
|
|
284 |
dydxlong = DblToLong(itxInfo.dydx);
|
|
285 |
dxdylong = DblToLong(itxInfo.dxdy);
|
|
286 |
dydylong = DblToLong(itxInfo.dydy);
|
|
287 |
x = dxoff+dstInfo.bounds.x1+0.5; /* Center of pixel x1 */
|
|
288 |
y = dyoff+dstInfo.bounds.y1+0.5; /* Center of pixel y1 */
|
|
289 |
Transform_transform(&itxInfo, &x, &y);
|
|
290 |
xbase = DblToLong(x);
|
|
291 |
ybase = DblToLong(y);
|
|
292 |
|
|
293 |
pEdges = calculateEdges(edgebuf, &dstInfo.bounds, &itxInfo,
|
|
294 |
xbase, ybase, sx2-sx1, sy2-sy1);
|
|
295 |
|
|
296 |
if (!Region_IsEmpty(&clipInfo)) {
|
|
297 |
srcOps->GetRasInfo(env, srcOps, &srcInfo);
|
|
298 |
dstOps->GetRasInfo(env, dstOps, &dstInfo);
|
|
299 |
if (srcInfo.rasBase && dstInfo.rasBase) {
|
|
300 |
union {
|
|
301 |
jlong align;
|
|
302 |
jint data[LINE_SIZE];
|
|
303 |
} rgb;
|
|
304 |
SurfaceDataBounds span;
|
|
305 |
|
|
306 |
Region_StartIteration(env, &clipInfo);
|
|
307 |
while (Region_NextIteration(&clipInfo, &span)) {
|
|
308 |
jlong rowxlong, rowylong;
|
|
309 |
void *pDst;
|
|
310 |
|
|
311 |
dy1 = span.y1;
|
|
312 |
dy2 = span.y2;
|
|
313 |
rowxlong = xbase + (dy1 - dstInfo.bounds.y1) * dxdylong;
|
|
314 |
rowylong = ybase + (dy1 - dstInfo.bounds.y1) * dydylong;
|
|
315 |
|
|
316 |
while (dy1 < dy2) {
|
|
317 |
jlong xlong, ylong;
|
|
318 |
|
|
319 |
/* Note - process at most one scanline at a time. */
|
|
320 |
|
|
321 |
dx1 = pEdges[(dy1 - dstInfo.bounds.y1) * 2];
|
|
322 |
dx2 = pEdges[(dy1 - dstInfo.bounds.y1) * 2 + 1];
|
|
323 |
if (dx1 < span.x1) dx1 = span.x1;
|
|
324 |
if (dx2 > span.x2) dx2 = span.x2;
|
|
325 |
|
|
326 |
/* All pixels from dx1 to dx2 have centers in bounds */
|
|
327 |
while (dx1 < dx2) {
|
|
328 |
/* Can process at most one buffer full at a time */
|
|
329 |
jint numpix = dx2 - dx1;
|
|
330 |
if (numpix > maxlinepix) {
|
|
331 |
numpix = maxlinepix;
|
|
332 |
}
|
|
333 |
|
|
334 |
xlong =
|
|
335 |
rowxlong + ((dx1 - dstInfo.bounds.x1) * dxdxlong);
|
|
336 |
ylong =
|
|
337 |
rowylong + ((dx1 - dstInfo.bounds.x1) * dydxlong);
|
|
338 |
|
|
339 |
/* Get IntArgbPre pixel data from source */
|
|
340 |
(*pHelperFunc)(&srcInfo,
|
|
341 |
rgb.data, numpix,
|
|
342 |
xlong, dxdxlong,
|
|
343 |
ylong, dydxlong);
|
|
344 |
|
|
345 |
/* Interpolate result pixels if needed */
|
|
346 |
if (pInterpFunc) {
|
|
347 |
(*pInterpFunc)(rgb.data, numpix,
|
|
348 |
FractOfLong(xlong-LongOneHalf),
|
|
349 |
FractOfLong(dxdxlong),
|
|
350 |
FractOfLong(ylong-LongOneHalf),
|
|
351 |
FractOfLong(dydxlong));
|
|
352 |
}
|
|
353 |
|
|
354 |
/* Store/Composite interpolated pixels into dest */
|
|
355 |
pDst = PtrCoord(dstInfo.rasBase,
|
|
356 |
dx1, dstInfo.pixelStride,
|
|
357 |
dy1, dstInfo.scanStride);
|
|
358 |
(*pMaskBlitPrim->funcs.maskblit)(pDst, rgb.data,
|
|
359 |
0, 0, 0,
|
|
360 |
numpix, 1,
|
|
361 |
&dstInfo, &srcInfo,
|
|
362 |
pMaskBlitPrim,
|
|
363 |
&compInfo);
|
|
364 |
|
|
365 |
/* Increment to next buffer worth of input pixels */
|
|
366 |
dx1 += maxlinepix;
|
|
367 |
}
|
|
368 |
|
|
369 |
/* Increment to next scanline */
|
|
370 |
rowxlong += dxdylong;
|
|
371 |
rowylong += dydylong;
|
|
372 |
dy1++;
|
|
373 |
}
|
|
374 |
}
|
|
375 |
Region_EndIteration(env, &clipInfo);
|
|
376 |
}
|
|
377 |
SurfaceData_InvokeRelease(env, dstOps, &dstInfo);
|
|
378 |
SurfaceData_InvokeRelease(env, srcOps, &srcInfo);
|
|
379 |
}
|
|
380 |
SurfaceData_InvokeUnlock(env, dstOps, &dstInfo);
|
|
381 |
SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
|
|
382 |
if (!JNU_IsNull(env, edgeArray)) {
|
|
383 |
(*env)->SetIntArrayRegion(env, edgeArray, 0, 1, &dstInfo.bounds.y1);
|
|
384 |
(*env)->SetIntArrayRegion(env, edgeArray, 1, 1, &dstInfo.bounds.y2);
|
|
385 |
(*env)->SetIntArrayRegion(env, edgeArray,
|
|
386 |
2, (dstInfo.bounds.y2 - dstInfo.bounds.y1)*2,
|
|
387 |
pEdges);
|
|
388 |
}
|
|
389 |
if (pEdges != edgebuf) {
|
|
390 |
free(pEdges);
|
|
391 |
}
|
|
392 |
}
|
|
393 |
|
|
394 |
#define BL_INTERP_V1_to_V2_by_F(v1, v2, f) \
|
|
395 |
(((v1)<<8) + ((v2)-(v1))*(f))
|
|
396 |
|
|
397 |
#define BL_ACCUM(comp) \
|
|
398 |
do { \
|
|
399 |
jint c1 = ((jubyte *) pRGB)[comp]; \
|
|
400 |
jint c2 = ((jubyte *) pRGB)[comp+4]; \
|
|
401 |
jint cR = BL_INTERP_V1_to_V2_by_F(c1, c2, xfactor); \
|
|
402 |
c1 = ((jubyte *) pRGB)[comp+8]; \
|
|
403 |
c2 = ((jubyte *) pRGB)[comp+12]; \
|
|
404 |
c2 = BL_INTERP_V1_to_V2_by_F(c1, c2, xfactor); \
|
|
405 |
cR = BL_INTERP_V1_to_V2_by_F(cR, c2, yfactor); \
|
|
406 |
((jubyte *)pRes)[comp] = (jubyte) ((cR + (1<<15)) >> 16); \
|
|
407 |
} while (0)
|
|
408 |
|
|
409 |
static void
|
|
410 |
BilinearInterp(jint *pRGB, jint numpix,
|
|
411 |
jint xfract, jint dxfract,
|
|
412 |
jint yfract, jint dyfract)
|
|
413 |
{
|
|
414 |
jint j;
|
|
415 |
jint *pRes = pRGB;
|
|
416 |
|
|
417 |
for (j = 0; j < numpix; j++) {
|
|
418 |
jint xfactor;
|
|
419 |
jint yfactor;
|
|
420 |
xfactor = URShift(xfract, 32-8);
|
|
421 |
yfactor = URShift(yfract, 32-8);
|
|
422 |
BL_ACCUM(0);
|
|
423 |
BL_ACCUM(1);
|
|
424 |
BL_ACCUM(2);
|
|
425 |
BL_ACCUM(3);
|
|
426 |
pRes++;
|
|
427 |
pRGB += 4;
|
|
428 |
xfract += dxfract;
|
|
429 |
yfract += dyfract;
|
|
430 |
}
|
|
431 |
}
|
|
432 |
|
|
433 |
#define SAT(val, max) \
|
|
434 |
do { \
|
|
435 |
val &= ~(val >> 31); /* negatives become 0 */ \
|
|
436 |
val -= max; /* only overflows are now positive */ \
|
|
437 |
val &= (val >> 31); /* positives become 0 */ \
|
|
438 |
val += max; /* range is now [0 -> max] */ \
|
|
439 |
} while (0)
|
|
440 |
|
|
441 |
#ifdef __sparc
|
|
442 |
/* For sparc, floating point multiplies are faster than integer */
|
|
443 |
#define BICUBIC_USE_DBL_LUT
|
|
444 |
#else
|
|
445 |
/* For x86, integer multiplies are faster than floating point */
|
|
446 |
/* Note that on x86 Linux the choice of best algorithm varies
|
|
447 |
* depending on the compiler optimization and the processor type.
|
|
448 |
* Currently, the sun/awt x86 Linux builds are not optimized so
|
|
449 |
* all the variations produce mediocre performance.
|
|
450 |
* For now we will use the choice that works best for the Windows
|
|
451 |
* build until the (lack of) optimization issues on Linux are resolved.
|
|
452 |
*/
|
|
453 |
#define BICUBIC_USE_INT_MATH
|
|
454 |
#endif
|
|
455 |
|
|
456 |
#ifdef BICUBIC_USE_DBL_CAST
|
|
457 |
|
|
458 |
#define BC_DblToCoeff(v) (v)
|
|
459 |
#define BC_COEFF_ONE 1.0
|
|
460 |
#define BC_TYPE jdouble
|
|
461 |
#define BC_V_HALF 0.5
|
|
462 |
#define BC_CompToV(v) ((jdouble) (v))
|
|
463 |
#define BC_STORE_COMPS(pRes) \
|
|
464 |
do { \
|
|
465 |
jint a = (jint) accumA; \
|
|
466 |
jint r = (jint) accumR; \
|
|
467 |
jint g = (jint) accumG; \
|
|
468 |
jint b = (jint) accumB; \
|
|
469 |
SAT(a, 255); \
|
|
470 |
SAT(r, a); \
|
|
471 |
SAT(g, a); \
|
|
472 |
SAT(b, a); \
|
|
473 |
*pRes = ((a << 24) | (r << 16) | (g << 8) | (b)); \
|
|
474 |
} while (0)
|
|
475 |
|
|
476 |
#endif /* BICUBIC_USE_DBL_CAST */
|
|
477 |
|
|
478 |
#ifdef BICUBIC_USE_DBL_LUT
|
|
479 |
|
|
480 |
#define ItoD1(v) ((jdouble) (v))
|
|
481 |
#define ItoD4(v) ItoD1(v), ItoD1(v+1), ItoD1(v+2), ItoD1(v+3)
|
|
482 |
#define ItoD16(v) ItoD4(v), ItoD4(v+4), ItoD4(v+8), ItoD4(v+12)
|
|
483 |
#define ItoD64(v) ItoD16(v), ItoD16(v+16), ItoD16(v+32), ItoD16(v+48)
|
|
484 |
|
|
485 |
static jdouble ItoD_table[] = {
|
|
486 |
ItoD64(0), ItoD64(64), ItoD64(128), ItoD64(192)
|
|
487 |
};
|
|
488 |
|
|
489 |
#define BC_DblToCoeff(v) (v)
|
|
490 |
#define BC_COEFF_ONE 1.0
|
|
491 |
#define BC_TYPE jdouble
|
|
492 |
#define BC_V_HALF 0.5
|
|
493 |
#define BC_CompToV(v) ItoD_table[v]
|
|
494 |
#define BC_STORE_COMPS(pRes) \
|
|
495 |
do { \
|
|
496 |
jint a = (jint) accumA; \
|
|
497 |
jint r = (jint) accumR; \
|
|
498 |
jint g = (jint) accumG; \
|
|
499 |
jint b = (jint) accumB; \
|
|
500 |
SAT(a, 255); \
|
|
501 |
SAT(r, a); \
|
|
502 |
SAT(g, a); \
|
|
503 |
SAT(b, a); \
|
|
504 |
*pRes = ((a << 24) | (r << 16) | (g << 8) | (b)); \
|
|
505 |
} while (0)
|
|
506 |
|
|
507 |
#endif /* BICUBIC_USE_DBL_LUT */
|
|
508 |
|
|
509 |
#ifdef BICUBIC_USE_INT_MATH
|
|
510 |
|
|
511 |
#define BC_DblToCoeff(v) ((jint) ((v) * 256))
|
|
512 |
#define BC_COEFF_ONE 256
|
|
513 |
#define BC_TYPE jint
|
|
514 |
#define BC_V_HALF (1 << 15)
|
|
515 |
#define BC_CompToV(v) ((jint) v)
|
|
516 |
#define BC_STORE_COMPS(pRes) \
|
|
517 |
do { \
|
|
518 |
accumA >>= 16; \
|
|
519 |
accumR >>= 16; \
|
|
520 |
accumG >>= 16; \
|
|
521 |
accumB >>= 16; \
|
|
522 |
SAT(accumA, 255); \
|
|
523 |
SAT(accumR, accumA); \
|
|
524 |
SAT(accumG, accumA); \
|
|
525 |
SAT(accumB, accumA); \
|
|
526 |
*pRes = ((accumA << 24) | (accumR << 16) | (accumG << 8) | (accumB)); \
|
|
527 |
} while (0)
|
|
528 |
|
|
529 |
#endif /* BICUBIC_USE_INT_MATH */
|
|
530 |
|
|
531 |
#define BC_ACCUM(index, ycindex, xcindex) \
|
|
532 |
do { \
|
|
533 |
BC_TYPE factor = bicubic_coeff[xcindex] * bicubic_coeff[ycindex]; \
|
|
534 |
int rgb; \
|
|
535 |
rgb = pRGB[index]; \
|
|
536 |
accumB += BC_CompToV((rgb >> 0) & 0xff) * factor; \
|
|
537 |
accumG += BC_CompToV((rgb >> 8) & 0xff) * factor; \
|
|
538 |
accumR += BC_CompToV((rgb >> 16) & 0xff) * factor; \
|
|
539 |
accumA += BC_CompToV((rgb >> 24) & 0xff) * factor; \
|
|
540 |
} while (0)
|
|
541 |
|
|
542 |
static BC_TYPE bicubic_coeff[513];
|
|
543 |
static jboolean bicubictableinited;
|
|
544 |
|
|
545 |
static void
|
|
546 |
init_bicubic_table(jdouble A)
|
|
547 |
{
|
|
548 |
/*
|
|
549 |
* The following formulas are designed to give smooth
|
|
550 |
* results when 'A' is -0.5 or -1.0.
|
|
551 |
*/
|
|
552 |
int i;
|
|
553 |
for (i = 0; i < 256; i++) {
|
|
554 |
/* r(x) = (A + 2)|x|^3 - (A + 3)|x|^2 + 1 , 0 <= |x| < 1 */
|
|
555 |
jdouble x = i / 256.0;
|
|
556 |
x = ((A+2)*x - (A+3))*x*x + 1;
|
|
557 |
bicubic_coeff[i] = BC_DblToCoeff(x);
|
|
558 |
}
|
|
559 |
|
|
560 |
for (; i < 384; i++) {
|
|
561 |
/* r(x) = A|x|^3 - 5A|x|^2 + 8A|x| - 4A , 1 <= |x| < 2 */
|
|
562 |
jdouble x = i / 256.0;
|
|
563 |
x = ((A*x - 5*A)*x + 8*A)*x - 4*A;
|
|
564 |
bicubic_coeff[i] = BC_DblToCoeff(x);
|
|
565 |
}
|
|
566 |
|
|
567 |
bicubic_coeff[384] = (BC_COEFF_ONE - bicubic_coeff[128]*2) / 2;
|
|
568 |
|
|
569 |
for (i++; i <= 512; i++) {
|
|
570 |
bicubic_coeff[i] = BC_COEFF_ONE - (bicubic_coeff[512-i] +
|
|
571 |
bicubic_coeff[i-256] +
|
|
572 |
bicubic_coeff[768-i]);
|
|
573 |
}
|
|
574 |
|
|
575 |
bicubictableinited = JNI_TRUE;
|
|
576 |
}
|
|
577 |
|
|
578 |
static void
|
|
579 |
BicubicInterp(jint *pRGB, jint numpix,
|
|
580 |
jint xfract, jint dxfract,
|
|
581 |
jint yfract, jint dyfract)
|
|
582 |
{
|
|
583 |
jint i;
|
|
584 |
jint *pRes = pRGB;
|
|
585 |
|
|
586 |
if (!bicubictableinited) {
|
|
587 |
init_bicubic_table(-0.5);
|
|
588 |
}
|
|
589 |
|
|
590 |
for (i = 0; i < numpix; i++) {
|
|
591 |
BC_TYPE accumA, accumR, accumG, accumB;
|
|
592 |
jint xfactor, yfactor;
|
|
593 |
|
|
594 |
xfactor = URShift(xfract, 32-8);
|
|
595 |
yfactor = URShift(yfract, 32-8);
|
|
596 |
accumA = accumR = accumG = accumB = BC_V_HALF;
|
|
597 |
BC_ACCUM(0, yfactor+256, xfactor+256);
|
|
598 |
BC_ACCUM(1, yfactor+256, xfactor+ 0);
|
|
599 |
BC_ACCUM(2, yfactor+256, 256-xfactor);
|
|
600 |
BC_ACCUM(3, yfactor+256, 512-xfactor);
|
|
601 |
BC_ACCUM(4, yfactor+ 0, xfactor+256);
|
|
602 |
BC_ACCUM(5, yfactor+ 0, xfactor+ 0);
|
|
603 |
BC_ACCUM(6, yfactor+ 0, 256-xfactor);
|
|
604 |
BC_ACCUM(7, yfactor+ 0, 512-xfactor);
|
|
605 |
BC_ACCUM(8, 256-yfactor, xfactor+256);
|
|
606 |
BC_ACCUM(9, 256-yfactor, xfactor+ 0);
|
|
607 |
BC_ACCUM(10, 256-yfactor, 256-xfactor);
|
|
608 |
BC_ACCUM(11, 256-yfactor, 512-xfactor);
|
|
609 |
BC_ACCUM(12, 512-yfactor, xfactor+256);
|
|
610 |
BC_ACCUM(13, 512-yfactor, xfactor+ 0);
|
|
611 |
BC_ACCUM(14, 512-yfactor, 256-xfactor);
|
|
612 |
BC_ACCUM(15, 512-yfactor, 512-xfactor);
|
|
613 |
BC_STORE_COMPS(pRes);
|
|
614 |
pRes++;
|
|
615 |
pRGB += 16;
|
|
616 |
xfract += dxfract;
|
|
617 |
yfract += dyfract;
|
|
618 |
}
|
|
619 |
}
|
|
620 |
|
|
621 |
#ifdef MAKE_STUBS
|
|
622 |
|
|
623 |
static void
|
|
624 |
BilinearInterpStub(jint *pRGBbase, jint numpix,
|
|
625 |
jint xfract, jint dxfract,
|
|
626 |
jint yfract, jint dyfract)
|
|
627 |
{
|
|
628 |
jint *pRGB = pRGBbase;
|
|
629 |
while (--numpix >= 0) {
|
|
630 |
*pRGBbase = *pRGB;
|
|
631 |
pRGBbase += 1;
|
|
632 |
pRGB += 4;
|
|
633 |
}
|
|
634 |
}
|
|
635 |
|
|
636 |
static void
|
|
637 |
BicubicInterpStub(jint *pRGBbase, jint numpix,
|
|
638 |
jint xfract, jint dxfract,
|
|
639 |
jint yfract, jint dyfract)
|
|
640 |
{
|
|
641 |
jint *pRGB = pRGBbase+5;
|
|
642 |
while (--numpix >= 0) {
|
|
643 |
*pRGBbase = *pRGB;
|
|
644 |
pRGBbase += 1;
|
|
645 |
pRGB += 16;
|
|
646 |
}
|
|
647 |
}
|
|
648 |
|
|
649 |
#endif /* MAKE_STUBS */
|