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_s16_1ch_bl
  30  *      mlib_ImageAffine_s16_2ch_bl
  31  *      mlib_ImageAffine_s16_3ch_bl
  32  *      mlib_ImageAffine_s16_4ch_bl
  33  *        - image affine transformation with Bilinear filtering
  34  * SYNOPSIS
  35  *      mlib_status mlib_ImageAffine_s16_?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_s16
  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_s16_##CHAN##_bl
  78 
  79 /***************************************************************/
  80 /* for x86, using integer multiplies is faster */
  81 
  82 /***************************************************************/
  83 /* for SHORT/USHORT decrease MLIB_SHIFT due to
  84  * overflow in multiplies like fdy * (a10 - a00)
  85  */
  86 #undef  MLIB_SHIFT
  87 #define MLIB_SHIFT  15
  88 
  89 #define MLIB_ROUND   (1 << (MLIB_SHIFT - 1))
  90 
  91 /***************************************************************/
  92 #define GET_POINTERS(ind)                                        \
  93   fdx = X & MLIB_MASK;                                           \
  94   fdy = Y & MLIB_MASK;                                           \
  95   ySrc = MLIB_POINTER_SHIFT(Y);                                  \
  96   xSrc = X >> MLIB_SHIFT;                                        \
  97   srcPixelPtr = MLIB_POINTER_GET(lineAddr, ySrc) + ind * xSrc;   \
  98   srcPixelPtr2 = (DTYPE *)((mlib_u8 *)srcPixelPtr + srcYStride); \
  99   X += dX;                                                       \
 100   Y += dY
 101 
 102 /***************************************************************/
 103 #define COUNT(ind)                                                                       \
 104   pix0_##ind = a00_##ind + ((fdy * (a10_##ind - a00_##ind) + MLIB_ROUND) >> MLIB_SHIFT); \
 105   pix1_##ind = a01_##ind + ((fdy * (a11_##ind - a01_##ind) + MLIB_ROUND) >> MLIB_SHIFT); \
 106   res##ind = pix0_##ind + ((fdx * (pix1_##ind - pix0_##ind) + MLIB_ROUND) >> MLIB_SHIFT)
 107 
 108 /***************************************************************/
 109 #define LOAD(ind, ind1, ind2)                                   \
 110   a00_##ind = srcPixelPtr[ind1];                                \
 111   a01_##ind = srcPixelPtr[ind2];                                \
 112   a10_##ind = srcPixelPtr2[ind1];                               \
 113   a11_##ind = srcPixelPtr2[ind2]
 114 
 115 /***************************************************************/
 116 mlib_status FUN_NAME(1ch)(mlib_affine_param *param)
 117 {
 118   DECLAREVAR_BL();
 119   DTYPE *dstLineEnd;
 120   DTYPE *srcPixelPtr2;
 121 
 122 #if MLIB_SHIFT == 15
 123   dX = (dX + 1) >> 1;
 124   dY = (dY + 1) >> 1;
 125 #endif /* MLIB_SHIFT == 15 */
 126 
 127   for (j = yStart; j <= yFinish; j++) {
 128     mlib_s32 fdx, fdy;
 129     mlib_s32 a00_0, a01_0, a10_0, a11_0;
 130     mlib_s32 pix0_0, pix1_0, res0;
 131 
 132     CLIP(1);
 133     dstLineEnd = (DTYPE *) dstData + xRight;
 134 #if MLIB_SHIFT == 15
 135     X = X >> 1;
 136     Y = Y >> 1;
 137 
 138     if (warp_tbl != NULL) {
 139       dX = (dX + 1) >> 1;
 140       dY = (dY + 1) >> 1;
 141     }
 142 
 143 #endif /* MLIB_SHIFT == 15 */
 144 
 145     GET_POINTERS(1);
 146     LOAD(0, 0, 1);
 147     for (; dstPixelPtr < dstLineEnd; dstPixelPtr++) {
 148       COUNT(0);
 149       GET_POINTERS(1);
 150       LOAD(0, 0, 1);
 151       dstPixelPtr[0] = (DTYPE) res0;
 152     }
 153 
 154     COUNT(0);
 155     dstPixelPtr[0] = (DTYPE) res0;
 156   }
 157 
 158   return MLIB_SUCCESS;
 159 }
 160 
 161 /***************************************************************/
 162 mlib_status FUN_NAME(2ch)(mlib_affine_param *param)
 163 {
 164   DECLAREVAR_BL();
 165   DTYPE *dstLineEnd;
 166   DTYPE *srcPixelPtr2;
 167 
 168 #if MLIB_SHIFT == 15
 169   dX = (dX + 1) >> 1;
 170   dY = (dY + 1) >> 1;
 171 #endif /* MLIB_SHIFT == 15 */
 172 
 173   for (j = yStart; j <= yFinish; j++) {
 174     mlib_s32 fdx, fdy;
 175     mlib_s32 a00_0, a01_0, a10_0, a11_0;
 176     mlib_s32 a00_1, a01_1, a10_1, a11_1;
 177     mlib_s32 pix0_0, pix1_0, res0;
 178     mlib_s32 pix0_1, pix1_1, res1;
 179 
 180     CLIP(2);
 181     dstLineEnd = (DTYPE *) dstData + 2 * xRight;
 182 #if MLIB_SHIFT == 15
 183     X = X >> 1;
 184     Y = Y >> 1;
 185 
 186     if (warp_tbl != NULL) {
 187       dX = (dX + 1) >> 1;
 188       dY = (dY + 1) >> 1;
 189     }
 190 
 191 #endif /* MLIB_SHIFT == 15 */
 192 
 193     GET_POINTERS(2);
 194     LOAD(0, 0, 2);
 195     LOAD(1, 1, 3);
 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 /* MLIB_SHIFT == 15 */
 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 /* MLIB_SHIFT == 15 */
 248 
 249     GET_POINTERS(3);
 250     LOAD(0, 0, 3);
 251     LOAD(1, 1, 4);
 252     LOAD(2, 2, 5);
 253     for (; dstPixelPtr < dstLineEnd; dstPixelPtr += 3) {
 254       COUNT(0);
 255       COUNT(1);
 256       COUNT(2);
 257       GET_POINTERS(3);
 258       LOAD(0, 0, 3);
 259       LOAD(1, 1, 4);
 260       LOAD(2, 2, 5);
 261       dstPixelPtr[0] = (DTYPE) res0;
 262       dstPixelPtr[1] = (DTYPE) res1;
 263       dstPixelPtr[2] = (DTYPE) res2;
 264     }
 265 
 266     COUNT(0);
 267     COUNT(1);
 268     COUNT(2);
 269     dstPixelPtr[0] = (DTYPE) res0;
 270     dstPixelPtr[1] = (DTYPE) res1;
 271     dstPixelPtr[2] = (DTYPE) res2;
 272   }
 273 
 274   return MLIB_SUCCESS;
 275 }
 276 
 277 /***************************************************************/
 278 mlib_status FUN_NAME(4ch)(mlib_affine_param *param)
 279 {
 280   DECLAREVAR_BL();
 281   DTYPE *dstLineEnd;
 282   DTYPE *srcPixelPtr2;
 283 
 284 #if MLIB_SHIFT == 15
 285   dX = (dX + 1) >> 1;
 286   dY = (dY + 1) >> 1;
 287 #endif /* MLIB_SHIFT == 15 */
 288 
 289   for (j = yStart; j <= yFinish; j++) {
 290     mlib_s32 fdx, fdy;
 291     mlib_s32 a00_0, a01_0, a10_0, a11_0;
 292     mlib_s32 a00_1, a01_1, a10_1, a11_1;
 293     mlib_s32 a00_2, a01_2, a10_2, a11_2;
 294     mlib_s32 a00_3, a01_3, a10_3, a11_3;
 295     mlib_s32 pix0_0, pix1_0, res0;
 296     mlib_s32 pix0_1, pix1_1, res1;
 297     mlib_s32 pix0_2, pix1_2, res2;
 298     mlib_s32 pix0_3, pix1_3, res3;
 299 
 300     CLIP(4);
 301     dstLineEnd = (DTYPE *) dstData + 4 * xRight;
 302 #if MLIB_SHIFT == 15
 303     X = X >> 1;
 304     Y = Y >> 1;
 305 
 306     if (warp_tbl != NULL) {
 307       dX = (dX + 1) >> 1;
 308       dY = (dY + 1) >> 1;
 309     }
 310 
 311 #endif /* MLIB_SHIFT == 15 */
 312 
 313     GET_POINTERS(4);
 314     LOAD(0, 0, 4);
 315     LOAD(1, 1, 5);
 316     LOAD(2, 2, 6);
 317     LOAD(3, 3, 7);
 318     for (; dstPixelPtr < dstLineEnd; dstPixelPtr += 4) {
 319       COUNT(0);
 320       COUNT(1);
 321       COUNT(2);
 322       COUNT(3);
 323       GET_POINTERS(4);
 324       LOAD(0, 0, 4);
 325       LOAD(1, 1, 5);
 326       LOAD(2, 2, 6);
 327       LOAD(3, 3, 7);
 328       dstPixelPtr[0] = (DTYPE) res0;
 329       dstPixelPtr[1] = (DTYPE) res1;
 330       dstPixelPtr[2] = (DTYPE) res2;
 331       dstPixelPtr[3] = (DTYPE) res3;
 332     }
 333 
 334     COUNT(0);
 335     COUNT(1);
 336     COUNT(2);
 337     COUNT(3);
 338     dstPixelPtr[0] = (DTYPE) res0;
 339     dstPixelPtr[1] = (DTYPE) res1;
 340     dstPixelPtr[2] = (DTYPE) res2;
 341     dstPixelPtr[3] = (DTYPE) res3;
 342   }
 343 
 344   return MLIB_SUCCESS;
 345 }
 346 
 347 /***************************************************************/