Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/awt/medialib/mlib_ImageConvMxN.c
38918 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526/*27* FUNCTION28* mlib_ImageConvMxN - image convolution with edge condition29*30* SYNOPSIS31* mlib_status mlib_ImageConvMxN(mlib_image *dst,32* const mlib_image *src,33* const mlib_s32 *kernel,34* mlib_s32 m,35* mlib_s32 n,36* mlib_s32 dm,37* mlib_s32 dn,38* mlib_s32 scale,39* mlib_s32 cmask,40* mlib_edge edge)41*42* ARGUMENTS43* dst Pointer to destination image.44* src Pointer to source image.45* m Kernel width (m must be not less than 1).46* n Kernel height (n must be not less than 1).47* dm, dn Position of key element in convolution kernel.48* kernel Pointer to convolution kernel.49* scale The scaling factor to convert the input integer50* coefficients into floating-point coefficients:51* floating-point coefficient = integer coefficient * 2^(-scale)52* cmask Channel mask to indicate the channels to be convolved.53* Each bit of which represents a channel in the image. The54* channels corresponded to 1 bits are those to be processed.55* edge Type of edge condition.56*57* DESCRIPTION58* 2-D convolution, MxN kernel.59*60* The center of the source image is mapped to the center of the61* destination image.62* The unselected channels are not overwritten. If both src and dst have63* just one channel, cmask is ignored.64*65* The edge condition can be one of the following:66* MLIB_EDGE_DST_NO_WRITE (default)67* MLIB_EDGE_DST_FILL_ZERO68* MLIB_EDGE_DST_COPY_SRC69* MLIB_EDGE_SRC_EXTEND70*71* RESTRICTION72* The src and the dst must be the same type and have same number73* of channels (1, 2, 3, or 4). They can be in MLIB_BIT, MLIB_BYTE,74* MLIB_SHORT, MLIB_USHORT or MLIB_INT data type.75* m >= 1, n >= 1,76* 0 <= dm < m, 0 <= dn < n.77* For data type MLIB_BYTE: 16 <= scale <= 31 (to be compatible with VIS version)78* For data type MLIB_SHORT: 17 <= scale <= 32 (to be compatible with VIS version)79* For data type MLIB_USHORT: 17 <= scale <= 32 (to be compatible with VIS version)80* For data type MLIB_INT: scale >= 081*/8283#include "mlib_image.h"84#include "mlib_ImageCheck.h"85#include "mlib_ImageConv.h"86#include "mlib_ImageCreate.h"87#include "mlib_c_ImageConv.h"88#include "mlib_ImageClipping.h"89#include "mlib_ImageConvEdge.h"9091/***************************************************************/92mlib_status mlib_ImageConvMxN(mlib_image *dst,93const mlib_image *src,94const mlib_s32 *kernel,95mlib_s32 m,96mlib_s32 n,97mlib_s32 dm,98mlib_s32 dn,99mlib_s32 scale,100mlib_s32 cmask,101mlib_edge edge)102{103MLIB_IMAGE_CHECK(dst);104105switch (mlib_ImageGetType(dst)) {106case MLIB_BYTE:107108if (scale < 16 || scale > 31)109return MLIB_FAILURE;110break;111case MLIB_SHORT:112case MLIB_USHORT:113114if (scale < 17 || scale > 32)115return MLIB_FAILURE;116break;117case MLIB_INT:118119if (scale < 0)120return MLIB_FAILURE;121break;122default:123return MLIB_FAILURE;124}125126return mlib_ImageConvMxN_f(dst, src, kernel, m, n, dm, dn, scale, cmask, edge);127}128129/***************************************************************/130mlib_status mlib_ImageConvMxN_f(mlib_image *dst,131const mlib_image *src,132const void *kernel,133mlib_s32 m,134mlib_s32 n,135mlib_s32 dm,136mlib_s32 dn,137mlib_s32 scale,138mlib_s32 cmask,139mlib_edge edge)140{141mlib_image dst_i[1], src_i[1], dst_e[1], src_e[1];142mlib_type type;143mlib_s32 nchan, dx_l, dx_r, dy_t, dy_b;144mlib_s32 edg_sizes[8];145mlib_status ret;146147if (m < 1 || n < 1 || dm < 0 || dm > m - 1 || dn < 0 || dn > n - 1)148return MLIB_FAILURE;149150if (kernel == NULL)151return MLIB_NULLPOINTER;152153ret =154mlib_ImageClippingMxN(dst_i, src_i, dst_e, src_e, edg_sizes, dst, src, m, n, dm, dn);155156if (ret != MLIB_SUCCESS)157return ret;158159nchan = mlib_ImageGetChannels(dst);160type = mlib_ImageGetType(dst);161162if (nchan == 1)163cmask = 1;164165if ((cmask & ((1 << nchan) - 1)) == 0)166return MLIB_SUCCESS;167168dx_l = edg_sizes[0];169dx_r = edg_sizes[1];170dy_t = edg_sizes[2];171dy_b = edg_sizes[3];172173if (dx_l + dx_r + dy_t + dy_b == 0)174edge = MLIB_EDGE_DST_NO_WRITE;175176if (edge != MLIB_EDGE_SRC_EXTEND) {177if (mlib_ImageGetWidth(dst_i) >= m && mlib_ImageGetHeight(dst_i) >= n) {178switch (type) {179case MLIB_BYTE:180ret = mlib_convMxNnw_u8(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);181break;182case MLIB_SHORT:183#ifdef __sparc184ret = mlib_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);185#else186187if (mlib_ImageConvVersion(m, n, scale, type) == 0)188ret = mlib_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);189else190ret = mlib_i_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);191#endif /* __sparc */192break;193case MLIB_USHORT:194#ifdef __sparc195ret = mlib_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);196#else197198if (mlib_ImageConvVersion(m, n, scale, type) == 0)199ret = mlib_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);200else201ret = mlib_i_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);202#endif /* __sparc */203break;204case MLIB_INT:205ret = mlib_convMxNnw_s32(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask);206break;207case MLIB_FLOAT:208ret = mlib_convMxNnw_f32(dst_i, src_i, kernel, m, n, dm, dn, cmask);209break;210case MLIB_DOUBLE:211ret = mlib_convMxNnw_d64(dst_i, src_i, kernel, m, n, dm, dn, cmask);212break;213214default:215/* For some reasons, there is no convolution routine for type MLIB_BIT.216* For now, we silently ignore it (because this image type is not used by java),217* but probably we have to report an error.218*/219break;220}221}222223switch (edge) {224case MLIB_EDGE_DST_FILL_ZERO:225mlib_ImageConvZeroEdge(dst_e, dx_l, dx_r, dy_t, dy_b, cmask);226break;227case MLIB_EDGE_DST_COPY_SRC:228mlib_ImageConvCopyEdge(dst_e, src_e, dx_l, dx_r, dy_t, dy_b, cmask);229break;230default:231/* Other edge conditions do not need additional handling.232* Note also that they are not exposed in public Java API233*/234break;235}236}237else { /* MLIB_EDGE_SRC_EXTEND */238/* adjust src_e image */239mlib_ImageSetSubimage(src_e, src_e, dx_l - dm, dy_t - dn,240mlib_ImageGetWidth(src_e), mlib_ImageGetHeight(src_e));241242switch (type) {243case MLIB_BYTE:244ret =245mlib_convMxNext_u8(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,246cmask);247break;248case MLIB_SHORT:249#ifdef __sparc250ret =251mlib_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,252cmask);253#else254255if (mlib_ImageConvVersion(m, n, scale, type) == 0)256ret =257mlib_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,258cmask);259else260ret =261mlib_i_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b,262scale, cmask);263#endif /* __sparc */264break;265case MLIB_USHORT:266#ifdef __sparc267ret =268mlib_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,269cmask);270#else271272if (mlib_ImageConvVersion(m, n, scale, type) == 0)273ret =274mlib_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,275cmask);276else277ret =278mlib_i_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b,279scale, cmask);280#endif /* __sparc */281break;282case MLIB_INT:283ret =284mlib_convMxNext_s32(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale,285cmask);286break;287case MLIB_FLOAT:288mlib_convMxNext_f32(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, cmask);289break;290case MLIB_DOUBLE:291mlib_convMxNext_d64(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, cmask);292break;293default:294/* For some reasons, there is no convolution routine for type MLIB_BIT.295* For now, we silently ignore it (because this image type is not used by java),296* but probably we have to report an error.297*/298break;299}300}301302return ret;303}304305/***************************************************************/306307308