| 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_ImageConvClearEdge_BIt - Set edge of an bit type image to a specific |
| 30 | * color. |
| 31 | * |
| 32 | * SYNOPSIS |
| 33 | * mlib_status mlib_ImageConvClearEdge_Bit(mlib_image *img, |
| 34 | * mlib_s32 dx_l, |
| 35 | * mlib_32 dx_r, |
| 36 | * mlib_s32 dy_t, |
| 37 | * mlib_32 dy_b, |
| 38 | * const mlib_s32 *color, |
| 39 | * mlib_s32 cmask); |
| 40 | * |
| 41 | * ARGUMENT |
| 42 | * img Pointer to an image. |
| 43 | * dx_l Number of columns on the left side of the |
| 44 | * image to be cleared. |
| 45 | * dx_r Number of columns on the right side of the |
| 46 | * image to be cleared. |
| 47 | * dy_t Number of rows on the top edge of the |
| 48 | * image to be cleared. |
| 49 | * dy_b Number of rows on the top edge of the |
| 50 | * image to be cleared. |
| 51 | * color Pointer to the color that the edges are set to. |
| 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 | * img can have 1 channels of MLIB_BIT data type. |
| 58 | * |
| 59 | * DESCRIPTION |
| 60 | * Set edge of an image to a specific color. |
| 61 | * The unselected channels are not overwritten. |
| 62 | * If src and dst have just one channel, |
| 63 | * cmask is ignored. |
| 64 | */ |
| 65 | |
| 66 | #include "mlib_image.h" |
| 67 | #include "mlib_ImageConvEdge.h" |
| 68 | |
| 69 | /***************************************************************/ |
| 70 | mlib_status mlib_ImageConvClearEdge_Bit(mlib_image *img, |
| 71 | mlib_s32 dx_l, |
| 72 | mlib_s32 dx_r, |
| 73 | mlib_s32 dy_t, |
| 74 | mlib_s32 dy_b, |
| 75 | const mlib_s32 *color, |
| 76 | mlib_s32 cmask) |
| 77 | { |
| 78 | mlib_u8 *pimg = mlib_ImageGetData(img), *pd; |
| 79 | mlib_s32 img_height = mlib_ImageGetHeight(img); |
| 80 | mlib_s32 img_width = mlib_ImageGetWidth(img); |
| 81 | mlib_s32 img_stride = mlib_ImageGetStride(img); |
| 82 | mlib_s32 bitoff = mlib_ImageGetBitOffset(img); |
| 83 | mlib_s32 bitoff_end; |
| 84 | mlib_u8 color_i, mask, mask_end, tmp_color; |
| 85 | mlib_u8 tmp_start, tmp_end; |
| 86 | mlib_s32 i, j, amount; |
| 87 | |
| 88 | if ((mlib_ImageGetType(img) != MLIB_BIT) || (mlib_ImageGetChannels(img) != 1)) |
| 89 | return MLIB_FAILURE; |
| 90 | |
| 91 | color_i = (mlib_u8)(color[0] & 1); |
| 92 | color_i |= (color_i << 1); |
| 93 | color_i |= (color_i << 2); |
| 94 | color_i |= (color_i << 4); |
| 95 | |
| 96 | pd = pimg; |
| 97 | |
| 98 | if (dx_l > 0) { |
| 99 | if (bitoff + dx_l <= 8) { |
| 100 | mask = (0xFF >> bitoff) & (0xFF << ((8 - (bitoff + dx_l)) & 7)); |
| 101 | tmp_color = color_i & mask; |
| 102 | mask = ~mask; |
| 103 | |
| 104 | for (i = dy_t; i < (img_height - dy_b); i++) { |
| 105 | pd[i*img_stride] = (pd[i*img_stride] & mask) | tmp_color; |
| 106 | } |
| 107 | |
| 108 | } else { |
| 109 | mask = (0xFF >> bitoff); |
| 110 | tmp_color = color_i & mask; |
| 111 | mask = ~mask; |
| 112 | |
| 113 | for (i = dy_t; i < (img_height - dy_b); i++) { |
| 114 | pd[i*img_stride] = (pd[i*img_stride] & mask) | tmp_color; |
| 115 | } |
| 116 | |
| 117 | amount = (bitoff + dx_l + 7) >> 3; |
| 118 | mask = (0xFF << ((8 - (bitoff + dx_l)) & 7)); |
| 119 | tmp_color = color_i & mask; |
| 120 | mask = ~mask; |
| 121 | |
| 122 | for (j = 1; j < amount - 1; j++) { |
| 123 | for (i = dy_t; i < (img_height - dy_b); i++) { |
| 124 | pd[i*img_stride + j] = color_i; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | for (i = dy_t; i < (img_height - dy_b); i++) { |
| 129 | pd[i*img_stride + amount - 1] = (pd[i*img_stride + amount - 1] & mask) | tmp_color; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (dx_r > 0) { |
| 135 | pd = pimg + (img_width + bitoff - dx_r) / 8; |
| 136 | bitoff = (img_width + bitoff - dx_r) & 7; |
| 137 | |
| 138 | if (bitoff + dx_r <= 8) { |
| 139 | mask = (0xFF >> bitoff) & (0xFF << ((8 - (bitoff + dx_r)) & 7)); |
| 140 | tmp_color = color_i & mask; |
| 141 | mask = ~mask; |
| 142 | |
| 143 | for (i = dy_t; i < (img_height - dy_b); i++) { |
| 144 | pd[i*img_stride] = (pd[i*img_stride] & mask) | tmp_color; |
| 145 | } |
| 146 | |
| 147 | } else { |
| 148 | mask = (0xFF >> bitoff); |
| 149 | tmp_color = color_i & mask; |
| 150 | mask = ~mask; |
| 151 | |
| 152 | for (i = dy_t; i < (img_height - dy_b); i++) { |
| 153 | pd[i*img_stride] = (pd[i*img_stride] & mask) | tmp_color; |
| 154 | } |
| 155 | |
| 156 | amount = (bitoff + dx_r + 7) >> 3; |
| 157 | mask = (0xFF << ((8 - (bitoff + dx_r)) & 7)); |
| 158 | tmp_color = color_i & mask; |
| 159 | mask = ~mask; |
| 160 | |
| 161 | for (j = 1; j < amount - 1; j++) { |
| 162 | for (i = dy_t; i < (img_height - dy_b); i++) { |
| 163 | pd[i*img_stride + j] = color_i; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | for (i = dy_t; i < (img_height - dy_b); i++) { |
| 168 | pd[i*img_stride + amount - 1] = (pd[i*img_stride + amount - 1] & mask) | tmp_color; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | bitoff = mlib_ImageGetBitOffset(img); |
| 174 | bitoff_end = (bitoff + img_width) & 7; |
| 175 | amount = (bitoff + img_width + 7) >> 3; |
| 176 | mask = (0xFF >> bitoff); |
| 177 | mask_end = (0xFF << ((8 - bitoff_end) & 7)); |
| 178 | |
| 179 | pd = pimg; |
| 180 | |
| 181 | for (i = 0; i < dy_t; i++) { |
| 182 | tmp_start = pd[i*img_stride]; |
| 183 | tmp_end = pd[i*img_stride+amount-1]; |
| 184 | for (j = 0; j < amount; j++) { |
| 185 | pd[i*img_stride + j] = color_i; |
| 186 | } |
| 187 | |
| 188 | pd[i*img_stride] = (tmp_start & (~mask)) | (pd[i*img_stride] & mask); |
| 189 | pd[i*img_stride+amount-1] = (tmp_end & (~mask_end)) | |
| 190 | (pd[i*img_stride+amount-1] & mask_end); |
| 191 | } |
| 192 | |
| 193 | pd = pimg + (img_height-1)*img_stride; |
| 194 | |
| 195 | for (i = 0; i < dy_b; i++) { |
| 196 | tmp_start = pd[-i*img_stride]; |
| 197 | tmp_end = pd[-i*img_stride+amount-1]; |
| 198 | for (j = 0; j < amount; j++) { |
| 199 | pd[-i*img_stride + j] = color_i; |
| 200 | } |
| 201 | |
| 202 | pd[-i*img_stride] = (tmp_start & (~mask)) | (pd[-i*img_stride] & mask); |
| 203 | pd[-i*img_stride+amount-1] = (tmp_end & (~mask_end)) | |
| 204 | (pd[-i*img_stride+amount-1] & mask_end); |
| 205 | } |
| 206 | |
| 207 | return MLIB_SUCCESS; |
| 208 | } |
| 209 | |
| 210 | /***************************************************************/ |
| 211 | |