1 /* 2 * Copyright (c) 2003, 2011, 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 mlib_status JNICALL mlib_ImageConvMxN(mlib_image *dst, 94 const mlib_image *src, 95 const mlib_s32 *kernel, 96 mlib_s32 m, 97 mlib_s32 n, 98 mlib_s32 dm, 99 mlib_s32 dn, 100 mlib_s32 scale, 101 mlib_s32 cmask, 102 mlib_edge edge) 103 { 104 MLIB_IMAGE_CHECK(dst); 105 106 switch (mlib_ImageGetType(dst)) { 107 case MLIB_BYTE: 108 109 if (scale < 16 || scale > 31) 110 return MLIB_FAILURE; 111 break; 112 case MLIB_SHORT: 113 case MLIB_USHORT: 114 115 if (scale < 17 || scale > 32) 116 return MLIB_FAILURE; 117 break; 118 case MLIB_INT: 119 120 if (scale < 0) 121 return MLIB_FAILURE; 122 break; 123 default: 124 return MLIB_FAILURE; 125 } 126 127 return mlib_ImageConvMxN_f(dst, src, kernel, m, n, dm, dn, scale, cmask, edge); 128 } 129 130 /***************************************************************/ 131 mlib_status mlib_ImageConvMxN_f(mlib_image *dst, 132 const mlib_image *src, 133 const void *kernel, 134 mlib_s32 m, 135 mlib_s32 n, 136 mlib_s32 dm, 137 mlib_s32 dn, 138 mlib_s32 scale, 139 mlib_s32 cmask, 140 mlib_edge edge) 141 { 142 mlib_image dst_i[1], src_i[1], dst_e[1], src_e[1]; 143 mlib_type type; 144 mlib_s32 nchan, dx_l, dx_r, dy_t, dy_b; 145 mlib_s32 edg_sizes[8]; 146 mlib_status ret; 147 148 if (m < 1 || n < 1 || dm < 0 || dm > m - 1 || dn < 0 || dn > n - 1) 149 return MLIB_FAILURE; 150 151 if (kernel == NULL) 152 return MLIB_NULLPOINTER; 153 154 ret = 155 mlib_ImageClippingMxN(dst_i, src_i, dst_e, src_e, edg_sizes, dst, src, m, n, dm, dn); 156 157 if (ret != MLIB_SUCCESS) 158 return ret; 159 160 nchan = mlib_ImageGetChannels(dst); 161 type = mlib_ImageGetType(dst); 162 163 if (nchan == 1) 164 cmask = 1; 165 166 if ((cmask & ((1 << nchan) - 1)) == 0) 167 return MLIB_SUCCESS; 168 169 dx_l = edg_sizes[0]; 170 dx_r = edg_sizes[1]; 171 dy_t = edg_sizes[2]; 172 dy_b = edg_sizes[3]; 173 174 if (dx_l + dx_r + dy_t + dy_b == 0) 175 edge = MLIB_EDGE_DST_NO_WRITE; 176 177 if (edge != MLIB_EDGE_SRC_EXTEND) { 178 if (mlib_ImageGetWidth(dst_i) >= m && mlib_ImageGetHeight(dst_i) >= n) { 179 switch (type) { 180 case MLIB_BYTE: 181 ret = mlib_convMxNnw_u8(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 182 break; 183 case MLIB_SHORT: 184 #ifdef __sparc 185 ret = mlib_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 186 #else 187 188 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 189 ret = mlib_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 190 else 191 ret = mlib_i_convMxNnw_s16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 192 #endif /* __sparc */ 193 break; 194 case MLIB_USHORT: 195 #ifdef __sparc 196 ret = mlib_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 197 #else 198 199 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 200 ret = mlib_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 201 else 202 ret = mlib_i_convMxNnw_u16(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 203 #endif /* __sparc */ 204 break; 205 case MLIB_INT: 206 ret = mlib_convMxNnw_s32(dst_i, src_i, kernel, m, n, dm, dn, scale, cmask); 207 break; 208 case MLIB_FLOAT: 209 ret = mlib_convMxNnw_f32(dst_i, src_i, kernel, m, n, dm, dn, cmask); 210 break; 211 case MLIB_DOUBLE: 212 ret = mlib_convMxNnw_d64(dst_i, src_i, kernel, m, n, dm, dn, cmask); 213 break; 214 215 default: 216 /* For some reasons, there is no convolution routine for type MLIB_BIT. 217 * For now, we silently ignore it (because this image type is not used by java), 218 * but probably we have to report an error. 219 */ 220 break; 221 } 222 } 223 224 switch (edge) { 225 case MLIB_EDGE_DST_FILL_ZERO: 226 mlib_ImageConvZeroEdge(dst_e, dx_l, dx_r, dy_t, dy_b, cmask); 227 break; 228 case MLIB_EDGE_DST_COPY_SRC: 229 mlib_ImageConvCopyEdge(dst_e, src_e, dx_l, dx_r, dy_t, dy_b, cmask); 230 break; 231 default: 232 /* Other edge conditions do not need additional handling. 233 * Note also that they are not exposed in public Java API 234 */ 235 break; 236 } 237 } 238 else { /* MLIB_EDGE_SRC_EXTEND */ 239 /* adjust src_e image */ 240 mlib_ImageSetSubimage(src_e, src_e, dx_l - dm, dy_t - dn, 241 mlib_ImageGetWidth(src_e), mlib_ImageGetHeight(src_e)); 242 243 switch (type) { 244 case MLIB_BYTE: 245 ret = 246 mlib_convMxNext_u8(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 247 cmask); 248 break; 249 case MLIB_SHORT: 250 #ifdef __sparc 251 ret = 252 mlib_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 253 cmask); 254 #else 255 256 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 257 ret = 258 mlib_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 259 cmask); 260 else 261 ret = 262 mlib_i_convMxNext_s16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, 263 scale, cmask); 264 #endif /* __sparc */ 265 break; 266 case MLIB_USHORT: 267 #ifdef __sparc 268 ret = 269 mlib_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 270 cmask); 271 #else 272 273 if (mlib_ImageConvVersion(m, n, scale, type) == 0) 274 ret = 275 mlib_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 276 cmask); 277 else 278 ret = 279 mlib_i_convMxNext_u16(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, 280 scale, cmask); 281 #endif /* __sparc */ 282 break; 283 case MLIB_INT: 284 ret = 285 mlib_convMxNext_s32(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, scale, 286 cmask); 287 break; 288 case MLIB_FLOAT: 289 mlib_convMxNext_f32(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, cmask); 290 break; 291 case MLIB_DOUBLE: 292 mlib_convMxNext_d64(dst_e, src_e, kernel, m, n, dx_l, dx_r, dy_t, dy_b, cmask); 293 break; 294 default: 295 /* For some reasons, there is no convolution routine for type MLIB_BIT. 296 * For now, we silently ignore it (because this image type is not used by java), 297 * but probably we have to report an error. 298 */ 299 break; 300 } 301 } 302 303 return ret; 304 } 305 306 /***************************************************************/