! * \brief pixConvert1To2() * * \param[in] pixd [optional] 2 bpp, can be null * \param[in] pixs 1 bpp * \param[in] val0 2 bit value to be used for 0s in pixs * \param[in] val1 2 bit value to be used for 1s in pixs * \return pixd 2 bpp * * * Notes: * (1) If pixd is null, a new pix is made. * (2) If pixd is not null, it must be of equal width an
| 2120 | * </pre> |
| 2121 | */ |
| 2122 | PIX * |
| 2123 | pixConvert1To2(PIX *pixd, |
| 2124 | PIX *pixs, |
| 2125 | l_int32 val0, |
| 2126 | l_int32 val1) |
| 2127 | { |
| 2128 | l_int32 w, h, i, j, byteval, nbytes, wpls, wpld; |
| 2129 | l_uint8 val[2]; |
| 2130 | l_uint32 index; |
| 2131 | l_uint16 *tab; |
| 2132 | l_uint32 *datas, *datad, *lines, *lined; |
| 2133 | |
| 2134 | PROCNAME("pixConvert1To2"); |
| 2135 | |
| 2136 | if (!pixs) |
| 2137 | return (PIX *)ERROR_PTR("pixs not defined", procName, pixd); |
| 2138 | if (pixGetDepth(pixs) != 1) |
| 2139 | return (PIX *)ERROR_PTR("pixs not 1 bpp", procName, pixd); |
| 2140 | |
| 2141 | pixGetDimensions(pixs, &w, &h, NULL); |
| 2142 | if (pixd) { |
| 2143 | if (w != pixGetWidth(pixd) || h != pixGetHeight(pixd)) |
| 2144 | return (PIX *)ERROR_PTR("pix sizes unequal", procName, pixd); |
| 2145 | if (pixGetDepth(pixd) != 2) |
| 2146 | return (PIX *)ERROR_PTR("pixd not 2 bpp", procName, pixd); |
| 2147 | } else { |
| 2148 | if ((pixd = pixCreate(w, h, 2)) == NULL) |
| 2149 | return (PIX *)ERROR_PTR("pixd not made", procName, NULL); |
| 2150 | } |
| 2151 | pixCopyResolution(pixd, pixs); |
| 2152 | pixCopyInputFormat(pixd, pixs); |
| 2153 | |
| 2154 | /* Use a table to convert 8 src bits to 16 dest bits */ |
| 2155 | tab = (l_uint16 *)LEPT_CALLOC(256, sizeof(l_uint16)); |
| 2156 | val[0] = val0; |
| 2157 | val[1] = val1; |
| 2158 | for (index = 0; index < 256; index++) { |
| 2159 | tab[index] = (val[(index >> 7) & 1] << 14) | |
| 2160 | (val[(index >> 6) & 1] << 12) | |
| 2161 | (val[(index >> 5) & 1] << 10) | |
| 2162 | (val[(index >> 4) & 1] << 8) | |
| 2163 | (val[(index >> 3) & 1] << 6) | |
| 2164 | (val[(index >> 2) & 1] << 4) | |
| 2165 | (val[(index >> 1) & 1] << 2) | val[index & 1]; |
| 2166 | } |
| 2167 | |
| 2168 | datas = pixGetData(pixs); |
| 2169 | wpls = pixGetWpl(pixs); |
| 2170 | datad = pixGetData(pixd); |
| 2171 | wpld = pixGetWpl(pixd); |
| 2172 | nbytes = (w + 7) / 8; |
| 2173 | for (i = 0; i < h; i++) { |
| 2174 | lines = datas + i * wpls; |
| 2175 | lined = datad + i * wpld; |
| 2176 | for (j = 0; j < nbytes; j++) { |
| 2177 | byteval = GET_DATA_BYTE(lines, j); |
| 2178 | SET_DATA_TWO_BYTES(lined, j, tab[byteval]); |
| 2179 | } |
no test coverage detected