Path: blob/master/src/java.desktop/share/native/libmlib_image/mlib_ImageConvKernelConvert.c
41149 views
/*1* Copyright (c) 2003, 2020, 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/***************************************************************/6364#define CLAMP_S32(dst, src) { \65mlib_d64 s0 = (mlib_d64)(src); \66if (s0 > (mlib_d64)MLIB_S32_MAX) s0 = (mlib_d64)MLIB_S32_MAX; \67if (s0 < (mlib_d64)MLIB_S32_MIN) s0 = (mlib_d64)MLIB_S32_MIN; \68dst = (mlib_s32)s0; \69}7071/***************************************************************/72JNIEXPORT73mlib_status mlib_ImageConvKernelConvert(mlib_s32 *ikernel,74mlib_s32 *iscale,75const mlib_d64 *fkernel,76mlib_s32 m,77mlib_s32 n,78mlib_type type)79{80mlib_d64 sum_pos, sum_neg, sum, norm, max, f;81mlib_s32 isum_pos, isum_neg, isum, test;82mlib_s32 i, scale, scale1, chk_flag;8384if (ikernel == NULL || iscale == NULL || fkernel == NULL || m < 1 || n < 1) {85return MLIB_FAILURE;86}8788if ((type == MLIB_BYTE) || (type == MLIB_SHORT) || (type == MLIB_USHORT)) {8990if (type != MLIB_SHORT) { /* MLIB_BYTE, MLIB_USHORT */91sum_pos = 0;92sum_neg = 0;9394for (i = 0; i < m * n; i++) {95if (fkernel[i] > 0)96sum_pos += fkernel[i];97else98sum_neg -= fkernel[i];99}100101sum = (sum_pos > sum_neg) ? sum_pos : sum_neg;102scale = mlib_ilogb(sum);103scale++;104105scale = 31 - scale;106}107else { /* MLIB_SHORT */108sum = 0;109max = 0;110111for (i = 0; i < m * n; i++) {112f = mlib_fabs(fkernel[i]);113sum += f;114max = (max > f) ? max : f;115}116117scale1 = mlib_ilogb(max) + 1;118scale = mlib_ilogb(sum);119scale = (scale > scale1) ? scale : scale1;120scale++;121122scale = 32 - scale;123}124125if (scale <= 16)126return MLIB_FAILURE;127if (scale > 31)128scale = 31;129130*iscale = scale;131132chk_flag = mlib_ImageConvVersion(m, n, scale, type);133134if (!chk_flag) {135norm = (1u << scale);136for (i = 0; i < m * n; i++) {137CLAMP_S32(ikernel[i], fkernel[i] * norm);138}139140return MLIB_SUCCESS;141}142143/* try to round coefficients */144if (chk_flag == 3)145scale1 = 16; /* MMX */146else147scale1 = (type == MLIB_BYTE) ? 8 : 16;148norm = (1u << (scale - scale1));149150for (i = 0; i < m * n; i++) {151if (fkernel[i] > 0)152ikernel[i] = (mlib_s32) (fkernel[i] * norm + 0.5);153else154ikernel[i] = (mlib_s32) (fkernel[i] * norm - 0.5);155}156157isum_pos = 0;158isum_neg = 0;159test = 0;160161for (i = 0; i < m * n; i++) {162if (ikernel[i] > 0)163isum_pos += ikernel[i];164else165isum_neg -= ikernel[i];166}167168if (type == MLIB_BYTE || type == MLIB_USHORT) {169isum = (isum_pos > isum_neg) ? isum_pos : isum_neg;170171if (isum >= (1 << (31 - scale1)))172test = 1;173}174else {175isum = isum_pos + isum_neg;176177if (isum >= (1 << (32 - scale1)))178test = 1;179for (i = 0; i < m * n; i++) {180if (abs(ikernel[i]) >= (1 << (31 - scale1)))181test = 1;182}183}184185if (test == 1) { /* rounding according scale1 cause overflow, truncate instead of round */186for (i = 0; i < m * n; i++)187ikernel[i] = (mlib_s32) (fkernel[i] * norm) << scale1;188}189else { /* rounding is Ok */190for (i = 0; i < m * n; i++)191ikernel[i] = ikernel[i] << scale1;192}193194return MLIB_SUCCESS;195}196else if ((type == MLIB_INT) || (type == MLIB_BIT)) {197max = 0;198199for (i = 0; i < m * n; i++) {200f = mlib_fabs(fkernel[i]);201max = (max > f) ? max : f;202}203204scale = mlib_ilogb(max);205206if (scale > 29)207return MLIB_FAILURE;208209if (scale < -100)210scale = -100;211212*iscale = 29 - scale;213scale = 29 - scale;214215norm = 1.0;216while (scale > 30) {217norm *= (1 << 30);218scale -= 30;219}220221norm *= (1 << scale);222223for (i = 0; i < m * n; i++) {224if (fkernel[i] > 0) {225CLAMP_S32(ikernel[i], fkernel[i] * norm + 0.5);226}227else {228CLAMP_S32(ikernel[i], fkernel[i] * norm - 0.5);229}230}231232return MLIB_SUCCESS;233}234else {235return MLIB_FAILURE;236}237}238239/***************************************************************/240241242