! * \brief pixMultMatrixColor() * * \param[in] pixs colormapped or rgb * \param[in] kel kernel 3x3 matrix of floats * \return pixd colormapped or rgb, or NULL on error * * * Notes: * (1) The kernel is a data structure used mostly for floating point * convolution. Here it is a 3x3 matrix of floats that are used * to transform the pixel values
| 2101 | * </pre> |
| 2102 | */ |
| 2103 | PIX * |
| 2104 | pixMultMatrixColor(PIX *pixs, |
| 2105 | L_KERNEL *kel) |
| 2106 | { |
| 2107 | l_int32 i, j, index, kw, kh, w, h, d, wpls, wpld; |
| 2108 | l_int32 ncolors, rval, gval, bval, nrval, ngval, nbval; |
| 2109 | l_uint32 nval; |
| 2110 | l_uint32 *datas, *datad, *lines, *lined; |
| 2111 | l_float32 v[9]; /* use linear array for convenience */ |
| 2112 | PIX *pixd; |
| 2113 | PIXCMAP *cmap; |
| 2114 | |
| 2115 | PROCNAME("pixMultMatrixColor"); |
| 2116 | |
| 2117 | if (!pixs) |
| 2118 | return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); |
| 2119 | if (!kel) |
| 2120 | return (PIX *)ERROR_PTR("kel not defined", procName, NULL); |
| 2121 | kernelGetParameters(kel, &kw, &kh, NULL, NULL); |
| 2122 | if (kw != 3 || kh != 3) |
| 2123 | return (PIX *)ERROR_PTR("matrix not 3x3", procName, NULL); |
| 2124 | pixGetDimensions(pixs, &w, &h, &d); |
| 2125 | cmap = pixGetColormap(pixs); |
| 2126 | if (!cmap && d != 32) |
| 2127 | return (PIX *)ERROR_PTR("pixs not cmapped or 32 bpp", procName, NULL); |
| 2128 | |
| 2129 | for (i = 0, index = 0; i < 3; i++) |
| 2130 | for (j = 0; j < 3; j++, index++) |
| 2131 | kernelGetElement(kel, i, j, v + index); |
| 2132 | |
| 2133 | if (cmap) { |
| 2134 | if ((pixd = pixCopy(NULL, pixs)) == NULL) |
| 2135 | return (PIX *)ERROR_PTR("pixd not made", procName, NULL); |
| 2136 | cmap = pixGetColormap(pixd); |
| 2137 | ncolors = pixcmapGetCount(cmap); |
| 2138 | for (i = 0; i < ncolors; i++) { |
| 2139 | pixcmapGetColor(cmap, i, &rval, &gval, &bval); |
| 2140 | nrval = (l_int32)(v[0] * rval + v[1] * gval + v[2] * bval); |
| 2141 | ngval = (l_int32)(v[3] * rval + v[4] * gval + v[5] * bval); |
| 2142 | nbval = (l_int32)(v[6] * rval + v[7] * gval + v[8] * bval); |
| 2143 | nrval = L_MAX(0, L_MIN(255, nrval)); |
| 2144 | ngval = L_MAX(0, L_MIN(255, ngval)); |
| 2145 | nbval = L_MAX(0, L_MIN(255, nbval)); |
| 2146 | pixcmapResetColor(cmap, i, nrval, ngval, nbval); |
| 2147 | } |
| 2148 | return pixd; |
| 2149 | } |
| 2150 | |
| 2151 | if ((pixd = pixCreateTemplateNoInit(pixs)) == NULL) |
| 2152 | return (PIX *)ERROR_PTR("pixd not made", procName, NULL); |
| 2153 | datas = pixGetData(pixs); |
| 2154 | datad = pixGetData(pixd); |
| 2155 | wpls = pixGetWpl(pixs); |
| 2156 | wpld = pixGetWpl(pixd); |
| 2157 | for (i = 0; i < h; i++) { |
| 2158 | lines = datas + i * wpls; |
| 2159 | lined = datad + i * wpld; |
| 2160 | for (j = 0; j < w; j++) { |
nothing calls this directly
no test coverage detected