Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/awt/medialib/mlib_ImageConvKernelConvert.c
38918 views
/*1* Copyright (c) 2003, 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_ImageConvKernelConvert - Convert convolution kernel from29* floating point version to integer30* version.31*32* SYNOPSIS33* mlib_status mlib_ImageConvKernelConvert(mlib_s32 *ikernel,34* mlib_s32 *iscale,35* const mlib_d64 *fkernel,36* mlib_s32 m,37* mlib_s32 n,38* mlib_type type);39*40* ARGUMENT41* ikernel integer kernel42* iscale scaling factor of the integer kernel43* fkernel floating-point kernel44* m width of the convolution kernel45* n height of the convolution kernel46* type image type47*48* DESCRIPTION49* Convert a floating point convolution kernel to integer kernel50* with scaling factor. The result integer kernel and scaling factor51* can be used in convolution functions directly without overflow.52*53* RESTRICTION54* The type can be MLIB_BYTE, MLIB_SHORT, MLIB_USHORT or MLIB_INT.55*/5657#include <stdlib.h>58#include "mlib_image.h"59#include "mlib_SysMath.h"60#include "mlib_ImageConv.h"6162/***************************************************************/63#ifdef __sparc6465#define CLAMP_S32(dst, src) \66dst = (mlib_s32)(src)6768#else6970#define CLAMP_S32(dst, src) { \71mlib_d64 s0 = (mlib_d64)(src); \72if (s0 > (mlib_d64)MLIB_S32_MAX) s0 = (mlib_d64)MLIB_S32_MAX; \73if (s0 < (mlib_d64)MLIB_S32_MIN) s0 = (mlib_d64)MLIB_S32_MIN; \74dst = (mlib_s32)s0; \75}7677#endif /* __sparc */7879/***************************************************************/80mlib_status mlib_ImageConvKernelConvert(mlib_s32 *ikernel,81mlib_s32 *iscale,82const mlib_d64 *fkernel,83mlib_s32 m,84mlib_s32 n,85mlib_type type)86{87mlib_d64 sum_pos, sum_neg, sum, norm, max, f;88mlib_s32 isum_pos, isum_neg, isum, test;89mlib_s32 i, scale, scale1, chk_flag;9091if (ikernel == NULL || iscale == NULL || fkernel == NULL || m < 1 || n < 1) {92return MLIB_FAILURE;93}9495if ((type == MLIB_BYTE) || (type == MLIB_SHORT) || (type == MLIB_USHORT)) {9697if (type != MLIB_SHORT) { /* MLIB_BYTE, MLIB_USHORT */98sum_pos = 0;99sum_neg = 0;100101for (i = 0; i < m * n; i++) {102if (fkernel[i] > 0)103sum_pos += fkernel[i];104else105sum_neg -= fkernel[i];106}107108sum = (sum_pos > sum_neg) ? sum_pos : sum_neg;109scale = mlib_ilogb(sum);110scale++;111112scale = 31 - scale;113}114else { /* MLIB_SHORT */115sum = 0;116max = 0;117118for (i = 0; i < m * n; i++) {119f = mlib_fabs(fkernel[i]);120sum += f;121max = (max > f) ? max : f;122}123124scale1 = mlib_ilogb(max) + 1;125scale = mlib_ilogb(sum);126scale = (scale > scale1) ? scale : scale1;127scale++;128129scale = 32 - scale;130}131132if (scale <= 16)133return MLIB_FAILURE;134if (scale > 31)135scale = 31;136137*iscale = scale;138139chk_flag = mlib_ImageConvVersion(m, n, scale, type);140141if (!chk_flag) {142norm = (1u << scale);143for (i = 0; i < m * n; i++) {144CLAMP_S32(ikernel[i], fkernel[i] * norm);145}146147return MLIB_SUCCESS;148}149150/* try to round coefficients */151#ifdef __sparc152scale1 = 16; /* shift of coefficients is 16 */153#else154155if (chk_flag == 3)156scale1 = 16; /* MMX */157else158scale1 = (type == MLIB_BYTE) ? 8 : 16;159#endif /* __sparc */160norm = (1u << (scale - scale1));161162for (i = 0; i < m * n; i++) {163if (fkernel[i] > 0)164ikernel[i] = (mlib_s32) (fkernel[i] * norm + 0.5);165else166ikernel[i] = (mlib_s32) (fkernel[i] * norm - 0.5);167}168169isum_pos = 0;170isum_neg = 0;171test = 0;172173for (i = 0; i < m * n; i++) {174if (ikernel[i] > 0)175isum_pos += ikernel[i];176else177isum_neg -= ikernel[i];178}179180if (type == MLIB_BYTE || type == MLIB_USHORT) {181isum = (isum_pos > isum_neg) ? isum_pos : isum_neg;182183if (isum >= (1 << (31 - scale1)))184test = 1;185}186else {187isum = isum_pos + isum_neg;188189if (isum >= (1 << (32 - scale1)))190test = 1;191for (i = 0; i < m * n; i++) {192if (abs(ikernel[i]) >= (1 << (31 - scale1)))193test = 1;194}195}196197if (test == 1) { /* rounding according scale1 cause overflow, truncate instead of round */198for (i = 0; i < m * n; i++)199ikernel[i] = (mlib_s32) (fkernel[i] * norm) << scale1;200}201else { /* rounding is Ok */202for (i = 0; i < m * n; i++)203ikernel[i] = ikernel[i] << scale1;204}205206return MLIB_SUCCESS;207}208else if ((type == MLIB_INT) || (type == MLIB_BIT)) {209max = 0;210211for (i = 0; i < m * n; i++) {212f = mlib_fabs(fkernel[i]);213max = (max > f) ? max : f;214}215216scale = mlib_ilogb(max);217218if (scale > 29)219return MLIB_FAILURE;220221if (scale < -100)222scale = -100;223224*iscale = 29 - scale;225scale = 29 - scale;226227norm = 1.0;228while (scale > 30) {229norm *= (1 << 30);230scale -= 30;231}232233norm *= (1 << scale);234235for (i = 0; i < m * n; i++) {236if (fkernel[i] > 0) {237CLAMP_S32(ikernel[i], fkernel[i] * norm + 0.5);238}239else {240CLAMP_S32(ikernel[i], fkernel[i] * norm - 0.5);241}242}243244return MLIB_SUCCESS;245}246else {247return MLIB_FAILURE;248}249}250251/***************************************************************/252253254