0N/A/*
2362N/A * Copyright (c) 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 * FUNCTION
0N/A * mlib_ImageConvMxN - image convolution with edge condition
0N/A *
0N/A * SYNOPSIS
0N/A * mlib_status mlib_ImageConvMxN(mlib_image *dst,
0N/A * const mlib_image *src,
0N/A * const mlib_s32 *kernel,
0N/A * mlib_s32 m,
0N/A * mlib_s32 n,
0N/A * mlib_s32 dm,
0N/A * mlib_s32 dn,
0N/A * mlib_s32 scale,
0N/A * mlib_s32 cmask,
0N/A * mlib_edge edge)
0N/A *
0N/A * ARGUMENTS
0N/A * dst Pointer to destination image.
0N/A * src Pointer to source image.
0N/A * m Kernel width (m must be not less than 1).
0N/A * n Kernel height (n must be not less than 1).
0N/A * dm, dn Position of key element in convolution kernel.
0N/A * kernel Pointer to convolution kernel.
0N/A * scale The scaling factor to convert the input integer
0N/A * coefficients into floating-point coefficients:
0N/A * floating-point coefficient = integer coefficient * 2^(-scale)
0N/A * cmask Channel mask to indicate the channels to be convolved.
0N/A * Each bit of which represents a channel in the image. The
0N/A * channels corresponded to 1 bits are those to be processed.
0N/A * edge Type of edge condition.
0N/A *
0N/A * DESCRIPTION
0N/A * 2-D convolution, MxN kernel.
0N/A *
0N/A * The center of the source image is mapped to the center of the
0N/A * destination image.
0N/A * The unselected channels are not overwritten. If both src and dst have
0N/A * just one channel, cmask is ignored.
0N/A *
0N/A * The edge condition can be one of the following:
0N/A * MLIB_EDGE_DST_NO_WRITE (default)
0N/A * MLIB_EDGE_DST_FILL_ZERO
0N/A * MLIB_EDGE_DST_COPY_SRC
0N/A * MLIB_EDGE_SRC_EXTEND
0N/A *
0N/A * RESTRICTION
0N/A * The src and the dst must be the same type and have same number
0N/A * of channels (1, 2, 3, or 4). They can be in MLIB_BIT, MLIB_BYTE,
0N/A * MLIB_SHORT, MLIB_USHORT or MLIB_INT data type.
0N/A * m >= 1, n >= 1,
0N/A * 0 <= dm < m, 0 <= dn < n.
0N/A * For data type MLIB_BYTE: 16 <= scale <= 31 (to be compatible with VIS version)
0N/A * For data type MLIB_SHORT: 17 <= scale <= 32 (to be compatible with VIS version)
0N/A * For data type MLIB_USHORT: 17 <= scale <= 32 (to be compatible with VIS version)
0N/A * For data type MLIB_INT: scale >= 0
0N/A */
0N/A
0N/A#include "mlib_image.h"
0N/A#include "mlib_ImageCheck.h"
0N/A#include "mlib_ImageConv.h"
0N/A#include "mlib_ImageCreate.h"
0N/A#include "mlib_c_ImageConv.h"
0N/A#include "mlib_ImageClipping.h"
0N/A#include "mlib_ImageConvEdge.h"
0N/A
0N/A/***************************************************************/
0N/Amlib_status mlib_ImageConvMxN(mlib_image *dst,
0N/A const mlib_image *src,
0N/A const mlib_s32 *kernel,
0N/A mlib_s32 m,
0N/A mlib_s32 n,
0N/A mlib_s32 dm,
0N/A mlib_s32 dn,
0N/A mlib_s32 scale,
0N/A mlib_s32 cmask,
0N/A mlib_edge edge)
0N/A{
0N/A MLIB_IMAGE_CHECK(dst);
0N/A
0N/A switch (mlib_ImageGetType(dst)) {
0N/A case MLIB_BYTE:
0N/A
0N/A if (scale < 16 || scale > 31)
0N/A return MLIB_FAILURE;
0N/A break;
0N/A case MLIB_SHORT:
0N/A case MLIB_USHORT:
0N/A
0N/A if (scale < 17 || scale > 32)
0N/A return MLIB_FAILURE;
0N/A break;
0N/A case MLIB_INT:
0N/A
0N/A if (scale < 0)
0N/A return MLIB_FAILURE;
0N/A break;
0N/A default:
0N/A return MLIB_FAILURE;
0N/A }
0N/A
0N/A return mlib_ImageConvMxN_f(dst, src, kernel, m, n, dm, dn, scale, cmask, edge);
0N/A}
0N/A
0N/A/***************************************************************/
0N/Amlib_status mlib_ImageConvMxN_f(mlib_image *dst,
0N/A const mlib_image *src,
0N/A const void *kernel,
0N/A mlib_s32 m,
0N/A mlib_s32 n,
0N/A mlib_s32 dm,
0N/A mlib_s32 dn,
0N/A mlib_s32 scale,
0N/A mlib_s32 cmask,
0N/A mlib_edge edge)
0N/A{
0N/A mlib_image dst_i[1], src_i[1], dst_e[1], src_e[1];
0N/A mlib_type type;
0N/A mlib_s32 nchan, dx_l, dx_r, dy_t, dy_b;
0N/A mlib_s32 edg_sizes[8];
0N/A mlib_status ret;
0N/A
0N/A if (m < 1 || n < 1 || dm < 0 || dm > m - 1 || dn < 0 || dn > n - 1)
0N/A return MLIB_FAILURE;
0N/A
0N/A if (kernel == NULL)
0N/A return MLIB_NULLPOINTER;
0N/A
0N/A ret =
0N/A mlib_ImageClippingMxN(dst_i, src_i, dst_e, src_e, edg_sizes, dst, src, m, n, dm, dn);
0N/A
0N/A if (ret != MLIB_SUCCESS)
0N/A return ret;
0N/A
0N/A nchan = mlib_ImageGetChannels(dst);
0N/A type = mlib_ImageGetType(dst);
0N/A
0N/A if (nchan == 1)
0N/A cmask = 1;
0N/A
0N/A if ((cmask & ((1 << nchan) - 1)) == 0)
0N/A return MLIB_SUCCESS;
0N/A
0N/A dx_l = edg_sizes[0];
0N/A dx_r = edg_sizes[1];
0N/A dy_t = edg_sizes[2];
0N/A dy_b = edg_sizes[3];
0N/A
0N/A if (dx_l + dx_r + dy_t + dy_b == 0)
0N/A edge = MLIB_EDGE_DST_NO_WRITE;
0N/A
0N/A if (edge != MLIB_EDGE_SRC_EXTEND) {
0N/A if (mlib_ImageGetWidth(dst_i) >= m && mlib_ImageGetHeight(dst_i) >= n) {
0N/A switch (type) {
0N/A case MLIB_BYTE:
0N/A ret = mlib_convMxNnw_u8(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);
0N/A break;
0N/A case MLIB_SHORT:
0N/A#ifdef __sparc
0N/A ret = mlib_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);
0N/A#else
0N/A
0N/A if (mlib_ImageConvVersion(m, n, scale, type) == 0)
0N/A ret = mlib_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);
0N/A else
0N/A ret = mlib_i_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);
0N/A#endif /* __sparc */
0N/A break;
0N/A case MLIB_USHORT:
0N/A#ifdef __sparc
0N/A ret = mlib_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);
0N/A#else
0N/A
0N/A if (mlib_ImageConvVersion(m, n, scale, type) == 0)
0N/A ret = mlib_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);
0N/A else
0N/A ret = mlib_i_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);
0N/A#endif /* __sparc */
0N/A break;
0N/A case MLIB_INT:
0N/A ret = mlib_convMxNnw_s32(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);
0N/A break;
0N/A case MLIB_FLOAT:
0N/A ret = mlib_convMxNnw_f32(dst_i, src_i, kernel, m, n, dm, dn, cmask);
0N/A break;
0N/A case MLIB_DOUBLE:
0N/A ret = mlib_convMxNnw_d64(dst_i, src_i, kernel, m, n, dm, dn, cmask);
0N/A break;
3813N/A
3813N/A default:
3813N/A /* For some reasons, there is no convolution routine for type MLIB_BIT.
3813N/A * For now, we silently ignore it (because this image type is not used by java),
3813N/A * but probably we have to report an error.
3813N/A */
3813N/A break;
0N/A }
0N/A }
0N/A
0N/A switch (edge) {
0N/A case MLIB_EDGE_DST_FILL_ZERO:
0N/A mlib_ImageConvZeroEdge(dst_e, dx_l, dx_r, dy_t, dy_b, cmask);
0N/A break;
0N/A case MLIB_EDGE_DST_COPY_SRC:
0N/A mlib_ImageConvCopyEdge(dst_e, src_e, dx_l, dx_r, dy_t, dy_b, cmask);
0N/A break;
3813N/A default:
3813N/A /* Other edge conditions do not need additional handling.
3813N/A * Note also that they are not exposed in public Java API
3813N/A */
3813N/A break;
0N/A }
0N/A }
0N/A else { /* MLIB_EDGE_SRC_EXTEND */
0N/A /* adjust src_e image */
0N/A mlib_ImageSetSubimage(src_e, src_e, dx_l - dm, dy_t - dn,
0N/A mlib_ImageGetWidth(src_e), mlib_ImageGetHeight(src_e));
0N/A
0N/A switch (type) {
0N/A case MLIB_BYTE:
0N/A ret =
0N/A mlib_convMxNext_u8(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,
0N/A cmask);
0N/A break;
0N/A case MLIB_SHORT:
0N/A#ifdef __sparc
0N/A ret =
0N/A mlib_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,
0N/A cmask);
0N/A#else
0N/A
0N/A if (mlib_ImageConvVersion(m, n, scale, type) == 0)
0N/A ret =
0N/A mlib_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,
0N/A cmask);
0N/A else
0N/A ret =
0N/A mlib_i_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b,
0N/A scale, cmask);
0N/A#endif /* __sparc */
0N/A break;
0N/A case MLIB_USHORT:
0N/A#ifdef __sparc
0N/A ret =
0N/A mlib_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,
0N/A cmask);
0N/A#else
0N/A
0N/A if (mlib_ImageConvVersion(m, n, scale, type) == 0)
0N/A ret =
0N/A mlib_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,
0N/A cmask);
0N/A else
0N/A ret =
0N/A mlib_i_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b,
0N/A scale, cmask);
0N/A#endif /* __sparc */
0N/A break;
0N/A case MLIB_INT:
0N/A ret =
0N/A mlib_convMxNext_s32(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,
0N/A cmask);
0N/A break;
0N/A case MLIB_FLOAT:
0N/A mlib_convMxNext_f32(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, cmask);
0N/A break;
0N/A case MLIB_DOUBLE:
0N/A mlib_convMxNext_d64(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, cmask);
0N/A break;
3813N/A default:
3813N/A /* For some reasons, there is no convolution routine for type MLIB_BIT.
3813N/A * For now, we silently ignore it (because this image type is not used by java),
3813N/A * but probably we have to report an error.
3813N/A */
3813N/A break;
0N/A }
0N/A }
0N/A
0N/A return ret;
0N/A}
0N/A
0N/A/***************************************************************/