aboutsummaryrefslogtreecommitdiff
path: root/src/java.desktop/share/native/libmlib_image/mlib_ImageConvCopyEdge_Bit.c
blob: d5ce0f18133a005e398aeddb7e2ef189d81243ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */


/*
 * FUNCTIONS
 *      mlib_ImageConvCopyEdge_Bit  - Copy src edges  to dst edges
 *
 *
 * SYNOPSIS
 *      mlib_status mlib_ImageConvCopyEdge_Bit(mlib_image      *dst,
 *                                            const mlib_image *src,
 *                                            mlib_s32         dx_l,
 *                                            mlib_32          dx_r,
 *                                            mlib_s32         dy_t,
 *                                            mlib_32          dy_b,
 *                                            mlib_s32         cmask);
 *
 * ARGUMENT
 *      dst       Pointer to an dst image.
 *      src       Pointer to an src image.
 *      dx_l      Number of columns on the left side of the
 *                image to be copyed.
 *      dx_r      Number of columns on the right side of the
 *                image to be copyed.
 *      dy_t      Number of rows on the top edge of the
 *                image to be copyed.
 *      dy_b      Number of rows on the top edge of the
 *                image to be copyed.
 *      cmask     Channel mask to indicate the channels to be convolved.
 *                Each bit of which represents a channel in the image. The
 *                channels corresponded to 1 bits are those to be processed.
 *
 * RESTRICTION
 *      The src and the dst must be the MLIB_BIT type, same width, same height and have same number
 *      of channels (1). The unselected channels are not
 *      overwritten. If both src and dst have just one channel,
 *      cmask is ignored.
 *
 * DESCRIPTION
 *      Copy src edges  to dst edges.
 *
 *      The unselected channels are not overwritten.
 *      If src and dst have just one channel,
 *      cmask is ignored.
 */

#include "mlib_image.h"
#include "mlib_ImageConvEdge.h"

