jdk/src/share/native/sun/awt/medialib/mlib_ImageConvMxN_ext.c
author jgodinez
Fri, 08 Feb 2013 11:25:42 -0800
changeset 15628 228422512f97
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8005129: [parfait] #1122 - #1130 native/sun/awt/medialib/mlib_Image*.c Memory leak of pointer 'k' allocated with mlib_malloc Reviewed-by: prr, vadim Contributed-by: jia-hong.chen@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * FUNCTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *      mlib_ImageConvMxN - image convolution with edge condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * SYNOPSIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *      mlib_status mlib_ImageConvMxN(mlib_image       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *                                    const mlib_image *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *                                    const mlib_s32   *kernel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *                                    mlib_s32         m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *                                    mlib_s32         n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *                                    mlib_s32         dm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *                                    mlib_s32         dn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *                                    mlib_s32         scale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *                                    mlib_s32         cmask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *                                    mlib_edge        edge)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * ARGUMENTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      dst       Pointer to destination image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      src       Pointer to source image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      m         Kernel width (m must be not less than 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      n         Kernel height (n must be not less than 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      dm, dn    Position of key element in convolution kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      kernel    Pointer to convolution kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *      scale     The scaling factor to convert the input integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *                coefficients into floating-point coefficients:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *                floating-point coefficient = integer coefficient * 2^(-scale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      cmask     Channel mask to indicate the channels to be convolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *                Each bit of which represents a channel in the image. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *                channels corresponded to 1 bits are those to be processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *      edge      Type of edge condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * DESCRIPTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *      2-D convolution, MxN kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *      The center of the source image is mapped to the center of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *      destination image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      The unselected channels are not overwritten. If both src and dst have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *      just one channel, cmask is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *      The edge condition can be one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *              MLIB_EDGE_DST_NO_WRITE  (default)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *              MLIB_EDGE_DST_FILL_ZERO
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *              MLIB_EDGE_DST_COPY_SRC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *              MLIB_EDGE_SRC_EXTEND
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * RESTRICTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *      The src and the dst must be the same type and have same number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *      of channels (1, 2, 3, or 4).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      m >= 1, n >= 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      0 <= dm < m, 0 <= dn < n.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      For data type MLIB_BYTE:   16 <= scale <= 31 (to be compatible with VIS version)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      For data type MLIB_USHORT: 17 <= scale <= 32 (to be compatible with VIS version)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *      For data type MLIB_SHORT:  17 <= scale <= 32 (to be compatible with VIS version)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      For data type MLIB_INT:    scale >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
#include "mlib_image.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
#include "mlib_ImageConv.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
static void mlib_ImageConvMxNMulAdd_S32(mlib_d64       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                        const mlib_s32 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                        const mlib_d64 *dkernel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                        mlib_s32       n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                        mlib_s32       m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                        mlib_s32       nch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
static void mlib_ImageConvMxNMedian_S32(mlib_s32 *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                        mlib_d64 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                        mlib_s32 n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                        mlib_s32 nch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
static void mlib_ImageConvMxNS322S32_ext(mlib_s32       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                         const mlib_s32 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                         mlib_s32       n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                         mlib_s32       nch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                         mlib_s32       dx_l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                         mlib_s32       dx_r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
#ifdef MLIB_USE_FTOI_CLAMPING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
#define CLAMP_S32(dst, src)                                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  dst = (mlib_s32)(src)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
#define CLAMP_S32(dst, src) {                                   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
  mlib_d64 s0 = (mlib_d64)(src);                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  if (s0 > (mlib_d64)MLIB_S32_MAX) s0 = (mlib_d64)MLIB_S32_MAX; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
  if (s0 < (mlib_d64)MLIB_S32_MIN) s0 = (mlib_d64)MLIB_S32_MIN; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
  dst = (mlib_s32)s0;                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
#endif /* MLIB_USE_FTOI_CLAMPING */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
void mlib_ImageConvMxNMulAdd_S32(mlib_d64       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                 const mlib_s32 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                 const mlib_d64 *dkernel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                 mlib_s32       n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                 mlib_s32       m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                 mlib_s32       nch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
  mlib_d64 *dst1 = dst + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
  mlib_s32 i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
  for (j = 0; j < m; j += 3, src += 3 * nch, dkernel += 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    const mlib_s32 *src2 = src + 2 * nch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    mlib_d64 hval0 = dkernel[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    mlib_d64 hval1 = dkernel[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    mlib_d64 hval2 = dkernel[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    mlib_d64 val0 = src[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    mlib_d64 val1 = src[nch];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    mlib_d64 dval = dst[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    if (j == m - 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
      hval2 = 0.f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    else if (j == m - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
      hval1 = 0.f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
      hval2 = 0.f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
#ifdef __SUNPRO_C
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
#pragma pipeloop(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
#endif /* __SUNPRO_C */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    for (i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
      mlib_d64 dval0 = val0 * hval0 + dval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
      mlib_d64 val2 = src2[i * nch];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
      dval = dst1[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
      dval0 += val1 * hval1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
      dval0 += val2 * hval2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
      val0 = val1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
      val1 = val2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
      dst[i] = dval0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
void mlib_ImageConvMxNMedian_S32(mlib_s32 *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                 mlib_d64 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                 mlib_s32 n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                 mlib_s32 nch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
#ifdef __SUNPRO_C
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
#pragma pipeloop(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
#endif /* __SUNPRO_C */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
  for (i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    mlib_s32 res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    CLAMP_S32(res, src[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    src[i] = 0.5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    dst[i * nch] = res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
void mlib_ImageConvMxNS322S32_ext(mlib_s32       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                  const mlib_s32 *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                  mlib_s32       n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                  mlib_s32       nch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                  mlib_s32       dx_l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                  mlib_s32       dx_r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
  mlib_s32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
  mlib_d64 val = src[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
  for (i = 0; i < dx_l; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    dst[i] = (mlib_s32) val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
#ifdef __SUNPRO_C
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
#pragma pipeloop(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
#endif /* __SUNPRO_C */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
  for (; i < n - dx_r; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    dst[i] = src[nch * (i - dx_l)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
  val = dst[n - dx_r - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
  for (; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    dst[i] = (mlib_s32) val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
mlib_status mlib_convMxNext_s32(mlib_image       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                const mlib_image *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                const mlib_s32   *kernel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                mlib_s32         m,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                mlib_s32         n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                mlib_s32         dx_l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                mlib_s32         dx_r,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                mlib_s32         dy_t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                mlib_s32         dy_b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                mlib_s32         scale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                mlib_s32         cmask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
  mlib_d64 dspace[1024], *dsa = dspace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
  mlib_d64 akernel[256], *dkernel = akernel, fscale = 1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
  mlib_s32 wid_e = mlib_ImageGetWidth(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
  mlib_d64 *dsh, *dsv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
  mlib_s32 *isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
  mlib_s32 *da = mlib_ImageGetData(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
  mlib_s32 *sa = mlib_ImageGetData(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
  mlib_s32 dlb = mlib_ImageGetStride(dst) >> 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
  mlib_s32 slb = mlib_ImageGetStride(src) >> 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
  mlib_s32 dw = mlib_ImageGetWidth(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
  mlib_s32 dh = mlib_ImageGetHeight(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
  mlib_s32 nch = mlib_ImageGetChannels(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
  mlib_s32 i, j, j1, k, mn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
  /* internal buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
  if (3 * wid_e + m > 1024) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    dsa = mlib_malloc((3 * wid_e + m) * sizeof(mlib_d64));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    if (dsa == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
      return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
  isa = (mlib_s32 *) dsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
  /* load kernel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
  mn = m * n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
  if (mn > 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    dkernel = mlib_malloc(mn * sizeof(mlib_d64));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
15628
228422512f97 8005129: [parfait] #1122 - #1130 native/sun/awt/medialib/mlib_Image*.c Memory leak of pointer 'k' allocated with mlib_malloc
jgodinez
parents: 5506
diff changeset
   256
    if (dkernel == NULL) {
228422512f97 8005129: [parfait] #1122 - #1130 native/sun/awt/medialib/mlib_Image*.c Memory leak of pointer 'k' allocated with mlib_malloc
jgodinez
parents: 5506
diff changeset
   257
      if (dsa != dspace) mlib_free(dsa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
      return MLIB_FAILURE;
15628
228422512f97 8005129: [parfait] #1122 - #1130 native/sun/awt/medialib/mlib_Image*.c Memory leak of pointer 'k' allocated with mlib_malloc
jgodinez
parents: 5506
diff changeset
   259
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
  while (scale > 30) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    fscale /= (1 << 30);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    scale -= 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
  fscale /= (1 << scale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
  for (i = 0; i < mn; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    dkernel[i] = ((mlib_s32 *) kernel)[i] * fscale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
  dsh = dsa + dw + m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
  dsv = dsh + dw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
  for (i = 0; i < dw; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    dsh[i] = 0.5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    dsv[i] = 0.5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
  for (j = 0; j < dh; j++, da += dlb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    for (k = 0; k < nch; k++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
      if (cmask & (1 << (nch - 1 - k))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        mlib_s32 *sa1 = sa + k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        mlib_d64 *dkernel1 = dkernel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        for (j1 = 0; j1 < n; j1++, dkernel1 += m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
          mlib_ImageConvMxNS322S32_ext(isa, sa1, dw + m - 1, nch, dx_l, dx_r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
          mlib_ImageConvMxNMulAdd_S32(dsh, isa, dkernel1, dw, m, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
          if ((j + j1 >= dy_t) && (j + j1 < dh + n - dy_b - 2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            sa1 += slb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        mlib_ImageConvMxNMedian_S32(da + k, dsh, dw, nch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    if ((j >= dy_t) && (j < dh + n - dy_b - 2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
      sa += slb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
  if (dkernel != akernel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    mlib_free(dkernel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
  if (dsa != dspace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    mlib_free(dsa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
  return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
/***************************************************************/