1 | /* |
2 | * Copyright (c) 2003, 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 | * FUNCTIONS |
29 | * mlib_ImageConvCopyEdge_Bit - Copy src edges to dst edges |
30 | * |
31 | * |
32 | * SYNOPSIS |
33 | * mlib_status mlib_ImageConvCopyEdge_Bit(mlib_image *dst, |
34 | * const mlib_image *src, |
35 | * mlib_s32 dx_l, |
36 | * mlib_32 dx_r, |
37 | * mlib_s32 dy_t, |
38 | * mlib_32 dy_b, |
39 | * mlib_s32 cmask); |
40 | * |
41 | * ARGUMENT |
42 | * dst Pointer to an dst image. |
43 | * src Pointer to an src image. |
44 | * dx_l Number of columns on the left side of the |
45 | * image to be copyed. |
46 | * dx_r Number of columns on the right side of the |
47 | * image to be copyed. |
48 | * dy_t Number of rows on the top edge of the |
49 | * image to be copyed. |
50 | * dy_b Number of rows on the top edge of the |
51 | * image to be copyed. |
52 | * cmask Channel mask to indicate the channels to be convolved. |
53 | * Each bit of which represents a channel in the image. The |
54 | * channels corresponded to 1 bits are those to be processed. |
55 | * |
56 | * RESTRICTION |
57 | * The src and the dst must be the MLIB_BIT type, same width, same height and have same number |
58 | * of channels (1). The unselected channels are not |
59 | * overwritten. If both src and dst have just one channel, |
60 | * cmask is ignored. |
61 | * |
62 | * DESCRIPTION |
63 | * Copy src edges to dst edges. |
64 | * |
65 | * The unselected channels are not overwritten. |
66 | * If src and dst have just one channel, |
67 | * cmask is ignored. |
68 | */ |
69 | |
70 | #include "mlib_image.h" |
71 | #include "mlib_ImageConvEdge.h" |
72 | |
73 | /***************************************************************/ |
74 | mlib_status mlib_ImageConvCopyEdge_Bit(mlib_image *dst, |
75 | const mlib_image *src, |
76 | mlib_s32 dx_l, |
77 | mlib_s32 dx_r, |
78 | mlib_s32 dy_t, |
79 | mlib_s32 dy_b, |
80 | mlib_s32 cmask) |
81 | { |
82 | mlib_u8 *pdst = mlib_ImageGetData(dst), *pd; |
83 | mlib_u8 *psrc = mlib_ImageGetData(src), *ps; |
84 | mlib_s32 img_height = mlib_ImageGetHeight(dst); |
85 | mlib_s32 img_width = mlib_ImageGetWidth(dst); |
86 | mlib_s32 img_strided = mlib_ImageGetStride(dst); |
87 | mlib_s32 img_strides = mlib_ImageGetStride(src); |
88 | mlib_s32 bitoffd = mlib_ImageGetBitOffset(dst); |
89 | mlib_s32 bitoffs = mlib_ImageGetBitOffset(src); |
90 | mlib_s32 bitoff_end, test, shift1, shift2; |
91 | mlib_u32 s0, s1, tmp; |
92 | mlib_u8 mask, mask_end; |
93 | mlib_u8 tmp_start, tmp_end; |
94 | mlib_s32 i, j, amount; |
95 | |
96 | if (bitoffd == bitoffs) { |
97 | pd = pdst; |
98 | ps = psrc; |
99 | |
100 | if (dx_l > 0) { |
101 | if (bitoffd + dx_l <= 8) { |
102 | mask = (0xFF >> bitoffd) & (0xFF << ((8 - (bitoffd + dx_l)) & 7)); |
103 | |
104 | for (i = dy_t; i < (img_height - dy_b); i++) { |
105 | pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (ps[i*img_strides] & mask); |
106 | } |
107 | |
108 | } else { |
109 | mask = (0xFF >> bitoffd); |
110 | |
111 | for (i = dy_t; i < (img_height - dy_b); i++) { |
112 | pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (ps[i*img_strides] & mask); |
113 | } |
114 | |
115 | amount = (bitoffd + dx_l + 7) >> 3; |
116 | mask = (0xFF << ((8 - (bitoffd + dx_l)) & 7)); |
117 | |
118 | for (j = 1; j < amount - 1; j++) { |
119 | for (i = dy_t; i < (img_height - dy_b); i++) { |
120 | pd[i*img_strided + j] = ps[i*img_strides + j]; |
121 | } |
122 | } |
123 | |
124 | for (i = dy_t; i < (img_height - dy_b); i++) { |
125 | pd[i*img_strided + amount - 1] = (pd[i*img_strided + amount - 1] & ~mask) | |
126 | (ps[i*img_strides + amount - 1] & mask); |
127 | } |
128 | } |
129 | } |
130 | |
131 | if (dx_r > 0) { |
132 | pd = pdst + (img_width + bitoffd - dx_r) / 8; |
133 | ps = psrc + (img_width + bitoffd - dx_r) / 8; |
134 | bitoffd = (img_width + bitoffd - dx_r) & 7; |
135 | |
136 | if (bitoffd + dx_r <= 8) { |
137 | mask = (0xFF >> bitoffd) & (0xFF << ((8 - (bitoffd + dx_r)) & 7)); |
138 | |
139 | for (i = dy_t; i < (img_height - dy_b); i++) { |
140 | pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (ps[i*img_strides] & mask); |
141 | } |
142 | |
143 | } else { |
144 | mask = (0xFF >> bitoffd); |
145 | |
146 | for (i = dy_t; i < (img_height - dy_b); i++) { |
147 | pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (ps[i*img_strides] & mask); |
148 | } |
149 | |
150 | amount = (bitoffd + dx_r + 7) >> 3; |
151 | mask = (0xFF << ((8 - (bitoffd + dx_r)) & 7)); |
152 | |
153 | for (j = 1; j < amount - 1; j++) { |
154 | for (i = dy_t; i < (img_height - dy_b); i++) { |
155 | pd[i*img_strided + j] = ps[i*img_strides + j]; |
156 | } |
157 | } |
158 | |
159 | for (i = dy_t; i < (img_height - dy_b); i++) { |
160 | pd[i*img_strided + amount - 1] = (pd[i*img_strided + amount - 1] & ~mask) | |
161 | (ps[i*img_strides + amount - 1] & mask); |
162 | } |
163 | } |
164 | } |
165 | |
166 | bitoffd = mlib_ImageGetBitOffset(dst); |
167 | bitoff_end = (bitoffd + img_width) & 7; |
168 | amount = (bitoffd + img_width + 7) >> 3; |
169 | mask = (0xFF >> bitoffd); |
170 | mask_end = (0xFF << ((8 - bitoff_end) & 7)); |
171 | |
172 | pd = pdst; |
173 | ps = psrc; |
174 | |
175 | for (i = 0; i < dy_t; i++) { |
176 | tmp_start = pd[i*img_strided]; |
177 | tmp_end = pd[i*img_strided+amount-1]; |
178 | for (j = 0; j < amount; j++) { |
179 | pd[i*img_strided + j] = ps[i*img_strides + j]; |
180 | } |
181 | |
182 | pd[i*img_strided] = (tmp_start & (~mask)) | (pd[i*img_strided] & mask); |
183 | pd[i*img_strided+amount-1] = (tmp_end & (~mask_end)) | |
184 | (pd[i*img_strided+amount-1] & mask_end); |
185 | } |
186 | |
187 | pd = pdst + (img_height-1)*img_strided; |
188 | ps = psrc + (img_height-1)*img_strides; |
189 | |
190 | for (i = 0; i < dy_b; i++) { |
191 | tmp_start = pd[-i*img_strided]; |
192 | tmp_end = pd[-i*img_strided+amount-1]; |
193 | for (j = 0; j < amount; j++) { |
194 | pd[-i*img_strided + j] = ps[-i*img_strides + j]; |
195 | } |
196 | |
197 | pd[-i*img_strided] = (tmp_start & (~mask)) | (pd[-i*img_strided] & mask); |
198 | pd[-i*img_strided+amount-1] = (tmp_end & (~mask_end)) | |
199 | (pd[-i*img_strided+amount-1] & mask_end); |
200 | } |
201 | |
202 | } else { |
203 | pd = pdst; |
204 | |
205 | if (bitoffs > bitoffd) { |
206 | ps = psrc; |
207 | shift2 = (8 - (bitoffs - bitoffd)); |
208 | test = 0; |
209 | } else { |
210 | test = 1; |
211 | ps = psrc - 1; |
212 | shift2 = bitoffd - bitoffs; |
213 | } |
214 | |
215 | shift1 = 8 - shift2; |
216 | |
217 | if (dx_l > 0) { |
218 | if (bitoffd + dx_l <= 8) { |
219 | mask = (0xFF >> bitoffd) & (0xFF << ((8 - (bitoffd + dx_l)) & 7)); |
220 | |
221 | for (i = dy_t; i < (img_height - dy_b); i++) { |
222 | s0 = ps[i*img_strides]; |
223 | s1 = ps[i*img_strides+1]; |
224 | tmp = (s0 << shift1) | (s1 >> shift2); |
225 | pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (tmp & mask); |
226 | } |
227 | |
228 | } else { |
229 | mask = (0xFF >> bitoffd); |
230 | |
231 | for (i = dy_t; i < (img_height - dy_b); i++) { |
232 | s0 = ps[i*img_strides]; |
233 | s1 = ps[i*img_strides+1]; |
234 | tmp = (s0 << shift1) | (s1 >> shift2); |
235 | pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (tmp & mask); |
236 | } |
237 | |
238 | amount = (bitoffd + dx_l + 7) >> 3; |
239 | mask = (0xFF << ((8 - (bitoffd + dx_l)) & 7)); |
240 | |
241 | for (j = 1; j < amount - 1; j++) { |
242 | for (i = dy_t; i < (img_height - dy_b); i++) { |
243 | s0 = ps[i*img_strides+j]; |
244 | s1 = ps[i*img_strides+j+1]; |
245 | pd[i*img_strided + j] = (s0 << shift1) | (s1 >> shift2); |
246 | s0 = s1; |
247 | } |
248 | } |
249 | |
250 | for (i = dy_t; i < (img_height - dy_b); i++) { |
251 | s0 = ps[i*img_strides+amount-1]; |
252 | s1 = ps[i*img_strides+amount]; |
253 | tmp = (s0 << shift1) | (s1 >> shift2); |
254 | pd[i*img_strided + amount - 1] = (pd[i*img_strided + amount - 1] & ~mask) | |
255 | (tmp & mask); |
256 | } |
257 | } |
258 | } |
259 | |
260 | if (dx_r > 0) { |
261 | pd = pdst + (img_width + bitoffd - dx_r) / 8; |
262 | ps = psrc + (img_width + bitoffd - dx_r) / 8; |
263 | bitoffd = (img_width + bitoffd - dx_r) & 7; |
264 | ps -= test; |
265 | |
266 | if (bitoffd + dx_r <= 8) { |
267 | mask = (0xFF >> bitoffd) & (0xFF << ((8 - (bitoffd + dx_r)) & 7)); |
268 | |
269 | for (i = dy_t; i < (img_height - dy_b); i++) { |
270 | s0 = ps[i*img_strides]; |
271 | s1 = ps[i*img_strides+1]; |
272 | tmp = (s0 << shift1) | (s1 >> shift2); |
273 | pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (tmp & mask); |
274 | } |
275 | |
276 | } else { |
277 | mask = (0xFF >> bitoffd); |
278 | |
279 | for (i = dy_t; i < (img_height - dy_b); i++) { |
280 | s0 = ps[i*img_strides]; |
281 | s1 = ps[i*img_strides+1]; |
282 | tmp = (s0 << shift1) | (s1 >> shift2); |
283 | pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (tmp & mask); |
284 | } |
285 | |
286 | amount = (bitoffd + dx_r + 7) >> 3; |
287 | mask = (0xFF << ((8 - (bitoffd + dx_r)) & 7)); |
288 | |
289 | for (j = 1; j < amount - 1; j++) { |
290 | for (i = dy_t; i < (img_height - dy_b); i++) { |
291 | s0 = ps[i*img_strides+j]; |
292 | s1 = ps[i*img_strides+j+1]; |
293 | pd[i*img_strided + j] = (s0 << shift1) | (s1 >> shift2); |
294 | } |
295 | } |
296 | |
297 | for (i = dy_t; i < (img_height - dy_b); i++) { |
298 | s0 = ps[i*img_strides+amount-1]; |
299 | s1 = ps[i*img_strides+amount]; |
300 | tmp = (s0 << shift1) | (s1 >> shift2); |
301 | pd[i*img_strided + amount - 1] = (pd[i*img_strided + amount - 1] & ~mask) | |
302 | (tmp & mask); |
303 | } |
304 | } |
305 | } |
306 | |
307 | bitoffd = mlib_ImageGetBitOffset(dst); |
308 | bitoff_end = (bitoffd + img_width) & 7; |
309 | amount = (bitoffd + img_width + 7) >> 3; |
310 | mask = (0xFF >> bitoffd); |
311 | mask_end = (0xFF << ((8 - bitoff_end) & 7)); |
312 | |
313 | pd = pdst; |
314 | ps = psrc-test; |
315 | |
316 | for (i = 0; i < dy_t; i++) { |
317 | tmp_start = pd[i*img_strided]; |
318 | tmp_end = pd[i*img_strided+amount-1]; |
319 | s0 = ps[i*img_strides]; |
320 | for (j = 0; j < amount; j++) { |
321 | s1 = ps[i*img_strides+j+1]; |
322 | pd[i*img_strided + j] = (s0 << shift1) | (s1 >> shift2); |
323 | s0 = s1; |
324 | } |
325 | |
326 | pd[i*img_strided] = (tmp_start & (~mask)) | (pd[i*img_strided] & mask); |
327 | pd[i*img_strided+amount-1] = (tmp_end & (~mask_end)) | |
328 | (pd[i*img_strided+amount-1] & mask_end); |
329 | } |
330 | |
331 | pd = pdst + (img_height-1)*img_strided; |
332 | ps = psrc + (img_height-1)*img_strides - test; |
333 | |
334 | for (i = 0; i < dy_b; i++) { |
335 | tmp_start = pd[-i*img_strided]; |
336 | tmp_end = pd[-i*img_strided+amount-1]; |
337 | s0 = ps[-i*img_strides]; |
338 | for (j = 0; j < amount; j++) { |
339 | s1 = ps[-i*img_strides+j+1]; |
340 | pd[-i*img_strided + j] = (s0 << shift1) | (s1 >> shift2); |
341 | s0 = s1; |
342 | } |
343 | |
344 | pd[-i*img_strided] = (tmp_start & (~mask)) | (pd[-i*img_strided] & mask); |
345 | pd[-i*img_strided+amount-1] = (tmp_end & (~mask_end)) | |
346 | (pd[-i*img_strided+amount-1] & mask_end); |
347 | } |
348 | } |
349 | |
350 | return MLIB_SUCCESS; |
351 | } |
352 | |
353 | /***************************************************************/ |
354 | |