/***************************************************************/
mlib_status mlib_ImageConvCopyEdge_Bit(mlib_image       *dst,
                                       const mlib_image *src,
                                       mlib_s32         dx_l,
                                       mlib_s32         dx_r,
                                       mlib_s32         dy_t,
                                       mlib_s32         dy_b,
                                       mlib_s32         cmask)
{
  mlib_u8  *pdst = mlib_ImageGetData(dst), *pd;
  mlib_u8  *psrc = mlib_ImageGetData(src), *ps;
  mlib_s32 img_height = mlib_ImageGetHeight(dst);
  mlib_s32 img_width  = mlib_ImageGetWidth(dst);
  mlib_s32 img_strided = mlib_ImageGetStride(dst);
  mlib_s32 img_strides = mlib_ImageGetStride(src);
  mlib_s32 bitoffd = mlib_ImageGetBitOffset(dst);
  mlib_s32 bitoffs = mlib_ImageGetBitOffset(src);
  mlib_s32 bitoff_end, test, shift1, shift2;
  mlib_u32 s0, s1, tmp;
  mlib_u8  mask, mask_end;
  mlib_u8  tmp_start, tmp_end;
  mlib_s32 i, j, amount;

  if (bitoffd == bitoffs) {
    pd = pdst;
    ps = psrc;

    if (dx_l > 0) {
      if (bitoffd + dx_l <= 8) {
        mask = (0xFF >> bitoffd) & (0xFF << ((8 - (bitoffd + dx_l)) & 7));

        for (i = dy_t; i < (img_height - dy_b); i++) {
          pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (ps[i*img_strides] & mask);
        }

      } else {
        mask = (0xFF >> bitoffd);

        for (i = dy_t; i < (img_height - dy_b); i++) {
          pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (ps[i*img_strides] & mask);
        }

        amount = (bitoffd + dx_l + 7) >> 3;
        mask = (0xFF << ((8 - (bitoffd + dx_l)) & 7));

        for (j = 1; j < amount - 1; j++) {
          for (i = dy_t; i < (img_height - dy_b); i++) {
            pd[i*img_strided + j] = ps[i*img_strides + j];
          }
        }

        for (i = dy_t; i < (img_height - dy_b); i++) {
          pd[i*img_strided + amount - 1] = (pd[i*img_strided + amount - 1] & ~mask) |
                                           (ps[i*img_strides + amount - 1] & mask);
        }
      }
    }

    if (dx_r > 0) {
      pd = pdst + (img_width + bitoffd - dx_r) / 8;
      ps = psrc + (img_width + bitoffd - dx_r) / 8;
      bitoffd = (img_width + bitoffd - dx_r) & 7;

      if (bitoffd + dx_r <= 8) {
        mask = (0xFF >> bitoffd) & (0xFF << ((8 - (bitoffd + dx_r)) & 7));

        for (i = dy_t; i < (img_height - dy_b); i++) {
          pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (ps[i*img_strides] & mask);
        }

      } else {
        mask = (0xFF >> bitoffd);

        for (i = dy_t; i < (img_height - dy_b); i++) {
          pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (ps[i*img_strides] & mask);
        }

        amount = (bitoffd + dx_r + 7) >> 3;
        mask = (0xFF << ((8 - (bitoffd + dx_r)) & 7));

        for (j = 1; j < amount - 1; j++) {
          for (i = dy_t; i < (img_height - dy_b); i++) {
            pd[i*img_strided + j] = ps[i*img_strides + j];
          }
        }

        for (i = dy_t; i < (img_height - dy_b); i++) {
          pd[i*img_strided + amount - 1] = (pd[i*img_strided + amount - 1] & ~mask) |
                                           (ps[i*img_strides + amount - 1] & mask);
        }
      }
    }

    bitoffd = mlib_ImageGetBitOffset(dst);
    bitoff_end = (bitoffd + img_width) & 7;
    amount = (bitoffd + img_width + 7) >> 3;
    mask = (0xFF >> bitoffd);
    mask_end = (0xFF << ((8 - bitoff_end) & 7));

    pd = pdst;
    ps = psrc;

    for (i = 0; i < dy_t; i++) {
      tmp_start = pd[i*img_strided];
      tmp_end = pd[i*img_strided+amount-1];
      for (j = 0; j < amount; j++) {
        pd[i*img_strided + j] = ps[i*img_strides + j];
      }

      pd[i*img_strided] = (tmp_start & (~mask)) | (pd[i*img_strided] & mask);
      pd[i*img_strided+amount-1] = (tmp_end & (~mask_end)) |
                                  (pd[i*img_strided+amount-1] & mask_end);
    }

    pd = pdst + (img_height-1)*img_strided;
    ps = psrc + (img_height-1)*img_strides;

    for (i = 0; i < dy_b; i++) {
      tmp_start = pd[-i*img_strided];
      tmp_end = pd[-i*img_strided+amount-1];
      for (j = 0; j < amount; j++) {
       pd[-i*img_strided + j] = ps[-i*img_strides + j];
      }

      pd[-i*img_strided] = (tmp_start & (~mask)) | (pd[-i*img_strided] & mask);
      pd[-i*img_strided+amount-1] = (tmp_end & (~mask_end)) |
                                   (pd[-i*img_strided+amount-1] & mask_end);
    }

  } else {
    pd = pdst;

    if (bitoffs > bitoffd) {
      ps = psrc;
      shift2 = (8 - (bitoffs - bitoffd));
      test = 0;
    } else {
      test = 1;
      ps = psrc - 1;
      shift2 = bitoffd - bitoffs;
    }

    shift1 = 8 - shift2;

    if (dx_l > 0) {
      if (bitoffd + dx_l <= 8) {
        mask = (0xFF >> bitoffd) & (0xFF << ((8 - (bitoffd + dx_l)) & 7));

        for (i = dy_t; i < (img_height - dy_b); i++) {
          s0 = ps[i*img_strides];
          s1 = ps[i*img_strides+1];
          tmp = (s0 << shift1) | (s1 >> shift2);
          pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (tmp & mask);
        }

      } else {
        mask = (0xFF >> bitoffd);

        for (i = dy_t; i < (img_height - dy_b); i++) {
          s0 = ps[i*img_strides];
          s1 = ps[i*img_strides+1];
          tmp = (s0 << shift1) | (s1 >> shift2);
          pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (tmp & mask);
        }

        amount = (bitoffd + dx_l + 7) >> 3;
        mask = (0xFF << ((8 - (bitoffd + dx_l)) & 7));

        for (j = 1; j < amount - 1; j++) {
          for (i = dy_t; i < (img_height - dy_b); i++) {
            s0 = ps[i*img_strides+j];
            s1 = ps[i*img_strides+j+1];
            pd[i*img_strided + j] = (s0 << shift1) | (s1 >> shift2);
            s0 = s1;
          }
        }

        for (i = dy_t; i < (img_height - dy_b); i++) {
          s0 = ps[i*img_strides+amount-1];
          s1 = ps[i*img_strides+amount];
          tmp = (s0 << shift1) | (s1 >> shift2);
          pd[i*img_strided + amount - 1] = (pd[i*img_strided + amount - 1] & ~mask) |
                                           (tmp & mask);
        }
      }
    }

    if (dx_r > 0) {
      pd = pdst + (img_width + bitoffd - dx_r) / 8;
      ps = psrc + (img_width + bitoffd - dx_r) / 8;
      bitoffd = (img_width + bitoffd - dx_r) & 7;
      ps -= test;

      if (bitoffd + dx_r <= 8) {
        mask = (0xFF >> bitoffd) & (0xFF << ((8 - (bitoffd + dx_r)) & 7));

        for (i = dy_t; i < (img_height - dy_b); i++) {
          s0 = ps[i*img_strides];
          s1 = ps[i*img_strides+1];
          tmp = (s0 << shift1) | (s1 >> shift2);
          pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (tmp & mask);
        }

      } else {
        mask = (0xFF >> bitoffd);

        for (i = dy_t; i < (img_height - dy_b); i++) {
          s0 = ps[i*img_strides];
          s1 = ps[i*img_strides+1];
          tmp = (s0 << shift1) | (s1 >> shift2);
          pd[i*img_strided] = (pd[i*img_strided] & ~mask) | (tmp & mask);
        }

        amount = (bitoffd + dx_r + 7) >> 3;
        mask = (0xFF << ((8 - (bitoffd + dx_r)) & 7));

        for (j = 1; j < amount - 1; j++) {
          for (i = dy_t; i < (img_height - dy_b); i++) {
            s0 = ps[i*img_strides+j];
            s1 = ps[i*img_strides+j+1];
            pd[i*img_strided + j] = (s0 << shift1) | (s1 >> shift2);
          }
        }

        for (i = dy_t; i < (img_height - dy_b); i++) {
          s0 = ps[i*img_strides+amount-1];
          s1 = ps[i*img_strides+amount];
          tmp = (s0 << shift1) | (s1 >> shift2);
          pd[i*img_strided + amount - 1] = (pd[i*img_strided + amount - 1] & ~mask) |
                                           (tmp & mask);
        }
      }
    }

    bitoffd = mlib_ImageGetBitOffset(dst);
    bitoff_end = (bitoffd + img_width) & 7;
    amount = (bitoffd + img_width + 7) >> 3;
    mask = (0xFF >> bitoffd);
    mask_end = (0xFF << ((8 - bitoff_end) & 7));

    pd = pdst;
    ps = psrc-test;

    for (i = 0; i < dy_t; i++) {
      tmp_start = pd[i*img_strided];
      tmp_end = pd[i*img_strided+amount-1];
      s0 = ps[i*img_strides];
      for (j = 0; j < amount; j++) {
        s1 = ps[i*img_strides+j+1];
        pd[i*img_strided + j] = (s0 << shift1) | (s1 >> shift2);
        s0 = s1;
      }

      pd[i*img_strided] = (tmp_start & (~mask)) | (pd[i*img_strided] & mask);
      pd[i*img_strided+amount-1] = (tmp_end & (~mask_end)) |
                                   (pd[i*img_strided+amount-1] & mask_end);
    }

    pd = pdst + (img_height-1)*img_strided;
    ps = psrc + (img_height-1)*img_strides - test;

    for (i = 0; i < dy_b; i++) {
      tmp_start = pd[-i*img_strided];
      tmp_end = pd[-i*img_strided+amount-1];
      s0 = ps[-i*img_strides];
      for (j = 0; j < amount; j++) {
       s1 = ps[-i*img_strides+j+1];
       pd[-i*img_strided + j] = (s0 << shift1) | (s1 >> shift2);
       s0 = s1;
      }

      pd[-i*img_strided] = (tmp_start & (~mask)) | (pd[-i*img_strided] & mask);
      pd[-i*img_strided+amount-1] = (tmp_end & (~mask_end)) |
                                   (pd[-i*img_strided+amount-1] & mask_end);
    }
  }

  return MLIB_SUCCESS;
}

/***************************************************************/