0N/A/*
2362N/A * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "GraphicsPrimitiveMgr.h"
0N/A
0N/A#include "LineUtils.h"
0N/A
0N/A#include "sun_java2d_loops_DrawLine.h"
0N/A
0N/A#define OUTCODE_TOP 1
0N/A#define OUTCODE_BOTTOM 2
0N/A#define OUTCODE_LEFT 4
0N/A#define OUTCODE_RIGHT 8
0N/A
0N/Astatic void
0N/ARefineBounds(SurfaceDataBounds *bounds, jint x1, jint y1, jint x2, jint y2)
0N/A{
0N/A jint min, max;
0N/A if (x1 < x2) {
0N/A min = x1;
0N/A max = x2;
0N/A } else {
0N/A min = x2;
0N/A max = x1;
0N/A }
0N/A max++;
0N/A if (max <= min) {
0N/A /* integer overflow */
0N/A max--;
0N/A }
0N/A if (bounds->x1 < min) bounds->x1 = min;
0N/A if (bounds->x2 > max) bounds->x2 = max;
0N/A if (y1 < y2) {
0N/A min = y1;
0N/A max = y2;
0N/A } else {
0N/A min = y2;
0N/A max = y1;
0N/A }
0N/A max++;
0N/A if (max <= min) {
0N/A /* integer overflow */
0N/A max--;
0N/A }
0N/A if (bounds->y1 < min) bounds->y1 = min;
0N/A if (bounds->y2 > max) bounds->y2 = max;
0N/A}
0N/A
0N/A#define _out(v, vmin, vmax, cmin, cmax) \
0N/A ((v < vmin) ? cmin : ((v > vmax) ? cmax : 0))
0N/A
0N/A#define outcode(x, y, xmin, ymin, xmax, ymax) \
0N/A (_out(y, ymin, ymax, OUTCODE_TOP, OUTCODE_BOTTOM) | \
0N/A _out(x, xmin, xmax, OUTCODE_LEFT, OUTCODE_RIGHT))
0N/A
0N/A/*
0N/A * "Small" math here will be done if the coordinates are less
0N/A * than 15 bits in range (-16384 => 16383). This could be
0N/A * expanded to 16 bits if we rearrange some of the math in
0N/A * the normal version of SetupBresenham.
0N/A * "Big" math here will be done with coordinates with 30 bits
0N/A * of total range - 2 bits less than a jint holds.
0N/A * Intermediate calculations for "Big" coordinates will be
0N/A * done using jlong variables.
0N/A */
0N/A#define OverflowsSmall(v) ((v) != (((v) << 17) >> 17))
0N/A#define OverflowsBig(v) ((v) != (((v) << 2) >> 2))
0N/A#define BIG_MAX ((1 << 29) - 1)
0N/A#define BIG_MIN (-(1 << 29))
0N/A
0N/A#define SETUP_BRESENHAM(CALC_TYPE, ORIGX1, ORIGY1, ORIGX2, ORIGY2, SHORTEN) \
0N/Ado { \
0N/A jint X1 = ORIGX1, Y1 = ORIGY1, X2 = ORIGX2, Y2 = ORIGY2; \
0N/A jint dx, dy, ax, ay; \
0N/A jint cxmin, cymin, cxmax, cymax; \
0N/A jint outcode1, outcode2; \
0N/A jboolean xmajor; \
0N/A jint errminor, errmajor; \
0N/A jint error; \
0N/A jint steps; \
0N/A \
0N/A dx = X2 - X1; \
0N/A dy = Y2 - Y1; \
0N/A ax = (dx < 0) ? -dx : dx; \
0N/A ay = (dy < 0) ? -dy : dy; \
0N/A \
0N/A cxmin = pBounds->x1; \
0N/A cymin = pBounds->y1; \
0N/A cxmax = pBounds->x2 - 1; \
0N/A cymax = pBounds->y2 - 1; \
0N/A xmajor = (ax >= ay); \
0N/A \
0N/A outcode1 = outcode(X1, Y1, cxmin, cymin, cxmax, cymax); \
0N/A outcode2 = outcode(X2, Y2, cxmin, cymin, cxmax, cymax); \
0N/A while ((outcode1 | outcode2) != 0) { \
0N/A CALC_TYPE xsteps, ysteps; \
0N/A if ((outcode1 & outcode2) != 0) { \
0N/A return JNI_FALSE; \
0N/A } \
0N/A if (outcode1 != 0) { \
0N/A if (outcode1 & (OUTCODE_TOP | OUTCODE_BOTTOM)) { \
0N/A if (outcode1 & OUTCODE_TOP) { \
0N/A Y1 = cymin; \
0N/A } else { \
0N/A Y1 = cymax; \
0N/A } \
0N/A ysteps = Y1 - ORIGY1; \
0N/A if (ysteps < 0) { \
0N/A ysteps = -ysteps; \
0N/A } \
0N/A xsteps = 2 * ysteps * ax + ay; \
0N/A if (xmajor) { \
0N/A xsteps += ay - ax - 1; \
0N/A } \
0N/A xsteps = xsteps / (2 * ay); \
0N/A if (dx < 0) { \
0N/A xsteps = -xsteps; \
0N/A } \
0N/A X1 = ORIGX1 + (jint) xsteps; \
0N/A } else if (outcode1 & (OUTCODE_LEFT | OUTCODE_RIGHT)) { \
0N/A if (outcode1 & OUTCODE_LEFT) { \
0N/A X1 = cxmin; \
0N/A } else { \
0N/A X1 = cxmax; \
0N/A } \
0N/A xsteps = X1 - ORIGX1; \
0N/A if (xsteps < 0) { \
0N/A xsteps = -xsteps; \
0N/A } \
0N/A ysteps = 2 * xsteps * ay + ax; \
0N/A if (!xmajor) { \
0N/A ysteps += ax - ay - 1; \
0N/A } \
0N/A ysteps = ysteps / (2 * ax); \
0N/A if (dy < 0) { \
0N/A ysteps = -ysteps; \
0N/A } \
0N/A Y1 = ORIGY1 + (jint) ysteps; \
0N/A } \
0N/A outcode1 = outcode(X1, Y1, cxmin, cymin, cxmax, cymax); \
0N/A } else { \
0N/A if (outcode2 & (OUTCODE_TOP | OUTCODE_BOTTOM)) { \
0N/A if (outcode2 & OUTCODE_TOP) { \
0N/A Y2 = cymin; \
0N/A } else { \
0N/A Y2 = cymax; \
0N/A } \
0N/A ysteps = Y2 - ORIGY2; \
0N/A if (ysteps < 0) { \
0N/A ysteps = -ysteps; \
0N/A } \
0N/A xsteps = 2 * ysteps * ax + ay; \
0N/A if (xmajor) { \
0N/A xsteps += ay - ax; \
0N/A } else { \
0N/A xsteps -= 1; \
0N/A } \
0N/A xsteps = xsteps / (2 * ay); \
0N/A if (dx > 0) { \
0N/A xsteps = -xsteps; \
0N/A } \
0N/A X2 = ORIGX2 + (jint) xsteps; \
0N/A } else if (outcode2 & (OUTCODE_LEFT | OUTCODE_RIGHT)) { \
0N/A if (outcode2 & OUTCODE_LEFT) { \
0N/A X2 = cxmin; \
0N/A } else { \
0N/A X2 = cxmax; \
0N/A } \
0N/A xsteps = X2 - ORIGX2; \
0N/A if (xsteps < 0) { \
0N/A xsteps = -xsteps; \
0N/A } \
0N/A ysteps = 2 * xsteps * ay + ax; \
0N/A if (xmajor) { \
0N/A ysteps -= 1; \
0N/A } else { \
0N/A ysteps += ax - ay; \
0N/A } \
0N/A ysteps = ysteps / (2 * ax); \
0N/A if (dy > 0) { \
0N/A ysteps = -ysteps; \
0N/A } \
0N/A Y2 = ORIGY2 + (jint) ysteps; \
0N/A } \
0N/A outcode2 = outcode(X2, Y2, cxmin, cymin, cxmax, cymax); \
0N/A } \
0N/A } \
0N/A *pStartX = X1; \
0N/A *pStartY = Y1; \
0N/A \
0N/A if (xmajor) { \
0N/A errmajor = ay * 2; \
0N/A errminor = ax * 2; \
0N/A *pBumpMajorMask = (dx < 0) ? BUMP_NEG_PIXEL : BUMP_POS_PIXEL; \
0N/A *pBumpMinorMask = (dy < 0) ? BUMP_NEG_SCAN : BUMP_POS_SCAN; \
0N/A ax = -ax; /* For clipping adjustment below */ \
0N/A steps = X2 - X1; \
0N/A if (X2 != ORIGX2) { \
0N/A SHORTEN = 0; \
0N/A } \
0N/A } else { \
0N/A errmajor = ax * 2; \
0N/A errminor = ay * 2; \
0N/A *pBumpMajorMask = (dy < 0) ? BUMP_NEG_SCAN : BUMP_POS_SCAN; \
0N/A *pBumpMinorMask = (dx < 0) ? BUMP_NEG_PIXEL : BUMP_POS_PIXEL; \
0N/A ay = -ay; /* For clipping adjustment below */ \
0N/A steps = Y2 - Y1; \
0N/A if (Y2 != ORIGY2) { \
0N/A SHORTEN = 0; \
0N/A } \
0N/A } \
0N/A if ((steps = ((steps >= 0) ? steps : -steps) + 1 - SHORTEN) == 0) { \
0N/A return JNI_FALSE; \
0N/A } \
0N/A error = - (errminor / 2); \
0N/A if (Y1 != ORIGY1) { \
0N/A jint ysteps = Y1 - ORIGY1; \
0N/A if (ysteps < 0) { \
0N/A ysteps = -ysteps; \
0N/A } \
0N/A error += ysteps * ax * 2; \
0N/A } \
0N/A if (X1 != ORIGX1) { \
0N/A jint xsteps = X1 - ORIGX1; \
0N/A if (xsteps < 0) { \
0N/A xsteps = -xsteps; \
0N/A } \
0N/A error += xsteps * ay * 2; \
0N/A } \
0N/A error += errmajor; \
0N/A errminor -= errmajor; \
0N/A \
0N/A *pSteps = steps; \
0N/A *pError = error; \
0N/A *pErrMajor = errmajor; \
0N/A *pErrMinor = errminor; \
0N/A} while (0)
0N/A
0N/Astatic jboolean
0N/ALineUtils_SetupBresenhamBig(jint _x1, jint _y1, jint _x2, jint _y2,
0N/A jint shorten,
0N/A SurfaceDataBounds *pBounds,
0N/A jint *pStartX, jint *pStartY,
0N/A jint *pSteps, jint *pError,
0N/A jint *pErrMajor, jint *pBumpMajorMask,
0N/A jint *pErrMinor, jint *pBumpMinorMask)
0N/A{
0N/A /*
0N/A * Part of calculating the Bresenham parameters for line stepping
0N/A * involves being able to store numbers that are twice the magnitude
0N/A * of the biggest absolute difference in coordinates. Since we
0N/A * want the stepping parameters to be stored in jints, we then need
0N/A * to avoid any absolute differences more than 30 bits. Thus, we
0N/A * need to preprocess the coordinates to reduce their range to 30
0N/A * bits regardless of clipping. We need to cut their range back
0N/A * before we do the clipping because the Bresenham stepping values
0N/A * need to be calculated based on the "unclipped" coordinates.
0N/A *
0N/A * Thus, first we perform a "pre-clipping" stage to bring the
0N/A * coordinates within the 30-bit range and then we proceed to the
0N/A * regular clipping procedure, pretending that these were the
0N/A * original coordinates all along. Since this operation occurs
0N/A * based on a constant "pre-clip" rectangle of +/- 30 bits without
0N/A * any consideration for the final clip, the rounding errors that
0N/A * occur here will depend only on the line coordinates and be
0N/A * invariant with respect to the particular device/user clip
0N/A * rectangles in effect at the time. Thus, rendering a given
0N/A * large-range line will be consistent under a variety of
0N/A * clipping conditions.
0N/A */
0N/A if (OverflowsBig(_x1) || OverflowsBig(_y1) ||
0N/A OverflowsBig(_x2) || OverflowsBig(_y2))
0N/A {
0N/A /*
0N/A * Use doubles to get us into range for "Big" arithmetic.
0N/A *
0N/A * The math of adjusting an endpoint for clipping can involve
0N/A * an intermediate result with twice the number of bits as the
0N/A * original coordinate range. Since we want to maintain as
0N/A * much as 30 bits of precision in the resulting coordinates,
0N/A * we will get roundoff here even using IEEE double-precision
0N/A * arithmetic which cannot carry 60 bits of mantissa. Since
0N/A * the rounding errors will be consistent for a given set
0N/A * of input coordinates the potential roundoff error should
0N/A * not affect the consistency of our rendering.
0N/A */
0N/A double X1d = _x1;
0N/A double Y1d = _y1;
0N/A double X2d = _x2;
0N/A double Y2d = _y2;
0N/A double DXd = X2d - X1d;
0N/A double DYd = Y2d - Y1d;
0N/A if (_x1 < BIG_MIN) {
0N/A Y1d = _y1 + (BIG_MIN - _x1) * DYd / DXd;
0N/A X1d = BIG_MIN;
0N/A } else if (_x1 > BIG_MAX) {
0N/A Y1d = _y1 - (_x1 - BIG_MAX) * DYd / DXd;
0N/A X1d = BIG_MAX;
0N/A }
0N/A /* Use Y1d instead of _y1 for testing now as we may have modified it */
0N/A if (Y1d < BIG_MIN) {
0N/A X1d = _x1 + (BIG_MIN - _y1) * DXd / DYd;
0N/A Y1d = BIG_MIN;
0N/A } else if (Y1d > BIG_MAX) {
0N/A X1d = _x1 - (_y1 - BIG_MAX) * DXd / DYd;
0N/A Y1d = BIG_MAX;
0N/A }
0N/A if (_x2 < BIG_MIN) {
0N/A Y2d = _y2 + (BIG_MIN - _x2) * DYd / DXd;
0N/A X2d = BIG_MIN;
0N/A } else if (_x2 > BIG_MAX) {
0N/A Y2d = _y2 - (_x2 - BIG_MAX) * DYd / DXd;
0N/A X2d = BIG_MAX;
0N/A }
0N/A /* Use Y2d instead of _y2 for testing now as we may have modified it */
0N/A if (Y2d < BIG_MIN) {
0N/A X2d = _x2 + (BIG_MIN - _y2) * DXd / DYd;
0N/A Y2d = BIG_MIN;
0N/A } else if (Y2d > BIG_MAX) {
0N/A X2d = _x2 - (_y2 - BIG_MAX) * DXd / DYd;
0N/A Y2d = BIG_MAX;
0N/A }
0N/A _x1 = (int) X1d;
0N/A _y1 = (int) Y1d;
0N/A _x2 = (int) X2d;
0N/A _y2 = (int) Y2d;
0N/A }
0N/A
0N/A SETUP_BRESENHAM(jlong, _x1, _y1, _x2, _y2, shorten);
0N/A
0N/A return JNI_TRUE;
0N/A}
0N/A
0N/Ajboolean
0N/ALineUtils_SetupBresenham(jint _x1, jint _y1, jint _x2, jint _y2,
0N/A jint shorten,
0N/A SurfaceDataBounds *pBounds,
0N/A jint *pStartX, jint *pStartY,
0N/A jint *pSteps, jint *pError,
0N/A jint *pErrMajor, jint *pBumpMajorMask,
0N/A jint *pErrMinor, jint *pBumpMinorMask)
0N/A{
0N/A if (OverflowsSmall(_x1) || OverflowsSmall(_y1) ||
0N/A OverflowsSmall(_x2) || OverflowsSmall(_y2))
0N/A {
0N/A return LineUtils_SetupBresenhamBig(_x1, _y1, _x2, _y2, shorten,
0N/A pBounds,
0N/A pStartX, pStartY,
0N/A pSteps, pError,
0N/A pErrMajor, pBumpMajorMask,
0N/A pErrMinor, pBumpMinorMask);
0N/A }
0N/A
0N/A SETUP_BRESENHAM(jint, _x1, _y1, _x2, _y2, shorten);
0N/A
0N/A return JNI_TRUE;
0N/A}
0N/A
0N/A/*
0N/A * Class: sun_java2d_loops_DrawLine
0N/A * Method: DrawLine
0N/A * Signature: (Lsun/java2d/SunGraphics2D;Lsun/java2d/SurfaceData;IIII)V
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_java2d_loops_DrawLine_DrawLine
0N/A (JNIEnv *env, jobject self,
0N/A jobject sg2d, jobject sData,
0N/A jint x1, jint y1, jint x2, jint y2)
0N/A{
0N/A SurfaceDataOps *sdOps;
0N/A SurfaceDataRasInfo rasInfo;
0N/A NativePrimitive *pPrim;
0N/A CompositeInfo compInfo;
0N/A jint pixel = GrPrim_Sg2dGetPixel(env, sg2d);
0N/A
0N/A pPrim = GetNativePrim(env, self);
0N/A if (pPrim == NULL) {
0N/A return;
0N/A }
0N/A if (pPrim->pCompType->getCompInfo != NULL) {
0N/A GrPrim_Sg2dGetCompInfo(env, sg2d, pPrim, &compInfo);
0N/A }
0N/A
0N/A sdOps = SurfaceData_GetOps(env, sData);
0N/A if (sdOps == 0) {
0N/A return;
0N/A }
0N/A
0N/A GrPrim_Sg2dGetClip(env, sg2d, &rasInfo.bounds);
0N/A
0N/A RefineBounds(&rasInfo.bounds, x1, y1, x2, y2);
0N/A
0N/A if (sdOps->Lock(env, sdOps, &rasInfo, pPrim->dstflags) != SD_SUCCESS) {
0N/A return;
0N/A }
0N/A
0N/A if (rasInfo.bounds.x2 > rasInfo.bounds.x1 &&
0N/A rasInfo.bounds.y2 > rasInfo.bounds.y1)
0N/A {
0N/A sdOps->GetRasInfo(env, sdOps, &rasInfo);
0N/A if (rasInfo.rasBase) {
0N/A LineUtils_ProcessLine(&rasInfo, pixel,
0N/A pPrim->funcs.drawline, pPrim, &compInfo,
0N/A x1, y1, x2, y2, 0);
0N/A }
0N/A SurfaceData_InvokeRelease(env, sdOps, &rasInfo);
0N/A }
0N/A SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);
0N/A}