jdk/src/share/native/sun/java2d/loops/TransformHelper.c
author serb
Fri, 11 Apr 2014 16:18:58 +0400
changeset 24178 2190139aa21b
parent 23010 6dadb192ad81
permissions -rw-r--r--
8039418: [macosx] Calling JNI functions in the scope of Get/ReleasePrimitiveArrayCritical Reviewed-by: bae, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24178
2190139aa21b 8039418: [macosx] Calling JNI functions in the scope of Get/ReleasePrimitiveArrayCritical
serb
parents: 23010
diff changeset
     2
 * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "math.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "GraphicsPrimitiveMgr.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "Region.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "sun_java2d_loops_TransformHelper.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "java_awt_image_AffineTransformOp.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The stub functions replace the bilinear and bicubic interpolation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * functions with NOP versions so that the performance of the helper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * functions that fetch the data can be more directly tested.  They
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * are not compiled or enabled by default.  Change the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * #undef to a #define to build the stub functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * When compiled, they are enabled by the environment variable TXSTUB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * When compiled, there is also code to disable the VIS versions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * use the C versions in this file in their place by defining the TXNOVIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * environment variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#undef MAKE_STUBS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/* The number of IntArgbPre samples to store in the temporary buffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#define LINE_SIZE       2048
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/* The size of a stack allocated buffer to hold edge coordinates (see below). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#define MAXEDGES 1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/* Declare the software interpolation functions. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
static TransformInterpFunc BilinearInterp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
static TransformInterpFunc BicubicInterp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
#ifdef MAKE_STUBS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
/* Optionally Declare the stub interpolation functions. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
static TransformInterpFunc BilinearInterpStub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
static TransformInterpFunc BicubicInterpStub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
#endif /* MAKE_STUBS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Initially choose the software interpolation functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * These choices can be overridden by platform code that runs during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * primitive registration phase of initialization by storing pointers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * better functions in these pointers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * Compiling the stubs also turns on code below that can re-install the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * software functions or stub functions on the first call to this primitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
TransformInterpFunc *pBilinearFunc = BilinearInterp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
TransformInterpFunc *pBicubicFunc = BicubicInterp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
/*
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    78
 * The dxydxy parameters of the inverse transform determine how
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    79
 * quickly we step through the source image.  For tiny scale
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    80
 * factors (on the order of 1E-16 or so) the stepping distances
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    81
 * are huge.  The image has been scaled so small that stepping
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    82
 * a single pixel in device space moves the sampling point by
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    83
 * billions (or more) pixels in the source image space.  These
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    84
 * huge stepping values can overflow the whole part of the longs
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    85
 * we use for the fixed point stepping equations and so we need
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    86
 * a more robust solution.  We could simply iterate over every
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    87
 * device pixel, use the inverse transform to transform it back
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    88
 * into the source image coordinate system and then test it for
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    89
 * being in range and sample pixel-by-pixel, but that is quite
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    90
 * a bit more expensive.  Fortunately, if the scale factors are
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    91
 * so tiny that we overflow our long values then the number of
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    92
 * pixels we are planning to visit should be very tiny.  The only
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    93
 * exception to that rule is if the scale factor along one
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    94
 * dimension is tiny (creating the huge stepping values), and
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    95
 * the scale factor along the other dimension is fairly regular
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    96
 * or an up-scale.  In that case we have a lot of pixels along
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    97
 * the direction of the larger axis to sample, but few along the
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    98
 * smaller axis.  Though, pessimally, with an added shear factor
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
    99
 * such a linearly tiny image could have bounds that cover a large
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   100
 * number of pixels.  Such odd transformations should be very
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   101
 * rare and the absolute limit on calculations would involve a
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   102
 * single reverse transform of every pixel in the output image
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   103
 * which is not fast, but it should not cause an undue stall
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   104
 * of the rendering software.
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   105
 *
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   106
 * The specific test we will use is to calculate the inverse
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   107
 * transformed values of every corner of the destination bounds
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   108
 * (in order to be user-clip independent) and if we can
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   109
 * perform a fixed-point-long inverse transform of all of
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   110
 * those points without overflowing we will use the fast
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   111
 * fixed point algorithm.  Otherwise we will use the safe
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   112
 * per-pixel transform algorithm.
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   113
 * The 4 corners are 0,0, 0,dsth, dstw,0, dstw,dsth
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   114
 * Transformed they are:
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   115
 *     tx,               ty
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   116
 *     tx       +dxdy*H, ty       +dydy*H
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   117
 *     tx+dxdx*W,        ty+dydx*W
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   118
 *     tx+dxdx*W+dxdy*H, ty+dydx*W+dydy*H
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   119
 */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   120
