Path: blob/master/src/java.desktop/share/native/libmlib_image/mlib_ImageClipping.c
41149 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_ImageClipping29* mlib_ImageClippingMxN30* Clipping for image processing in case of pixel-to-pixel31* square kernel filtering. Source and destination images can have32* different sizes, center of the source is mapped to the center of33* the destination image.34* Examples of this type of image processing are Convolve, Gradient,35* Dilate/Erode functions, etc.36*37* SYNOPSIS38* mlib_status mlib_ImageClipping(mlib_image *dst_i,39* mlib_image *src_i,40* mlib_image *dst_e,41* mlib_image *src_e,42* mlib_s32 *edg_sizes,43* const mlib_image *dst,44* const mlib_image *src,45* mlib_s32 ker_size)46*47* mlib_status mlib_ImageClippingMxN(mlib_image *dst_i,48* mlib_image *src_i,49* mlib_image *dst_e,50* mlib_image *src_e,51* mlib_s32 *edg_sizes,52* const mlib_image *dst,53* const mlib_image *src,54* mlib_s32 kw,55* mlib_s32 kh,56* mlib_s32 kw1,57* mlib_s32 kh1)58*59* OUTPUT ARGUMENTS60* dst_i Pointer to destination image of internal pixels61* src_i Pointer to source image of internal pixels62* dst_e Pointer to destination image for edge processing63* src_e Pointer to source image for edge processing64* edg_sizes Array of edge sizes65*66* INPUT ARGUMENTS67* dst Pointer to destination image.68* src Pointer to source image.69* ksize Size of kernel70*71* RESTRICTION72* The src and the dst must be images of the same type.73* The src and dst must have same number of channels.74*75*/7677#include "mlib_image.h"78#include "mlib_ImageCheck.h"79#include "mlib_ImageClipping.h"80#include "mlib_ImageCreate.h"8182/***************************************************************/83mlib_status mlib_ImageClippingMxN(mlib_image *dst_i,84mlib_image *src_i,85mlib_image *dst_e,86mlib_image *src_e,87mlib_s32 *edg_sizes,88const mlib_image *dst,89const mlib_image *src,90mlib_s32 kw,91mlib_s32 kh,92mlib_s32 kw1,93mlib_s32 kh1)94{95mlib_s32 kw2 = kw - 1 - kw1;96mlib_s32 kh2 = kh - 1 - kh1;97mlib_s32 src_wid, src_hgt, dst_wid, dst_hgt;98mlib_s32 dx, dy, dxd, dxs, dyd, dys, wid_e, hgt_e;99mlib_s32 dx_l, dx_r, dy_t, dy_b, wid_i, hgt_i;100101MLIB_IMAGE_CHECK(dst);102MLIB_IMAGE_CHECK(src);103MLIB_IMAGE_TYPE_EQUAL(dst, src);104MLIB_IMAGE_CHAN_EQUAL(dst, src);105106dst_wid = mlib_ImageGetWidth(dst);107dst_hgt = mlib_ImageGetHeight(dst);108src_wid = mlib_ImageGetWidth(src);109src_hgt = mlib_ImageGetHeight(src);110111/* X clipping */112dx = src_wid - dst_wid;113114if (dx > 0) {115dxs = (dx + 1) >> 1;116dxd = 0;117} else {118dxs = 0;119dxd = (-dx) >> 1;120}121122dx_l = kw1 - dxs;123dx_r = kw2 + dxs - dx;124125if (dx_l < 0) dx_l = 0;126if (dx_r < 0) dx_r = 0;127if (dx_r > kw2) dx_r = kw2;128129/* Y clipping */130dy = src_hgt - dst_hgt;131132if (dy > 0) {133dys = (dy + 1) >> 1;134dyd = 0;135} else {136dys = 0;137dyd = (-dy) >> 1;138}139140dy_t = kh1 - dys;141dy_b = kh2 + dys - dy;142143if (dy_t < 0) dy_t = 0;144if (dy_b < 0) dy_b = 0;145if (dy_b > kh2) dy_b = kh2;146147/* image sizes */148wid_e = (src_wid < dst_wid) ? src_wid : dst_wid;149hgt_e = (src_hgt < dst_hgt) ? src_hgt : dst_hgt;150wid_i = wid_e + (kw1 - dx_l) + (kw2 - dx_r);151hgt_i = hgt_e + (kh1 - dy_t) + (kh2 - dy_b);152153mlib_ImageSetSubimage(dst_i, dst, dxd - (kw1 - dx_l), dyd - (kh1 - dy_t), wid_i, hgt_i);154mlib_ImageSetSubimage(src_i, src, dxs - (kw1 - dx_l), dys - (kh1 - dy_t), wid_i, hgt_i);155156if (dst_e != NULL && src_e != NULL) { /* images for edge processing */157mlib_ImageSetSubimage(dst_e, dst, dxd, dyd, wid_e, hgt_e);158mlib_ImageSetSubimage(src_e, src, dxs, dys, wid_e, hgt_e);159}160161if (edg_sizes != NULL) { /* save edges */162edg_sizes[0] = dx_l;163edg_sizes[1] = dx_r;164edg_sizes[2] = dy_t;165edg_sizes[3] = dy_b;166}167168return MLIB_SUCCESS;169}170171/***************************************************************/172mlib_status mlib_ImageClipping(mlib_image *dst_i,173mlib_image *src_i,174mlib_image *dst_e,175mlib_image *src_e,176mlib_s32 *edg_sizes,177const mlib_image *dst,178const mlib_image *src,179mlib_s32 ker_size)180{181mlib_s32 kw1 = (ker_size - 1)/2;182return mlib_ImageClippingMxN(dst_i, src_i, dst_e, src_e, edg_sizes,183dst, src, ker_size, ker_size, kw1, kw1);184}185186/***************************************************************/187188189