0N/A/*
2362N/A * Copyright (c) 1997, 2003, 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
0N/A/*
0N/A * DESCRIPTION
0N/A * Calculates cliping boundary for Affine functions.
0N/A *
0N/A */
0N/A
0N/A#include "mlib_image.h"
0N/A#include "mlib_SysMath.h"
0N/A#include "mlib_ImageAffine.h"
0N/A
0N/A/***************************************************************/
0N/Amlib_status mlib_AffineEdges(mlib_affine_param *param,
0N/A const mlib_image *dst,
0N/A const mlib_image *src,
0N/A void *buff_lcl,
0N/A mlib_s32 buff_size,
0N/A mlib_s32 kw,
0N/A mlib_s32 kh,
0N/A mlib_s32 kw1,
0N/A mlib_s32 kh1,
0N/A mlib_edge edge,
0N/A const mlib_d64 *mtx,
0N/A mlib_s32 shiftx,
0N/A mlib_s32 shifty)
0N/A{
0N/A mlib_u8 *buff = buff_lcl;
0N/A mlib_u8 **lineAddr = param->lineAddr;
0N/A mlib_s32 srcWidth, dstWidth, srcHeight, dstHeight, srcYStride, dstYStride;
0N/A mlib_s32 *leftEdges, *rightEdges, *xStarts, *yStarts, bsize0, bsize1 = 0;
0N/A mlib_u8 *srcData, *dstData;
0N/A mlib_u8 *paddings;
0N/A void *warp_tbl = NULL;
0N/A mlib_s32 yStart = 0, yFinish = -1, dX, dY;
0N/A
0N/A mlib_d64 xClip, yClip, wClip, hClip;
0N/A mlib_d64 delta = 0.;
0N/A mlib_d64 minX, minY, maxX, maxY;
0N/A
0N/A mlib_d64 coords[4][2];
0N/A mlib_d64 a = mtx[0], b = mtx[1], tx = mtx[2], c = mtx[3], d = mtx[4], ty = mtx[5];
0N/A mlib_d64 a2, b2, tx2, c2, d2, ty2;
0N/A mlib_d64 dx, dy, div;
0N/A mlib_s32 sdx, sdy;
0N/A mlib_d64 dTop;
0N/A mlib_d64 val0;
0N/A mlib_s32 top, bot;
0N/A mlib_s32 topIdx, max_xsize = 0;
0N/A mlib_s32 i, j, t;
0N/A
0N/A srcData = mlib_ImageGetData(src);
0N/A dstData = mlib_ImageGetData(dst);
0N/A srcWidth = mlib_ImageGetWidth(src);
0N/A srcHeight = mlib_ImageGetHeight(src);
0N/A dstWidth = mlib_ImageGetWidth(dst);
0N/A dstHeight = mlib_ImageGetHeight(dst);
0N/A srcYStride = mlib_ImageGetStride(src);
0N/A dstYStride = mlib_ImageGetStride(dst);
0N/A paddings = mlib_ImageGetPaddings(src);
0N/A
0N/A if (srcWidth >= (1 << 15) || srcHeight >= (1 << 15)) {
0N/A return MLIB_FAILURE;
0N/A }
0N/A
0N/A div = a * d - b * c;
0N/A
0N/A if (div == 0.0) {
0N/A return MLIB_FAILURE;
0N/A }
0N/A
0N/A bsize0 = (dstHeight * sizeof(mlib_s32) + 7) & ~7;
0N/A
0N/A if (lineAddr == NULL) {
0N/A bsize1 = ((srcHeight + 4 * kh) * sizeof(mlib_u8 *) + 7) & ~7;
0N/A }
0N/A
0N/A param->buff_malloc = NULL;
0N/A
0N/A if ((4 * bsize0 + bsize1) > buff_size) {
0N/A buff = param->buff_malloc = mlib_malloc(4 * bsize0 + bsize1);
0N/A
0N/A if (buff == NULL)
0N/A return MLIB_FAILURE;
0N/A }
0N/A
0N/A leftEdges = (mlib_s32 *) (buff);
0N/A rightEdges = (mlib_s32 *) (buff += bsize0);
0N/A xStarts = (mlib_s32 *) (buff += bsize0);
0N/A yStarts = (mlib_s32 *) (buff += bsize0);
0N/A
0N/A if (lineAddr == NULL) {
0N/A mlib_u8 *srcLinePtr = srcData;
0N/A lineAddr = (mlib_u8 **) (buff += bsize0);
0N/A for (i = 0; i < 2 * kh; i++)
0N/A lineAddr[i] = srcLinePtr;
0N/A lineAddr += 2 * kh;
0N/A for (i = 0; i < srcHeight - 1; i++) {
0N/A lineAddr[i] = srcLinePtr;
0N/A srcLinePtr += srcYStride;
0N/A }
0N/A
0N/A for (i = srcHeight - 1; i < srcHeight + 2 * kh; i++)
0N/A lineAddr[i] = srcLinePtr;
0N/A }
0N/A
0N/A if ((mlib_s32) edge < 0) { /* process edges */
0N/A minX = 0;
0N/A minY = 0;
0N/A maxX = srcWidth;
0N/A maxY = srcHeight;
0N/A }
0N/A else {
0N/A
0N/A if (kw > 1)
0N/A delta = -0.5; /* for MLIB_NEAREST filter delta = 0. */
0N/A
0N/A minX = (kw1 - delta);
0N/A minY = (kh1 - delta);
0N/A maxX = srcWidth - ((kw - 1) - (kw1 - delta));
0N/A maxY = srcHeight - ((kh - 1) - (kh1 - delta));
0N/A
0N/A if (edge == MLIB_EDGE_SRC_PADDED) {
0N/A if (minX < paddings[0])
0N/A minX = paddings[0];
0N/A
0N/A if (minY < paddings[1])
0N/A minY = paddings[1];
0N/A
0N/A if (maxX > (srcWidth - paddings[2]))
0N/A maxX = srcWidth - paddings[2];
0N/A
0N/A if (maxY > (srcHeight - paddings[3]))
0N/A maxY = srcHeight - paddings[3];
0N/A }
0N/A }
0N/A
0N/A xClip = minX;
0N/A yClip = minY;
0N/A wClip = maxX;
0N/A hClip = maxY;
0N/A
0N/A/*
0N/A * STORE_PARAM(param, src);
0N/A * STORE_PARAM(param, dst);
0N/A */
0N/A param->src = (void *)src;
0N/A param->dst = (void *)dst;
0N/A STORE_PARAM(param, lineAddr);
0N/A STORE_PARAM(param, dstData);
0N/A STORE_PARAM(param, srcYStride);
0N/A STORE_PARAM(param, dstYStride);
0N/A STORE_PARAM(param, leftEdges);
0N/A STORE_PARAM(param, rightEdges);
0N/A STORE_PARAM(param, xStarts);
0N/A STORE_PARAM(param, yStarts);
0N/A STORE_PARAM(param, max_xsize);
0N/A STORE_PARAM(param, yStart);
0N/A STORE_PARAM(param, yFinish);
0N/A STORE_PARAM(param, warp_tbl);
0N/A
0N/A if ((xClip >= wClip) || (yClip >= hClip)) {
0N/A return MLIB_SUCCESS;
0N/A }
0N/A
0N/A a2 = d;
0N/A b2 = -b;
0N/A tx2 = (-d * tx + b * ty);
0N/A c2 = -c;
0N/A d2 = a;
0N/A ty2 = (c * tx - a * ty);
0N/A
0N/A dx = a2;
0N/A dy = c2;
0N/A
0N/A tx -= 0.5;
0N/A ty -= 0.5;
0N/A
0N/A coords[0][0] = xClip * a + yClip * b + tx;
0N/A coords[0][1] = xClip * c + yClip * d + ty;
0N/A
0N/A coords[2][0] = wClip * a + hClip * b + tx;
0N/A coords[2][1] = wClip * c + hClip * d + ty;
0N/A
0N/A if (div > 0) {
0N/A coords[1][0] = wClip * a + yClip * b + tx;
0N/A coords[1][1] = wClip * c + yClip * d + ty;
0N/A
0N/A coords[3][0] = xClip * a + hClip * b + tx;
0N/A coords[3][1] = xClip * c + hClip * d + ty;
0N/A }
0N/A else {
0N/A coords[3][0] = wClip * a + yClip * b + tx;
0N/A coords[3][1] = wClip * c + yClip * d + ty;
0N/A
0N/A coords[1][0] = xClip * a + hClip * b + tx;
0N/A coords[1][1] = xClip * c + hClip * d + ty;
0N/A }
0N/A
0N/A topIdx = 0;
0N/A for (i = 1; i < 4; i++) {
0N/A
0N/A if (coords[i][1] < coords[topIdx][1])
0N/A topIdx = i;
0N/A }
0N/A
0N/A dTop = coords[topIdx][1];
0N/A val0 = dTop;
0N/A SAT32(top);
0N/A bot = -1;
0N/A
0N/A if (top >= dstHeight) {
0N/A return MLIB_SUCCESS;
0N/A }
0N/A
0N/A if (dTop >= 0.0) {
0N/A mlib_d64 xLeft, xRight, x;
0N/A mlib_s32 nextIdx;
0N/A
0N/A if (dTop == top) {
0N/A xLeft = coords[topIdx][0];
0N/A xRight = coords[topIdx][0];
0N/A nextIdx = (topIdx + 1) & 0x3;
0N/A
0N/A if (dTop == coords[nextIdx][1]) {
0N/A x = coords[nextIdx][0];
0N/A xLeft = (xLeft <= x) ? xLeft : x;
0N/A xRight = (xRight >= x) ? xRight : x;
0N/A }
0N/A
0N/A nextIdx = (topIdx - 1) & 0x3;
0N/A
0N/A if (dTop == coords[nextIdx][1]) {
0N/A x = coords[nextIdx][0];
0N/A xLeft = (xLeft <= x) ? xLeft : x;
0N/A xRight = (xRight >= x) ? xRight : x;
0N/A }
0N/A
0N/A val0 = xLeft;
0N/A SAT32(t);
0N/A leftEdges[top] = (t >= xLeft) ? t : ++t;
0N/A
0N/A if (xLeft >= MLIB_S32_MAX)
0N/A leftEdges[top] = MLIB_S32_MAX;
0N/A
0N/A val0 = xRight;
0N/A SAT32(rightEdges[top]);
0N/A }
0N/A else
0N/A top++;
0N/A }
0N/A else
0N/A top = 0;
0N/A
0N/A for (i = 0; i < 2; i++) {
0N/A mlib_d64 dY1 = coords[(topIdx - i) & 0x3][1];
0N/A mlib_d64 dX1 = coords[(topIdx - i) & 0x3][0];
0N/A mlib_d64 dY2 = coords[(topIdx - i - 1) & 0x3][1];
0N/A mlib_d64 dX2 = coords[(topIdx - i - 1) & 0x3][0];
0N/A mlib_d64 x = dX1, slope = (dX2 - dX1) / (dY2 - dY1);
0N/A mlib_s32 y1;
0N/A mlib_s32 y2;
0N/A
0N/A if (dY1 == dY2)
0N/A continue;
0N/A
0N/A if (dY1 < 0.0)
0N/A y1 = 0;
0N/A else {
0N/A val0 = dY1 + 1;
0N/A SAT32(y1);
0N/A }
0N/A
0N/A val0 = dY2;
0N/A SAT32(y2);
0N/A
0N/A if (y2 >= dstHeight)
0N/A y2 = (mlib_s32) (dstHeight - 1);
0N/A
0N/A x += slope * (y1 - dY1);
0N/A#ifdef __SUNPRO_C
0N/A#pragma pipeloop(0)
0N/A#endif /* __SUNPRO_C */
0N/A for (j = y1; j <= y2; j++) {
0N/A val0 = x;
0N/A SAT32(t);
0N/A leftEdges[j] = (t >= x) ? t : ++t;
0N/A
0N/A if (x >= MLIB_S32_MAX)
0N/A leftEdges[j] = MLIB_S32_MAX;
0N/A x += slope;
0N/A }
0N/A }
0N/A
0N/A for (i = 0; i < 2; i++) {
0N/A mlib_d64 dY1 = coords[(topIdx + i) & 0x3][1];
0N/A mlib_d64 dX1 = coords[(topIdx + i) & 0x3][0];
0N/A mlib_d64 dY2 = coords[(topIdx + i + 1) & 0x3][1];
0N/A mlib_d64 dX2 = coords[(topIdx + i + 1) & 0x3][0];
0N/A mlib_d64 x = dX1, slope = (dX2 - dX1) / (dY2 - dY1);
0N/A mlib_s32 y1;
0N/A mlib_s32 y2;
0N/A
0N/A if (dY1 == dY2)
0N/A continue;
0N/A
0N/A if (dY1 < 0.0)
0N/A y1 = 0;
0N/A else {
0N/A val0 = dY1 + 1;
0N/A SAT32(y1);
0N/A }
0N/A
0N/A val0 = dY2;
0N/A SAT32(y2);
0N/A
0N/A if (y2 >= dstHeight)
0N/A y2 = (mlib_s32) (dstHeight - 1);
0N/A
0N/A x += slope * (y1 - dY1);
0N/A#ifdef __SUNPRO_C
0N/A#pragma pipeloop(0)
0N/A#endif /* __SUNPRO_C */
0N/A for (j = y1; j <= y2; j++) {
0N/A val0 = x;
0N/A SAT32(rightEdges[j]);
0N/A x += slope;
0N/A }
0N/A
0N/A bot = y2;
0N/A }
0N/A
0N/A {
0N/A mlib_d64 dxCl = xClip * div;
0N/A mlib_d64 dyCl = yClip * div;
0N/A mlib_d64 dwCl = wClip * div;
0N/A mlib_d64 dhCl = hClip * div;
0N/A
0N/A mlib_s32 xCl = (mlib_s32) (xClip + delta);
0N/A mlib_s32 yCl = (mlib_s32) (yClip + delta);
0N/A mlib_s32 wCl = (mlib_s32) (wClip + delta);
0N/A mlib_s32 hCl = (mlib_s32) (hClip + delta);
0N/A
0N/A /*
0N/A * mlib_s32 xCl = (mlib_s32)(xClip + delta);
0N/A * mlib_s32 yCl = (mlib_s32)(yClip + delta);
0N/A * mlib_s32 wCl = (mlib_s32)(wClip);
0N/A * mlib_s32 hCl = (mlib_s32)(hClip);
0N/A */
0N/A
0N/A if (edge == MLIB_EDGE_SRC_PADDED) {
0N/A xCl = kw1;
0N/A yCl = kh1;
0N/A wCl = (mlib_s32) (srcWidth - ((kw - 1) - kw1));
0N/A hCl = (mlib_s32) (srcHeight - ((kh - 1) - kh1));
0N/A }
0N/A
0N/A div = 1.0 / div;
0N/A
0N/A sdx = (mlib_s32) (a2 * div * (1 << shiftx));
0N/A sdy = (mlib_s32) (c2 * div * (1 << shifty));
0N/A
0N/A if (div > 0) {
0N/A
0N/A#ifdef __SUNPRO_C
0N/A#pragma pipeloop(0)
0N/A#endif /* __SUNPRO_C */
0N/A for (i = top; i <= bot; i++) {
0N/A mlib_s32 xLeft = leftEdges[i];
0N/A mlib_s32 xRight = rightEdges[i];
0N/A mlib_s32 xs, ys, x_e, y_e, x_s, y_s;
0N/A mlib_d64 dxs, dys, dxe, dye;
0N/A mlib_d64 xl, ii, xr;
0N/A
0N/A xLeft = (xLeft < 0) ? 0 : xLeft;
0N/A xRight = (xRight >= dstWidth) ? (mlib_s32) (dstWidth - 1) : xRight;
0N/A
0N/A xl = xLeft + 0.5;
0N/A ii = i + 0.5;
0N/A xr = xRight + 0.5;
0N/A dxs = xl * a2 + ii * b2 + tx2;
0N/A dys = xl * c2 + ii * d2 + ty2;
0N/A
0N/A if ((dxs < dxCl) || (dxs >= dwCl) || (dys < dyCl) || (dys >= dhCl)) {
0N/A dxs += dx;
0N/A dys += dy;
0N/A xLeft++;
0N/A
0N/A if ((dxs < dxCl) || (dxs >= dwCl) || (dys < dyCl) || (dys >= dhCl))
0N/A xRight = -1;
0N/A }
0N/A
0N/A dxe = xr * a2 + ii * b2 + tx2;
0N/A dye = xr * c2 + ii * d2 + ty2;
0N/A
0N/A if ((dxe < dxCl) || (dxe >= dwCl) || (dye < dyCl) || (dye >= dhCl)) {
0N/A dxe -= dx;
0N/A dye -= dy;
0N/A xRight--;
0N/A
0N/A if ((dxe < dxCl) || (dxe >= dwCl) || (dye < dyCl) || (dye >= dhCl))
0N/A xRight = -1;
0N/A }
0N/A
0N/A xs = (mlib_s32) ((dxs * div + delta) * (1 << shiftx));
0N/A x_s = xs >> shiftx;
0N/A
0N/A ys = (mlib_s32) ((dys * div + delta) * (1 << shifty));
0N/A y_s = ys >> shifty;
0N/A
0N/A if (x_s < xCl)
0N/A xs = (xCl << shiftx);
0N/A else if (x_s >= wCl)
0N/A xs = ((wCl << shiftx) - 1);
0N/A
0N/A if (y_s < yCl)
0N/A ys = (yCl << shifty);
0N/A else if (y_s >= hCl)
0N/A ys = ((hCl << shifty) - 1);
0N/A
0N/A if (xRight >= xLeft) {
0N/A x_e = ((xRight - xLeft) * sdx + xs) >> shiftx;
0N/A y_e = ((xRight - xLeft) * sdy + ys) >> shifty;
0N/A
0N/A if ((x_e < xCl) || (x_e >= wCl)) {
0N/A if (sdx > 0)
0N/A sdx -= 1;
0N/A else
0N/A sdx += 1;
0N/A }
0N/A
0N/A if ((y_e < yCl) || (y_e >= hCl)) {
0N/A if (sdy > 0)
0N/A sdy -= 1;
0N/A else
0N/A sdy += 1;
0N/A }
0N/A }
0N/A
0N/A leftEdges[i] = xLeft;
0N/A rightEdges[i] = xRight;
0N/A xStarts[i] = xs;
0N/A yStarts[i] = ys;
0N/A
0N/A if ((xRight - xLeft + 1) > max_xsize)
0N/A max_xsize = (xRight - xLeft + 1);
0N/A }
0N/A }
0N/A else {
0N/A
0N/A#ifdef __SUNPRO_C
0N/A#pragma pipeloop(0)
0N/A#endif /* __SUNPRO_C */
0N/A for (i = top; i <= bot; i++) {
0N/A mlib_s32 xLeft = leftEdges[i];
0N/A mlib_s32 xRight = rightEdges[i];
0N/A mlib_s32 xs, ys, x_e, y_e, x_s, y_s;
0N/A mlib_d64 dxs, dys, dxe, dye;
0N/A mlib_d64 xl, ii, xr;
0N/A
0N/A xLeft = (xLeft < 0) ? 0 : xLeft;
0N/A xRight = (xRight >= dstWidth) ? (mlib_s32) (dstWidth - 1) : xRight;
0N/A
0N/A xl = xLeft + 0.5;
0N/A ii = i + 0.5;
0N/A xr = xRight + 0.5;
0N/A dxs = xl * a2 + ii * b2 + tx2;
0N/A dys = xl * c2 + ii * d2 + ty2;
0N/A
0N/A if ((dxs > dxCl) || (dxs <= dwCl) || (dys > dyCl) || (dys <= dhCl)) {
0N/A dxs += dx;
0N/A dys += dy;
0N/A xLeft++;
0N/A
0N/A if ((dxs > dxCl) || (dxs <= dwCl) || (dys > dyCl) || (dys <= dhCl))
0N/A xRight = -1;
0N/A }
0N/A
0N/A dxe = xr * a2 + ii * b2 + tx2;
0N/A dye = xr * c2 + ii * d2 + ty2;
0N/A
0N/A if ((dxe > dxCl) || (dxe <= dwCl) || (dye > dyCl) || (dye <= dhCl)) {
0N/A dxe -= dx;
0N/A dye -= dy;
0N/A xRight--;
0N/A
0N/A if ((dxe > dxCl) || (dxe <= dwCl) || (dye > dyCl) || (dye <= dhCl))
0N/A xRight = -1;
0N/A }
0N/A
0N/A xs = (mlib_s32) ((dxs * div + delta) * (1 << shiftx));
0N/A x_s = xs >> shiftx;
0N/A
0N/A if (x_s < xCl)
0N/A xs = (xCl << shiftx);
0N/A else if (x_s >= wCl)
0N/A xs = ((wCl << shiftx) - 1);
0N/A
0N/A ys = (mlib_s32) ((dys * div + delta) * (1 << shifty));
0N/A y_s = ys >> shifty;
0N/A
0N/A if (y_s < yCl)
0N/A ys = (yCl << shifty);
0N/A else if (y_s >= hCl)
0N/A ys = ((hCl << shifty) - 1);
0N/A
0N/A if (xRight >= xLeft) {
0N/A x_e = ((xRight - xLeft) * sdx + xs) >> shiftx;
0N/A y_e = ((xRight - xLeft) * sdy + ys) >> shifty;
0N/A
0N/A if ((x_e < xCl) || (x_e >= wCl)) {
0N/A if (sdx > 0)
0N/A sdx -= 1;
0N/A else
0N/A sdx += 1;
0N/A }
0N/A
0N/A if ((y_e < yCl) || (y_e >= hCl)) {
0N/A if (sdy > 0)
0N/A sdy -= 1;
0N/A else
0N/A sdy += 1;
0N/A }
0N/A }
0N/A
0N/A leftEdges[i] = xLeft;
0N/A rightEdges[i] = xRight;
0N/A xStarts[i] = xs;
0N/A yStarts[i] = ys;
0N/A
0N/A if ((xRight - xLeft + 1) > max_xsize)
0N/A max_xsize = (xRight - xLeft + 1);
0N/A }
0N/A }
0N/A }
0N/A
0N/A while (leftEdges[top] > rightEdges[top] && top <= bot)
0N/A top++;
0N/A
0N/A if (top < bot)
0N/A while (leftEdges[bot] > rightEdges[bot])
0N/A bot--;
0N/A
0N/A yStart = top;
0N/A yFinish = bot;
0N/A dX = sdx;
0N/A dY = sdy;
0N/A
0N/A dstData += (yStart - 1) * dstYStride;
0N/A
0N/A STORE_PARAM(param, dstData);
0N/A STORE_PARAM(param, yStart);
0N/A STORE_PARAM(param, yFinish);
0N/A STORE_PARAM(param, max_xsize);
0N/A STORE_PARAM(param, dX);
0N/A STORE_PARAM(param, dY);
0N/A
0N/A return MLIB_SUCCESS;
0N/A}
0N/A
0N/A/***************************************************************/