85 #include "allheaders.h" 110 PROCNAME(
"kernelCreate");
113 return (
L_KERNEL *)ERROR_PTR(
"kel not made", procName, NULL);
118 return (
L_KERNEL *)ERROR_PTR(
"data not allocated", procName, NULL);
137 PROCNAME(
"kernelDestroy");
140 L_WARNING(
"ptr address is NULL!\n", procName);
143 if ((kel = *pkel) == NULL)
146 for (i = 0; i < kel->
sy; i++)
147 LEPT_FREE(kel->
data[i]);
148 LEPT_FREE(kel->
data);
165 l_int32 i, j, sx, sy, cx, cy;
168 PROCNAME(
"kernelCopy");
171 return (
L_KERNEL *)ERROR_PTR(
"kels not defined", procName, NULL);
175 return (
L_KERNEL *)ERROR_PTR(
"keld not made", procName, NULL);
178 for (i = 0; i < sy; i++)
179 for (j = 0; j < sx; j++)
180 keld->
data[i][j] = kels->
data[i][j];
204 PROCNAME(
"kernelGetElement");
207 return ERROR_INT(
"&val not defined", procName, 1);
210 return ERROR_INT(
"kernel not defined", procName, 1);
211 if (row < 0 || row >= kel->
sy)
212 return ERROR_INT(
"kernel row out of bounds", procName, 1);
213 if (col < 0 || col >= kel->
sx)
214 return ERROR_INT(
"kernel col out of bounds", procName, 1);
216 *pval = kel->
data[row][col];
236 PROCNAME(
"kernelSetElement");
239 return ERROR_INT(
"kel not defined", procName, 1);
240 if (row < 0 || row >= kel->
sy)
241 return ERROR_INT(
"kernel row out of bounds", procName, 1);
242 if (col < 0 || col >= kel->
sx)
243 return ERROR_INT(
"kernel col out of bounds", procName, 1);
245 kel->
data[row][col] = val;
264 PROCNAME(
"kernelGetParameters");
271 return ERROR_INT(
"kernel not defined", procName, 1);
272 if (psy) *psy = kel->
sy;
273 if (psx) *psx = kel->
sx;
274 if (pcy) *pcy = kel->
cy;
275 if (pcx) *pcx = kel->
cx;
292 PROCNAME(
"kernelSetOrigin");
295 return ERROR_INT(
"kel not defined", procName, 1);
313 l_int32 sx, sy, i, j;
315 PROCNAME(
"kernelGetSum");
318 return ERROR_INT(
"&sum not defined", procName, 1);
321 return ERROR_INT(
"kernel not defined", procName, 1);
324 for (i = 0; i < sy; i++) {
325 for (j = 0; j < sx; j++) {
326 *psum += kel->
data[i][j];
346 l_int32 sx, sy, i, j;
347 l_float32 val, minval, maxval;
349 PROCNAME(
"kernelGetMinmax");
352 return ERROR_INT(
"neither &min nor &max defined", procName, 1);
353 if (pmin) *pmin = 0.0;
354 if (pmax) *pmax = 0.0;
356 return ERROR_INT(
"kernel not defined", procName, 1);
360 maxval = -10000000.0;
361 for (i = 0; i < sy; i++) {
362 for (j = 0; j < sx; j++) {
363 val = kel->
data[i][j];
401 l_int32 i, j, sx, sy, cx, cy;
402 l_float32 sum, factor;
405 PROCNAME(
"kernelNormalize");
408 return (
L_KERNEL *)ERROR_PTR(
"kels not defined", procName, NULL);
411 if (L_ABS(sum) < 0.00001) {
412 L_WARNING(
"null sum; not normalizing; returning a copy\n", procName);
418 return (
L_KERNEL *)ERROR_PTR(
"keld not made", procName, NULL);
422 factor = normsum / sum;
423 for (i = 0; i < sy; i++)
424 for (j = 0; j < sx; j++)
425 keld->
data[i][j] = factor * kels->
data[i][j];
446 l_int32 i, j, sx, sy, cx, cy;
449 PROCNAME(
"kernelInvert");
452 return (
L_KERNEL *)ERROR_PTR(
"kels not defined", procName, NULL);
456 return (
L_KERNEL *)ERROR_PTR(
"keld not made", procName, NULL);
457 keld->
cy = sy - 1 - cy;
458 keld->
cx = sx - 1 - cx;
460 for (i = 0; i < sy; i++)
461 for (j = 0; j < sx; j++)
462 keld->
data[i][j] = kels->
data[sy - 1 - i][sx - 1 - j];
492 PROCNAME(
"create2dFloatArray");
494 if ((array = (l_float32 **)LEPT_CALLOC(sy,
sizeof(l_float32 *))) == NULL)
495 return (l_float32 **)ERROR_PTR(
"ptr array not made", procName, NULL);
497 for (i = 0; i < sy; i++)
498 array[i] = (l_float32 *)LEPT_CALLOC(sx,
sizeof(l_float32));
518 PROCNAME(
"kernelRead");
521 return (
L_KERNEL *)ERROR_PTR(
"fname not defined", procName, NULL);
524 return (
L_KERNEL *)ERROR_PTR(
"stream not opened", procName, NULL);
527 return (
L_KERNEL *)ERROR_PTR(
"kel not returned", procName, NULL);
544 l_int32 sy, sx, cy, cx, i, j, ret, version, ignore;
547 PROCNAME(
"kernelReadStream");
550 return (
L_KERNEL *)ERROR_PTR(
"stream not defined", procName, NULL);
552 ret = fscanf(fp,
" Kernel Version %d\n", &version);
554 return (
L_KERNEL *)ERROR_PTR(
"not a kernel file", procName, NULL);
555 if (version != KERNEL_VERSION_NUMBER)
556 return (
L_KERNEL *)ERROR_PTR(
"invalid kernel version", procName, NULL);
558 if (fscanf(fp,
" sy = %d, sx = %d, cy = %d, cx = %d\n",
559 &sy, &sx, &cy, &cx) != 4)
560 return (
L_KERNEL *)ERROR_PTR(
"dimensions not read", procName, NULL);
563 return (
L_KERNEL *)ERROR_PTR(
"kel not made", procName, NULL);
566 for (i = 0; i < sy; i++) {
567 for (j = 0; j < sx; j++)
568 ignore = fscanf(fp,
"%15f", &kel->
data[i][j]);
569 ignore = fscanf(fp,
"\n");
571 ignore = fscanf(fp,
"\n");
590 PROCNAME(
"kernelWrite");
593 return ERROR_INT(
"fname not defined", procName, 1);
595 return ERROR_INT(
"kel not defined", procName, 1);
598 return ERROR_INT(
"stream not opened", procName, 1);
617 l_int32 sx, sy, cx, cy, i, j;
619 PROCNAME(
"kernelWriteStream");
622 return ERROR_INT(
"stream not defined", procName, 1);
624 return ERROR_INT(
"kel not defined", procName, 1);
627 fprintf(fp,
" Kernel Version %d\n", KERNEL_VERSION_NUMBER);
628 fprintf(fp,
" sy = %d, sx = %d, cy = %d, cx = %d\n", sy, sx, cy, cx);
629 for (i = 0; i < sy; i++) {
630 for (j = 0; j < sx; j++)
631 fprintf(fp,
"%15.4f", kel->
data[i][j]);
672 l_int32 n, i, j, index;
677 PROCNAME(
"kernelCreateFromString");
680 return (
L_KERNEL *)ERROR_PTR(
"height must be > 0", procName, NULL);
682 return (
L_KERNEL *)ERROR_PTR(
"width must be > 0", procName, NULL);
683 if (cy < 0 || cy >= h)
684 return (
L_KERNEL *)ERROR_PTR(
"cy invalid", procName, NULL);
685 if (cx < 0 || cx >= w)
686 return (
L_KERNEL *)ERROR_PTR(
"cx invalid", procName, NULL);
695 fprintf(stderr,
"w = %d, h = %d, num ints = %d\n", w, h, n);
696 return (
L_KERNEL *)ERROR_PTR(
"invalid integer data", procName, NULL);
700 for (i = 0; i < h; i++) {
701 for (j = 0; j < w; j++) {
754 char *filestr, *line;
755 l_int32 nlines, i, j, first, index, w, h, cx, cy, n;
762 PROCNAME(
"kernelCreateFromFile");
765 return (
L_KERNEL *)ERROR_PTR(
"filename not defined", procName, NULL);
767 if ((filestr = (
char *)
l_binaryRead(filename, &size)) == NULL)
768 return (
L_KERNEL *)ERROR_PTR(
"file not found", procName, NULL);
771 return (
L_KERNEL *)ERROR_PTR(
"file is empty", procName, NULL);
779 for (i = 0, first = 0; i < nlines; i++) {
781 if (line[0] !=
'#') {
789 if (sscanf(line,
"%d %d", &h, &w) != 2) {
791 return (
L_KERNEL *)ERROR_PTR(
"error reading h,w", procName, NULL);
794 if (sscanf(line,
"%d %d", &cy, &cx) != 2) {
796 return (
L_KERNEL *)ERROR_PTR(
"error reading cy,cx", procName, NULL);
803 for (i = first + 2; i < nlines; i++) {
805 if (line[0] ==
'\0' || line[0] ==
'\n' || line[0] ==
'#')
816 fprintf(stderr,
"w = %d, h = %d, num ints = %d\n", w, h, n);
817 return (
L_KERNEL *)ERROR_PTR(
"invalid integer data", procName, NULL);
823 for (i = 0; i < h; i++) {
824 for (j = 0; j < w; j++) {
856 l_int32 i, j, w, h, d;
860 PROCNAME(
"kernelCreateFromPix");
863 return (
L_KERNEL *)ERROR_PTR(
"pix not defined", procName, NULL);
866 return (
L_KERNEL *)ERROR_PTR(
"pix not 8 bpp", procName, NULL);
867 if (cy < 0 || cx < 0 || cy >= h || cx >= w)
868 return (
L_KERNEL *)ERROR_PTR(
"(cy, cx) invalid", procName, NULL);
872 for (i = 0; i < h; i++) {
873 for (j = 0; j < w; j++) {
917 l_int32 i, j, w, h, sx, sy, cx, cy, width, x0, y0;
919 l_float32 minval, maxval, max, val, norm;
920 PIX *pixd, *pixt0, *pixt1;
922 PROCNAME(
"kernelDisplayInPix");
925 return (
PIX *)ERROR_PTR(
"kernel not defined", procName, NULL);
930 max = L_MAX(maxval, -minval);
932 return (
PIX *)ERROR_PTR(
"kernel elements all 0.0", procName, NULL);
933 norm = 255. / (l_float32)max;
936 if (size == 1 && gthick == 0) {
938 for (i = 0; i < sy; i++) {
939 for (j = 0; j < sx; j++) {
941 normval = (l_int32)(norm * L_ABS(val));
950 L_WARNING(
"size < 17; setting to 17\n", procName);
956 L_WARNING(
"grid thickness < 2; setting to 2\n", procName);
960 w = size * sx + gthick * (sx + 1);
961 h = size * sy + gthick * (sy + 1);
965 for (i = 0; i <= sy; i++)
967 w - 1, gthick / 2 + i * (size + gthick),
969 for (j = 0; j <= sx; j++)
971 gthick / 2 + j * (size + gthick), h - 1,
982 size / 2, (l_int32)(0.88 * size),
985 (l_int32)(0.85 * size), size / 2,
987 pixRasterop(pixt1, size / 2 - width, size / 2 - width,
992 for (i = 0; i < sy; i++) {
994 for (j = 0; j < sx; j++) {
996 normval = (l_int32)(norm * L_ABS(val));
998 if (i == cy && j == cx)
1000 x0 += size + gthick;
1002 y0 += size + gthick;
1030 char *newstr, *head;
1035 PROCNAME(
"parseStringForNumbers");
1038 return (
NUMA *)ERROR_PTR(
"str not defined", procName, NULL);
1046 while ((head =
strtokSafe(NULL, seps, &tail)) != NULL) {
1088 PROCNAME(
"makeFlatKernel");
1091 return (
L_KERNEL *)ERROR_PTR(
"kel not made", procName, NULL);
1093 normval = 1.0 / (l_float32)(height * width);
1094 for (i = 0; i < height; i++) {
1095 for (j = 0; j < width; j++) {
1129 l_int32 sx, sy, i, j;
1133 PROCNAME(
"makeGaussianKernel");
1135 sx = 2 * halfwidth + 1;
1136 sy = 2 * halfheight + 1;
1138 return (
L_KERNEL *)ERROR_PTR(
"kel not made", procName, NULL);
1140 for (i = 0; i < sy; i++) {
1141 for (j = 0; j < sx; j++) {
1142 val = expf(-(l_float32)((i - halfheight) * (i - halfheight) +
1143 (j - halfwidth) * (j - halfwidth)) /
1144 (2. * stdev * stdev));
1185 PROCNAME(
"makeGaussianKernelSep");
1187 if (!pkelx || !pkely)
1188 return ERROR_INT(
"&kelx and &kely not defined", procName, 1);
1228 l_int32 sx, sy, i, j;
1229 l_float32 pi, squaredist, highnorm, lownorm, val;
1232 PROCNAME(
"makeDoGKernel");
1234 sx = 2 * halfwidth + 1;
1235 sy = 2 * halfheight + 1;
1237 return (
L_KERNEL *)ERROR_PTR(
"kel not made", procName, NULL);
1241 for (i = 0; i < sy; i++) {
1242 for (j = 0; j < sx; j++) {
1243 squaredist = (l_float32)((i - halfheight) * (i - halfheight) +
1244 (j - halfwidth) * (j - halfwidth));
1245 highnorm = 1. / (2 * stdev * stdev);
1246 lownorm = highnorm / (ratio * ratio);
1247 val = (highnorm / pi) * expf(-(highnorm * squaredist))
1248 - (lownorm / pi) * expf(-(lownorm * squaredist));
l_int32 kernelWriteStream(FILE *fp, L_KERNEL *kel)
kernelWriteStream()
PIX * kernelDisplayInPix(L_KERNEL *kel, l_int32 size, l_int32 gthick)
kernelDisplayInPix()
l_int32 pixSetMaskedGeneral(PIX *pixd, PIX *pixm, l_uint32 val, l_int32 x, l_int32 y)
pixSetMaskedGeneral()
l_int32 numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
l_int32 makeGaussianKernelSep(l_int32 halfheight, l_int32 halfwidth, l_float32 stdev, l_float32 max, L_KERNEL **pkelx, L_KERNEL **pkely)
makeGaussianKernelSep()
L_KERNEL * makeGaussianKernel(l_int32 halfheight, l_int32 halfwidth, l_float32 stdev, l_float32 max)
makeGaussianKernel()
NUMA * parseStringForNumbers(const char *str, const char *seps)
parseStringForNumbers()
l_int32 pixRenderLine(PIX *pix, l_int32 x1, l_int32 y1, l_int32 x2, l_int32 y2, l_int32 width, l_int32 op)
pixRenderLine()
l_int32 pixGetPixel(PIX *pix, l_int32 x, l_int32 y, l_uint32 *pval)
pixGetPixel()
char * stringNew(const char *src)
stringNew()
l_int32 kernelGetSum(L_KERNEL *kel, l_float32 *psum)
kernelGetSum()
L_KERNEL * kernelCreateFromPix(PIX *pix, l_int32 cy, l_int32 cx)
kernelCreateFromPix()
l_int32 numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
l_int32 numaJoin(NUMA *nad, NUMA *nas, l_int32 istart, l_int32 iend)
numaJoin()
NUMA * numaCreate(l_int32 n)
numaCreate()
l_int32 kernelGetElement(L_KERNEL *kel, l_int32 row, l_int32 col, l_float32 *pval)
kernelGetElement()
l_int32 pixRasterop(PIX *pixd, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op, PIX *pixs, l_int32 sx, l_int32 sy)
pixRasterop()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_int32 kernelSetElement(L_KERNEL *kel, l_int32 row, l_int32 col, l_float32 val)
kernelSetElement()
L_KERNEL * kernelCopy(L_KERNEL *kels)
kernelCopy()
l_int32 kernelGetMinMax(L_KERNEL *kel, l_float32 *pmin, l_float32 *pmax)
kernelGetMinMax()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
void pixDestroy(PIX **ppix)
pixDestroy()
L_KERNEL * kernelReadStream(FILE *fp)
kernelReadStream()
L_KERNEL * kernelNormalize(L_KERNEL *kels, l_float32 normsum)
kernelNormalize()
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
l_int32 pixPaintThroughMask(PIX *pixd, PIX *pixm, l_int32 x, l_int32 y, l_uint32 val)
pixPaintThroughMask()
void numaDestroy(NUMA **pna)
numaDestroy()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
L_KERNEL * kernelInvert(L_KERNEL *kels)
kernelInvert()
void kernelDestroy(L_KERNEL **pkel)
kernelDestroy()
FILE * fopenReadStream(const char *filename)
fopenReadStream()
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
l_int32 pixSetPixel(PIX *pix, l_int32 x, l_int32 y, l_uint32 val)
pixSetPixel()
l_int32 kernelGetParameters(L_KERNEL *kel, l_int32 *psy, l_int32 *psx, l_int32 *pcy, l_int32 *pcx)
kernelGetParameters()
L_KERNEL * kernelCreateFromString(l_int32 h, l_int32 w, l_int32 cy, l_int32 cx, const char *kdata)
kernelCreateFromString()
L_KERNEL * makeFlatKernel(l_int32 height, l_int32 width, l_int32 cy, l_int32 cx)
makeFlatKernel()
L_KERNEL * kernelRead(const char *fname)
kernelRead()
char * strtokSafe(char *cstr, const char *seps, char **psaveptr)
strtokSafe()
L_KERNEL * kernelCreateFromFile(const char *filename)
kernelCreateFromFile()
l_int32 pixSetAll(PIX *pix)
pixSetAll()
l_int32 kernelSetOrigin(L_KERNEL *kel, l_int32 cy, l_int32 cx)
kernelSetOrigin()
l_int32 pixGetDimensions(PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
l_int32 kernelWrite(const char *fname, L_KERNEL *kel)
kernelWrite()
L_KERNEL * makeDoGKernel(l_int32 halfheight, l_int32 halfwidth, l_float32 stdev, l_float32 ratio)
makeDoGKernel()
l_float32 ** create2dFloatArray(l_int32 sy, l_int32 sx)
create2dFloatArray()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
L_KERNEL * kernelCreate(l_int32 height, l_int32 width)
kernelCreate()