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