/* We reject coordinates not less than 1<<30 so that the distance between */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   121
/* any 2 of them is less than 1<<31 which would overflow into the sign */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   122
/* bit of a signed long value used to represent fixed point coordinates. */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   123
#define TX_FIXED_UNSAFE(v)  (fabs(v) >= (1<<30))
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   124
static jboolean
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   125
checkOverflow(jint dxoff, jint dyoff,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   126
              SurfaceDataBounds *pBounds,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   127
              TransformInfo *pItxInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   128
              jdouble *retx, jdouble *rety)
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   129
{
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   130
    jdouble x, y;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   131
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   132
    x = dxoff+pBounds->x1+0.5; /* Center of pixel x1 */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   133
    y = dyoff+pBounds->y1+0.5; /* Center of pixel y1 */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   134
    Transform_transform(pItxInfo, &x, &y);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   135
    *retx = x;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   136
    *rety = y;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   137
    if (TX_FIXED_UNSAFE(x) || TX_FIXED_UNSAFE(y)) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   138
        return JNI_TRUE;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   139
    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   140
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   141
    x = dxoff+pBounds->x2-0.5; /* Center of pixel x2-1 */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   142
    y = dyoff+pBounds->y1+0.5; /* Center of pixel y1 */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   143
    Transform_transform(pItxInfo, &x, &y);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   144
    if (TX_FIXED_UNSAFE(x) || TX_FIXED_UNSAFE(y)) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   145
        return JNI_TRUE;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   146
    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   147
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   148
    x = dxoff+pBounds->x1+0.5; /* Center of pixel x1 */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   149
    y = dyoff+pBounds->y2-0.5; /* Center of pixel y2-1 */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   150
    Transform_transform(pItxInfo, &x, &y);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   151
    if (TX_FIXED_UNSAFE(x) || TX_FIXED_UNSAFE(y)) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   152
        return JNI_TRUE;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   153
    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   154
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   155
    x = dxoff+pBounds->x2-0.5; /* Center of pixel x2-1 */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   156
    y = dyoff+pBounds->y2-0.5; /* Center of pixel y2-1 */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   157
    Transform_transform(pItxInfo, &x, &y);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   158
    if (TX_FIXED_UNSAFE(x) || TX_FIXED_UNSAFE(y)) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   159
        return JNI_TRUE;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   160
    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   161
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   162
    return JNI_FALSE;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   163
}
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   164
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   165
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * Fill the edge buffer with pairs of coordinates representing the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * left and right pixels of the destination surface that should be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * on each scanline, clipped to the bounds parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * The number of scanlines to calculate is implied by the bounds parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * Only pixels that map back through the specified (inverse) transform to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * source coordinate that falls within the (0, 0, sw, sh) bounds of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * source image should be processed.
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   173
 * pEdges points to an array of jints that holds 2 + numedges*2 values where
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   174
 * numedges should match (pBounds->y2 - pBounds->y1).
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   175
 * The first two jints in pEdges should be set to y1 and y2 and every pair
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   176
 * of jints after that represent the xmin,xmax of all pixels in range of
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   177
 * the transformed blit for the corresponding scanline.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 */
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   179
static void
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   180
calculateEdges(jint *pEdges,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
               SurfaceDataBounds *pBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
               TransformInfo *pItxInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
               jlong xbase, jlong ybase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
               juint sw, juint sh)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    jlong dxdxlong, dydxlong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    jlong dxdylong, dydylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    jlong drowxlong, drowylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    jint dx1, dy1, dx2, dy2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    dxdxlong = DblToLong(pItxInfo->dxdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    dydxlong = DblToLong(pItxInfo->dydx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    dxdylong = DblToLong(pItxInfo->dxdy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    dydylong = DblToLong(pItxInfo->dydy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    dx1 = pBounds->x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    dy1 = pBounds->y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    dx2 = pBounds->x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    dy2 = pBounds->y2;
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   200
    *pEdges++ = dy1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   201
    *pEdges++ = dy2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    drowxlong = (dx2-dx1-1) * dxdxlong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    drowylong = (dx2-dx1-1) * dydxlong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    while (dy1 < dy2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        jlong xlong, ylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        dx1 = pBounds->x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        dx2 = pBounds->x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        xlong = xbase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        ylong = ybase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        while (dx1 < dx2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
               (((juint) WholeOfLong(ylong)) >= sh ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                ((juint) WholeOfLong(xlong)) >= sw))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            dx1++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            xlong += dxdxlong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            ylong += dydxlong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        xlong = xbase + drowxlong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        ylong = ybase + drowylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        while (dx2 > dx1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
               (((juint) WholeOfLong(ylong)) >= sh ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                ((juint) WholeOfLong(xlong)) >= sw))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            dx2--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            xlong -= dxdxlong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            ylong -= dydxlong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        *pEdges++ = dx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        *pEdges++ = dx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        /* Increment to next scanline */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        xbase += dxdylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        ybase += dydylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        dy1++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   242
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   244
static void
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   245
Transform_SafeHelper(JNIEnv *env,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   246
                     SurfaceDataOps *srcOps,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   247
                     SurfaceDataOps *dstOps,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   248
                     SurfaceDataRasInfo *pSrcInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   249
                     SurfaceDataRasInfo *pDstInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   250
                     NativePrimitive *pMaskBlitPrim,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   251
                     CompositeInfo *pCompInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   252
                     TransformHelperFunc *pHelperFunc,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   253
                     TransformInterpFunc *pInterpFunc,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   254
                     RegionData *pClipInfo, TransformInfo *pItxInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   255
                     jint *pData, jint *pEdges,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   256
                     jint dxoff, jint dyoff, jint sw, jint sh);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * Class:     sun_java2d_loops_TransformHelper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * Method:    Transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
Java_sun_java2d_loops_TransformHelper_Transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    (JNIEnv *env, jobject self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     jobject maskblit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     jobject srcData, jobject dstData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     jobject comp, jobject clip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     jobject itxform, jint txtype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     jint sx1, jint sy1, jint sx2, jint sy2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     jint dx1, jint dy1, jint dx2, jint dy2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     jintArray edgeArray, jint dxoff, jint dyoff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    SurfaceDataOps *srcOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    SurfaceDataOps *dstOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    SurfaceDataRasInfo srcInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    SurfaceDataRasInfo dstInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    NativePrimitive *pHelperPrim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    NativePrimitive *pMaskBlitPrim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    CompositeInfo compInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    RegionData clipInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    TransformInfo itxInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    jint maxlinepix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    TransformHelperFunc *pHelperFunc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    TransformInterpFunc *pInterpFunc;
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   286
    jdouble xorig, yorig;
10910
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   287
    jlong numedges;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    jint *pEdges;
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   289
    jint edgebuf[2 + MAXEDGES * 2];
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   290
    union {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   291
        jlong align;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   292
        jint data[LINE_SIZE];
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   293
    } rgb;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
#ifdef MAKE_STUBS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    static int th_initialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /* For debugging only - used to swap in alternate funcs for perf testing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    if (!th_initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (getenv("TXSTUB") != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            pBilinearFunc = BilinearInterpStub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            pBicubicFunc = BicubicInterpStub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } else if (getenv("TXNOVIS") != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            pBilinearFunc = BilinearInterp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            pBicubicFunc = BicubicInterp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        th_initialized = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
#endif /* MAKE_STUBS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    pHelperPrim = GetNativePrim(env, self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    if (pHelperPrim == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        /* Should never happen... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    pMaskBlitPrim = GetNativePrim(env, maskblit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    if (pMaskBlitPrim == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        /* Exception was thrown by GetNativePrim */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    if (pMaskBlitPrim->pCompType->getCompInfo != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        (*pMaskBlitPrim->pCompType->getCompInfo)(env, &compInfo, comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    if (Region_GetInfo(env, clip, &clipInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    srcOps = SurfaceData_GetOps(env, srcData);
20421
8fab9959a1bc 8025280: [parfait] warnings from b107 for jdk.src.share.native.sun.java2d.loops: JNI exception pending, JNI critical region violation
jchen
parents: 15966
diff changeset
   329
    if (srcOps == 0) {
8fab9959a1bc 8025280: [parfait] warnings from b107 for jdk.src.share.native.sun.java2d.loops: JNI exception pending, JNI critical region violation
jchen
parents: 15966
diff changeset
   330
        return;
8fab9959a1bc 8025280: [parfait] warnings from b107 for jdk.src.share.native.sun.java2d.loops: JNI exception pending, JNI critical region violation
jchen
parents: 15966
diff changeset
   331
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    dstOps = SurfaceData_GetOps(env, dstData);
20421
8fab9959a1bc 8025280: [parfait] warnings from b107 for jdk.src.share.native.sun.java2d.loops: JNI exception pending, JNI critical region violation
jchen
parents: 15966
diff changeset
   333
    if (dstOps == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Grab the appropriate pointer to the helper and interpolation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * routines and calculate the maximum number of destination pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * that can be processed in one intermediate buffer based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * size of the buffer and the number of samples needed per pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    switch (txtype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    case java_awt_image_AffineTransformOp_TYPE_NEAREST_NEIGHBOR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        pHelperFunc = pHelperPrim->funcs.transformhelpers->nnHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        pInterpFunc = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        maxlinepix = LINE_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    case java_awt_image_AffineTransformOp_TYPE_BILINEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        pHelperFunc = pHelperPrim->funcs.transformhelpers->blHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        pInterpFunc = pBilinearFunc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        maxlinepix = LINE_SIZE / 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    case java_awt_image_AffineTransformOp_TYPE_BICUBIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        pHelperFunc = pHelperPrim->funcs.transformhelpers->bcHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        pInterpFunc = pBicubicFunc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        maxlinepix = LINE_SIZE / 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        break;
15966
258abbf3f19f 8008173: [parfait] #1173 Uninitialised variable -- TransformHelper.cpp
jgodinez
parents: 10910
diff changeset
   359
    default:
258abbf3f19f 8008173: [parfait] #1173 Uninitialised variable -- TransformHelper.cpp
jgodinez
parents: 10910
diff changeset
   360
        // Should not happen, but just in case.
258abbf3f19f 8008173: [parfait] #1173 Uninitialised variable -- TransformHelper.cpp
jgodinez
parents: 10910
diff changeset
   361
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    srcInfo.bounds.x1 = sx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    srcInfo.bounds.y1 = sy1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    srcInfo.bounds.x2 = sx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    srcInfo.bounds.y2 = sy2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    dstInfo.bounds.x1 = dx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    dstInfo.bounds.y1 = dy1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    dstInfo.bounds.x2 = dx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    dstInfo.bounds.y2 = dy2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    SurfaceData_IntersectBounds(&dstInfo.bounds, &clipInfo.bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    if (srcOps->Lock(env, srcOps, &srcInfo, pHelperPrim->srcflags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        != SD_SUCCESS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    {
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   376
        /* edgeArray should already contain zeros for min/maxy */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    if (dstOps->Lock(env, dstOps, &dstInfo, pMaskBlitPrim->dstflags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        != SD_SUCCESS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   383
        /* edgeArray should already contain zeros for min/maxy */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    Region_IntersectBounds(&clipInfo, &dstInfo.bounds);
24178
2190139aa21b 8039418: [macosx] Calling JNI functions in the scope of Get/ReleasePrimitiveArrayCritical
serb
parents: 23010
diff changeset
   387
    Transform_GetInfo(env, itxform, &itxInfo);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
10910
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   389
    numedges = (((jlong) dstInfo.bounds.y2) - ((jlong) dstInfo.bounds.y1));
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   390
    if (numedges <= 0) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   391
        pEdges = NULL;
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   392
    } else if (!JNU_IsNull(env, edgeArray)) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   393
        /*
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   394
         * Ideally Java should allocate an array large enough, but if
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   395
         * we ever have a miscommunication about the number of edge
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   396
         * lines, or if the Java array calculation should overflow to
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   397
         * a positive number and succeed in allocating an array that
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   398
         * is too small, we need to verify that it can still hold the
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   399
         * number of integers that we plan to store to be safe.
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   400
         */
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   401
        jsize edgesize = (*env)->GetArrayLength(env, edgeArray);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   402
        /* (edgesize/2 - 1) should avoid any overflow or underflow. */
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   403
        pEdges = (((edgesize / 2) - 1) >= numedges)
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   404
            ? (*env)->GetPrimitiveArrayCritical(env, edgeArray, NULL)
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   405
            : NULL;
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   406
    } else if (numedges > MAXEDGES) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   407
        /* numedges variable (jlong) can be at most ((1<<32)-1) */
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   408
        /* memsize can overflow a jint, but not a jlong */
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   409
        jlong memsize = ((numedges * 2) + 2) * sizeof(*pEdges);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   410
        pEdges = (memsize == ((size_t) memsize))
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   411
            ? malloc((size_t) memsize)
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   412
            : NULL;
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   413
    } else {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   414
        pEdges = edgebuf;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   415
    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   416
10910
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   417
    if (pEdges == NULL) {
20421
8fab9959a1bc 8025280: [parfait] warnings from b107 for jdk.src.share.native.sun.java2d.loops: JNI exception pending, JNI critical region violation
jchen
parents: 15966
diff changeset
   418
        if (!(*env)->ExceptionCheck(env) && numedges > 0) {
10910
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   419
            JNU_ThrowInternalError(env, "Unable to allocate edge list");
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   420
        }
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   421
        SurfaceData_InvokeUnlock(env, dstOps, &dstInfo);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   422
        SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   423
        /* edgeArray should already contain zeros for min/maxy */
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   424
        return;
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   425
    }
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   426
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    if (!Region_IsEmpty(&clipInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        srcOps->GetRasInfo(env, srcOps, &srcInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        dstOps->GetRasInfo(env, dstOps, &dstInfo);
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   431
        if (srcInfo.rasBase == NULL || dstInfo.rasBase == NULL) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   432
            pEdges[0] = pEdges[1] = 0;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   433
        } else if (checkOverflow(dxoff, dyoff, &dstInfo.bounds,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   434
                                 &itxInfo, &xorig, &yorig))
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   435
        {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   436
            Transform_SafeHelper(env, srcOps, dstOps,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   437
                                 &srcInfo, &dstInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   438
                                 pMaskBlitPrim, &compInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   439
                                 pHelperFunc, pInterpFunc,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   440
                                 &clipInfo, &itxInfo, rgb.data, pEdges,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   441
                                 dxoff, dyoff, sx2-sx1, sy2-sy1);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   442
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            SurfaceDataBounds span;
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   444
            jlong dxdxlong, dydxlong;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   445
            jlong dxdylong, dydylong;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   446
            jlong xbase, ybase;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   447
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   448
            dxdxlong = DblToLong(itxInfo.dxdx);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   449
            dydxlong = DblToLong(itxInfo.dydx);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   450
            dxdylong = DblToLong(itxInfo.dxdy);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   451
            dydylong = DblToLong(itxInfo.dydy);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   452
            xbase = DblToLong(xorig);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   453
            ybase = DblToLong(yorig);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   454
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   455
            calculateEdges(pEdges, &dstInfo.bounds, &itxInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   456
                           xbase, ybase, sx2-sx1, sy2-sy1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            Region_StartIteration(env, &clipInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            while (Region_NextIteration(&clipInfo, &span)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                jlong rowxlong, rowylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                void *pDst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                dy1 = span.y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                dy2 = span.y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                rowxlong = xbase + (dy1 - dstInfo.bounds.y1) * dxdylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                rowylong = ybase + (dy1 - dstInfo.bounds.y1) * dydylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                while (dy1 < dy2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    jlong xlong, ylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    /* Note - process at most one scanline at a time. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   473
                    dx1 = pEdges[(dy1 - dstInfo.bounds.y1) * 2 + 2];
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   474
                    dx2 = pEdges[(dy1 - dstInfo.bounds.y1) * 2 + 3];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    if (dx1 < span.x1) dx1 = span.x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    if (dx2 > span.x2) dx2 = span.x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    /* All pixels from dx1 to dx2 have centers in bounds */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    while (dx1 < dx2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        /* Can process at most one buffer full at a time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                        jint numpix = dx2 - dx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                        if (numpix > maxlinepix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                            numpix = maxlinepix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        xlong =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                            rowxlong + ((dx1 - dstInfo.bounds.x1) * dxdxlong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        ylong =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                            rowylong + ((dx1 - dstInfo.bounds.x1) * dydxlong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        /* Get IntArgbPre pixel data from source */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        (*pHelperFunc)(&srcInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                       rgb.data, numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                       xlong, dxdxlong,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                       ylong, dydxlong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                        /* Interpolate result pixels if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                        if (pInterpFunc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                            (*pInterpFunc)(rgb.data, numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                                           FractOfLong(xlong-LongOneHalf),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                                           FractOfLong(dxdxlong),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                                           FractOfLong(ylong-LongOneHalf),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                                           FractOfLong(dydxlong));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        /* Store/Composite interpolated pixels into dest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        pDst = PtrCoord(dstInfo.rasBase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                                        dx1, dstInfo.pixelStride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                                        dy1, dstInfo.scanStride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                        (*pMaskBlitPrim->funcs.maskblit)(pDst, rgb.data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                                         0, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                                         numpix, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                                         &dstInfo, &srcInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                                         pMaskBlitPrim,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                                         &compInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        /* Increment to next buffer worth of input pixels */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        dx1 += maxlinepix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    /* Increment to next scanline */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    rowxlong += dxdylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    rowylong += dydylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    dy1++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            Region_EndIteration(env, &clipInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        SurfaceData_InvokeRelease(env, dstOps, &dstInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        SurfaceData_InvokeRelease(env, srcOps, &srcInfo);
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   531
    } else {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   532
        pEdges[0] = pEdges[1] = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
10910
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   534
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   535
    if (!JNU_IsNull(env, edgeArray)) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   536
        (*env)->ReleasePrimitiveArrayCritical(env, edgeArray, pEdges, 0);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   537
    } else if (pEdges != edgebuf) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   538
        free(pEdges);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   539
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    SurfaceData_InvokeUnlock(env, dstOps, &dstInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   544
static void
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   545
Transform_SafeHelper(JNIEnv *env,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   546
                     SurfaceDataOps *srcOps,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   547
                     SurfaceDataOps *dstOps,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   548
                     SurfaceDataRasInfo *pSrcInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   549
                     SurfaceDataRasInfo *pDstInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   550
                     NativePrimitive *pMaskBlitPrim,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   551
                     CompositeInfo *pCompInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   552
                     TransformHelperFunc *pHelperFunc,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   553
                     TransformInterpFunc *pInterpFunc,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   554
                     RegionData *pClipInfo, TransformInfo *pItxInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   555
                     jint *pData, jint *pEdges,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   556
                     jint dxoff, jint dyoff, jint sw, jint sh)
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   557
{
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   558
    SurfaceDataBounds span;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   559
    jint dx1, dx2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   560
    jint dy1, dy2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   561
    jint i, iy;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   562
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   563
    dy1 = pDstInfo->bounds.y1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   564
    dy2 = pDstInfo->bounds.y2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   565
    dx1 = pDstInfo->bounds.x1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   566
    dx2 = pDstInfo->bounds.x2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   567
    pEdges[0] = dy1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   568
    pEdges[1] = dy2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   569
    for (iy = dy1; iy < dy2; iy++) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   570
        jint i = (iy - dy1) * 2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   571
        /* row spans are set to max,min until we find a pixel in range below */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   572
        pEdges[i + 2] = dx2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   573
        pEdges[i + 3] = dx1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   574
    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   575
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   576
    Region_StartIteration(env, pClipInfo);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   577
    while (Region_NextIteration(pClipInfo, &span)) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   578
        dy1 = span.y1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   579
        dy2 = span.y2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   580
        while (dy1 < dy2) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   581
            dx1 = span.x1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   582
            dx2 = span.x2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   583
            i = (dy1 - pDstInfo->bounds.y1) * 2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   584
            while (dx1 < dx2) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   585
                jdouble x, y;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   586
                jlong xlong, ylong;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   587
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   588
                x = dxoff + dx1 + 0.5;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   589
                y = dyoff + dy1 + 0.5;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   590
                Transform_transform(pItxInfo, &x, &y);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   591
                xlong = DblToLong(x);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   592
                ylong = DblToLong(y);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   593
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   594
                /* Process only pixels with centers in bounds
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   595
                 * Test double values to avoid overflow in conversion
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   596
                 * to long values and then also test the long values
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   597
                 * in case they rounded up and out of bounds during
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   598
                 * the conversion.
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   599
                 */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   600
                if (x >= 0 && y >= 0 && x < sw && y < sh &&
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   601
                    WholeOfLong(xlong) < sw &&
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   602
                    WholeOfLong(ylong) < sh)
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   603
                {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   604
                    void *pDst;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   605
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   606
                    if (pEdges[i + 2] > dx1) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   607
                        pEdges[i + 2] = dx1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   608
                    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   609
                    if (pEdges[i + 3] <= dx1) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   610
                        pEdges[i + 3] = dx1 + 1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   611
                    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   612
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   613
                    /* Get IntArgbPre pixel data from source */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   614
                    (*pHelperFunc)(pSrcInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   615
                                   pData, 1,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   616
                                   xlong, 0,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   617
                                   ylong, 0);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   618
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   619
                    /* Interpolate result pixels if needed */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   620
                    if (pInterpFunc) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   621
                        (*pInterpFunc)(pData, 1,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   622
                                       FractOfLong(xlong-LongOneHalf), 0,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   623
                                       FractOfLong(ylong-LongOneHalf), 0);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   624
                    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   625
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   626
                    /* Store/Composite interpolated pixels into dest */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   627
                    pDst = PtrCoord(pDstInfo->rasBase,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   628
                                    dx1, pDstInfo->pixelStride,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   629
                                    dy1, pDstInfo->scanStride);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   630
                    (*pMaskBlitPrim->funcs.maskblit)(pDst, pData,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   631
                                                     0, 0, 0,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   632
                                                     1, 1,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   633
                                                     pDstInfo, pSrcInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   634
                                                     pMaskBlitPrim,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   635
                                                     pCompInfo);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   636
                }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   637
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   638
                /* Increment to next input pixel */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   639
                dx1++;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   640
            }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   641
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   642
            /* Increment to next scanline */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   643
            dy1++;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   644
        }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   645
    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   646
    Region_EndIteration(env, pClipInfo);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   647
}
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   648
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
#define BL_INTERP_V1_to_V2_by_F(v1, v2, f) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    (((v1)<<8) + ((v2)-(v1))*(f))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
#define BL_ACCUM(comp) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        jint c1 = ((jubyte *) pRGB)[comp]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        jint c2 = ((jubyte *) pRGB)[comp+4]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        jint cR = BL_INTERP_V1_to_V2_by_F(c1, c2, xfactor); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        c1 = ((jubyte *) pRGB)[comp+8]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        c2 = ((jubyte *) pRGB)[comp+12]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        c2 = BL_INTERP_V1_to_V2_by_F(c1, c2, xfactor); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        cR = BL_INTERP_V1_to_V2_by_F(cR, c2, yfactor); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        ((jubyte *)pRes)[comp] = (jubyte) ((cR + (1<<15)) >> 16); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
BilinearInterp(jint *pRGB, jint numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
               jint xfract, jint dxfract,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
               jint yfract, jint dyfract)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    jint j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    jint *pRes = pRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    for (j = 0; j < numpix; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        jint xfactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        jint yfactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        xfactor = URShift(xfract, 32-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        yfactor = URShift(yfract, 32-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        BL_ACCUM(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        BL_ACCUM(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        BL_ACCUM(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        BL_ACCUM(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        pRes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        pRGB += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        xfract += dxfract;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        yfract += dyfract;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
#define SAT(val, max) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        val &= ~(val >> 31);  /* negatives become 0 */ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        val -= max;           /* only overflows are now positive */ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        val &= (val >> 31);   /* positives become 0 */ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        val += max;           /* range is now [0 -> max] */ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
#ifdef __sparc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
/* For sparc, floating point multiplies are faster than integer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
#define BICUBIC_USE_DBL_LUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
/* For x86, integer multiplies are faster than floating point */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
/* Note that on x86 Linux the choice of best algorithm varies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
 * depending on the compiler optimization and the processor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
 * Currently, the sun/awt x86 Linux builds are not optimized so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
 * all the variations produce mediocre performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
 * For now we will use the choice that works best for the Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
 * build until the (lack of) optimization issues on Linux are resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
#define BICUBIC_USE_INT_MATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
#ifdef BICUBIC_USE_DBL_CAST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
#define BC_DblToCoeff(v)        (v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
#define BC_COEFF_ONE            1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
#define BC_TYPE                 jdouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
#define BC_V_HALF               0.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
#define BC_CompToV(v)           ((jdouble) (v))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
#define BC_STORE_COMPS(pRes) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        jint a = (jint) accumA; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        jint r = (jint) accumR; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        jint g = (jint) accumG; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        jint b = (jint) accumB; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        SAT(a, 255); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        SAT(r, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        SAT(g, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        SAT(b, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        *pRes = ((a << 24) | (r << 16) | (g <<  8) | (b)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
#endif /* BICUBIC_USE_DBL_CAST */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
#ifdef BICUBIC_USE_DBL_LUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
#define ItoD1(v)    ((jdouble) (v))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
#define ItoD4(v)    ItoD1(v),  ItoD1(v+1),   ItoD1(v+2),   ItoD1(v+3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
#define ItoD16(v)   ItoD4(v),  ItoD4(v+4),   ItoD4(v+8),   ItoD4(v+12)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
#define ItoD64(v)   ItoD16(v), ItoD16(v+16), ItoD16(v+32), ItoD16(v+48)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
static jdouble ItoD_table[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    ItoD64(0), ItoD64(64), ItoD64(128), ItoD64(192)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
#define BC_DblToCoeff(v)        (v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
#define BC_COEFF_ONE            1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
#define BC_TYPE                 jdouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
#define BC_V_HALF               0.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
#define BC_CompToV(v)           ItoD_table[v]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
#define BC_STORE_COMPS(pRes) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        jint a = (jint) accumA; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        jint r = (jint) accumR; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        jint g = (jint) accumG; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        jint b = (jint) accumB; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        SAT(a, 255); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        SAT(r, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        SAT(g, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        SAT(b, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        *pRes = ((a << 24) | (r << 16) | (g <<  8) | (b)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
#endif /* BICUBIC_USE_DBL_LUT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
#ifdef BICUBIC_USE_INT_MATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
#define BC_DblToCoeff(v)        ((jint) ((v) * 256))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
#define BC_COEFF_ONE            256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
#define BC_TYPE                 jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
#define BC_V_HALF               (1 << 15)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
#define BC_CompToV(v)           ((jint) v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
#define BC_STORE_COMPS(pRes) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        accumA >>= 16; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        accumR >>= 16; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        accumG >>= 16; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        accumB >>= 16; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        SAT(accumA, 255); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        SAT(accumR, accumA); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        SAT(accumG, accumA); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        SAT(accumB, accumA); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        *pRes = ((accumA << 24) | (accumR << 16) | (accumG << 8) | (accumB)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
#endif /* BICUBIC_USE_INT_MATH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
#define BC_ACCUM(index, ycindex, xcindex) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        BC_TYPE factor = bicubic_coeff[xcindex] * bicubic_coeff[ycindex]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        int rgb; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        rgb = pRGB[index]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        accumB += BC_CompToV((rgb >>  0) & 0xff) * factor; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        accumG += BC_CompToV((rgb >>  8) & 0xff) * factor; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        accumR += BC_CompToV((rgb >> 16) & 0xff) * factor; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        accumA += BC_CompToV((rgb >> 24) & 0xff) * factor; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
static BC_TYPE bicubic_coeff[513];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
static jboolean bicubictableinited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
init_bicubic_table(jdouble A)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * The following formulas are designed to give smooth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * results when 'A' is -0.5 or -1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        /* r(x) = (A + 2)|x|^3 - (A + 3)|x|^2 + 1 , 0 <= |x| < 1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        jdouble x = i / 256.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        x = ((A+2)*x - (A+3))*x*x + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        bicubic_coeff[i] = BC_DblToCoeff(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    for (; i < 384; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        /* r(x) = A|x|^3 - 5A|x|^2 + 8A|x| - 4A , 1 <= |x| < 2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        jdouble x = i / 256.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        x = ((A*x - 5*A)*x + 8*A)*x - 4*A;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        bicubic_coeff[i] = BC_DblToCoeff(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    bicubic_coeff[384] = (BC_COEFF_ONE - bicubic_coeff[128]*2) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    for (i++; i <= 512; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        bicubic_coeff[i] = BC_COEFF_ONE - (bicubic_coeff[512-i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                           bicubic_coeff[i-256] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                                           bicubic_coeff[768-i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    bicubictableinited = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
BicubicInterp(jint *pRGB, jint numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
              jint xfract, jint dxfract,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
              jint yfract, jint dyfract)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    jint i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    jint *pRes = pRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    if (!bicubictableinited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        init_bicubic_table(-0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    for (i = 0; i < numpix; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        BC_TYPE accumA, accumR, accumG, accumB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        jint xfactor, yfactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        xfactor = URShift(xfract, 32-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        yfactor = URShift(yfract, 32-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        accumA = accumR = accumG = accumB = BC_V_HALF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        BC_ACCUM(0, yfactor+256, xfactor+256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        BC_ACCUM(1, yfactor+256, xfactor+  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        BC_ACCUM(2, yfactor+256, 256-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        BC_ACCUM(3, yfactor+256, 512-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        BC_ACCUM(4, yfactor+  0, xfactor+256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        BC_ACCUM(5, yfactor+  0, xfactor+  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        BC_ACCUM(6, yfactor+  0, 256-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        BC_ACCUM(7, yfactor+  0, 512-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        BC_ACCUM(8, 256-yfactor, xfactor+256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        BC_ACCUM(9, 256-yfactor, xfactor+  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        BC_ACCUM(10, 256-yfactor, 256-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        BC_ACCUM(11, 256-yfactor, 512-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        BC_ACCUM(12, 512-yfactor, xfactor+256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        BC_ACCUM(13, 512-yfactor, xfactor+  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        BC_ACCUM(14, 512-yfactor, 256-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        BC_ACCUM(15, 512-yfactor, 512-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        BC_STORE_COMPS(pRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        pRes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        pRGB += 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        xfract += dxfract;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        yfract += dyfract;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
#ifdef MAKE_STUBS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
BilinearInterpStub(jint *pRGBbase, jint numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                   jint xfract, jint dxfract,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                   jint yfract, jint dyfract)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    jint *pRGB = pRGBbase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    while (--numpix >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        *pRGBbase = *pRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        pRGBbase += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        pRGB += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
BicubicInterpStub(jint *pRGBbase, jint numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                  jint xfract, jint dxfract,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                  jint yfract, jint dyfract)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    jint *pRGB = pRGBbase+5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    while (--numpix >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        *pRGBbase = *pRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        pRGBbase += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        pRGB += 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
#endif /* MAKE_STUBS */