jdk/src/share/native/sun/java2d/loops/TransformHelper.c
author asaha
Wed, 20 Jul 2011 14:45:44 -0700
changeset 10910 5ad2744fc78a
parent 9830 692619a439fc
child 15966 258abbf3f19f
permissions -rw-r--r--
7023640: calculation for malloc size in TransformHelper.c could overflow an integer Reviewed-by: flar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10910
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
     2
 * Copyright (c) 2004, 2011, 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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    dstOps = SurfaceData_GetOps(env, dstData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    if (srcOps == 0 || dstOps == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Grab the appropriate pointer to the helper and interpolation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * routines and calculate the maximum number of destination pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * that can be processed in one intermediate buffer based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * size of the buffer and the number of samples needed per pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    switch (txtype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    case java_awt_image_AffineTransformOp_TYPE_NEAREST_NEIGHBOR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        pHelperFunc = pHelperPrim->funcs.transformhelpers->nnHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        pInterpFunc = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        maxlinepix = LINE_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    case java_awt_image_AffineTransformOp_TYPE_BILINEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        pHelperFunc = pHelperPrim->funcs.transformhelpers->blHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        pInterpFunc = pBilinearFunc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        maxlinepix = LINE_SIZE / 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    case java_awt_image_AffineTransformOp_TYPE_BICUBIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        pHelperFunc = pHelperPrim->funcs.transformhelpers->bcHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        pInterpFunc = pBicubicFunc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        maxlinepix = LINE_SIZE / 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    srcInfo.bounds.x1 = sx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    srcInfo.bounds.y1 = sy1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    srcInfo.bounds.x2 = sx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    srcInfo.bounds.y2 = sy2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    dstInfo.bounds.x1 = dx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    dstInfo.bounds.y1 = dy1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    dstInfo.bounds.x2 = dx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    dstInfo.bounds.y2 = dy2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    SurfaceData_IntersectBounds(&dstInfo.bounds, &clipInfo.bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    if (srcOps->Lock(env, srcOps, &srcInfo, pHelperPrim->srcflags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        != SD_SUCCESS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    {
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   370
        /* edgeArray should already contain zeros for min/maxy */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    if (dstOps->Lock(env, dstOps, &dstInfo, pMaskBlitPrim->dstflags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        != SD_SUCCESS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        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
   377
        /* edgeArray should already contain zeros for min/maxy */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    Region_IntersectBounds(&clipInfo, &dstInfo.bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
10910
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   382
    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
   383
    if (numedges <= 0) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   384
        pEdges = NULL;
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   385
    } else if (!JNU_IsNull(env, edgeArray)) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   386
        /*
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   387
         * 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
   388
         * 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
   389
         * 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
   390
         * 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
   391
         * 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
   392
         * 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
   393
         */
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   394
        jsize edgesize = (*env)->GetArrayLength(env, edgeArray);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   395
        /* (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
   396
        pEdges = (((edgesize / 2) - 1) >= numedges)
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   397
            ? (*env)->GetPrimitiveArrayCritical(env, edgeArray, NULL)
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   398
            : NULL;
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   399
    } else if (numedges > MAXEDGES) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   400
        /* 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
   401
        /* 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
   402
        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
   403
        pEdges = (memsize == ((size_t) memsize))
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   404
            ? malloc((size_t) memsize)
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   405
            : NULL;
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   406
    } else {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   407
        pEdges = edgebuf;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   408
    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   409
10910
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   410
    if (pEdges == NULL) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   411
        if (numedges > 0) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   412
            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
   413
        }
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   414
        SurfaceData_InvokeUnlock(env, dstOps, &dstInfo);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   415
        SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   416
        /* 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
   417
        return;
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   418
    }
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   419
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    Transform_GetInfo(env, itxform, &itxInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    if (!Region_IsEmpty(&clipInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        srcOps->GetRasInfo(env, srcOps, &srcInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        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
   425
        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
   426
            pEdges[0] = pEdges[1] = 0;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   427
        } 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
   428
                                 &itxInfo, &xorig, &yorig))
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   429
        {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   430
            Transform_SafeHelper(env, srcOps, dstOps,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   431
                                 &srcInfo, &dstInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   432
                                 pMaskBlitPrim, &compInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   433
                                 pHelperFunc, pInterpFunc,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   434
                                 &clipInfo, &itxInfo, rgb.data, pEdges,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   435
                                 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
   436
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            SurfaceDataBounds span;
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   438
            jlong dxdxlong, dydxlong;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   439
            jlong dxdylong, dydylong;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   440
            jlong xbase, ybase;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   441
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   442
            dxdxlong = DblToLong(itxInfo.dxdx);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   443
            dydxlong = DblToLong(itxInfo.dydx);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   444
            dxdylong = DblToLong(itxInfo.dxdy);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   445
            dydylong = DblToLong(itxInfo.dydy);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   446
            xbase = DblToLong(xorig);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   447
            ybase = DblToLong(yorig);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   448
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   449
            calculateEdges(pEdges, &dstInfo.bounds, &itxInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   450
                           xbase, ybase, sx2-sx1, sy2-sy1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            Region_StartIteration(env, &clipInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            while (Region_NextIteration(&clipInfo, &span)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                jlong rowxlong, rowylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                void *pDst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                dy1 = span.y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                dy2 = span.y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                rowxlong = xbase + (dy1 - dstInfo.bounds.y1) * dxdylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                rowylong = ybase + (dy1 - dstInfo.bounds.y1) * dydylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                while (dy1 < dy2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    jlong xlong, ylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    /* Note - process at most one scanline at a time. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   467
                    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
   468
                    dx2 = pEdges[(dy1 - dstInfo.bounds.y1) * 2 + 3];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    if (dx1 < span.x1) dx1 = span.x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    if (dx2 > span.x2) dx2 = span.x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    /* All pixels from dx1 to dx2 have centers in bounds */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    while (dx1 < dx2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        /* Can process at most one buffer full at a time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        jint numpix = dx2 - dx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        if (numpix > maxlinepix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                            numpix = maxlinepix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        xlong =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                            rowxlong + ((dx1 - dstInfo.bounds.x1) * dxdxlong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                        ylong =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                            rowylong + ((dx1 - dstInfo.bounds.x1) * dydxlong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        /* Get IntArgbPre pixel data from source */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        (*pHelperFunc)(&srcInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                       rgb.data, numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                                       xlong, dxdxlong,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                                       ylong, dydxlong);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        /* Interpolate result pixels if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        if (pInterpFunc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                            (*pInterpFunc)(rgb.data, numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                           FractOfLong(xlong-LongOneHalf),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                           FractOfLong(dxdxlong),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                           FractOfLong(ylong-LongOneHalf),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                           FractOfLong(dydxlong));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        /* Store/Composite interpolated pixels into dest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        pDst = PtrCoord(dstInfo.rasBase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                                        dx1, dstInfo.pixelStride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                                        dy1, dstInfo.scanStride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        (*pMaskBlitPrim->funcs.maskblit)(pDst, rgb.data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                                                         0, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                                                         numpix, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                                                         &dstInfo, &srcInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                                                         pMaskBlitPrim,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                                                         &compInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        /* Increment to next buffer worth of input pixels */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        dx1 += maxlinepix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    /* Increment to next scanline */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    rowxlong += dxdylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    rowylong += dydylong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    dy1++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            Region_EndIteration(env, &clipInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        SurfaceData_InvokeRelease(env, dstOps, &dstInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        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
   525
    } else {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   526
        pEdges[0] = pEdges[1] = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
10910
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   528
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   529
    if (!JNU_IsNull(env, edgeArray)) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   530
        (*env)->ReleasePrimitiveArrayCritical(env, edgeArray, pEdges, 0);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   531
    } else if (pEdges != edgebuf) {
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   532
        free(pEdges);
5ad2744fc78a 7023640: calculation for malloc size in TransformHelper.c could overflow an integer
asaha
parents: 9830
diff changeset
   533
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    SurfaceData_InvokeUnlock(env, dstOps, &dstInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
9830
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   538
static void
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   539
Transform_SafeHelper(JNIEnv *env,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   540
                     SurfaceDataOps *srcOps,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   541
                     SurfaceDataOps *dstOps,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   542
                     SurfaceDataRasInfo *pSrcInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   543
                     SurfaceDataRasInfo *pDstInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   544
                     NativePrimitive *pMaskBlitPrim,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   545
                     CompositeInfo *pCompInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   546
                     TransformHelperFunc *pHelperFunc,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   547
                     TransformInterpFunc *pInterpFunc,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   548
                     RegionData *pClipInfo, TransformInfo *pItxInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   549
                     jint *pData, jint *pEdges,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   550
                     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
   551
{
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   552
    SurfaceDataBounds span;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   553
    jint dx1, dx2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   554
    jint dy1, dy2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   555
    jint i, iy;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   556
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   557
    dy1 = pDstInfo->bounds.y1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   558
    dy2 = pDstInfo->bounds.y2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   559
    dx1 = pDstInfo->bounds.x1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   560
    dx2 = pDstInfo->bounds.x2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   561
    pEdges[0] = dy1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   562
    pEdges[1] = dy2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   563
    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
   564
        jint i = (iy - dy1) * 2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   565
        /* 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
   566
        pEdges[i + 2] = dx2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   567
        pEdges[i + 3] = dx1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   568
    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   569
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   570
    Region_StartIteration(env, pClipInfo);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   571
    while (Region_NextIteration(pClipInfo, &span)) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   572
        dy1 = span.y1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   573
        dy2 = span.y2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   574
        while (dy1 < dy2) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   575
            dx1 = span.x1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   576
            dx2 = span.x2;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   577
            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
   578
            while (dx1 < dx2) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   579
                jdouble x, y;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   580
                jlong xlong, ylong;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   581
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   582
                x = dxoff + dx1 + 0.5;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   583
                y = dyoff + dy1 + 0.5;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   584
                Transform_transform(pItxInfo, &x, &y);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   585
                xlong = DblToLong(x);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   586
                ylong = DblToLong(y);
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
                /* 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
   589
                 * 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
   590
                 * 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
   591
                 * 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
   592
                 * the conversion.
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
                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
   595
                    WholeOfLong(xlong) < sw &&
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   596
                    WholeOfLong(ylong) < sh)
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   597
                {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   598
                    void *pDst;
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 (pEdges[i + 2] > dx1) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   601
                        pEdges[i + 2] = dx1;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   602
                    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   603
                    if (pEdges[i + 3] <= dx1) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   604
                        pEdges[i + 3] = dx1 + 1;
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
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   607
                    /* 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
   608
                    (*pHelperFunc)(pSrcInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   609
                                   pData, 1,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   610
                                   xlong, 0,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   611
                                   ylong, 0);
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
                    /* Interpolate result pixels if needed */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   614
                    if (pInterpFunc) {
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   615
                        (*pInterpFunc)(pData, 1,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   616
                                       FractOfLong(xlong-LongOneHalf), 0,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   617
                                       FractOfLong(ylong-LongOneHalf), 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
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   620
                    /* 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
   621
                    pDst = PtrCoord(pDstInfo->rasBase,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   622
                                    dx1, pDstInfo->pixelStride,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   623
                                    dy1, pDstInfo->scanStride);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   624
                    (*pMaskBlitPrim->funcs.maskblit)(pDst, pData,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   625
                                                     0, 0, 0,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   626
                                                     1, 1,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   627
                                                     pDstInfo, pSrcInfo,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   628
                                                     pMaskBlitPrim,
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   629
                                                     pCompInfo);
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   630
                }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   631
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   632
                /* Increment to next input pixel */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   633
                dx1++;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   634
            }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   635
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   636
            /* Increment to next scanline */
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   637
            dy1++;
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   638
        }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   639
    }
692619a439fc 7016495: Crash in Java 2D transforming an image with scale close to zero
flar
parents: 5506
diff changeset
   640
    Region_EndIteration(env, pClipInfo);
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
#define BL_INTERP_V1_to_V2_by_F(v1, v2, f) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    (((v1)<<8) + ((v2)-(v1))*(f))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
#define BL_ACCUM(comp) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        jint c1 = ((jubyte *) pRGB)[comp]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        jint c2 = ((jubyte *) pRGB)[comp+4]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        jint cR = BL_INTERP_V1_to_V2_by_F(c1, c2, xfactor); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        c1 = ((jubyte *) pRGB)[comp+8]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        c2 = ((jubyte *) pRGB)[comp+12]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        c2 = BL_INTERP_V1_to_V2_by_F(c1, c2, xfactor); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        cR = BL_INTERP_V1_to_V2_by_F(cR, c2, yfactor); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        ((jubyte *)pRes)[comp] = (jubyte) ((cR + (1<<15)) >> 16); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
BilinearInterp(jint *pRGB, jint numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
               jint xfract, jint dxfract,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
               jint yfract, jint dyfract)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    jint j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    jint *pRes = pRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    for (j = 0; j < numpix; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        jint xfactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        jint yfactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        xfactor = URShift(xfract, 32-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        yfactor = URShift(yfract, 32-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        BL_ACCUM(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        BL_ACCUM(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        BL_ACCUM(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        BL_ACCUM(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        pRes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        pRGB += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        xfract += dxfract;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        yfract += dyfract;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
#define SAT(val, max) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        val &= ~(val >> 31);  /* negatives become 0 */ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        val -= max;           /* only overflows are now positive */ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        val &= (val >> 31);   /* positives become 0 */ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        val += max;           /* range is now [0 -> max] */ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
#ifdef __sparc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
/* For sparc, floating point multiplies are faster than integer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
#define BICUBIC_USE_DBL_LUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
/* For x86, integer multiplies are faster than floating point */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
/* Note that on x86 Linux the choice of best algorithm varies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
 * depending on the compiler optimization and the processor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
 * Currently, the sun/awt x86 Linux builds are not optimized so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
 * all the variations produce mediocre performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
 * For now we will use the choice that works best for the Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
 * build until the (lack of) optimization issues on Linux are resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
#define BICUBIC_USE_INT_MATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
#ifdef BICUBIC_USE_DBL_CAST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
#define BC_DblToCoeff(v)        (v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
#define BC_COEFF_ONE            1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
#define BC_TYPE                 jdouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
#define BC_V_HALF               0.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
#define BC_CompToV(v)           ((jdouble) (v))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
#define BC_STORE_COMPS(pRes) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        jint a = (jint) accumA; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        jint r = (jint) accumR; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        jint g = (jint) accumG; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        jint b = (jint) accumB; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        SAT(a, 255); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        SAT(r, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        SAT(g, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        SAT(b, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        *pRes = ((a << 24) | (r << 16) | (g <<  8) | (b)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
#endif /* BICUBIC_USE_DBL_CAST */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
#ifdef BICUBIC_USE_DBL_LUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
#define ItoD1(v)    ((jdouble) (v))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
#define ItoD4(v)    ItoD1(v),  ItoD1(v+1),   ItoD1(v+2),   ItoD1(v+3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
#define ItoD16(v)   ItoD4(v),  ItoD4(v+4),   ItoD4(v+8),   ItoD4(v+12)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
#define ItoD64(v)   ItoD16(v), ItoD16(v+16), ItoD16(v+32), ItoD16(v+48)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
static jdouble ItoD_table[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    ItoD64(0), ItoD64(64), ItoD64(128), ItoD64(192)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
#define BC_DblToCoeff(v)        (v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
#define BC_COEFF_ONE            1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
#define BC_TYPE                 jdouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
#define BC_V_HALF               0.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
#define BC_CompToV(v)           ItoD_table[v]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
#define BC_STORE_COMPS(pRes) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        jint a = (jint) accumA; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        jint r = (jint) accumR; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        jint g = (jint) accumG; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        jint b = (jint) accumB; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        SAT(a, 255); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        SAT(r, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        SAT(g, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        SAT(b, a); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        *pRes = ((a << 24) | (r << 16) | (g <<  8) | (b)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
#endif /* BICUBIC_USE_DBL_LUT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
#ifdef BICUBIC_USE_INT_MATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
#define BC_DblToCoeff(v)        ((jint) ((v) * 256))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
#define BC_COEFF_ONE            256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
#define BC_TYPE                 jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
#define BC_V_HALF               (1 << 15)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
#define BC_CompToV(v)           ((jint) v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
#define BC_STORE_COMPS(pRes) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        accumA >>= 16; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        accumR >>= 16; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        accumG >>= 16; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        accumB >>= 16; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        SAT(accumA, 255); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        SAT(accumR, accumA); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        SAT(accumG, accumA); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        SAT(accumB, accumA); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        *pRes = ((accumA << 24) | (accumR << 16) | (accumG << 8) | (accumB)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
#endif /* BICUBIC_USE_INT_MATH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
#define BC_ACCUM(index, ycindex, xcindex) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        BC_TYPE factor = bicubic_coeff[xcindex] * bicubic_coeff[ycindex]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        int rgb; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        rgb = pRGB[index]; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        accumB += BC_CompToV((rgb >>  0) & 0xff) * factor; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        accumG += BC_CompToV((rgb >>  8) & 0xff) * factor; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        accumR += BC_CompToV((rgb >> 16) & 0xff) * factor; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        accumA += BC_CompToV((rgb >> 24) & 0xff) * factor; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    } while (0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
static BC_TYPE bicubic_coeff[513];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
static jboolean bicubictableinited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
init_bicubic_table(jdouble A)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * The following formulas are designed to give smooth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * results when 'A' is -0.5 or -1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        /* r(x) = (A + 2)|x|^3 - (A + 3)|x|^2 + 1 , 0 <= |x| < 1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        jdouble x = i / 256.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        x = ((A+2)*x - (A+3))*x*x + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        bicubic_coeff[i] = BC_DblToCoeff(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    for (; i < 384; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        /* r(x) = A|x|^3 - 5A|x|^2 + 8A|x| - 4A , 1 <= |x| < 2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        jdouble x = i / 256.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        x = ((A*x - 5*A)*x + 8*A)*x - 4*A;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        bicubic_coeff[i] = BC_DblToCoeff(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    bicubic_coeff[384] = (BC_COEFF_ONE - bicubic_coeff[128]*2) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    for (i++; i <= 512; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        bicubic_coeff[i] = BC_COEFF_ONE - (bicubic_coeff[512-i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                                           bicubic_coeff[i-256] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                                           bicubic_coeff[768-i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    bicubictableinited = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
BicubicInterp(jint *pRGB, jint numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
              jint xfract, jint dxfract,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
              jint yfract, jint dyfract)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    jint i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    jint *pRes = pRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    if (!bicubictableinited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        init_bicubic_table(-0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    for (i = 0; i < numpix; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        BC_TYPE accumA, accumR, accumG, accumB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        jint xfactor, yfactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        xfactor = URShift(xfract, 32-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        yfactor = URShift(yfract, 32-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        accumA = accumR = accumG = accumB = BC_V_HALF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        BC_ACCUM(0, yfactor+256, xfactor+256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        BC_ACCUM(1, yfactor+256, xfactor+  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        BC_ACCUM(2, yfactor+256, 256-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        BC_ACCUM(3, yfactor+256, 512-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        BC_ACCUM(4, yfactor+  0, xfactor+256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        BC_ACCUM(5, yfactor+  0, xfactor+  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        BC_ACCUM(6, yfactor+  0, 256-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        BC_ACCUM(7, yfactor+  0, 512-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        BC_ACCUM(8, 256-yfactor, xfactor+256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        BC_ACCUM(9, 256-yfactor, xfactor+  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        BC_ACCUM(10, 256-yfactor, 256-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        BC_ACCUM(11, 256-yfactor, 512-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        BC_ACCUM(12, 512-yfactor, xfactor+256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        BC_ACCUM(13, 512-yfactor, xfactor+  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        BC_ACCUM(14, 512-yfactor, 256-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        BC_ACCUM(15, 512-yfactor, 512-xfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        BC_STORE_COMPS(pRes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        pRes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        pRGB += 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        xfract += dxfract;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        yfract += dyfract;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
#ifdef MAKE_STUBS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
BilinearInterpStub(jint *pRGBbase, jint numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                   jint xfract, jint dxfract,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                   jint yfract, jint dyfract)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    jint *pRGB = pRGBbase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    while (--numpix >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        *pRGBbase = *pRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        pRGBbase += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        pRGB += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
BicubicInterpStub(jint *pRGBbase, jint numpix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                  jint xfract, jint dxfract,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                  jint yfract, jint dyfract)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    jint *pRGB = pRGBbase+5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    while (--numpix >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        *pRGBbase = *pRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        pRGBbase += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        pRGB += 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
#endif /* MAKE_STUBS */