540l_int32 sy, sx, cy, cx, i, j, ret, version, ignore;
544 return (
L_KERNEL *)ERROR_PTR(
"stream not defined", __func__, NULL);
546 ret = fscanf(fp,
" Kernel Version %d\n", &version);
548 return (
L_KERNEL *)ERROR_PTR(
"not a kernel file", __func__, NULL);
549 if (version != KERNEL_VERSION_NUMBER)
550 return (
L_KERNEL *)ERROR_PTR(
"invalid kernel version", __func__, NULL);
552 if (fscanf(fp,
" sy = %d, sx = %d, cy = %d, cx = %d\n",
553 &sy, &sx, &cy, &cx) != 4)
554 return (
L_KERNEL *)ERROR_PTR(
"dimensions not read", __func__, NULL);
555 if (sx > MaxArraySize || sy > MaxArraySize) {
556 L_ERROR(
"sx = %d or sy = %d > %d\n", __func__, sx, sy, MaxArraySize);
560 return (
L_KERNEL *)ERROR_PTR(
"kel not made", __func__, NULL);
563 for (i = 0; i < sy; i++) {
564 for (j = 0; j < sx; j++)
565 ignore = fscanf(fp,
"%15f", &kel->
data[i][j]);
566 ignore = fscanf(fp,
"\n");
568 ignore = fscanf(fp,
"\n");
746l_int32 nlines, i, j, first, index, w, h, cx, cy, n;
754 return (
L_KERNEL *)ERROR_PTR(
"filename not defined", __func__, NULL);
756 if ((filestr = (
char *)l_binaryRead(filename, &size)) == NULL)
757 return (
L_KERNEL *)ERROR_PTR_1(
"file not found",
758 filename, __func__, NULL);
761 return (
L_KERNEL *)ERROR_PTR_1(
"file is empty",
762 filename, __func__, NULL);
765 sa = sarrayCreateLinesFromString(filestr, 1);
767 nlines = sarrayGetCount(sa);
770 for (i = 0, first = 0; i < nlines; i++) {
771 line = sarrayGetString(sa, i,
L_NOCOPY);
772 if (line[0] !=
'#') {
779 line = sarrayGetString(sa, first,
L_NOCOPY);
780 if (sscanf(line,
"%d %d", &h, &w) != 2) {
782 return (
L_KERNEL *)ERROR_PTR(
"error reading h,w", __func__, NULL);
784 if (h > MaxArraySize || w > MaxArraySize) {
785 L_ERROR(
"h = %d or w = %d > %d\n", __func__, h, w, MaxArraySize);
789 line = sarrayGetString(sa, first + 1,
L_NOCOPY);
790 if (sscanf(line,
"%d %d", &cy, &cx) != 2) {
792 return (
L_KERNEL *)ERROR_PTR(
"error reading cy,cx", __func__, NULL);
799 for (i = first + 2; i < nlines; i++) {
800 line = sarrayGetString(sa, i,
L_NOCOPY);
801 if (line[0] ==
'\0' || line[0] ==
'\n' || line[0] ==
'#')
804 numaJoin(na, nat, 0, -1);
809 n = numaGetCount(na);
812 lept_stderr(
"w = %d, h = %d, num ints = %d\n", w, h, n);
813 return (
L_KERNEL *)ERROR_PTR(
"invalid integer data", __func__, NULL);
819 for (i = 0; i < h; i++) {
820 for (j = 0; j < w; j++) {
821 numaGetFValue(na, index, &val);
911l_int32 i, j, w, h, sx, sy, cx, cy, width, x0, y0;
913l_float32 minval, maxval, max, val, norm;
914PIX *pixd, *pixt0, *pixt1;
917 return (
PIX *)ERROR_PTR(
"kernel not defined", __func__, NULL);
922 max = L_MAX(maxval, -minval);
924 return (
PIX *)ERROR_PTR(
"kernel elements all 0.0", __func__, NULL);
925 norm = 255.f / (l_float32)max;
928 if (size == 1 && gthick == 0) {
929 pixd = pixCreate(sx, sy, 8);
930 for (i = 0; i < sy; i++) {
931 for (j = 0; j < sx; j++) {
933 normval = (l_int32)(norm * L_ABS(val));
934 pixSetPixel(pixd, j, i, normval);
942 L_WARNING(
"size < 17; setting to 17\n", __func__);
948 L_WARNING(
"grid thickness < 2; setting to 2\n", __func__);
952 w = size * sx + gthick * (sx + 1);
953 h = size * sy + gthick * (sy + 1);
954 pixd = pixCreate(w, h, 8);
957 for (i = 0; i <= sy; i++)
958 pixRenderLine(pixd, 0, gthick / 2 + i * (size + gthick),
959 w - 1, gthick / 2 + i * (size + gthick),
961 for (j = 0; j <= sx; j++)
962 pixRenderLine(pixd, gthick / 2 + j * (size + gthick), 0,
963 gthick / 2 + j * (size + gthick), h - 1,
967 pixt0 = pixCreate(size, size, 1);
971 pixt1 = pixCreate(size, size, 1);
973 pixRenderLine(pixt1, size / 2, (l_int32)(0.12 * size),
974 size / 2, (l_int32)(0.88 * size),
976 pixRenderLine(pixt1, (l_int32)(0.15 * size), size / 2,
977 (l_int32)(0.85 * size), size / 2,
979 pixRasterop(pixt1, size / 2 - width, size / 2 - width,
984 for (i = 0; i < sy; i++) {
986 for (j = 0; j < sx; j++) {
988 normval = (l_int32)(norm * L_ABS(val));
989 pixSetMaskedGeneral(pixd, pixt0, normval, x0, y0);
990 if (i == cy && j == cx)
991 pixPaintThroughMask(pixd, pixt1, x0, y0, 255 - normval);
1215l_int32 sx, sy, i, j;
1216l_float32 pi, squaredist, highnorm, lownorm, val;
1222 return (
L_KERNEL *)ERROR_PTR(
"kel not made", __func__, NULL);
1226 for (i = 0; i < sy; i++) {
1227 for (j = 0; j < sx; j++) {
1228 squaredist = (l_float32)((i - halfh) * (i - halfh) +
1229 (j - halfw) * (j - halfw));
1230 highnorm = 1.f / (2 * stdev * stdev);
1231 lownorm = highnorm / (ratio * ratio);
1232 val = (highnorm / pi) * expf(-(highnorm * squaredist))
1233 - (lownorm / pi) * expf(-(lownorm * squaredist));