1 /* 2 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 27 /* 28 * FUNCTION 29 * mlib_ImageConvMxN - image convolution with edge condition 30 * 31 * SYNOPSIS 32 * mlib_status mlib_ImageConvMxN(mlib_image *dst, 33 * const mlib_image *src, 34 * const mlib_s32 *kernel, 35 * mlib_s32 m, 36 * mlib_s32 n, 37 * mlib_s32 dm, 38 * mlib_s32 dn, 39 * mlib_s32 scale, 40 * mlib_s32 cmask, 41 * mlib_edge edge) 42 * 43 * ARGUMENTS 44 * dst Pointer to destination image. 45 * src Pointer to source image. 46 * m Kernel width (m must be not less than 1). 47 * n Kernel height (n must be not less than 1). 48 * dm, dn Position of key element in convolution kernel. 49 * kernel Pointer to convolution kernel. 50 * scale The scaling factor to convert the input integer 51 * coefficients into floating-point coefficients: 52 * floating-point coefficient = integer coefficient * 2^(-scale) 53 * cmask Channel mask to indicate the channels to be convolved. 54 * Each bit of which represents a channel in the image. The 55 * channels corresponded to 1 bits are those to be processed. 56 * edge Type of edge condition. 57 * 58 * DESCRIPTION 59 * 2-D convolution, MxN kernel. 60 * 61 * The center of the source image is mapped to the center of the 62 * destination image. 63 * The unselected channels are not overwritten. If both src and dst have 64 * just one channel, cmask is ignored. 65 * 66 * The edge condition can be one of the following: 67 * MLIB_EDGE_DST_NO_WRITE (default) 68 * MLIB_EDGE_DST_FILL_ZERO 69 * MLIB_EDGE_DST_COPY_SRC 70 * MLIB_EDGE_SRC_EXTEND 71 * 72 * RESTRICTION 73 * The src and the dst must be the same type and have same number 74 * of channels (1, 2, 3, or 4). They can be in MLIB_BIT, MLIB_BYTE, 75 * MLIB_SHORT, MLIB_USHORT or MLIB_INT data type. 76 * m >= 1, n >= 1, 77 * 0 <= dm < m, 0 <= dn < n. 78 * For data type MLIB_BYTE: 16 <= scale <= 31 (to be compatible with VIS version) 79 * For data type MLIB_SHORT: 17 <= scale <= 32 (to be compatible with VIS version) 80 * For data type MLIB_USHORT: 17 <= scale <= 32 (to be compatible with VIS version) 81 * For data type MLIB_INT: scale >= 0 82 */ 83 84 #include "mlib_image.h" 85 #include "mlib_ImageCheck.h" 86 #include "mlib_ImageConv.h" 87 #include "mlib_ImageCreate.h" 88 #include "mlib_c_ImageConv.h" 89 #include "mlib_ImageClipping.h" 90 #include "mlib_ImageConvEdge.h" 91 92 /***************************************************************/ 93 JNIEXPORT 94 mlib_status mlib_ImageConvMxN(mlib_image *dst, 95 const mlib_image *src, 96 const mlib_s32 *kernel, 97 mlib_s32 m, 98 mlib_s32 n, 99 mlib_s32 dm, 100 mlib_s32 dn, 101 mlib_s32 scale, 102 mlib_s32 cmask, 103 mlib_edge edge) 104 { 105 MLIB_IMAGE_CHECK(dst); 106 107 switch (mlib_ImageGetType(dst)) { 108 case MLIB_BYTE: 109 110 if (scale < 16 || scale > 31) 111 return MLIB_FAILURE; 112 break; 113 case MLIB_SHORT: 114 case MLIB_USHORT: 115 116 if (scale < 17 || scale > 32) 117 return MLIB_FAILURE; 118 break; 119 case MLIB_INT: 120 121 if (scale < 0) 122 return MLIB_FAILURE; 123 break; 124 default: 125 return MLIB_FAILURE; 126 } 127 128 return mlib_ImageConvMxN_f(dst, src, kernel, m, n, dm, dn, scale, cmask, edge); 129 } 130 131 /***************************************************************/ 132 mlib_status mlib_ImageConvMxN_f(mlib_image *dst, 133 const mlib_image *src, 134 const void *kernel, 135 mlib_s32 m, 136 mlib_s32 n, 137 mlib_s32 dm, 138 mlib_s32 dn, 139 mlib_s32 scale, 140 mlib_s32 cmask, 141 mlib_edge edge) 142 { 143 mlib_image dst_i[1], src_i[1], dst_e[1], src_e[1]; 144 mlib_type type; 145 mlib_s32 nchan, dx_l, dx_r, dy_t, dy_b; 146 mlib_s32 edg_sizes[8]; 147 mlib_status ret; 148 149 if (m < 1 || n < 1 || dm < 0 || dm > m - 1 || dn < 0 || dn > n - 1) 150 return MLIB_FAILURE; 151 152 if (kernel == NULL) 153 return MLIB_NULLPOINTER; 154 155 ret = 156 mlib_ImageClippingMxN(dst_i, src_i, dst_e, src_e, edg_sizes, dst, src, m, n, dm, dn); 157 158 if (ret != MLIB_SUCCESS) 159 return ret; 160 161 nchan = mlib_ImageGetChannels(dst); 162 type = mlib_ImageGetType(dst); 163 164 if (nchan == 1) 165 cmask = 1; 166 167 if ((cmask & ((1 << nchan) - 1)) == 0) 168 return MLIB_SUCCESS; 169 170 dx_l = edg_sizes[0]; 171 dx_r = edg_sizes[1]; 172 dy_t = edg_sizes[2]; 173 dy_b = edg_sizes[3]; 174 175 if (dx_l + dx_r + dy_t + dy_b == 0) 176 edge = MLIB_EDGE_DST_NO_WRITE; 177 178 if (edge != MLIB_EDGE_SRC_EXTEND) { 179 if (mlib_ImageGetWidth(dst_i) >= m && mlib_ImageGetHeight(dst_i) >= n) { 180 switch (type) { 181 case MLIB_BYTE: 182 ret = mlib_convMxNnw_u8(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 183 break; 184 case MLIB_SHORT: 185 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 186 ret = mlib_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 187 else 188 ret = mlib_i_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 189 break; 190 case MLIB_USHORT: 191 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 192 ret = mlib_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 193 else 194 ret = mlib_i_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 195 break; 196 case MLIB_INT: 197 ret = mlib_convMxNnw_s32(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 198 break; 199 case MLIB_FLOAT: 200 ret = mlib_convMxNnw_f32(dst_i, src_i, kernel, m, n, dm, dn, cmask); 201 break; 202 case MLIB_DOUBLE: 203 ret = mlib_convMxNnw_d64(dst_i, src_i, kernel, m, n, dm, dn, cmask); 204 break; 205 206 default: 207 /* For some reasons, there is no convolution routine for type MLIB_BIT. 208 * For now, we silently ignore it (because this image type is not used by java), 209 * but probably we have to report an error. 210 */ 211 break; 212 } 213 } 214 215 switch (edge) { 216 case MLIB_EDGE_DST_FILL_ZERO: 217 mlib_ImageConvZeroEdge(dst_e, dx_l, dx_r, dy_t, dy_b, cmask); 218 break; 219 case MLIB_EDGE_DST_COPY_SRC: 220 mlib_ImageConvCopyEdge(dst_e, src_e, dx_l, dx_r, dy_t, dy_b, cmask); 221 break; 222 default: 223 /* Other edge conditions do not need additional handling. 224 * Note also that they are not exposed in public Java API 225 */ 226 break; 227 } 228 } 229 else { /* MLIB_EDGE_SRC_EXTEND */ 230 /* adjust src_e image */ 231 mlib_ImageSetSubimage(src_e, src_e, dx_l - dm, dy_t - dn, 232 mlib_ImageGetWidth(src_e), mlib_ImageGetHeight(src_e)); 233 234 switch (type) { 235 case MLIB_BYTE: 236 ret = 237 mlib_convMxNext_u8(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 238 cmask); 239 break; 240 case MLIB_SHORT: 241 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 242 ret = 243 mlib_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 244 cmask); 245 else 246 ret = 247 mlib_i_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, 248 scale, cmask); 249 break; 250 case MLIB_USHORT: 251 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 252 ret = 253 mlib_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 254 cmask); 255 else 256 ret = 257 mlib_i_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, 258 scale, cmask); 259 break; 260 case MLIB_INT: 261 ret = 262 mlib_convMxNext_s32(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 263 cmask); 264 break; 265 case MLIB_FLOAT: 266 mlib_convMxNext_f32(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, cmask); 267 break; 268 case MLIB_DOUBLE: 269 mlib_convMxNext_d64(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, cmask); 270 break; 271 default: 272 /* For some reasons, there is no convolution routine for type MLIB_BIT. 273 * For now, we silently ignore it (because this image type is not used by java), 274 * but probably we have to report an error. 275 */ 276 break; 277 } 278 } 279 280 return ret; 281 } 282 283 /***************************************************************/