1 /* 2 * Copyright (c) 2003, 2018, 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 #ifdef __sparc 186 ret = mlib_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 187 #else 188 189 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 190 ret = mlib_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 191 else 192 ret = mlib_i_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 193 #endif /* __sparc */ 194 break; 195 case MLIB_USHORT: 196 #ifdef __sparc 197 ret = mlib_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 198 #else 199 200 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 201 ret = mlib_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 202 else 203 ret = mlib_i_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 204 #endif /* __sparc */ 205 break; 206 case MLIB_INT: 207 ret = mlib_convMxNnw_s32(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 208 break; 209 case MLIB_FLOAT: 210 ret = mlib_convMxNnw_f32(dst_i, src_i, kernel, m, n, dm, dn, cmask); 211 break; 212 case MLIB_DOUBLE: 213 ret = mlib_convMxNnw_d64(dst_i, src_i, kernel, m, n, dm, dn, cmask); 214 break; 215 216 default: 217 /* For some reasons, there is no convolution routine for type MLIB_BIT. 218 * For now, we silently ignore it (because this image type is not used by java), 219 * but probably we have to report an error. 220 */ 221 break; 222 } 223 } 224 225 switch (edge) { 226 case MLIB_EDGE_DST_FILL_ZERO: 227 mlib_ImageConvZeroEdge(dst_e, dx_l, dx_r, dy_t, dy_b, cmask); 228 break; 229 case MLIB_EDGE_DST_COPY_SRC: 230 mlib_ImageConvCopyEdge(dst_e, src_e, dx_l, dx_r, dy_t, dy_b, cmask); 231 break; 232 default: 233 /* Other edge conditions do not need additional handling. 234 * Note also that they are not exposed in public Java API 235 */ 236 break; 237 } 238 } 239 else { /* MLIB_EDGE_SRC_EXTEND */ 240 /* adjust src_e image */ 241 mlib_ImageSetSubimage(src_e, src_e, dx_l - dm, dy_t - dn, 242 mlib_ImageGetWidth(src_e), mlib_ImageGetHeight(src_e)); 243 244 switch (type) { 245 case MLIB_BYTE: 246 ret = 247 mlib_convMxNext_u8(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 248 cmask); 249 break; 250 case MLIB_SHORT: 251 #ifdef __sparc 252 ret = 253 mlib_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 254 cmask); 255 #else 256 257 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 258 ret = 259 mlib_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 260 cmask); 261 else 262 ret = 263 mlib_i_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, 264 scale, cmask); 265 #endif /* __sparc */ 266 break; 267 case MLIB_USHORT: 268 #ifdef __sparc 269 ret = 270 mlib_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 271 cmask); 272 #else 273 274 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 275 ret = 276 mlib_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 277 cmask); 278 else 279 ret = 280 mlib_i_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, 281 scale, cmask); 282 #endif /* __sparc */ 283 break; 284 case MLIB_INT: 285 ret = 286 mlib_convMxNext_s32(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 287 cmask); 288 break; 289 case MLIB_FLOAT: 290 mlib_convMxNext_f32(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, cmask); 291 break; 292 case MLIB_DOUBLE: 293 mlib_convMxNext_d64(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, cmask); 294 break; 295 default: 296 /* For some reasons, there is no convolution routine for type MLIB_BIT. 297 * For now, we silently ignore it (because this image type is not used by java), 298 * but probably we have to report an error. 299 */ 300 break; 301 } 302 } 303 304 return ret; 305 } 306 307 /***************************************************************/