1 /*
   2  * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 /*
  28  * FUNCTION
  29  *      mlib_ImageAffine_u16_1ch_bl
  30  *      mlib_ImageAffine_u16_2ch_bl
  31  *      mlib_ImageAffine_u16_3ch_bl
  32  *      mlib_ImageAffine_u16_4ch_bl
  33  *        - image affine transformation with Bilinear filtering
  34  * SYNOPSIS
  35  *      mlib_status mlib_ImageAffine_u16_?ch_bl(mlib_s32 *leftEdges,
  36  *                                              mlib_s32 *rightEdges,
  37  *                                              mlib_s32 *xStarts,
  38  *                                              mlib_s32 *yStarts,
  39  *                                              mlib_s32 *sides,
  40  *                                              mlib_u8  *dstData,
  41  *                                              mlib_u8  **lineAddr,
  42  *                                              mlib_s32 dstYStride,
  43  *                                              mlib_s32 is_affine,
  44  *                                              mlib_s32 srcYStride)
  45  *
  46  * ARGUMENTS
  47  *      leftEdges  array[dstHeight] of xLeft coordinates
  48  *      RightEdges array[dstHeight] of xRight coordinates
  49  *      xStarts    array[dstHeight] of xStart * 65536 coordinates
  50  *      yStarts    array[dstHeight] of yStart * 65536 coordinates
  51  *      sides      output array[4]. sides[0] is yStart, sides[1] is yFinish,
  52  *                 sides[2] is dx * 65536, sides[3] is dy * 65536
  53  *      dstData    pointer to the first pixel on (yStart - 1) line
  54  *      lineAddr   array[srcHeight] of pointers to the first pixel on
  55  *                 the corresponding lines
  56  *      dstYStride stride of destination image
  57  *      is_affine  indicator (Affine - GridWarp)
  58  *      srcYStride stride of source image
  59  *
  60  * DESCRIPTION
  61  *      The functions step along the lines from xLeft to xRight and apply
  62  *      the bilinear filtering.
  63  *
  64  */
  65 
  66 #include "mlib_ImageAffine.h"
  67 
  68 /***************************************************************/
  69 #define DTYPE  mlib_u16
  70 #define FTYPE  mlib_d64
  71 
  72 /***************************************************************/
  73 #define TTYPE    mlib_s32
  74 #define I2F(x)   (x)
  75 #define ROUND(x) (x)
  76 
  77 #define FUN_NAME(CHAN) mlib_ImageAffine_u16_##CHAN##_bl
  78 
  79 /***************************************************************/
  80 /* for x86, using integer multiplies is faster */
  81 
  82 /***************************************************************/
  83 /* for SHORT/USHORT decrease MLIB_SHIFT due to overflow in multiplies like fdy * (a10 - a00) */
  84 #undef  MLIB_SHIFT
  85 #define MLIB_SHIFT  15
  86 
  87 #define MLIB_ROUND   (1 << (MLIB_SHIFT - 1))
  88 
  89 /***************************************************************/
  90 #define GET_POINTERS(ind)                                        \
  91   fdx = X & MLIB_MASK;                                           \
  92   fdy = Y & MLIB_MASK;                                           \
  93   ySrc = MLIB_POINTER_SHIFT(Y);                                  \
  94   xSrc = X >> MLIB_SHIFT;                                        \
  95   srcPixelPtr = MLIB_POINTER_GET(lineAddr, ySrc) + ind * xSrc;   \
  96   srcPixelPtr2 = (DTYPE *)((mlib_u8 *)srcPixelPtr + srcYStride); \
  97   X += dX;                                                       \
  98   Y += dY
  99 
 100 /***************************************************************/
 101 #define COUNT(ind)                                                                       \
 102   pix0_##ind = a00_##ind + ((fdy * (a10_##ind - a00_##ind) + MLIB_ROUND) >> MLIB_SHIFT); \
 103   pix1_##ind = a01_##ind + ((fdy * (a11_##ind - a01_##ind) + MLIB_ROUND) >> MLIB_SHIFT); \
 104   res##ind = pix0_##ind + ((fdx * (pix1_##ind - pix0_##ind) + MLIB_ROUND) >> MLIB_SHIFT)
 105 
 106 /***************************************************************/
 107 #define LOAD(ind, ind1, ind2)                                   \
 108   a00_##ind = srcPixelPtr[ind1];                                \
 109   a01_##ind = srcPixelPtr[ind2];                                \
 110   a10_##ind = srcPixelPtr2[ind1];                               \
 111   a11_##ind = srcPixelPtr2[ind2]
 112 
 113 /***************************************************************/
 114 mlib_status FUN_NAME(1ch)(mlib_affine_param *param)
 115 {
 116   DECLAREVAR_BL();
 117   DTYPE *dstLineEnd;
 118   DTYPE *srcPixelPtr2;
 119 
 120 #if MLIB_SHIFT == 15
 121   dX = (dX + 1) >> 1;
 122   dY = (dY + 1) >> 1;
 123 #endif
 124 
 125   for (j = yStart; j <= yFinish; j++) {
 126     mlib_s32 fdx, fdy;
 127     mlib_s32 a00_0, a01_0, a10_0, a11_0;
 128     mlib_s32 pix0_0, pix1_0, res0;
 129 
 130     CLIP(1);
 131     dstLineEnd = (DTYPE *) dstData + xRight;
 132 #if MLIB_SHIFT == 15
 133     X = X >> 1;
 134     Y = Y >> 1;
 135 
 136     if (warp_tbl != NULL) {
 137       dX = (dX + 1) >> 1;
 138       dY = (dY + 1) >> 1;
 139     }
 140 
 141 #endif
 142 
 143     GET_POINTERS(1);
 144     LOAD(0, 0, 1);
 145 
 146     for (; dstPixelPtr < dstLineEnd; dstPixelPtr++) {
 147       COUNT(0);
 148       GET_POINTERS(1);
 149       LOAD(0, 0, 1);
 150       dstPixelPtr[0] = (DTYPE) res0;
 151     }
 152 
 153     COUNT(0);
 154     dstPixelPtr[0] = (DTYPE) res0;
 155   }
 156 
 157   return MLIB_SUCCESS;
 158 }
 159 
 160 /***************************************************************/
 161 mlib_status FUN_NAME(2ch)(mlib_affine_param *param)
 162 {
 163   DECLAREVAR_BL();
 164   DTYPE *dstLineEnd;
 165   DTYPE *srcPixelPtr2;
 166 
 167 #if MLIB_SHIFT == 15
 168   dX = (dX + 1) >> 1;
 169   dY = (dY + 1) >> 1;
 170 #endif
 171 
 172   for (j = yStart; j <= yFinish; j++) {
 173     mlib_s32 fdx, fdy;
 174     mlib_s32 a00_0, a01_0, a10_0, a11_0;
 175     mlib_s32 a00_1, a01_1, a10_1, a11_1;
 176     mlib_s32 pix0_0, pix1_0, res0;
 177     mlib_s32 pix0_1, pix1_1, res1;
 178 
 179     CLIP(2);
 180     dstLineEnd = (DTYPE *) dstData + 2 * xRight;
 181 #if MLIB_SHIFT == 15
 182     X = X >> 1;
 183     Y = Y >> 1;
 184 
 185     if (warp_tbl != NULL) {
 186       dX = (dX + 1) >> 1;
 187       dY = (dY + 1) >> 1;
 188     }
 189 
 190 #endif
 191 
 192     GET_POINTERS(2);
 193     LOAD(0, 0, 2);
 194     LOAD(1, 1, 3);
 195 
 196     for (; dstPixelPtr < dstLineEnd; dstPixelPtr += 2) {
 197       COUNT(0);
 198       COUNT(1);
 199       GET_POINTERS(2);
 200       LOAD(0, 0, 2);
 201       LOAD(1, 1, 3);
 202       dstPixelPtr[0] = (DTYPE) res0;
 203       dstPixelPtr[1] = (DTYPE) res1;
 204     }
 205 
 206     COUNT(0);
 207     COUNT(1);
 208     dstPixelPtr[0] = (DTYPE) res0;
 209     dstPixelPtr[1] = (DTYPE) res1;
 210   }
 211 
 212   return MLIB_SUCCESS;
 213 }
 214 
 215 /***************************************************************/
 216 mlib_status FUN_NAME(3ch)(mlib_affine_param *param)
 217 {
 218   DECLAREVAR_BL();
 219   DTYPE *dstLineEnd;
 220   DTYPE *srcPixelPtr2;
 221 
 222 #if MLIB_SHIFT == 15
 223   dX = (dX + 1) >> 1;
 224   dY = (dY + 1) >> 1;
 225 #endif
 226 
 227   for (j = yStart; j <= yFinish; j++) {
 228     mlib_s32 fdx, fdy;
 229     mlib_s32 a00_0, a01_0, a10_0, a11_0;
 230     mlib_s32 a00_1, a01_1, a10_1, a11_1;
 231     mlib_s32 a00_2, a01_2, a10_2, a11_2;
 232     mlib_s32 pix0_0, pix1_0, res0;
 233     mlib_s32 pix0_1, pix1_1, res1;
 234     mlib_s32 pix0_2, pix1_2, res2;
 235 
 236     CLIP(3);
 237     dstLineEnd = (DTYPE *) dstData + 3 * xRight;
 238 #if MLIB_SHIFT == 15
 239     X = X >> 1;
 240     Y = Y >> 1;
 241 
 242     if (warp_tbl != NULL) {
 243       dX = (dX + 1) >> 1;
 244       dY = (dY + 1) >> 1;
 245     }
 246 
 247 #endif
 248 
 249     GET_POINTERS(3);
 250     LOAD(0, 0, 3);
 251     LOAD(1, 1, 4);
 252     LOAD(2, 2, 5);
 253 
 254     for (; dstPixelPtr < dstLineEnd; dstPixelPtr += 3) {
 255       COUNT(0);
 256       COUNT(1);
 257       COUNT(2);
 258       GET_POINTERS(3);
 259       LOAD(0, 0, 3);
 260       LOAD(1, 1, 4);
 261       LOAD(2, 2, 5);
 262       dstPixelPtr[0] = (DTYPE) res0;
 263       dstPixelPtr[1] = (DTYPE) res1;
 264       dstPixelPtr[2] = (DTYPE) res2;
 265     }
 266 
 267     COUNT(0);
 268     COUNT(1);
 269     COUNT(2);
 270     dstPixelPtr[0] = (DTYPE) res0;
 271     dstPixelPtr[1] = (DTYPE) res1;
 272     dstPixelPtr[2] = (DTYPE) res2;
 273   }
 274 
 275   return MLIB_SUCCESS;
 276 }
 277 
 278 /***************************************************************/
 279 mlib_status FUN_NAME(4ch)(mlib_affine_param *param)
 280 {
 281   DECLAREVAR_BL();
 282   DTYPE *dstLineEnd;
 283   DTYPE *srcPixelPtr2;
 284 
 285 #if MLIB_SHIFT == 15
 286   dX = (dX + 1) >> 1;
 287   dY = (dY + 1) >> 1;
 288 #endif
 289 
 290   for (j = yStart; j <= yFinish; j++) {
 291     mlib_s32 fdx, fdy;
 292     mlib_s32 a00_0, a01_0, a10_0, a11_0;
 293     mlib_s32 a00_1, a01_1, a10_1, a11_1;
 294     mlib_s32 a00_2, a01_2, a10_2, a11_2;
 295     mlib_s32 a00_3, a01_3, a10_3, a11_3;
 296     mlib_s32 pix0_0, pix1_0, res0;
 297     mlib_s32 pix0_1, pix1_1, res1;
 298     mlib_s32 pix0_2, pix1_2, res2;
 299     mlib_s32 pix0_3, pix1_3, res3;
 300 
 301     CLIP(4);
 302     dstLineEnd = (DTYPE *) dstData + 4 * xRight;
 303 #if MLIB_SHIFT == 15
 304     X = X >> 1;
 305     Y = Y >> 1;
 306 
 307     if (warp_tbl != NULL) {
 308       dX = (dX + 1) >> 1;
 309       dY = (dY + 1) >> 1;
 310     }
 311 
 312 #endif
 313 
 314     GET_POINTERS(4);
 315     LOAD(0, 0, 4);
 316     LOAD(1, 1, 5);
 317     LOAD(2, 2, 6);
 318     LOAD(3, 3, 7);
 319 
 320     for (; dstPixelPtr < dstLineEnd; dstPixelPtr += 4) {
 321       COUNT(0);
 322       COUNT(1);
 323       COUNT(2);
 324       COUNT(3);
 325       GET_POINTERS(4);
 326       LOAD(0, 0, 4);
 327       LOAD(1, 1, 5);
 328       LOAD(2, 2, 6);
 329       LOAD(3, 3, 7);
 330       dstPixelPtr[0] = (DTYPE) res0;
 331       dstPixelPtr[1] = (DTYPE) res1;
 332       dstPixelPtr[2] = (DTYPE) res2;
 333       dstPixelPtr[3] = (DTYPE) res3;
 334     }
 335 
 336     COUNT(0);
 337     COUNT(1);
 338     COUNT(2);
 339     COUNT(3);
 340     dstPixelPtr[0] = (DTYPE) res0;
 341     dstPixelPtr[1] = (DTYPE) res1;
 342     dstPixelPtr[2] = (DTYPE) res2;
 343     dstPixelPtr[3] = (DTYPE) res3;
 344   }
 345 
 346   return MLIB_SUCCESS;
 347 }
 348 
 349 /***************************************************************/